huodongdong 7 år sedan
förälder
incheckning
31ca7939f3

+ 3 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/dto/product/TagDTO.java

@@ -1,6 +1,7 @@
 package cn.rankin.common.utils.dto.product;
 
 import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.common.utils.enums.TagTypeEnum;
 import lombok.Data;
 
 import java.io.Serializable;
@@ -15,6 +16,8 @@ public class TagDTO implements Serializable {
 
     private String name;
 
+    private TagTypeEnum type;
+
     private Integer sort;
 
     private List<String> itemList;

+ 32 - 0
rankin-common-utils/src/main/java/cn/rankin/common/utils/dto/search/TagSearchDTO.java

@@ -0,0 +1,32 @@
+package cn.rankin.common.utils.dto.search;
+
+import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.common.utils.enums.TagTypeEnum;
+import lombok.Data;
+import lombok.ToString;
+
+import java.io.Serializable;
+
+@Data
+@ToString
+public class TagSearchDTO implements Serializable {
+
+    private String code;
+
+    private String name;
+
+    private String merchantId;
+
+    private TagTypeEnum type;
+
+    private BaseStatusEnum status;
+
+    private String sort;
+
+    private Integer by;
+
+    private Integer pageNo = 1;
+
+    private Integer pageSize = 10;
+
+}

+ 139 - 123
rankin-product-service/src/main/java/cn/rankin/productservice/controller/TagController.java

@@ -5,6 +5,7 @@ import cn.rankin.common.utils.api.model.APICodeManager;
 import cn.rankin.common.utils.api.model.APIResult;
 import cn.rankin.common.utils.api.page.Page;
 import cn.rankin.common.utils.dto.product.TagDTO;
+import cn.rankin.common.utils.dto.search.TagSearchDTO;
 import cn.rankin.common.utils.enums.*;
 import cn.rankin.productservice.entity.Item;
 import cn.rankin.productservice.entity.ItemTagRelation;
@@ -38,127 +39,142 @@ public class TagController {
     @Autowired
     private ItemService itemService;
 
-//    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
-//    public APIResult<Tag> getTag(@PathVariable String id) {
-//        APIResult<Tag> result = tagService.findById(id);
-//        Tag tag = result.getData();
-//        if (tag == null) {
-//            return result;
-//        }
-//
-//        APIResult<List<Item>> itemResult = getTagItem(tag.getId());
-//        tag.setItemList(itemResult.getData());
-//
-//        return result;
-//
-//    }
-//
-//    @Transactional
-//    @RequestMapping(method = RequestMethod.POST)
-//    public APIResult<Tag> create(@RequestBody TagDTO tagDTO) {
-//
-//        APIResult<Tag> result = tagService.create(tagDTO);
-//        Tag tag = result.getData();
-//
-//        List<String> itemIdList = tagDTO.getItemList();
-//        if (itemIdList == null || tag == null) {
-//            return result;
-//        }
-//
-//        APIResult<List<Item>> relationResult = updateTagRelation(tag, itemIdList);
-//        if (!relationResult.getSuccess()) {
-//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
-//            return APIResult.error(new APICode(relationResult.getCode(), relationResult.getMessage()));
-//        }
-//        tag.setItemList(relationResult.getData());
-//
-//        return result;
-//
-//    }
-//
-//    @Transactional
-//    @RequestMapping(method = RequestMethod.PUT)
-//    public APIResult<Tag> update(@RequestBody TagDTO tagDTO) {
-//        if (tagDTO.getId() == null) {
-//            return APIResult.error(APICodeManager.PARAMETER_ERROR);
-//        }
-//
-//        APIResult<Tag> result = tagService.update(tagDTO);
-//        Tag tag = result.getData();
-//
-//        List<String> itemIdList = tagDTO.getItemList();
-//        if (itemIdList == null || tag == null) {
-//            return result;
-//        }
-//
-//        APIResult<List<Item>> relationResult = updateTagRelation(tag, itemIdList);
-//        if (!relationResult.getSuccess()) {
-//            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
-//            return APIResult.error(new APICode(relationResult.getCode(), relationResult.getMessage()));
-//        }
-//        tag.setItemList(relationResult.getData());
-//
-//        return tagService.update(tagDTO);
-//    }
-//
-//    @RequestMapping(value = "/id", method = RequestMethod.DELETE)
-//    public APIResult delete(@PathVariable String id) {
-//        if (StringUtils.isEmpty(id)) {
-//            return APIResult.error(APICodeManager.PARAMETER_ERROR);
-//        }
-//
-//        List<ItemTagRelation> relationList = tagRelationService.findAllByTagId(id);
-//        if (CollectionUtils.isEmpty(relationList)) {
-//            return tagService.delete(id);
-//        }
-//
-//        List<String> itemIdList = new ArrayList<>();
-//        relationList.forEach( relation -> {
-//            String itemId = relation.getItemId();
-//            if (itemId != null && !itemIdList.contains(itemId)) {
-//                itemIdList.add(itemId);
-//            }
-//        });
-//
-//        List<Item> itemList = itemService.findAllByIds(itemIdList).getData();
-//
-//        APIResult<List<Item>> result = APIResult.error(APICodeManager.CAN_NOT_DEL);
-//        result.setData(itemList);
-//
-//        return result;
-//    }
-//
-//    @Transactional
-//    public APIResult<List<Item>> updateTagRelation(Tag tag, List<String> itemIdList) {
-//        List<Item> itemList = itemService.findAllByIds(itemIdList).getData();
-//        if (itemList == null || itemList.size() != itemIdList.size()) {
-//            return APIResult.error(APICodeManager.error("itemId不匹配"));
-//        }
-//
-//        TagTypeEnum tagType = tag.getType();
-//        String merchantId = tag.getMerchantId();
-//        for (int i = 0; i < itemList.size(); i++) {
-//            Item item = itemList.get(i);
-//            if (!item.getMerchantId().equals(merchantId) || !EnumUtil.compare(item.getType(), tagType)) {
-//                return APIResult.error(APICodeManager.error("类型或渠道商不匹配"));
-//            }
-//        }
-//
-//        tagRelationService.updateByTag(tag.getId(), itemIdList);
-//        return APIResult.ok(itemList);
-//    }
-//
-//    public APIResult<List<Item>> getTagItem(String tagId) {
-//        List<ItemTagRelation> relationList = tagRelationService.findByTagId(tagId);
-//        if (relationList == null || relationList.size() == 0) {
-//            return APIResult.ok();
-//        }
-//
-//        List<String> itemIdList = new ArrayList<>();
-//        relationList.forEach( relation -> itemIdList.add(relation.getItemId()));
-//
-//        // 返回上架的item
-//        return itemService.findAllByIds(itemIdList, ItemStatusEnum.DEL);
-//
-//    }
+    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
+    public APIResult<Tag> getTag(@PathVariable String id) {
+        APIResult<Tag> result = tagService.findById(id);
+        Tag tag = result.getData();
+        if (tag == null) {
+            return result;
+        }
+
+        APIResult<List<Item>> itemResult = getTagItem(tag.getId());
+        tag.setItemList(itemResult.getData());
+
+        return result;
+
+    }
+
+    @RequestMapping(value = "/list", method = RequestMethod.GET)
+    public APIResult<Page<Tag>> search(TagSearchDTO searchDTO) {
+        Tag tag = new Tag();
+
+        String name = searchDTO.getName();
+        if (name != null) {
+            tag.setName("%" + name + "%");
+        }
+
+        tag.setMerchantId(searchDTO.getMerchantId());
+        tag.setType(searchDTO.getType());
+
+        return tagService.search(tag, searchDTO.getPageNo(), searchDTO.getPageSize());
+    }
+
+    @Transactional
+    @RequestMapping(method = RequestMethod.POST)
+    public APIResult<Tag> create(@RequestBody TagDTO tagDTO) {
+
+        APIResult<Tag> result = tagService.create(tagDTO);
+        Tag tag = result.getData();
+
+        List<String> itemIdList = tagDTO.getItemList();
+        if (itemIdList == null || tag == null) {
+            return result;
+        }
+
+        APIResult<List<Item>> relationResult = updateTagRelation(tag, itemIdList);
+        if (!relationResult.getSuccess()) {
+            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
+            return APIResult.error(new APICode(relationResult.getCode(), relationResult.getMessage()));
+        }
+        tag.setItemList(relationResult.getData());
+
+        return result;
+
+    }
+
+    @Transactional
+    @RequestMapping(method = RequestMethod.PUT)
+    public APIResult<Tag> update(@RequestBody TagDTO tagDTO) {
+        if (tagDTO.getId() == null) {
+            return APIResult.error(APICodeManager.PARAMETER_ERROR);
+        }
+
+        APIResult<Tag> result = tagService.update(tagDTO);
+        Tag tag = result.getData();
+
+        List<String> itemIdList = tagDTO.getItemList();
+        if (itemIdList == null || tag == null) {
+            return result;
+        }
+
+        APIResult<List<Item>> relationResult = updateTagRelation(tag, itemIdList);
+        if (!relationResult.getSuccess()) {
+            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
+            return APIResult.error(new APICode(relationResult.getCode(), relationResult.getMessage()));
+        }
+        tag.setItemList(relationResult.getData());
+
+        return tagService.update(tagDTO);
+    }
+
+    @RequestMapping(value = "/id", method = RequestMethod.DELETE)
+    public APIResult delete(@PathVariable String id) {
+        if (StringUtils.isEmpty(id)) {
+            return APIResult.error(APICodeManager.PARAMETER_ERROR);
+        }
+
+        List<ItemTagRelation> relationList = tagRelationService.findAllByTagId(id);
+        if (CollectionUtils.isEmpty(relationList)) {
+            return tagService.delete(id);
+        }
+
+        List<String> itemIdList = new ArrayList<>();
+        relationList.forEach( relation -> {
+            String itemId = relation.getItemId();
+            if (itemId != null && !itemIdList.contains(itemId)) {
+                itemIdList.add(itemId);
+            }
+        });
+
+        List<Item> itemList = itemService.findAllByIds(itemIdList).getData();
+
+        APIResult<List<Item>> result = APIResult.error(APICodeManager.CAN_NOT_DEL);
+        result.setData(itemList);
+
+        return result;
+    }
+
+    @Transactional
+    public APIResult<List<Item>> updateTagRelation(Tag tag, List<String> itemIdList) {
+        List<Item> itemList = itemService.findAllByIds(itemIdList).getData();
+        if (itemList == null || itemList.size() != itemIdList.size()) {
+            return APIResult.error(APICodeManager.error("itemId不匹配"));
+        }
+
+        TagTypeEnum tagType = tag.getType();
+        String merchantId = tag.getMerchantId();
+        for (int i = 0; i < itemList.size(); i++) {
+            Item item = itemList.get(i);
+            if (!item.getMerchantId().equals(merchantId) || !EnumUtil.compare(item.getType(), tagType)) {
+                return APIResult.error(APICodeManager.error("类型或渠道商不匹配"));
+            }
+        }
+
+        tagRelationService.updateByTag(tag.getId(), itemIdList);
+        return APIResult.ok(itemList);
+    }
+
+    public APIResult<List<Item>> getTagItem(String tagId) {
+        List<ItemTagRelation> relationList = tagRelationService.findByTagId(tagId);
+        if (relationList == null || relationList.size() == 0) {
+            return APIResult.ok();
+        }
+
+        List<String> itemIdList = new ArrayList<>();
+        relationList.forEach( relation -> itemIdList.add(relation.getItemId()));
+
+        // 返回上架的item
+        return itemService.findAllByIds(itemIdList, ItemStatusEnum.DEL);
+
+    }
 }

+ 25 - 0
rankin-product-service/src/main/java/cn/rankin/productservice/entity/Tag.java

@@ -1,6 +1,7 @@
 package cn.rankin.productservice.entity;
 
 import cn.rankin.common.utils.enums.BaseStatusEnum;
+import cn.rankin.common.utils.enums.TagTypeEnum;
 import lombok.Data;
 import lombok.ToString;
 import org.hibernate.annotations.DynamicInsert;
@@ -9,6 +10,7 @@ import org.hibernate.annotations.DynamicUpdate;
 import javax.persistence.*;
 import java.io.Serializable;
 import java.util.Date;
+import java.util.List;
 
 @Data
 @ToString
@@ -33,6 +35,10 @@ public class Tag implements Serializable {
 
     @Column
     @Enumerated(EnumType.ORDINAL)
+    private TagTypeEnum type;
+
+    @Column
+    @Enumerated(EnumType.ORDINAL)
     private BaseStatusEnum status;
 
     @Column(name = "gmt_created", updatable = false, insertable = false, columnDefinition = "timestamp NULL DEFAULT CURRENT_TIMESTAMP")
@@ -48,4 +54,23 @@ public class Tag implements Serializable {
 
     @Transient
     private String groupName;
+
+    @Transient
+    private List<Item> itemList;
+
+    public Tag() {}
+
+    public Tag(String id, String name, TagTypeEnum type, String groupId, Integer sort, BaseStatusEnum status,
+               Date gmtCreated, Date gmtModified, String groupName, String merchantId) {
+        this.id = id;
+        this.name = name;
+        this.type = type;
+        this.groupId = groupId;
+        this.sort = sort;
+        this.status = status;
+        this.gmtCreated = gmtCreated;
+        this.gmtModified = gmtModified;
+        this.merchantId = merchantId;
+        this.groupName = groupName;
+    }
 }

+ 3 - 3
rankin-product-service/src/main/java/cn/rankin/productservice/proxy/ItemServiceProxy.java

@@ -98,9 +98,9 @@ public class ItemServiceProxy {
             ItemTypeEnum itemType = item.getType();
             for (int i = 0; i < tagList.size(); i++) {
                 Tag tag = tagList.get(i);
-//                if (!tag.getMerchantId().equals(merchantId) || !EnumUtil.compare(itemType, tag.getType())) {
-//                    return APIResult.error(APICodeManager.error("包含错误的标签"));
-//                }
+                if (!tag.getMerchantId().equals(merchantId) || !EnumUtil.compare(itemType, tag.getType())) {
+                    return APIResult.error(APICodeManager.error("包含错误的标签"));
+                }
             }
 
             tagRelationService.updateByItem(itemId, tagIdList);

+ 5 - 3
rankin-product-service/src/main/java/cn/rankin/productservice/repository/TagRepository.java

@@ -13,12 +13,14 @@ import java.util.List;
 
 public interface TagRepository extends BasicJpaRepository<Tag, String> {
 
-    List<Tag> findByIdss(List<String> tagIdList, BaseStatusEnum status);
+    String SQL = "select new Tag(t.id, t.name, t.type, t.groupId, t.sort, t.status, t.gmtCreated, t.gmtModified, g.name, g.merchantId) from Tag t, Group g where t.groupId = g.id";
 
-    @Query(value = "select t from Tag t where t.groupId = :groupId and t.status = :status")
+    List<Tag> search(Tag tag, Integer start, Integer size);
+
+    @Query(value = SQL + " and t.groupId = :groupId and t.status = :status")
     List<Tag> findNormalByGroupId(@Param("groupId") String groupId, @Param("status") BaseStatusEnum status);
 
-    @Query(value = "select t from Tag t where t.id in (:ids) and t.status = :status order by t.sort asc")
+    @Query(value = SQL + " and t.id in (:ids) and t.status = :status order by t.sort asc")
     List<Tag> findByIdIn(@Param("ids") List<String> ids, @Param("status") BaseStatusEnum status);
 
     @Transactional

+ 48 - 0
rankin-product-service/src/main/java/cn/rankin/productservice/repository/TagRepositoryImpl.java

@@ -0,0 +1,48 @@
+package cn.rankin.productservice.repository;
+
+import cn.rankin.common.utils.enums.TagTypeEnum;
+import cn.rankin.productservice.entity.Tag;
+import org.springframework.stereotype.Repository;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.Query;
+import java.util.List;
+
+@Repository
+public class TagRepositoryImpl {
+
+    @PersistenceContext
+    private EntityManager entityManager;
+
+    List<Tag> search(Tag tag, Integer start, Integer size) {
+        StringBuilder builder = new StringBuilder(TagRepository.SQL);
+
+        String name = tag.getName();
+        if (name != null) {
+            builder.append(String.format(" and t.name like %s", name));
+        }
+
+        String merchantId = tag.getMerchantId();
+        if (merchantId != null) {
+            builder.append(String.format(" and g.merchantId = %s", merchantId));
+        }
+
+        TagTypeEnum type = tag.getType();
+        if (type != null) {
+            builder.append(" and t.type = :type");
+        }
+
+        builder.append(" order by t.gmtModified desc");
+
+        Query query = entityManager.createQuery(builder.toString());
+        if (type != null) {
+            query.setParameter("type", type);
+        }
+
+        query.setFirstResult(start);
+        query.setMaxResults(size);
+
+        return query.getResultList();
+    }
+}

+ 0 - 24
rankin-product-service/src/main/java/cn/rankin/productservice/repository/tagRepositoryImpl.java

@@ -1,24 +0,0 @@
-package cn.rankin.productservice.repository;
-
-import cn.rankin.common.utils.enums.BaseStatusEnum;
-import cn.rankin.productservice.entity.Tag;
-import org.springframework.stereotype.Component;
-import org.springframework.stereotype.Repository;
-
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-import javax.persistence.Query;
-import java.util.List;
-
-@Repository
-public class tagRepositoryImpl {
-
-    @PersistenceContext
-    private EntityManager entityManager;
-
-    public List<Tag> findByIdss(List<String> tagIdList, BaseStatusEnum status) {
-        StringBuilder builder = new StringBuilder("select t.*, g.name as group_name, g.merchant_id from lj_tag t left outer join lj_group g on t.group_id = g.id where 1=1");
-        Query query = entityManager.createNativeQuery(builder.toString(), Tag.class);
-        return query.getResultList();
-    }
-}

+ 3 - 2
rankin-product-service/src/main/java/cn/rankin/productservice/service/TagService.java

@@ -38,7 +38,7 @@ public class TagService {
         return APIResult.ok(tagList);
     }
 
-    public APIResult<Page<Tag>> search(Tag tag, Integer pageNo, Integer pageSize, LinkedHashMap<String, BaseOrderEnum> sort) {
+    public APIResult<Page<Tag>> search(Tag tag, Integer pageNo, Integer pageSize) {
         Long count = tagRepository.count(tag);
         Page<Tag> page = new Page(count, pageNo, pageSize);
 
@@ -46,7 +46,8 @@ public class TagService {
             return APIResult.ok(page);
         }
 
-        List<Tag> tagList = tagRepository.find(tag, page.getStart(), pageSize, JpaSortUtil.sort(sort));
+        List<Tag> tagList = tagRepository.search(tag, Long.valueOf(page.getStart()).intValue(), pageSize);
+//        List<Tag> tagList = tagRepository.find(tag, page.getStart(), pageSize, JpaSortUtil.sort(sort));
         page.setList(tagList);
 
         return APIResult.ok(page);

+ 3 - 5
rankin-product-service/src/test/java/cn/rankin/productservice/repository/tagRepositoryImplTest.java

@@ -2,6 +2,7 @@ package cn.rankin.productservice.repository;
 
 import cn.rankin.common.utils.enums.BaseStatusEnum;
 import cn.rankin.productservice.ProductServiceApplication;
+import cn.rankin.productservice.entity.Tag;
 import com.alibaba.fastjson.JSON;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.Test;
@@ -13,8 +14,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import java.util.ArrayList;
 import java.util.List;
 
-import static org.junit.Assert.*;
-
 @Slf4j
 @RunWith(SpringJUnit4ClassRunner.class)
 @SpringBootTest(classes = ProductServiceApplication.class)
@@ -24,10 +23,9 @@ public class tagRepositoryImplTest {
     private TagRepository tagRepository;
 
     @Test
-    public void findByIds() throws Exception {
+    public void findByIdIn() {
         List<String> idList = new ArrayList<>();
         idList.add("1");
-        log.info(JSON.toJSONString(tagRepository.findByIdss(idList, BaseStatusEnum.NORMAL)));
+        List<Tag> tagList = tagRepository.findByIdIn(idList, BaseStatusEnum.NORMAL);
     }
-
 }