|
@@ -3,18 +3,28 @@ package cn.rankin.productservice.service;
|
|
|
import cn.rankin.common.utils.api.model.APICode;
|
|
|
import cn.rankin.common.utils.api.model.APIResult;
|
|
|
import cn.rankin.common.utils.api.page.Page;
|
|
|
+import cn.rankin.common.utils.enums.BaseOrderEnum;
|
|
|
+import cn.rankin.common.utils.util.JpaSortUtil;
|
|
|
import cn.rankin.common.utils.util.ListUtil;
|
|
|
import cn.rankin.data.api.product.dto.TagDTO;
|
|
|
import cn.rankin.common.utils.enums.BaseStatusEnum;
|
|
|
+import cn.rankin.data.api.product.entity.MerchantProduct;
|
|
|
+import cn.rankin.data.api.product.entity.MerchantProductTagRelation;
|
|
|
import cn.rankin.data.api.product.entity.Tag;
|
|
|
+import cn.rankin.data.api.product.entity.TagGroup;
|
|
|
+import cn.rankin.productservice.code.ProductServiceAPICode;
|
|
|
+import cn.rankin.productservice.repository.MerchantProductRepository;
|
|
|
+import cn.rankin.productservice.repository.TagGroupRepository;
|
|
|
import cn.rankin.productservice.repository.TagRepository;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
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;
|
|
|
import java.util.Map;
|
|
|
|
|
@@ -27,106 +37,117 @@ public class TagService {
|
|
|
@Autowired
|
|
|
private TagRepository tagRepository;
|
|
|
|
|
|
- public APIResult<Tag> findById(String id) {
|
|
|
- Tag tag = tagRepository.find(id);
|
|
|
- return APIResult.ok(tag);
|
|
|
- }
|
|
|
+ @Autowired
|
|
|
+ private TagGroupRepository tagGroupRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MerchantProductRepository merchantProductRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MerchantProductTagRelationService merchantProductTagRelationService;
|
|
|
|
|
|
- public List<Tag> findByIds(List<String> tagIdList) {
|
|
|
- List<Tag> tagList = tagRepository.findByIds(tagIdList, BaseStatusEnum.NORMAL);
|
|
|
+ public List<Tag> findByGroupId(String groupId) {
|
|
|
+ List<Tag> tagList = tagRepository.findByGroupId(groupId);
|
|
|
+ setGroupInfo(tagList);
|
|
|
return tagList;
|
|
|
}
|
|
|
|
|
|
- public APIResult<Page<Tag>> search(Tag tag, Integer pageNo, Integer pageSize) {
|
|
|
+ public void setGroupInfo(List<Tag> tagList) {
|
|
|
+ if (CollectionUtils.isEmpty(tagList)) return;
|
|
|
+ List<String> groupIdList = new ArrayList<>();
|
|
|
+ tagList.forEach(tag -> {
|
|
|
+ String groupId = tag.getGroupId();
|
|
|
+ if (!groupIdList.contains(groupId)) {
|
|
|
+ groupIdList.add(groupId);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ List<TagGroup> groupList = tagGroupRepository.findByIds(groupIdList);
|
|
|
+ Map<String, TagGroup> tagGroupMap = ListUtil.convert(groupList, "id", TagGroup.class);
|
|
|
+
|
|
|
+ for (Tag tag : tagList) {
|
|
|
+ String groupId = tag.getGroupId();
|
|
|
+ TagGroup tagGroup = tagGroupMap.get(groupId);
|
|
|
+ if (tagGroup != null) {
|
|
|
+ tag.setGroupName(tagGroup.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<MerchantProduct> getMerchantProductList(String tagId, String merchantId) {
|
|
|
+ List<MerchantProductTagRelation> relationList = merchantProductTagRelationService.findByTagId(tagId);
|
|
|
+ if (CollectionUtils.isEmpty(relationList)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> productIdList = new ArrayList<>();
|
|
|
+ relationList.forEach(relation -> productIdList.add(relation.getPid()));
|
|
|
+ return merchantProductRepository.findByPidsAndMerchantId(productIdList, merchantId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 搜索标签
|
|
|
+ public APIResult<Page<Tag>> search(Tag tag, Integer pageNo, Integer pageSize, LinkedHashMap<String, BaseOrderEnum> sort) {
|
|
|
Long count = tagRepository.count(tag);
|
|
|
Page<Tag> page = new Page(count, pageNo, pageSize);
|
|
|
-
|
|
|
if (count == 0) {
|
|
|
return APIResult.ok(page);
|
|
|
}
|
|
|
|
|
|
- List<Tag> tagList = tagRepository.search(tag, Long.valueOf(page.getStart()).intValue(), pageSize);
|
|
|
-// List<Tag> tagList = tagRepository.find(tag, page.getStart(), pageSize, JpaSortUtil.sort(sort));
|
|
|
+ List<Tag> tagList = tagRepository.find(tag, page.getStart(), page.getPageSize(), JpaSortUtil.sort(sort));
|
|
|
+ setGroupInfo(tagList);
|
|
|
page.setList(tagList);
|
|
|
|
|
|
return APIResult.ok(page);
|
|
|
}
|
|
|
|
|
|
+ // 标签详情
|
|
|
+ public APIResult<Tag> getTag(String tagId) {
|
|
|
+ Tag tag = tagRepository.find(tagId);
|
|
|
+ String merchantId = tag.getMerchantId();
|
|
|
+
|
|
|
+ List<MerchantProduct> merchantProductList = getMerchantProductList(tagId, merchantId);
|
|
|
+ tag.setProducts(merchantProductList);
|
|
|
+ return APIResult.ok(tag);
|
|
|
+ }
|
|
|
+
|
|
|
@Transactional
|
|
|
public APIResult<Tag> create(TagDTO tagDTO) {
|
|
|
-
|
|
|
Tag tag = convert(tagDTO);
|
|
|
Tag result = tagRepository.save(tag);
|
|
|
|
|
|
- return APIResult.ok(result);
|
|
|
+ String tagId = result.getId();
|
|
|
+ String merchantId = result.getMerchantId();
|
|
|
+ List<String> productIdList = tagDTO.getProductList();
|
|
|
+ merchantProductTagRelationService.updateByTagId(tagId, merchantId, productIdList);
|
|
|
+
|
|
|
+ List<MerchantProduct> merchantProductList = getMerchantProductList(tagId, merchantId);
|
|
|
+ tag.setProducts(merchantProductList);
|
|
|
+
|
|
|
+ return APIResult.ok(tag);
|
|
|
}
|
|
|
|
|
|
@Transactional
|
|
|
public APIResult<Tag> update(TagDTO tagDTO) {
|
|
|
-
|
|
|
Tag tag = convert(tagDTO);
|
|
|
Tag result = tagRepository.update(tag);
|
|
|
|
|
|
- return APIResult.ok(result);
|
|
|
- }
|
|
|
-
|
|
|
- @Transactional
|
|
|
- public APIResult<Boolean> delete(String id) {
|
|
|
- Integer count = tagRepository.updateStatusById(id, BaseStatusEnum.DEL);
|
|
|
- if (count > 0) {
|
|
|
- return APIResult.ok(true);
|
|
|
- }
|
|
|
- return APIResult.error(APICode.NOT_EXISTS);
|
|
|
- }
|
|
|
+ String tagId = result.getId();
|
|
|
+ String merchantId = result.getMerchantId();
|
|
|
+ List<String> productIdList = tagDTO.getProductList();
|
|
|
+ merchantProductTagRelationService.updateByTagId(tagId, merchantId, productIdList);
|
|
|
|
|
|
- @Transactional
|
|
|
- public APIResult<Boolean> deleteByGroupId(String groupId) {
|
|
|
- tagRepository.deleteByGroupId(groupId, BaseStatusEnum.DEL);
|
|
|
- return APIResult.ok(true);
|
|
|
- }
|
|
|
+ List<MerchantProduct> merchantProductList = getMerchantProductList(tagId, merchantId);
|
|
|
+ tag.setProducts(merchantProductList);
|
|
|
|
|
|
- public APIResult<List<Tag>> findNormalByGroupId(String groupId) {
|
|
|
- List<Tag> tagList = tagRepository.findNormalByGroupId(groupId, BaseStatusEnum.NORMAL);
|
|
|
- return APIResult.ok(tagList);
|
|
|
+ return APIResult.ok(tag);
|
|
|
}
|
|
|
|
|
|
@Transactional
|
|
|
- public APIResult<List<Tag>> updateGroupRelation(String groupId, List<TagDTO> tagDTOList) {
|
|
|
- if (tagDTOList == null) {
|
|
|
- return APIResult.ok();
|
|
|
- }
|
|
|
-
|
|
|
- deleteByGroupId(groupId);
|
|
|
-
|
|
|
- if (tagDTOList.size() == 0) {
|
|
|
- return APIResult.ok(new ArrayList<>());
|
|
|
- }
|
|
|
-
|
|
|
- List<String> tagIdList = new ArrayList<>();
|
|
|
- List<Tag> tagList = new ArrayList<>();
|
|
|
- for (int i = 0; i < tagDTOList.size(); i++) {
|
|
|
- TagDTO dto = tagDTOList.get(i);
|
|
|
- if (dto == null) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- String tagId = dto.getId();
|
|
|
- if (!StringUtils.isEmpty(tagId)) {
|
|
|
- tagIdList.add(tagId);
|
|
|
- }
|
|
|
-
|
|
|
- Tag tag = convert(dto);
|
|
|
- tag.setGroupId(groupId);
|
|
|
- tag.setSort(i);
|
|
|
- tag.setStatus(BaseStatusEnum.NORMAL);
|
|
|
-
|
|
|
- tagList.add(tag);
|
|
|
+ public APIResult<Boolean> delete(String tagId) {
|
|
|
+ Integer count = tagRepository.deleteById(tagId);
|
|
|
+ if (count > 0) {
|
|
|
+ return APIResult.ok(true);
|
|
|
}
|
|
|
-
|
|
|
- List<Tag> result = tagRepository.save(tagList);
|
|
|
-
|
|
|
- return APIResult.ok(result);
|
|
|
-
|
|
|
+ return APIResult.error(ProductServiceAPICode.NOT_EXISTS);
|
|
|
}
|
|
|
-
|
|
|
}
|