161 lines
7.0 KiB
Java
161 lines
7.0 KiB
Java
package com.syzb.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.syzb.advisor.constant.AdvisorInfoStatus;
|
|
import com.syzb.common.config.cache.CacheKey;
|
|
import com.syzb.common.constant.ProductType;
|
|
import com.syzb.common.constant.ThirdPartyProductStatus;
|
|
import com.syzb.common.entity.Recommend;
|
|
import com.syzb.common.handler.BizException;
|
|
import com.syzb.common.mapper.RecommendMapper;
|
|
import com.syzb.common.query.*;
|
|
import com.syzb.common.result.Pager;
|
|
import com.syzb.common.result.ResponseStatus;
|
|
import com.syzb.common.vo.BackendUserVO;
|
|
import com.syzb.common.vo.MergeProductInfoVO;
|
|
import com.syzb.common.vo.RecommendVO;
|
|
import com.syzb.rbac.entity.UserDept;
|
|
import com.syzb.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_PRODUCT.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);
|
|
}
|
|
}
|
|
|
|
}
|