<?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.MajorStatisticalStatementMapper">

    <sql id="majorLedgerQuery">
        SELECT (@i :=  @i + 1) AS id,
               major.order_id AS orderId,
               report.report_no AS reportNo,
               department.name AS departmentName,
               major.name AS projectName,
               customer.name AS customerName,
               customer_sub.name AS customerSubName,
               manager.name AS clientManagerName,
               principal.name AS principalName,
               major.members AS members,
               major.owner AS owner,
               major.bailor AS bailor,
               major.bailor_contact_tel AS bailorContactTel,
               major.bailor_address AS bailorAddress,
               major.evaluate_aim AS evaluateAim,
               report.value_timing AS valueTiming,
               GROUP_CONCAT(target.located SEPARATOR ';') AS located,
               GROUP_CONCAT(target.land_use SEPARATOR ';') AS landUse,
               GROUP_CONCAT(target.build_acreage SEPARATOR ';') AS buildAcreage,
               GROUP_CONCAT(target.land_acreage SEPARATOR ';') AS landAcreage,
               GROUP_CONCAT(DATE(target.created) SEPARATOR ';') AS reconnaissanceDate,
               report.evaluate_price AS evaluatePrice,
               report.evaluate_amount AS evaluateAmount,
               fund.real_amount AS productionRealAmount,
               report.repertory_out_time AS repertoryOutTime,
               report.main_worker AS firstReporter,
               report.minor_worker AS secReporter,
               (
                   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 = major.id
                     AND instance.business_sub_id = statement.report_no
                     AND node.code = 'RECHECK_STATEMENT'
                     AND instance.deleted = 0
               ) AS secStatementChecker,
               (
                   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 = major.id
                     AND instance.business_sub_id = statement.report_no
                     AND node.code = 'THIRD_CHECK_STATEMENT'
                     AND instance.deleted = 0
               ) AS thirdStatementChecker,
               (
                   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 = major.id
                     AND instance.business_min_id = report.report_no
                     AND node.code = 'RECHECK_REPORT'
                     AND instance.deleted = 0
               ) AS secReportChecker,
               (
                   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 = major.id
                     AND instance.business_min_id = report.report_no
                     AND node.code = 'THIRD_CHECK_REPORT'
                     AND instance.deleted = 0
               ) AS thirdReportReChecker,
            major.remark AS comments
        FROM  (SELECT @i := 0) AS sort,major
        LEFT JOIN major_target AS target ON target.major_id = major.id
        LEFT JOIN major_production AS statement ON statement.report_no = target.statement_no AND statement.production = 'STATEMENT'
        LEFT JOIN major_production AS report ON report.report_no = target.report_no AND report.production != 'STATEMENT'
        LEFT JOIN production_fund AS fund ON fund.production_no = report.report_no
        LEFT JOIN user AS manager ON manager.id = major.client_manager_id
        LEFT JOIN user AS principal ON principal.id = major.principal_id
        LEFT JOIN department ON department.id = major.department_id
        LEFT JOIN customer_company AS customer ON customer.id = major.clientele_id
        LEFT JOIN customer_company AS customer_sub ON customer_sub.id = major.clientele_sub_id
        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 = major.id
            AND (IF(report.report_no IS NULL, nodeInfo.business_min_id IS NULL,
                    report.report_no = nodeInfo.business_min_id))
        WHERE major.deleted = 0
        <if test="dto != null and dto.keyWord != null and dto.keyWord != '' ">
            AND (
            major.order_id LIKE CONCAT('%',#{dto.keyWord},'%') OR
            major.name LIKE CONCAT('%',#{dto.keyWord},'%') OR
            report.name LIKE CONCAT('%',#{dto.keyWord},'%') OR
            report.report_no LIKE CONCAT('%',#{dto.keyWord},'%') OR
            principal.name LIKE CONCAT('%',#{dto.keyWord},'%') OR
            major.business_object_type LIKE CONCAT('%',#{dto.keyWord},'%') OR
            major.allot_type LIKE CONCAT('%',#{dto.keyWord},'%') OR
            customer.name LIKE CONCAT('%',#{dto.keyWord},'%') OR
            customer_sub.name LIKE CONCAT('%',#{dto.keyWord},'%')
            )
        </if>
        <if test="dto != null and dto.clientManagerId">
            AND major.client_manager_id = #{dto.clientManagerId}
        </if>
        <if test="dto != null and dto.principalId">
            AND major.principal_id = #{dto.principalId}
        </if>
        <if test="dto != null and dto.departmentId != null">
            AND major.department_id = #{dto.departmentId}
        </if>
        <if test="dto != null and dto.memberId != null">
            AND JSON_CONTAINS(major.members, CAST(#{dto.memberId} AS JSON))
        </if>
        <if test="dto != null and dto.nodeCode != null">
            AND nodeInfo.node = #{dto.nodeCode}
        </if>
        <if test="dto != null and dto.startTime != null and dto.endTime!= null">
            AND (major.created BETWEEN #{dto.startTime} AND #{dto.endTime})
        </if>
        GROUP BY reportNo,major.id
        ORDER BY major.created DESC
    </sql>

    <!--查询大中型台账-->
    <select id="getMajorLedgerVO" resultType="com.dayou.vo.MajorLedgerVO">
        <include refid="majorLedgerQuery" />
    </select>

    <!--导出大中型台账-->
    <select id="exportMajorLedgerVO" resultType="com.dayou.vo.MajorLedgerVO">
        <include refid="majorLedgerQuery" />
    </select>

    <!--评估部绩效扣分查询-->
    <select id="getMajorPerformanceDeductionVO" resultType="com.dayou.vo.MajorPerformanceDeductionVO">
        SELECT
        department.id AS departmentId,
        department.name AS departmentName,
        COALESCE ( SUM( reCheckPer.normal_mistake ), 0 ) * 3 AS reCheckNormalMistakeScore,
        COALESCE ( SUM( reCheckPer.hard_mistake ), 0 ) * 8 AS reCheckHardMistakeScore,
        COALESCE ( SUM( reCheckPer.fatal_mistake ), 0 ) * 41 AS reCheckFatalMistakeScore,
        COALESCE ( (COALESCE ( SUM( reCheckPer.normal_mistake ), 0 ) *3 ) + (COALESCE ( SUM( reCheckPer.hard_mistake ), 0 ) * 8) + (COALESCE ( SUM( reCheckPer.fatal_mistake ), 0 ) * 41) , 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 = 'MAJOR_BUSINESS'
        ) AS department
        LEFT JOIN (
        SELECT
        department_id,
        major.created,
        major_production.id AS production_id
        FROM
        major
        LEFT JOIN major_production ON major_production.major_id = major.id
        AND major_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 = 'MAJOR_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
        <where>
            <if test="dto != null and dto.departmentId != null">
                AND department.id = #{dto.departmentId}
            </if>
        </where>
        GROUP BY department.id
    </select>

    <!--评估人员绩效扣分查询-->
    <select id="getMajorPerformanceDeductionVOByEvaluator" resultType="com.dayou.vo.MajorPerformanceDeductionVO">
        SELECT user.id AS userId,
        user.userName,
        user.departmentId,
        user.departmentName,
        COALESCE ( SUM( reCheckPer.normal_mistake ), 0 ) * 3 AS reCheckNormalMistakeScore,
        COALESCE ( SUM( reCheckPer.hard_mistake ), 0 ) * 8 AS reCheckHardMistakeScore,
        COALESCE ( SUM( reCheckPer.fatal_mistake ), 0 ) * 41 AS reCheckFatalMistakeScore,
        COALESCE ( (COALESCE ( SUM( reCheckPer.normal_mistake ), 0 ) *3 ) + (COALESCE ( SUM( reCheckPer.hard_mistake ), 0 ) * 8) + (COALESCE ( SUM( reCheckPer.fatal_mistake ), 0 ) * 41) , 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 = 'MAJOR_BUSINESS'
        GROUP BY user.id
        ) AS user
        LEFT JOIN (
        SELECT
        principal_id,
        major.created,
        major_production.id AS production_id
        FROM
        major
        LEFT JOIN major_production ON major_production.major_id = major.id
        AND major_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 = 'MAJOR_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
        <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>

    <!--根据部门id获取部门绩效扣分-->
    <select id="getMajorPerformanceDeductionVOByDepId" resultType="com.dayou.vo.MajorPerformanceDeductionVO">
        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 ( (COALESCE ( SUM( checkPer.normal_mistake ), 0 ) * 3) + (COALESCE ( SUM( checkPer.hard_mistake ), 0 ) * 8) + (COALESCE ( SUM( checkPer.fatal_mistake ), 0 ) * 41) + (COALESCE ( SUM( reCheckPer.normal_mistake ), 0 ) *3 ) + (COALESCE ( SUM( reCheckPer.hard_mistake ), 0 ) * 8) + (COALESCE ( SUM( reCheckPer.fatal_mistake ), 0 ) * 41) , 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 = 'MAJOR_BUSINESS'
        GROUP BY user.id
        ) AS user
        LEFT JOIN (
        SELECT
        principal_id,
        major.created,
        major_production.id AS production_id
        FROM
        major
        LEFT JOIN major_production ON major_production.major_id = major.id
        AND major_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 = 'MAJOR_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 = 'MAJOR_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
        <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="getMajorErrorTypeCountVO" resultType="com.dayou.vo.MajorErrorTypeCountVO">
        SELECT user.id AS userId,
        user.departmentId,
        user.userName,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','委托书界定事项和报告描述不符','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount01,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','方位图有误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount02,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','实景图有误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount03,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','测算参数取值不充分','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount04,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','估价结果确定方式不恰当','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount05,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','估价方法表述不完整','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount06,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','实物、权益、区位描述缺项','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount07,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','价值定义表述不完整','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount08,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','非重要文字和数据录入错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount09,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','估价结果价格有误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount10,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','大小写不一致','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount11,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','面积错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount12,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','委托方函重要数据出错','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount13,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','委托人名称出错','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount14,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','假设限制条件错漏','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount15,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','价值定义表述错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount16,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','报告章节缺漏','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount17,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','报告出具日期错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount18,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','报告有效期错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount19,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','重要文字和数据录入错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount20,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','估价对象错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount21,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','估价目的错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount22,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','价值时点设定出错','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount23,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','登记状况错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount24,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','权利状况错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount25,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','利用状况错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount26,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','技术路线错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount27,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','关键参数取值错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount28,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','编造虚假案例','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount29,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','编造虚假照片','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount30,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','估价依据资料严重不全','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount31,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','评估结果严重失真超30%以上','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount32,
        (SELECT COUNT(id) FROM business_production_performance WHERE business_type = 'MAJOR_BUSINESS' AND production_id
        = orderInfo.production_id AND reason LIKE CONCAT('%','关键文字和数据错误','%') AND deleted = 0
        <if test="dto.checkType == 'dep'">
            AND check_loop = '初审'
        </if>
        <if test="dto.checkType == 'com'">
            AND check_loop != '初审'
        </if>
        ) AS errorTypeCount33
        FROM (SELECT DISTINCT user.id AS id,
        user.`name` AS userName,
        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 = 'MAJOR_BUSINESS'
        ) AS user
        LEFT JOIN (
        SELECT
        principal_id,
        major.created,
        major_production.id AS production_id
        FROM
        major
        LEFT JOIN major_production ON major_production.major_id = major.id
        AND major_production.deleted = 0
        ) AS orderInfo ON user.id = orderInfo.principal_id
        <where>
            <if test="self == true">
                AND user.id = #{dto.userId}
            </if>
            <if test="dto != null and dto.departmentId != null and dep == true">
                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-->
    <sql id="majorPerformanceDeductionDetailQuery">
        SELECT performance.id,
        major.order_id            AS orderId,
        major.financial AS financial,
        major.members             AS members,
        major.evaluate_aim AS evaluateAim,
        production.name     AS projectName,
        customer.name              AS customerName,
        production.report_no   AS productionNo,
        (CASE production.production
        WHEN 'STATEMENT' THEN '意见书'
        WHEN 'LETTER' 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,
        (CASE #{dto.mistakeType}
        WHEN 'normal' THEN performance.normal_mistake * 3
        WHEN 'hard' THEN performance.hard_mistake * 8
        WHEN 'fatal' THEN performance.fatal_mistake * 41
        ELSE '-'
        END)                  AS mistakeScore,
        production.evaluate_amount AS evaluateAmount,
        performance.modified       AS checkTime,
        performance.reason         AS reason
        FROM major
        LEFT JOIN major_production AS production ON major.id = production.major_id
        LEFT JOIN business_production_performance AS performance ON production.id = performance.production_id
        LEFT JOIN customer_company AS customer ON customer.id = major.clientele_id
        LEFT JOIN user AS principal ON principal.id = major.principal_id
        LEFT JOIN user AS checker ON checker.id = performance.check_id
        WHERE major.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 major.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="getMajorPerformanceDeductionDetailVO" resultType="com.dayou.vo.MajorPerformanceDeductionDetailVO">
        <include refid="majorPerformanceDeductionDetailQuery" />
    </select>

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

    <!--评估部效率查询-->
    <!--大中型指定需要看了现场才算有效的接单数-->
    <select id="getMajorEvaluateDepEfficiencyVO" resultType="com.dayou.vo.MajorEvaluateEfficiencyVO">
        SELECT aim.evaluate_aim AS evaluateAim,
        aim.financial AS financial,
        (SELECT COUNT(major.id) FROM major LEFT JOIN major_target AS target ON target.major_id = major.id AND target.deleted = 0 WHERE target.created IS NULL AND major.deleted = 0 AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS unSiteCount,
        (SELECT COUNT(major.id) FROM (SELECT major.*, major_target.created AS targetCreated FROM major, major_target WHERE major.id = major_target.major_id) AS major LEFT JOIN major_production AS pro ON pro.major_id = major.id AND production != 'STATEMENT' AND pro.deleted = 0 WHERE major.informal_evaluate = 1 AND major.deleted = 0 AND major.targetCreated BETWEEN #{dto.startTime} AND #{dto.endTime} AND major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS informalEvaluateCount,
        (SELECT COUNT(major.id) FROM (SELECT major.*, major_target.created AS targetCreated FROM major, major_target WHERE major.id = major_target.major_id) AS major LEFT JOIN major_production AS pro ON pro.major_id = major.id AND production != 'STATEMENT' AND pro.deleted = 0 WHERE major.informal_evaluate = 0 AND major.deleted = 0 AND major.targetCreated BETWEEN #{dto.startTime} AND #{dto.endTime} AND major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS unInformalEvaluateCount,
        (SELECT COUNT(major.id) FROM (SELECT major.*, major_target.created AS targetCreated FROM major, major_target WHERE major.id = major_target.major_id) AS major LEFT JOIN major_production AS pro ON pro.major_id = major.id AND production != 'STATEMENT' AND pro.deleted = 0 WHERE major.deleted = 0 AND major.targetCreated BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS YoYOrderVolume,
        (SELECT COUNT(major.id) FROM (SELECT major.*, major_target.created AS targetCreated FROM major, major_target WHERE major.id = major_target.major_id) AS major LEFT JOIN major_production AS pro ON pro.major_id = major.id AND production != 'STATEMENT' AND pro.deleted = 0 WHERE major.deleted = 0 AND major.targetCreated 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 major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS QoQOrderVolume,
        (SELECT COUNT(production.id) FROM major,major_production AS production WHERE major.deleted = 0 AND major.id = production.major_id AND production = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = report_no AND deleted = 0) > 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS currentPeriodStatementVolume,
        (SELECT COUNT(production.id) FROM major,major_production AS production WHERE major.deleted = 0 AND major.id = production.major_id AND production = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = report_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 major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS YoYStatementVolume,
        (SELECT COUNT(production.id) FROM major,major_production AS production WHERE major.deleted = 0 AND major.id = production.major_id AND production = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = report_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 major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS QoQStatementVolume,
        (SELECT COUNT(production.id) FROM major,major_production AS production WHERE major.deleted = 0 AND major.id = production.major_id AND production != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = report_no AND deleted = 0) > 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS currentPeriodReportVolume,
        (SELECT COUNT(production.id) FROM major,major_production AS production WHERE major.deleted = 0 AND major.id = production.major_id AND production != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = report_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 major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS YoYReportVolume,
        (SELECT COUNT(production.id) FROM major,major_production AS production WHERE major.deleted = 0 AND major.id = production.major_id AND production != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = report_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 major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS QoQReportVolume,
        (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS currentPeriodShouldAmountSum,
        (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS YoYShouldAmountSum,
        (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.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 major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS QoQShouldAmountSum,
        (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS currentPeriodRealAmountSum,
        (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS YoYRealAmountSum,
        (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.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 major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS QoQRealAmountSum,
        (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS totalRealAmountSum,
        (SELECT COUNT(major.id) FROM major WHERE major.informal_evaluate = 1 AND major.deleted = 0 AND YEAR(major.created) = YEAR(CURDATE()) AND major.evaluate_aim = aim.evaluate_aim AND major.financial = aim.financial
        <if test="dto != null and dto.departmentId != null">
            AND department_id = #{dto.departmentId}
        </if>
        ) AS yearInformalEvaluateCount
        FROM
        (
        SELECT DISTINCT evaluate_aim,
               financial
        FROM major) AS aim
        GROUP BY aim.evaluate_aim,aim.financial
    </select>

    <!--评估人员效率查询-->
    <!--大中型指定需要看了现场才算有效的接单数-->
    <select id="getMajorEvaluatePerEfficiencyVO" resultType="com.dayou.vo.MajorEvaluateEfficiencyVO">
        SELECT user.id AS userId,
               user.userName AS userName,
               user.departmentId AS departmentId,
               user.departmentName AS departmentName,
        (SELECT COUNT(major.id) FROM (SELECT major.*, major_target.created AS targetCreated FROM major, major_target WHERE major.id = major_target.major_id) AS major LEFT JOIN major_production AS pro ON pro.major_id = major.id AND production != 'STATEMENT' AND pro.deleted = 0 WHERE major.informal_evaluate = 1 AND major.deleted = 0 AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime}  AND (principal_id = user.id OR JSON_CONTAINS(members, CAST(user.id AS JSON)))) AS informalEvaluateCount,
        (SELECT COUNT(major.id) FROM (SELECT major.*, major_target.created AS targetCreated FROM major, major_target WHERE major.id = major_target.major_id) AS major LEFT JOIN major_production AS pro ON pro.major_id = major.id AND production != 'STATEMENT' AND pro.deleted = 0 WHERE major.informal_evaluate = 0 AND major.deleted = 0 AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime}  AND (principal_id = user.id OR JSON_CONTAINS(members, CAST(user.id AS JSON)))) AS unInformalEvaluateCount,
        (SELECT COUNT(major.id) FROM (SELECT major.*, major_target.created AS targetCreated FROM major, major_target WHERE major.id = major_target.major_id) AS major LEFT JOIN major_production AS pro ON pro.major_id = major.id AND production != 'STATEMENT' AND pro.deleted = 0 WHERE major.deleted = 0 AND major.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR)  AND (principal_id = user.id OR JSON_CONTAINS(members, CAST(user.id AS JSON)))) AS YoYOrderVolume,
        (SELECT COUNT(major.id) FROM (SELECT major.*, major_target.created AS targetCreated FROM major, major_target WHERE major.id = major_target.major_id) AS major LEFT JOIN major_production AS pro ON pro.major_id = major.id AND production != 'STATEMENT' AND pro.deleted = 0 WHERE major.deleted = 0 AND major.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 OR JSON_CONTAINS(members, CAST(user.id AS JSON)))) AS QoQOrderVolume,
        (SELECT COUNT(production.id) FROM major,major_production AS production WHERE major.deleted = 0 AND major.id = production.major_id AND production = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = report_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 major,major_production AS production WHERE major.deleted = 0 AND major.id = production.major_id AND production = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = report_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 major,major_production AS production WHERE major.deleted = 0 AND major.id = production.major_id AND production = 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_sub_id = report_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 major,major_production AS production WHERE major.deleted = 0 AND major.id = production.major_id AND production != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = report_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 major,major_production AS production WHERE major.deleted = 0 AND major.id = production.major_id AND production != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = report_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 major,major_production AS production WHERE major.deleted = 0 AND major.id = production.major_id AND production != 'STATEMENT' AND production.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = report_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 major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime}  AND principal_id = user.id) AS currentPeriodShouldAmountSum,
        (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.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 major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.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 major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime}  AND principal_id = user.id) AS currentPeriodRealAmountSum,
        (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.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 major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.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 major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS'  AND principal_id = user.id) AS totalRealAmountSum,
        (
               SELECT ROUND(SUM(TIMESTAMPDIFF(HOUR, start.created, end.created)) / COUNT(major.id) / 24, 2)
               FROM major
                        INNER JOIN (
                   SELECT major.id,node.`code`, instance.created
                   FROM work_node AS node, work_flow_node_instance AS instance, major
                   WHERE node.id = instance.node_id
                     AND instance.business_id = major.id
                     AND instance.deleted = 0
                     AND node.`code` = 'WRITE_STATEMENT'
                     AND business_type = 'MAJOR_BUSINESS'
               ) AS start ON major.id = start.id
                        INNER JOIN (
                   SELECT major.id,node.`code`, instance.created
                   FROM work_node AS node, work_flow_node_instance AS instance, major
                   WHERE node.id = instance.node_id
                     AND instance.business_id = major.id
                     AND instance.business_sub_id IS NOT NULL
                     AND instance.deleted = 0
                     AND node.`code` = 'STATEMENT_BOOKBINDING_STAMP'
                     AND business_type = 'MAJOR_BUSINESS'
               ) AS end ON major.id = end.id
               WHERE principal_id = user.id
                 AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime}
        ) AS statementAICPTime,
       (
           SELECT ROUND(SUM(TIMESTAMPDIFF(HOUR, start.created, end.created)) / COUNT(major.id) / 24, 2)
           FROM major
                    INNER JOIN (
               SELECT major.id,node.`code`, instance.created
               FROM work_node AS node, work_flow_node_instance AS instance, major
               WHERE node.id = instance.node_id
                 AND instance.business_id = major.id
                 AND instance.deleted = 0
                 AND node.`code` = 'WRITE_REPORT'
                 AND business_type = 'MAJOR_BUSINESS'
           ) AS start ON major.id = start.id
                    INNER JOIN (
               SELECT major.id,node.`code`, instance.created
               FROM work_node AS node, work_flow_node_instance AS instance, major
               WHERE node.id = instance.node_id
                 AND instance.business_id = major.id
                 AND instance.business_min_id IS NOT NULL
                 AND instance.deleted = 0
                 AND node.`code` = 'REPORT_BOOKBINDING_STAMP'
                 AND business_type = 'MAJOR_BUSINESS'
           ) AS end ON major.id = end.id
           WHERE principal_id = user.id
             AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime}
       ) AS reportAICPTime,
        (
               SELECT ROUND(SUM(TIMESTAMPDIFF(HOUR, major.created, end.created)) / COUNT(major.id) / 24, 2)
               FROM major
                        INNER JOIN (
                   SELECT major.id,node.`code`, instance.created
                   FROM work_node AS node, work_flow_node_instance AS instance, major
                   WHERE node.id = instance.node_id
                     AND instance.business_id = major.id
                     AND instance.deleted = 0
                     AND node.`code` = 'FORMAL_QUOTATION'
                     AND business_type = 'MAJOR_BUSINESS'
               ) AS end ON major.id = end.id
               WHERE principal_id = user.id
                 AND financial = 1
                 AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime}
        ) AS financialQuoteProcessingTime,
        (
            SELECT ROUND(SUM(TIMESTAMPDIFF(HOUR, major.created, end.created)) / COUNT(major.id) / 24, 2)
            FROM major
                     INNER JOIN (
                SELECT major.id,node.`code`, instance.created
                FROM work_node AS node, work_flow_node_instance AS instance, major
                WHERE node.id = instance.node_id
                AND instance.business_id = major.id
                AND instance.deleted = 0
                AND node.`code` = 'FORMAL_QUOTATION'
                AND business_type = 'MAJOR_BUSINESS'
            ) AS end ON major.id = end.id
            WHERE principal_id = user.id
            AND financial = 1
            AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime}
           ) AS unFinancialQuoteProcessingTime
        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 = 'MAJOR_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="majorEvaEffDetailQuery">
        SELECT
        major.order_id AS orderId,
        financial,
        major.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 = major.clientele_contact_id) AS linkmanName,
        major.owner,
        report.report_no AS reportNo,
        (SELECT name FROM user WHERE id = major.principal_id) AS principalName,
        members,
        (SELECT name FROM user WHERE id = major.client_manager_id) AS clientManagerName,
        (SELECT SUM(check_value) FROM major_target WHERE report_no = report.report_no AND deleted = 0) AS checkValueSum,
        (SELECT SUM(build_acreage) FROM major_target WHERE report_no = report.report_no AND deleted = 0) AS buildAcreageSum,
        evaluate_aim,
        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 major
        LEFT JOIN user ON user.id = major.principal_id
        LEFT JOIN customer_company AS customer ON customer.id = major.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 = 'MAJOR_BUSINESS'
        <if test="dto != null and 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 = major.id
        LEFT JOIN major_production AS statement ON statement.major_id =nodeInfo.businessId AND statement.report_no = nodeInfo.business_sub_id AND statement.deleted = 0 AND statement.production = 'STATEMENT'
        LEFT JOIN major_production AS report ON report.major_id =nodeInfo.businessId AND report.report_no = nodeInfo.business_min_id AND report.deleted = 0 AND report.production != 'STATEMENT'
        LEFT JOIN order_fund ON order_fund.business_id = major.id AND business_type = 'MAJOR_BUSINESS' AND order_fund.deleted = 0
        WHERE major.deleted = 0
        <if test="dto != null and dto.efficiencyType == 'order'">
            AND (SELECT COUNT(major_target.major_id) FROM major_target WHERE major_target.deleted = 0 AND major_target.major_id = major.id) > 0
        </if>
        <if test="dto != null and dto.customerId != null">
            AND major.clientele_id = #{dto.customerId}
        </if>
        <if test="dto != null and dto.clienteleContactId != null">
            AND major.clientele_contact_id = #{dto.clienteleContactId}
        </if>
        <if test="dto != null and dto.departmentId != null">
            AND major.department_id = #{dto.departmentId}
        </if>
        <if test="dto != null and dto.evaluateAim != null and dto.financial != null">
            AND major.evaluate_aim = #{dto.evaluateAim}
            AND major.financial = #{dto.financial}
        </if>
        <if test="dto != null and dto.informalEvaluate != null">
            AND major.informal_evaluate = #{dto.informalEvaluate}
        </if>
        <if test="dto != null and dto.marketDepartmentId != null">
            AND major.market_department_id = #{dto.marketDepartmentId}
        </if>
        <if test="dto != null and dto.principalId != null">
            AND (major.principal_id = #{dto.principalId}
            <if test="dto != null and dto.memberId != null">
                OR JSON_CONTAINS(major.members, CAST(#{dto.memberId} AS JSON))
            </if>)
        </if>
        <if test="dto != null and dto.clientManagerId != null">
            AND major.client_manager_id = #{dto.clientManagerId}
        </if>
        <if test="dto != null and dto.efficiencyType == 'order'">
            AND major.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 major.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 major.created BETWEEN #{dto.startTime} AND #{dto.endTime}
            AND order_fund.real_amount IS NOT NULL
        </if>
        <if test="dto != null and dto.efficiencyType == 'unSite'">
            AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime}
            AND (SELECT COUNT(major_target.major_id) FROM major_target WHERE major_target.deleted = 0 AND major_target.major_id = major.id) = 0
        </if>
        ORDER BY major.created DESC
    </sql>

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

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

    <!--项目部绩效查询-->
    <select id="getMajorDepPerformanceOverviewVO" resultType="com.dayou.vo.MajorDepPerformanceOverviewVO">
        SELECT department.departmentId,
               department.departmentName,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM major, order_fund WHERE major.deleted = 0 AND major.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'MAJOR_BUSINESS' AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND major.department_id = department.departmentId) AS outputValue,
               (SELECT COUNT(major.id) FROM major LEFT JOIN major_production AS pro ON pro.major_id = major.id AND production != 'STATEMENT' AND pro.deleted = 0 WHERE major.deleted = 0 AND (SELECT COUNT(id) FROM work_flow_node_instance WHERE business_min_id = report_no AND deleted = 0) > 0 AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND major.department_id = department.departmentId) AS orderVolume,
               (
                   SELECT ROUND(SUM(TIMESTAMPDIFF(HOUR, major.created, end.created)) / COUNT(major.id) / 24, 2)
                   FROM major
                            INNER JOIN (
                       SELECT major.id,node.`code`, instance.created
                       FROM work_node AS node, work_flow_node_instance AS instance, major
                       WHERE node.id = instance.node_id
                         AND instance.business_id = major.id
                         AND instance.deleted = 0
                         AND node.`code` = 'FORMAL_QUOTATION'
                         AND business_type = 'MAJOR_BUSINESS'
                   ) AS end ON major.id = end.id
                   WHERE department_id = department.departmentId
                     AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime}
               ) AS quoteProcessingTime,
               COALESCE ( SUM( reCheckPer.normal_mistake ), 0 ) AS reCheckNormalMistakeCount,
               COALESCE ( SUM( reCheckPer.hard_mistake ), 0 ) AS reCheckHardMistakeCount,
               COALESCE ( SUM( reCheckPer.fatal_mistake ), 0 ) AS reCheckFatalMistakeCount,
               (SELECT ROUND(SUM((
                        IF
                        ((target.evaluate_value * 10000) > production.evaluate_amount,
                         (target.evaluate_value * 10000) / production.evaluate_amount,
                         production.evaluate_amount / (target.evaluate_value * 10000))
                        )) / COUNT(major.id), 2)
                FROM
                    major,
                    major_target AS target,
                    major_production AS production
                WHERE major.deleted = 0
                  AND major.id = target.major_id
                  AND target.report_no = production.report_no
                  AND department_id = department.departmentId
                  AND major.created BETWEEN #{dto.startTime} AND #{dto.endTime}
               ) AS quotationAccuracy
        FROM (
             SELECT DISTINCT
                 department.name AS departmentName,
                 department.id AS departmentId
             FROM sorted_department,department
             WHERE sorted_department.department_id = department.id
               AND sorted_department.deleted = 0
               AND sorted_department.business_type = 'MAJOR_BUSINESS'
             ) AS department
        LEFT JOIN (
        SELECT
            department_id,
            major.created,
            major_production.id AS production_id
        FROM
            major
        LEFT JOIN major_production ON major_production.major_id = major.id
        AND major_production.deleted = 0
        ) AS orderInfo ON department.departmentId = orderInfo.department_id
        LEFT JOIN ( SELECT production_id, fatal_mistake, hard_mistake, normal_mistake
                    FROM
                        business_production_performance
                    WHERE business_type = 'MAJOR_BUSINESS' AND check_loop = '复审'
                    AND (business_production_performance.modified BETWEEN #{dto.startTime} AND #{dto.endTime})
        ) AS reCheckPer ON reCheckPer.production_id = orderInfo.production_id
        GROUP BY department.departmentId
    </select>
</mapper>