<?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">
<mapper namespace="com.dayou.mapper.AssetsStatisticalStatementMapper">

    <!--资产台账查询SQL-->
    <sql id="assetsLedgerQuery">
        SELECT (@i :=  @i + 1) AS id,
               assets.id                                                             AS assetsId,
               assets.order_id                                                       AS orderId,
               statement.production_no                                               AS statementNo,
               report.production_no                                                  AS reportNo,
               CASE report.production_type WHEN 'LETTER' THEN '意见函' WHEN 'CONSULT' THEN '咨询报告' WHEN 'REPORT' THEN '评估报告' ELSE '-' END AS productionType,
               report.valuation_basis_date                                           AS valuationBasisDate,
               report.create_production_date                                         AS createReportDate,
               report.print_count AS reportPrintCount,
               COALESCE(report.assets_name, statement.assets_name, assets.name)      AS projectName,
               IF(nodeInfo.state = 'CLOSED','项目终止',IF(nodeInfo.name IS NULL,IF(statement.production_no IS NULL, '撰写意见书', '撰写报告'),nodeInfo.name)) AS currentNodeName,
               assets.security AS security,
               assets.significant_assets_reorganization AS significantAssetsReorganization,
               assets.state_assets AS stateAssets,
               assets.foreign_assets_involved AS foreignAssetsInvolved,
               assets.dispense_benefit AS dispenseBenefit,
               manager.name AS clientManagerName,
               principal.name AS principalName,
               (SELECT customer_company.name FROM customer_company WHERE customer_company.id = clientele_id) AS customerName,
               (SELECT customer_company.name FROM customer_company WHERE customer_company.id = clientele_sub_id) AS customerSubName,
               assets.bailor AS bailor,
               assets.bailor_contact_tel AS bailorContactTel,
               GROUP_CONCAT(target.id SEPARATOR '; ')                                AS targetId,
               GROUP_CONCAT(target.purpose_name SEPARATOR '; ')                      AS purposeName,
               GROUP_CONCAT(target.type_name SEPARATOR '; ')                         AS valueTypeName,
               GROUP_CONCAT(
                   IF(target.evaluation_type_defined_sec IS NOT NULL, CONCAT(target.targetTypeName, '-', target.secTargetTypaName,
                   '-', target.evaluation_type_defined_sec), CONCAT(target.targetTypeName, '-', target.secTargetTypaName))
                   SEPARATOR '; '
               ) AS targetTypeName,
               GROUP_CONCAT(target.evaluation_method_id SEPARATOR ', ')              AS evaluationMethods,
               GROUP_CONCAT(target.defined_evaluation_method SEPARATOR '; ')         AS definedEvaluationMethod,
               GROUP_CONCAT(target.choice_evaluation_method_id SEPARATOR ', ')       AS choiceEvaluationMethods,
               GROUP_CONCAT(target.choice_defined_evaluation_method SEPARATOR '; ')  AS choiceDefinedEvaluationMethod,
               GROUP_CONCAT(target.estimated_value  SEPARATOR '; ')                  AS estimatedValue,
               fund.should_amount                                                    AS shouldAmount,
               fund.real_amount                                                      AS realAmount,
               report.signatory                                                      AS signatory,
               report.repertory_out_time                                             AS repertoryOutTime,
               (
                   SELECT user.name
                   FROM work_flow_node_instance AS instance,
                        work_task_record        AS record,
                        work_node               AS node,
                        user
                   WHERE instance.id = record.instance_id
                     AND record.handler_id = user.id
                     AND node.id = instance.node_id
                     AND instance.business_id = assets.id
                     AND instance.business_sub_id = statement.production_no
                     AND node.code = 'REVIEW_STATEMENT'
                     AND instance.deleted = 0
               ) AS statementCheckerName,
               (
                   SELECT user.name
                   FROM work_flow_node_instance AS instance,
                        work_task_record        AS record,
                        work_node               AS node,
                        user
                   WHERE instance.id = record.instance_id
                     AND record.handler_id = user.id
                     AND node.id = instance.node_id
                     AND instance.business_id = assets.id
                     AND instance.business_sub_id = statement.production_no
                     AND node.code = 'REEXAMINE_STATEMENT'
                     AND instance.deleted = 0
               ) AS statementReCheckerName,
               (
                   SELECT user.name
                   FROM work_flow_node_instance AS instance,
                        work_task_record        AS record,
                        work_node               AS node,
                        user
                   WHERE instance.id = record.instance_id
                     AND record.handler_id = user.id
                     AND node.id = instance.node_id
                     AND instance.business_id = assets.id
                     AND instance.business_min_id = report.production_no
                     AND node.code = 'CHECK_REPORT'
                     AND instance.deleted = 0
               ) AS reportCheckerName,
               (
                   SELECT user.name
                   FROM work_flow_node_instance AS instance,
                        work_task_record        AS record,
                        work_node               AS node,
                        user
                   WHERE instance.id = record.instance_id
                     AND record.handler_id = user.id
                     AND node.id = instance.node_id
                     AND instance.business_id = assets.id
                     AND instance.business_min_id = report.production_no
                     AND node.code = 'RECHECK_REPORT'
                     AND instance.deleted = 0
               ) AS reportReCheckerName,
               (
                   SELECT user.name
                   FROM work_flow_node_instance AS instance,
                        work_task_record        AS record,
                        work_node               AS node,
                        user
                   WHERE instance.id = record.instance_id
                     AND record.handler_id = user.id
                     AND node.id = instance.node_id
                     AND instance.business_id = assets.id
                     AND instance.business_min_id = report.production_no
                     AND node.code = 'FOURTH_CHECK_REPORT'
                     AND instance.deleted = 0
               ) AS reportFourthCheckerName
        FROM (SELECT @i := 0) AS sort,assets
        LEFT JOIN (
            SELECT target.id,
                   target.assets_id,
                   purpose_name,
                   vType.type_name,
                   evaluation_method_id,
                   defined_evaluation_method,
                   choice_evaluation_method_id,
                   choice_defined_evaluation_method,
                   type.type_name AS targetTypeName,
                   secType.type_name AS secTargetTypaName,
                   evaluation_type_defined_sec,
                   estimated_value,
                   statement_no,
                   report_no
            FROM assets_evaluation_target AS target
                     LEFT JOIN assets_evaluation_target_purpose AS purpose ON evaluation_purpose_id = purpose.id
                     LEFT JOIN assets_evaluation_target_type AS type ON evaluation_type_id = type.id
                     LEFT JOIN assets_evaluation_target_type AS secType ON evaluation_type_sec_id = secType.id
                     LEFT JOIN assets_value_type AS vType ON assets_value_id = vType.id
        ) AS target ON assets.id = target.assets_id
        LEFT JOIN assets_production AS report ON report_no = report.production_no
        LEFT JOIN assets_production AS statement ON statement_no = statement.production_no
        LEFT JOIN user AS manager ON manager.id = assets.client_manager_id
        LEFT JOIN user AS principal ON principal.id = assets.principal_id
        LEFT JOIN order_fund AS fund ON fund.business_id = assets.id AND fund.business_type = 'ASSET_BUSINESS'
        LEFT JOIN (
            SELECT node.`name`,instance.business_id,instance.business_sub_id,instance.business_min_id,instance.state
            FROM work_flow_node_instance AS instance,
            work_node AS node
            WHERE instance.node_id = node.id
            AND node.deleted = 0
            AND instance.deleted = 0
            AND (instance.state = 'PENDING' OR instance.state = 'CLOSED' OR
            (instance.state = 'FINISHED' AND node.`code` = 'BUSINESS_ARCHIVING'))
        ) AS nodeInfo ON nodeInfo.business_id = assets.id
        AND (IF(statement.production_no IS NULL, nodeInfo.business_sub_id IS NULL,
        statement.production_no = nodeInfo.business_sub_id))
        AND (IF(report.production_no IS NULL, nodeInfo.business_min_id IS NULL,
        report.production_no = nodeInfo.business_min_id))
        WHERE assets.deleted = 0
        <if test="dto != null and dto.keyWord != null and dto.keyWord != '' ">
            AND (
            assets.order_id LIKE CONCAT('%',#{dto.keyWord},'%') OR
            statement.assets_name LIKE CONCAT('%',#{dto.keyWord},'%') OR
            report.assets_name LIKE CONCAT('%',#{dto.keyWord},'%')
            )
        </if>
        <if test="dto != null and dto.clientManagerId">
            AND assets.client_manager_id = #{dto.clientManagerId}
        </if>
        <if test="dto != null and dto.principalId">
            AND assets.principal_id = #{dto.principalId}
        </if>
        <if test="dto != null and dto.departmentId != null">
            AND assets.department_id = #{dto.departmentId}
        </if>
        <if test="dto != null and dto.startTime != null and dto.endTime!= null">
            AND (assets.created BETWEEN #{dto.startTime} AND #{dto.endTime})
        </if>
        <if test="dto != null and dto.assetsBusinessGener">
            AND assets.assets_business_gener = #{dto.assetsBusinessGener}
        </if>
        GROUP BY statementNo,reportNo,assets.id
        ORDER BY assets.created DESC
    </sql>

    <!--获取资产台账-->
    <select id="getAssetsLedgerVO" resultType="com.dayou.vo.AssetsLedgerVO">
        <include refid="assetsLedgerQuery" />
     </select>

    <!--资产台账导出-->
    <select id="assetsLedgerVOExport" resultType="com.dayou.vo.AssetsLedgerVO">
        <include refid="assetsLedgerQuery" />
    </select>

    <sql id="assetsPerformanceDeductionQuery">
        SELECT
        department.id AS departmentId,
        department.name AS departmentName,
        COALESCE ( SUM( checkPer.normal_mistake ), 0 ) AS checkNormalMistakeScore,
        COALESCE ( SUM( checkPer.hard_mistake ), 0 ) AS checkHardMistakeScore,
        COALESCE ( SUM( checkPer.fatal_mistake ), 0 ) AS checkFatalMistakeScore,
        COALESCE ( SUM( reCheckPer.normal_mistake ), 0 ) AS reCheckNormalMistakeScore,
        COALESCE ( SUM( reCheckPer.hard_mistake ), 0 ) AS reCheckHardMistakeScore,
        COALESCE ( SUM( reCheckPer.fatal_mistake ), 0 ) AS reCheckFatalMistakeScore,
        COALESCE ( SUM( fourthCheckPer.normal_mistake ), 0 ) AS fourthCheckNormalMistakeScore,
        COALESCE ( SUM( fourthCheckPer.hard_mistake ), 0 ) AS fourthCheckHardMistakeScore,
        COALESCE ( SUM( fourthCheckPer.fatal_mistake ), 0 ) AS fourthCheckFatalMistakeScore,
        COALESCE (
        COALESCE ( SUM( checkPer.normal_mistake ), 0 ) + COALESCE ( SUM( checkPer.hard_mistake ), 0 ) + COALESCE ( SUM( checkPer.fatal_mistake ), 0 ) + COALESCE ( SUM( reCheckPer.normal_mistake ), 0 ) + COALESCE ( SUM( reCheckPer.hard_mistake ), 0 ) + COALESCE ( SUM( reCheckPer.fatal_mistake ), 0 ) + COALESCE ( SUM( fourthCheckPer.normal_mistake ), 0 ) + COALESCE ( SUM( fourthCheckPer.hard_mistake ), 0 ) + COALESCE ( SUM( fourthCheckPer.fatal_mistake ), 0 ),
        0
        ) AS allMistakeScore
        FROM
        (
            SELECT
                department.id,
                department.name
            FROM
                department,
                sorted_department
            WHERE
                department.id = sorted_department.department_id
                AND department.deleted = 0
                AND sorted_department.business_type = 'ASSET_BUSINESS'
        ) AS department
        LEFT JOIN (
        SELECT
        department_id,
        assets.created,
        assets_production.id AS production_id
        FROM
        assets
        LEFT JOIN assets_production ON assets_production.business_id = assets.id
        AND assets_production.deleted = 0
        ) AS orderInfo ON department.id = orderInfo.department_id
        LEFT JOIN ( SELECT production_id, fatal_mistake, hard_mistake, normal_mistake FROM
        business_production_performance WHERE business_type = 'ASSET_BUSINESS' AND check_loop = '初审'
        <if test="dto != null and dto.startTime != null and dto.endTime!= null">
            AND (business_production_performance.modified BETWEEN #{dto.startTime} AND #{dto.endTime})
        </if>
        ) AS checkPer ON checkPer.production_id = orderInfo.production_id
        LEFT JOIN ( SELECT production_id, fatal_mistake, hard_mistake, normal_mistake FROM
        business_production_performance WHERE business_type = 'ASSET_BUSINESS' AND check_loop = '复审'
        <if test="dto != null and dto.startTime != null and dto.endTime!= null">
            AND (business_production_performance.modified BETWEEN #{dto.startTime} AND #{dto.endTime})
        </if>
        ) AS reCheckPer ON reCheckPer.production_id = orderInfo.production_id
        LEFT JOIN ( SELECT production_id, fatal_mistake, hard_mistake, normal_mistake FROM
        business_production_performance WHERE business_type = 'ASSET_BUSINESS' AND check_loop = '四审'
        <if test="dto != null and dto.startTime != null and dto.endTime!= null">
            AND (business_production_performance.modified BETWEEN #{dto.startTime} AND #{dto.endTime})
        </if>
        ) AS fourthCheckPer ON fourthCheckPer.production_id = orderInfo.production_id
        <where>
            <if test="dto != null and dto.departmentId != null">
                AND department.id = #{dto.departmentId}
            </if>
        </where>
        GROUP BY department.id
    </sql>

    <!--评估部绩效扣分查询-->
    <select id="getAssetsPerformanceDeductionVO" resultType="com.dayou.vo.AssetsPerformanceDeductionVO">
        <include refid="assetsPerformanceDeductionQuery" />
    </select>

    <!--评估部绩效扣分导出-->
    <select id="assetsPerformanceDeductionVOExport" resultType="com.dayou.vo.AssetsPerformanceDeductionVO">
        <include refid="assetsPerformanceDeductionQuery" />
    </select>

    <!--查询错误详情SQL-->
    <sql id="assetsPerformanceDeductionDetailQuery">
        SELECT performance.id,
               assets.order_id            AS orderId,
               assets.members             AS members,
               production.assets_name     AS projectName,
               customer.name              AS customerName,
               production.production_no   AS productionNo,
               (CASE production.production_type
                    WHEN 'STATEMENT' THEN '意见书'
                    WHEN 'LETTER' THEN '意见函'
                    WHEN 'CONSULT' THEN '咨询报告'
                    WHEN 'REPORT' THEN '评估报告'
                    END)                  AS productionType,
               principal.name             AS principalName,
               checker.name               AS checkerName,
               (CASE #{dto.mistakeType}
                    WHEN 'normal' THEN performance.normal_mistake
                    WHEN 'hard' THEN performance.hard_mistake
                    WHEN 'fatal' THEN performance.fatal_mistake
                    ELSE '-'
                    END)                  AS mistakeCount,
               production.evaluate_amount AS evaluateAmount,
               performance.modified       AS checkTime,
               performance.reason         AS reason
        FROM assets
                 LEFT JOIN assets_production AS production ON assets.id = production.business_id
                 LEFT JOIN business_production_performance AS performance ON production.id = performance.production_id
                 LEFT JOIN customer_company AS customer ON customer.id = assets.clientele_id
                 LEFT JOIN user AS principal ON principal.id = assets.principal_id
                 LEFT JOIN user AS checker ON checker.id = performance.check_id
        WHERE assets.department_id = #{dto.departmentId}
          AND performance.check_loop = #{dto.checkLoop}
          AND (performance.modified BETWEEN #{dto.startTime} AND #{dto.endTime})
          <if test="dto.principalId != null">
              AND assets.principal_id = #{dto.principalId}
          </if>
          <if test="dto.mistakeType == 'normal' ">
            AND performance.normal_mistake > 0
          </if>
          <if test="dto.mistakeType == 'hard' ">
            AND performance.hard_mistake > 0
          </if>
          <if test="dto.mistakeType == 'fatal' ">
            AND performance.fatal_mistake > 0
          </if>
    </sql>

    <!--查询错误详情列表-->
    <select id="getAssetsPerformanceDeductionDetail" resultType="com.dayou.vo.AssetsPerformanceDeductionDetailVO">
        <include refid="assetsPerformanceDeductionDetailQuery" />
    </select>

    <!--错误详情列表导出-->
    <select id="assetsPerformanceDeductionDetailExport" resultType="com.dayou.vo.AssetsPerformanceDeductionDetailVO">
        <include refid="assetsPerformanceDeductionDetailQuery" />
    </select>

    <!--评估人员绩效扣分查询SQL-->
    <sql id="assetsPerformanceDeductionByUserQuery">
        SELECT user.id AS userId,
               user.userName,
               user.departmentId,
               user.departmentName,
               COALESCE ( SUM( checkPer.normal_mistake ), 0 ) AS checkNormalMistakeScore,
               COALESCE ( SUM( checkPer.hard_mistake ), 0 ) AS checkHardMistakeScore,
               COALESCE ( SUM( checkPer.fatal_mistake ), 0 ) AS checkFatalMistakeScore,
               COALESCE ( SUM( reCheckPer.normal_mistake ), 0 ) AS reCheckNormalMistakeScore,
               COALESCE ( SUM( reCheckPer.hard_mistake ), 0 ) AS reCheckHardMistakeScore,
               COALESCE ( SUM( reCheckPer.fatal_mistake ), 0 ) AS reCheckFatalMistakeScore,
               COALESCE ( SUM( fourthCheckPer.normal_mistake ), 0 ) AS fourthCheckNormalMistakeScore,
               COALESCE ( SUM( fourthCheckPer.hard_mistake ), 0 ) AS fourthCheckHardMistakeScore,
               COALESCE ( SUM( fourthCheckPer.fatal_mistake ), 0 ) AS fourthCheckFatalMistakeScore,
               COALESCE (
               COALESCE ( SUM( checkPer.normal_mistake ), 0 ) + COALESCE ( SUM( checkPer.hard_mistake ), 0 ) + COALESCE ( SUM( checkPer.fatal_mistake ), 0 ) + COALESCE ( SUM( reCheckPer.normal_mistake ), 0 ) + COALESCE ( SUM( reCheckPer.hard_mistake ), 0 ) + COALESCE ( SUM( reCheckPer.fatal_mistake ), 0 ) + COALESCE ( SUM( fourthCheckPer.normal_mistake ), 0 ) + COALESCE ( SUM( fourthCheckPer.hard_mistake ), 0 ) + COALESCE ( SUM( fourthCheckPer.fatal_mistake ), 0 ),
                0
                ) AS allMistakeScore
        FROM (SELECT user.id AS id,
                     user.`name` AS userName,
                     department.name AS departmentName,
                     department.id AS departmentId
              FROM user,post,user_post,sorted_department,department
              WHERE `user`.id = user_post.user_id
                AND user_post.post_id = post.id
                AND post.department_id = sorted_department.department_id
                AND sorted_department.department_id = department.id
                AND sorted_department.deleted = 0
                AND user_post.deleted = 0
                AND sorted_department.business_type = 'ASSET_BUSINESS'
              GROUP BY user.id
             ) AS user
        LEFT JOIN (
            SELECT
            principal_id,
            assets.created,
            assets_production.id AS production_id
            FROM
            assets
            LEFT JOIN assets_production ON assets_production.business_id = assets.id
            AND assets_production.deleted = 0
        ) AS orderInfo ON user.id = orderInfo.principal_id
        LEFT JOIN ( SELECT production_id, fatal_mistake, hard_mistake, normal_mistake FROM
        business_production_performance WHERE business_type = 'ASSET_BUSINESS' AND check_loop = '初审'
        <if test="dto != null and dto.startTime != null and dto.endTime!= null">
            AND (business_production_performance.modified BETWEEN #{dto.startTime} AND #{dto.endTime})
        </if>
        ) AS checkPer ON checkPer.production_id = orderInfo.production_id
        LEFT JOIN ( SELECT production_id, fatal_mistake, hard_mistake, normal_mistake FROM
        business_production_performance WHERE business_type = 'ASSET_BUSINESS' AND check_loop = '复审'
        <if test="dto != null and dto.startTime != null and dto.endTime!= null">
            AND (business_production_performance.modified BETWEEN #{dto.startTime} AND #{dto.endTime})
        </if>
        ) AS reCheckPer ON reCheckPer.production_id = orderInfo.production_id
        LEFT JOIN ( SELECT production_id, fatal_mistake, hard_mistake, normal_mistake FROM
        business_production_performance WHERE business_type = 'ASSET_BUSINESS' AND check_loop = '四审'
        <if test="dto != null and dto.startTime != null and dto.endTime!= null">
            AND (business_production_performance.modified BETWEEN #{dto.startTime} AND #{dto.endTime})
        </if>
        ) AS fourthCheckPer ON fourthCheckPer.production_id = orderInfo.production_id
        <where>
            <if test="dto != null and dto.departmentId != null">
                AND user.departmentId = #{dto.departmentId}
            </if>
            <if test="dto != null and dto.userName != null">
                AND user.userName LIKE CONCAT('%',#{dto.userName},'%')
            </if>
        </where>
        GROUP BY user.id
    </sql>

    <!--评估人员绩效扣分查询-->
    <select id="getAssetsPerformanceDeductionVOByEvaluator" resultType="com.dayou.vo.AssetsPerformanceDeductionVO">
        <include refid="assetsPerformanceDeductionByUserQuery" />
    </select>

    <!--评估人员绩效扣分导出-->
    <select id="assetsPerformanceDeductionVOByEvaluatorExport" resultType="com.dayou.vo.AssetsPerformanceDeductionVO">
        <include refid="assetsPerformanceDeductionByUserQuery" />
    </select>

    <!--评估部效率查询-->
    <select id="getAssetsEvaluateDepEfficiencyVO" resultType="com.dayou.vo.AssetsEvaluateEfficiencyVO">
        SELECT department.id AS departmentId,
               department.name AS departmentName,
               (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND department_id = department.id) AS currentPeriodOrderVolume,
               (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND department_id = department.id) AS YoYOrderVolume,
               (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND department_id = department.id) AS QoQOrderVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND department_id = department.id) AS currentPeriodStatementVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND department_id = department.id) AS YoYStatementVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND department_id = department.id) AS QoQStatementVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND department_id = department.id) AS currentPeriodReportVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND department_id = department.id) AS YoYReportVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND department_id = department.id) AS QoQReportVolume,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND department_id = department.id) AS currentPeriodShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND department_id = department.id) AS YoYShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND department_id = department.id) AS QoQShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND department_id = department.id) AS currentPeriodRealAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND department_id = department.id) AS YoYRealAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND department_id = department.id) AS QoQRealAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND department_id = department.id) AS totalRealAmountSum
        FROM
            (
                SELECT
                    department.id,
                    department.name
                FROM
                    department,
                    sorted_department
                WHERE
                    department.id = sorted_department.department_id
                  AND department.deleted = 0
                  AND sorted_department.business_type = 'ASSET_BUSINESS'
                  <if test="dto != null and dto.departmentId != null">
                      AND department.id = #{dto.departmentId}
                  </if>
            ) AS department
        GROUP BY department.id
    </select>

    <!--评估人员效率查询-->
    <select id="getAssetsEvaluatePerEfficiencyVO" resultType="com.dayou.vo.AssetsEvaluateEfficiencyVO">
        SELECT user.id AS userId,
               user.userName AS userName,
               user.departmentId AS departmentId,
               user.departmentName AS departmentName,
               (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND principal_id = user.id) AS currentPeriodOrderVolume,
               (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND principal_id = user.id) AS YoYOrderVolume,
               (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND principal_id = user.id) AS QoQOrderVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND principal_id = user.id) AS currentPeriodStatementVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND principal_id = user.id) AS YoYStatementVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND principal_id = user.id) AS QoQStatementVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND principal_id = user.id) AS currentPeriodReportVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND principal_id = user.id) AS YoYReportVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND principal_id = user.id) AS QoQReportVolume,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND principal_id = user.id) AS currentPeriodShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND principal_id = user.id) AS YoYShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND principal_id = user.id) AS QoQShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND principal_id = user.id) AS currentPeriodRealAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND principal_id = user.id) AS YoYRealAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND principal_id = user.id) AS QoQRealAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND principal_id = user.id) AS totalRealAmountSum
        FROM
            (SELECT DISTINCT user.id AS id,
                    user.`name` AS userName,
                    department.name AS departmentName,
                    department.id AS departmentId
             FROM user,post,user_post,sorted_department,department
             WHERE `user`.id = user_post.user_id
               AND user_post.post_id = post.id
               AND post.department_id = sorted_department.department_id
               AND sorted_department.department_id = department.id
               AND sorted_department.deleted = 0
               AND user_post.deleted = 0
               AND sorted_department.business_type = 'ASSET_BUSINESS'
            ) AS user
        <where>
            <if test="dto != null and dto.departmentId != null">
                AND user.departmentId = #{dto.departmentId}
            </if>
            <if test="dto != null and dto.userName != null">
                AND user.userName LIKE CONCAT('%',#{dto.userName},'%')
            </if>
        </where>
        GROUP BY user.id
    </select>


    <sql id="assetsEvaEffDetailQuery">
        SELECT
        assets.order_id AS orderId,
        assets.name AS projectName,
        customer.name AS customerName,
        (SELECT customer_company.name FROM customer_company WHERE customer_company.id = clientele_sub_id) AS customerSubName,
        (SELECT name FROM customer_linkman WHERE id = assets.clientele_contact_id) AS linkmanName,
        bailor,
        report.production_no AS reportNo,
        (SELECT name FROM user WHERE id = assets.principal_id) AS principalName,
        members,
        (SELECT name FROM user WHERE id = assets.client_manager_id) AS clientManagerName,
        (SELECT SUM(check_value) FROM assets_evaluation_target WHERE report_no = report.production_no AND deleted = 0) AS checkValueSum,
        statement.created AS writeStatementDate,
        report.created AS writeReportDate,
        order_fund.should_amount AS shouldAmount,
        order_fund.real_amount AS realAmount,
        nodeInfo.currentNodeName,
        nodeInfo.handlerName
        FROM assets
        LEFT JOIN user ON user.id = assets.principal_id
        LEFT JOIN customer_company AS customer ON customer.id = assets.clientele_id
        LEFT JOIN (SELECT nodeInstance.id AS currentNodeId,
        user.name AS handlerName,
        node.name AS currentNodeName,
        nodeInstance.business_id AS businessId,
        nodeInstance.business_sub_id,
        nodeInstance.business_min_id,
        nodeInstance.state
        FROM work_flow_node_instance AS nodeInstance,
        work_task_record AS record,
        user,
        work_node AS node
        WHERE (state = 'PENDING' OR( state = 'FINISHED' AND node.`code` = 'BUSINESS_ARCHIVING' ))
        AND nodeInstance.node_id = node.id
        AND record.handler_id = user.id
        AND nodeInstance.deleted = 0
        AND record.instance_id = nodeInstance.id
        AND nodeInstance.business_type = 'ASSET_BUSINESS'
        <if test="dto != null and dto.efficiencyType == 'order' or dto.efficiencyType == 'report'">
            AND nodeInstance.business_min_id IS NOT NULL
        </if>
        <if test="dto != null and dto.efficiencyType == 'statement'">
            AND nodeInstance.business_sub_id IS NOT NULL
        </if>
        ) AS nodeInfo ON nodeInfo.businessId = assets.id
        LEFT JOIN assets_production AS statement ON statement.business_id =nodeInfo.businessId AND statement.production_no = nodeInfo.business_sub_id AND statement.deleted = 0 AND statement.production_type = 'STATEMENT'
        LEFT JOIN assets_production AS report ON report.business_id =nodeInfo.businessId AND report.production_no = nodeInfo.business_min_id AND report.deleted = 0 AND report.production_type != 'STATEMENT'
        LEFT JOIN order_fund ON order_fund.business_id = assets.id AND business_type = 'ASSET_BUSINESS' AND order_fund.deleted = 0
        WHERE assets.deleted = 0
        <if test="dto != null and dto.customerId != null">
            AND assets.clientele_id = #{dto.customerId}
        </if>
        <if test="dto != null and dto.clienteleContactId != null">
            AND assets.clientele_contact_id = #{dto.clienteleContactId}
        </if>
        <if test="dto != null and dto.departmentId != null">
            AND assets.department_id = #{dto.departmentId}
        </if>
        <if test="dto != null and dto.marketDepartmentId != null">
            AND assets.market_department_id = #{dto.marketDepartmentId}
        </if>
        <if test="dto != null and dto.principalId != null">
            AND assets.principal_id = #{dto.principalId}
        </if>
        <if test="dto != null and dto.clientManagerId != null">
            AND assets.client_manager_id = #{dto.clientManagerId}
        </if>
        <if test="dto != null and dto.efficiencyType == 'order'">
            AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime}
        </if>
        <if test="dto != null and dto.efficiencyType == 'statement'">
            AND statement.created BETWEEN #{dto.startTime} AND #{dto.endTime}
        </if>
        <if test="dto != null and dto.efficiencyType == 'report'">
            AND report.created BETWEEN #{dto.startTime} AND #{dto.endTime}
        </if>
        <if test="dto != null and dto.efficiencyType == 'should'">
            AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime}
            AND order_fund.should_amount IS NOT NULL
        </if>
        <if test="dto != null and dto.efficiencyType == 'real'">
            AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime}
            AND order_fund.real_amount IS NOT NULL
        </if>
        ORDER BY assets.created DESC
    </sql>

    <!--效率详情列表查询-->
    <select id="getAssetsEvaEffDetailVO" resultType="com.dayou.vo.AssetsEfficiencyDetailVO">
        <include refid="assetsEvaEffDetailQuery" />
    </select>

    <!--效率详情列表导出-->
    <select id="assetsEvaEffDetailVOExport" resultType="com.dayou.vo.AssetsEfficiencyDetailVO">
        <include refid="assetsEvaEffDetailQuery" />
    </select>

    <!--市场部效率查询-->
    <select id="getAssetsMarketDepEfficiencyVO" resultType="com.dayou.vo.AssetsMarketEfficiencyVO">
        SELECT department.id AS departmentId,
               department.name AS departmentName,
               (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND market_department_id = department.id) AS currentPeriodOrderVolume,
               (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND market_department_id = department.id) AS YoYOrderVolume,
               (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND market_department_id = department.id) AS QoQOrderVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND market_department_id = department.id) AS currentPeriodStatementVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND market_department_id = department.id) AS YoYStatementVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND market_department_id = department.id) AS QoQStatementVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND market_department_id = department.id) AS currentPeriodReportVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND market_department_id = department.id) AS YoYReportVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND market_department_id = department.id) AS QoQReportVolume,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND market_department_id = department.id) AS currentPeriodShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND market_department_id = department.id) AS YoYShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND market_department_id = department.id) AS QoQShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND market_department_id = department.id) AS currentPeriodRealAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND market_department_id = department.id) AS YoYRealAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND market_department_id = department.id) AS QoQRealAmountSum,
               (SELECT COUNT(assets.id) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.real_amount IS NOT NULL AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND market_department_id = department.id) AS currentPeriodRealAmountOrderCount,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND market_department_id = department.id) AS totalRealAmountSum
        FROM
            (
                SELECT department.name,
                       department.id
                FROM assets,department
                WHERE assets.market_department_id = department.id
                <if test="dto != null and dto.departmentId != null">
                    AND department.id = #{dto.departmentId}
                </if>
                GROUP BY department.id
            ) AS department
        GROUP BY department.id
    </select>

    <!--客户经理效率查询-->
    <select id="getAssetsMarketPerEfficiencyVO" resultType="com.dayou.vo.AssetsMarketEfficiencyVO">
        SELECT user.id AS userId,
               user.userName,
               user.departmentId,
               user.departmentName,
               (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND client_manager_id = user.id) AS currentPeriodOrderVolume,
               (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND client_manager_id = user.id) AS YoYOrderVolume,
               (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND client_manager_id = user.id) AS QoQOrderVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND client_manager_id = user.id) AS currentPeriodStatementVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND client_manager_id = user.id) AS YoYStatementVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND client_manager_id = user.id) AS QoQStatementVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND client_manager_id = user.id) AS currentPeriodReportVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND client_manager_id = user.id) AS YoYReportVolume,
               (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND client_manager_id = user.id) AS QoQReportVolume,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND client_manager_id = user.id) AS currentPeriodShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND client_manager_id = user.id) AS YoYShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND client_manager_id = user.id) AS QoQShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND client_manager_id = user.id) AS currentPeriodRealAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND client_manager_id = user.id) AS YoYRealAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND client_manager_id = user.id) AS QoQRealAmountSum,
               (SELECT COUNT(assets.id) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.real_amount IS NOT NULL AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND client_manager_id = user.id) AS currentPeriodRealAmountOrderCount,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND client_manager_id = user.id) AS totalRealAmountSum
        FROM
            (
                SELECT user.name AS userName,
                       user.id,
                       department.id AS departmentId,
                       department.name AS departmentName
                FROM assets,user,user_post,post,department
                WHERE assets.client_manager_id = user.id
                  AND user.id = user_post.user_id
                  AND user_post.post_id = post.id
                  AND post.department_id = department.id
                GROUP BY user.id
            ) AS user
        <where>
            <if test="dto != null and dto.departmentId != null">
                AND user.departmentId = #{dto.departmentId}
            </if>
            <if test="dto != null and dto.userName != null">
                AND user.userName LIKE CONCAT('%',#{dto.userName},'%')
            </if>
        </where>
        GROUP BY user.id
    </select>

    <!--资产客户效率查询-->
    <select id="getAssetsCustomerTotalEfficiencyVO" resultType="com.dayou.vo.AssetsCustomerEfficiencyVO">
        SET @start_date = #{dto.startTime};
        SET @end_date = #{dto.endTime};
        SET @start_date_last_year = DATE_SUB(@start_date, INTERVAL 1 YEAR);
        SET @end_date_last_year = DATE_SUB(@end_date, INTERVAL 1 YEAR);
        SET @start_date_qoq = DATE_SUB(@start_date, INTERVAL DATEDIFF(@end_date, @start_date) DAY);
        SET @end_date_qoq = DATE_SUB(@end_date, INTERVAL DATEDIFF(@end_date, @start_date) DAY);
        SELECT customer.id AS customerId,
        customer.name AS customerName,
        (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN @start_date AND @end_date AND clientele_id = customer.id) AS currentPeriodOrderVolume,
        (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN @start_date_last_year AND @end_date_last_year AND clientele_id = customer.id) AS YoYOrderVolume,
        (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN @start_date_qoq AND @end_date_qoq AND clientele_id = customer.id) AS QoQOrderVolume,
        (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN @start_date AND @end_date AND clientele_id = customer.id) AS currentPeriodStatementVolume,
        (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN @start_date_last_year AND @end_date_last_year AND clientele_id = customer.id) AS YoYStatementVolume,
        (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN @start_date_qoq AND @end_date_qoq AND clientele_id = customer.id) AS QoQStatementVolume,
        (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN @start_date AND @end_date AND clientele_id = customer.id) AS currentPeriodReportVolume,
        (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN @start_date_last_year AND @end_date_last_year AND clientele_id = customer.id) AS YoYReportVolume,
        (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN @start_date_qoq AND @end_date_qoq AND clientele_id = customer.id) AS QoQReportVolume,
        (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN @start_date AND @end_date AND clientele_id = customer.id) AS currentPeriodShouldAmountSum,
        (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN @start_date_last_year AND @end_date_last_year AND clientele_id = customer.id) AS YoYShouldAmountSum,
        (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN @start_date_qoq AND @end_date_qoq AND clientele_id = customer.id) AS QoQShouldAmountSum,
        (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN @start_date AND @end_date AND clientele_id = customer.id) AS currentPeriodRealAmountSum,
        (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN @start_date_last_year AND @end_date_last_year AND clientele_id = customer.id) AS YoYRealAmountSum,
        (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN @start_date_qoq AND @end_date_qoq AND clientele_id = customer.id) AS QoQRealAmountSum,
        (SELECT COUNT(order_fund.real_amount) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND order_fund.discount > 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN @start_date AND @end_date AND clientele_id = customer.id) AS currentDiscountVolume,
        (SELECT COUNT(order_fund.real_amount) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND order_fund.discount > 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN @start_date_last_year AND @end_date_last_year AND clientele_id = customer.id) AS YoYDiscountVolume,
        (SELECT COUNT(order_fund.real_amount) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND order_fund.discount > 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN @start_date_qoq AND @end_date_qoq AND clientele_id = customer.id) AS QoQDiscountVolume,
        (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND clientele_id = customer.id) AS totalRealAmountSum
        FROM
        (
            SELECT customer_company.id, customer_company.name
            FROM assets,customer_company
            WHERE customer_company.id = assets.clientele_id
            AND parent_id IS NULL
            AND customer_company.name != '个人客户'
            AND terminal = 0
            AND customer_company.deleted = 0
            AND assets.created BETWEEN @start_date AND @end_date
            GROUP BY id
        ) AS customer
        <where>
            <if test="dto != null and dto.customerId != null">
                AND customer.id = #{dto.customerId}
            </if>
        </where>
    </select>

    <!--资产客户效率值(客户经理)查询-->
    <select id="getAssetsCustomerManagerEfficiencyVO" resultType="com.dayou.vo.AssetsCustomerEfficiencyVO">
        SELECT customer.id AS customerId,
        customer.name AS customerName,
        customer.userId,
        customer.userName,
        customer.clientele_contact_id AS clienteleContactId,
        (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND clientele_contact_id = customer.clientele_contact_id) AS currentPeriodOrderVolume,
        (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND clientele_contact_id = customer.clientele_contact_id) AS YoYOrderVolume,
        (SELECT COUNT(assets.id) FROM assets LEFT JOIN assets_production AS pro ON pro.business_id = assets.id AND production_type != 'STATEMENT' AND pro.deleted = 0 WHERE assets.deleted = 0 AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND clientele_contact_id = customer.clientele_contact_id) AS QoQOrderVolume,
        (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND clientele_contact_id = customer.clientele_contact_id) AS currentPeriodStatementVolume,
        (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND clientele_contact_id = customer.clientele_contact_id) AS YoYStatementVolume,
        (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND clientele_contact_id = customer.clientele_contact_id) AS QoQStatementVolume,
        (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND clientele_contact_id = customer.clientele_contact_id) AS currentPeriodReportVolume,
        (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND clientele_contact_id = customer.clientele_contact_id) AS YoYReportVolume,
        (SELECT COUNT(production.id) FROM assets,assets_production AS production WHERE assets.deleted = 0 AND assets.id = production.business_id AND production_type != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = production_no AND deleted = 0) > 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND clientele_contact_id = customer.clientele_contact_id) AS QoQReportVolume,
        (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND clientele_contact_id = customer.clientele_contact_id) AS currentPeriodShouldAmountSum,
        (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND clientele_contact_id = customer.clientele_contact_id) AS YoYShouldAmountSum,
        (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND clientele_contact_id = customer.clientele_contact_id) AS QoQShouldAmountSum,
        (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND clientele_contact_id = customer.clientele_contact_id) AS currentPeriodRealAmountSum,
        (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND clientele_contact_id = customer.clientele_contact_id) AS YoYRealAmountSum,
        (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND clientele_contact_id = customer.clientele_contact_id) AS QoQRealAmountSum,
        (SELECT COUNT(order_fund.real_amount) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND order_fund.discount > 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND clientele_contact_id = customer.clientele_contact_id) AS currentDiscountVolume,
        (SELECT COUNT(order_fund.real_amount) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND order_fund.discount > 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND clientele_contact_id = customer.clientele_contact_id) AS YoYDiscountVolume,
        (SELECT COUNT(order_fund.real_amount) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND order_fund.discount > 0 AND business_type = 'ASSET_BUSINESS' AND assets.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND DATE_SUB(#{dto.endTime}, INTERVAL DATEDIFF(#{dto.endTime},#{dto.startTime}) DAY) AND clientele_contact_id = customer.clientele_contact_id) AS QoQDiscountVolume,
        (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM assets, order_fund WHERE assets.deleted = 0 AND assets.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'ASSET_BUSINESS' AND clientele_contact_id = customer.clientele_contact_id) AS totalRealAmountSum
        FROM
        (
        SELECT user.id AS userId, user.name AS userName,par.id,par.name,customer_linkman.id AS clientele_contact_id
        FROM assets,customer_company AS sub,user,customer_linkman, customer_company AS par
        WHERE assets.clientele_contact_id = customer_linkman.id
        AND customer_linkman.client_manager_id = user.id
        AND customer_linkman.cc_id = sub.id
        AND sub.parent_id = par.id
        AND assets.created BETWEEN #{dto.startTime} AND #{dto.endTime}
        ORDER BY id DESC
        ) AS customer
        <where>
            <if test="dto != null and dto.customerId != null">
                AND customer.id = #{dto.customerId}
            </if>
            <if test="dto != null and dto.userName != null">
                AND customer.userName LIKE CONCAT('%',#{dto.userName},'%')
            </if>
        </where>
        GROUP BY customer.clientele_contact_id
    </select>


</mapper>