| | |
| | | package cn.iocoder.yudao.module.crm.dal.mysql.contract; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.date.LocalDateTimeUtil; |
| | | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| | |
| | | import cn.iocoder.yudao.module.crm.enums.common.CrmBizTypeEnum; |
| | | import cn.iocoder.yudao.module.crm.enums.common.CrmSceneTypeEnum; |
| | | import cn.iocoder.yudao.module.crm.util.CrmPermissionUtils; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import org.apache.ibatis.annotations.Mapper; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDateTime; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; |
| | | |
| | | /** |
| | | * CRM 合同 Mapper |
| | |
| | | } |
| | | |
| | | default PageResult<CrmContractDO> selectPage(CrmContractPageReqVO pageReqVO, Long userId, CrmContractConfigDO config) { |
| | | return selectPage(pageReqVO, userId, config, null); |
| | | } |
| | | |
| | | /** |
| | | * 分页查询(带客户权限过滤) |
| | | * |
| | | * @param pageReqVO 查询条件 |
| | | * @param userId 用户ID |
| | | * @param config 合同配置 |
| | | * @param permittedCustomerIds 有权限的客户ID列表(null表示不限制) |
| | | */ |
| | | default PageResult<CrmContractDO> selectPage(CrmContractPageReqVO pageReqVO, Long userId, CrmContractConfigDO config, List<Long> permittedCustomerIds) { |
| | | MPJLambdaWrapperX<CrmContractDO> query = new MPJLambdaWrapperX<>(); |
| | | // 拼接数据权限的查询条件 |
| | | CrmPermissionUtils.appendPermissionCondition(query, CrmBizTypeEnum.CRM_CONTRACT.getType(), |
| | | CrmContractDO::getId, userId, pageReqVO.getSceneType()); |
| | | // 拼接自身的查询条件 |
| | | query.selectAll(CrmContractDO.class) |
| | | .inIfPresent(CrmContractDO::getId, pageReqVO.getIds()) |
| | | .likeIfPresent(CrmContractDO::getNo, pageReqVO.getNo()) |
| | | .likeIfPresent(CrmContractDO::getName, pageReqVO.getName()) |
| | | .eqIfPresent(CrmContractDO::getCustomerId, pageReqVO.getCustomerId()) |
| | | .eqIfPresent(CrmContractDO::getBusinessId, pageReqVO.getBusinessId()) |
| | | .eqIfPresent(CrmContractDO::getAuditStatus, pageReqVO.getAuditStatus()) |
| | | .orderByDesc(CrmContractDO::getId); |
| | | |
| | | // 客户权限过滤 |
| | | if (permittedCustomerIds != null) { |
| | | if (CollUtil.isEmpty(permittedCustomerIds)) { |
| | | return PageResult.empty(); |
| | | } |
| | | query.in(CrmContractDO::getCustomerId, permittedCustomerIds); |
| | | } |
| | | |
| | | // Backlog: 即将到期的合同 |
| | | LocalDateTime beginOfToday = LocalDateTimeUtil.beginOfDay(LocalDateTime.now()); |
| | |
| | | |
| | | default CrmContractDO selectByBusinessId(Long businessId) { |
| | | return selectOne(CrmContractDO::getBusinessId, businessId); |
| | | } |
| | | |
| | | default CrmContractDO selectByOrderId(Long orderId) { |
| | | return selectOne(CrmContractDO::getOrderId, orderId); |
| | | } |
| | | |
| | | default List<CrmContractDO> selectListByCustomerId(Long customerId) { |
| | |
| | | .reduce(BigDecimal.ZERO, BigDecimal::add); |
| | | } |
| | | |
| | | // ========== 按客户批量统计(客户列表展示「签订合同」) ========== |
| | | |
| | | /** |
| | | * 批量统计各客户「已签订」(审核通过)的合同数量 |
| | | * |
| | | * @param customerIds 客户编号集合 |
| | | * @return key:客户编号,value:合同数量。没有合同的客户不在 map 中 |
| | | */ |
| | | default Map<Long, Long> selectContractCountMapByCustomerIds(Collection<Long> customerIds) { |
| | | if (CollUtil.isEmpty(customerIds)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | List<Map<String, Object>> result = selectMaps(new QueryWrapper<CrmContractDO>() |
| | | .select("customer_id, COUNT(*) AS total") |
| | | .eq("audit_status", CrmAuditStatusEnum.APPROVE.getStatus()) |
| | | .groupBy("customer_id") |
| | | .in("customer_id", customerIds)); |
| | | return convertMap(result, obj -> (Long) obj.get("customer_id"), |
| | | obj -> ((Number) obj.get("total")).longValue()); |
| | | } |
| | | |
| | | /** |
| | | * 批量统计各客户「已签订」(审核通过)的合同金额合计 |
| | | * |
| | | * @param customerIds 客户编号集合 |
| | | * @return key:客户编号,value:金额合计。没有合同的客户不在 map 中 |
| | | */ |
| | | default Map<Long, BigDecimal> selectContractPriceMapByCustomerIds(Collection<Long> customerIds) { |
| | | if (CollUtil.isEmpty(customerIds)) { |
| | | return Collections.emptyMap(); |
| | | } |
| | | // 金额用 IFNULL 兜底:convertMap 内部是 Collectors.toMap,value 为 null 会抛 NPE |
| | | List<Map<String, Object>> result = selectMaps(new QueryWrapper<CrmContractDO>() |
| | | .select("customer_id, IFNULL(SUM(total_price), 0) AS total_price") |
| | | .eq("audit_status", CrmAuditStatusEnum.APPROVE.getStatus()) |
| | | .groupBy("customer_id") |
| | | .in("customer_id", customerIds)); |
| | | return convertMap(result, obj -> (Long) obj.get("customer_id"), |
| | | obj -> (BigDecimal) obj.get("total_price")); |
| | | } |
| | | |
| | | } |