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

    <!--个贷部门效率值查询-->
    <select id="getPersonalDepEfficiencyVO" resultType="com.dayou.vo.PersonalDepEfficiencyVO">
        SELECT evaluateAim,
               (SELECT COUNT(personal.id) FROM personal WHERE personal.deleted = 0 AND personal.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS currentPeriodOrderVolume,
               (SELECT COUNT(personal.id) FROM personal WHERE personal.deleted = 0 AND personal.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS YoYOrderVolume,
               (SELECT COUNT(personal.id) FROM personal WHERE personal.deleted = 0 AND personal.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 IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS QoQOrderVolume,
               (SELECT COUNT(production.id) FROM personal,personal_target AS target,personal_production AS production WHERE personal.id = target.personal_id AND target.id = production.target_id AND production = 'STATEMENT' AND production.deleted = 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS currentPeriodStatementVolume,
               (SELECT COUNT(production.id) FROM personal,personal_target AS target,personal_production AS production WHERE personal.id = target.personal_id AND target.id = production.target_id AND production = 'STATEMENT' AND production.deleted = 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS YoYStatementVolume,
               (SELECT COUNT(production.id) FROM personal,personal_target AS target,personal_production AS production WHERE personal.id = target.personal_id AND target.id = production.target_id AND production = 'STATEMENT' AND production.deleted = 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 IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS QoQStatementVolume,
               (SELECT COUNT(production.id) FROM personal,personal_target AS target,personal_production AS production WHERE personal.id = target.personal_id AND target.id = production.target_id AND (production = 'FINAL' OR production = 'LETTER') AND production.deleted = 0 AND production.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS currentPeriodReportVolume,
               (SELECT COUNT(production.id) FROM personal,personal_target AS target,personal_production AS production WHERE personal.id = target.personal_id AND target.id = production.target_id AND (production = 'FINAL' OR production = 'LETTER') AND production.deleted = 0 AND production.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS YoYReportVolume,
               (SELECT COUNT(production.id) FROM personal,personal_target AS target,personal_production AS production WHERE personal.id = target.personal_id AND target.id = production.target_id AND (production = 'FINAL' OR production = 'LETTER') AND production.deleted = 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 IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS QoQReportVolume,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM personal, order_fund WHERE personal.deleted = 0 AND personal.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'PERSONAL_BUSINESS' AND personal.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS currentPeriodShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM personal, order_fund WHERE personal.deleted = 0 AND personal.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'PERSONAL_BUSINESS' AND personal.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS YoYShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.should_amount),0) FROM personal, order_fund WHERE personal.deleted = 0 AND personal.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'PERSONAL_BUSINESS' AND personal.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 IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS QoQShouldAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM personal, order_fund WHERE personal.deleted = 0 AND personal.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'PERSONAL_BUSINESS' AND personal.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS currentPeriodRealAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM personal, order_fund WHERE personal.deleted = 0 AND personal.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'PERSONAL_BUSINESS' AND personal.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS YoYRealAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM personal, order_fund WHERE personal.deleted = 0 AND personal.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'PERSONAL_BUSINESS' AND personal.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 IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS QoQRealAmountSum,
               (SELECT COALESCE(SUM(order_fund.real_amount),0) FROM personal, order_fund WHERE personal.deleted = 0 AND personal.id = order_fund.business_id AND order_fund.deleted = 0 AND business_type = 'PERSONAL_BUSINESS' AND IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS totalRealAmountSum,
               (SELECT COUNT(personal.id) FROM personal, order_fund WHERE personal.deleted = 0 AND personal.id = order_fund.business_id AND order_fund.real_amount IS NOT NULL AND order_fund.deleted = 0 AND business_type = 'PERSONAL_BUSINESS' AND personal.created BETWEEN #{dto.startTime} AND #{dto.endTime} AND IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS currentPeriodRealAmountOrderCount,
               (SELECT COUNT(personal.id) FROM personal, order_fund WHERE personal.deleted = 0 AND personal.id = order_fund.business_id AND order_fund.real_amount IS NOT NULL AND order_fund.deleted = 0 AND business_type = 'PERSONAL_BUSINESS' AND personal.created BETWEEN DATE_SUB(#{dto.startTime}, INTERVAL 1 YEAR) AND DATE_SUB(#{dto.endTime}, INTERVAL 1 YEAR) AND IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS YoYRealAmountOrderCount,
               (SELECT COUNT(personal.id) FROM personal, order_fund WHERE personal.deleted = 0 AND personal.id = order_fund.business_id AND order_fund.real_amount IS NOT NULL AND order_fund.deleted = 0 AND business_type = 'PERSONAL_BUSINESS' AND personal.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 IF(eva.evaluateAim = '总计',TRUE,evaluate_aim = eva.evaluateAim)) AS QoQRealAmountOrderCount
        FROM (
                 SELECT '咨询' AS evaluateAim
                 UNION ALL
                 SELECT '抵押'
                 UNION ALL
                 SELECT '按揭'
                 UNION ALL
                 SELECT '总计'
             ) AS eva
    </select>

    <sql id="personalEfficiencyDetailQuery">
        SELECT personal.id,
        personal.order_id,
        personal.location,
        (SELECT name FROM customer_company WHERE id = personal.clientele_id) AS customerName,
        (SELECT name FROM customer_company WHERE id = personal.clientele_sub_id) AS customerSubName,
        personal.agent,
        personal.bailorA AS bailor,
        target.acreage,
        target.price,
        target.amount,
        statement.created AS writeStatementDate,
        IF(report.created IS NOT NULL,report.created,letter.created) AS writeReportDate,
        JSON_EXTRACT(selling_ability, '$.sceneDateD') AS valuationDate,
        personal.purpose,
        fund.should_amount,
        fund.real_amount,
        (SELECT name FROM user WHERE user.id = personal.client_manager_id) AS clientManagerName,
        nodeInfo.currentNodeName,
        nodeInfo.handlerName
        FROM personal
        LEFT JOIN personal_target AS target ON personal.id = target.personal_id
        LEFT JOIN order_fund AS fund ON fund.business_id = personal.id AND fund.business_type = 'PERSONAL_BUSINESS'
        LEFT JOIN personal_production AS statement ON statement.target_id = target.id AND statement.production = 'STATEMENT' AND statement.deleted = 0
        LEFT JOIN personal_production AS report ON report.target_id = target.id AND report.production = 'FINAL' AND report.deleted = 0
        LEFT JOIN personal_production AS letter ON letter.target_id = target.id AND letter.production = 'LETTER' AND letter.deleted = 0
        LEFT JOIN (
        SELECT node.name AS currentNodeName,
        instance.business_id AS businessId,
        user.name AS handlerName
        FROM work_node AS node,
        work_flow_node_instance AS instance,
        work_task_record AS record,
        user
        WHERE node.id = instance.node_id
        AND instance.id = record.instance_id
        AND record.handler_id = user.id
        AND business_type = 'PERSONAL_BUSINESS'
        AND instance.state = 'PENDING'
        AND instance.deleted = 0
        ) AS nodeInfo ON personal.id = nodeInfo.businessId
        WHERE personal.deleted = 0
        <if test="dto != null and dto.evaluateAim != null and dto.evaluateAim != '总计'">
            AND evaluate_aim = #{dto.evaluateAim}
        </if>
        <if test="dto != null and dto.efficiencyType == 'order'">
            AND personal.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 == 'letter'">
            AND letter.created BETWEEN #{dto.startTime} AND #{dto.endTime}
        </if>
        <if test="dto != null and dto.efficiencyType == 'reportAndLetter'">
            AND (report.created BETWEEN #{dto.startTime} AND #{dto.endTime}
            OR letter.created BETWEEN #{dto.startTime} AND #{dto.endTime})
        </if>
        <if test="dto != null and dto.efficiencyType == 'should'">
            AND fund.created BETWEEN #{dto.startTime} AND #{dto.endTime}
            AND fund.should_amount IS NOT NULL
        </if>
        <if test="dto != null and dto.efficiencyType == 'real'">
            AND fund.created BETWEEN #{dto.startTime} AND #{dto.endTime}
            AND fund.real_amount IS NOT NULL
        </if>
        <if test="dto != null and dto.outwardStaff != null">
            AND personal.outward_staff = #{dto.outwardStaff}
        </if>
        <if test="dto != null and dto.inwardStaff != null">
            AND personal.inward_staff = #{dto.inwardStaff}
        </if>
    </sql>

    <!--获取效率值相关个贷订单列表-->
    <select id="getPersonalEfficiencyDetailVO" resultType="com.dayou.vo.PersonalEfficiencyDetailVO">
        <include refid="personalEfficiencyDetailQuery" />
    </select>

    <!--导出效率值相关个贷订单列表-->
    <select id="exportPersonalEfficiencyDetailVO" resultType="com.dayou.vo.PersonalEfficiencyDetailVO">
        <include refid="personalEfficiencyDetailQuery" />
    </select>

    <!--个贷外业效率值查询-->
    <select id="getPersonalOutwardEfficiencyVO" resultType="com.dayou.vo.PersonalOutwardEfficiencyVO">
       SELECT outward_staff,
              user.name AS userName,
              COUNT(personal.id) AS orderCount,
              COALESCE(SUM(scene.in_ring_building), 0) + COALESCE(SUM(scene.out_ring_building), 0) + COALESCE(SUM(scene.in_ring_dewlling), 0) + COALESCE(SUM(scene.out_ring_dewlling), 0) AS houseCount
       FROM personal
       LEFT JOIN personal_scene AS scene ON scene.personal_id = personal.id AND scene.deleted = 0
       LEFT JOIN user ON user.id = personal.outward_staff
       WHERE personal.deleted = 0
        AND outward_staff IS NOT NULL
        AND personal.created BETWEEN #{dto.startTime} AND #{dto.endTime}
       GROUP BY outward_staff
    </select>

    <!--个贷内业效率值查询-->
    <select id="getPersonalInwardEfficiencyVO" resultType="com.dayou.vo.PersonalInwardEfficiencyVO">
        SELECT
            main.inward_staff,
            USER.NAME AS userName,
            (
                SELECT
                    COUNT( production.id )
                FROM
                    personal,
                    personal_target AS target,
                    personal_production AS production
                WHERE
                    production.deleted = 0
                  AND inward_staff = main.inward_staff
                  AND production = 'STATEMENT'
                  AND personal.id = target.personal_id
                  AND target.id = production.target_id
            ) AS statementCount,
            (
                SELECT
                    COUNT( production.id )
                FROM
                    personal,
                    personal_target AS target,
                    personal_production AS production
                WHERE
                    production.deleted = 0
                  AND inward_staff = main.inward_staff
                  AND production = 'FINAL'
                  AND personal.id = target.personal_id
                  AND target.id = production.target_id
            ) AS reportCount,
            (
                SELECT
                    COUNT( production.id )
                FROM
                    personal,
                    personal_target AS target,
                    personal_production AS production
                WHERE
                    production.deleted = 0
                  AND inward_staff = main.inward_staff
                  AND production = 'LETTER'
                  AND personal.id = target.personal_id
                  AND target.id = production.target_id
            ) AS letterCount
        FROM
            personal AS main
                LEFT JOIN USER ON USER.id = main.inward_staff
        WHERE
            main.deleted = 0
          AND main.inward_staff IS NOT NULL
          AND main.created BETWEEN #{dto.startTime} AND #{dto.endTime}
        GROUP BY
            main.inward_staff
    </select>
</mapper>