164 lines
		
	
	
		
			7.4 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
		
		
			
		
	
	
			164 lines
		
	
	
		
			7.4 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
|   | package com.upchina.common.service;
 | ||
|  | 
 | ||
|  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | ||
|  | import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 | ||
|  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | ||
|  | import com.google.common.collect.ImmutableList;
 | ||
|  | import com.google.common.collect.Table;
 | ||
|  | import com.hazelcast.core.HazelcastInstance;
 | ||
|  | import com.upchina.advisor.constant.AdvisorInfoStatus;
 | ||
|  | import com.upchina.common.config.cache.CacheKey;
 | ||
|  | import com.upchina.common.constant.ProductType;
 | ||
|  | import com.upchina.common.constant.ThirdPartyProductStatus;
 | ||
|  | import com.upchina.common.entity.Recommend;
 | ||
|  | import com.upchina.common.handler.BizException;
 | ||
|  | import com.upchina.common.mapper.RecommendMapper;
 | ||
|  | import com.upchina.common.query.*;
 | ||
|  | import com.upchina.common.result.Pager;
 | ||
|  | import com.upchina.common.result.ResponseStatus;
 | ||
|  | import com.upchina.common.vo.BackendUserVO;
 | ||
|  | import com.upchina.common.vo.MergeProductInfoVO;
 | ||
|  | import com.upchina.common.vo.RecommendVO;
 | ||
|  | import com.upchina.rbac.entity.UserDept;
 | ||
|  | import com.upchina.rbac.service.UserService;
 | ||
|  | import org.springframework.dao.DuplicateKeyException;
 | ||
|  | import org.springframework.stereotype.Service;
 | ||
|  | import org.springframework.transaction.annotation.Transactional;
 | ||
|  | 
 | ||
|  | import javax.annotation.Resource;
 | ||
|  | import java.util.List;
 | ||
|  | import java.util.Map;
 | ||
|  | import java.util.Objects;
 | ||
|  | import java.util.stream.Collectors;
 | ||
|  | 
 | ||
|  | @Service
 | ||
|  | public class RecommendService {
 | ||
|  | 
 | ||
|  |     @Resource
 | ||
|  |     private RecommendMapper recommendMapper;
 | ||
|  | 
 | ||
|  |     @Resource
 | ||
|  |     private UserService userService;
 | ||
|  | 
 | ||
|  |     @Resource
 | ||
|  |     private MergeProductService mergeProductService;
 | ||
|  | 
 | ||
|  |     @Resource
 | ||
|  |     private CacheService cacheService;
 | ||
|  | 
 | ||
|  |     @Resource
 | ||
|  |     private HazelcastInstance hazelcastInstance;
 | ||
|  | 
 | ||
|  |     public Pager<RecommendVO> list(ListRecommendQuery query) {
 | ||
|  |         Integer productType = query.getProductType();
 | ||
|  |         QueryWrapper<Recommend> wrapper = Wrappers.query();
 | ||
|  |         wrapper.eq(productType != null, "product_type", productType);
 | ||
|  |         wrapper.last("order by weight desc,create_time desc");
 | ||
|  |         Page<Recommend> page = recommendMapper.selectPage(query.toPage(), wrapper);
 | ||
|  |         Map<Integer, UserDept> userMap = userService.getUserMap();
 | ||
|  |         List<RecommendVO> list = page.getRecords().stream().map(recommend -> new RecommendVO(recommend, userMap.get(recommend.getCreateUserId()))).collect(Collectors.toList());
 | ||
|  |         // 填充产品名称
 | ||
|  |         Table<Integer, Integer, MergeProductInfoVO> productTable = mergeProductService.queryMergeProductInfo(list);
 | ||
|  |         list.forEach(vo -> {
 | ||
|  |             MergeProductInfoVO productVO = productTable.get(vo.getProductType(), vo.getProductId());
 | ||
|  |             if (productVO != null) {
 | ||
|  |                 vo.setName(productVO.getProductName());
 | ||
|  |                 vo.setAdvisorBasic(productVO.getAdvisorBasic());
 | ||
|  |                 vo.setPlayType(productVO.getVideoPlayType());
 | ||
|  |             }
 | ||
|  |         });
 | ||
|  |         return new Pager<>(list, page.getTotal());
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     @Transactional(rollbackFor = Exception.class)
 | ||
|  |     public void save(SaveRecommendQuery query, BackendUserVO backendUserVO) {
 | ||
|  |         validateProduct(query);
 | ||
|  |         QueryWrapper<Recommend> wrapper = Wrappers.query();
 | ||
|  |         wrapper.eq("product_type", query.getProductType());
 | ||
|  |         long count = recommendMapper.selectCount(wrapper);
 | ||
|  |         if (count > 9) {
 | ||
|  |             throw new BizException(ResponseStatus.PARM_ERROR, "推荐位最多设置10个产品");
 | ||
|  |         }
 | ||
|  |         Recommend recommend = query.toPO(backendUserVO.getUserId());
 | ||
|  |         try {
 | ||
|  |             recommendMapper.insert(recommend);
 | ||
|  |         } catch (DuplicateKeyException e) {
 | ||
|  |             throw new BizException(ResponseStatus.RECOMMEND_DUPLICATE);
 | ||
|  |         }
 | ||
|  |         this.clearCache(ProductType.fromValue(query.getProductType()));
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     @Transactional(rollbackFor = Exception.class)
 | ||
|  |     public void update(UpdateRecommendQuery query, BackendUserVO backendUserVO) {
 | ||
|  |         validateProduct(query);
 | ||
|  |         Recommend recommend = query.toPO();
 | ||
|  |         int rows;
 | ||
|  |         try {
 | ||
|  |             rows = recommendMapper.updateById(recommend);
 | ||
|  |         } catch (DuplicateKeyException e) {
 | ||
|  |             throw new BizException(ResponseStatus.RECOMMEND_DUPLICATE);
 | ||
|  |         }
 | ||
|  |         if (rows == 0) {
 | ||
|  |             throw new BizException(ResponseStatus.ID_NOT_EXIST_ERROR);
 | ||
|  |         }
 | ||
|  |         this.clearCache(ProductType.fromValue(query.getProductType()));
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     @Transactional(rollbackFor = Exception.class)
 | ||
|  |     public void delete(OnlyIdQuery query, BackendUserVO backendUserVO) {
 | ||
|  |         Recommend recommend = recommendMapper.selectById(query.getId());
 | ||
|  |         if (recommend == null) {
 | ||
|  |             throw new BizException(ResponseStatus.ID_NOT_EXIST_ERROR);
 | ||
|  |         }
 | ||
|  |         recommendMapper.deleteById(query.getId());
 | ||
|  |         this.clearCache(ProductType.fromValue(recommend.getProductType()));
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public List<Object> listForApp(Integer productType) {
 | ||
|  |         return cacheService.get(CacheKey.RECOMMEND, CacheKey.RecommendKey.APP_RECOMMEND_LIST + productType, () -> {
 | ||
|  |             QueryWrapper<Recommend> wrapper = Wrappers.query();
 | ||
|  |             wrapper.eq("product_type", productType).last("order by weight desc,create_time desc");
 | ||
|  |             List<Recommend> list = recommendMapper.selectList(wrapper);
 | ||
|  |             Table<Integer, Integer, Object> productTable = mergeProductService.queryMergeProductInfo(list, true);
 | ||
|  |             return list.stream().map(recommend -> productTable.get(recommend.getProductType(), recommend.getProductId())).filter(Objects::nonNull).collect(Collectors.toList());
 | ||
|  |         });
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public void clearCache(ProductType productType) {
 | ||
|  |         Map<String, Object> cacheMap = hazelcastInstance.getMap(CacheKey.RECOMMEND);
 | ||
|  |         cacheMap.remove(CacheKey.RecommendKey.APP_RECOMMEND_LIST + productType.value);
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public void validateProduct(IProduct product) {
 | ||
|  |         ProductType productType = ProductType.fromValue(product.getProductType());
 | ||
|  |         if (ProductType.H5.equals(productType)) {
 | ||
|  |             return;
 | ||
|  |         }
 | ||
|  |         Table<Integer, Integer, MergeProductInfoVO> table = mergeProductService.queryMergeProductInfo(ImmutableList.of(product));
 | ||
|  |         if (table.isEmpty()) {
 | ||
|  |             throw new BizException(ResponseStatus.PRODUCT_NOT_EXIST);
 | ||
|  |         }
 | ||
|  |         MergeProductInfoVO vo = table.get(product.getProductType(), product.getProductId());
 | ||
|  |         if ((ProductType.ADVISOR_INFO.equals(productType) && !AdvisorInfoStatus.PASS.value.equals(vo.getStatus()))
 | ||
|  |                 || (ProductType.THIRD_VALUE_PRODUCT.equals(productType) && !ThirdPartyProductStatus.PASS.value.equals(vo.getStatus()))
 | ||
|  |                 || (ProductType.THIRD_COURSE.equals(productType) && !ThirdPartyProductStatus.PASS.value.equals(vo.getStatus()))
 | ||
|  |                 || (ProductType.THIRD_ETF.equals(productType) && !ThirdPartyProductStatus.PASS.value.equals(vo.getStatus()))
 | ||
|  |                 || (ProductType.THIRD_STOCK_TOOL.equals(productType) && !ThirdPartyProductStatus.PASS.value.equals(vo.getStatus()))
 | ||
|  |         ) {
 | ||
|  |             throw new BizException(ResponseStatus.PRODUCT_STATUS_ERROR);
 | ||
|  |         }
 | ||
|  |     }
 | ||
|  | 
 | ||
|  |     public void validateRecommendExist(ProductType productType, Integer productId) {
 | ||
|  |         // TODO 校验关联数据(投顾\观点\观点包\锦囊\三方产品\套餐产品)
 | ||
|  |         QueryWrapper<Recommend> recommendWrapper = Wrappers.query();
 | ||
|  |         recommendWrapper.eq("product_type", productType.value)
 | ||
|  |                 .eq("product_id", productId);
 | ||
|  |         Long recommendCount = recommendMapper.selectCount(recommendWrapper);
 | ||
|  |         if (recommendCount > 0) {
 | ||
|  |             throw new BizException(ResponseStatus.RECOMMEND_CAN_NOT_SOLD_OUT);
 | ||
|  |         }
 | ||
|  |     }
 | ||
|  | 
 | ||
|  | }
 |