<?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.dwxx.DwxxMapper">

     <select id="selectWrapperPage" parameterType="com.unis.module.coding.controller.admin.delivery.vo.DeliveryPageReqVO" resultType="com.unis.module.coding.controller.admin.delivery.vo.DeliveryRespVO">
        select
            ck.id kqId,
            cd.id deptId,
            SUBSTRING_INDEX(ck.SHENG, ',', 1) SHENG,
            SUBSTRING_INDEX(SUBSTRING_INDEX(ck.SHENG, ',', 2), ',', -1) SHI,
            SUBSTRING_INDEX(ck.SHENG, ',', -1) XIAN,
            cd.TYXYDM,
            cd.DWMC,
            ck.KQMC,
            TRUNC(ifnull(t1.sjcrNum, 0)) sjcrNum,
            ifnull(t1.cfnum, 0) cfnum,
            ifnull(t2.yxcfnum, 0) yxcfnum,
            ck.xzqhdm
        from  CODING_DWXX cd
        left join coding_kqxx ck on ck.DWID=cd.id
        left join
        (
                select
                        cf.BASE_KQXX_ID,
                        sum(cc.SJCR) sjcrNum,
                        count(cf.BASE_CAXX_ID) cfnum,
                        ck.xzqhdm
                from
                        CODING_CAXX cc
                join CODING_FMXX cf on cc.id=cf.BASE_CAXX_ID
                join CODING_KQXX ck on cc.KQID=ck.id
                <where>
                        cf.deleted=0
                    and cf.lsxz='000'
                    AND NOT EXISTS (SELECT 1  from  DELIVERY_CAXX dc   where dc.DELETED=0 AND  cc.id=dc.id)
                    <if test="pageReqVO.cflx!=null and pageReqVO.cflx!=''">
                        and cc.CFLX=#{pageReqVO.cflx}
                    </if>
                    <if test="pageReqVO.cfzt!=null and pageReqVO.cfzt!=''">
                        and cc.CAZT=#{pageReqVO.cfzt}
                    </if>
                    <if test="pageReqVO.cfStart!=null and pageReqVO.cfStart!='' and pageReqVO.cfEnd!=null and pageReqVO.cfEnd!=''">
                        and cc.SJCR between #{pageReqVO.cfStart} and #{pageReqVO.cfEnd}
                    </if>
                    <if test="(pageReqVO.cfStart == null or pageReqVO.cfStart == '') and pageReqVO.cfEnd != null and pageReqVO.cfEnd != ''">
                        and cc.SJCR <![CDATA[<=]]>  #{pageReqVO.cfEnd}
                    </if>
                    <if test=" pageReqVO.cfStart!=null and pageReqVO.cfStart!='' and (pageReqVO.cfEnd == null or pageReqVO.cfEnd == '') ">
                        and cc.SJCR <![CDATA[>=]]>  #{pageReqVO.cfStart}
                    </if>
                </where>
                group by cf.BASE_KQXX_ID
        ) t1 on t1.BASE_KQXX_ID=ck.id
        left join
        (
        select
                cc.KQID,
                count(cc.id) yxcfnum,
                ck.xzqhdm
        from  DELIVERY_KQXX ck
        join  DELIVERY_CAXX cc on cc.KQID=ck.id
        where cc.yxcf=1 and ck.deleted=0 and  cc.deleted=0
        group by ck.id
        ) t2 on t2.KQID=ck.id
        <where>
            cd.bz is null
            and cd.qyxz!='10'
            and cd.DELETED=0
            <choose>
                <when test=" (pageReqVO.cflx!=null and pageReqVO.cflx!='')
                         or (pageReqVO.cfzt!=null and pageReqVO.cfzt!='')
                         or (pageReqVO.cfStart!=null and pageReqVO.cfStart !='')
                         or (pageReqVO.cfEnd!=null and pageReqVO.cfEnd !='') ">
                    and t1.cfnum !=0
                </when>
                <otherwise>
                   and  (t1.cfnum !=0  or t2.yxcfnum !=0)
                </otherwise>
            </choose>
            <if test="pageReqVO.areaCode!=null and pageReqVO.areaCode!=''">
                and ck.sheng like #{pageReqVO.areaCode}
            </if>
            <if test="pageReqVO.kqStart!=null and pageReqVO.kqEnd!=null ">
                and IFNULL(t1.sjcrNum, 0)between #{pageReqVO.kqStart} and #{pageReqVO.kqEnd}
            </if>
            <if test="pageReqVO.kqStart!=null  and pageReqVO.kqEnd==null ">
                and t1.sjcrNum <![CDATA[>=]]>  #{pageReqVO.kqStart}
            </if>
            <if test="pageReqVO.kqStart==null  and pageReqVO.kqEnd!=null ">
                and t1.sjcrNum <![CDATA[<=]]>  #{pageReqVO.kqEnd}
            </if>           
            <if test="pageReqVO.dwmc!=null and pageReqVO.dwmc!=''">
                and cd.DWMC like #{pageReqVO.dwmc}
            </if>
        </where>
        order by cd.id desc
    </select>

     <select id="selectWrapperPageForSheng" parameterType="com.unis.module.coding.controller.admin.delivery.vo.DeliveryPageReqVO" resultType="com.unis.module.coding.controller.admin.delivery.vo.DeliveryRespVO">
        select
            ck.id kqId,
            cd.id deptId,
            ck.sheng_name SHENG,
            ck.shi_name  SHI,
            ck.xian_name XIAN,
            cd.TYXYDM,
            cd.DWMC,
            ck.KQMC,
            TRUNC(ifnull(t1.sjcrNum, 0)) sjcrNum,
            ifnull(t1.cfnum, 0) cfnum,
            ifnull(t2.yxcfnum, 0) yxcfnum
        from  DELIVERY_DWXX cd
        left join DELIVERY_KQXX ck on ck.DWID=cd.id
        left join
        (
                select
                        cc.KQID,
                        sum(cc.SJCR) sjcrNum,
                        count(cc.ID) cfnum
                from DELIVERY_CAXX cc
                <where>
                     NOT EXISTS (SELECT 1  from  DELIVERY_PROVINCE dp   where dp.DELETED=0 AND  cc.id=dp.CA_ID)
                    <if test="pageReqVO.cflx!=null and pageReqVO.cflx!=''">
                        and cc.CFLX=#{pageReqVO.cflx}
                    </if>
                    <if test="pageReqVO.cfzt!=null and pageReqVO.cfzt!=''">
                        and cc.CAZT=#{pageReqVO.cfzt}
                    </if>
                    <if test="pageReqVO.cfStart!=null and pageReqVO.cfStart!='' and pageReqVO.cfEnd!=null and pageReqVO.cfEnd!=''">
                        and cc.SJCR between #{pageReqVO.cfStart} and #{pageReqVO.cfEnd}
                    </if>
                    <if test="(pageReqVO.cfStart == null or pageReqVO.cfStart == '') and pageReqVO.cfEnd != null and pageReqVO.cfEnd != ''">
                        and cc.SJCR <![CDATA[<=]]>  #{pageReqVO.cfEnd}
                    </if>
                    <if test=" pageReqVO.cfStart!=null and pageReqVO.cfStart!='' and (pageReqVO.cfEnd == null or pageReqVO.cfEnd == '') ">
                        and cc.SJCR <![CDATA[>=]]>  #{pageReqVO.cfStart}
                    </if>
                </where>
                group by cc.KQID
        ) t1 on t1.KQID=ck.id
        left join
        (
        select
                pro.KQ_ID,
                count(pro.CA_ID) yxcfnum
        from  DELIVERY_PROVINCE  pro
        where   pro.DELETED=0
        group by pro.KQ_ID
        ) t2 on t2.KQ_ID=ck.id
        <where>
            cd.qyxz!='10'
            and cd.DELETED=0
            <choose>
                <when test=" (pageReqVO.cflx!=null and pageReqVO.cflx!='')
                         or (pageReqVO.cfzt!=null and pageReqVO.cfzt!='')
                         or (pageReqVO.cfStart!=null and pageReqVO.cfStart !='')
                         or (pageReqVO.cfEnd!=null and pageReqVO.cfEnd !='') ">
                    and t1.cfnum !=0
                </when>
                <otherwise>
                   and  (t1.cfnum !=0  or t2.yxcfnum !=0)
                </otherwise>
            </choose>
            <if test="pageReqVO.kqStart!=null  and pageReqVO.kqEnd!=null">
                and IFNULL(t1.sjcrNum, 0) between #{pageReqVO.kqStart} and #{pageReqVO.kqEnd}
            </if>
            <if test="pageReqVO.kqStart!=null  and pageReqVO.kqEnd==null">
                and t1.sjcrNum <![CDATA[>=]]>  #{pageReqVO.kqStart}
            </if>
            <if test="pageReqVO.kqStart==null  and pageReqVO.kqEnd!=null">
                and t1.sjcrNum <![CDATA[<=]]>  #{pageReqVO.kqEnd}
            </if>
            <if test="pageReqVO.dwmc!=null and pageReqVO.dwmc!=''">
                and cd.DWMC like #{pageReqVO.dwmc}
            </if>
            <if test="pageReqVO.areaXian!=null and pageReqVO.areaXian!=''">
                and ck.XIAN = #{pageReqVO.areaXian}
            </if>
            <if test="pageReqVO.areaShi!=null and pageReqVO.areaShi!=''">
                and ck.SHI = #{pageReqVO.areaShi}
            </if>
            <if test="pageReqVO.areaSheng!=null and pageReqVO.areaSheng!=''">
                and ck.SHENG = #{pageReqVO.areaSheng}
            </if>
        </where>
        order by cd.id desc
    </select>

     <select id="selectWrapperPageForBank" parameterType="com.unis.module.coding.controller.admin.delivery.vo.DeliveryPageReqVO" resultType="com.unis.module.coding.controller.admin.delivery.vo.DeliveryRespVO">
        select
            ck.id kqId,
            cd.id deptId,
            t2.cwId,
            ck.sheng_name SHENG,
            ck.shi_name  SHI,
            ck.xian_name XIAN,
            cd.TYXYDM,
            cd.DWMC,
            ck.KQMC,
            TRUNC(ifnull(t1.sjcrNum, 0)) sjcrNum,
            ifnull(t1.cfnum, 0) cfnum,
            ifnull(t3.yxcfnum, 0) yxcfnum,
            ifnull(t2.SYNDZZC, 0) zzc,
            ifnull(t2.JLR, 0) sndjlr,
            ck.KQFZR kqfzr,
            ck.LXDH lxdh
        from  DELIVERY_DWXX cd
        join DELIVERY_KQXX ck on ck.DWID=cd.id
        left join
        (
                select
                        pe.KQ_ID,
                        sum(cc.SJCR) sjcrNum,
                        count(cc.id) cfnum
                from  DELIVERY_CAXX cc
                join DELIVERY_PROVINCE  pe  on cc.id=pe.CA_ID
                <where>
                    pe.HY_STATE=1  and  pe.DELETED=0
                    AND NOT EXISTS (SELECT 1  from  DELIVERY_BANK bk   where bk.DELETED=0 AND  cc.id=bk.CA_ID   and  rdyh_id = #{pageReqVO.yhmc})
                    <if test="pageReqVO.cflx!=null and pageReqVO.cflx!=''">
                        and cc.CFLX=#{pageReqVO.cflx}
                    </if>
                    <if test="pageReqVO.cfzt!=null and pageReqVO.cfzt!=''">
                        and cc.CAZT=#{pageReqVO.cfzt}
                    </if>
                    <if test="pageReqVO.cfStart!=null and pageReqVO.cfStart!='' and pageReqVO.cfEnd!=null and pageReqVO.cfEnd!=''">
                        and cc.SJCR between #{pageReqVO.cfStart} and #{pageReqVO.cfEnd}
                    </if>
                    <if test="(pageReqVO.cfStart == null or pageReqVO.cfStart == '') and pageReqVO.cfEnd != null and pageReqVO.cfEnd != ''">
                        and cc.SJCR <![CDATA[<=]]>  #{pageReqVO.cfEnd}
                    </if>
                    <if test=" pageReqVO.cfStart!=null and pageReqVO.cfStart!='' and (pageReqVO.cfEnd == null or pageReqVO.cfEnd == '') ">
                        and cc.SJCR <![CDATA[>=]]>  #{pageReqVO.cfStart}
                    </if>
                </where>
                group by pe.KQ_ID
        ) t1 on t1.KQ_ID=ck.id
        left join
        (
                select
                        cx.KQID,
                        cx.id cwId,
                        cx.SYNDZZC,
                        cy.JLR
                from   DELIVERY_CWXX  cx
                JOIN
                  (
                   SELECT d.CWXX_ID,d.JLR
                   FROM DELIVERY_CWXX_YEAR d
                   JOIN (
                   SELECT CWXX_ID, MAX(CN) AS maxCN
                   FROM  DELIVERY_CWXX_YEAR
                   GROUP BY  CWXX_ID) m
                   ON d.CWXX_ID = m.CWXX_ID AND d.CN = m.maxCN
                   )  cy  on cx.id=cy.CWXX_ID
        ) t2 on t2.KQID=ck.id
        left join
        (
        select
                KQ_ID,
                count( distinct CA_ID) yxcfnum
        from  DELIVERY_BANK
        where DELETED=0
        <if test="pageReqVO.yhmc!=null and pageReqVO.yhmc!=''">
             and rdyh_id = #{pageReqVO.yhmc}
        </if>
        group by KQ_ID
        ) t3 on t3.KQ_ID=ck.id
        <where>
            cd.qyxz!='10' and cd.DELETED=0
            <choose>
                <when test=" (pageReqVO.cflx!=null and pageReqVO.cflx!='')
                         or (pageReqVO.cfzt!=null and pageReqVO.cfzt!='')
                         or (pageReqVO.cfStart!=null and pageReqVO.cfStart !='')
                         or (pageReqVO.cfEnd!=null and pageReqVO.cfEnd !='') ">
                    and t1.cfnum !=0
                </when>
                <otherwise>
                   and  (t1.cfnum !=0  or t3.yxcfnum !=0)
                </otherwise>
            </choose>
            <if test="pageReqVO.kqStart!=null and pageReqVO.kqEnd!=null">
                and IFNULL(t1.sjcrNum, 0) between #{pageReqVO.kqStart} and #{pageReqVO.kqEnd}
            </if>
            <if test="pageReqVO.kqStart!=null and pageReqVO.kqEnd==null ">
                and t1.sjcrNum <![CDATA[>=]]>  #{pageReqVO.kqStart}
            </if>
            <if test="pageReqVO.kqStart==null  and pageReqVO.kqEnd!=null">
                and t1.sjcrNum <![CDATA[<=]]>  #{pageReqVO.kqEnd}
            </if>
            <if test="pageReqVO.dwmc!=null and pageReqVO.dwmc!=''">
                and cd.DWMC like #{pageReqVO.dwmc}
            </if>
            <if test="pageReqVO.areaShi!=null and pageReqVO.areaShi!=''">
               and ck.SHI = #{pageReqVO.areaShi}
            </if>
            <if test="pageReqVO.areaSheng!=null and pageReqVO.areaSheng!=''">
                and ck.SHENG = #{pageReqVO.areaSheng}
            </if>
            <if test="pageReqVO.areaXian!=null and pageReqVO.areaXian!=''">
                and ck.XIAN = #{pageReqVO.areaXian}
            </if>
        </where>
        order by cd.id desc
    </select>

    <select id="getStoreListForNation" parameterType="com.unis.module.coding.controller.admin.delivery.vo.StorePageReqVO" resultType="com.unis.module.coding.controller.admin.delivery.vo.StoreRespVO">
        select
                cc.ID  ,
                cc.KQMC,
                cc.CAMC,
                cc.cfmc,
                cc.CAZT,
                cc.CFLX,
                TRUNC(cc.SJCR)  sjcr
        from  CODING_CAXX cc
        join CODING_FMXX cf on cc.id=cf.BASE_CAXX_ID
        <where>
            cf.deleted=0
            and cf.lsxz='000'
            and cc.KQID=#{pageReqVO.kqId}
            <if test="pageReqVO.cflx!=null and pageReqVO.cflx!=''">
                        and cc.CFLX=#{pageReqVO.cflx}
            </if>
            <if test="pageReqVO.cfzt!=null and pageReqVO.cfzt!=''">
                        and cc.CAZT=#{pageReqVO.cfzt}
            </if>
            <if test="pageReqVO.cfStart!=null and pageReqVO.cfStart!='' and pageReqVO.cfEnd!=null and pageReqVO.cfEnd!=''">
                        and cc.SJCR between #{pageReqVO.cfStart} and #{pageReqVO.cfEnd}
            </if>
            <if test="(pageReqVO.cfStart == null or pageReqVO.cfStart == '') and pageReqVO.cfEnd != null and pageReqVO.cfEnd != ''">
                        and cc.SJCR <![CDATA[<=]]>  #{pageReqVO.cfEnd}
            </if>
            <if test=" pageReqVO.cfStart!=null and pageReqVO.cfStart!='' and (pageReqVO.cfEnd == null or pageReqVO.cfEnd == '') ">
                        and cc.SJCR <![CDATA[>=]]>  #{pageReqVO.cfStart}
            </if>
        </where>
        order by cc.KQID desc
    </select>

<delete id="deleteByDwId" >
    DELETE FROM DELIVERY_DWXX  where id = #{id}
</delete>



</mapper>