guozhaoshun 6 роки тому
батько
коміт
9a91434191

+ 155 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/controller/UserTagController.java

@@ -0,0 +1,155 @@
+package cn.rankin.userservice.controller;
+
+import cn.rankin.common.utils.api.page.Page;
+import cn.rankin.common.utils.enums.BaseOrderEnum;
+import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.common.utils.util.ListUtil;
+import cn.rankin.data.api.app.vo.ItemVo;
+import cn.rankin.data.api.product.entity.MerchantProduct;
+import cn.rankin.data.api.user.dto.UserTagDTO;
+import cn.rankin.data.api.user.dto.UserTagSearchDTO;
+import cn.rankin.data.api.user.entity.UserTag;
+import cn.rankin.data.api.user.entity.UserTagProductRelation;
+import cn.rankin.userservice.service.UserTagProductRelationService;
+import cn.rankin.userservice.service.UserTagService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.util.CollectionUtils;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+@Slf4j
+@RestController
+@RequestMapping(value = "userTag")
+public class UserTagController {
+
+    @Autowired
+    private UserTagService userTagService;
+
+    @Autowired
+    private UserTagProductRelationService userTagProductRelationService;
+
+/*    @Autowired
+    private MerchantProductRepository merchantProductRepository;*/
+
+    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
+    public UserTag getUserTag(@PathVariable("id") String id) {
+
+        UserTag userTag = userTagService.getUserTag(id);
+
+        if(null != userTag){
+            String userTagId = userTag.getId();
+            List<UserTagProductRelation> userTagProductRelations = userTagProductRelationService.findByTagId(userTagId);
+            userTag.setRelationList(userTagProductRelations);
+        }
+
+        return userTag;
+    }
+
+    @RequestMapping(value = "/code/{code}", method = RequestMethod.GET)
+    public List<UserTag> findUserTagByCodeUid(@PathVariable("code") String code, @RequestParam("uid") String uid) {
+        List<UserTag> userTagList = userTagService.findUserTagByCodeUid(code, uid);
+
+        //标签下   课程数据
+        userTagList.forEach(userTag -> {
+            String userTagId = userTag.getId();
+            List<UserTagProductRelation> userTagProductRelations = userTagProductRelationService.findByTagId(userTagId);
+            userTag.setRelationList(userTagProductRelations);
+        });
+
+        return userTagList;
+    }
+
+    @RequestMapping(value = "/uid/{uid}", method = RequestMethod.GET)
+    public List<UserTag> findUserTagByUid(@PathVariable("uid") String uid) {
+        List<UserTag> userTagList = userTagService.findUserTagByUid(uid);
+
+        //标签下   课程数据
+        userTagList.forEach(userTag -> {
+            String userTagId = userTag.getId();
+            List<UserTagProductRelation> userTagProductRelations = userTagProductRelationService.findByTagId(userTagId);
+            userTag.setRelationList(userTagProductRelations);
+        });
+
+        return userTagList;
+    }
+
+    @RequestMapping(value = "/list", method = RequestMethod.GET)
+    public Page<UserTag> search(UserTagSearchDTO searchDTO) {
+        UserTag userTag = new UserTag();
+
+        String name = searchDTO.getName();
+        if (name != null) {
+            userTag.setName("%" + name + "%");
+        }
+        // sort
+        LinkedHashMap<String, BaseOrderEnum> sort = new LinkedHashMap<>();
+        sort.put("gmtModified", BaseOrderEnum.DESC);
+        return userTagService.search(userTag, searchDTO.getPageNo(), searchDTO.getPageSize(), sort);
+    }
+
+    @RequestMapping(method = RequestMethod.POST)
+    public UserTag create(@RequestBody UserTagDTO userTagDTO) {
+        UserTag userTag = userTagService.create(userTagDTO);
+        if(null == userTag){
+            return null;
+        }
+
+        String userTagId = userTag.getId();
+        List<String> productIdList = userTagDTO.getProductList();
+        userTagProductRelationService.updateByTagId(userTagId,productIdList);
+
+       /* List<MerchantProduct> merchantProductList = getMerchantProductList(userTagId);
+        userTag.setProductList(merchantProductList);*/
+        return userTag;
+    }
+
+    @RequestMapping(method = RequestMethod.PUT)
+    public UserTag update(@RequestBody UserTagDTO userTagDTO) {
+        UserTag result = userTagService.update(userTagDTO);
+        if(null == result){
+            return null;
+        }
+
+        String userTagId = result.getId();
+        List<String> productIdList = userTagDTO.getProductList();
+        userTagProductRelationService.updateByTagId(userTagId,productIdList);
+
+        /*List<MerchantProduct> merchantProductList = getMerchantProductList(userTagId);
+        result.setProductList(merchantProductList);*/
+
+        return result;
+    }
+
+    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
+    public boolean delete(@PathVariable("id") String id) {
+        return userTagService.delete(id);
+    }
+
+
+/*    public List<MerchantProduct> getMerchantProductList(String tagId) {
+        List<UserTagProductRelation> relationList = userTagProductRelationService.findByTagId(tagId, BaseStatusEnum.NORMAL);
+        if (CollectionUtils.isEmpty(relationList)) {
+            return new ArrayList<>();
+        }
+
+        List<String> productIdList = new ArrayList<>();
+        relationList.forEach(relation -> productIdList.add(relation.getPid()));
+        List<MerchantProduct> merchantProductList = merchantProductRepository.findByPidIn(productIdList);
+        Map<String, MerchantProduct> merchantProductMap = ListUtil.convert(merchantProductList, "pid", MerchantProduct.class);
+
+        List<MerchantProduct> sortMerchantProductList = new ArrayList<>();
+        for (String productId : productIdList) {
+            MerchantProduct merchantProduct = merchantProductMap.get(productId);
+            if (merchantProduct != null) {
+                sortMerchantProductList.add(merchantProduct);
+            }
+        }
+        return sortMerchantProductList;
+    }*/
+
+}

+ 40 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/repository/UserTagProductRelationRepository.java

@@ -0,0 +1,40 @@
+package cn.rankin.userservice.repository;
+
+import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.common.utils.jpa.BasicJpaRepository;
+import cn.rankin.data.api.product.entity.MerchantProductTagRelation;
+import cn.rankin.data.api.user.entity.UserTagProductRelation;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
+
+import java.util.List;
+
+public interface UserTagProductRelationRepository extends BasicJpaRepository<UserTagProductRelation, String> {
+
+    @Query(value = "select r from UserTagProductRelation r where r.tagId = ?1 and r.status = ?2 order by r.sort")
+    List<UserTagProductRelation> findByTagId(String tagId, BaseStatusEnum status);
+
+    List<UserTagProductRelation> findByTagId(String tagId);
+
+    List<UserTagProductRelation> findByTagIdIn(List<String> tagIds);
+
+    @Query(value = "select r from UserTagProductRelation r where r.pid in (?1) and r.tagId in (?2) and r.status = 0")
+    List<UserTagProductRelation> findByPidsAndTagIds(List<String> pids, List<String> tagIds);
+
+    @Query(value = "select r from UserTagProductRelation r where r.pid = ?1 and r.tagId = ?2 and r.status = ?3")
+    List<UserTagProductRelation> findByPidAndTagId(String pid, String tagId, BaseStatusEnum status);
+
+    List<UserTagProductRelation> findByPidAndTagId(String pid, String merchantId);
+
+    @Query(value = "select r from UserTagProductRelation r where r.tagId = ?1 and r.pid in (?2) and r.status = 0 order by r.sort limit ?3, ?4", nativeQuery = true)
+    List<UserTagProductRelation> findByTagIdAndPids(String tagId, List<String> productIdList, Long start, Integer size);
+
+    @Modifying
+    @Query(value = "update UserTagProductRelation r set r.status = 1 where r.tagId = :tagId")
+    Integer deleteByTagId(@Param("tagId") String tagId);
+
+    @Modifying
+    @Query(value = "update UserTagProductRelation r set r.status = 1 where r.pid = :productId and r.tagId = :tagId")
+    Integer deleteByPidAndTagId(@Param("productId") String productId, @Param("tagId") String tagId);
+}

+ 33 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/repository/UserTagRepository.java

@@ -0,0 +1,33 @@
+package cn.rankin.userservice.repository;
+
+import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.common.utils.jpa.BasicJpaRepository;
+import cn.rankin.data.api.user.entity.UserTag;
+import org.springframework.data.jpa.repository.Modifying;
+import org.springframework.data.jpa.repository.Query;
+
+import java.util.List;
+
+public interface UserTagRepository extends BasicJpaRepository<UserTag, String> {
+
+    @Query(value = "select t from UserTag t where t.typeCode = ?1 and t.uid = ?2  and t.status = ?3 order by t.sort")
+    List<UserTag> findByTypeCodeUid(String typeCode, String uid, BaseStatusEnum status);
+
+/*
+    @Query(value = "select t from UserTag t where t.typeCode=?1 and  t.uid = ?2  and t.status = ?3 order by t.sort")
+    List<UserTag> findByCodeUid(String code, String uid, BaseStatusEnum status);
+*/
+
+    @Query(value = "select t from UserTag t where t.typeCode = ?1 order by t.sort")
+    List<UserTag> findByTypeCode(String typeCode);
+
+    @Query(value = "select t from UserTag t where t.id in (?1) and t.status = ?2")
+    List<UserTag> findByIds(List<String> tagIdList, BaseStatusEnum status);
+
+    @Modifying
+    @Query(value = "update UserTag t set t.status = 1 where t.id = ?1")
+    Integer deleteById(String id);
+
+    @Query(value = "select t from UserTag t where t.uid = ?1  order by t.sort")
+    List<UserTag> findUserTagByUid(String uid);
+}

+ 153 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/service/UserTagProductRelationService.java

@@ -0,0 +1,153 @@
+package cn.rankin.userservice.service;
+
+import cn.rankin.common.utils.enums.BaseOrderEnum;
+import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.common.utils.util.JpaSortUtil;
+import cn.rankin.common.utils.util.ListUtil;
+import cn.rankin.data.api.app.vo.ItemVo;
+import cn.rankin.data.api.product.entity.MerchantProductTagRelation;
+import cn.rankin.data.api.user.entity.UserTagProductRelation;
+//import cn.rankin.productservice.repository.MerchantProductTagRelationRepository;
+import cn.rankin.userservice.repository.UserTagProductRelationRepository;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.CollectionUtils;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+
+@Slf4j
+@Service
+public class UserTagProductRelationService {
+
+    @Autowired
+    private UserTagProductRelationRepository relationRepository;
+
+    public List<UserTagProductRelation> findByTagId(String tagId) {
+        UserTagProductRelation relation = new UserTagProductRelation();
+        relation.setTagId(tagId);
+        relation.setStatus(BaseStatusEnum.NORMAL);
+
+
+        // sort
+        LinkedHashMap<String, BaseOrderEnum> sort = new LinkedHashMap() { {
+            this.put("sort", BaseOrderEnum.ASC);
+        }};
+        List<UserTagProductRelation> relationList = relationRepository.find(relation, JpaSortUtil.sort(sort));
+/*
+        if (CollectionUtils.isEmpty(relationList)) {
+            return relationList;
+        }
+
+        List<String> productIdList = new ArrayList<>();
+
+        relationList.forEach(userTagProductRelation -> {
+            String productId = userTagProductRelation.getPid();
+            productIdList.add(productId);
+        });
+*/
+
+        return relationList;
+    }
+
+/*    public List<UserTagProductRelation> findByTagId(String tagId) {
+        return relationRepository.findByTagId(tagId);
+    }*/
+
+    public List<UserTagProductRelation> findByTagId(String tagId, BaseStatusEnum status) {
+        return relationRepository.findByTagId(tagId, status);
+    }
+
+    public List<UserTagProductRelation> findByPidsAndTagIds(List<String> pids, List<String> tagIds) {
+        List<UserTagProductRelation> userTagProductRelationList = relationRepository.findByPidsAndTagIds(pids, tagIds);
+        return userTagProductRelationList;
+    }
+
+    public List<UserTagProductRelation> findByPidAndTagId(String pid, String tagId) {
+        return relationRepository.findByPidAndTagId(pid, tagId, BaseStatusEnum.NORMAL);
+    }
+
+    @Transactional
+    public void updateByPid(String productId, String tagId, List<String> tagIdList) {
+        if (tagIdList == null) {
+            return;
+        }else if (tagIdList.size() == 0) {
+            relationRepository.deleteByPidAndTagId(productId, tagId);
+            return;
+        }
+
+        List<UserTagProductRelation> relationList = relationRepository.findByPidAndTagId(productId, tagId);
+
+        List<String> currentIdList = new ArrayList<>();
+        relationList.forEach( relation -> {
+            String tid = relation.getTagId();
+            if (!tagIdList.contains(tid)) {
+                relation.setStatus(BaseStatusEnum.DEL);
+            }else {
+                relation.setStatus(BaseStatusEnum.NORMAL);
+            }
+            currentIdList.add(tid);
+        });
+
+        List<String> notExistTagIdList = ListUtil.subtract(tagIdList, currentIdList);
+        if (notExistTagIdList != null && notExistTagIdList.size() > 0) {
+            notExistTagIdList.forEach( tid -> {
+                UserTagProductRelation relation = new UserTagProductRelation();
+                relation.setPid(productId);
+                relation.setTagId(tid);
+                relation.setSort(0);
+                relation.setStatus(BaseStatusEnum.NORMAL);
+                relationList.add(relation);
+            });
+        }
+
+        relationRepository.save(relationList);
+    }
+
+    @Transactional
+    public void updateByTagId(String tagId, List<String> productIdList) {
+        if (productIdList == null) {
+            return;
+        }else if (productIdList.size() == 0) {
+            relationRepository.deleteByTagId(tagId);
+            return;
+        }
+
+        List<UserTagProductRelation> relationList = relationRepository.findByTagId(tagId);
+
+        List<String> currentIdList = new ArrayList<>();
+        relationList.forEach( relation -> {
+            String productId = relation.getPid();
+            if (productIdList.contains(productId)) {
+                relation.setStatus(BaseStatusEnum.NORMAL);
+            }else {
+                relation.setStatus(BaseStatusEnum.DEL);
+            }
+            currentIdList.add(productId);
+        });
+
+        List<String> notExistItemIdList = ListUtil.subtract(productIdList, currentIdList);
+        if (notExistItemIdList != null && notExistItemIdList.size() > 0) {
+            notExistItemIdList.forEach( productId -> {
+                UserTagProductRelation relation = new UserTagProductRelation();
+                relation.setPid(productId);
+                relation.setTagId(tagId);
+                relation.setSort(0);
+                relation.setStatus(BaseStatusEnum.NORMAL);
+                relationList.add(relation);
+            });
+        }
+
+        // 排序
+        relationList.forEach( relation -> {
+            String productId = relation.getPid();
+            Integer index = productIdList.indexOf(productId);
+            relation.setSort(index);
+        });
+
+        relationRepository.save(relationList);
+    }
+}

+ 189 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/service/UserTagService.java

@@ -0,0 +1,189 @@
+package cn.rankin.userservice.service;
+
+import cn.rankin.common.utils.api.page.Page;
+import cn.rankin.common.utils.enums.BaseOrderEnum;
+import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.common.utils.util.JpaSortUtil;
+import cn.rankin.data.api.product.entity.Tag;
+import cn.rankin.data.api.user.dto.UserTagDTO;
+import cn.rankin.data.api.user.entity.UserTag;
+import cn.rankin.userservice.repository.UserTagRepository;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.LinkedHashMap;
+import java.util.List;
+
+import static cn.rankin.userservice.utils.DTOConverter.convert;
+
+@Slf4j
+@Service
+public class UserTagService {
+
+    @Autowired
+    private UserTagRepository userTagRepository;
+
+/*    @Autowired
+    private TagGroupRepository tagGroupRepository;
+
+    @Autowired
+    private MerchantProductRepository merchantProductRepository;
+
+    @Autowired
+    private MerchantProductTagRelationService merchantProductTagRelationService;
+
+    @Autowired
+    private GoodsService goodsService;*/
+
+    public List<UserTag> findByTypeCode(String typeCode,String uid) {
+        List<UserTag> userTagList = userTagRepository.findByTypeCodeUid(typeCode,uid, BaseStatusEnum.NORMAL);
+        return userTagList;
+    }
+
+    public List<UserTag> findByTypeCode(String typeCode) {
+        List<UserTag> userTagList = userTagRepository.findByTypeCode(typeCode);
+        return userTagList;
+    }
+
+/*    public List<MerchantProduct> getMerchantProductList(String tagId, String merchantId) {
+        List<MerchantProductTagRelation> relationList = merchantProductTagRelationService.findByTagId(tagId, BaseStatusEnum.NORMAL);
+        if (CollectionUtils.isEmpty(relationList)) {
+            return new ArrayList<>();
+        }
+
+        List<String> productIdList = new ArrayList<>();
+        relationList.forEach(relation -> productIdList.add(relation.getPid()));
+        List<MerchantProduct> merchantProductList = merchantProductRepository.findByPidsAndMerchantId(productIdList, merchantId);
+        Map<String, MerchantProduct> merchantProductMap = ListUtil.convert(merchantProductList, "pid", MerchantProduct.class);
+
+        List<MerchantProduct> sortMerchantProductList = new ArrayList<>();
+        for (String productId : productIdList) {
+            MerchantProduct merchantProduct = merchantProductMap.get(productId);
+            if (merchantProduct != null) {
+                sortMerchantProductList.add(merchantProduct);
+            }
+        }
+        return sortMerchantProductList;
+    }*/
+
+    // 搜索用户标签
+    public Page<UserTag> search(UserTag userTag, Integer pageNo, Integer pageSize, LinkedHashMap<String, BaseOrderEnum> sort) {
+        Long count = userTagRepository.count(userTag);
+        Page<UserTag> page = new Page(count, pageNo, pageSize);
+        if (count == 0) {
+            return page;
+        }
+
+        List<UserTag> userTagList = userTagRepository.find(userTag, page.getStart(), page.getPageSize(), JpaSortUtil.sort(sort));
+        page.setList(userTagList);
+
+        return page;
+    }
+
+    // 标签详情
+    public UserTag getUserTag(String id) {
+        UserTag userTag = userTagRepository.find(id);
+        String uid = userTag.getUid();
+
+        /*List<MerchantProduct> merchantProductList = getUserProductList(tagId, uid);
+        userTag.setProductList(merchantProductList);*/
+        return userTag;
+    }
+
+    // 标签详情
+    public List<UserTag> findUserTagByCodeUid(String code,String uid) {
+        List<UserTag> userTagLIst = userTagRepository.findByTypeCodeUid(code,uid, BaseStatusEnum.NORMAL);
+//        String uid = userTag.getUid();
+
+        /*List<MerchantProduct> merchantProductList = getUserProductList(tagId, uid);
+        userTag.setProductList(merchantProductList);*/
+        return userTagLIst;
+    }
+
+    @Transactional
+    public UserTag create(UserTagDTO userTagDTO) {
+        UserTag userTag = convert(userTagDTO);
+
+/*        String merchantId = null;
+
+        String groupId = tagDTO.getGroupId();
+        if(StringUtils.isNotBlank(groupId)){
+            //兼容旧版本   tagGroup
+            TagGroup tagGroup = tagGroupRepository.find(groupId);
+            if (tagGroup == null) {
+                return APIResult.error(ProductServiceAPICode.NOT_EXISTS);
+            }
+
+            merchantId = tagGroup.getMerchantId();
+
+        }else{
+            //新版本
+            merchantId = tagDTO.getMerchantId();
+        }
+
+        tag.setMerchantId(merchantId);*/
+        UserTag result = userTagRepository.save(userTag);
+
+        return result;
+    }
+
+    @Transactional
+    public UserTag update(UserTagDTO userTagDTO) {
+        UserTag userTag = convert(userTagDTO);
+        UserTag result = userTagRepository.update(userTag);
+
+     /*   String tagId = result.getId();
+        String uid = result.getUid();
+        List<String> productIdList = tagDTO.getProductList();
+        merchantProductTagRelationService.updateByTagId(tagId, merchantId, productIdList);
+
+        List<MerchantProduct> merchantProductList = getMerchantProductList(tagId, merchantId);
+        tag.setProductList(merchantProductList);*/
+
+        return result;
+    }
+
+    @Transactional
+    public Boolean delete(String tagId) {
+        Integer count = userTagRepository.deleteById(tagId);
+        if (count > 0) {
+            return true;
+        }else{
+            return false;
+        }
+    }
+
+    public List<UserTag> findUserTagByUid(String uid) {
+        return userTagRepository.findUserTagByUid(uid);
+    }
+
+/*    @Transactional
+    public void sortTag(String groupId, List<String> tagIdList) {
+        List<Tag> tagList = tagRepository.findByGroupId(groupId, BaseStatusEnum.NORMAL);
+        sortTag(tagIdList, tagList);
+        tagRepository.update(tagList);
+    }
+
+
+
+    @Transactional
+    public void sortTagByTypeCode(String typeCode, List<String> tagIdList) {
+        List<Tag> tagList = tagRepository.findByTypeCode(typeCode);
+        sortTag(tagIdList, tagList);
+        tagRepository.update(tagList);
+    }*/
+
+/*    private void sortTag(List<String> tagIdList, List<Tag> tagList) {
+        for (Tag tag : tagList) {
+            String tagId = tag.getId();
+            if (!tagIdList.contains(tagId)) {
+                tag.setStatus(BaseStatusEnum.DEL);
+                continue;
+            }
+            Integer index = tagIdList.indexOf(tagId);
+            tag.setSort(index);
+        }
+    }*/
+}

+ 6 - 0
rankin-user-service/src/main/java/cn/rankin/userservice/utils/DTOConverter.java

@@ -64,4 +64,10 @@ public class DTOConverter {
         BeanUtils.copyProperties(whiteUserDTO, whiteUser);
         return whiteUser;
     }
+
+    public static UserTag convert(UserTagDTO userTagDTO) {
+        UserTag userTag = new UserTag();
+        BeanUtils.copyProperties(userTagDTO, userTag);
+        return userTag;
+    }
 }