From f4ef6d1f4489e0d4cbba1845792945b924c16228 Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期三, 14 十月 2026 18:29:56 +0800
Subject: [PATCH] feat: 客户签订合同统计 + MES 首页质检合格率

---
 yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/contract/CrmContractMapper.java |   69 ++++++++++++++++++++++++++++++++++
 1 files changed, 69 insertions(+), 0 deletions(-)

diff --git a/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/contract/CrmContractMapper.java b/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/contract/CrmContractMapper.java
index b85d74c..296d4d5 100644
--- a/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/contract/CrmContractMapper.java
+++ b/yudao-module-crm/src/main/java/cn/iocoder/yudao/module/crm/dal/mysql/contract/CrmContractMapper.java
@@ -1,5 +1,6 @@
 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;
@@ -12,11 +13,17 @@
 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
@@ -51,18 +58,39 @@
     }
 
     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鍒楄〃锛坣ull琛ㄧず涓嶉檺鍒讹級
+     */
+    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());
@@ -185,4 +213,45 @@
                 .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 鍏滃簳锛歝onvertMap 鍐呴儴鏄� Collectors.toMap锛寁alue 涓� 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"));
+    }
+
 }

--
Gitblit v1.9.3