Browse Source

APP客户分析查询接口优化

liyuan 8 months ago
parent
commit
1d5994868f

+ 14 - 1
blade-service-api/blade-sales-part-api/src/main/java/org/springblade/salesPart/entity/PjCorpsDesc.java

@@ -338,12 +338,25 @@ public class PjCorpsDesc implements Serializable {
 	private List<String> createTimeList;
 
 	/**
-	 * 查询天数
+	 * 查询天数起始
 	 */
 	@TableField(exist = false)
 	private Long days;
 
 	/**
+	 * 查询天数结束
+	 */
+	@TableField(exist = false)
+	private Integer endDays;
+
+	/**
+	 * 客户下单数(备用)
+	 */
+	@TableField(exist = false)
+	private Long orderCount;
+
+
+	/**
 	 * 排序
 	 */
 	@TableField(exist = false)

+ 60 - 34
blade-service/blade-sales-part/src/main/java/org/springblade/salesPart/corps/mapper/CorpsDescMapper.xml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<!--suppress ALL -->
 <mapper namespace="org.springblade.salesPart.corps.mapper.CorpsDescMapper">
 
     <!-- 通用查询映射结果 -->
@@ -346,56 +347,81 @@
           and tenant_id = #{tenantId}
     </select>
     <select id="appCustomerAnalysis" resultType="org.springblade.salesPart.entity.PjCorpsDesc">
-        SELECT DISTINCT
-            pcd.*,
-        o.busines_date as businesDate,
-        pca.detailed_address as detailed_Address
+        SELECT
+        allCunstomer.id,
+        allCunstomer.tenant_id,
+        allCunstomer.cname,
+        allCunstomer.tel,
+        allCunstomer.salesman_name,
+        allCunstomer.businesDate,
+        allCunstomer.daysDiff as days,
+        allCunstomer.orderCount,
+        allCunstomer.detailed_Address
+        FROM (
+        SELECT
+        pcd.id,
+        pcd.tenant_id,
+        pcd.cname,
+        pcd.tel,
+        pcd.salesman_id,
+        pcd.salesman_name,
+        MAX(o.busines_date) AS businesDate,
+        IFNULL(DATEDIFF(NOW(), MAX(o.busines_date)),0) AS daysDiff,
+        COUNT(o.customer_id) AS orderCount,
+        pca.detailed_address AS detailed_Address
         FROM
-            pjpf_corps_desc pcd
-        LEFT JOIN pjpf_corps_addr pca on pcd.id = pca.pid and  pca.default_addres = '1'
-        LEFT JOIN (
-                SELECT
-                    customer_id,
-                    po.busines_date
-                FROM
-                    pjpf_order po
-                WHERE
-                    po.tenant_id = #{CorpsDesc.tenantId}
-                    AND po.is_deleted = '0'
-                    AND po.bs_type = 'XS'
-                    AND FIND_IN_SET( po.`status`, '已发货,已收货' )
-                <if test="CorpsDesc.notPurchased == null  or CorpsDesc.notPurchased == ''">
-                    AND  po.busines_date >= CURDATE() - INTERVAL #{CorpsDesc.days} DAY
-                </if>
-                ORDER BY
-                po.busines_date DESC
-        ) o ON pcd.id = o.customer_id
+        pjpf_corps_desc pcd
+        LEFT JOIN pjpf_corps_addr pca ON pcd.id = pca.pid
+        AND pca.default_addres = '1'
+        AND pca.tenant_id = #{CorpsDesc.tenantId}
+        AND pca.is_deleted = '0'
+        LEFT JOIN pjpf_order o ON pcd.id = o.customer_id
+        AND o.tenant_id = #{CorpsDesc.tenantId}
+        AND o.is_deleted = '0'
+        AND o.bs_type = 'XS'
+        AND o.status IN ('已发货', '已收货')
+        <if test="CorpsDesc.endDays == null">
+            AND o.busines_date >= CURDATE() - INTERVAL #{CorpsDesc.days} DAY
+        </if>
+        <if test="CorpsDesc.endDays != null">
+            AND ( o.busines_date >= CURDATE() - INTERVAL #{CorpsDesc.days} DAY or o.busines_date >= CURDATE() - INTERVAL
+            #{CorpsDesc.endDays} DAY)
+        </if>
         WHERE
-            pcd.corp_type = 'KH'
-            AND pcd.tenant_id = #{CorpsDesc.tenantId}
-            AND pcd.is_deleted = '0'
-            AND pca.tenant_id = #{CorpsDesc.tenantId}
-            AND pca.is_deleted = '0'
+        pcd.corp_type = 'KH'
+        AND pcd.tenant_id = #{CorpsDesc.tenantId}
+        AND pcd.is_deleted = '0'
         <if test="CorpsDesc.notPurchased !=null  and CorpsDesc.notPurchased != ''">
-            AND  o.customer_id IS NULL
+            AND o.customer_id IS NULL
         </if>
         <if test="CorpsDesc.notPurchased == null or CorpsDesc.notPurchased == ''">
-            AND  o.customer_id IS NOT NULL
+            AND o.customer_id IS NOT NULL
         </if>
         <if test="CorpsDesc.cname !=null and CorpsDesc.cname != ''">
             and pcd.cname like CONCAT(CONCAT('%', #{CorpsDesc.cname}), '%')
         </if>
+        GROUP BY
+        pcd.id
+        ) allCunstomer WHERE allCunstomer.daysDiff >= #{CorpsDesc.days}
+        <if test="CorpsDesc.notPurchased != null  and CorpsDesc.notPurchased != ''">
+            or o.customer_id IS NULL
+        </if>
+        <if test="CorpsDesc.notPurchased == null or CorpsDesc.notPurchased == ''">
+            AND o.customer_id IS NOT NULL
+        </if>
+        ORDER BY
+        (allCunstomer.orderCount * 0.7 + allCunstomer.daysDiff * 0.3) DESC
         <if test="CorpsDesc.sort !=null and CorpsDesc.sort == 1">
-            ORDER BY pcd.salesman_id
+            , allCunstomer.salesman_id
         </if>
         <if test="CorpsDesc.sort !=null and CorpsDesc.sort == 2">
-            ORDER BY pcd.cname
+            , allCunstomer.cname
         </if>
         <if test="CorpsDesc.sort !=null and CorpsDesc.sort == 3">
-            ORDER BY pca.detailed_address
+            , allCunstomer.detailed_address
         </if>
         <if test="CorpsDesc.sort !=null and CorpsDesc.sort == 4">
-            ORDER BY o.busines_date ASC
+            , allCunstomer.busines_date ASC
         </if>
     </select>
 </mapper>

+ 2 - 2
blade-service/blade-sales-part/src/main/java/org/springblade/salesPart/corps/service/impl/CorpsDescServiceImpl.java

@@ -1198,14 +1198,14 @@ public class CorpsDescServiceImpl extends ServiceImpl<CorpsDescMapper, PjCorpsDe
 	@Override
 	public IPage<PjCorpsDesc> appCustomerAnalysis(IPage<PjCorpsDesc> page, PjCorpsDesc corpsDescDto) {
 		List<PjCorpsDesc> corpsDescList = baseMapper.appCustomerAnalysis(page, corpsDescDto);
-		Date date = new Date();
+/*		Date date = new Date();
 		for (PjCorpsDesc item : corpsDescList) {
 			if (ObjectUtils.isNotNull(item.getBusinesDate())) {
 				long msNum = date.getTime() - item.getBusinesDate().getTime();
 				long dayNum = msNum / (24 * 60 * 60 * 1000);
 				item.setDays(dayNum);
 			}
-		}
+		}*/
 		return page.setRecords(corpsDescList);
 	}