<?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.tofly.qualitycontrol.mapper.RectifyOrderMapper">

    <resultMap id="rectifyOrderMap" type="com.tofly.qualitycontrol.entity.RectifyOrder">
        <id property="id" column="ID"/>
        <result property="rectifyNumber" column="RECTIFY_NUMBER"/>
        <result property="certificationNumber" column="CERTIFICATION_NUMBER"/>
        <result property="clientNumber" column="CLIENT_NUMBER"/>
        <result property="clientName" column="CLIENT_NAME"/>
        <result property="clientPhone" column="CLIENT_PHONE"/>
        <result property="clientAddress" column="CLIENT_ADDRESS"/>
        <result property="meterType" column="METER_TYPE"/>
        <result property="waterCategory" column="WATER_CATEGORY"/>
        <result property="certificationDept" column="CERTIFICATION_DEPT"/>
        <result property="certificationPerson" column="CERTIFICATION_PERSON"/>
        <result property="certificationPersonId" column="CERTIFICATION_PERSON_ID"/>
        <result property="resultTimeLimit" column="RESULT_TIME_LIMIT"/>
        <result property="rectifyType" column="RECTIFY_TYPE"/>
        <result property="rectifyDesc" column="RECTIFY_DESC"/>
        <result property="increaseVolume" column="INCREASE_VOLUME"/>
        <result property="increaseProfit" column="INCREASE_PROFIT"/>
        <result property="rectifyStatus" column="RECTIFY_STATUS"/>
        <result property="auditStatus" column="AUDIT_STATUS"/>
        <result property="verifyStatus" column="VERIFY_STATUS"/>
        <result property="rectifyDept" column="RECTIFY_DEPT"/>
        <result property="rectifyPerson" column="RECTIFY_PERSON"/>
        <result property="rectifyPersonId" column="RECTIFY_PERSON_ID"/>
        <result property="rectifyTime" column="RECTIFY_TIME"/>
        <result property="auditDept" column="AUDIT_DEPT"/>
        <result property="auditPerson" column="AUDIT_PERSON"/>
        <result property="auditPersonId" column="AUDIT_PERSON_ID"/>
        <result property="auditTime" column="AUDIT_TIME"/>
        <result property="verifyDept" column="VERIFY_DEPT"/>
        <result property="verifyPerson" column="VERIFY_PERSON"/>
        <result property="verifyPersonId" column="VERIFY_PERSON_ID"/>
        <result property="verifyTime" column="VERIFY_TIME"/>
        <result property="rectifyResultDesc" column="RECTIFY_RESULT_DESC"/>
        <result property="rectifyAuditDesc" column="RECTIFY_AUDIT_DESC"/>
        <result property="rectifyVerifyDesc" column="RECTIFY_VERIFY_DESC"/>
        <result property="verifyTimeLimit" column="VERIFY_TIME_LIMIT"/>
        <result property="dispatchPersonId" column="DISPATCH_PERSON_ID"/>
        <result property="issueTime" column="ISSUE_TIME"/>
        <result property="issueTimeLimit" column="ISSUE_TIME_LIMIT"/>
        <result property="issueDept" column="ISSUE_DEPT"/>
        <result property="issuePerson" column="ISSUE_PERSON"/>
        <result property="issuePersonId" column="ISSUE_PERSON_ID"/>
        <result property="issueStatus" column="ISSUE_STATUS"/>
        <result property="issueFileIds" column="ISSUE_FILE_IDS"/>
        <result property="rectifyIssueDesc" column="RECTIFY_ISSUE_DESC"/>
        <result property="rectifyFileIds" column="RECTIFY_FILE_IDS"/>
        <result property="validateDept" column="VALIDATE_DEPT"/>
        <result property="validatePerson" column="VALIDATE_PERSON"/>
        <result property="validatePersonId" column="VALIDATE_PERSON_ID"/>
        <result property="validateTime" column="VALIDATE_TIME"/>
        <result property="validateStatus" column="VALIDATE_STATUS"/>
        <result property="rectifyValidateDesc" column="RECTIFY_VALIDATE_DESC"/>
        <result property="issueExceptionStatus" column="ISSUE_EXCEPTION_STATUS"/>
        <result property="rectifyExceptionStatus" column="RECTIFY_EXCEPTION_STATUS"/>
        <result property="auditExceptionStatus" column="AUDIT_EXCEPTION_STATUS"/>
        <result property="verifyExceptionStatus" column="VERIFY_EXCEPTION_STATUS"/>
        <result property="validateExceptionStatus" column="VALIDATE_EXCEPTION_STATUS"/>
        <result property="exceptionCause" column="EXCEPTION_CAUSE"/>
        <result property="certificationOrderId" column="CERTIFICATION_ORDER_ID"/>
        <result property="certificationTime" column="CERTIFICATION_TIME"/>
        <result property="rectifyVerifyOk" column="RECTIFY_VERIFY_OK"/>
        <result property="problemLevel" column="PROBLEM_LEVEL"/>
        <result property="parentId" column="PARENT_ID"/>
        <result property="ysxzBm" column="YSXZ_BM"/>
        <result property="ysxzMc" column="YSXZ_MC"/>
    </resultMap>
    <select id="getTypeName" resultType="String">
        SELECT GDLX_MC
        FROM TF_YWPN_GDGLTYPEBM_W
        WHERE ID = #{id}
    </select>
    <select id="getBusinessMonitorList" resultType="com.tofly.qualitycontrol.vo.BusinessMonitorVO_L">
        SELECT
        a.ID,
        a.CERTIFICATION_NUMBER,
        a.CLIENT_NUMBER,
        a.CLIENT_NAME,
        a.CLIENT_PHONE,
        a.CLIENT_ADDRESS,
        a.METER_TYPE,
        a.WATER_CATEGORY,
        CERTIFICATION_PROJECT,
        CURRENT_PROCESS,
        ORDER_EXCEPTION_STATUS

        FROM TF_YWPN_ZLRZ_W a
        LEFT JOIN TF_YWPN_ZLZG_W b ON a.CERTIFICATION_NUMBER = b.CERTIFICATION_NUMBER

        <where>
            <if test="currentProcess != null and currentProcess != '' and currentProcess.equals('-1')">
                AND CURRENT_PROCESS IS NULL
            </if>
            <if test="currentProcess != null and currentProcess != '' and !currentProcess.equals('-1')">
                AND CURRENT_PROCESS = #{currentProcess}
            </if>
            <if test="orderExceptionStatus != null">
                AND ORDER_EXCEPTION_STATUS = #{orderExceptionStatus}
            </if>
            <if test="number != null and number != ''">
                AND (a.CERTIFICATION_NUMBER LIKE CONCAT(CONCAT('%',#{number}),'%')
                OR a.CLIENT_NUMBER LIKE CONCAT(CONCAT('%',#{number}),'%'))
            </if>
            <if test="startDate != null  and endDate != null ">
                <![CDATA[    AND a.DISPATCH_TIME >= #{startDate}
                AND a.DISPATCH_TIME <= #{endDate} ]]>
            </if>
        </where>

        ORDER BY a.CERTIFICATION_NUMBER DESC
    </select>

    <select id="getIssueSummary" resultType="com.tofly.qualitycontrol.vo.ReportSummaryVo">
        SELECT CERTIFICATION_PROJECT,CERTIFICATION_PROJECT_NAME,SUM(REPORT_SUM) as REPORT_SUM,SUM(DEALT_WITH_COUNT) as
        DEALT_WITH_COUNT,SUM(DONE_COUNT) as DONE_COUNT,SUM(WAIT_TIMEOUT_COUNT) as WAIT_TIMEOUT_COUNT,SUM(OVERTIME_COUNT)
        as OVERTIME_COUNT,SUM(BH_COUNT) as BH_COUNT
        FROM(
        SELECT CERTIFICATION_PROJECT,t.GDLX_MC as CERTIFICATION_PROJECT_NAME, count(z.id) as REPORT_SUM,
        SUM(CASE when (select count(*) from TF_YWPN_MASSAUDIT_W m1 where CODE = z.CERTIFICATION_NUMBER and PROCESS = 0
        and
        AUDIT_STATE = 0 and AUDIT_USER = #{userId} and num = (select min(num) from TF_YWPN_MASSAUDIT_W m2 where m2.code
        =
        m1.code and m2.process = 0 and audit_state = 0)) > 0 then 1 WHEN (ISSUE_STATUS is NULL OR ISSUE_STATUS = 0) AND
        CURRENT_PROCESS = 1 and (select count(*) from TF_YWPN_MASSAUDIT_W where CODE = z.CERTIFICATION_NUMBER and
        PROCESS = 0) = 0 THEN 1 ELSE
        0 END) as
        DEALT_WITH_COUNT,
        SUM(CASE when (select count(*) from TF_YWPN_MASSAUDIT_W where CODE = z.CERTIFICATION_NUMBER and PROCESS = 0 and
        AUDIT_STATE = 0 and AUDIT_USER = #{userId}) > 0 then 1 WHEN (ISSUE_STATUS is NULL OR ISSUE_STATUS = 0) AND
        CURRENT_PROCESS = 1 and (select count(*) from TF_YWPN_MASSAUDIT_W where CODE = z.CERTIFICATION_NUMBER and
        PROCESS = 0) = 0 THEN 0 ELSE 1 END) as
        DONE_COUNT,
        SUM(case when (
        <![CDATA[ (ceil((ISSUE_TIME_LIMIT-sysdate)*24*60)/ceil((ISSUE_TIME_LIMIT- z.CERTIFICATION_TIME)*24*60) )*100 <  #{ratio}
                    AND CURRENT_PROCESS = 1 AND sysdate < ISSUE_TIME_LIMIT ) then 1 else 0 end) WAIT_TIMEOUT_COUNT,]]>
        SUM(CASE WHEN (CURRENT_PROCESS = 1 AND sysdate > ISSUE_TIME_LIMIT) OR ISSUE_EXCEPTION_STATUS =1 OR
        ISSUE_EXCEPTION_STATUS=3 THEN 1 else 0 end) OVERTIME_COUNT,
        (SELECT COUNT(distinct r.CERTIFICATION_NUMBER) ct FROM TF_YWPN_ZJGLFLOW_W r WHERE r.CERTIFICATION_NUMBER =
        z.CERTIFICATION_NUMBER AND r.CURRENT_PROCESS = 2 ) as BH_COUNT
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        LEFT JOIN TF_YWPN_GDGLTYPEBM_W t ON (z.CERTIFICATION_PROJECT = t.ID)
        <where>
            AND CURRENT_PROCESS > 0
            <if test="userId != null">
                and (z.ISSUE_PERSON_ID = #{userId} or
                <include refid="queryAuditUser">
                    <property name="process"
                              value="0"/>
                </include>
                )
            </if>
            <if test="departmentId != null">
                AND z.ISSUE_DEPT = #{departmentId}
            </if>
            <include refid="com.tofly.qualitycontrol.mapper.CertificationOrderMapper.summaryWhere"/>
        </where>
        GROUP BY CERTIFICATION_PROJECT ,z.CERTIFICATION_NUMBER,t.GDLX_MC) tb
        GROUP BY CERTIFICATION_PROJECT,CERTIFICATION_PROJECT_NAME
        ORDER BY CERTIFICATION_PROJECT
    </select>

    <select id="getRectifySummary" resultType="com.tofly.qualitycontrol.vo.ReportSummaryVo">
        SELECT CERTIFICATION_PROJECT,CERTIFICATION_PROJECT_NAME,SUM(REPORT_SUM) as REPORT_SUM,SUM(DEALT_WITH_COUNT) as
        DEALT_WITH_COUNT,SUM(DONE_COUNT) as DONE_COUNT,
        SUM(WAIT_TIMEOUT_COUNT) as WAIT_TIMEOUT_COUNT,SUM(OVERTIME_COUNT) as OVERTIME_COUNT,SUM(ZP_COUNT) as
        ZP_COUNT,SUM(BH_COUNT) as BH_COUNT
        FROM (
        SELECT CERTIFICATION_PROJECT,t.GDLX_MC as CERTIFICATION_PROJECT_NAME,
        count(z.id) as REPORT_SUM,
        SUM(CASE WHEN (RECTIFY_STATUS is NULL OR RECTIFY_STATUS = 0) AND CURRENT_PROCESS = 2 THEN 1 ELSE 0 END) as
        DEALT_WITH_COUNT,
        SUM(CASE WHEN (RECTIFY_STATUS is NULL OR RECTIFY_STATUS = 0) AND CURRENT_PROCESS = 2 THEN 0 ELSE 1 END) as
        DONE_COUNT,
        SUM(case when
        <![CDATA[
                     ((ceil((g.RESULT_TIME_LIMIT-sysdate)*24*60)/ceil((g.RESULT_TIME_LIMIT- g.ISSUE_TIME)*24*60) )*100  <  #{ratio}
        AND CURRENT_PROCESS = 2 AND sysdate < RESULT_TIME_LIMIT  ) then 1 else 0 end) WAIT_TIMEOUT_COUNT,]]>
        SUM(CASE WHEN (CURRENT_PROCESS = 2 AND sysdate > g.RESULT_TIME_LIMIT) OR RECTIFY_EXCEPTION_STATUS =1 OR
        RECTIFY_EXCEPTION_STATUS=3 THEN 1 else 0 end) OVERTIME_COUNT,
        (SELECT COUNT(distinct r.GDBH) ct FROM TF_YWPN_ZLTRANSFER_W r WHERE r.GDBH = z.CERTIFICATION_NUMBER AND r.LCJD =
        2 ) as ZP_COUNT,
        (SELECT COUNT(distinct r.CERTIFICATION_NUMBER) ct FROM TF_YWPN_ZJGLFLOW_W r WHERE r.CERTIFICATION_NUMBER =
        z.CERTIFICATION_NUMBER AND r.CURRENT_PROCESS = 3 ) as BH_COUNT
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        LEFT JOIN TF_YWPN_GDGLTYPEBM_W t ON (z.CERTIFICATION_PROJECT = t.ID)
        <where>
            AND CURRENT_PROCESS > 1
            <if test="userId != null">
                and z.RECTIFY_PERSON_ID = #{userId}
            </if>
            <if test="departmentId != null">
                AND g.RECTIFY_DEPT = #{departmentId}
            </if>
            <include refid="com.tofly.qualitycontrol.mapper.CertificationOrderMapper.summaryWhere"></include>
        </where>
        GROUP BY CERTIFICATION_PROJECT,z.CERTIFICATION_NUMBER ,t.GDLX_MC) tb
        GROUP BY CERTIFICATION_PROJECT,CERTIFICATION_PROJECT_NAME
        ORDER BY CERTIFICATION_PROJECT
    </select>
    <select id="getAuditSummary" resultType="com.tofly.qualitycontrol.vo.ReportSummaryVo">
        SELECT CERTIFICATION_PROJECT,CERTIFICATION_PROJECT_NAME,SUM(REPORT_SUM) as REPORT_SUM,SUM(DEALT_WITH_COUNT) as
        DEALT_WITH_COUNT,SUM(DONE_COUNT) as DONE_COUNT,SUM(BH_COUNT) as BH_COUNT
        FROM (
        SELECT CERTIFICATION_PROJECT,t.GDLX_MC as CERTIFICATION_PROJECT_NAME,
        count(z.id) as REPORT_SUM,
        SUM(CASE WHEN (AUDIT_STATUS is NULL OR AUDIT_STATUS = 0) AND CURRENT_PROCESS = 3 THEN 1 ELSE 0 END) as
        DEALT_WITH_COUNT,
        SUM(CASE WHEN (AUDIT_STATUS is NULL OR AUDIT_STATUS = 0) AND CURRENT_PROCESS = 3 THEN 0 ELSE 1 END) as
        DONE_COUNT,
        (SELECT COUNT(distinct r.CERTIFICATION_NUMBER) ct FROM TF_YWPN_ZJGLFLOW_W r WHERE r.CERTIFICATION_NUMBER =
        z.CERTIFICATION_NUMBER AND r.CURRENT_PROCESS = 4 ) as BH_COUNT
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        LEFT JOIN TF_YWPN_GDGLTYPEBM_W t ON (z.CERTIFICATION_PROJECT = t.ID)
        <where>
            AND CURRENT_PROCESS > 2
            <if test="userId != null">
                and g.AUDIT_PERSON_ID = #{userId}
            </if>
            <if test="departmentId != null">
                AND g.AUDIT_DEPT = #{departmentId}
            </if>
            <include refid="com.tofly.qualitycontrol.mapper.CertificationOrderMapper.summaryWhere"/>
        </where>
        GROUP BY CERTIFICATION_PROJECT,z.CERTIFICATION_NUMBER ,t.GDLX_MC) tb
        GROUP BY CERTIFICATION_PROJECT,CERTIFICATION_PROJECT_NAME
        ORDER BY CERTIFICATION_PROJECT
    </select>

    <select id="getVerifySummary" resultType="com.tofly.qualitycontrol.vo.ReportSummaryVo">
        SELECT CERTIFICATION_PROJECT,CERTIFICATION_PROJECT_NAME,SUM(REPORT_SUM) as REPORT_SUM,SUM(DEALT_WITH_COUNT) as
        DEALT_WITH_COUNT,SUM(DONE_COUNT) as DONE_COUNT,
        SUM(WAIT_TIMEOUT_COUNT) as WAIT_TIMEOUT_COUNT,SUM(OVERTIME_COUNT) as OVERTIME_COUNT,SUM(ZP_COUNT) as
        ZP_COUNT,SUM(BH_COUNT) as BH_COUNT
        FROM (
        SELECT CERTIFICATION_PROJECT,t.GDLX_MC as CERTIFICATION_PROJECT_NAME,
        count(z.id) as REPORT_SUM,
        SUM(CASE WHEN (VERIFY_STATUS is NULL OR VERIFY_STATUS = 0) AND CURRENT_PROCESS = 4 THEN 1 ELSE 0 END) as
        DEALT_WITH_COUNT,
        SUM(CASE WHEN (VERIFY_STATUS is NULL OR VERIFY_STATUS = 0) AND CURRENT_PROCESS = 4 THEN 0 ELSE 1 END) as
        DONE_COUNT,
        SUM(case when
        <![CDATA[
                     ((ceil((g.VERIFY_TIME_LIMIT-sysdate)*24*60)/ceil((g.VERIFY_TIME_LIMIT- g.ISSUE_TIME)*24*60) )*100  <  #{ratio}
        AND CURRENT_PROCESS = 4 AND sysdate < VERIFY_TIME_LIMIT  ) then 1 else 0 end) WAIT_TIMEOUT_COUNT,]]>
        SUM(CASE WHEN (CURRENT_PROCESS = 4 AND sysdate > g.VERIFY_TIME_LIMIT) OR VERIFY_EXCEPTION_STATUS =1 OR
        VERIFY_EXCEPTION_STATUS=3 THEN 1 else 0 end) OVERTIME_COUNT,
        (SELECT COUNT(distinct r.GDBH) ct FROM TF_YWPN_ZLTRANSFER_W r WHERE r.GDBH = z.CERTIFICATION_NUMBER AND r.LCJD =
        4 ) as ZP_COUNT,
        (SELECT COUNT(distinct r.CERTIFICATION_NUMBER) ct FROM TF_YWPN_ZJGLFLOW_W r WHERE r.CERTIFICATION_NUMBER =
        z.CERTIFICATION_NUMBER AND r.CURRENT_PROCESS = 5 ) as BH_COUNT,
        SUM(decode(VALIDATE_STATUS,3,1,0)) as END_COUNT
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        LEFT JOIN TF_YWPN_GDGLTYPEBM_W t ON (z.CERTIFICATION_PROJECT = t.ID)
        <where>
            AND CURRENT_PROCESS > 3
            <if test="userId != null">
                and VERIFY_PERSON_ID = #{userId}
            </if>
            <if test="departmentId != null">
                AND VERIFY_DEPT = #{departmentId}
            </if>
            <include refid="com.tofly.qualitycontrol.mapper.CertificationOrderMapper.summaryWhere"/>
        </where>
        GROUP BY CERTIFICATION_PROJECT,z.CERTIFICATION_NUMBER ,t.GDLX_MC) tb
        GROUP BY CERTIFICATION_PROJECT,CERTIFICATION_PROJECT_NAME
        ORDER BY CERTIFICATION_PROJECT
    </select>

    <select id="getValidateSummary" resultType="com.tofly.qualitycontrol.vo.ReportSummaryVo">
        SELECT CERTIFICATION_PROJECT,t.GDLX_MC as CERTIFICATION_PROJECT_NAME,
        count(z.id) as REPORT_SUM,
        SUM(CASE when (select count(*) from TF_YWPN_MASSAUDIT_W m1 where CODE = z.CERTIFICATION_NUMBER and PROCESS = 1
        and
        AUDIT_STATE = 0 and AUDIT_USER = #{userId} and num = (select min(num) from TF_YWPN_MASSAUDIT_W m2 where m2.code
        =
        m1.code and m2.process = 1 and audit_state = 0)) > 0 then 1 WHEN (VALIDATE_STATUS is NULL OR VALIDATE_STATUS = 0
        )
        AND CURRENT_PROCESS = 5 and (select count(*) from TF_YWPN_MASSAUDIT_W where CODE = z.CERTIFICATION_NUMBER and
        PROCESS = 1) = 0 THEN 1 ELSE 0 END) as
        DEALT_WITH_COUNT,
        SUM(CASE when (select count(*) from TF_YWPN_MASSAUDIT_W where CODE = z.CERTIFICATION_NUMBER and PROCESS = 1 and
        AUDIT_STATE = 0 and AUDIT_USER = #{userId}) > 0 then 0 WHEN (VALIDATE_STATUS is NULL OR VALIDATE_STATUS = 0) AND
        CURRENT_PROCESS = 5 and (select count(*) from TF_YWPN_MASSAUDIT_W where CODE = z.CERTIFICATION_NUMBER and
        PROCESS = 1) = 0 THEN 0 ELSE 1 END) as
        DONE_COUNT,

        SUM(decode(VALIDATE_STATUS,3,1,0)) as END_COUNT
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        LEFT JOIN TF_YWPN_GDGLTYPEBM_W t ON (z.CERTIFICATION_PROJECT = t.ID)
        <where>
            AND CURRENT_PROCESS > 4
            <if test="userId != null">
                and (VALIDATE_PERSON_ID = #{userId} or
                <include refid="queryAuditUser">
                    <property name="process"
                              value="1"/>
                </include>
                )
            </if>
            <if test="departmentId != null">
                AND VALIDATE_DEPT = #{departmentId}
            </if>
            <include refid="com.tofly.qualitycontrol.mapper.CertificationOrderMapper.summaryWhere"/>
        </where>
        GROUP BY CERTIFICATION_PROJECT,t.GDLX_MC
        ORDER BY CERTIFICATION_PROJECT
    </select>

    <sql id="queryAuditUser">
        (select AUDIT_USER
         from TF_YWPN_MASSAUDIT_W
         where CODE = z.CERTIFICATION_NUMBER
           and PROCESS = ${process}
           and AUDIT_USER = #{userId}
           and rownum = 1)
        is not null
    </sql>

    <sql id="columnListSql">
        SELECT distinct g.ID,
                        z.ID as CERTIFICATION_ORDER_ID,
                        z.CERTIFICATION_NUMBER,
                        z.CLIENT_NUMBER,
                        z.CLIENT_NAME,
                        z.CLIENT_PHONE,
                        z.CLIENT_ADDRESS,
                        z.METER_TYPE,
                        z.WATER_CATEGORY,
                        g.RECTIFY_NUMBER,
                        z.CERTIFICATION_PROJECT,
                        CERTIFICATION_RESULT,
                        z.CERTIFICATION_DEPT,
                        z.CERTIFICATION_PERSON,
                        z.CERTIFICATION_PERSON_ID,
                        z.CERTIFICATION_TIME,
                        z.RECTIFY_DEPT,
                        z.RECTIFY_PERSON,
                        z.RECTIFY_PERSON_ID,
                        g.RECTIFY_TIME,
                        g.AUDIT_DEPT,
                        g.AUDIT_PERSON,
                        g.AUDIT_PERSON_ID,
                        g.AUDIT_TIME,
                        g.VERIFY_DEPT,
                        g.VERIFY_PERSON,
                        g.VERIFY_PERSON_ID,
                        g.VERIFY_TIME,
                        g.ISSUE_DEPT,
                        g.ISSUE_PERSON,
                        g.ISSUE_PERSON_ID,
                        g.ISSUE_TIME,
                        g.ISSUE_STATUS,
                        g.VALIDATE_DEPT,
                        g.VALIDATE_PERSON,
                        g.VALIDATE_PERSON_ID,
                        g.VALIDATE_TIME,
                        z.DISPATCH_TIME,
                        z.CURRENT_PROCESS,
                        g.RECTIFY_STATUS,
                        g.AUDIT_STATUS,
                        g.VERIFY_STATUS,
                        g.VALIDATE_STATUS
    </sql>
    <select id="loadRectifyIssueList" resultType="com.tofly.qualitycontrol.vo.RectifyOrderListVo">
        <include refid="columnListSql"></include>
        , t.GDLX_MC as CERTIFICATION_PROJECT_NAME
        ,(CASE WHEN z.CURRENT_PROCESS = 1 AND sysdate > g.ISSUE_TIME_LIMIT OR ISSUE_EXCEPTION_STATUS =1 OR
        ISSUE_EXCEPTION_STATUS=3 THEN 1
        WHEN
        <![CDATA[ (ceil((ISSUE_TIME_LIMIT-sysdate)*24*60)/ceil((ISSUE_TIME_LIMIT- z.CERTIFICATION_TIME)*24*60) )*100 < #{ratio} ]]>
        AND CURRENT_PROCESS = 1 THEN 2
        else 0 end) as IS_CS
        ,(SELECT COUNT(ID) FROM TF_YWPN_ZJGLFLOW_W w WHERE w.CERTIFICATION_NUMBER = z.CERTIFICATION_NUMBER AND
        w.CURRENT_PROCESS = 2 ) as IS_BH
        ,ISSUE_STATUS as STATUS
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        LEFT JOIN TF_YWPN_GDGLTYPEBM_W t ON (z.CERTIFICATION_PROJECT = t.ID)
        <where>
            AND CURRENT_PROCESS > 0
            <if test="userId != null">
                and (z.ISSUE_PERSON_ID = #{userId} or
                <include refid="queryAuditUser">
                    <property name="process"
                              value="0"/>
                </include>
                )
            </if>
            <if test="departmentId != null">
                AND z.ISSUE_DEPT = #{departmentId}
            </if>
            <include refid="com.tofly.qualitycontrol.mapper.CertificationOrderMapper.orderListWhereSql"></include>
            <if test=" dto.state != null">
                <choose>
                    <when test="dto.state == 1 ">
                        AND (
                        (z.CURRENT_PROCESS = 1 and (select count(*)
                        from TF_YWPN_MASSAUDIT_W
                        where CODE = z.CERTIFICATION_NUMBER
                        and PROCESS = 0) = 0) or
                        (
                        (
                        select count(*)
                        from TF_YWPN_MASSAUDIT_W m1
                        where CODE = z.CERTIFICATION_NUMBER
                        and PROCESS = 0
                        and AUDIT_STATE = 0
                        and AUDIT_USER = #{userId}
                        and num = (select min(num) from TF_YWPN_MASSAUDIT_W m2 where m2.code
                        =
                        m1.code and m2.process = 0 and audit_state = 0)
                        ) > 0

                        )
                        )
                    </when>
                    <when test="dto.state == 2 ">
                        AND (g.ISSUE_STATUS = 4 or (select count(*) from TF_YWPN_MASSAUDIT_W where
                        CODE = z.CERTIFICATION_NUMBER and PROCESS = 0 and
                        AUDIT_STATE = 0 and AUDIT_USER = #{userId}) = 0 and (select count(*) from TF_YWPN_MASSAUDIT_W
                        where CODE = z.CERTIFICATION_NUMBER and
                        PROCESS = 0) > 0)
                    </when>
                    <when test="dto.state == 3 ">
                        AND z.VALIDATE_STATUS = 3
                    </when>
                    <when test="dto.state == 4 ">
                        <![CDATA[
                        AND (ceil((g.ISSUE_TIME_LIMIT-sysdate)*24*60)/ceil((g.ISSUE_TIME_LIMIT-z.CERTIFICATION_TIME)*24*60) )*100  <#{ratio}
                        AND z.CURRENT_PROCESS = 1  AND sysdate < g.ISSUE_TIME_LIMIT  ]]>
                    </when>
                    <when test="dto.state == 5 ">
                        AND ((CURRENT_PROCESS = 1 AND sysdate > ISSUE_TIME_LIMIT) OR ISSUE_EXCEPTION_STATUS =1 OR
                        ISSUE_EXCEPTION_STATUS=3)

                    </when>
                    <when test="dto.state == 6 ">
                        AND (ISSUE_EXCEPTION_STATUS =2 OR ISSUE_EXCEPTION_STATUS = 3)
                    </when>
                </choose>
            </if>
        </where>
        ORDER BY z.DISPATCH_TIME DESC
    </select>

    <select id="loadRectifyResultList" resultType="com.tofly.qualitycontrol.vo.RectifyOrderListVo">
        select * from (
        <include refid="columnListSql"></include>
        , t.GDLX_MC as CERTIFICATION_PROJECT_NAME
        ,(CASE WHEN z.CURRENT_PROCESS = 2 AND sysdate > g.RESULT_TIME_LIMIT OR RECTIFY_EXCEPTION_STATUS =1 OR
        RECTIFY_EXCEPTION_STATUS=3 THEN 1
        WHEN <![CDATA[
                     (ceil((g.RESULT_TIME_LIMIT-sysdate)*24*60)/ceil((g.RESULT_TIME_LIMIT- g.ISSUE_TIME)*24*60) )*100  <  #{ratio} ]]>
        AND CURRENT_PROCESS = 2 THEN 2
        else 0 end) as IS_CS
        ,(SELECT COUNT(1) FROM TF_YWPN_ZJGLFLOW_W w WHERE w.CERTIFICATION_NUMBER = z.CERTIFICATION_NUMBER AND
        w.CURRENT_PROCESS = 3 ) as IS_BH
        ,RECTIFY_STATUS as STATUS
        ,(SELECT COUNT(1) FROM TF_YWPN_ZLTRANSFER_W f WHERE f.GDBH=z.CERTIFICATION_NUMBER AND f.LCJD=2 ) IS_ZP
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        LEFT JOIN TF_YWPN_GDGLTYPEBM_W t ON (z.CERTIFICATION_PROJECT = t.ID)
        <where>
            AND CURRENT_PROCESS > 1
            <if test="userId != null">
                and z.RECTIFY_PERSON_ID = #{userId}
            </if>
            <if test="departmentId != null">
                AND z.RECTIFY_DEPT = #{departmentId}
            </if>
            <include refid="com.tofly.qualitycontrol.mapper.CertificationOrderMapper.orderListWhereSql"></include>
            <if test="dto.state != null">
                <choose>
                    <when test="dto.state == 1 ">
                        AND z.CURRENT_PROCESS = 2
                    </when>
                    <when test="dto.state == 2 ">
                        AND g.RECTIFY_STATUS = 4
                    </when>
                    <when test="dto.state == 3 ">
                        AND g.VALIDATE_STATUS = 3
                    </when>
                    <when test="dto.state == 4">
                        <![CDATA[
                        AND (ceil((g.RESULT_TIME_LIMIT-sysdate)*24*60)/ceil((g.RESULT_TIME_LIMIT-g.ISSUE_TIME)*24*60) )*100  <#{ratio}
                        AND z.CURRENT_PROCESS = 2  AND sysdate < g.RESULT_TIME_LIMIT  ]]>
                    </when>
                    <when test="dto.state == 5">
                        AND ((CURRENT_PROCESS = 2 AND sysdate > g.RESULT_TIME_LIMIT) OR RECTIFY_EXCEPTION_STATUS =1 OR
                        RECTIFY_EXCEPTION_STATUS=3)
                    </when>
                    <when test="dto.state == 6">
                        AND (RECTIFY_EXCEPTION_STATUS = 2 OR RECTIFY_EXCEPTION_STATUS = 3)
                    </when>

                </choose>
            </if>
        </where>
        ORDER BY z.DISPATCH_TIME DESC ) tab
        <where>
            <if test="dto.state != null">
                <choose>
                    <when test="dto.state == 7">
                        AND IS_ZP >0
                    </when>
                </choose>
            </if>
        </where>
    </select>

    <select id="loadRectifyAuditList" resultType="com.tofly.qualitycontrol.vo.RectifyOrderListVo">
        <include refid="columnListSql"></include>
        , t.GDLX_MC as CERTIFICATION_PROJECT_NAME
        ,(SELECT COUNT(ID) FROM TF_YWPN_ZJGLFLOW_W w WHERE w.CERTIFICATION_NUMBER = z.CERTIFICATION_NUMBER AND
        w.CURRENT_PROCESS = 4 ) as IS_BH
        ,AUDIT_STATUS as STATUS
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        LEFT JOIN TF_YWPN_GDGLTYPEBM_W t ON (z.CERTIFICATION_PROJECT = t.ID)
        <where>
            AND CURRENT_PROCESS > 2
            <if test="userId != null">
                and g.AUDIT_PERSON_ID = #{userId}
            </if>
            <if test="departmentId != null">
                AND g.AUDIT_DEPT = #{departmentId}
            </if>
            <include refid="com.tofly.qualitycontrol.mapper.CertificationOrderMapper.orderListWhereSql"></include>
            <if test="dto.state != null">
                <choose>
                    <when test="dto.state == 1 ">
                        AND z.CURRENT_PROCESS = 3
                    </when>
                    <when test="dto.state == 2 ">
                        AND g.AUDIT_STATUS = 4
                    </when>
                    <when test="dto.state == 3 ">
                        AND g.VALIDATE_STATUS = 3
                    </when>
                    <when test="dto.state == 6">
                        AND (AUDIT_EXCEPTION_STATUS = 2 OR AUDIT_EXCEPTION_STATUS = 3)
                    </when>
                </choose>
            </if>
        </where>
        ORDER BY z.DISPATCH_TIME DESC
    </select>

    <select id="loadRectifyVerifyList" resultType="com.tofly.qualitycontrol.vo.RectifyOrderListVo">
        <include refid="columnListSql"/>
        , t.GDLX_MC as CERTIFICATION_PROJECT_NAME
        ,(CASE WHEN z.CURRENT_PROCESS = 4 AND sysdate > g.VERIFY_TIME_LIMIT OR VERIFY_EXCEPTION_STATUS =1 OR
        VERIFY_EXCEPTION_STATUS=3 THEN 1
        WHEN <![CDATA[
                     (ceil((g.VERIFY_TIME_LIMIT-sysdate)*24*60)/ceil((g.VERIFY_TIME_LIMIT- g.ISSUE_TIME)*24*60) )*100  <  #{ratio} ]]>
        AND CURRENT_PROCESS = 4 THEN 2
        else 0 end) as IS_CS
        ,(SELECT COUNT(ID) FROM TF_YWPN_ZJGLFLOW_W w WHERE w.CERTIFICATION_NUMBER = z.CERTIFICATION_NUMBER AND
        w.CURRENT_PROCESS = 5 ) as IS_BH
        ,VERIFY_STATUS as STATUS
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        LEFT JOIN TF_YWPN_GDGLTYPEBM_W t ON (z.CERTIFICATION_PROJECT = t.ID)
        <where>
            AND CURRENT_PROCESS > 3
            <if test="userId != null">
                and VERIFY_PERSON_ID = #{userId}
            </if>
            <if test="departmentId != null">
                AND VERIFY_DEPT = #{departmentId}
            </if>
            <include refid="com.tofly.qualitycontrol.mapper.CertificationOrderMapper.orderListWhereSql"/>
            <if test="dto.state != null">
                <choose>
                    <when test="dto.state == 1 ">
                        AND z.CURRENT_PROCESS = 4
                    </when>
                    <when test="dto.state == 2 ">
                        AND g.VERIFY_STATUS = 4
                    </when>
                    <when test="dto.state == 3 ">
                        AND g.VALIDATE_STATUS = 3
                    </when>
                    <when test="dto.state == 4">
                        <![CDATA[
                        AND (ceil((g.VERIFY_TIME_LIMIT-sysdate)*24*60)/ceil((g.VERIFY_TIME_LIMIT-g.AUDIT_TIME)*24*60) )*100  <#{ratio}
                        AND z.CURRENT_PROCESS = 4  AND sysdate < g.VERIFY_TIME_LIMIT  ]]>
                    </when>
                    <when test="dto.state == 5">
                        AND ((CURRENT_PROCESS = 4 AND sysdate > g.VERIFY_TIME_LIMIT) OR VERIFY_EXCEPTION_STATUS =1 OR
                        VERIFY_EXCEPTION_STATUS=3 )
                    </when>
                    <when test="dto.state == 6">
                        AND (VERIFY_EXCEPTION_STATUS = 2 OR VERIFY_EXCEPTION_STATUS = 3)
                    </when>
                </choose>
            </if>
        </where>
        ORDER BY z.DISPATCH_TIME DESC
    </select>

    <select id="loadRectifyValidateList" resultType="com.tofly.qualitycontrol.vo.RectifyOrderListVo">
        <include refid="columnListSql"/>
        , t.GDLX_MC as CERTIFICATION_PROJECT_NAME
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        LEFT JOIN TF_YWPN_GDGLTYPEBM_W t ON (z.CERTIFICATION_PROJECT = t.ID)
        <where>
            AND CURRENT_PROCESS > 4
            <if test="userId != null">
                and (VALIDATE_PERSON_ID = #{userId} or
                <include refid="queryAuditUser">
                    <property name="process"
                              value="1"/>
                </include>
                )
            </if>
            <if test="departmentId != null">
                AND VALIDATE_DEPT = #{departmentId}
            </if>
            <include refid="com.tofly.qualitycontrol.mapper.CertificationOrderMapper.orderListWhereSql"/>
            <if test="dto.state != null">
                <choose>
                    <when test="dto.state == 1 ">
                        AND ((z.CURRENT_PROCESS = 5 AND g.VALIDATE_STATUS = 0 and (select count(*)
                        from TF_YWPN_MASSAUDIT_W
                        where CODE = z.CERTIFICATION_NUMBER
                        and PROCESS = 1) = 0) or
                        ((select count(*)
                        from TF_YWPN_MASSAUDIT_W m1
                        where CODE = z.CERTIFICATION_NUMBER
                        and PROCESS = 1
                        and AUDIT_STATE = 0
                        and AUDIT_USER = #{userId}
                        and num = (select min(num) from TF_YWPN_MASSAUDIT_W m2 where m2.code
                        =
                        m1.code and m2.process = 1 and audit_state = 0)
                        ) > 0
                        ))
                    </when>
                    <when test="dto.state == 2 ">
                        AND (g.VALIDATE_STATUS = 3 or (select count(*) from TF_YWPN_MASSAUDIT_W where
                        CODE = z.CERTIFICATION_NUMBER and PROCESS = 1 and
                        AUDIT_STATE = 0 and AUDIT_USER = #{userId}) = 0 and (select count(*) from TF_YWPN_MASSAUDIT_W
                        where CODE = z.CERTIFICATION_NUMBER and
                        PROCESS = 1) > 0)
                    </when>
                    <when test="dto.state == 3 ">
                        AND g.VALIDATE_STATUS = 3
                    </when>
                </choose>
            </if>
        </where>
        ORDER BY z.DISPATCH_TIME DESC
    </select>

    <resultMap id="getRectifyOrderInfoMap" type="com.tofly.qualitycontrol.vo.RectifyOrderInfoVo">
        <result property="rectifyType" column="RECTIFY_TYPE"/>
        <association property="rectifyTypeName" column="RECTIFY_TYPE"
                     select="getTypeName"></association>
    </resultMap>
    <select id="getRectifyOrderInfo" resultMap="getRectifyOrderInfoMap">
        SELECT z.ID,
               z.RECTIFY_NUMBER,
               z.RESULT_TIME_LIMIT,
               z.RECTIFY_TYPE,
               z.INCREASE_VOLUME,
               z.INCREASE_PROFIT,
               z.RECTIFY_STATUS,
               z.AUDIT_STATUS,
               z.VERIFY_STATUS,
               z.RECTIFY_TIME,
               z.AUDIT_DEPT,
               z.AUDIT_PERSON,
               z.AUDIT_PERSON_ID,
               z.AUDIT_TIME,
               z.VERIFY_DEPT,
               z.VERIFY_PERSON,
               z.VERIFY_PERSON_ID,
               z.VERIFY_TIME,
               z.RECTIFY_RESULT_DESC,
               z.RECTIFY_VERIFY_OK,
               z.RECTIFY_AUDIT_DESC,
               z.RECTIFY_VERIFY_DESC,
               z.VERIFY_TIME_LIMIT,
               z.ISSUE_TIME,
               z.ISSUE_TIME_LIMIT,
               z.ISSUE_STATUS,
               z.ISSUE_FILE_IDS,
               z.RECTIFY_ISSUE_DESC,
               z.RECTIFY_FILE_IDS,
               z.VALIDATE_DEPT,
               z.VALIDATE_PERSON,
               z.VALIDATE_PERSON_ID,
               z.VALIDATE_TIME,
               z.VALIDATE_STATUS,
               z.RECTIFY_VALIDATE_DESC,
               z.ISSUE_EXCEPTION_STATUS,
               z.RECTIFY_EXCEPTION_STATUS,
               z.AUDIT_EXCEPTION_STATUS,
               z.VERIFY_EXCEPTION_STATUS,
               z.VALIDATE_EXCEPTION_STATUS,
               z.YSXZ_BM,
               z.YSXZ_MC

        FROM TF_YWPN_ZLZG_W z
        WHERE z.CERTIFICATION_ORDER_ID = #{id}
    </select>


    <select id="loadOrderRectifySummary" resultType="com.tofly.qualitycontrol.vo.RectifySummaryVo">
        SELECT tb.*,(DISPATCH_SIZE-WC) as NWC,DECODE(DISPATCH_SIZE,0,0,ROUND((WC/DISPATCH_SIZE)*100,1))
        WCL,(CS - CS_WC) CS_NWC,DECODE(DISPATCH_SIZE,0,0,ROUND((CS/DISPATCH_SIZE)*100,1)) CSL
        FROM (
        SELECT g.RECTIFY_DEPT as DEPT,count(1) as DISPATCH_SIZE,
        SUM(decode(RECTIFY_STATUS,4,1,0)) as WC,
        SUM(CASE WHEN (CURRENT_PROCESS = 2 AND sysdate > g.RESULT_TIME_LIMIT) OR RECTIFY_EXCEPTION_STATUS =1 OR
        RECTIFY_EXCEPTION_STATUS=3 THEN 1 else 0 end) CS,
        SUM(CASE WHEN ((CURRENT_PROCESS = 2 AND sysdate > g.RESULT_TIME_LIMIT) OR RECTIFY_EXCEPTION_STATUS =1 OR
        RECTIFY_EXCEPTION_STATUS=3) and RECTIFY_STATUS = 4 THEN 1 else 0 end) CS_WC
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        WHERE CURRENT_PROCESS >1
        <include refid="com.tofly.qualitycontrol.mapper.CertificationOrderMapper.timewhere"/>
        <if test="dto.deptId != null and dto.deptId.size>0">
            AND g.RECTIFY_DEPT in
            <foreach collection="dto.deptId" item="code" open="(" separator="," close=")">
                #{code}
            </foreach>
        </if>
        GROUP BY g.RECTIFY_DEPT
        )tb
    </select>

    <select id="loadEconomyBenefit" resultType="Map">
        select g.RECTIFY_TYPE as RECTIFY_TYPE,decode(t.GDLX_MC,NULL,'未选类型',t.GDLX_MC )as RECTIFY_TYPE_NAME,count(1) as
        count,nvl(sum(g.INCREASE_PROFIT),0) as amount
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        LEFT JOIN TF_YWPN_GDGLTYPEBM_W t ON(g.RECTIFY_TYPE = t.ID)
        where
        VERIFY_STATUS = '4'
        <include refid="com.tofly.qualitycontrol.mapper.CertificationOrderMapper.timewhere"></include>
        <if test="dto.deptId != null and dto.deptId.size>0">
            AND g.RECTIFY_DEPT in
            <foreach collection="dto.deptId" item="code" open="(" separator="," close=")">
                #{code}
            </foreach>
        </if>
        <if test="dto.startDate != null and dto.endDate != null">
            and z.DISPATCH_TIME between #{dto.startDate} and #{dto.endDate}
        </if>
        group by g.RECTIFY_TYPE,t.GDLX_MC
    </select>

    <select id="maxAuditNum" resultType="long">
        select nvl(max(NUM), 0) + 1
        from TF_YWPN_MASSAUDIT_W
        where CODE = #{code}
          and AUDIT_STATE = #{state}
          and process = #{process}
    </select>

    <select id="getDeptIdByUser" resultType="long">
        select DEPARTMENT_ID
        from TF_SMPN_USER_X
        where ID = #{userId}
    </select>

    <select id="getAuditUser" resultType="long">
        select
        <choose>
            <when test="process == 0">
                z.ISSUE_PERSON_ID
            </when>
            <when test="process == 1">
                VALIDATE_PERSON_ID
            </when>
            <otherwise>
                -1
            </otherwise>
        </choose>
        from TF_YWPN_ZLRZ_W z
        LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        where z.CERTIFICATION_NUMBER = #{code}
    </select>

    <select id="maxAuditLevel" resultType="long">
        select nvl(max(NUM), 0)
        from TF_YWPN_MASSAUDIT_W
        where CODE = #{code,jdbcType=VARCHAR}
          and process = #{process,jdbcType=NUMERIC}
    </select>

    <select id="getMaxId" resultType="long">
        select nvl(max(id), 0) + 1
        from ${tableName}
    </select>

    <select id="getNowLevel" resultType="long">
        select num
        from (select num
              from TF_YWPN_MASSAUDIT_W
              where CODE = #{code}
                and AUDIT_STATE = 0
                and process = #{process}
              order by num)
        where rownum = 1
    </select>

    <select id="getNowAuditUser" resultType="long">
        select AUDIT_USER
        from TF_YWPN_MASSAUDIT_W
        where CODE = #{code,jdbcType=VARCHAR}
          and process = #{process,jdbcType=NUMERIC}
          and num = #{level}
          and rownum = 1
    </select>

</mapper>
