package cn.fw.valhalla.service.bus.follow; import cn.fw.common.page.AppPage; import cn.fw.common.web.auth.LoginAuthBean; import cn.fw.valhalla.common.utils.DateUtil; import cn.fw.valhalla.common.utils.StringUtils; import cn.fw.valhalla.domain.db.customer.AffiliationRecord; import cn.fw.valhalla.domain.dto.*; import cn.fw.valhalla.domain.enums.*; import cn.fw.valhalla.domain.query.*; import cn.fw.valhalla.domain.vo.AppPageVO; import cn.fw.valhalla.domain.vo.pool.*; import cn.fw.valhalla.rpc.ehr.EhrRpcService; import cn.fw.valhalla.rpc.erp.dto.UserInfoDTO; import cn.fw.valhalla.rpc.oop.OopService; import cn.fw.valhalla.rpc.oop.dto.ShopDTO; import cn.fw.valhalla.rpc.order.OrderRpcService; import cn.fw.valhalla.rpc.order.dto.NewCarOrderInfo; import cn.fw.valhalla.service.data.*; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import java.time.LocalDate; import java.util.*; import java.util.stream.Collectors; import static cn.fw.common.businessvalidator.Validator.BV; /** * @author : kurisu * @className : FollowPoolBizService * @description : 跟进池服务 * @date: 2020-09-23 15:30 */ @Slf4j @Service @RequiredArgsConstructor public class PoolBizService { private final PublicPoolService publicPoolService; private final StammkundePoolService stammkundePoolService; private final PublicCluePoolService publicCluePoolService; private final OopService oopService; private final EhrRpcService ehrRpcService; private final AffiliationRecordService affiliationRecordService; private final OrderRpcService orderRpcService; private final ClueTaskService clueTaskService; private final PubCluePoolService pubCluePoolService; private final FollowClueService followClueService; /** * 跟进池 * * @param queryVO * @return */ public AppPage followList(LoginAuthBean user, FollowPoolQueryVO queryVO) { prepareParams(user, queryVO); AppPageVO page = AppPageVO.init(queryVO); List list; if (FollowTypeEnum.PL.getValue().equals(queryVO.getType())) { final long total = pubCluePoolService.followListCount(queryVO); if (total <= 0) { return page; } page.setTotal(total); list = pubCluePoolService.followList(queryVO); } else { final long total = clueTaskService.followListCount(queryVO); if (total <= 0) { return page; } page.setTotal(total); list = clueTaskService.followList(queryVO); } List followList = new ArrayList<>(); for (FollowPoolDTO followPoolDTO : list) { FollowPoolListVO vo = trans2FollowPool(followPoolDTO); followList.add(vo); } page.setData(followList); return page; } /** * 公共池 * * @param queryVO * @return */ public AppPage publicList(LoginAuthBean currentUser, PublicPoolQueryVO queryVO) { if (!Boolean.TRUE.equals(queryVO.getFromReport())) { List affiliationRecordList = Optional.ofNullable(affiliationRecordService.list(Wrappers.lambdaQuery() .eq(AffiliationRecord::getType, CustomerChangeTypeEnum.DEFEAT) .eq(AffiliationRecord::getOriginUserId, currentUser.getUserId()) .ge(AffiliationRecord::getDefeatTime, DateUtil.getNowExpiredMonth(-6)) )).orElse(new ArrayList<>()); List custIds = affiliationRecordList.stream().map(AffiliationRecord::getCustomerId).distinct().collect(Collectors.toList()); if (custIds.size() > 0) { queryVO.setIgnoreCustIds(custIds); } } AppPageVO page = AppPageVO.init(queryVO); Long count = publicPoolService.publicListCount(queryVO); if (count <= 0) { return page; } List appList = new ArrayList<>(); List list = publicPoolService.publicList(queryVO); for (PublicPoolDTO dto : list) { PublicPoolVO vo = new PublicPoolVO(); BeanUtils.copyProperties(dto, vo); PublicPoolTypeEnum typeEnum = PublicPoolTypeEnum.ofValue(dto.getType()); if (Objects.nonNull(typeEnum)) { vo.setTypeDesc(typeEnum.getName()); } if (StringUtils.isEmpty(dto.getTags())) { vo.setMaintainCard(Boolean.FALSE); vo.setWarrantyCard(Boolean.FALSE); } else { vo.setMaintainCard(dto.getTags().contains(CusTagEnum.MAINTAIN_CARD.getValue())); vo.setWarrantyCard(dto.getTags().contains(CusTagEnum.QUA_ASS_CARD.getValue())); } appList.add(vo); } page.setTotal(count); page.setData(appList); return page; } /** * 保有客池 * * @param user * @param queryVO * @return */ public AppPage stammkundeList(LoginAuthBean user, StammkundePoolQueryVO queryVO) { prepareParams(user, queryVO); AppPageVO page = AppPageVO.init(queryVO); long total = stammkundePoolService.stammkundeListCount(queryVO); if (total <= 0) { return page; } page.setTotal(total); List list = stammkundePoolService.stammkundeList(queryVO); List appList = new ArrayList<>(); for (StammkundePoolDTO dto : list) { StammkundePoolVO vo = trans2StaPool(dto); appList.add(vo); } page.setData(appList); return page; } /** * 客户线索池 * * @param queryVO * @return */ public AppPage clueList(CustomerCluePoolQueryVO queryVO) { prepareParams(null, queryVO); BV.isNotEmpty(queryVO.getScope(), () -> "人员权限范围不正确,请确认是否有管理权限"); if (ClueStatusEnum.ONGOING.getValue().equals(queryVO.getStatus())) { queryVO.setCloseTime(null); } AppPageVO page = AppPageVO.init(queryVO); long total = followClueService.clueListCount(queryVO); if (total <= 0) { return page; } page.setTotal(total); final HashSet scope = new HashSet<>(queryVO.getScope()); List list = followClueService.clueList(queryVO); List appList = new ArrayList<>(); for (FollowClueDTO dto : list) { CustomerCluePoolVO vo = trans2CluePool(dto, scope); appList.add(vo); } page.setData(appList); return page; } /** * 线索池概况 * * @param queryVO * @return */ public CustomerClueSummaryVO summary(CustomerCluePoolQueryVO queryVO) { prepareParams(null, queryVO); BV.isNotEmpty(queryVO.getScope(), () -> "人员权限范围不正确,请确认是否有管理权限"); CustomerClueSummaryVO vo = new CustomerClueSummaryVO(); vo.setCompleteNum(followClueService.completeNum(queryVO)); vo.setDefeatNum(followClueService.defeatNum(queryVO)); vo.setOnGoingNum(followClueService.onGoingNum(queryVO)); vo.setClosedNum(vo.getCompleteNum() + vo.getDefeatNum()); return vo; } /** * 公共客户线索池 * * @param queryVO * @return */ public AppPage publicClueList(PublicCluePoolQueryVO queryVO, LoginAuthBean user) { prepareParams(user, queryVO); AppPageVO page = AppPageVO.init(queryVO); long total = publicCluePoolService.publicCluePoolListCount(queryVO); if (total <= 0) { return page; } page.setTotal(total); List list = publicCluePoolService.publicCluePoolList(queryVO); List appList = new ArrayList<>(); for (PublicCluePoolDTO dto : list) { PublicCluePoolVO vo = new PublicCluePoolVO(); BeanUtils.copyProperties(dto, vo); if (StringUtils.isEmpty(dto.getTags())) { vo.setMaintainCard(Boolean.FALSE); vo.setWarrantyCard(Boolean.FALSE); } else { vo.setMaintainCard(dto.getTags().contains(CusTagEnum.MAINTAIN_CARD.getValue())); vo.setWarrantyCard(dto.getTags().contains(CusTagEnum.QUA_ASS_CARD.getValue())); } if (Objects.nonNull(dto.getBuyDate())) { LocalDate buyDate = DateUtil.date2LocalDate(dto.getBuyDate()); long days = LocalDate.now().toEpochDay() - buyDate.toEpochDay(); double years = Math.ceil(days / 365.0); vo.setAgeLimit(Math.round(years)); } appList.add(vo); } page.setData(appList); return page; } /** * 跟进池数据转换 * * @param poolDTO * @return */ private FollowPoolListVO trans2FollowPool(FollowPoolDTO poolDTO) { FollowPoolListVO vo = new FollowPoolListVO(); BeanUtils.copyProperties(poolDTO, vo); vo.setFollower(poolDTO.getFollowUserName()); if (StringUtils.isEmpty(poolDTO.getFollowUserName()) && Objects.nonNull(poolDTO.getUserId())) { UserInfoDTO user = ehrRpcService.user(poolDTO.getUserId()); if (Objects.nonNull(user)) { vo.setFollower(user.getUserName()); } } if (Objects.nonNull(poolDTO.getShopId())) { ShopDTO shop = oopService.shop(poolDTO.getShopId()); if (Objects.nonNull(shop)) { vo.setShopName(shop.getShortName()); } } if (Objects.nonNull(poolDTO.getFinishShop())) { ShopDTO fshop = oopService.shop(poolDTO.getFinishShop()); if (Objects.nonNull(fshop)) { vo.setFinishShopName(fshop.getShortName()); } } if (Objects.nonNull(poolDTO.getLoanExpires())) { vo.setLoanCustomer(DateUtil.date2LocalDate(poolDTO.getLoanExpires()).isAfter(LocalDate.now())); vo.setLoanDate(poolDTO.getLoanExpires()); } TaskDefeatTypeEnum defeatTypeEnum = TaskDefeatTypeEnum.ofValue(poolDTO.getInitiative()); if (Objects.nonNull(defeatTypeEnum)) { vo.setDefeatType(defeatTypeEnum.getName()); } return vo; } private StammkundePoolVO trans2StaPool(StammkundePoolDTO dto) { StammkundePoolVO vo = new StammkundePoolVO(); BeanUtils.copyProperties(dto, vo); ShopDTO shop = oopService.shop(dto.getShopId()); if (Objects.nonNull(shop)) { vo.setShopName(shop.getShortName()); } if (Objects.nonNull(dto.getLoanExpires())) { vo.setLoanCustomer(DateUtil.date2LocalDate(dto.getLoanExpires()).isAfter(LocalDate.now())); } String tags = dto.getTags(); if (StringUtils.isEmpty(tags)) { vo.setMaintainCard(Boolean.FALSE); vo.setWarrantyCard(Boolean.FALSE); } else { vo.setMaintainCard(tags.contains(CusTagEnum.MAINTAIN_CARD.getValue())); vo.setWarrantyCard(tags.contains(CusTagEnum.QUA_ASS_CARD.getValue())); } if (Objects.nonNull(dto.getReason())) { DefeatReasonEnum reasonEnum = DefeatReasonEnum.ofValue(dto.getReason()); vo.setReason(Objects.nonNull(reasonEnum) ? reasonEnum.getName() : null); } if (Objects.nonNull(dto.getBuyDate())) { LocalDate buyDate = DateUtil.date2LocalDate(dto.getBuyDate()); long days = LocalDate.now().toEpochDay() - buyDate.toEpochDay(); double years = Math.ceil(days / 365.0); vo.setAgeLimit(Math.round(years)); } return vo; } private CustomerCluePoolVO trans2CluePool(FollowClueDTO poolDTO, Set scope) { CustomerCluePoolVO vo = new CustomerCluePoolVO(); BeanUtils.copyProperties(poolDTO, vo); vo.setStatus(poolDTO.getClueStatus()); if (Objects.nonNull(poolDTO.getLoanExpires())) { vo.setLoanCustomer(DateUtil.date2LocalDate(poolDTO.getLoanExpires()).isAfter(LocalDate.now())); } if (ClueStatusEnum.COMPLETE.getValue().equals(poolDTO.getClueStatus())) { vo.setStatus(ClueStatusEnum.COMPLETE.getValue()); } if (ClueStatusEnum.FAILURE.getValue().equals(poolDTO.getClueStatus())) { vo.setStatus(ClueStatusEnum.FAILURE.getValue()); } //首保 if (FollowTypeEnum.FM.getValue().equals(poolDTO.getType())) { NewCarOrderInfo orderInfo = orderRpcService.getOrderInfo(poolDTO.getFrameNo()); if (Objects.nonNull(orderInfo)) { ShopDTO shop = oopService.shop(orderInfo.getShopId()); vo.setSaleShop(Objects.nonNull(shop) ? shop.getShortName() : "-"); } } return vo; } private void prepareParams(LoginAuthBean user, PoolQuery queryVO) { if (Objects.nonNull(user)) { queryVO.setUserId(user.getUserId()); queryVO.setGroupId(user.getGroupId()); } if (Objects.nonNull(queryVO.getOrder()) && StringUtils.isValid(queryVO.getOrderAtt())) { StringBuilder sb = new StringBuilder(" order by "); sb.append(StringUtils.toColumnName(queryVO.getOrderAtt()).toLowerCase()); if (queryVO.getOrder() == 1) { sb.append(" asc "); } else { sb.append(" desc "); } queryVO.setOrderString(sb.toString()); } } }