<?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.unis.module.coding.dal.mysql.dk.DkMapper">

    <select id="getTaskList" resultType="com.unis.module.coding.controller.admin.task.vo.TaskRespVO">
        SELECT
            dk.ID AS kqId,
            dk.KQDM ,
            dk.KQMC ,
            dk.JTDZ ,
            dk.SHENG ,dk.SHI ,dk.XIAN ,
            dk.KQFZR , dk.LXDH ,
            dp.HYRY_ID ,
            dp.HYRY_NAME ,
            dp.HY_STATE ,
            dk.SHXYDM ,
            dk.KQBM ,
            COUNT(distinct dc.ID) AS HY_NUM,
            SUM(CASE WHEN dp.HY_STATE = 1 THEN 1 ELSE 0 END) AS HYTG_NUM,
            SUM(CASE WHEN dp.HY_STATE = 2 THEN 1 ELSE 0 END) AS HYBTG_NUM
        FROM
            ${pageReqVO.tableName} dp
        LEFT JOIN DELIVERY_CAXX dc ON dp.CA_ID = dc.ID
        LEFT JOIN DELIVERY_KQXX dk ON dp.KQ_ID = dk.ID
        WHERE
            dk.DELETED = 0 AND dk.SFZX = 0 and dp.DELETED = 0 AND dc.DELETED = 0
        <if test="pageReqVO.sheng != null and pageReqVO.sheng != ''">
            AND dk.SHENG = #{pageReqVO.sheng}
        </if>
        <if test="pageReqVO.shi != null and pageReqVO.shi != ''">
            AND dk.SHI = #{pageReqVO.shi}
        </if>
        <if test="pageReqVO.xian != null and pageReqVO.xian != ''">
            AND dk.XIAN = #{pageReqVO.xian}
        </if>
        <if test="pageReqVO.hyState != null">
            AND dp.HY_STATE = #{pageReqVO.hyState}
        </if>
        <if test="pageReqVO.hyryId != null">
            AND dp.HYRY_ID = #{pageReqVO.hyryId}
        </if>
        <if test="pageReqVO.bankType != null and pageReqVO.bankType != ''">
            AND FIND_IN_SET(#{pageReqVO.bankType},dp.RDYH_ID)
        </if>
        <if test="pageReqVO.kqIds != null and pageReqVO.kqIds.size() > 0">
            AND dp.KQ_ID IN
            <foreach collection="pageReqVO.kqIds" item="kqId" open="(" separator="," close=")">
                #{kqId}
            </foreach>
        </if>
        GROUP BY
            dk.ID
        ORDER BY
            dp.ID desc
    </select>
    <select id="getMyStoreroomList" resultType="com.unis.module.coding.controller.admin.task.vo.TaskRespVO">
        SELECT
        dk.ID AS kqId,
        dk.KQDM ,
        dk.KQMC ,
        dk.JTDZ ,
        dk.SHENG ,dk.SHI ,dk.XIAN ,
        dk.KQFZR , dk.LXDH ,
        COUNT(distinct dc.ID) AS HY_NUM
        FROM
        DELIVERY_USERS du
        LEFT JOIN DELIVERY_CAXX dc ON du.CA_ID = dc.ID
        LEFT JOIN DELIVERY_KQXX dk ON du.KQ_ID = dk.ID
        WHERE dk.DELETED = 0 AND dk.SFZX = 0 and du.DELETED = 0 AND dc.DELETED = 0
            <if test="pageReqVO.sheng != null and pageReqVO.sheng != ''">
                AND dk.SHENG = #{pageReqVO.sheng}
            </if>
            <if test="pageReqVO.shi != null and pageReqVO.shi != ''">
                AND dk.SHI = #{pageReqVO.shi}
            </if>
            <if test="pageReqVO.xian != null and pageReqVO.xian != ''">
                AND dk.XIAN = #{pageReqVO.xian}
            </if>
            <if test="pageReqVO.creator != null">
                AND du.CREATOR = #{pageReqVO.creator}
            </if>
        GROUP BY
            dk.ID
        ORDER BY
            du.ID desc
    </select>
    <select id="getAllStoreroomList" resultType="com.unis.module.coding.controller.admin.task.vo.TaskRespVO">
        SELECT
        dk.ID AS kqId,
        dk.KQDM ,
        dk.KQMC ,
        dk.JTDZ ,
        dk.SHENG ,dk.SHI ,dk.XIAN ,
        dk.KQFZR , dk.LXDH ,
        dp.HYRY_ID ,
        dp.HYRY_NAME ,
        dp.HY_STATE ,
        COUNT(distinct dc.ID) AS HY_NUM
        FROM
        DELIVERY_BANK dp
        LEFT JOIN DELIVERY_CAXX dc ON dp.CA_ID = dc.ID
        LEFT JOIN DELIVERY_KQXX dk ON dp.KQ_ID = dk.ID
        LEFT JOIN DELIVERY_USERS du ON dc.ID = du.CA_ID AND du.DELETED = 0
        WHERE
        dk.DELETED = 0 AND dk.SFZX = 0 and dp.DELETED = 0 AND dc.DELETED = 0
        AND du.ID IS NULL
        <if test="pageReqVO.sheng != null and pageReqVO.sheng != ''">
            AND dk.SHENG = #{pageReqVO.sheng}
        </if>
        <if test="pageReqVO.shi != null and pageReqVO.shi != ''">
            AND dk.SHI = #{pageReqVO.shi}
        </if>
        <if test="pageReqVO.xian != null and pageReqVO.xian != ''">
            AND dk.XIAN = #{pageReqVO.xian}
        </if>
        <if test="pageReqVO.hyState != null">
            AND dp.HY_STATE = #{pageReqVO.hyState}
        </if>
        <if test="pageReqVO.hyryId != null">
            AND dp.HYRY_ID = #{pageReqVO.hyryId}
        </if>
        <if test="pageReqVO.bankType != null and pageReqVO.bankType != ''">
            AND FIND_IN_SET(#{pageReqVO.bankType},dp.RDYH_ID)
        </if>
        <if test="pageReqVO.kqIds != null and pageReqVO.kqIds.size() > 0">
            AND dp.KQ_ID IN
            <foreach collection="pageReqVO.kqIds" item="kqId" open="(" separator="," close=")">
                #{kqId}
            </foreach>
        </if>
        GROUP BY
        dk.ID
        ORDER BY
        dp.ID desc
    </select>
    <select id="getCaxxByKqIdForClr" resultType="com.unis.module.coding.controller.admin.task.vo.TaskCaxxRespVO">
        SELECT
            dk.ID AS kqId,
            dc.ID AS caId,
            dc.CFMC ,
            dc.CFLX ,
            dc.CAZT ,
            dc.SJCR ,
            dc.SFKC ,
            dk."zcckj" ,
            dk."wtbgckj"
        FROM
            DELIVERY_KQXX dk
        LEFT JOIN DELIVERY_CAXX dc ON dk.ID = dc.KQID
        left join DELIVERY_BANK db on DC.ID = db.CA_ID and db.deleted = 0
        LEFT JOIN DELIVERY_USERS du ON dc.ID = du.CA_ID and du.deleted = 0
        WHERE
            dk.DELETED =0 AND dc.DELETED = 0 AND dk.SFZX = 0
        <if test="pageReqVO.kqId != null">
            AND dk.ID = #{pageReqVO.kqId}
        </if>
        <if test="pageReqVO.bankType != null and pageReqVO.bankType != ''">
            AND FIND_IN_SET(#{pageReqVO.bankType},db.RDYH_ID)
        </if>
        <if test="pageReqVO.clrsfxd != null and pageReqVO.clrsfxd == 0 ">
            AND du.ID IS NULL
        </if>
        <if test="pageReqVO.clrsfxd != null and pageReqVO.clrsfxd == 1 ">
            AND du.ID IS NOT NULL
        </if>
        <if test="pageReqVO.creator != null">
            AND du.CREATOR = #{pageReqVO.creator}
        </if>
        <if test="pageReqVO.hyState != null">
            AND db.HY_STATE = #{pageReqVO.hyState}
        </if>
        <if test="pageReqVO.caIds != null and pageReqVO.caIds.size() > 0">
            AND dc.ID IN
            <foreach collection="pageReqVO.caIds" item="caId" open="(" separator="," close=")">
                #{caId}
            </foreach>
        </if>
        GROUP BY
        dc.ID
        ORDER BY
        dc.ID desc
    </select>
    <select id="getHwkByCfId" resultType="com.unis.module.coding.controller.admin.task.vo.TaskHwkRespVO">
        SELECT
            dh.CF_ID ,
            dc.CFMC ,
            dh.HWMC ,
            dh.LSPZMC ,
            dh.LSDJBM ,
            dh.SHND ,
            dh.CDBM ,
            sal."area_name" ,
            TO_CHAR( dh.RKSJ ,'yyyy-MM-dd HH:mm:ss') as RKSJ,
            dh.SJSL ,
            dh.JJSL ,
            dh.BGFS ,
            dh.BGYXM
        FROM
            DELIVERY_HWK dh
                LEFT JOIN DELIVERY_CAXX dc ON dc.ID = dh.CF_ID
                LEFT JOIN "system_area_level" sal ON sal."area_code" = dh.cdbm AND sal."deleted" =0
        WHERE
            dh.DELETED = 0 AND dc.DELETED = 0 and dh.CF_ID = #{cfId}
            LIMIT 1
    </select>
    <select id="getSyxxByCfId" resultType="com.unis.module.coding.controller.admin.task.vo.TaskSyxxRespVO">
        SELECT * FROM DELIVERY_SYXX ds WHERE ds.DELETED = 0  and ds.CF_ID = #{cfId}
            LIMIT 1
    </select>
    <select id="getLqjcxxByCfId" resultType="com.unis.module.coding.controller.admin.task.vo.TaskLqjcxxRespVO">
        SELECT
            dl.*,dh.LSPZMC ,dh.LSDJBM
        FROM
            DELIVERY_LQJCXX dl
        LEFT JOIN DELIVERY_HWK dh ON dh.ID = dl.HWK_ID
        WHERE
            dl.DELETED = 0 and dl.CF_ID = #{cfId}
            LIMIT 1
    </select>
    <select id="getCrkxxByCfId" resultType="com.unis.module.coding.controller.admin.task.vo.TaskCrkxxRespVO">
        SELECT
            dc.ID ,
            dc.LSPZMC ,
            dc.LSDJBM ,
            dc.JZ ,
            dc.CCH ,
            dc.FILE_IDS ,
            dc.CRKLX ,
            CASE WHEN dc.CRKLX = 0 THEN TO_CHAR( dc.CMSJ  ,'yyyy-MM-dd HH:mm:ss') ELSE TO_CHAR( dc.RMSJ  ,'yyyy-MM-dd HH:mm:ss') END AS YWSJ
        FROM
            DELIVERY_CRKXX dc
        WHERE
            dc.DELETED = 0 and dc.CF_ID = #{pageReqVO.caId}
        <if test="pageReqVO.crklx != null">
            AND dc.CRKLX = #{pageReqVO.crklx}
        </if>
        ORDER BY ywsj DESC ,dc.ID DESC
    </select>
    <select id="getZjxxByCfId" resultType="com.unis.module.coding.controller.admin.task.vo.TaskZjxxRespVO">
        SELECT
            dz.ID ,
            dz.CF_ID ,
            dz.JCSJ ,
            dz.JCLX ,
            dz.LSPZMC ,
            dz.LSDJBM ,
            dz.SFHG ,
            dz.JCRRXM ,
            dz.FILE_IDS
        FROM
            DELIVERY_ZJXX dz
        WHERE
            dz.DELETED = 0 AND dz.CF_ID = #{pageReqVO.caId}
        ORDER BY dz.JCSJ DESC ,dz.ID  DESC
    </select>
    <select id="selectCaxxListByKqId"
            resultType="com.unis.module.coding.controller.admin.task.vo.TaskCaxxRespVO">
        SELECT
            dc.KQID AS KQ_ID,
            dc.ID AS CA_ID,
            dc.CFMC,
            dc.CFLX,
            dc.CAZT,
            dc.SJCR,
            dc.SJSYNX,
            dc.SFKC,
            dk."zcckj",
            dk."wtbgckj",
            dp.HY_STATE
        FROM
            ${pageReqVO.tableName} dp
            LEFT JOIN DELIVERY_CAXX dc ON dp.CA_ID = dc.ID
            LEFT JOIN DELIVERY_KQXX dk ON dp.KQ_ID = dk.ID
        WHERE
        dk.DELETED = 0 AND dk.SFZX = 0 and dp.DELETED = 0 AND dc.DELETED = 0
        and dp.KQ_ID = #{pageReqVO.kqId}
        <if test="pageReqVO.hyState != null">
            AND dp.HY_STATE = #{pageReqVO.hyState}
        </if>
        <if test="pageReqVO.hyryId != null">
            AND dp.HYRY_ID = #{pageReqVO.hyryId}
        </if>
        <if test="pageReqVO.bankType != null and pageReqVO.bankType != ''">
            AND FIND_IN_SET(#{pageReqVO.bankType},dp.RDYH_ID)
        </if>
        <if test="pageReqVO.cfmc != null and pageReqVO.cfmc != ''">
            AND dc.CFMC LIKE CONCAT('%', #{pageReqVO.cfmc}, '%')
        </if>
        <if test="pageReqVO.kqIds != null and pageReqVO.kqIds.size() > 0">
            AND dp.KQ_ID IN
            <foreach collection="pageReqVO.kqIds" item="kqId" open="(" separator="," close=")">
                #{kqId}
            </foreach>
        </if>
        GROUP BY
        dc.ID
        ORDER BY
        dc.ID desc
    </select>
</mapper>