<?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.caxx.CaxxMapper">
    <select id="getDeliverySparePage" parameterType="com.unis.module.coding.controller.admin.delivery.vo.SparePageReqVO" resultType="com.unis.module.coding.controller.admin.delivery.vo.SpareRespVO">
        select
            ck.id kqId,
            cd.id deptId,
            t2.cwId,
            ck.sheng_name SHENG,
            ck.shi_name  SHI,
            ck.xian_name XIAN,
            cd.DWMC,
            ck.KQMC,
            ifnull(t1.yxcfnum, 0) yxcfnum,
            ifnull(t2.SYNDZZC, 0) syndzzc,
            ifnull(t2.JLR, 0) jlr,
            ck.KQFZR kqfzr,
            ck.LXDH lxdh
        from  DELIVERY_DWXX cd
        join DELIVERY_KQXX ck on ck.DWID=cd.id
        inner join
        (
                select
                        KQID,
                        count(id) yxcfnum
                from DELIVERY_CAXX
                where deleted=0
                group by  KQID
                having  count(id)>0
        ) t1 on t1.KQID=ck.id
        left join
        (
                select
                        cx.DEPT_ID,
                        cx.id cwId,
                        cx.SYNDZZC,
                        cy.JLR
                from   DELIVERY_CWXX  cx
                JOIN
                  (
                   SELECT d.CWXX_ID,d.JLR
                   FROM DELIVERY_CWXX_YEAR d
                   INNER 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.DEPT_ID=cd.id
        <where>
            cd.qyxz!='10'
            and cd.DELETED=0
            <if test="pageReqVO.dwmc!=null and pageReqVO.dwmc!=''">
                and cd.DWMC like #{pageReqVO.dwmc}
            </if>
            <if test="pageReqVO.kqmc!=null and pageReqVO.kqmc!=''">
                and ck.KQMC like #{pageReqVO.kqmc}
            </if>
            <if test="pageReqVO.shengCode!=null and pageReqVO.shengCode!=''">
                and ck.SHENG = #{pageReqVO.shengCode}
            </if>
            <if test="pageReqVO.shiCode!=null and pageReqVO.shiCode!=''">
                and ck.SHI = #{pageReqVO.shiCode}
            </if>
            <if test="pageReqVO.xianCode!=null and pageReqVO.xianCode!=''">
                and ck.XIAN = #{pageReqVO.xianCode}
            </if>
        </where>
        order by cd.id desc
    </select>

    <select id="selectWrapperPageForSheng"
                     resultType="com.unis.module.coding.controller.admin.delivery.vo.SpareRespVO">
        select
            ck.id kqId,
            cd.id deptId,
            t2.cwId,
            ck.sheng_name SHENG,
            ck.shi_name  SHI,
            ck.xian_name XIAN,
            cd.DWMC,
            ck.KQMC,
            ifnull(t1.yxcfnum, 0) yxcfnum,
            ifnull(t2.SYNDZZC, 0) syndzzc,
            ifnull(t2.JLR, 0) jlr,
            ck.KQFZR kqfzr,
            ck.LXDH lxdh,
            ifnull(t1.HYRY_NAME, '-') hyry_name,
            t1.hy_state,
            t1.file_ids
        from DELIVERY_DWXX cd
        join DELIVERY_KQXX ck on ck.DWID=cd.id
        join
        (
                select
                        KQ_ID,
                        count(CA_ID) yxcfnum,
                        HYRY_NAME,
                        hy_state,
                        file_ids
                from DELIVERY_PROVINCE
                where DELETED=0
                group by  KQ_ID
                having  count(CA_ID)>0
        ) t1 on t1.KQ_ID=ck.id
        left join
        (
                select
                        cx.DEPT_ID,
                        cx.id cwId,
                        cx.SYNDZZC,
                        cy.JLR
                from   DELIVERY_CWXX  cx
                JOIN
                  (
                   SELECT d.CWXX_ID,d.JLR
                   FROM DELIVERY_CWXX_YEAR d
                   INNER 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.DEPT_ID=cd.id
        <where>
            cd.qyxz!='10' and cd.DELETED=0
            <if test="pageReqVO.dwmc!=null and pageReqVO.dwmc!=''">
                and cd.DWMC like #{pageReqVO.dwmc}
            </if>
            <if test="pageReqVO.kqmc!=null and pageReqVO.kqmc!=''">
                and ck.KQMC like #{pageReqVO.kqmc}
            </if>
            <if test="pageReqVO.shengCode!=null and pageReqVO.shengCode!=''">
                and ck.SHENG = #{pageReqVO.shengCode}
            </if>
            <if test="pageReqVO.shiCode!=null and pageReqVO.shiCode!=''">
                and ck.SHI = #{pageReqVO.shiCode}
            </if>
            <if test="pageReqVO.xianCode!=null and pageReqVO.xianCode!=''">
                and ck.XIAN = #{pageReqVO.xianCode}
            </if>
        </where>
        order by cd.id desc
    </select>

    <select id="selectWrapperPageForBank"
                     resultType="com.unis.module.coding.controller.admin.delivery.vo.SpareRespVO">
        select
            ck.id kqId,
            cd.id deptId,
            ck.sheng_name SHENG,
            ck.shi_name  SHI,
            ck.xian_name XIAN,
            cd.DWMC,
            ck.KQMC,
            ifnull(t1.yxcfnum, 0) yxcfnum,
            ck.KQFZR kqfzr,
            ck.LXDH lxdh,
            ifnull(t1.HYRY_NAME, '-') hyry_name,
            t1.hy_state,
            t1.rdyh_name
        from  DELIVERY_DWXX cd
        join DELIVERY_KQXX ck on ck.DWID=cd.id
        join
        (
                select
                        KQ_ID,
                        count(CA_ID) yxcfnum,
                        HYRY_NAME,
                        hy_state,
                        LISTAGG(DISTINCT rdyh_name, ',') rdyh_name
                from DELIVERY_BANK
                where  DELETED=0  and rdyh_id = #{pageReqVO.yhmc}
                group by  KQ_ID
                having  count(CA_ID)>0
        ) t1 on t1.KQ_ID=ck.id
        <where>
            cd.qyxz!='10' and cd.DELETED=0
            <if test="pageReqVO.dwmc!=null and pageReqVO.dwmc!=''">
                and cd.DWMC like #{pageReqVO.dwmc}
            </if>
            <if test="pageReqVO.kqmc!=null and pageReqVO.kqmc!=''">
                and ck.KQMC like #{pageReqVO.kqmc}
            </if>
            <if test="pageReqVO.shengCode!=null and pageReqVO.shengCode!=''">
                and ck.SHENG = #{pageReqVO.shengCode}
            </if>
            <if test="pageReqVO.shiCode!=null and pageReqVO.shiCode!=''">
                and ck.SHI = #{pageReqVO.shiCode}
            </if>
            <if test="pageReqVO.xianCode!=null and pageReqVO.xianCode!=''">
                and ck.XIAN = #{pageReqVO.xianCode}
            </if>
        </where>
        order by cd.id desc
     </select>

    <delete id="deleteByIds">
    DELETE FROM DELIVERY_CAXX
    WHERE id IN
    <foreach item="id" collection="ids" open="(" separator="," close=")">
        #{id}
    </foreach>
    </delete>

    <delete id="deleteListByKqId">
     DELETE FROM DELIVERY_CAXX  where  KQID = #{kqId}
    </delete>

    <select id="getIdentifySparePage"
        resultType="com.unis.module.coding.controller.admin.delivery.vo.SpareRespVO">
       select
            ck.id kqId,
            cd.id deptId,
            ck.sheng_name SHENG,
            ck.shi_name  SHI,
            ck.xian_name XIAN,
            cd.DWMC,
            ck.KQMC,
            ck.KQFZR kqfzr,
            ck.LXDH lxdh,
            ifnull(t1.yxcfnum, 0) yxcfnum,
            ifnull(t2.yhrdnum, 0) yhrdnum,
            t2.rdyh_name
        from DELIVERY_DWXX cd
        join DELIVERY_KQXX ck on ck.DWID=cd.id
        join
        (
                select
                        KQID,
                        count(ID) yxcfnum
                from DELIVERY_CAXX
                where DELETED=0
                group by  KQID
                having count(ID)>0
        ) t1 on t1.KQID=ck.id
        join
        (
                select
                        KQ_ID,
                        count( distinct CA_ID) yhrdnum,
                        LISTAGG(DISTINCT rdyh_name, ',') rdyh_name
                from DELIVERY_BANK
                where  DELETED=0 and HY_STATE=1
                <if test="pageReqVO.yhmc!=null and pageReqVO.yhmc!=''">
                   and rdyh_id = #{pageReqVO.yhmc}
                </if>
                group by  KQ_ID
                having  count(CA_ID)>0
        )  t2 on t2.KQ_ID=ck.id
        <where>
            cd.qyxz!='10'
            and cd.DELETED=0
            <if test="pageReqVO.dwmc!=null and pageReqVO.dwmc!=''">
                and cd.DWMC like #{pageReqVO.dwmc}
            </if>
            <if test="pageReqVO.kqmc!=null and pageReqVO.kqmc!=''">
                and ck.KQMC like #{pageReqVO.kqmc}
            </if>
            <if test="pageReqVO.shengCode!=null and pageReqVO.shengCode!=''">
                and ck.SHENG = #{pageReqVO.shengCode}
            </if>
            <if test="pageReqVO.shiCode!=null and pageReqVO.shiCode!=''">
                and ck.SHI = #{pageReqVO.shiCode}
            </if>
            <if test="pageReqVO.xianCode!=null and pageReqVO.xianCode!=''">
                and ck.XIAN = #{pageReqVO.xianCode}
            </if>
        </where>
        order by cd.id desc
    </select>

    <select id="getIdentifyPageForBank"
                resultType="com.unis.module.coding.controller.admin.delivery.vo.SpareRespVO">
     select
            ck.id kqId,
            cd.id deptId,
            t2.cwId,
            ck.sheng_name SHENG,
            ck.shi_name  SHI,
            ck.xian_name XIAN,
            cd.DWMC,
            ck.KQMC,
            ifnull(t2.SYNDZZC, 0) syndzzc,
            ifnull(t2.JLR, 0) jlr,
            ck.KQFZR kqfzr,
            ck.LXDH lxdh,
            ifnull(t1.yhrdnum, 0) yhrdnum,
            t1.rdyh_name
        from DELIVERY_DWXX cd
        join DELIVERY_KQXX ck on ck.DWID=cd.id
        join
        (
                select
                        KQ_ID,
                        count(CA_ID) yhrdnum,
                        LISTAGG(DISTINCT rdyh_name, ',') rdyh_name
                from DELIVERY_BANK
                where  DELETED=0  and  rdyh_id = #{pageReqVO.yhmc}  and HY_STATE=1
                group by  KQ_ID
                having  count(CA_ID)>0
        )  t1 on t1.KQ_ID=ck.id
        left join
        (
                select
                        cx.DEPT_ID,
                        cx.id cwId,
                        cx.SYNDZZC,
                        cy.JLR
                from   DELIVERY_CWXX  cx
                JOIN
                  (
                   SELECT d.CWXX_ID,d.JLR
                   FROM DELIVERY_CWXX_YEAR d
                   INNER 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.DEPT_ID=cd.id
        <where>
            cd.qyxz!='10'and cd.DELETED=0
            <if test="pageReqVO.dwmc!=null and pageReqVO.dwmc!=''">
                and cd.DWMC like #{pageReqVO.dwmc}
            </if>
            <if test="pageReqVO.kqmc!=null and pageReqVO.kqmc!=''">
                and ck.KQMC like #{pageReqVO.kqmc}
            </if>
            <if test="pageReqVO.shengCode!=null and pageReqVO.shengCode!=''">
                and ck.SHENG = #{pageReqVO.shengCode}
            </if>
            <if test="pageReqVO.shiCode!=null and pageReqVO.shiCode!=''">
                and ck.SHI = #{pageReqVO.shiCode}
            </if>
            <if test="pageReqVO.xianCode!=null and pageReqVO.xianCode!=''">
                and ck.XIAN = #{pageReqVO.xianCode}
            </if>
        </where>
        order by cd.id desc
    </select>

    <select id="getIdentifyPageForSheng"
                resultType="com.unis.module.coding.controller.admin.delivery.vo.SpareRespVO">
     select   distinct
            ck.id kqId,
            cd.id deptId,
            ck.sheng_name SHENG,
            ck.shi_name  SHI,
            ck.xian_name XIAN,
            cd.DWMC,
            ck.KQMC,
            ck.KQFZR kqfzr,
            ck.LXDH lxdh,
            ifnull(t1.yxcfnum, 0) yxcfnum,
            ifnull(t2.yhrdnum, 0) yhrdnum,
            t2.rdyh_name
        from DELIVERY_DWXX cd
        join DELIVERY_KQXX ck on ck.DWID=cd.id
        join
        (
                select
                        KQ_ID,
                        count(CA_ID) yxcfnum
                from DELIVERY_PROVINCE
                where DELETED=0
                group by  KQ_ID
                having  count(CA_ID)>0
        ) t1 on t1.KQ_ID=ck.id
        join
        (
                select
                        KQ_ID,
                        count(distinct CA_ID) yhrdnum,
                        LISTAGG(DISTINCT rdyh_name, ',') rdyh_name
                from DELIVERY_BANK
                where  DELETED=0 and HY_STATE=1
                <if test="pageReqVO.yhmc!=null and pageReqVO.yhmc!=''">
                   and rdyh_id = #{pageReqVO.yhmc}
                </if>
                group by  KQ_ID
                having  count(CA_ID)>0
        )  t2 on t2.KQ_ID=ck.id
        <where>
            cd.qyxz!='10' and cd.DELETED=0
            <if test="pageReqVO.dwmc!=null and pageReqVO.dwmc!=''">
                and cd.DWMC like #{pageReqVO.dwmc}
            </if>
            <if test="pageReqVO.kqmc!=null and pageReqVO.kqmc!=''">
                and ck.KQMC like #{pageReqVO.kqmc}
            </if>
            <if test="pageReqVO.shengCode!=null and pageReqVO.shengCode!=''">
                and ck.SHENG = #{pageReqVO.shengCode}
            </if>
            <if test="pageReqVO.shiCode!=null and pageReqVO.shiCode!=''">
                and ck.SHI = #{pageReqVO.shiCode}
            </if>
            <if test="pageReqVO.xianCode!=null and pageReqVO.xianCode!=''">
                and ck.XIAN = #{pageReqVO.xianCode}
            </if>
        </where>
        order by cd.id desc
    </select>

    <select id="selectListForIdentify"
            resultType="com.unis.module.coding.controller.admin.delivery.vo.StoreRespVO">
     select
                cc.ID,
                cc.KQMC,
                cc.CAMC,
                cc.CFLX,
                cc.CAZT,
                TRUNC(cc.SJCR) sjcr,
                LISTAGG(DISTINCT bk.RDYH_NAME, ',') rdyh_name
        from DELIVERY_CAXX cc
        join DELIVERY_BANK bk on cc.id=bk.CA_ID
        <where>
            cc.deleted=0 and bk.deleted=0  and bk.HY_STATE=1
            <if test="kqId!=null and kqId!=''">
                and cc.KQID=#{kqId}
                and bk.KQ_ID=#{kqId}
            </if>
            <if test="yhmc!=null and yhmc!=''">
               and bk.RDYH_ID=#{yhmc}
            </if>
        </where>
        group  by cc.ID
        order by cc.KQID desc
     </select>
     <select id="selectListForSelect"
        resultType="com.unis.module.coding.controller.admin.delivery.vo.StoreRespVO">
        select
                cc.ID,
                cc.KQMC,
                cc.CAMC,
                cc.CFLX,
                cc.CAZT,
                TRUNC(cc.SJCR) sjcr,
                t1.RDYH_NAME,
                t1.CCZT,
                t1.NICKNAME,
                t1.MOBILE
        from DELIVERY_CAXX cc
        join
        (
        select
              us.CA_ID,
              us.CCZT,
              ur.NICKNAME,
              LISTAGG(DISTINCT bk.RDYH_NAME, ',') rdyh_name,
              ur.MOBILE
        from  DELIVERY_USERS  us
        join  DELIVERY_BANK   bk   on us.KQ_ID=bk.KQ_ID   and  us.CA_ID=bk.CA_ID
        left join  SYSTEM_USERS  ur  on ur.id=us.USER_ID and ur.deleted=0
        where us.deleted=0 and bk.deleted=0 and bk.HY_STATE=1
            <if test="kqId!=null and kqId!=''">
                and bk.KQ_ID=#{kqId}
                and us.KQ_ID=#{kqId}
            </if>
            <if test="yhmc!=null and yhmc!=''">
               and bk.RDYH_ID=#{yhmc}
            </if>
        group  by us.CA_ID
        ) t1 on cc.id=t1.CA_ID
        <where>
            cc.deleted=0
            <if test="kqId!=null and kqId!=''">
                and cc.KQID=#{kqId}
            </if>
        </where>
        order by cc.KQID desc
     </select>

    <select id="getSelectSparePage"
                      resultType="com.unis.module.coding.controller.admin.delivery.vo.SpareRespVO">
    select
            ck.id kqId,
            cd.id deptId,
            ck.sheng_name SHENG,
            ck.shi_name  SHI,
            ck.xian_name XIAN,
            cd.DWMC,
            ck.KQMC,
            ck.KQFZR kqfzr,
            ck.LXDH lxdh,
            ifnull(t1.yhrdnum, 0) yhrdnum,
            ifnull(t2.xdnum, 0) xdnum,
            t1.rdyh_name
        from DELIVERY_DWXX cd
        join DELIVERY_KQXX ck on ck.DWID=cd.id
        join
        (
                select
                        KQ_ID,
                        count(distinct CA_ID) yhrdnum,
                        LISTAGG(DISTINCT rdyh_name, ',') rdyh_name
                from DELIVERY_BANK
                where  DELETED=0 and HY_STATE=1
                <if test="pageReqVO.yhmc!=null and pageReqVO.yhmc!=''">
                     and rdyh_id = #{pageReqVO.yhmc}
                 </if>
                group by  KQ_ID
                having  count(CA_ID)>0
        )  t1 on t1.KQ_ID=ck.id
       join
        (
                select
                        KQ_ID,
                        count(CA_ID) xdnum
                from DELIVERY_USERS
                where DELETED=0
                <if test="pageReqVO.yhmc!=null and pageReqVO.yhmc!=''">
                     and rdyh_id = #{pageReqVO.yhmc}
                </if>
                group by  KQ_ID
                having  count(CA_ID)>0
        ) t2 on t2.KQ_ID=ck.id
        <where>
            cd.qyxz!='10' and cd.DELETED=0
            <if test="pageReqVO.dwmc!=null and pageReqVO.dwmc!=''">
                and cd.DWMC like #{pageReqVO.dwmc}
            </if>
            <if test="pageReqVO.kqmc!=null and pageReqVO.kqmc!=''">
                and ck.KQMC like #{pageReqVO.kqmc}
            </if>
            <if test="pageReqVO.shengCode!=null and pageReqVO.shengCode!=''">
                and ck.SHENG = #{pageReqVO.shengCode}
            </if>
            <if test="pageReqVO.shiCode!=null and pageReqVO.shiCode!=''">
                and ck.SHI = #{pageReqVO.shiCode}
            </if>
            <if test="pageReqVO.xianCode!=null and pageReqVO.xianCode!=''">
                and ck.XIAN = #{pageReqVO.xianCode}
            </if>
        </where>
        order by cd.id desc
    </select>

    <select id="getSelectPageForBank"
                     resultType="com.unis.module.coding.controller.admin.delivery.vo.SpareRespVO">
         select
            ck.id kqId,
            cd.id deptId,
            ck.sheng_name SHENG,
            ck.shi_name  SHI,
            ck.xian_name XIAN,
            cd.DWMC,
            ck.KQMC,
            ck.KQFZR kqfzr,
            ck.LXDH lxdh,
            ifnull(t3.SYNDZZC, 0) syndzzc,
            ifnull(t3.JLR, 0) jlr,
            ifnull(t2.xdnum, 0) xdnum,
            t1.rdyh_name
        from DELIVERY_DWXX cd
        join DELIVERY_KQXX ck on ck.DWID=cd.id
        join
        (
                select
                        KQ_ID,
                        LISTAGG(DISTINCT rdyh_name, ',') rdyh_name
                from DELIVERY_BANK
                where  DELETED=0 and HY_STATE=1
                 <if test="pageReqVO.yhmc!=null and pageReqVO.yhmc!=''">
                     and rdyh_id = #{pageReqVO.yhmc}
                 </if>
                group by  KQ_ID
                having  count(CA_ID)>0
        )  t1 on t1.KQ_ID=ck.id
       join
        (
                select
                        KQ_ID,
                        count(CA_ID) xdnum
                from DELIVERY_USERS
                where DELETED=0
                <if test="pageReqVO.yhmc!=null and pageReqVO.yhmc!=''">
                     and rdyh_id = #{pageReqVO.yhmc}
                </if>
                group by  KQ_ID
                having  count(CA_ID)>0
        ) t2 on t2.KQ_ID=ck.id
        left join
        (
                select
                        cx.DEPT_ID,
                        cx.id cwId,
                        cx.SYNDZZC,
                        cy.JLR
                from   DELIVERY_CWXX  cx
                JOIN
                  (
                   SELECT d.CWXX_ID,d.JLR
                   FROM DELIVERY_CWXX_YEAR d
                   INNER 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
        ) t3 on t3.DEPT_ID=cd.id
        <where>
            cd.qyxz!='10' and cd.DELETED=0
            <if test="pageReqVO.dwmc!=null and pageReqVO.dwmc!=''">
                and cd.DWMC like #{pageReqVO.dwmc}
            </if>
            <if test="pageReqVO.kqmc!=null and pageReqVO.kqmc!=''">
                and ck.KQMC like #{pageReqVO.kqmc}
            </if>
            <if test="pageReqVO.shengCode!=null and pageReqVO.shengCode!=''">
                and ck.SHENG = #{pageReqVO.shengCode}
            </if>
            <if test="pageReqVO.shiCode!=null and pageReqVO.shiCode!=''">
                and ck.SHI = #{pageReqVO.shiCode}
            </if>
            <if test="pageReqVO.xianCode!=null and pageReqVO.xianCode!=''">
                and ck.XIAN = #{pageReqVO.xianCode}
            </if>
        </where>
        order by cd.id desc


     </select>

    <select id="getSelectPageForSheng"
                      resultType="com.unis.module.coding.controller.admin.delivery.vo.SpareRespVO">
    select
            ck.id kqId,
            cd.id deptId,
            ck.sheng_name SHENG,
            ck.shi_name  SHI,
            ck.xian_name XIAN,
            cd.DWMC,
            ck.KQMC,
            ck.KQFZR kqfzr,
            ck.LXDH lxdh,
            ifnull(t1.yhrdnum, 0) yhrdnum,
            ifnull(t2.xdnum, 0) xdnum,
            t1.rdyh_name
        from DELIVERY_DWXX cd
        join DELIVERY_KQXX ck on ck.DWID=cd.id
        join
        (
                select
                        KQ_ID,
                        count(distinct CA_ID) yhrdnum,
                        LISTAGG(DISTINCT rdyh_name, ',') rdyh_name
                from DELIVERY_BANK
                where  DELETED=0 and HY_STATE=1
                <if test="pageReqVO.yhmc!=null and pageReqVO.yhmc!=''">
                     and rdyh_id = #{pageReqVO.yhmc}
                 </if>
                group by  KQ_ID
                having  count(CA_ID)>0
        )  t1 on t1.KQ_ID=ck.id
       join
        (
                select
                        KQ_ID,
                        count(CA_ID) xdnum
                from DELIVERY_USERS
                where DELETED=0
                <if test="pageReqVO.yhmc!=null and pageReqVO.yhmc!=''">
                     and rdyh_id = #{pageReqVO.yhmc}
                </if>
                group by  KQ_ID
                having  count(CA_ID)>0
        ) t2 on t2.KQ_ID=ck.id
        <where>
            cd.qyxz!='10' and cd.DELETED=0
            <if test="pageReqVO.dwmc!=null and pageReqVO.dwmc!=''">
                and cd.DWMC like #{pageReqVO.dwmc}
            </if>
            <if test="pageReqVO.kqmc!=null and pageReqVO.kqmc!=''">
                and ck.KQMC like #{pageReqVO.kqmc}
            </if>
            <if test="pageReqVO.shengCode!=null and pageReqVO.shengCode!=''">
                and ck.SHENG = #{pageReqVO.shengCode}
            </if>
            <if test="pageReqVO.shiCode!=null and pageReqVO.shiCode!=''">
                and ck.SHI = #{pageReqVO.shiCode}
            </if>
            <if test="pageReqVO.xianCode!=null and pageReqVO.xianCode!=''">
                and ck.XIAN = #{pageReqVO.xianCode}
            </if>
        </where>
        order by cd.id desc
    </select>


</mapper>