|
@@ -0,0 +1,299 @@
|
|
|
+package cn.rankin.productservice.service;
|
|
|
+
|
|
|
+import cn.rankin.common.utils.api.model.APIResult;
|
|
|
+import cn.rankin.common.utils.api.page.Page;
|
|
|
+import cn.rankin.common.utils.enums.BaseStatusEnum;
|
|
|
+import cn.rankin.common.utils.enums.ProductTypeEnum;
|
|
|
+import cn.rankin.common.utils.util.ListUtil;
|
|
|
+import cn.rankin.data.api.app.vo.ItemGoodsVo;
|
|
|
+import cn.rankin.data.api.app.vo.ItemVo;
|
|
|
+import cn.rankin.data.api.product.entity.*;
|
|
|
+import cn.rankin.data.api.product.vo.CourseItemVo;
|
|
|
+import cn.rankin.data.api.product.vo.SupportItemVo;
|
|
|
+import cn.rankin.productservice.repository.*;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class ItemService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MerchantProductRepository merchantProductRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CourseRepository courseRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SupportRepository supportRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GoodsRepository goodsRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MerchantProductTagRelationRepository merchantProductTagRelationRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CourseSupportRelationRepository courseSupportRelationRepository;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SupportReferenceRepository supportReferenceRepository;
|
|
|
+
|
|
|
+ public CourseItemVo findCourseItem(String courseId, String merchantId) {
|
|
|
+ MerchantProduct merchantProduct = merchantProductRepository.findByPidAndMerchantId(courseId, merchantId);
|
|
|
+ if (merchantProduct == null || !BaseStatusEnum.NORMAL.equals(merchantProduct.getStatus())) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Course course = courseRepository.find(courseId);
|
|
|
+ CourseItemVo courseItemVo = new CourseItemVo();
|
|
|
+ BeanUtils.copyProperties(course, courseItemVo);
|
|
|
+ List<Goods> goodsList = goodsRepository.findByPidAndMerchantId(courseId, merchantId);
|
|
|
+ List<ItemGoodsVo> itemGoodsVoList = new ArrayList<>();
|
|
|
+ for (Goods goods : goodsList) {
|
|
|
+ ItemGoodsVo itemGoodsVo = convert(goods);
|
|
|
+ itemGoodsVoList.add(itemGoodsVo);
|
|
|
+ }
|
|
|
+ courseItemVo.setGoods(itemGoodsVoList);
|
|
|
+ return courseItemVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public SupportItemVo findSupportItem(String supportId, String merchantId) {
|
|
|
+ MerchantProduct merchantProduct = merchantProductRepository.findByPidAndMerchantId(supportId, merchantId);
|
|
|
+ if (merchantProduct == null || !BaseStatusEnum.NORMAL.equals(merchantProduct.getStatus())) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Support support = supportRepository.find(supportId);
|
|
|
+ SupportItemVo supportItemVo = new SupportItemVo();
|
|
|
+ BeanUtils.copyProperties(support, supportItemVo);
|
|
|
+ List<Goods> goodsList = goodsRepository.findByPidAndMerchantId(supportId, merchantId);
|
|
|
+ List<ItemGoodsVo> itemGoodsVoList = new ArrayList<>();
|
|
|
+ for (Goods goods : goodsList) {
|
|
|
+ ItemGoodsVo itemGoodsVo = convert(goods);
|
|
|
+ itemGoodsVoList.add(itemGoodsVo);
|
|
|
+ }
|
|
|
+ supportItemVo.setGoods(itemGoodsVoList);
|
|
|
+
|
|
|
+ // 取相关周边
|
|
|
+ List<SupportReference> supportReferenceList = supportReferenceRepository.findByFid(supportId, BaseStatusEnum.NORMAL);
|
|
|
+ if (!CollectionUtils.isEmpty(supportReferenceList)) {
|
|
|
+ List<String> supportIdList = new ArrayList<>();
|
|
|
+ for (SupportReference supportReference : supportReferenceList) {
|
|
|
+ supportIdList.add(supportReference.getTid());
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(supportIdList)) {
|
|
|
+ List<MerchantProduct> merchantProductList = merchantProductRepository.findByPidsAndMerchantId(supportIdList, merchantId);
|
|
|
+ List<String> idList = new ArrayList<>();
|
|
|
+ merchantProductList.forEach(item -> idList.add(item.getPid()));
|
|
|
+ List<Support> supportList = supportRepository.findByIds(idList, BaseStatusEnum.NORMAL);
|
|
|
+ Map<String, Support> supportMap = ListUtil.convert(supportList, "id", Support.class);
|
|
|
+ List<ItemVo> itemVoList = new ArrayList<>();
|
|
|
+ for (String itemId : supportIdList) {
|
|
|
+ Support item = supportMap.get(itemId);
|
|
|
+ if (item == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ ItemVo itemVo = convert(item);
|
|
|
+ itemVoList.add(itemVo);
|
|
|
+ }
|
|
|
+ supportItemVo.setRelatedSupports(itemVoList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 取相关课程
|
|
|
+ List<CourseSupportRelation> courseSupportRelationList = courseSupportRelationRepository.findBySupportId(supportId);
|
|
|
+ if (!CollectionUtils.isEmpty(courseSupportRelationList)) {
|
|
|
+ List<String> courseIdList = new ArrayList<>();
|
|
|
+ for (CourseSupportRelation relation : courseSupportRelationList) {
|
|
|
+ courseIdList.add(relation.getCourseId());
|
|
|
+ }
|
|
|
+ if (!CollectionUtils.isEmpty(courseIdList)) {
|
|
|
+ List<MerchantProduct> merchantProductList = merchantProductRepository.findByPidsAndMerchantId(courseIdList, merchantId);
|
|
|
+ List<String> idList = new ArrayList<>();
|
|
|
+ merchantProductList.forEach(item -> idList.add(item.getPid()));
|
|
|
+ List<Course> courseList = courseRepository.findByIds(idList, BaseStatusEnum.NORMAL);
|
|
|
+ Map<String, Course> courseMap = ListUtil.convert(courseList, "id", Course.class);
|
|
|
+ List<ItemVo> itemVoList = new ArrayList<>();
|
|
|
+ for (String itemId : courseIdList) {
|
|
|
+ Course item = courseMap.get(itemId);
|
|
|
+ if (item == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ ItemVo itemVo = convert(item);
|
|
|
+ itemVoList.add(itemVo);
|
|
|
+ }
|
|
|
+ supportItemVo.setRelatedCourses(itemVoList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return supportItemVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<ItemVo> findSupportByCourseId(String courseId, String merchantId) {
|
|
|
+ List<CourseSupportRelation> relationList = courseSupportRelationRepository.findByCourseId(courseId, BaseStatusEnum.NORMAL);
|
|
|
+ if (CollectionUtils.isEmpty(relationList)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ List<String> productIdList = new ArrayList<>();
|
|
|
+ relationList.forEach(relation -> productIdList.add(relation.getSupportId()));
|
|
|
+ List<ItemVo> itemVoList = this.findByIds(merchantId, productIdList);
|
|
|
+ return itemVoList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<ItemVo> findByIds(String merchantId, List<String> productIdList) {
|
|
|
+ if (CollectionUtils.isEmpty(productIdList)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ List<MerchantProduct> merchantProductList = merchantProductRepository.findByPidsAndMerchantId(productIdList, merchantId);
|
|
|
+ List<String> courseIdList = new ArrayList<>();
|
|
|
+ List<String> supportIdList = new ArrayList<>();
|
|
|
+ for (MerchantProduct merchantProduct : merchantProductList) {
|
|
|
+ String productId = merchantProduct.getPid();
|
|
|
+ ProductTypeEnum type = merchantProduct.getType();
|
|
|
+ if (type.equals(ProductTypeEnum.COURSE)) {
|
|
|
+ courseIdList.add(productId);
|
|
|
+ } else if (type.equals(ProductTypeEnum.SUPPORT)) {
|
|
|
+ supportIdList.add(productId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Course> courseList = new ArrayList<>();
|
|
|
+ if (!CollectionUtils.isEmpty(courseIdList)) {
|
|
|
+ courseList = courseRepository.findByIds(productIdList);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<Support> supportList = new ArrayList<>();
|
|
|
+ if (!CollectionUtils.isEmpty(supportIdList)) {
|
|
|
+ supportList = supportRepository.findByIds(supportIdList);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 拿产品信息
|
|
|
+ Map<String, ItemVo> itemVoMap = new HashMap<>();
|
|
|
+ for (Course course : courseList) {
|
|
|
+ String productId = course.getId();
|
|
|
+ ItemVo itemVo = convert(course);
|
|
|
+ itemVoMap.put(productId, itemVo);
|
|
|
+ }
|
|
|
+ for (Support support : supportList) {
|
|
|
+ String productId = support.getId();
|
|
|
+ ItemVo itemVo = convert(support);
|
|
|
+ itemVoMap.put(productId, itemVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 取商品信息
|
|
|
+ List<Goods> goodsList = goodsRepository.findByPidsAndMerchantId(productIdList, merchantId);
|
|
|
+ Map<String, List<Goods>> goodsMap = new HashMap<>();
|
|
|
+ for (Goods goods : goodsList) {
|
|
|
+ String productId = goods.getPid();
|
|
|
+ if (!goodsMap.containsKey(productId)) {
|
|
|
+ List<Goods> tmpList = new ArrayList<>();
|
|
|
+ tmpList.add(goods);
|
|
|
+ goodsMap.put(productId, tmpList);
|
|
|
+ }else {
|
|
|
+ List<Goods> tmpList = goodsMap.get(productId);
|
|
|
+ tmpList.add(goods);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 开始组装返回数据了啊
|
|
|
+ List<ItemVo> itemVoList = new ArrayList<>();
|
|
|
+ for (String productId : productIdList) {
|
|
|
+ ItemVo itemVo = itemVoMap.get(productId);
|
|
|
+ if (itemVo == null) {
|
|
|
+ log.error("item not exists, pid={}", productId);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ List<Goods> tmpList = goodsMap.get(productId);
|
|
|
+ tmpList.sort(new Comparator<Goods>() {
|
|
|
+ @Override
|
|
|
+ public int compare(Goods o1, Goods o2) {
|
|
|
+ return o1.getSort() - o2.getSort();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ List<ItemGoodsVo> itemGoodsVoList = new ArrayList<>();
|
|
|
+ for (Goods goods : tmpList) {
|
|
|
+ ItemGoodsVo itemGoodsVo = convert(goods);
|
|
|
+ itemGoodsVoList.add(itemGoodsVo);
|
|
|
+ }
|
|
|
+ itemVo.setGoods(itemGoodsVoList);
|
|
|
+ itemVoList.add(itemVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ return itemVoList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public APIResult<Page<ItemVo>> findPageByTagIdAndPids(String merchantId, String tagId, List<String> productIdList, Integer pageNo, Integer pageSize) {
|
|
|
+ Page<ItemVo> page = new Page((long)productIdList.size(), pageNo, pageSize);
|
|
|
+ List<MerchantProductTagRelation> relationList = merchantProductTagRelationRepository.findByTagIdAndPids(tagId, productIdList, page.getStart(), pageSize);
|
|
|
+ if (CollectionUtils.isEmpty(relationList)) {
|
|
|
+ return APIResult.ok(page);
|
|
|
+ }
|
|
|
+ List<String> currentIdList = new ArrayList<>();
|
|
|
+ for (MerchantProductTagRelation merchantProductTagRelation : relationList) {
|
|
|
+ String productId = merchantProductTagRelation.getPid();
|
|
|
+ currentIdList.add(productId);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<ItemVo> itemVoList = findByIds(merchantId, currentIdList);
|
|
|
+ page.setList(itemVoList);
|
|
|
+ return APIResult.ok(page);
|
|
|
+ }
|
|
|
+
|
|
|
+ public APIResult<Page<ItemVo>> findPageByTagId(String tagId, String merchantId, Integer pageNo, Integer pageSize) {
|
|
|
+ MerchantProductTagRelation relation = new MerchantProductTagRelation();
|
|
|
+ relation.setTagId(tagId);
|
|
|
+ relation.setMerchantId(merchantId);
|
|
|
+ Long total = merchantProductTagRelationRepository.count(relation);
|
|
|
+
|
|
|
+ Page<ItemVo> page = new Page(total, pageNo, pageSize);
|
|
|
+ List<MerchantProductTagRelation> relationList = merchantProductTagRelationRepository.find(relation, page.getStart(), pageSize);
|
|
|
+ if (CollectionUtils.isEmpty(relationList)) {
|
|
|
+ return APIResult.ok(page);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> productIdList = new ArrayList<>();
|
|
|
+ for (MerchantProductTagRelation merchantProductTagRelation : relationList) {
|
|
|
+ String productId = merchantProductTagRelation.getPid();
|
|
|
+ productIdList.add(productId);
|
|
|
+ }
|
|
|
+ List<ItemVo> itemVoList = this.findByIds(merchantId, productIdList);
|
|
|
+ page.setList(itemVoList);
|
|
|
+ return APIResult.ok(page);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ItemVo convert(Course course) {
|
|
|
+ ItemVo itemVo = new ItemVo();
|
|
|
+ itemVo.setId(course.getId());
|
|
|
+ itemVo.setCode(course.getCode());
|
|
|
+ itemVo.setTitle(course.getTitle());
|
|
|
+ itemVo.setSubTitle(course.getSubTitle());
|
|
|
+ itemVo.setBreadCrumb(course.getBreadCrumb());
|
|
|
+ itemVo.setType(course.getType());
|
|
|
+ itemVo.setCoverUrl(course.getCoverUrl());
|
|
|
+ return itemVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ItemVo convert(Support support) {
|
|
|
+ ItemVo itemVo = new ItemVo();
|
|
|
+ itemVo.setId(support.getId());
|
|
|
+ itemVo.setCode(support.getCode());
|
|
|
+ itemVo.setTitle(support.getTitle());
|
|
|
+ itemVo.setSubTitle(support.getSubTitle());
|
|
|
+ itemVo.setType(support.getType());
|
|
|
+ itemVo.setCoverUrl(support.getCoverUrl());
|
|
|
+ itemVo.setImgList(support.getImgList());
|
|
|
+ return itemVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static ItemGoodsVo convert(Goods goods) {
|
|
|
+ ItemGoodsVo itemGoodsVo = new ItemGoodsVo();
|
|
|
+ itemGoodsVo.setId(goods.getId());
|
|
|
+ itemGoodsVo.setChargeUnit(goods.getChargeUnit());
|
|
|
+ itemGoodsVo.setTerminalPrice(goods.getTerminalPrice());
|
|
|
+ return itemGoodsVo;
|
|
|
+ }
|
|
|
+}
|