package com.jtzx.crm.module.sys.service; import cc.uncarbon.framework.core.context.UserContextHolder; import cc.uncarbon.framework.core.exception.BusinessException; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.collection.CollUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.google.common.collect.Lists; import com.jtzx.crm.module.sys.entity.SysCustomerDataEntity; import com.jtzx.crm.module.sys.entity.SysCustomerFollowAuditEntity; import com.jtzx.crm.module.sys.mapper.SysCustomerDataMapper; import com.jtzx.crm.module.sys.mapper.SysCustomerFollowAuditMapper; import com.jtzx.crm.module.sys.model.response.SysCustomerFollowAuditBO; import com.jtzx.crm.module.sys.model.response.SysCustomerFollowAuditDataBO; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.*; /** * 赢单跟进审核信息 */ @RequiredArgsConstructor @Service @Slf4j public class SysCustomerFollowAuditService { private final SysCustomerFollowAuditMapper sysCustomerFollowAuditMapper; private final SysCustomerDataMapper sysCustomerDataMapper; /** * 赢单审核申请列表查询 */ public SysCustomerFollowAuditDataBO adminList(Integer auditStatus) { if (!UserContextHolder.getUserContext().getRoles().contains("Admin")) { throw new BusinessException(403, "无权操作"); } if(!Arrays.asList(0,1,2).contains(auditStatus)){ throw new BusinessException(400, "审核参数错误"); } SysCustomerFollowAuditDataBO resultData = SysCustomerFollowAuditDataBO.builder().build(); resultData.setAudit0(0); resultData.setAudit1(0); resultData.setAudit2(0); List entityPage = sysCustomerFollowAuditMapper.selectList( new QueryWrapper() .lambda() .eq(SysCustomerFollowAuditEntity::getAuditUserId, UserContextHolder.getUserId()) // 排序 .orderByDesc(SysCustomerFollowAuditEntity::getCreatedAt) ); if (auditStatus == null) { auditStatus = 0; } List dataList = Lists.newArrayList(); if (CollUtil.isNotEmpty(entityPage)) { resultData.setAudit0(entityPage.stream().filter(e -> e.getAuditStatus() == 0).toList().size()); resultData.setAudit1(entityPage.stream().filter(e -> e.getAuditStatus() == 1).toList().size()); resultData.setAudit2(entityPage.stream().filter(e -> e.getAuditStatus() == 2).toList().size()); Integer finalAuditStatus = auditStatus; List list = entityPage.stream() .filter(e -> Objects.equals(e.getAuditStatus(), finalAuditStatus)) .toList(); List boList = this.entityList2BOs(list); if (CollUtil.isNotEmpty(boList)) { dataList.addAll(boList); } } resultData.setAuditData(dataList); return resultData; } /** * 赢单审核通过 * auditStatus 1-通过,2-不通过 * @param id */ public void auditSuccess(Long id, Integer auditStatus) { if(!Arrays.asList(1,2).contains(auditStatus)){ throw new BusinessException(400, "审核参数错误"); } if (!UserContextHolder.getUserContext().getRoles().contains("Admin")) { throw new BusinessException(403, "无权操作"); } SysCustomerFollowAuditEntity entity = sysCustomerFollowAuditMapper.selectById(id); if (entity == null) { throw new BusinessException(400, "赢单跟进审核信息不存在"); } SysCustomerDataEntity sysCustomerDataEntity = sysCustomerDataMapper.selectById(entity.getCustomerId()); if (sysCustomerDataEntity == null) { throw new BusinessException(400, "客户数据不存在"); } sysCustomerFollowAuditMapper.updateById(entity); List sysCustomerFollowAuditEntities = sysCustomerFollowAuditMapper.selectList( new LambdaQueryWrapper<>(SysCustomerFollowAuditEntity.class) .select( SysCustomerFollowAuditEntity::getId ) .eq(SysCustomerFollowAuditEntity::getCustomerId, entity.getCustomerId()) ); if (CollUtil.isNotEmpty(sysCustomerFollowAuditEntities)) { sysCustomerFollowAuditEntities.forEach(e -> { e.setAuditStatus(auditStatus); e.setAuditUserId(UserContextHolder.getUserId()); e.setAuditUserName(UserContextHolder.getUserName()); sysCustomerFollowAuditMapper.updateById(e); }); } // 审核通过,设置客户数据表的赢单信息,以及赢单审核信息 if (auditStatus == 1) { sysCustomerDataEntity.setSuccessStatus(1); sysCustomerDataEntity.setSuccessUserId(entity.getClaimUserId()); sysCustomerDataEntity.setSuccessUserName(entity.getClaimUserName()); sysCustomerDataMapper.updateById(sysCustomerDataEntity); } } /* ---------------------------------------------------------------- 私有方法 private methods ---------------------------------------------------------------- */ /** * 实体转 BO * * @param entity 实体 * @return BO */ private SysCustomerFollowAuditBO entity2BO(SysCustomerFollowAuditEntity entity) { if (entity == null) { return null; } SysCustomerFollowAuditBO bo = new SysCustomerFollowAuditBO(); BeanUtil.copyProperties(entity, bo); // 可以在此处为BO填充字段 bo.setId(entity.getId()); return bo; } /** * 实体 List 转 BO List * * @param entityList 实体 List * @return BO List */ private List entityList2BOs(List entityList) { if (CollUtil.isEmpty(entityList)) { return Collections.emptyList(); } // 深拷贝 List ret = new ArrayList<>(entityList.size()); entityList.forEach( entity -> ret.add(this.entity2BO(entity)) ); return ret; } }