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

    <resultMap id="certificationOrderMap" type="com.tofly.qualitycontrol.entity.CertificationOrder">
        <id property="id" column="ID"/>
        <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="lastMonthVolume" column="LAST_MONTH_VOLUME"/>-->
        <!--        <result property="thisMonthVolume" column="THIS_MONTH_VOLUME"/>-->
        <!--        <result property="diffVolume" column="DIFF_VOLUME"/>-->
        <!--        <result property="monthOnMonthRatio" column="MONTH_ON_MONTH_RATIO"/>-->
        <!--        <result property="yearOnYearRatio" column="YEAR_ON_YEAR_RATIO"/>-->

        <result property="clientNumber" column="CLIENT_NUMBER"/>
        <result property="clientName" column="CLIENT_NAME"/>
        <result property="clientAddress" column="CLIENT_ADDRESS"/>
        <result property="meterType" column="METER_TYPE"/>
        <result property="waterCategory" column="WATER_CATEGORY"/>
        <result property="lastMonthVolume" column="LAST_MONTH_VOLUME"/>
        <result property="thisMonthVolume" column="THIS_MONTH_VOLUME"/>
        <result property="diffVolume" column="DIFF_VOLUME"/>
        <result property="monthOnMonthRatio" column="MONTH_ON_MONTH_RATIO"/>
        <result property="yearOnYearRatio" column="YEAR_ON_YEAR_RATIO"/>
        <result property="clientPhone" column="CLIENT_PHONE"/>
        <result property="actualPhone" column="ACTUAL_PHONE"/>
        <result property="tableNo" column="TABLE_NO"/>


        <result property="certificationProject" column="CERTIFICATION_PROJECT"/>
        <result property="certificationPerson" column="CERTIFICATION_PERSON"/>
        <result property="certificationPersonId" column="CERTIFICATION_PERSON_ID"/>
        <result property="certificationDept" column="CERTIFICATION_DEPT"/>
        <result property="completionTimeLimit" column="COMPLETION_TIME_LIMIT"/>
        <result property="certificationResult" column="CERTIFICATION_RESULT"/>
        <result property="isQualified" column="IS_QUALIFIED"/>
        <result property="isRectified" column="IS_RECTIFIED"/>
        <result property="certificationStatus" column="CERTIFICATION_STATUS"/>
        <result property="exceptionCause" column="EXCEPTION_CAUSE"/>
        <result property="rectifyDesc" column="RECTIFY_DESC"/>
        <result property="rectifyTimeLimit" column="RECTIFY_TIME_LIMIT"/>
        <result property="rectifyDept" column="RECTIFY_DEPT"/>
        <result property="rectifyPerson" column="RECTIFY_PERSON"/>
        <result property="rectifyPersonId" column="RECTIFY_PERSON_ID"/>
        <result property="fileIds" column="FILE_IDS"/>
        <result property="currentProcess" column="CURRENT_PROCESS"/>
        <result property="dispatchTime" column="DISPATCH_TIME"/>
        <result property="dispatchDept" column="DISPATCH_DEPT"/>
        <result property="dispatchPerson" column="DISPATCH_PERSON"/>
        <result property="dispatchPersonId" column="DISPATCH_PERSON_ID"/>
        <result property="issueDept" column="ISSUE_DEPT"/>
        <result property="issuePerson" column="ISSUE_PERSON"/>
        <result property="issuePersonId" column="ISSUE_PERSON_ID"/>
        <result property="certificationExceptionStatus" column="CERTIFICATION_EXCEPTION_STATUS"/>
        <result property="orderExceptionStatus" column="ORDER_EXCEPTION_STATUS"/>
        <result property="conclusion" column="CONCLUSION"/>
        <result property="certificationTime" column="CERTIFICATION_TIME"/>
        <result property="problemLevel" column="PROBLEM_LEVEL"/>
        <result property="parentId" column="PARENT_ID"/>
        <result property="isCb" column="IS_CB"/>
        <result property="yearMonth" column="YEAR_MONTH"/>
        <association property="synergists" column="{id=ID,parentId=PARENT_ID}"
                     select="com.tofly.qualitycontrol.mapper.ZlrzSynergistMapper.getSynergists"/>
        <association property="certificationProjectName" column="CERTIFICATION_PROJECT"
                     select="getTypeName"/>
        <association property="exceptionCauseName" column="EXCEPTION_CAUSE"
                     select="getTypeName"/>
    </resultMap>

    <select id="getTypeName" resultType="String">
        SELECT GDLX_MC
        FROM TF_YWPN_GDGLTYPEBM_W
        WHERE ID = #{id}
    </select>

    <select id="getProcessCount" resultType="java.util.LinkedHashMap">
        SELECT COUNT(CASE
                         WHEN (a.CERTIFICATION_STATUS = '0' OR a.CERTIFICATION_STATUS IS NULL) AND
                              a.CERTIFICATION_PERSON_ID = #{userId} THEN 1 END) 检验记录,
               COUNT(CASE
                         WHEN (b.ISSUE_STATUS = '0' OR b.ISSUE_STATUS IS NULL) AND b.ISSUE_PERSON_ID = #{userId}
                             THEN 1
                         when (select id
                               from TF_YWPN_MASSAUDIT_W m
                               where m.AUDIT_STATE = 0
                                 and PROCESS = 0
                                 and AUDIT_USER = #{userId}
                                 and CODE = a.CERTIFICATION_NUMBER) is not null
                             then 1 END)                                        整改下达,
               COUNT(CASE
                         WHEN (b.RECTIFY_STATUS = '0' OR b.RECTIFY_STATUS IS NULL) AND CURRENT_PROCESS > 1 AND
                              b.RECTIFY_PERSON_ID = #{userId} THEN 1 END)       整改处置,
               COUNT(CASE
                         WHEN (b.AUDIT_STATUS = '0' OR b.AUDIT_STATUS IS NULL) AND b.AUDIT_PERSON_ID = #{userId}
                             THEN 1 END)                                        整改审核,
               COUNT(CASE
                         WHEN (b.VERIFY_STATUS = '0' OR b.VERIFY_STATUS IS NULL) AND b.VERIFY_PERSON_ID = #{userId}
                             THEN 1 END)                                        整改验证,
               COUNT(CASE
                         WHEN (b.VALIDATE_STATUS = '0' OR b.VALIDATE_STATUS IS NULL) AND
                              b.VALIDATE_PERSON_ID = #{userId} THEN 1
                         when (select id
                               from TF_YWPN_MASSAUDIT_W m
                               where m.AUDIT_STATE = 0
                                 and PROCESS = 1
                                 and AUDIT_USER = #{userId}
                                 and CODE = a.CERTIFICATION_NUMBER) is not null
                             then 1 END)                                        验证审核
        FROM TF_YWPN_ZLRZ_W a
                 LEFT JOIN TF_YWPN_ZLZG_W b on a.CERTIFICATION_NUMBER = b.CERTIFICATION_NUMBER
    </select>

    <sql id="timewhere">
        <if test="dto.startDate != null ">
            and z.DISPATCH_TIME >= #{dto.startDate}
        </if>
        <if test="dto.endDate != null ">
            <![CDATA[ and z.DISPATCH_TIME <= #{dto.endDate}   ]]>
        </if>
    </sql>

    <sql id="summaryWhere">
        <choose>
            <when test="dto.projectId != null and dto.projectId.size()==1 ">
                AND z.CERTIFICATION_PROJECT = #{dto.projectId[0]}
            </when>
            <when test="dto.projectId != null and dto.projectId.size()>1 ">
                AND z.CERTIFICATION_PROJECT IN
                <foreach collection="dto.projectId" item="ids" index="index" open="(" close=")" separator=",">
                    #{ids}
                </foreach>
            </when>
        </choose>
        <if test="dto.content != null and dto.content.trim() != ''">
            AND ( z.CERTIFICATION_NUMBER like CONCAT(CONCAT('%',#{dto.content}),'%')
            <!--/*OR  z.CLIENT_NUMBER like CONCAT(CONCAT('%',#{dto.content}),'%')*/-->
            OR CERTIFICATION_RESULT like CONCAT(CONCAT('%',#{dto.content}),'%')
            )
        </if>

        <if test="dto.startDate != null ">
            and z.DISPATCH_TIME >= #{dto.startDate}
        </if>
        <if test="dto.endDate != null ">
            <![CDATA[ and z.DISPATCH_TIME <= #{dto.endDate}   ]]>
        </if>
    </sql>

    <select id="orderSummaryList" resultType="com.tofly.qualitycontrol.vo.OrderSummaryVo">
        SELECT * FROM(
        SELECT tab.* ,
        (REPORT_COUNT+INSPECT_COUNT+RELEASE_COUNT+HANDLE_COUNT+RECTIFY_AUDIT_COUNT+RECTIFY_VERIFY_COUNT+VALIDATE_COUNT)
        as TOTAL FROM (
        SELECT CERTIFICATION_PROJECT,g.GDLX_MC as CERTIFICATION_PROJECT_NAME,
        (SELECT count(distinct r.id) FROM TF_YWPN_ZLRZ_W r
        WHERE r.CERTIFICATION_PROJECT = z.CERTIFICATION_PROJECT
        <if test="dto.startDate != null and dto.endDate != null">
            and DISPATCH_TIME between #{dto.startDate} and #{dto.endDate}
        </if>
        <if test="userId != null">
            and DISPATCH_PERSON_ID = #{userId}
        </if>
        <if test="departmentId != null">
            AND DISPATCH_DEPT = #{departmentId}
        </if>
        and PARENT_ID is null ) as REPORT_COUNT ,
        (SELECT count(distinct r.id) FROM TF_YWPN_ZLRZ_W r
        WHERE r.CERTIFICATION_PROJECT = z.CERTIFICATION_PROJECT
        <if test="dto.startDate != null and dto.endDate != null">
            and DISPATCH_TIME between #{dto.startDate} and #{dto.endDate}
        </if>
        <if test="userId != null">
            and CERTIFICATION_PERSON_ID = #{userId}
        </if>
        <if test="departmentId != null">
            AND CERTIFICATION_DEPT = #{departmentId}
        </if>
        ) as INSPECT_COUNT,
        (SELECT count(distinct r.id) FROM TF_YWPN_ZLRZ_W r
        WHERE r.CERTIFICATION_PROJECT = z.CERTIFICATION_PROJECT
        <if test="dto.startDate != null and dto.endDate != null">
            and DISPATCH_TIME between #{dto.startDate} and #{dto.endDate}
        </if>
        <if test="userId != null">
            and ISSUE_PERSON_ID = #{userId}
        </if>
        <if test="departmentId != null">
            AND ISSUE_DEPT = #{departmentId}
        </if>

        ) RELEASE_COUNT,
        (SELECT count(distinct r.id) FROM TF_YWPN_ZLRZ_W r
        WHERE r.CERTIFICATION_PROJECT = z.CERTIFICATION_PROJECT
        <if test="dto.startDate != null and dto.endDate != null">
            and DISPATCH_TIME between #{dto.startDate} and #{dto.endDate}
        </if>
        <if test="userId != null">
            and RECTIFY_PERSON_ID = #{userId}
        </if>
        <if test="departmentId != null">
            AND RECTIFY_DEPT = #{departmentId}
        </if>

        ) HANDLE_COUNT,
        (SELECT count(distinct r.id) FROM TF_YWPN_ZLRZ_W r LEFT JOIN TF_YWPN_ZLZG_W g ON (r.ID =
        g.CERTIFICATION_ORDER_ID)
        WHERE r.CERTIFICATION_PROJECT = z.CERTIFICATION_PROJECT
        <if test="dto.startDate != null and dto.endDate != null">
            and r.DISPATCH_TIME between #{dto.startDate} and #{dto.endDate}
        </if>
        <if test="userId != null">
            and AUDIT_PERSON_ID = #{userId}
        </if>
        <if test="departmentId != null">
            AND AUDIT_DEPT = #{departmentId}
        </if>

        ) RECTIFY_AUDIT_COUNT,
        (SELECT count(distinct r.id) FROM TF_YWPN_ZLRZ_W r LEFT JOIN TF_YWPN_ZLZG_W g ON (r.ID =
        g.CERTIFICATION_ORDER_ID)
        WHERE r.CERTIFICATION_PROJECT = z.CERTIFICATION_PROJECT
        <if test="dto.startDate != null and dto.endDate != null">
            and r.DISPATCH_TIME between #{dto.startDate} and #{dto.endDate}
        </if>
        <if test="userId != null">
            and VERIFY_PERSON_ID = #{userId}
        </if>
        <if test="departmentId != null">
            AND VERIFY_DEPT = #{departmentId}
        </if>

        ) RECTIFY_VERIFY_COUNT,
        (SELECT count(distinct r.id) FROM TF_YWPN_ZLRZ_W r LEFT JOIN TF_YWPN_ZLZG_W g ON (r.ID =
        g.CERTIFICATION_ORDER_ID)
        WHERE r.CERTIFICATION_PROJECT = z.CERTIFICATION_PROJECT
        <if test="dto.startDate != null and dto.endDate != null">
            and r.DISPATCH_TIME between #{dto.startDate} and #{dto.endDate}
        </if>
        <if test="userId != null">
            and VALIDATE_PERSON_ID = #{userId}
        </if>
        <if test="departmentId != null">
            AND VALIDATE_DEPT = #{departmentId}
        </if>

        ) VALIDATE_COUNT
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_GDGLTYPEBM_W g ON (z.CERTIFICATION_PROJECT = g.ID)
        <where>
            <include refid="summaryWhere"/>
        </where>
        GROUP BY z.CERTIFICATION_PROJECT,g.GDLX_MC
        )tab ) t WHERE total>0 ORDER BY CERTIFICATION_PROJECT
    </select>


    <select id="orderReportSummary" resultType="com.tofly.qualitycontrol.vo.ReportSummaryVo">
        SELECT CERTIFICATION_PROJECT, g.GDLX_MC as CERTIFICATION_PROJECT_NAME,
        count(z.ID) REPORT_SUM
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_GDGLTYPEBM_W g ON (z.CERTIFICATION_PROJECT = g.ID)
        <where>
            AND PARENT_ID IS NULL
            <if test="userId != null">
                and z.DISPATCH_PERSON_ID = #{userId}
            </if>
            <if test="departmentId != null">
                AND z.DISPATCH_DEPT = #{departmentId}
            </if>
            <include refid="summaryWhere"/>
        </where>
        GROUP BY CERTIFICATION_PROJECT, g.GDLX_MC ORDER BY CERTIFICATION_PROJECT
    </select>

    <select id="orderInspectSummary" 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,SUM(END_COUNT) as END_COUNT
        FROM (
        SELECT CERTIFICATION_PROJECT,t.GDLX_MC as CERTIFICATION_PROJECT_NAME,
        count(z.id) as REPORT_SUM,
        SUM(CASE WHEN CERTIFICATION_STATUS is NULL OR CERTIFICATION_STATUS = 0 THEN 1 ELSE 0 END) as DEALT_WITH_COUNT,
        SUM(CASE WHEN CERTIFICATION_STATUS is NULL OR CERTIFICATION_STATUS = 0 THEN 0 ELSE 1 END) as DONE_COUNT,
        SUM(<![CDATA[
            case when ((ceil((COMPLETION_TIME_LIMIT-sysdate)*24*60)/ceil((COMPLETION_TIME_LIMIT-DISPATCH_TIME)*24*60) )*100  < #{ratio}
            AND (CURRENT_PROCESS = 0 or CURRENT_PROCESS is NULL) AND sysdate < COMPLETION_TIME_LIMIT AND CERTIFICATION_STATUS !=3 ) then 1 else 0 end ]]>
        ) WAIT_TIMEOUT_COUNT,
        SUM(CASE WHEN ((CURRENT_PROCESS = 0 or CURRENT_PROCESS is NULL) AND sysdate > COMPLETION_TIME_LIMIT AND
        CERTIFICATION_STATUS !=3) OR CERTIFICATION_EXCEPTION_STATUS = 1 OR CERTIFICATION_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 = 1) as BH_COUNT,
        (SUM(decode(VALIDATE_STATUS,3,1,0)) + SUM(decode(CERTIFICATION_STATUS,3,1,0))) 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>
            <if test="userId != null ">
                AND z.CERTIFICATION_PERSON_ID = #{userId}
            </if>
            <if test="departmentId != null">
                AND z.CERTIFICATION_DEPT = #{departmentId}
            </if>
            <include refid="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>

    <sql id="orderListWhereSql">
        <include refid="com.tofly.qualitycontrol.mapper.CertificationOrderMapper.summaryWhere"/>
    </sql>

    <select id="loadOrderList" resultMap="certificationOrderMap">
        SELECT z.* FROM TF_YWPN_ZLRZ_W z
        <if test="dto.state != null and dto.state == 3 ">
            LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        </if>
        <where>
            <if test="dto.reportType!=null and dto.reportType==1">
                AND z.PARENT_ID is NULL
            </if>
            <if test="userId != null">
                AND z.DISPATCH_PERSON_ID = #{userId}
            </if>
            <if test="departmentId != null">
                AND z.DISPATCH_DEPT = #{departmentId}
            </if>
            <include refid="summaryWhere"/>
        </where>
        ORDER BY z.DISPATCH_TIME DESC
    </select>

    <select id="getCertificationList" resultType="com.tofly.qualitycontrol.vo.BusinessMonitorVO_L">
        SELECT distinct
        z.ID,
        z.CERTIFICATION_NUMBER,
        z.CLIENT_NUMBER,
        z.CLIENT_NAME,
        z.CLIENT_PHONE,
        z.CLIENT_ADDRESS,
        z.METER_TYPE,
        z.WATER_CATEGORY,
        z.CERTIFICATION_PROJECT,
        t.GDLX_MC AS CERTIFICATION_PROJECT_NAME,
        (case when z.CURRENT_PROCESS is null then '0' else z.CURRENT_PROCESS end) as CURRENT_PROCESS,
        z.ORDER_EXCEPTION_STATUS,
        z.CERTIFICATION_RESULT,
        z.DISPATCH_TIME,
        z.DISPATCH_DEPT,
        z.DISPATCH_PERSON,
        z.DISPATCH_PERSON_ID,
        z.RECTIFY_DEPT,
        z.RECTIFY_PERSON,
        z.RECTIFY_PERSON_ID,
        z.CERTIFICATION_STATUS,
        z.CERTIFICATION_PERSON,
        z.CERTIFICATION_PERSON_ID,
        z.CERTIFICATION_DEPT,

         (CASE WHEN (((z.CURRENT_PROCESS = 0 OR
        z.CURRENT_PROCESS is null) AND sysdate > z.COMPLETION_TIME_LIMIT AND CERTIFICATION_STATUS !=3 )OR
        CERTIFICATION_EXCEPTION_STATUS = 1 OR CERTIFICATION_EXCEPTION_STATUS =3 ) THEN 1
        <![CDATA[     WHEN (z.CURRENT_PROCESS = 0 OR z.CURRENT_PROCESS is null) AND CERTIFICATION_STATUS !=3 AND (ceil((COMPLETION_TIME_LIMIT-sysdate)*24*60)/ceil((COMPLETION_TIME_LIMIT-DISPATCH_TIME)*24*60) )*100  < #{ratio} THEN 2 ]]>
        else 0 end) as IS_CS
        ,(SELECT COUNT(r.ID) ct FROM TF_YWPN_ZJGLFLOW_W r WHERE r.CERTIFICATION_NUMBER = z.CERTIFICATION_NUMBER AND
        r.CURRENT_PROCESS = 1 ) as IS_BH
        FROM TF_YWPN_ZLRZ_W z
        LEFT JOIN TF_YWPN_GDGLTYPEBM_W t ON (z.CERTIFICATION_PROJECT = t.ID)
        <if test="dto.state != null and (dto.state == 3 or dto.state == 2)">
            LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        </if>
        <where>
            <if test="userId != null">
                AND z.CERTIFICATION_PERSON_ID = #{userId}
            </if>
            <if test="departmentId != null">
                AND z.CERTIFICATION_DEPT = #{departmentId}
            </if>
            <include refid="orderListWhereSql"/>
            <if test="dto.state != null">
                <choose>

                    <when test="dto.state == 1">
                        AND (z.CURRENT_PROCESS IS NULL OR z.CURRENT_PROCESS =0 ) AND (z.CERTIFICATION_STATUS != 3 or
                        z.CERTIFICATION_STATUS is null)
                    </when>
                    <when test="dto.state == 2">
                        AND (z.CERTIFICATION_STATUS = 3 OR z.CERTIFICATION_STATUS = 4)
                    </when>
                    <when test="dto.state == 3">
                        AND (z.CERTIFICATION_STATUS = 3 OR g.VALIDATE_STATUS = 3)
                    </when>
                    <when test="dto.state == 4">
                        <![CDATA[
                        AND (ceil((z.COMPLETION_TIME_LIMIT-sysdate)*24*60)/ceil((z.COMPLETION_TIME_LIMIT-z.DISPATCH_TIME)*24*60) )*100  <#{ratio}
                        AND (z.CURRENT_PROCESS = 0 or z.CURRENT_PROCESS is NULL) AND sysdate < z.COMPLETION_TIME_LIMIT  ]]>
                    </when>
                    <when test="dto.state == 5">
                        AND (((CURRENT_PROCESS = 0 or CURRENT_PROCESS is NULL) AND sysdate > COMPLETION_TIME_LIMIT) OR
                        CERTIFICATION_EXCEPTION_STATUS = 1 OR CERTIFICATION_EXCEPTION_STATUS =3)
                    </when>
                    <when test="dto.state == 6">
                        AND (CERTIFICATION_EXCEPTION_STATUS = 2 OR CERTIFICATION_EXCEPTION_STATUS = 3)
                    </when>
                </choose>
            </if>
        </where>
        ORDER BY z.DISPATCH_TIME DESC
    </select>

    <resultMap id="OrderInfoMap" type="com.tofly.qualitycontrol.vo.ReportOrderInfoVo" extends="certificationOrderMap">
        <association property="rectifyOrderInfoVo" column="ID"
                     select="com.tofly.qualitycontrol.mapper.RectifyOrderMapper.getRectifyOrderInfo"/>
        <association property="problemLevelName" column="PROBLEM_LEVEL"
                     select="getTypeName"/>
    </resultMap>

    <select id="loadOrderInfos" resultMap="OrderInfoMap">
        SELECT z.*,decode(g.GDLX_ID,0,z.EXCEPTION_CAUSE,g.GDLX_ID||','||z.EXCEPTION_CAUSE) EXCEPTION_TYPE_IDS
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_GDGLTYPEBM_W g ON(z.EXCEPTION_CAUSE = g.ID)
        WHERE z.ID = #{id}
        <if test=" type != null and type == 0 ">
            OR z.PARENT_ID = #{id}
        </if>
    </select>

    <resultMap id="loadWorkOrderListMap" type="com.tofly.qualitycontrol.vo.OrderListInfoVo">
        <result property="exceptionCause" column="EXCEPTION_CAUSE"/>
        <result property="certificationProject" column="CERTIFICATION_PROJECT"/>
        <association property="exceptionCauseName" column="EXCEPTION_CAUSE"
                     select="getTypeName"/>
        <association property="certificationProjectName" column="CERTIFICATION_PROJECT"
                     select="getTypeName"/>
    </resultMap>

    <select id="loadWorkOrderList" resultMap="loadWorkOrderListMap">
        SELECT * FROM (
        SELECT z.ID,
        z.CERTIFICATION_NUMBER,
        z.CLIENT_NUMBER,
        z.CLIENT_NAME,
        z.CLIENT_ADDRESS,
        z.WATER_CATEGORY,
        z.METER_TYPE,
        z.CLIENT_PHONE,
        z.CERTIFICATION_PERSON_ID,
        z.CERTIFICATION_PROJECT,
        z.EXCEPTION_CAUSE,
        z.CERTIFICATION_DEPT,
        z.CERTIFICATION_PERSON,
        z.COMPLETION_TIME_LIMIT,
        z.CERTIFICATION_RESULT,
        z.IS_QUALIFIED,
        z.CONCLUSION,
        z.CERTIFICATION_TIME,
        (case when z.CURRENT_PROCESS is null then '0' else z.CURRENT_PROCESS end) as CURRENT_PROCESS,
        z.DISPATCH_DEPT,
        z.DISPATCH_PERSON,
        z.DISPATCH_PERSON_ID,
        z.DISPATCH_TIME,
        g.RESULT_TIME_LIMIT,
        g.RECTIFY_DEPT,
        g.RECTIFY_PERSON,
        g.RECTIFY_DESC,
        g.RECTIFY_RESULT_DESC,
        g.RECTIFY_TIME,
        g.AUDIT_TIME,
        (case z.CURRENT_PROCESS WHEN '1' THEN z.ISSUE_DEPT WHEN '2' THEN z.RECTIFY_DEPT WHEN '3' THEN g.AUDIT_DEPT WHEN
        '4' THEN g.VERIFY_DEPT WHEN '5' THEN g.VALIDATE_DEPT ELSE z.CERTIFICATION_DEPT END) NEXT_DEPT ,
        (case z.CURRENT_PROCESS WHEN '1' THEN z.ISSUE_PERSON WHEN '2' THEN z.RECTIFY_PERSON WHEN '3' THEN g.AUDIT_PERSON
        WHEN '4' THEN g.VERIFY_PERSON WHEN '5' THEN g.VALIDATE_PERSON ELSE z.CERTIFICATION_PERSON END) NEXT_PERSON ,
        (case WHEN g.VALIDATE_STATUS = 3 THEN VALIDATE_TIME WHEN z.CERTIFICATION_STATUS = 3 THEN z.CERTIFICATION_TIME
        END ) GD_TIME
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        <where>
            <include refid="timewhere"/>
            <if test="dto.certificationProject !=null and dto.certificationProject.size() > 0">
                AND z.CERTIFICATION_PROJECT IN
                <foreach collection="dto.certificationProject" item="code" open="(" separator="," close=")">
                    #{code}
                </foreach>
            </if>
            <if test="dto.exceptionCause !=null and dto.exceptionCause.size() > 0">
                AND z.EXCEPTION_CAUSE IN
                <foreach collection="dto.exceptionCause" item="code" open="(" separator="," close=")">
                    #{code}
                </foreach>
            </if>
<!--            <if test="dto.currentProcess !=null and dto.currentProcess.size() > 0">-->
<!--                AND z.CURRENT_PROCESS IN-->
<!--                <foreach collection="dto.currentProcess" item="code" open="(" separator="," close=")">-->
<!--                    #{code}-->
<!--                </foreach>-->
<!--            </if>-->
            <if test="dto.currentProcess !=null and dto.currentProcess.size() > 0">
                AND
                (CASE
                WHEN (
                CASE
                WHEN g.VALIDATE_STATUS = 3 THEN
                CASE
                WHEN g.VALIDATE_TIME IS NOT NULL THEN 1
                ELSE NULL
                END
                WHEN z.CERTIFICATION_STATUS = 3 THEN
                CASE
                WHEN z.CERTIFICATION_TIME IS NOT NULL THEN 1
                ELSE NULL
                END
                END
                ) IS NOT NULL THEN '5'
                ELSE (case when z.CURRENT_PROCESS is null then '0' else z.CURRENT_PROCESS end)
                END)
                IN
                <foreach collection="dto.currentProcess" item="code" open="(" separator="," close=")">
                    #{code}
                </foreach>
            </if>

            <if test="dto.meterType !=null and dto.meterType.size() > 0">
                AND z.METER_TYPE IN
                <foreach collection="dto.meterType" item="code" open="(" separator="," close=")">
                    #{code}
                </foreach>
            </if>
            <if test="dto.rectifyType !=null and dto.rectifyType.size() > 0">
                AND g.RECTIFY_TYPE IN
                <foreach collection="dto.rectifyType" item="code" open="(" separator="," close=")">
                    #{code}
                </foreach>
            </if>
            <if test="dto.dispatchDept !=null and dto.dispatchDept.size() > 0">
                AND z.DISPATCH_DEPT IN
                <foreach collection="dto.dispatchDept" item="code" open="(" separator="," close=")">
                    #{code}
                </foreach>
            </if>
            <if test="dto.rectifyDept !=null and dto.rectifyDept.size() > 0">
                AND g.RECTIFY_DEPT IN
                <foreach collection="dto.rectifyDept" item="code" open="(" separator="," close=")">
                    #{code}
                </foreach>
            </if>
            <if test="dto.reportType != null">
                <choose>
                    <when test="dto.reportType==2 or dto.reportType==3">
                        AND CURRENT_PROCESS >0
                    </when>
                    <when test="dto.reportType==4">
                        AND CURRENT_PROCESS >1
                    </when>
                    <when test="dto.reportType==6 or dto.reportType==7">
                        AND VERIFY_STATUS = '4'
                    </when>
                </choose>
            </if>
            <if test="dto.link == null and dto.finish != null">
                <choose>
                    <when test="dto.finish == 1">
                        AND (z.CERTIFICATION_STATUS = 3 OR g.VALIDATE_STATUS = 3)
                    </when>
                    <when test="dto.finish == 0">
                        AND ((z.CERTIFICATION_STATUS != 3 OR z.CERTIFICATION_STATUS is null)AND (g.VALIDATE_STATUS != 3
                        or g.VALIDATE_STATUS is null))
                    </when>
                </choose>
            </if>
            <if test="dto.link != null">
                <choose>
                    <when test="dto.link==0">
                        <choose>
                            <when test="0==dto.finish">
                                AND ((z.CERTIFICATION_STATUS != 3 AND z.CERTIFICATION_STATUS != 4) OR
                                z.CERTIFICATION_STATUS is null)
                            </when>
                            <when test="2==dto.finish">
                                AND (((CURRENT_PROCESS = 0 or CURRENT_PROCESS is NULL) AND sysdate >
                                COMPLETION_TIME_LIMIT) OR CERTIFICATION_EXCEPTION_STATUS = 1 OR
                                CERTIFICATION_EXCEPTION_STATUS =3)
                            </when>
                        </choose>
                    </when>
                    <when test="dto.link==1">
                        <choose>
                            <when test="0==dto.finish">
                                AND z.CURRENT_PROCESS = #{dto.link}
                            </when>
                            <when test="2==dto.finish">
                                AND ((CURRENT_PROCESS = 1 AND sysdate > ISSUE_TIME_LIMIT) OR ISSUE_EXCEPTION_STATUS =1
                                OR ISSUE_EXCEPTION_STATUS=3)
                            </when>
                            <otherwise>
                                AND z.CURRENT_PROCESS > 0
                            </otherwise>
                        </choose>
                    </when>
                    <when test="dto.link==2">
                        <choose>
                            <when test="0==dto.finish">
                                AND z.CURRENT_PROCESS = #{dto.link}
                            </when>
                            <when test="1==dto.finish">
                                AND g.RECTIFY_STATUS = 4
                            </when>
                            <when test="2==dto.finish">
                                AND ((CURRENT_PROCESS = 2 AND sysdate > g.RESULT_TIME_LIMIT) OR RECTIFY_EXCEPTION_STATUS
                                =1 OR RECTIFY_EXCEPTION_STATUS=3)
                            </when>
                            <when test="3==dto.finish">
                                AND ((CURRENT_PROCESS = 2 AND sysdate > g.RESULT_TIME_LIMIT) OR RECTIFY_EXCEPTION_STATUS
                                =1 OR RECTIFY_EXCEPTION_STATUS=3) and g.RECTIFY_STATUS = 4
                            </when>
                            <when test="4==dto.finish">
                                AND ((CURRENT_PROCESS = 2 AND sysdate > g.RESULT_TIME_LIMIT) OR RECTIFY_EXCEPTION_STATUS
                                =1 OR RECTIFY_EXCEPTION_STATUS=3) and g.RECTIFY_STATUS != 4
                            </when>
                            <otherwise>
                                AND z.CURRENT_PROCESS > 1
                            </otherwise>
                        </choose>
                    </when>
                    <when test="dto.link==3">
                        <choose>
                            <when test="0==dto.finish">
                                AND z.CURRENT_PROCESS = #{dto.link}
                            </when>
                            <otherwise>
                                AND z.CURRENT_PROCESS > 2
                            </otherwise>
                        </choose>
                    </when>
                    <when test="dto.link==4">
                        <choose>
                            <when test="0==dto.finish">
                                AND z.CURRENT_PROCESS = #{dto.link}
                            </when>
                            <when test="2==dto.finish">
                                AND ((CURRENT_PROCESS = 4 AND sysdate > g.VERIFY_TIME_LIMIT) OR VERIFY_EXCEPTION_STATUS
                                =1 OR VERIFY_EXCEPTION_STATUS=3)
                            </when>
                            <otherwise>
                                AND z.CURRENT_PROCESS > 3
                            </otherwise>
                        </choose>
                    </when>
                    <when test="dto.link==5">
                        <choose>
                            <when test="0==dto.finish">
                                AND z.CURRENT_PROCESS = #{dto.link} AND (g.VALIDATE_STATUS != 3 or g.VALIDATE_STATUS is
                                null)
                            </when>
                            <otherwise>
                                AND z.CURRENT_PROCESS > 4
                            </otherwise>
                        </choose>
                    </when>
                </choose>
            </if>
            <if test="dto.clientNumber !=null and dto.clientNumber.trim() != ''">
                AND z.CLIENT_NUMBER = #{dto.clientNumber}
            </if>
            <if test="dto.rectifyVerifyOk !=null and dto.rectifyVerifyOk.trim() != ''">
                AND g.RECTIFY_VERIFY_OK = #{dto.rectifyVerifyOk}
            </if>
            <if test="dto.content !=null and dto.content.trim() != ''">

                AND (z.CERTIFICATION_NUMBER like CONCAT(CONCAT('%',#{dto.content}),'%')
                OR z.CLIENT_NUMBER like CONCAT(CONCAT('%',#{dto.content}),'%')
                OR z.CLIENT_NAME like CONCAT(CONCAT('%',#{dto.content}),'%')
                OR z.CLIENT_ADDRESS like CONCAT(CONCAT('%',#{dto.content}),'%')
                OR z.CLIENT_PHONE like CONCAT(CONCAT('%',#{dto.content}),'%'))
            </if>

        </where>
        ) tb
        <where>
            <if test="dto.nextDept !=null and dto.nextDept.size() > 0">
                AND NEXT_DEPT IN
                <foreach collection="dto.nextDept" item="code" open="(" separator="," close=")">
                    #{code}
                </foreach>
            </if>
        </where>
        order by tb.id desc
    </select>

    <resultMap id="loadOrderFinishMap" type="com.tofly.qualitycontrol.vo.OrderFinishVo">
        <result property="certificationProject" column="CERTIFICATION_PROJECT"/>
        <association property="certificationProjectName" column="CERTIFICATION_PROJECT"
                     select="getTypeName"/>
    </resultMap>

    <select id="loadOrderFinish" resultMap="loadOrderFinishMap">
        SELECT tab.*, DECODE(DISPATCH_SIZE,0,0,ROUND((JY_CS/DISPATCH_SIZE)*100,1)) as
        JY_CSL,DECODE(XD,0,0,ROUND((XD_CS/XD)*100,1)) as XD_CSL,DECODE(ZC,0,0,ROUND((ZC_CS/ZC)*100,1)) as
        ZC_CSL,DECODE(ZY,0,0,ROUND((ZY_CS/ZY)*100,1)) as ZY_CSL,(DISPATCH_SIZE-WC)
        NWC,DECODE(DISPATCH_SIZE,0,0,ROUND((WC/DISPATCH_SIZE)*100,1)) WCL
        FROM (
        SELECT
        <choose>
            <when test="dto.group==1">
                z.DISPATCH_DEPT DEPT,
            </when>
            <otherwise>
                z.CERTIFICATION_PROJECT,
            </otherwise>
        </choose>
        count(1) as DISPATCH_SIZE,
        SUM((case WHEN ((CERTIFICATION_STATUS !=3 and CERTIFICATION_STATUS !=4) OR CURRENT_PROCESS is null) THEN 1 ELSE
        0 END)) as JY_DB,
        SUM(CASE WHEN ((CURRENT_PROCESS = 0 or CURRENT_PROCESS is NULL) AND sysdate > COMPLETION_TIME_LIMIT AND
        CERTIFICATION_STATUS !=3) OR CERTIFICATION_EXCEPTION_STATUS = 1 OR CERTIFICATION_EXCEPTION_STATUS =3 THEN 1 else
        0 end) as JY_CS,
        SUM(decode(CURRENT_PROCESS,1,1,0))XD_DB,
        SUM(case WHEN CURRENT_PROCESS >0 THEN 1 ELSE 0 END ) as XD ,
        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) as XD_CS,
        SUM(decode(CURRENT_PROCESS,2,1,0))ZC_DB,
        SUM(case WHEN CURRENT_PROCESS >1 THEN 1 ELSE 0 END ) as ZC ,
        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) as ZC_CS,
        SUM(decode(CURRENT_PROCESS,3,1,0)) ZS_DB,
        SUM(case WHEN CURRENT_PROCESS >2 THEN 1 ELSE 0 END ) as ZS,
        SUM(decode(CURRENT_PROCESS,4,1,0)) ZY_DB,
        SUM(case WHEN CURRENT_PROCESS >3 THEN 1 ELSE 0 END ) as ZY,
        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) as ZY_CS,
        <![CDATA[ SUM(case WHEN(( VALIDATE_STATUS <3 OR VALIDATE_STATUS is null)AND CURRENT_PROCESS=5 )THEN 1 ELSE 0 END) YS_DB,]]>
        SUM(decode(CURRENT_PROCESS,5,1,0) ) as YS,
        SUM(decode(VALIDATE_STATUS,3,1,0)+decode(CERTIFICATION_STATUS,3,1,0)) WC

        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        <where>
            <include refid="timewhere"/>
            <if test="dto.typeIid != null and dto.typeIid.size>0">
                AND z.CERTIFICATION_PROJECT in
                <foreach collection="dto.typeIid" item="code" open="(" separator="," close=")">
                    #{code}
                </foreach>
            </if>
            <if test="dto.deptId != null and dto.deptId.size>0">
                AND z.DISPATCH_DEPT in
                <foreach collection="dto.deptId" item="code" open="(" separator="," close=")">
                    #{code}
                </foreach>
            </if>
        </where>
        GROUP BY
        <choose>
            <when test="dto.group==1">
                z.DISPATCH_DEPT
            </when>
            <otherwise>
                z.CERTIFICATION_PROJECT
            </otherwise>
        </choose>
        ) tab
    </select>

    <resultMap id="loadDefectTypeMap" type="com.tofly.qualitycontrol.vo.OrderFinishInfoVo">

    </resultMap>
    <select id="loadDefectType" resultMap="loadDefectTypeMap">
        SELECT tab.*,(DISPATCH_SIZE-WC) as NWC,DECODE(DISPATCH_SIZE,0,0,ROUND((WC/DISPATCH_SIZE)*100,1)) WCL FROM (
        SELECT z.EXCEPTION_CAUSE AS TYPE,GDLX_MC as TYPE_NAME ,count(1) as
        DISPATCH_SIZE,sum(decode(VALIDATE_STATUS,3,1,0)) as WC
        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.EXCEPTION_CAUSE = t.ID)
        <where>
            AND CURRENT_PROCESS >0
            <include refid="timewhere"/>
            <if test="dto.typeIid != null and dto.typeIid.size()>0">
                AND z.EXCEPTION_CAUSE in
                <foreach collection="dto.typeIid" item="code" open="(" separator="," close=")">
                    #{code}
                </foreach>
            </if>
        </where>
        GROUP BY z.EXCEPTION_CAUSE,GDLX_MC
        ) tab
    </select>

    <select id="loadDefectTrend" resultType="Map">
        select *
        from (
        select
        <choose>
            <when test="dto.group==1">
                g.RECTIFY_DEPT DEPT,
            </when>
            <otherwise>
                z.EXCEPTION_CAUSE ,GDLX_MC as EXCEPTION_CAUSE_NAME,
            </otherwise>
        </choose>
        TO_CHAR(DISPATCH_TIME,'yyyy-mm') DATA_TIME,count(*) ct
        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.EXCEPTION_CAUSE = t.ID)
        WHERE CURRENT_PROCESS >0
        <include refid="timewhere"/>
        <if test="dto.typeIid != null and dto.typeIid.size>0">
            AND z.EXCEPTION_CAUSE in
            <foreach collection="dto.typeIid" item="code" open="(" separator="," close=")">
                #{code}
            </foreach>
        </if>
        <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
        <choose>
            <when test="dto.group==1">
                g.RECTIFY_DEPT,
            </when>
            <otherwise>
                z.EXCEPTION_CAUSE,GDLX_MC,
            </otherwise>
        </choose>

        TO_CHAR(DISPATCH_TIME,'yyyy-mm')
        ) pivot (max(ct) for DATA_TIME in (${column})
        )
    </select>

    <select id="loadClientAttestation" resultType="com.tofly.qualitycontrol.vo.ClientAttestationVo">
        SELECT tab.*,(DISPATCH_SIZE-WC) NWC,DECODE(DISPATCH_SIZE,0,0,ROUND((WC/DISPATCH_SIZE)*100,1)) WCL FROM(
        SELECT CLIENT_NUMBER,CLIENT_NAME,CLIENT_ADDRESS,METER_TYPE,WATER_CATEGORY,CLIENT_PHONE,QYMC,QYDMC,COUNT(1) as
        DISPATCH_SIZE,SUM(WC) WC FROM (
        SELECT z.CLIENT_NUMBER,z.CLIENT_NAME,z.CLIENT_ADDRESS,q.SBKJ as
        METER_TYPE,z.WATER_CATEGORY,z.CLIENT_PHONE,c.QYMC,d.QYDMC,
        (case z.CURRENT_PROCESS WHEN '1' THEN z.ISSUE_DEPT WHEN '2' THEN z.RECTIFY_DEPT WHEN '3' THEN g.AUDIT_DEPT WHEN
        '4' THEN g.VERIFY_DEPT WHEN '5' THEN g.VALIDATE_DEPT ELSE z.CERTIFICATION_DEPT END) NEXT_DEPT
        ,(case WHEN z.CERTIFICATION_STATUS = 3 OR g.VALIDATE_STATUS = 3 THEN 1 ELSE 0 END) WC
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        LEFT JOIN TF_YWPN_YHBINGQY_W y ON (z.CLIENT_NUMBER = y.YHBH )
        LEFT JOIN TF_YWPN_QFKHJBXX_W q ON(z.CLIENT_NUMBER = q.YHBH)
        LEFT JOIN TF_YWPN_CBQYBM_W c ON (y.QYBM = c.QYBM)
        LEFT JOIN TF_YWPN_CBQYDBM_W d ON (y.QYDBM = d.QYDBM)
        WHERE z.CLIENT_NUMBER is not null
        <include refid="timewhere"/>
        <if test="dto.qybm !=null and dto.qybm.trim() != ''">
            AND y.QYBM = #{dto.qybm}
        </if>
        <if test="dto.qdbm !=null and dto.qdbm.size() > 0">
            AND y.QYDBM IN
            <foreach collection="dto.qdbm" item="code" open="(" separator="," close=")">
                #{code}
            </foreach>
        </if>

        <if test="dto.exceptionCause !=null and dto.exceptionCause.size() > 0">
            AND z.EXCEPTION_CAUSE IN
            <foreach collection="dto.exceptionCause" item="code" open="(" separator="," close=")">
                #{code}
            </foreach>
        </if>
        <if test="dto.currentProcess !=null and dto.currentProcess.size() > 0">
            AND z.CURRENT_PROCESS IN
            <foreach collection="dto.currentProcess" item="code" open="(" separator="," close=")">
                #{code}
            </foreach>
        </if>
        <if test="dto.meterType !=null and dto.meterType.size() > 0">
            AND q.SBKJ IN
            <foreach collection="dto.meterType" item="code" open="(" separator="," close=")">
                #{code}
            </foreach>
        </if>
        <if test="dto.dispatchDept !=null and dto.dispatchDept.size() > 0">
            AND z.DISPATCH_DEPT IN
            <foreach collection="dto.dispatchDept" item="code" open="(" separator="," close=")">
                #{code}
            </foreach>
        </if>
        <if test="dto.rectifyDept !=null and dto.rectifyDept.size() > 0">
            AND g.RECTIFY_DEPT IN
            <foreach collection="dto.rectifyDept" item="code" open="(" separator="," close=")">
                #{code}
            </foreach>
        </if>
        <if test="dto.content !=null and dto.content.trim() != ''">
            AND (z.CERTIFICATION_NUMBER like CONCAT(CONCAT('%',#{dto.content}),'%')
            OR z.CLIENT_NUMBER like CONCAT(CONCAT('%',#{dto.content}),'%')
            OR z.CLIENT_NAME like CONCAT(CONCAT('%',#{dto.content}),'%')
            OR z.CLIENT_ADDRESS like CONCAT(CONCAT('%',#{dto.content}),'%')
            OR z.CLIENT_PHONE like CONCAT(CONCAT('%',#{dto.content}),'%'))
        </if>
        ) t
        <where>
            <if test="dto.nextDept !=null and dto.nextDept.size() > 0">
                AND NEXT_DEPT IN
                <foreach collection="dto.nextDept" item="code" open="(" separator="," close=")">
                    #{code}
                </foreach>
            </if>
        </where>
        GROUP BY CLIENT_NUMBER,CLIENT_NAME,CLIENT_ADDRESS,METER_TYPE,WATER_CATEGORY,CLIENT_PHONE,QYMC,QYDMC
        ) tab
    </select>

    <select id="loadNotQualified" resultType="Map">
        select * from (
        select g.RECTIFY_DEPT DEPT,TO_CHAR(DISPATCH_TIME,'yyyy-mm') DATA_TIME,count(*) ct
        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.EXCEPTION_CAUSE = t.ID)
        WHERE g.VERIFY_STATUS = '4' AND g.RECTIFY_VERIFY_OK = '0'
        <include refid="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, TO_CHAR(DISPATCH_TIME,'yyyy-mm')
        ) pivot (max(ct) for DATA_TIME in (${column}))
    </select>

    <select id="loadQualified" resultType="com.tofly.qualitycontrol.vo.RectifyQualifiedVo">
        SELECT tab.*,(DISPATCH_SIZE-HG) NHG,DECODE(DISPATCH_SIZE,0,0,ROUND((HG/DISPATCH_SIZE)*100,1)) as HGL FROM(
        SELECT TO_CHAR(DISPATCH_TIME,'yyyy-mm') DATA_TIME,count(1) as DISPATCH_SIZE,
        SUM(DECODE(g.RECTIFY_VERIFY_OK,'1',1,0)) HG
        FROM TF_YWPN_ZLRZ_W z LEFT JOIN TF_YWPN_ZLZG_W g ON (z.ID = g.CERTIFICATION_ORDER_ID)
        WHERE g.VERIFY_STATUS = '4'
        <include refid="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 TO_CHAR(DISPATCH_TIME,'yyyy-mm')) tab
    </select>

    <update id="updateReadmeterhis">
        UPDATE TF_YWPN_READMETERHIS_W
        SET SHBZ = '3'
        WHERE YHBH = #{clientNumber}
          AND MONTH_PLAN = #{yearMonth}
    </update>

    <select id="getFilePath" resultType="string">
        select FILE_PATH from TF_YWPN_FILE_W where ID in
        <foreach collection="fileids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
</mapper>
