| | |
| | | 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 |
| | |
| | | .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")); |
| | | } |
| | | |
| | | } |