|
@@ -44,77 +44,90 @@
|
|
|
</select>
|
|
|
|
|
|
<sql id="customerCompanyQuerySql">
|
|
|
- SELECT company.id AS id,
|
|
|
- company.name AS companyName,
|
|
|
- parent.id AS parentId,
|
|
|
- parent.name AS parentCompany,
|
|
|
- company.company_type AS companyType,
|
|
|
- company.address AS address,
|
|
|
- company.phone AS phone,
|
|
|
- user.name AS clientManager,
|
|
|
- company.terminal AS terminal,
|
|
|
- ((SELECT COUNT(id)
|
|
|
- FROM assets
|
|
|
- WHERE assets.deleted = 0
|
|
|
- AND assets.clientele_sub_id = company.id) +
|
|
|
- (SELECT COUNT(id)
|
|
|
- FROM major
|
|
|
- WHERE major.deleted = 0
|
|
|
- AND major.clientele_sub_id = company.id) +
|
|
|
- (SELECT COUNT(id)
|
|
|
- FROM personal
|
|
|
- WHERE personal.deleted = 0
|
|
|
- AND personal.clientele_sub_id = company.id)) AS orderVolume,
|
|
|
- (COALESCE((SELECT SUM(order_fund.real_amount)
|
|
|
- FROM assets
|
|
|
- LEFT JOIN order_fund
|
|
|
- ON order_fund.business_type = 'ASSET_BUSINESS' AND order_fund.deleted = 0 AND
|
|
|
- business_id = assets.id
|
|
|
- WHERE assets.deleted = 0
|
|
|
- AND assets.clientele_sub_id = company.id), 0) +
|
|
|
- COALESCE((SELECT SUM(order_fund.real_amount)
|
|
|
- FROM major
|
|
|
- LEFT JOIN order_fund
|
|
|
- ON order_fund.business_type = 'MAJOR_BUSINESS' AND order_fund.deleted = 0 AND
|
|
|
- business_id = major.id
|
|
|
- WHERE major.deleted = 0
|
|
|
- AND major.clientele_sub_id = company.id), 0) +
|
|
|
- COALESCE((SELECT SUM(order_fund.real_amount)
|
|
|
- FROM personal
|
|
|
- LEFT JOIN order_fund
|
|
|
- ON order_fund.business_type = 'PERSONAL_BUSINESS' AND order_fund.deleted = 0 AND
|
|
|
- business_id = personal.id
|
|
|
- WHERE personal.deleted = 0
|
|
|
- AND personal.clientele_sub_id = company.id), 0)) AS estimatedRevenue,
|
|
|
- company.created AS created
|
|
|
+ SELECT company.id AS id,
|
|
|
+ company.name AS companyName,
|
|
|
+ parent.id AS parentId,
|
|
|
+ parent.name AS parentCompany,
|
|
|
+ company.company_type AS companyType,
|
|
|
+ company.address AS address,
|
|
|
+ company.phone AS phone,
|
|
|
+ user.name AS clientManager,
|
|
|
+ company.terminal AS terminal,
|
|
|
+ COALESCE(vol.cnt, 0) AS orderVolume,
|
|
|
+ COALESCE(vol.revenue, 0) AS estimatedRevenue,
|
|
|
+ company.created AS created
|
|
|
FROM customer_company AS company
|
|
|
LEFT JOIN customer_company AS parent ON parent.id = company.parent_id
|
|
|
LEFT JOIN user ON user.id = company.client_manager_id
|
|
|
- WHERE (company.level = 1
|
|
|
- OR (company.terminal = 1 AND company.level = 0))
|
|
|
- <if test="dto != null">
|
|
|
- <if test="dto.companyName != null">
|
|
|
- AND company.name LIKE CONCAT('%',#{dto.companyName},'%')
|
|
|
- </if>
|
|
|
- <if test="dto.parentCompany != null">
|
|
|
- AND parent.name LIKE CONCAT('%',#{dto.parentCompany},'%')
|
|
|
- </if>
|
|
|
- <if test="dto.companyType != null">
|
|
|
- AND company.company_type = #{dto.companyType}
|
|
|
- </if>
|
|
|
- <if test="dto.address != null">
|
|
|
- AND company.address LIKE CONCAT('%',#{dto.address},'%')
|
|
|
- </if>
|
|
|
- <if test="dto.phone != null">
|
|
|
- AND company.phone LIKE CONCAT('%',#{dto.phone},'%')
|
|
|
- </if>
|
|
|
- <if test="dto.clientManagerId != null">
|
|
|
- AND company.client_manager_id = #{dto.clientManagerId}
|
|
|
- </if>
|
|
|
- <if test="dto.terminal != null">
|
|
|
- AND company.terminal = #{dto.terminal}
|
|
|
+
|
|
|
+ /* 先算 来单量 与 评估值 再一次性 JOIN */
|
|
|
+ LEFT JOIN (SELECT clientele_sub_id,
|
|
|
+ SUM(cnt) AS cnt,
|
|
|
+ SUM(revenue) AS revenue
|
|
|
+ FROM (
|
|
|
+ /* assets */
|
|
|
+ SELECT assets.clientele_sub_id,
|
|
|
+ COUNT(*) AS cnt,
|
|
|
+ COALESCE(SUM(order_fund.real_amount), 0) AS revenue
|
|
|
+ FROM assets
|
|
|
+ LEFT JOIN order_fund
|
|
|
+ ON order_fund.business_type = 'ASSET_BUSINESS'
|
|
|
+ AND order_fund.deleted = 0
|
|
|
+ AND order_fund.business_id = assets.id
|
|
|
+ WHERE assets.deleted = 0
|
|
|
+ GROUP BY assets.clientele_sub_id
|
|
|
+
|
|
|
+ UNION ALL
|
|
|
+ /* major */
|
|
|
+ SELECT major.clientele_sub_id,
|
|
|
+ COUNT(*),
|
|
|
+ COALESCE(SUM(order_fund.real_amount), 0)
|
|
|
+ FROM major
|
|
|
+ LEFT JOIN order_fund
|
|
|
+ ON order_fund.business_type = 'MAJOR_BUSINESS'
|
|
|
+ AND order_fund.deleted = 0
|
|
|
+ AND order_fund.business_id = major.id
|
|
|
+ WHERE major.deleted = 0
|
|
|
+ GROUP BY major.clientele_sub_id
|
|
|
+
|
|
|
+ UNION ALL
|
|
|
+ /* personal */
|
|
|
+ SELECT personal.clientele_sub_id,
|
|
|
+ COUNT(*),
|
|
|
+ COALESCE(SUM(order_fund.real_amount), 0)
|
|
|
+ FROM personal
|
|
|
+ LEFT JOIN order_fund
|
|
|
+ ON order_fund.business_type = 'PERSONAL_BUSINESS'
|
|
|
+ AND order_fund.deleted = 0
|
|
|
+ AND order_fund.business_id = personal.id
|
|
|
+ WHERE personal.deleted = 0
|
|
|
+ GROUP BY personal.clientele_sub_id) AS t
|
|
|
+ GROUP BY clientele_sub_id) AS vol ON vol.clientele_sub_id = company.id
|
|
|
+ WHERE company.level = 1 OR (company.terminal = 1 AND company.level = 0)
|
|
|
+ <if test="dto != null">
|
|
|
+ <if test="dto.companyName != null">
|
|
|
+ AND company.name LIKE CONCAT('%',#{dto.companyName},'%')
|
|
|
+ </if>
|
|
|
+ <if test="dto.parentCompany != null">
|
|
|
+ AND parent.name LIKE CONCAT('%',#{dto.parentCompany},'%')
|
|
|
+ </if>
|
|
|
+ <if test="dto.companyType != null">
|
|
|
+ AND company.company_type = #{dto.companyType}
|
|
|
+ </if>
|
|
|
+ <if test="dto.address != null">
|
|
|
+ AND company.address LIKE CONCAT('%',#{dto.address},'%')
|
|
|
+ </if>
|
|
|
+ <if test="dto.phone != null">
|
|
|
+ AND company.phone LIKE CONCAT('%',#{dto.phone},'%')
|
|
|
+ </if>
|
|
|
+ <if test="dto.clientManagerId != null">
|
|
|
+ AND company.client_manager_id = #{dto.clientManagerId}
|
|
|
+ </if>
|
|
|
+ <if test="dto.terminal != null">
|
|
|
+ AND company.terminal = #{dto.terminal}
|
|
|
+ </if>
|
|
|
</if>
|
|
|
- </if>
|
|
|
+ ORDER BY COALESCE(vol.revenue, 0) DESC
|
|
|
</sql>
|
|
|
|
|
|
<!--分页查询企业客户列表-->
|
|
@@ -198,6 +211,89 @@
|
|
|
</select>
|
|
|
|
|
|
<select id="getCustomerCompanyIncomePage" resultType="com.dayou.vo.CustomerCompanyOrderVO">
|
|
|
-
|
|
|
+ SELECT *
|
|
|
+ FROM (SELECT assets.id AS id,
|
|
|
+ assets.order_id AS orderId,
|
|
|
+ assets.name AS projectName,
|
|
|
+ 'ASSET_BUSINESS' AS businessType,
|
|
|
+ customer.name AS customerName,
|
|
|
+ subCustomer.name AS customerSubName,
|
|
|
+ linkman.name AS linkmanName,
|
|
|
+ user.name AS clientManager,
|
|
|
+ assets.created AS created,
|
|
|
+ fund.real_amount AS orderRealAmount,
|
|
|
+ (SELECT created FROM finance_claim WHERE order_fund_id = fund.id ORDER BY created DESC LIMIT 1) AS collectionTime
|
|
|
+ FROM assets
|
|
|
+ LEFT JOIN customer_company AS customer ON customer.id = assets.clientele_id
|
|
|
+ LEFT JOIN customer_company AS subCustomer ON subCustomer.id = assets.clientele_sub_id
|
|
|
+ LEFT JOIN customer_linkman AS linkman ON linkman.id = assets.clientele_contact_id
|
|
|
+ LEFT JOIN user ON user.id = assets.client_manager_id
|
|
|
+ LEFT JOIN order_fund AS fund
|
|
|
+ ON fund.business_type = 'ASSET_BUSINESS' AND fund.business_id = assets.id AND
|
|
|
+ fund.deleted = 0
|
|
|
+ WHERE assets.deleted = 0
|
|
|
+ AND assets.clientele_sub_id = #{dto.subCustomerId}
|
|
|
+ UNION ALL
|
|
|
+ SELECT major.id AS id,
|
|
|
+ major.order_id AS orderId,
|
|
|
+ major.name AS projectName,
|
|
|
+ 'MAJOR_BUSINESS' AS businessType,
|
|
|
+ customer.name AS customerName,
|
|
|
+ subCustomer.name AS customerSubName,
|
|
|
+ linkman.name AS linkmanName,
|
|
|
+ user.name AS clientManager,
|
|
|
+ major.created AS created,
|
|
|
+ fund.real_amount AS orderRealAmount,
|
|
|
+ (SELECT created FROM finance_claim WHERE order_fund_id = fund.id ORDER BY created DESC LIMIT 1) AS collectionTime
|
|
|
+ FROM major
|
|
|
+ LEFT JOIN customer_company AS customer ON customer.id = major.clientele_id
|
|
|
+ LEFT JOIN customer_company AS subCustomer ON subCustomer.id = major.clientele_sub_id
|
|
|
+ LEFT JOIN customer_linkman AS linkman ON linkman.id = major.clientele_contact_id
|
|
|
+ LEFT JOIN user ON user.id = major.client_manager_id
|
|
|
+ LEFT JOIN order_fund AS fund
|
|
|
+ ON fund.business_type = 'MAJOR_BUSINESS' AND fund.business_id = major.id AND
|
|
|
+ fund.deleted = 0
|
|
|
+ WHERE major.deleted = 0
|
|
|
+ AND major.clientele_sub_id = #{dto.subCustomerId}
|
|
|
+ UNION ALL
|
|
|
+ SELECT personal.id AS id,
|
|
|
+ personal.order_id AS orderId,
|
|
|
+ personal.location AS projectName,
|
|
|
+ 'PERSONAL_BUSINESS' AS businessType,
|
|
|
+ customer.name AS customerName,
|
|
|
+ subCustomer.name AS customerSubName,
|
|
|
+ linkman.name AS linkmanName,
|
|
|
+ user.name AS clientManager,
|
|
|
+ personal.created AS created,
|
|
|
+ fund.real_amount AS orderRealAmount,
|
|
|
+ (SELECT created FROM finance_claim WHERE order_fund_id = fund.id ORDER BY created DESC LIMIT 1) AS collectionTime
|
|
|
+ FROM personal
|
|
|
+ LEFT JOIN customer_company AS customer ON customer.id = personal.clientele_id
|
|
|
+ LEFT JOIN customer_company AS subCustomer ON subCustomer.id = personal.clientele_sub_id
|
|
|
+ LEFT JOIN customer_linkman AS linkman ON linkman.id = personal.clientele_contact_id
|
|
|
+ LEFT JOIN user ON user.id = personal.client_manager_id
|
|
|
+ LEFT JOIN order_fund AS fund
|
|
|
+ ON fund.business_type = 'PERSONAL_BUSINESS' AND fund.business_id = personal.id AND
|
|
|
+ fund.deleted = 0
|
|
|
+ WHERE personal.deleted = 0
|
|
|
+ AND personal.clientele_sub_id = #{dto.subCustomerId}) AS custmerOrder
|
|
|
+ <where>
|
|
|
+ <if test="dto.orderId != null">
|
|
|
+ AND orderId = #{dto.orderId}
|
|
|
+ </if>
|
|
|
+ <if test="dto.projectName != null">
|
|
|
+ AND projectName LIKE CONCAT('%',#{dto.projectName},'%')
|
|
|
+ </if>
|
|
|
+ <if test="dto.businessType != null">
|
|
|
+ AND businessType = #{dto.businessType}
|
|
|
+ </if>
|
|
|
+ <if test="dto.createdStartTime != null and dto.createdEndTime != null">
|
|
|
+ AND created BETWEEN #{dto.createdStartTime} AND #{dto.createdEndTime}
|
|
|
+ </if>
|
|
|
+ <if test="dto.collectionStartTime != null and dto.collectionEndTime != null">
|
|
|
+ AND collectionTime BETWEEN #{dto.collectionStartTime} AND #{dto.collectionEndTime}
|
|
|
+ </if>
|
|
|
+ </where>
|
|
|
+ ORDER BY custmerOrder.collectionTime DESC
|
|
|
</select>
|
|
|
</mapper>
|