<?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="org.springblade.modules.csInfo.mapper.CsInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="infoResultMap" type="org.springblade.modules.csInfo.entity.CsInfoEntity">
        <result column="id" property="id"/>
        <result column="csmc" property="csmc"/>
        <result column="fzr" property="fzr"/>
        <result column="dz" property="dz"/>
        <result column="dwlb" property="dwlb"/>
        <result column="jzmj" property="jzmj"/>
        <result column="jzcs" property="jzcs"/>
        <result column="gd" property="gd"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <select id="selectInfoPage" resultType="org.springblade.modules.csInfo.vo.CsInfoVO">
        SELECT
            ci.id,
            ci.csmc,
            ci.fzr,
            ci.fzr_dh,
            ci.xffzr,
            ci.xffzrlxdh,
            ci.xffzrsfzh,
            ci.dwlb,
            ci.tyxydm,
            ci.sssq_bm,
            ci.jg_id,
            sq.dept_name AS sssqName,
            ci.sspcs_mc AS sspcsName,
            fj.dept_name AS ssfjName,
            ci.dz,
            ci.jzmj,
            ci.jzcs,
            ci.gd,
            ci.dwxl,
            ci.zczb,
            ci.clrq,
            ci.jyqx,
            ci.jyfw,
            ci.djjg,
            ci.djrq,
            ci.fwlx,
            ci.ldphxlh,
            ci.yyzt,
            ci.create_time,
            case when ci.yyzt = 0 then '正常' when ci.yyzt = 1 then '歇业' when ci.yyzt = 2 then '停业' when ci.yyzt = 3 then '重复数据' end as yyztName,
            jc.zgqx,
            jc.create_time AS time,
            sj.dept_name AS sssjName,
            jc.yjzt,
            CASE WHEN jc.zgzt IS NULL THEN '-1' :: INTEGER ELSE jc.zgzt END AS zgzt,
            CASE WHEN jc.zgzt IS NULL THEN '未检查' :: TEXT WHEN jc.zgzt = 0 THEN '不需要整改' :: TEXT WHEN jc.zgzt = 1 THEN '当场整改' :: TEXT WHEN jc.zgzt = 2 THEN '限期整改' :: TEXT WHEN jc.zgzt = 3 THEN '拒不整改' :: TEXT WHEN jc.zgzt = 4 THEN '逾期未整改' :: TEXT WHEN jc.zgzt = 5 THEN '重大火灾风险隐患' :: TEXT END AS jczt,
            ld.sqjcwhmc,
            ld.jlxmc AS jlxxqzdzrcmc,
            ld.mlph,
            ld.ldph,
            CASE WHEN cm.id IS NULL THEN '否' ELSE '是' END AS sfkh
        FROM
            jdrh.cs_info ci
        <if test="null != params.beginTime and '' != params.beginTime and null != params.endTime and '' != params.endTime">
        LEFT JOIN (
            SELECT
                ID ,
                cs_id,
                create_time,
                zgqx,
                yjzt,
                zgzt,
                ROW_NUMBER ( ) OVER ( PARTITION BY cs_id ORDER BY create_time DESC ) rn
            FROM
                jdrh.csjc_info
            WHERE
                create_time :: DATE BETWEEN #{params.beginTime} :: DATE AND #{params.endTime} :: DATE
                ) jc ON jc.cs_id = ci.id AND jc.rn = 1
        </if>
        <if test="null == params.beginTime or null == params.endTime">
        LEFT JOIN jdrh.csjc_info jc ON jc.id = ci.jc_id
        </if>
        LEFT JOIN jdrh.blade_dept sq ON sq.id = ci.sssq_bm
        LEFT JOIN jdrh.blade_dept pcs ON CASE WHEN sq.dept_category = 6 THEN pcs.id = sq.id ELSE pcs.id = sq.parent_id END
        LEFT JOIN jdrh.blade_dept fj ON fj.id = pcs.parent_id
        LEFT JOIN jdrh.blade_dept sj ON fj.parent_id = sj.id
        LEFT JOIN sspt.rk_ldphxx ld ON ld.xlh = ci.ldphxlh
        LEFT JOIN jdrh.cs_fm cm ON ci.id = cm.id
        WHERE
            ci.is_deleted = 0
        AND (((sq.id :: text = any(string_to_array(#{params.deptId},','))) OR sq.ancestors LIKE '%' || #{params.deptId} || '%') OR ci.sssq_bm = #{params.gajgdm} :: int8)
        <if test="params.yyztName != null and params.yyztName != ''">
            AND ci.yyzt :: text = #{params.yyztName}
        </if>
        <if test="'考核场所' == params.sfkh">
            AND cm.id is not null
        </if>
        <if test="'非考核场所' == params.sfkh">
            AND cm.id is null
        </if>
        <if test="null != params.csmc and '' != params.csmc">
            AND ci.csmc LIKE '%' || #{params.csmc} || '%'
        </if>
        <if test="null != params.cjr">
            AND ci.cjr = #{params.cjr} :: text
        </if>
        <if test="null != params.fzr and '' != params.fzr">
            AND ci.fzr LIKE '%' || #{params.fzr} || '%'
        </if>
        <if test="null != params.dz and '' != params.dz">
            AND ci.dz LIKE '%' || #{params.dz} || '%'
        </if>
        <if test="null != params.dwlb and '' != params.dwlb">
            AND ci.dwlb LIKE '%' || #{params.dwlb} || '%'
        </if>
        <if test="null != params.zgzt and '' != params.zgzt">
            AND ci.zgzt :: text = #{params.zgzt} :: text
            AND ci.dwlb = #{params.dwlb}
        </if>
        <if test="null != params.dwxl and '' != params.dwxl">
            AND ci.dwxl = #{params.dwxl}
        </if>
        <if test="null != params.fwlx and '' != params.fwlx">
            AND ci.fwlx = #{params.fwlx}
        </if>
        <if test="null != params.xffzr and '' != params.xffzr">
            AND ci.xffzr LIKE '%' || #{params.xffzr} || '%'
        </if>
        <if test="null != params.ssfjName and '' != params.ssfjName">
            AND fj.id = #{params.ssfjName} :: int8
        </if>
        <if test="null != params.sspcsName and '' != params.sspcsName">
            AND pcs.id = #{params.sspcsName} :: int8
        </if>
        <if test="null != params.sssqName and '' != params.sssqName">
            AND sq.id = #{params.sssqName} :: int8
        </if>
        <if test="null != params.sssjName and '' != params.sssjName">
            AND sj.id = #{params.sssjName} :: int8
        </if>
        <if test="null != params.jczt and '' != params.jczt and '-1' != params.jczt">
            AND jc.zgzt :: text = #{params.jczt} :: text
        </if>
        <if test="'-1' == params.jczt">
            AND jc.zgzt is null
        </if>
        <if test="null != params.ldphxlh and '' != params.ldphxlh">
            AND ci.ldphxlh = #{params.ldphxlh} :: int8
        </if>
        <if test="null != params.ld and 1 == params.ld">
            AND ci.ldphxlh IS NULL
        </if>
        <if test="null != params.yyzt and '' != params.jczt">
            AND ci.yyzt :: text = #{params.yyzt}
        </if>
        <if test="null != params.startTime and '' != params.startTime and null != params.endTime and '' != params.endTime">
            AND jc.create_time :: text between #{params.startTime} and #{params.endTime}
        </if>
        ORDER BY
            jc.create_time DESC,
            ci.csmc
    </select>

    <select id="selectInfoPagePublic" resultType="org.springblade.modules.csInfo.vo.CsInfoVO">
        SELECT
        a.id,
        csmc,
        dz,
        dwlb,
        xffzr,
        xffzrlxdh,
        b.name cjr
        FROM
        PUBLIC.cs_info a
        LEFT JOIN PUBLIC.blade_user b ON a.cjr = b.id::text
        WHERE a.is_deleted = '0' and sssq_bm in (select id from public.blade_dept where parent_id::text =
        #{params.sspcsBm})
        AND a.id NOT IN ( SELECT cszj FROM PUBLIC.csrlb where rlsq::text = #{params.sspcsBm} )
        <if test="null != params.csmc and '' != params.csmc">
            AND csmc LIKE '%' || #{params.csmc} || '%'
        </if>
        <if test="null != params.dwlb and '' != params.dwlb">
            AND dwlb = #{params.dwlb}
        </if>
        <if test="null != params.cjr and '' != params.cjr">
            AND b.name LIKE '%' || #{params.cjr} || '%'
        </if>
    </select>
    <select id="selectCsInfoByIdAndPcsbm" resultType="org.springblade.modules.csInfo.entity.CsInfoEntity">
        select * from public.cs_info where is_deleted = 0 and id::text = #{param.id} and sssq_bm in (select id from PUBLIC.blade_dept where parent_id::text = #{param.sspcsBm})
    </select>
    <select id="exportInfo" resultType="org.springblade.modules.csInfo.excel.CsInfoExcel">
        SELECT
        ci.id,
        ci.csmc,
        ci.fzr,
        ci.fzr_dh,
        ci.xffzr,
        ci.xffzrlxdh,
        ci.xffzrsfzh,
        ci.dwlb,
        ci.tyxydm,
        ci.sssq_bm,
        ci.jg_id,
        sq.dept_name AS sssqName,
        ci.sspcs_mc AS sspcsName,
        fj.dept_name AS ssfjName,
        ci.dz,
        ci.jzmj,
        ci.jzcs,
        ci.gd,
        ci.dwxl,
        ci.zczb,
        ci.clrq,
        ci.jyqx,
        ci.jyfw,
        ci.djjg,
        ci.djrq,
        ci.fwlx,
        ci.ldphxlh,
        case
        when ci.yyzt = 0 then '正常营业' :: text
        when ci.yyzt = 1 then '歇业' :: text
        when ci.yyzt = 2 then '停业' :: text
        end as yyzt,
        jc.zgqx,
        jc.create_time AS time,
        sj.dept_name AS sssjName,
        jc.yjzt,
        case when jc.zgzt is null then '-1' :: integer else jc.zgzt end as zgzt,
        case
        when jc.zgzt is null then '未检查' :: text
        when jc.zgzt = 0 then '不需要整改' :: text
        when jc.zgzt = 1 then '当场整改' :: text
        when jc.zgzt = 2 then '限期整改' :: text
        when jc.zgzt = 3 then '拒不整改' :: text
        when jc.zgzt = 4 then '逾期未整改' :: text
        when jc.zgzt = 5 then '重大火灾风险隐患' :: text
        end as jczt,
        ld.sqjcwhmc,
        ld.jlxmc AS jlxxqzdzrcmc,
        ld.mlph,
        ld.ldph,
        case when cm.id is null then '否' else '是' end as sfkh
        FROM
        jdrh.cs_info ci
        LEFT JOIN jdrh.csjc_info jc ON jc.ID = ci.jc_id
        LEFT JOIN jdrh.blade_dept sq ON sq.ID = ci.sssq_bm
        LEFT JOIN jdrh.blade_dept pcs ON case when sq.dept_category = 6 then pcs.id = sq.id else pcs.id = sq.parent_id end
        LEFT JOIN jdrh.blade_dept fj on	fj.id = pcs.parent_id
        LEFT JOIN jdrh.blade_dept sj on	fj.parent_id = sj.id
        LEFT JOIN sspt.rk_ldphxx ld ON ld.xlh = ci.ldphxlh
        LEFT JOIN jdrh.cs_fm cm ON ci.id = cm.id
        WHERE
        ci.is_deleted = 0
        AND (((sq.id :: text = any(string_to_array(#{params.deptId},','))) OR sq.ancestors LIKE '%' || #{params.deptId} || '%') OR ci.sssq_bm = #{params.gajgdm} :: int8)
        <if test="'考核场所' == params.sfkh">
            AND cm.id is not null
        </if>
        <if test="'非考核场所' == params.sfkh">
            AND cm.id is null
        </if>
        <if test="null != params.csmc and '' != params.csmc">
            AND ci.csmc LIKE '%' || #{params.csmc} || '%'
        </if>
        <if test="null != params.cjr">
            AND ci.cjr = #{params.cjr} :: text
        </if>
        <if test="null != params.fzr and '' != params.fzr">
            AND ci.fzr LIKE '%' || #{params.fzr} || '%'
        </if>
        <if test="null != params.dz and '' != params.dz">
            AND ci.dz LIKE '%' || #{params.dz} || '%'
        </if>
        <if test="null != params.dwlb and '' != params.dwlb">
            AND ci.dwlb LIKE '%' || #{params.dwlb} || '%'
        </if>
        <if test="null != params.zgzt and '' != params.zgzt">
            AND ci.zgzt :: text = #{params.zgzt} :: text
            AND ci.dwlb = #{params.dwlb}
        </if>
        <if test="null != params.dwxl and '' != params.dwxl">
            AND ci.dwxl = #{params.dwxl}
        </if>
        <if test="null != params.fwlx and '' != params.fwlx">
            AND ci.fwlx = #{params.fwlx}
        </if>
        <if test="null != params.xffzr and '' != params.xffzr">
            AND ci.xffzr LIKE '%' || #{params.xffzr} || '%'
        </if>
        <if test="null != params.ssfjName and '' != params.ssfjName">
            AND fj.id = #{params.ssfjName} :: int8
        </if>
        <if test="null != params.sspcsName and '' != params.sspcsName">
            AND pcs.id = #{params.sspcsName} :: int8
        </if>
        <if test="null != params.sssqName and '' != params.sssqName">
            AND sq.id = #{params.sssqName} :: int8
        </if>
        <if test="null != params.sssjName and '' != params.sssjName">
            AND sj.id = #{params.sssjName} :: int8
        </if>
        <if test="null != params.jczt and '' != params.jczt and '-1' != params.jczt">
            AND jc.zgzt :: text = #{params.jczt} :: text
        </if>
        <if test="'-1' == params.jczt">
            AND jc.zgzt is null
        </if>
        <if test="null != params.ldphxlh and '' != params.ldphxlh">
            AND ci.ldphxlh = #{params.ldphxlh} :: int8
        </if>
        <if test="null != params.ld and 1 == params.ld">
            AND ci.ldphxlh IS NULL
        </if>
        <if test="null != params.startTime and '' != params.startTime and null != params.endTime and '' != params.endTime">
            AND jc.create_time :: text between #{params.startTime} and #{params.endTime}
        </if>
        ORDER BY
        jc.create_time,
        ci.csmc
    </select>

    <select id="getOverDue" resultType="int">
        SELECT
            COUNT( * )
        FROM
            v_test_app temp
        LEFT JOIN blade_dept sq ON sq.id = temp.sssq_bm
        WHERE
            temp.yh = '未检查'
        AND ((#{params.deptId} LIKE '%' || sq.id || '%') OR (sq.ancestors LIKE '%' || #{params.deptId} || '%'))
    </select>

    <select id="getXqzgTx" resultType="int">
        SELECT
            COUNT(*)
        FROM
            (
                SELECT
                    ROW_NUMBER () OVER ( PARTITION BY ci.csmc ORDER BY csjc.create_time DESC ) maxRow,
                    csjc.zgqx
                FROM
                    cs_info ci
                LEFT JOIN csjc_info csjc ON ci.ID = csjc.cs_id
                LEFT JOIN blade_dept bd ON bd.ID = ci.sssq_bm
                WHERE
                    ci.is_deleted = 0
                AND ((#{params.deptId} LIKE '%' || bd.id || '%') OR (bd.ancestors LIKE '%' || #{params.deptId} || '%'))
            ) temp
        WHERE
            temp.maxRow = 1
        AND ( to_date( TEMP.zgqx :: text, 'yyyy-mm-dd' ) - to_date( now( ) :: text, 'yyyy-mm-dd' ) ) &lt;= 3
        AND ( to_date( TEMP.zgqx :: text, 'yyyy-mm-dd' ) - to_date( now( ) :: text, 'yyyy-mm-dd' ) ) > 0
    </select>

    <select id="selectCsPcs" resultMap="infoResultMap">
        WITH dept_cssl AS (
        SELECT
        dept.dept_name,
        dept.id,
        COUNT(1) AS cssl
        FROM jdrh.cs_info a
        JOIN jdrh.blade_dept dept ON a.sspcs_bm = dept.id
        WHERE a.is_deleted = 0 AND dept.dept_category = '6' AND dept.ancestors like '%' || #{deptId} || '%'
        <if test="csInfo.deptName != null and csInfo.deptName != ''">
            and dept.dept_name like '%' || #{csInfo.deptName} || '%'
        </if>
        <if test="str != null and str != '' and strEnd !=null and strEnd != ''">
            and a.create_time :: text between #{str} and #{strEnd}
        </if>
        GROUP BY dept.id,dept.dept_name
        ), dept_jcsl AS (
        SELECT
        dept.id,
        COUNT(distinct a.*) AS jcsl
        FROM jdrh.cs_info a
        JOIN jdrh.blade_dept dept ON a.sspcs_bm = dept.id
        JOIN jdrh.csjc_info c ON a.id = c.cs_id
        WHERE a.is_deleted = 0 AND dept.dept_category = '6'
        <if test="str != null and str != '' and strEnd !=null and strEnd != ''">
            and a.create_time :: text between #{str} and #{strEnd}
        </if>
        GROUP BY dept.dept_name, dept.id
        )
        SELECT
        dept_cssl.dept_name,
        dept_cssl.id,
        dept_cssl.cssl,
        COALESCE(dept_jcsl.jcsl, 0) as jcsl,
        COALESCE(ROUND((dept_jcsl.jcsl::numeric / dept_cssl.cssl::numeric) * 100, 2), 0) AS wcl
        FROM dept_cssl
        LEFT JOIN dept_jcsl ON dept_cssl.id = dept_jcsl.id
        ORDER BY wcl DESC;
    </select>

    <select id="selectCsSq" resultMap="infoResultMap">
        WITH dept_cssl AS (
        SELECT
        dept.dept_name,
        dept.id,
        COUNT(1) AS cssl
        FROM jdrh.cs_info a
        JOIN jdrh.blade_dept dept ON a.sssq_bm = dept.id
        WHERE a.is_deleted = 0 AND dept.dept_category = '7' AND dept.parent_id = #{id}
        <if test="str != null and str != '' and strEnd !=null and strEnd != ''">
            and a.create_time :: text between #{str} and #{strEnd}
        </if>
        GROUP BY dept.id,dept.dept_name
        ), dept_jcsl AS (
        SELECT
        dept.id,
        COUNT(distinct a.*) AS jcsl
        FROM jdrh.cs_info a
        JOIN jdrh.blade_dept dept ON a.sssq_bm = dept.id
        JOIN jdrh.csjc_info c ON a.id = c.cs_id
        WHERE a.is_deleted = 0 AND dept.dept_category = '7'
        <if test="str != null and str != '' and strEnd !=null and strEnd != ''">
            and a.create_time :: text between #{str} and #{strEnd}
        </if>
        GROUP BY dept.dept_name, dept.id
        )
        SELECT
        dept_cssl.dept_name,
        dept_cssl.id,
        dept_cssl.cssl,
        COALESCE(dept_jcsl.jcsl, 0) as jcsl,
        COALESCE(ROUND((dept_jcsl.jcsl::numeric / dept_cssl.cssl::numeric) * 100, 2), 0) AS wcl
        FROM dept_cssl
        LEFT JOIN dept_jcsl ON dept_cssl.id = dept_jcsl.id
        ORDER BY wcl DESC;
    </select>

    <select id="selectCsxx" resultMap="infoResultMap">
        SELECT
            ci.*,
            CASE WHEN jc.zgzt IS NULL THEN -1 ELSE jc.zgzt END AS zgzt,
            jc.yjzt,
            jc.create_time AS time,
            sq.dept_name AS sssqName,
            pcs.dept_name AS sspcsName
        FROM
            jdrh.cs_info ci
        LEFT JOIN jdrh.blade_dept sq ON sq.id = ci.sssq_bm
        LEFT JOIN jdrh.blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN jdrh.csjc_info jc ON jc.cs_id = ci.id
        WHERE ci.is_deleted = 0 and (ci.sssq_bm = #{csInfo.id} or sq.parent_id = #{csInfo.id})
    </select>

    <select id="selectSqByDeptname" resultMap="infoResultMap">
        WITH dept_cssl AS (
        SELECT
        dept.dept_name,
        dept.id,
        COUNT(1) AS cssl
        FROM jdrh.cs_info a
        JOIN jdrh.blade_dept dept ON a.sssq_bm = dept.id
        WHERE a.is_deleted = 0 AND dept.dept_category = '7' AND dept.ancestors like '%' || #{deptId} || '%'
        <if test="csInfo.deptName != null and csInfo.deptName != ''">
            and dept.dept_name like '%' || #{csInfo.deptName} || '%'
        </if>
        <if test="str != null and str != '' and strEnd !=null and strEnd != ''">
            and a.create_time :: text between #{str} and #{strEnd}
        </if>
        GROUP BY dept.id,dept.dept_name
        ), dept_jcsl AS (
        SELECT
        dept.id,
        COUNT(distinct a.*) AS jcsl
        FROM jdrh.cs_info a
        JOIN jdrh.blade_dept dept ON a.sssq_bm = dept.id
        JOIN jdrh.csjc_info c ON a.id = c.cs_id
        WHERE a.is_deleted = 0 AND dept.dept_category = '7'
        <if test="str != null and str != '' and strEnd !=null and strEnd != ''">
            and a.create_time :: text between #{str} and #{strEnd}
        </if>
        GROUP BY dept.dept_name, dept.id
        )
        SELECT
        dept_cssl.dept_name,
        dept_cssl.id,
        dept_cssl.cssl,
        COALESCE(dept_jcsl.jcsl, 0) as jcsl,
        COALESCE(ROUND((dept_jcsl.jcsl::numeric / dept_cssl.cssl::numeric) * 100, 2), 0) AS wcl
        FROM dept_cssl
        LEFT JOIN dept_jcsl ON dept_cssl.id = dept_jcsl.id
        ORDER BY wcl DESC;
    </select>

    <select id="getParentInfo" resultType="org.springblade.modules.system.vo.DeptAllVO">
        SELECT
            "id社区" AS sqId,
            "dept_name社区名" AS sqMc,
            "id派出所" AS pcsId,
            "dept_name派出所名" AS pcsMc,
            "id分局" AS fjId,
            "dept_name分局名" AS fjMc,
            "id市局" AS sjId,
            "dept_name市局名" AS sjMc
        FROM
            jdrh.v_dept_all
        WHERE
            "id社区" = #{sssqBm}
    </select>

    <select id="getDeptList" resultType="org.springblade.modules.system.entity.Dept">
        SELECT
            id AS value,
            dept_name AS label
        FROM
            blade_dept
        WHERE
            (
            #{params.deptId} LIKE ( '%' || id :: TEXT || '%' )
        OR ( SELECT sq.ancestors FROM blade_dept sq WHERE sq.id :: TEXT = (string_to_array( #{params.deptId}, ',' )) [1] ) LIKE ( '%' || id :: TEXT || '%' )
        OR ( ancestors like ( '%' || (string_to_array( #{params.deptId}, ',' )) [1] || '%' ))
            )
        <if test="params.value != '220000000000'">
            AND parent_id :: text = #{params.value}
        </if>
        AND dept_category :: text = #{params.deptCategory}
    </select>

    <select id="getQsJxcs" resultType="org.springblade.modules.csInfo.excel.QsJxcsExcel">
        SELECT
        ci.csmc,
        ci.dwlb,
        ci.dz,
        ci.fzr,
        ci.fzr_dh,
        ci.xffzr,
        ci.xffzrsfzh,
        ci.xffzrlxdh,
        ci.fwlx,
        ci.jzmj,
        ci.jzcs,
        ci.gd,
        ci.zczb,
        ci.clrq,
        ci.jyqx,
        ci.jyfw,
        ci.djjg,
        ci.djrq,
        ci.sspcs_mc,
        ci.sssq_mc,
        ci.ssfj_mc
        FROM
        cs_info ci
        LEFT JOIN blade_dept sq ON sq.id = ci.sssq_bm
        WHERE
        ci.is_deleted = 0
        AND ((#{params.deptId} LIKE '%' || sq.id || '%') OR (sq.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.ssfjMc and '' != params.ssfjMc">
            AND ci.ssfj_bm :: text LIKE '%' || #{params.ssfjMc} || '%'
        </if>
        <if test="null != params.sspcsMc and '' != params.sspcsMc">
            AND ci.sspcs_bm :: text LIKE '%' || #{params.sspcsMc} || '%'
        </if>
        <if test="null != params.sssqMc and '' != params.sssqMc">
            AND ci.sssq_bm :: text LIKE '%' || #{params.sssqMc} || '%'
        </if>
        ORDER BY
        ci.ssfj_mc,
        ci.sspcs_mc
    </select>

    <select id="exportQsJxcs" resultType="org.springblade.modules.csInfo.excel.QsJxcsExcel">
        SELECT
        ci.csmc,
        ci.dwlb,
        ci.dz,
        ci.fzr,
        ci.fzr_dh,
        ci.xffzr,
        ci.xffzrsfzh,
        ci.xffzrlxdh,
        ci.fwlx,
        ci.jzmj,
        ci.jzcs,
        ci.gd,
        ci.zczb,
        ci.clrq,
        ci.jyqx,
        ci.jyfw,
        ci.djjg,
        ci.djrq,
        ci.sspcs_mc,
        ci.sssq_mc,
        ci.ssfj_mc
        FROM
        cs_info ci
        LEFT JOIN blade_dept sq ON sq.id = ci.sssq_bm
        WHERE
        ci.is_deleted = 0
        AND ((#{params.deptId} LIKE '%' || sq.id || '%') OR (sq.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.ssfjMc and '' != params.ssfjMc">
            AND ci.ssfj_bm :: text LIKE '%' || #{params.ssfjMc} || '%'
        </if>
        <if test="null != params.sspcsMc and '' != params.sspcsMc">
            AND ci.sspcs_bm :: text LIKE '%' || #{params.sspcsMc} || '%'
        </if>
        <if test="null != params.sssqMc and '' != params.sssqMc">
            AND ci.sssq_bm :: text LIKE '%' || #{params.sssqMc} || '%'
        </if>
        ORDER BY
        ci.ssfj_mc,
        ci.sspcs_mc
    </select>

    <select id="getSqTj" resultType="org.springblade.modules.csInfo.excel.MjsjtjExcel">
        SELECT
        fjname,
        pcsname,
        sqname,
        yxs,
        jcs,
        ROUND( jcs :: NUMERIC / CASE yxs WHEN 0 THEN 1 ELSE yxs END :: NUMERIC, 2 ) AS jcl,
        yhs,
        dczgs,
        xqzgs,
        fjs,
        ROUND( fjs :: NUMERIC / CASE xqzgs WHEN 0 THEN 1 ELSE xqzgs END :: NUMERIC, 2 ) AS fjl,
        jbzgs,
        yqwzgs,
        zdhzyhs,
        xzs,
        scs
        FROM
        (
        SELECT
        fjname,
        pcsname,
        sqname,
        SUM ( CASE WHEN create_time BETWEEN begindate AND enddate THEN 1 ELSE 0 END ) xzs,
        SUM ( CASE WHEN is_deleted = 0 THEN 1 ELSE 0 END ) AS yxs,
        SUM ( CASE WHEN update_time BETWEEN begindate AND enddate AND is_deleted = 1 THEN 1 ELSE 0 END ) scs,
        SUM ( CASE WHEN is_deleted = 0 THEN jcqk ELSE 0 END ) jcs,
        SUM ( zt1 + zt2 + zt3 + zt4 + zt5 ) yhs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt1 ELSE 0 END ) dczgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt2 ELSE 0 END ) xqzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN fj2 ELSE 0 END ) fjs,
        SUM ( CASE WHEN is_deleted = 0 THEN yjs ELSE 0 END ) yyjs,
        SUM ( CASE WHEN is_deleted = 0 THEN qss ELSE 0 END ) yqss,
        SUM ( CASE WHEN is_deleted = 0 THEN zt3 ELSE 0 END ) jbzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt4 ELSE 0 END ) yqwzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt5 ELSE 0 END ) zdhzyhs
        FROM
        (
        SELECT
        ( CASE WHEN zt0 + zt1 + zt2 + zt3 + zt4 + zt5 > 0 THEN 1 ELSE 0 END ) AS jcqk,
        *
        FROM
        (
        SELECT
        dept.* ,
        cs.* ,
        tm.begindate,
        tm.enddate
        FROM
        cs_info cs,
        blade_test tm,
        v_dept dept
        WHERE
        cs.create_time &lt; tm.enddate
        AND cs.sssq_bm = dept.sqid
        AND tm.ID :: text = #{params.id}
        AND ((#{params.deptId} LIKE '%' || dept.sqid || '%') OR (dept.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.fjname and '' != params.fjname">
            AND cs.ssfj_bm :: text LIKE '%' || #{params.fjname} || '%'
        </if>
        <if test="null != params.pcsname and '' != params.pcsname">
            AND cs.sspcs_bm :: text LIKE '%' || #{params.pcsname} || '%'
        </if>
        <if test="null != params.sqname and '' != params.sqname">
            AND cs.sssq_bm :: text LIKE '%' || #{params.sqname} || '%'
        </if>
        ) jxcs
        LEFT JOIN (
        SELECT
        csjccs.cs_id,
        MAX ( CASE WHEN csjccs.zgzt = 0 THEN csjccs.jccs ELSE 0 END ) AS zt0,
        MAX ( CASE WHEN csjccs.zgzt = 1 THEN csjccs.jccs ELSE 0 END ) AS zt1,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.jccs ELSE 0 END ) AS zt2,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.fjcs ELSE 0 END ) AS fj2,
        MAX ( CASE WHEN csjccs.zgzt = 3 THEN csjccs.jccs ELSE 0 END ) AS zt3,
        MAX ( CASE WHEN csjccs.zgzt = 4 THEN csjccs.jccs ELSE 0 END ) AS zt4,
        MAX ( CASE WHEN csjccs.zgzt = 5 THEN csjccs.jccs ELSE 0 END ) AS zt5,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.yjcs ELSE 0 END ) AS yjs,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.qscs ELSE 0 END ) AS qss
        FROM
        (
        SELECT
        linshi_jc.cs_id,
        linshi_jc.zgzt,
        COUNT ( 0 ) AS jccs,
        SUM ( CASE WHEN linshi_jc.zgzt = 2 AND maxjc.maxdate > linshi_jc.create_time THEN 1 ELSE 0 END ) AS fjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 3 THEN 1 ELSE 0 END ) AS yjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 4 THEN 1 ELSE 0 END ) AS qscs
        FROM
        (
        SELECT
        *
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        ) linshi_jc,
        (
        SELECT
        cs_id,
        MAX ( create_time ) AS maxdate
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        GROUP BY
        cs_id
        ) maxjc
        WHERE
        linshi_jc.cs_id = maxjc.cs_id
        GROUP BY
        linshi_jc.cs_id,
        linshi_jc.zgzt
        ORDER BY
        linshi_jc.zgzt DESC
        ) csjccs
        GROUP BY
        cs_id
        ) jxcsjc
        ON jxcs.ID = jxcsjc.cs_id
        ) aaa
        GROUP BY
        fjname,
        pcsname,
        sqname
        ORDER BY
        fjname,
        pcsname,
        sqname
        ) tem
    </select>

    <select id="exportSqTj" resultType="org.springblade.modules.csInfo.excel.MjsjtjExcel">
        SELECT
        fjname,
        pcsname,
        sqname,
        yxs,
        jcs,
        ROUND( jcs :: NUMERIC / CASE yxs WHEN 0 THEN 1 ELSE yxs END :: NUMERIC, 2 ) * 100 || '%' AS jcl,
        yhs,
        dczgs,
        xqzgs,
        fjs,
        ROUND( fjs :: NUMERIC / CASE xqzgs WHEN 0 THEN 1 ELSE xqzgs END :: NUMERIC, 2 ) * 100 || '%' AS fjl,
        jbzgs,
        yqwzgs,
        zdhzyhs,
        xzs,
        scs
        FROM
        (
        SELECT
        fjname,
        pcsname,
        sqname,
        SUM ( CASE WHEN create_time BETWEEN begindate AND enddate THEN 1 ELSE 0 END ) xzs,
        SUM ( CASE WHEN is_deleted = 0 THEN 1 ELSE 0 END ) AS yxs,
        SUM ( CASE WHEN update_time BETWEEN begindate AND enddate AND is_deleted = 1 THEN 1 ELSE 0 END ) scs,
        SUM ( CASE WHEN is_deleted = 0 THEN jcqk ELSE 0 END ) jcs,
        SUM ( zt1 + zt2 + zt3 + zt4 + zt5 ) yhs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt1 ELSE 0 END ) dczgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt2 ELSE 0 END ) xqzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN fj2 ELSE 0 END ) fjs,
        SUM ( CASE WHEN is_deleted = 0 THEN yjs ELSE 0 END ) yyjs,
        SUM ( CASE WHEN is_deleted = 0 THEN qss ELSE 0 END ) yqss,
        SUM ( CASE WHEN is_deleted = 0 THEN zt3 ELSE 0 END ) jbzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt4 ELSE 0 END ) yqwzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt5 ELSE 0 END ) zdhzyhs
        FROM
        (
        SELECT
        ( CASE WHEN zt0 + zt1 + zt2 + zt3 + zt4 + zt5 > 0 THEN 1 ELSE 0 END ) AS jcqk,
        *
        FROM
        (
        SELECT
        dept.* ,
        cs.* ,
        tm.begindate,
        tm.enddate
        FROM
        cs_info cs,
        blade_test tm,
        v_dept dept
        WHERE
        cs.create_time &lt; tm.enddate
        AND cs.sssq_bm = dept.sqid
        AND tm.ID :: text = #{params.id}
        AND ((#{params.deptId} LIKE '%' || dept.sqid || '%') OR (dept.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.fjname and '' != params.fjname">
            AND cs.ssfj_bm :: text LIKE '%' || #{params.fjname} || '%'
        </if>
        <if test="null != params.pcsname and '' != params.pcsname">
            AND cs.sspcs_bm :: text LIKE '%' || #{params.pcsname} || '%'
        </if>
        <if test="null != params.sqname and '' != params.sqname">
            AND cs.sssq_bm :: text LIKE '%' || #{params.sqname} || '%'
        </if>
        ) jxcs
        LEFT JOIN (
        SELECT
        csjccs.cs_id,
        MAX ( CASE WHEN csjccs.zgzt = 0 THEN csjccs.jccs ELSE 0 END ) AS zt0,
        MAX ( CASE WHEN csjccs.zgzt = 1 THEN csjccs.jccs ELSE 0 END ) AS zt1,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.jccs ELSE 0 END ) AS zt2,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.fjcs ELSE 0 END ) AS fj2,
        MAX ( CASE WHEN csjccs.zgzt = 3 THEN csjccs.jccs ELSE 0 END ) AS zt3,
        MAX ( CASE WHEN csjccs.zgzt = 4 THEN csjccs.jccs ELSE 0 END ) AS zt4,
        MAX ( CASE WHEN csjccs.zgzt = 5 THEN csjccs.jccs ELSE 0 END ) AS zt5,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.yjcs ELSE 0 END ) AS yjs,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.qscs ELSE 0 END ) AS qss
        FROM
        (
        SELECT
        linshi_jc.cs_id,
        linshi_jc.zgzt,
        COUNT ( 0 ) AS jccs,
        SUM ( CASE WHEN linshi_jc.zgzt = 2 AND maxjc.maxdate > linshi_jc.create_time THEN 1 ELSE 0 END ) AS fjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 3 THEN 1 ELSE 0 END ) AS yjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 4 THEN 1 ELSE 0 END ) AS qscs
        FROM
        (
        SELECT
        *
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        ) linshi_jc,
        (
        SELECT
        cs_id,
        MAX ( create_time ) AS maxdate
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        GROUP BY
        cs_id
        ) maxjc
        WHERE
        linshi_jc.cs_id = maxjc.cs_id
        GROUP BY
        linshi_jc.cs_id,
        linshi_jc.zgzt
        ORDER BY
        linshi_jc.zgzt DESC
        ) csjccs
        GROUP BY
        cs_id
        ) jxcsjc
        ON jxcs.ID = jxcsjc.cs_id
        ) aaa
        GROUP BY
        fjname,
        pcsname,
        sqname
        ORDER BY
        fjname,
        pcsname,
        sqname
        ) tem
    </select>

    <select id="getPcsTj" resultType="org.springblade.modules.csInfo.excel.MjsjtjExcel">
        SELECT
        fjname,
        pcsname,
        yxs,
        jcs,
        ROUND( jcs :: NUMERIC / CASE yxs WHEN 0 THEN 1 ELSE yxs END :: NUMERIC, 2 ) AS jcl,
        yhs,
        dczgs,
        xqzgs,
        fjs,
        ROUND( fjs :: NUMERIC / CASE xqzgs WHEN 0 THEN 1 ELSE xqzgs END :: NUMERIC, 2 ) AS fjl,
        jbzgs,
        yqwzgs,
        zdhzyhs,
        xzs,
        scs
        FROM
        (
        SELECT
        fjname,
        pcsname,
        SUM ( CASE WHEN create_time BETWEEN begindate AND enddate THEN 1 ELSE 0 END ) xzs,
        SUM ( CASE WHEN is_deleted = 0 THEN 1 ELSE 0 END ) AS yxs,
        SUM ( CASE WHEN update_time BETWEEN begindate AND enddate AND is_deleted = 1 THEN 1 ELSE 0 END ) scs,
        SUM ( CASE WHEN is_deleted = 0 THEN jcqk ELSE 0 END ) jcs,
        SUM ( zt1 + zt2 + zt3 + zt4 + zt5 ) yhs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt1 ELSE 0 END ) dczgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt2 ELSE 0 END ) xqzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN fj2 ELSE 0 END ) fjs,
        SUM ( CASE WHEN is_deleted = 0 THEN yjs ELSE 0 END ) yyjs,
        SUM ( CASE WHEN is_deleted = 0 THEN qss ELSE 0 END ) yqss,
        SUM ( CASE WHEN is_deleted = 0 THEN zt3 ELSE 0 END ) jbzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt4 ELSE 0 END ) yqwzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt5 ELSE 0 END ) zdhzyhs
        FROM
        (
        SELECT
        ( CASE WHEN zt0 + zt1 + zt2 + zt3 + zt4 + zt5 > 0 THEN 1 ELSE 0 END ) AS jcqk,
        *
        FROM
        (
        SELECT
        dept.* ,
        cs.* ,
        tm.begindate,
        tm.enddate
        FROM
        cs_info cs,
        blade_test tm,
        v_dept dept
        WHERE
        cs.create_time &lt; tm.enddate
        AND cs.sssq_bm = dept.sqid
        AND tm.ID :: text = #{params.id}
        AND ((#{params.deptId} LIKE '%' || dept.sqid || '%') OR (dept.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.fjname and '' != params.fjname">
            AND cs.ssfj_bm :: text LIKE '%' || #{params.fjname} || '%'
        </if>
        <if test="null != params.pcsname and '' != params.pcsname">
            AND cs.sspcs_bm :: text LIKE '%' || #{params.pcsname} || '%'
        </if>
        <if test="null != params.sqname and '' != params.sqname">
            AND cs.sssq_bm :: text LIKE '%' || #{params.sqname} || '%'
        </if>
        ) jxcs
        LEFT JOIN (
        SELECT
        csjccs.cs_id,
        MAX ( CASE WHEN csjccs.zgzt = 0 THEN csjccs.jccs ELSE 0 END ) AS zt0,
        MAX ( CASE WHEN csjccs.zgzt = 1 THEN csjccs.jccs ELSE 0 END ) AS zt1,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.jccs ELSE 0 END ) AS zt2,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.fjcs ELSE 0 END ) AS fj2,
        MAX ( CASE WHEN csjccs.zgzt = 3 THEN csjccs.jccs ELSE 0 END ) AS zt3,
        MAX ( CASE WHEN csjccs.zgzt = 4 THEN csjccs.jccs ELSE 0 END ) AS zt4,
        MAX ( CASE WHEN csjccs.zgzt = 5 THEN csjccs.jccs ELSE 0 END ) AS zt5,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.yjcs ELSE 0 END ) AS yjs,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.qscs ELSE 0 END ) AS qss
        FROM
        (
        SELECT
        linshi_jc.cs_id,
        linshi_jc.zgzt,
        COUNT ( 0 ) AS jccs,
        SUM ( CASE WHEN linshi_jc.zgzt = 2 AND maxjc.maxdate > linshi_jc.create_time THEN 1 ELSE 0 END ) AS fjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 3 THEN 1 ELSE 0 END ) AS yjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 4 THEN 1 ELSE 0 END ) AS qscs
        FROM
        (
        SELECT
        *
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        ) linshi_jc,
        (
        SELECT
        cs_id,
        MAX ( create_time ) AS maxdate
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        GROUP BY
        cs_id
        ) maxjc
        WHERE
        linshi_jc.cs_id = maxjc.cs_id
        GROUP BY
        linshi_jc.cs_id,
        linshi_jc.zgzt
        ORDER BY
        linshi_jc.zgzt DESC
        ) csjccs
        GROUP BY
        cs_id
        ) jxcsjc
        ON jxcs.ID = jxcsjc.cs_id
        ) aaa
        GROUP BY
        fjname,
        pcsname
        ORDER BY
        fjname,
        pcsname
        ) tem
    </select>

    <select id="exportPcsTj" resultType="org.springblade.modules.csInfo.excel.MjsjtjExcel">
        SELECT
        fjname,
        pcsname,
        yxs,
        jcs,
        ROUND( jcs :: NUMERIC / CASE yxs WHEN 0 THEN 1 ELSE yxs END :: NUMERIC, 2 ) AS jcl,
        yhs,
        dczgs,
        xqzgs,
        fjs,
        ROUND( fjs :: NUMERIC / CASE xqzgs WHEN 0 THEN 1 ELSE xqzgs END :: NUMERIC, 2 ) AS fjl,
        jbzgs,
        yqwzgs,
        zdhzyhs,
        xzs,
        scs
        FROM
        (
        SELECT
        fjname,
        pcsname,
        SUM ( CASE WHEN create_time BETWEEN begindate AND enddate THEN 1 ELSE 0 END ) xzs,
        SUM ( CASE WHEN is_deleted = 0 THEN 1 ELSE 0 END ) AS yxs,
        SUM ( CASE WHEN update_time BETWEEN begindate AND enddate AND is_deleted = 1 THEN 1 ELSE 0 END ) scs,
        SUM ( CASE WHEN is_deleted = 0 THEN jcqk ELSE 0 END ) jcs,
        SUM ( zt1 + zt2 + zt3 + zt4 + zt5 ) yhs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt1 ELSE 0 END ) dczgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt2 ELSE 0 END ) xqzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN fj2 ELSE 0 END ) fjs,
        SUM ( CASE WHEN is_deleted = 0 THEN yjs ELSE 0 END ) yyjs,
        SUM ( CASE WHEN is_deleted = 0 THEN qss ELSE 0 END ) yqss,
        SUM ( CASE WHEN is_deleted = 0 THEN zt3 ELSE 0 END ) jbzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt4 ELSE 0 END ) yqwzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt5 ELSE 0 END ) zdhzyhs
        FROM
        (
        SELECT
        ( CASE WHEN zt0 + zt1 + zt2 + zt3 + zt4 + zt5 > 0 THEN 1 ELSE 0 END ) AS jcqk,
        *
        FROM
        (
        SELECT
        dept.* ,
        cs.* ,
        tm.begindate,
        tm.enddate
        FROM
        cs_info cs,
        blade_test tm,
        v_dept dept
        WHERE
        cs.create_time &lt; tm.enddate
        AND cs.sssq_bm = dept.sqid
        AND tm.ID :: text = #{params.id}
        AND ((#{params.deptId} LIKE '%' || dept.sqid || '%') OR (dept.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.fjname and '' != params.fjname">
            AND cs.ssfj_bm :: text LIKE '%' || #{params.fjname} || '%'
        </if>
        <if test="null != params.pcsname and '' != params.pcsname">
            AND cs.sspcs_bm :: text LIKE '%' || #{params.pcsname} || '%'
        </if>
        <if test="null != params.sqname and '' != params.sqname">
            AND cs.sssq_bm :: text LIKE '%' || #{params.sqname} || '%'
        </if>
        ) jxcs
        LEFT JOIN (
        SELECT
        csjccs.cs_id,
        MAX ( CASE WHEN csjccs.zgzt = 0 THEN csjccs.jccs ELSE 0 END ) AS zt0,
        MAX ( CASE WHEN csjccs.zgzt = 1 THEN csjccs.jccs ELSE 0 END ) AS zt1,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.jccs ELSE 0 END ) AS zt2,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.fjcs ELSE 0 END ) AS fj2,
        MAX ( CASE WHEN csjccs.zgzt = 3 THEN csjccs.jccs ELSE 0 END ) AS zt3,
        MAX ( CASE WHEN csjccs.zgzt = 4 THEN csjccs.jccs ELSE 0 END ) AS zt4,
        MAX ( CASE WHEN csjccs.zgzt = 5 THEN csjccs.jccs ELSE 0 END ) AS zt5,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.yjcs ELSE 0 END ) AS yjs,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.qscs ELSE 0 END ) AS qss
        FROM
        (
        SELECT
        linshi_jc.cs_id,
        linshi_jc.zgzt,
        COUNT ( 0 ) AS jccs,
        SUM ( CASE WHEN linshi_jc.zgzt = 2 AND maxjc.maxdate > linshi_jc.create_time THEN 1 ELSE 0 END ) AS fjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 3 THEN 1 ELSE 0 END ) AS yjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 4 THEN 1 ELSE 0 END ) AS qscs
        FROM
        (
        SELECT
        *
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        ) linshi_jc,
        (
        SELECT
        cs_id,
        MAX ( create_time ) AS maxdate
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        GROUP BY
        cs_id
        ) maxjc
        WHERE
        linshi_jc.cs_id = maxjc.cs_id
        GROUP BY
        linshi_jc.cs_id,
        linshi_jc.zgzt
        ORDER BY
        linshi_jc.zgzt DESC
        ) csjccs
        GROUP BY
        cs_id
        ) jxcsjc
        ON jxcs.ID = jxcsjc.cs_id
        ) aaa
        GROUP BY
        fjname,
        pcsname
        ORDER BY
        fjname,
        pcsname
        ) tem
    </select>

    <select id="getFjTj" resultType="org.springblade.modules.csInfo.excel.MjsjtjExcel">
        SELECT
        fjname,
        yxs,
        jcs,
        ROUND( jcs :: NUMERIC / CASE yxs WHEN 0 THEN 1 ELSE yxs END :: NUMERIC, 2 ) AS jcl,
        yhs,
        dczgs,
        xqzgs,
        fjs,
        ROUND( fjs :: NUMERIC / CASE xqzgs WHEN 0 THEN 1 ELSE xqzgs END :: NUMERIC, 2 ) AS fjl,
        jbzgs,
        yqwzgs,
        zdhzyhs,
        xzs,
        scs
        FROM
        (
        SELECT
        fjname,
        SUM ( CASE WHEN create_time BETWEEN begindate AND enddate THEN 1 ELSE 0 END ) xzs,
        SUM ( CASE WHEN is_deleted = 0 THEN 1 ELSE 0 END ) AS yxs,
        SUM ( CASE WHEN update_time BETWEEN begindate AND enddate AND is_deleted = 1 THEN 1 ELSE 0 END ) scs,
        SUM ( CASE WHEN is_deleted = 0 THEN jcqk ELSE 0 END ) jcs,
        SUM ( zt1 + zt2 + zt3 + zt4 + zt5 ) yhs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt1 ELSE 0 END ) dczgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt2 ELSE 0 END ) xqzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN fj2 ELSE 0 END ) fjs,
        SUM ( CASE WHEN is_deleted = 0 THEN yjs ELSE 0 END ) yyjs,
        SUM ( CASE WHEN is_deleted = 0 THEN qss ELSE 0 END ) yqss,
        SUM ( CASE WHEN is_deleted = 0 THEN zt3 ELSE 0 END ) jbzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt4 ELSE 0 END ) yqwzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt5 ELSE 0 END ) zdhzyhs
        FROM
        (
        SELECT
        ( CASE WHEN zt0 + zt1 + zt2 + zt3 + zt4 + zt5 > 0 THEN 1 ELSE 0 END ) AS jcqk,
        *
        FROM
        (
        SELECT
        dept.* ,
        cs.* ,
        tm.begindate,
        tm.enddate
        FROM
        cs_info cs,
        blade_test tm,
        v_dept dept
        WHERE
        cs.create_time &lt; tm.enddate
        AND cs.sssq_bm = dept.sqid
        AND tm.ID :: text = #{params.id}
        AND ((#{params.deptId} LIKE '%' || dept.sqid || '%') OR (dept.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.fjname and '' != params.fjname">
            AND cs.ssfj_bm :: text LIKE '%' || #{params.fjname} || '%'
        </if>
        <if test="null != params.pcsname and '' != params.pcsname">
            AND cs.sspcs_bm :: text LIKE '%' || #{params.pcsname} || '%'
        </if>
        <if test="null != params.sqname and '' != params.sqname">
            AND cs.sssq_bm :: text LIKE '%' || #{params.sqname} || '%'
        </if>
        ) jxcs
        LEFT JOIN (
        SELECT
        csjccs.cs_id,
        MAX ( CASE WHEN csjccs.zgzt = 0 THEN csjccs.jccs ELSE 0 END ) AS zt0,
        MAX ( CASE WHEN csjccs.zgzt = 1 THEN csjccs.jccs ELSE 0 END ) AS zt1,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.jccs ELSE 0 END ) AS zt2,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.fjcs ELSE 0 END ) AS fj2,
        MAX ( CASE WHEN csjccs.zgzt = 3 THEN csjccs.jccs ELSE 0 END ) AS zt3,
        MAX ( CASE WHEN csjccs.zgzt = 4 THEN csjccs.jccs ELSE 0 END ) AS zt4,
        MAX ( CASE WHEN csjccs.zgzt = 5 THEN csjccs.jccs ELSE 0 END ) AS zt5,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.yjcs ELSE 0 END ) AS yjs,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.qscs ELSE 0 END ) AS qss
        FROM
        (
        SELECT
        linshi_jc.cs_id,
        linshi_jc.zgzt,
        COUNT ( 0 ) AS jccs,
        SUM ( CASE WHEN linshi_jc.zgzt = 2 AND maxjc.maxdate > linshi_jc.create_time THEN 1 ELSE 0 END ) AS fjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 3 THEN 1 ELSE 0 END ) AS yjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 4 THEN 1 ELSE 0 END ) AS qscs
        FROM
        (
        SELECT
        *
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        ) linshi_jc,
        (
        SELECT
        cs_id,
        MAX ( create_time ) AS maxdate
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        GROUP BY
        cs_id
        ) maxjc
        WHERE
        linshi_jc.cs_id = maxjc.cs_id
        GROUP BY
        linshi_jc.cs_id,
        linshi_jc.zgzt
        ORDER BY
        linshi_jc.zgzt DESC
        ) csjccs
        GROUP BY
        cs_id
        ) jxcsjc
        ON jxcs.ID = jxcsjc.cs_id
        ) aaa
        GROUP BY
        fjname
        ORDER BY
        fjname
        ) tem
    </select>

    <select id="exportFjTj" resultType="org.springblade.modules.csInfo.excel.MjsjtjExcel">
        SELECT
        fjname,
        yxs,
        jcs,
        ROUND( jcs :: NUMERIC / CASE yxs WHEN 0 THEN 1 ELSE yxs END :: NUMERIC, 2 ) AS jcl,
        yhs,
        dczgs,
        xqzgs,
        fjs,
        ROUND( fjs :: NUMERIC / CASE xqzgs WHEN 0 THEN 1 ELSE xqzgs END :: NUMERIC, 2 ) AS fjl,
        jbzgs,
        yqwzgs,
        zdhzyhs,
        xzs,
        scs
        FROM
        (
        SELECT
        fjname,
        SUM ( CASE WHEN create_time BETWEEN begindate AND enddate THEN 1 ELSE 0 END ) xzs,
        SUM ( CASE WHEN is_deleted = 0 THEN 1 ELSE 0 END ) AS yxs,
        SUM ( CASE WHEN update_time BETWEEN begindate AND enddate AND is_deleted = 1 THEN 1 ELSE 0 END ) scs,
        SUM ( CASE WHEN is_deleted = 0 THEN jcqk ELSE 0 END ) jcs,
        SUM ( zt1 + zt2 + zt3 + zt4 + zt5 ) yhs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt1 ELSE 0 END ) dczgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt2 ELSE 0 END ) xqzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN fj2 ELSE 0 END ) fjs,
        SUM ( CASE WHEN is_deleted = 0 THEN yjs ELSE 0 END ) yyjs,
        SUM ( CASE WHEN is_deleted = 0 THEN qss ELSE 0 END ) yqss,
        SUM ( CASE WHEN is_deleted = 0 THEN zt3 ELSE 0 END ) jbzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt4 ELSE 0 END ) yqwzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt5 ELSE 0 END ) zdhzyhs
        FROM
        (
        SELECT
        ( CASE WHEN zt0 + zt1 + zt2 + zt3 + zt4 + zt5 > 0 THEN 1 ELSE 0 END ) AS jcqk,
        *
        FROM
        (
        SELECT
        dept.* ,
        cs.* ,
        tm.begindate,
        tm.enddate
        FROM
        cs_info cs,
        blade_test tm,
        v_dept dept
        WHERE
        cs.create_time &lt; tm.enddate
        AND cs.sssq_bm = dept.sqid
        AND tm.ID :: text = #{params.id}
        AND ((#{params.deptId} LIKE '%' || dept.sqid || '%') OR (dept.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.fjname and '' != params.fjname">
            AND cs.ssfj_bm :: text LIKE '%' || #{params.fjname} || '%'
        </if>
        <if test="null != params.pcsname and '' != params.pcsname">
            AND cs.sspcs_bm :: text LIKE '%' || #{params.pcsname} || '%'
        </if>
        <if test="null != params.sqname and '' != params.sqname">
            AND cs.sssq_bm :: text LIKE '%' || #{params.sqname} || '%'
        </if>
        ) jxcs
        LEFT JOIN (
        SELECT
        csjccs.cs_id,
        MAX ( CASE WHEN csjccs.zgzt = 0 THEN csjccs.jccs ELSE 0 END ) AS zt0,
        MAX ( CASE WHEN csjccs.zgzt = 1 THEN csjccs.jccs ELSE 0 END ) AS zt1,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.jccs ELSE 0 END ) AS zt2,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.fjcs ELSE 0 END ) AS fj2,
        MAX ( CASE WHEN csjccs.zgzt = 3 THEN csjccs.jccs ELSE 0 END ) AS zt3,
        MAX ( CASE WHEN csjccs.zgzt = 4 THEN csjccs.jccs ELSE 0 END ) AS zt4,
        MAX ( CASE WHEN csjccs.zgzt = 5 THEN csjccs.jccs ELSE 0 END ) AS zt5,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.yjcs ELSE 0 END ) AS yjs,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.qscs ELSE 0 END ) AS qss
        FROM
        (
        SELECT
        linshi_jc.cs_id,
        linshi_jc.zgzt,
        COUNT ( 0 ) AS jccs,
        SUM ( CASE WHEN linshi_jc.zgzt = 2 AND maxjc.maxdate > linshi_jc.create_time THEN 1 ELSE 0 END ) AS fjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 3 THEN 1 ELSE 0 END ) AS yjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 4 THEN 1 ELSE 0 END ) AS qscs
        FROM
        (
        SELECT
        *
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        ) linshi_jc,
        (
        SELECT
        cs_id,
        MAX ( create_time ) AS maxdate
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        GROUP BY
        cs_id
        ) maxjc
        WHERE
        linshi_jc.cs_id = maxjc.cs_id
        GROUP BY
        linshi_jc.cs_id,
        linshi_jc.zgzt
        ORDER BY
        linshi_jc.zgzt DESC
        ) csjccs
        GROUP BY
        cs_id
        ) jxcsjc
        ON jxcs.ID = jxcsjc.cs_id
        ) aaa
        GROUP BY
        fjname
        ORDER BY
        fjname
        ) tem
    </select>

    <select id="getQsTj" resultType="org.springblade.modules.csInfo.excel.MjsjtjExcel">
        SELECT
        yxs,
        jcs,
        ROUND( jcs :: NUMERIC / CASE yxs WHEN 0 THEN 1 ELSE yxs END :: NUMERIC, 2 ) AS jcl,
        yhs,
        dczgs,
        xqzgs,
        fjs,
        ROUND( fjs :: NUMERIC / CASE xqzgs WHEN 0 THEN 1 ELSE xqzgs END :: NUMERIC, 2 ) AS fjl,
        jbzgs,
        yqwzgs,
        zdhzyhs,
        xzs,
        scs
        FROM
        (
        SELECT
        SUM ( CASE WHEN create_time BETWEEN begindate AND enddate THEN 1 ELSE 0 END ) xzs,
        SUM ( CASE WHEN is_deleted = 0 THEN 1 ELSE 0 END ) AS yxs,
        SUM ( CASE WHEN update_time BETWEEN begindate AND enddate AND is_deleted = 1 THEN 1 ELSE 0 END ) scs,
        SUM ( CASE WHEN is_deleted = 0 THEN jcqk ELSE 0 END ) jcs,
        SUM ( zt1 + zt2 + zt3 + zt4 + zt5 ) yhs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt1 ELSE 0 END ) dczgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt2 ELSE 0 END ) xqzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN fj2 ELSE 0 END ) fjs,
        SUM ( CASE WHEN is_deleted = 0 THEN yjs ELSE 0 END ) yyjs,
        SUM ( CASE WHEN is_deleted = 0 THEN qss ELSE 0 END ) yqss,
        SUM ( CASE WHEN is_deleted = 0 THEN zt3 ELSE 0 END ) jbzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt4 ELSE 0 END ) yqwzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt5 ELSE 0 END ) zdhzyhs
        FROM
        (
        SELECT
        ( CASE WHEN zt0 + zt1 + zt2 + zt3 + zt4 + zt5 > 0 THEN 1 ELSE 0 END ) AS jcqk,
        *
        FROM
        (
        SELECT
        dept.* ,
        cs.* ,
        tm.begindate,
        tm.enddate
        FROM
        cs_info cs,
        blade_test tm,
        v_dept dept
        WHERE
        cs.create_time &lt; tm.enddate
        AND cs.sssq_bm = dept.sqid
        AND tm.ID :: text = #{params.id}
        AND ((#{params.deptId} LIKE '%' || dept.sqid || '%') OR (dept.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.fjname and '' != params.fjname">
            AND cs.ssfj_bm :: text LIKE '%' || #{params.fjname} || '%'
        </if>
        <if test="null != params.pcsname and '' != params.pcsname">
            AND cs.sspcs_bm :: text LIKE '%' || #{params.pcsname} || '%'
        </if>
        <if test="null != params.sqname and '' != params.sqname">
            AND cs.sssq_bm :: text LIKE '%' || #{params.sqname} || '%'
        </if>
        ) jxcs
        LEFT JOIN (
        SELECT
        csjccs.cs_id,
        MAX ( CASE WHEN csjccs.zgzt = 0 THEN csjccs.jccs ELSE 0 END ) AS zt0,
        MAX ( CASE WHEN csjccs.zgzt = 1 THEN csjccs.jccs ELSE 0 END ) AS zt1,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.jccs ELSE 0 END ) AS zt2,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.fjcs ELSE 0 END ) AS fj2,
        MAX ( CASE WHEN csjccs.zgzt = 3 THEN csjccs.jccs ELSE 0 END ) AS zt3,
        MAX ( CASE WHEN csjccs.zgzt = 4 THEN csjccs.jccs ELSE 0 END ) AS zt4,
        MAX ( CASE WHEN csjccs.zgzt = 5 THEN csjccs.jccs ELSE 0 END ) AS zt5,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.yjcs ELSE 0 END ) AS yjs,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.qscs ELSE 0 END ) AS qss
        FROM
        (
        SELECT
        linshi_jc.cs_id,
        linshi_jc.zgzt,
        COUNT ( 0 ) AS jccs,
        SUM ( CASE WHEN linshi_jc.zgzt = 2 AND maxjc.maxdate > linshi_jc.create_time THEN 1 ELSE 0 END ) AS fjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 3 THEN 1 ELSE 0 END ) AS yjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 4 THEN 1 ELSE 0 END ) AS qscs
        FROM
        (
        SELECT
        *
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        ) linshi_jc,
        (
        SELECT
        cs_id,
        MAX ( create_time ) AS maxdate
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        GROUP BY
        cs_id
        ) maxjc
        WHERE
        linshi_jc.cs_id = maxjc.cs_id
        GROUP BY
        linshi_jc.cs_id,
        linshi_jc.zgzt
        ORDER BY
        linshi_jc.zgzt DESC
        ) csjccs
        GROUP BY
        cs_id
        ) jxcsjc
        ON jxcs.ID = jxcsjc.cs_id
        ) aaa
        ) tem
    </select>

    <select id="exportQsTj" resultType="org.springblade.modules.csInfo.excel.MjsjtjExcel">
        SELECT
        yxs,
        jcs,
        ROUND( jcs :: NUMERIC / CASE yxs WHEN 0 THEN 1 ELSE yxs END :: NUMERIC, 2 ) AS jcl,
        yhs,
        dczgs,
        xqzgs,
        fjs,
        ROUND( fjs :: NUMERIC / CASE xqzgs WHEN 0 THEN 1 ELSE xqzgs END :: NUMERIC, 2 ) AS fjl,
        jbzgs,
        yqwzgs,
        zdhzyhs,
        xzs,
        scs
        FROM
        (
        SELECT
        SUM ( CASE WHEN create_time BETWEEN begindate AND enddate THEN 1 ELSE 0 END ) xzs,
        SUM ( CASE WHEN is_deleted = 0 THEN 1 ELSE 0 END ) AS yxs,
        SUM ( CASE WHEN update_time BETWEEN begindate AND enddate AND is_deleted = 1 THEN 1 ELSE 0 END ) scs,
        SUM ( CASE WHEN is_deleted = 0 THEN jcqk ELSE 0 END ) jcs,
        SUM ( zt1 + zt2 + zt3 + zt4 + zt5 ) yhs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt1 ELSE 0 END ) dczgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt2 ELSE 0 END ) xqzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN fj2 ELSE 0 END ) fjs,
        SUM ( CASE WHEN is_deleted = 0 THEN yjs ELSE 0 END ) yyjs,
        SUM ( CASE WHEN is_deleted = 0 THEN qss ELSE 0 END ) yqss,
        SUM ( CASE WHEN is_deleted = 0 THEN zt3 ELSE 0 END ) jbzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt4 ELSE 0 END ) yqwzgs,
        SUM ( CASE WHEN is_deleted = 0 THEN zt5 ELSE 0 END ) zdhzyhs
        FROM
        (
        SELECT
        ( CASE WHEN zt0 + zt1 + zt2 + zt3 + zt4 + zt5 > 0 THEN 1 ELSE 0 END ) AS jcqk,
        *
        FROM
        (
        SELECT
        dept.* ,
        cs.* ,
        tm.begindate,
        tm.enddate
        FROM
        cs_info cs,
        blade_test tm,
        v_dept dept
        WHERE
        cs.create_time &lt; tm.enddate
        AND cs.sssq_bm = dept.sqid
        AND tm.ID :: text = #{params.id}
        AND ((#{params.deptId} LIKE '%' || dept.sqid || '%') OR (dept.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.fjname and '' != params.fjname">
            AND cs.ssfj_bm :: text LIKE '%' || #{params.fjname} || '%'
        </if>
        <if test="null != params.pcsname and '' != params.pcsname">
            AND cs.sspcs_bm :: text LIKE '%' || #{params.pcsname} || '%'
        </if>
        <if test="null != params.sqname and '' != params.sqname">
            AND cs.sssq_bm :: text LIKE '%' || #{params.sqname} || '%'
        </if>
        ) jxcs
        LEFT JOIN (
        SELECT
        csjccs.cs_id,
        MAX ( CASE WHEN csjccs.zgzt = 0 THEN csjccs.jccs ELSE 0 END ) AS zt0,
        MAX ( CASE WHEN csjccs.zgzt = 1 THEN csjccs.jccs ELSE 0 END ) AS zt1,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.jccs ELSE 0 END ) AS zt2,
        MAX ( CASE WHEN csjccs.zgzt = 2 THEN csjccs.fjcs ELSE 0 END ) AS fj2,
        MAX ( CASE WHEN csjccs.zgzt = 3 THEN csjccs.jccs ELSE 0 END ) AS zt3,
        MAX ( CASE WHEN csjccs.zgzt = 4 THEN csjccs.jccs ELSE 0 END ) AS zt4,
        MAX ( CASE WHEN csjccs.zgzt = 5 THEN csjccs.jccs ELSE 0 END ) AS zt5,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.yjcs ELSE 0 END ) AS yjs,
        MAX ( CASE WHEN csjccs.zgzt >= 3 THEN csjccs.qscs ELSE 0 END ) AS qss
        FROM
        (
        SELECT
        linshi_jc.cs_id,
        linshi_jc.zgzt,
        COUNT ( 0 ) AS jccs,
        SUM ( CASE WHEN linshi_jc.zgzt = 2 AND maxjc.maxdate > linshi_jc.create_time THEN 1 ELSE 0 END ) AS fjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 3 THEN 1 ELSE 0 END ) AS yjcs,
        SUM ( CASE WHEN linshi_jc.zgzt >= 3 AND fjlzzt >= 4 THEN 1 ELSE 0 END ) AS qscs
        FROM
        (
        SELECT
        *
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        ) linshi_jc,
        (
        SELECT
        cs_id,
        MAX ( create_time ) AS maxdate
        FROM
        csjc_info csjc,
        blade_test tm
        WHERE
        csjc.is_deleted = 0
        AND csjc.create_time BETWEEN tm.begindate
        AND tm.enddate
        AND tm.ID :: text = #{params.id}
        GROUP BY
        cs_id
        ) maxjc
        WHERE
        linshi_jc.cs_id = maxjc.cs_id
        GROUP BY
        linshi_jc.cs_id,
        linshi_jc.zgzt
        ORDER BY
        linshi_jc.zgzt DESC
        ) csjccs
        GROUP BY
        cs_id
        ) jxcsjc
        ON jxcs.ID = jxcsjc.cs_id
        ) aaa
        ) tem
    </select>

    <select id="getXfjcTime" resultType="org.springblade.modules.csInfo.dto.TimeDTO">
        SELECT
            id as value,
            begindate :: TEXT || ' ~ ' || enddate :: TEXT AS label
        FROM
            blade_test
        ORDER BY
            begindate DESC
    </select>

    <select id="getJxcs" resultType="org.springblade.modules.appmenu.vo.SssjVO">
        SELECT COUNT (1) as jxcs FROM cs_info cs   LEFT JOIN blade_dept sq ON sq.id = cs.sssq_bm  WHERE cs.is_deleted = 0 AND ((sq.id :: text = any(string_to_array(#{params.deptId},','))) OR (sq.ancestors LIKE '%' || #{params.deptId} || '%')) <if test="startTime != null and startTime != '' and endTime != null and endTime != ''"> and cs.create_time between #{startTime} :: timestamp and #{endTime} :: timestamp</if>
    </select>

    <select id="getXfjc" resultType="org.springblade.modules.appmenu.vo.SssjVO">
        select
        COUNT (1) as xfjc
        from
        jdrh.cs_info info
        left join jdrh.csjc_info jc on
        info.jc_id = jc.id
        left join jdrh.blade_dept bd on
        bd.id = info.create_dept
        where info.is_deleted = 0 and jc.is_deleted = 0 AND ((bd.id :: text = any(string_to_array(#{params.deptId},','))) OR (bd.ancestors LIKE '%' || #{params.deptId} || '%')) <if test="startTime != null and startTime != '' and endTime != null and endTime != ''"> and jc.create_time between #{startTime} :: timestamp and #{endTime} :: timestamp</if>
    </select>

    <select id="getMdjf" resultType="org.springblade.modules.appmenu.vo.SssjVO">
        SELECT COUNT (1) AS mdjf FROM mdjf_dj md   LEFT JOIN blade_dept sq ON sq.id = md.jfsssq  WHERE md.is_deleted = 0 AND ((sq.id :: text = any(string_to_array(#{params.deptId},','))) OR (sq.ancestors LIKE '%' || #{params.deptId} || '%')) <if test="startTime != null and startTime != '' and endTime != null and endTime != ''"> and md.create_time between #{startTime} :: timestamp and #{endTime} :: timestamp</if>
    </select>

    <select id="getRhzf" resultType="org.springblade.modules.appmenu.vo.SssjVO">
        <if test="params.roleName == '社区民警'">
            SELECT COUNT (1) AS rhzf FROM rhzf_fw_info fw WHERE fw.is_deleted = 0 AND (fw.mj_id = #{params.userId} or fw.create_user = #{params.userId}) <if test="startTime != null and startTime != '' and endTime != null and endTime != ''"> and fw.create_time between #{startTime} :: timestamp and #{endTime} :: timestamp</if>
        </if>
        <if test="params.roleName != '社区民警'">
            SELECT COUNT (1) AS rhzf FROM rhzf_fw_info fw WHERE fw.is_deleted = 0 AND fw.create_user = #{params.userId} <if test="startTime != null and startTime != '' and endTime != null and endTime != ''"> and fw.create_time between #{startTime} :: timestamp and #{endTime} :: timestamp</if>
        </if>
    </select>

    <select id="getRhzfrs" resultType="org.springblade.modules.appmenu.vo.SssjVO">
        <if test="params.roleName == '社区民警'">
            SELECT COUNT (1) AS rhzfrs FROM rhzf_fw_ry fw WHERE fw.is_deleted = 0 AND fw.fw_id is not null AND (fw.mj_id = #{params.userId} or fw.create_user = #{params.userId}) <if test="startTime != null and startTime != '' and endTime != null and endTime != ''"> and fw.create_time between #{startTime} :: timestamp and #{endTime} :: timestamp</if>
        </if>
        <if test="params.roleName != '社区民警'">
            SELECT COUNT (1) AS rhzfrs FROM rhzf_fw_ry fw WHERE fw.is_deleted = 0 AND fw.fw_id is not null AND fw.create_user = #{params.userId} <if test="startTime != null and startTime != '' and endTime != null and endTime != ''"> and fw.create_time between #{startTime} :: timestamp and #{endTime} :: timestamp</if>
        </if>
    </select>

    <select id="getDbsxVOjc" resultType="org.springblade.modules.appmenu.vo.DbsxVO">
        select count(0) as total from cs_info
        left join csjc_info on cs_info.id = csjc_info.cs_id
        left join blade_dept on blade_dept.id = cs_info.sssq_bm
        where csjc_info.zgzt is null and ((#{deptId} LIKE '%' || blade_dept.id || '%') OR (blade_dept.ancestors LIKE '%' || #{deptId} || '%')) and cs_info.is_deleted = 0
    </select>


    <select id="getTjfxSjtj" resultType="org.springblade.modules.csInfo.vo.TjfxVO">
        select *
        from public.tjfxsjtj t1
        where 1 = 1
        <choose>
            <when test="tjfxVO.jglx == 14">
                And 1 = 1
            </when>
            <when test="tjfxVO.jglx == 4">
                And t1.jgdm :: TEXT =#{tjfxVO.jgdm}
            </when>
            <otherwise>
                And 1 = 2
            </otherwise>
        </choose>
        order by t1.jgdm
    </select>


    <select id="getTjfxFjtj" resultType="org.springblade.modules.csInfo.vo.TjfxVO">
        select *
        from public.tjfxfjtj t1
        where 1 = 1
        <if test="tjfxVO.sj != null and tjfxVO.sj != ''">
            And t1.sjid :: text = #{tjfxVO.sj}
        </if>
        <if test="tjfxVO.fj != null and tjfxVO.fj != ''">
            And t1.fjid :: text = #{tjfxVO.fj}
        </if>
        <choose>
            <when test="tjfxVO.jglx == 14">
                And 1 = 1
            </when>
            <when test="tjfxVO.jglx == 4">
                And t1."sjid" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <when test="tjfxVO.jglx == 5">
                And t1."fjid" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <otherwise>
                And 1 = 2
            </otherwise>
        </choose>
        order by sjid,fjid
    </select>


    <select id="getTjfxPcstj" resultType="org.springblade.modules.csInfo.vo.TjfxVO">
        select *
        from public.tjfxpcstj t1
        where 1 = 1
        <if test="tjfxVO.sj != null and tjfxVO.sj != ''">
            And t1.sjid :: text = #{tjfxVO.sj}
        </if>
        <if test="tjfxVO.fj != null and tjfxVO.fj != ''">
            And t1.fjid :: text = #{tjfxVO.fj}
        </if>
        <if test="tjfxVO.pcs != null and tjfxVO.pcs != ''">
            And t1.pcsid :: text = #{tjfxVO.pcs}
        </if>
        <choose>
            <when test="tjfxVO.jglx == 14">
                And 1 = 1
            </when>
            <when test="tjfxVO.jglx == 4">
                And t1."sjid" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <when test="tjfxVO.jglx == 5">
                And t1."fjid" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <when test="tjfxVO.jglx == 6">
                And t1."pcsid" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <otherwise>
                And 1 = 2
            </otherwise>
        </choose>
        order by sjid,fjid,pcsid
    </select>


    <select id="getTjfxMjtj" resultType="org.springblade.modules.csInfo.vo.TjfxVO">
        select
        dept."id市局" sjid,
        dept."dept_name市局名" sj,
        dept."id分局" fjid,
        dept."dept_name分局名" fj,
        dept."id派出所" pcsid,
        dept."dept_name派出所名" pcs,
        t1.*
        from public.tjfxmjtj as t1
        JOIN public.v_dept_all dept ON dept.ID社区 :: TEXT = (string_to_array( T1.dept_id, ','))[1]
        --where dept."dept_name市局名" IS NOT NULL
        where 1 = 1
        <choose>
            <when test="tjfxVO.jglx == 14">
                And 1 = 1
            </when>
            <when test="tjfxVO.jglx == 4">
                And dept."id市局" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <when test="tjfxVO.jglx == 5">
                And dept."id分局" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <when test="tjfxVO.jglx == 6">
                And dept."id派出所" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <otherwise>
                And 1 = 2
            </otherwise>
        </choose>
        ORDER BY
        dept."id市局",
        dept."id分局",
        dept."id派出所",
        T1.jh
    </select>


    <select id="exportTjfxSjtj" resultType="org.springblade.modules.csInfo.excel.TjfxSjExcel">
        select *
        from public.tjfxsjtj t1
        where 1 = 1
        <choose>
            <when test="tjfxVO.jglx == 14">
                And 1 = 1
            </when>
            <when test="tjfxVO.jglx == 4">
                And t1.jgdm :: TEXT =#{tjfxVO.jgdm}
            </when>
            <otherwise>
                And 1 = 2
            </otherwise>
        </choose>
        order by t1.jgdm
    </select>


    <select id="exportTjfxFjtj" resultType="org.springblade.modules.csInfo.excel.TjfxFjExcel">
        select *
        from public.tjfxfjtj t1
        where 1 = 1
        <choose>
            <when test="tjfxVO.jglx == 14">
                And 1 = 1
            </when>
            <when test="tjfxVO.jglx == 4">
                And t1."sjid" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <when test="tjfxVO.jglx == 5">
                And t1."fjid" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <otherwise>
                And 1 = 2
            </otherwise>
        </choose>
        order by sjid,fjid
    </select>


    <select id="exportTjfxPcstj" resultType="org.springblade.modules.csInfo.excel.TjfxPcsExcel">
        select *
        from public.tjfxpcstj t1
        where 1 = 1
        <choose>
            <when test="tjfxVO.jglx == 14">
                And 1 = 1
            </when>
            <when test="tjfxVO.jglx == 4">
                And t1."sjid" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <when test="tjfxVO.jglx == 5">
                And t1."fjid" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <when test="tjfxVO.jglx == 6">
                And t1."pcsid" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <otherwise>
                And 1 = 2
            </otherwise>
        </choose>
        order by sjid,fjid,pcsid
    </select>


    <select id="exportTjfxMjtj" resultType="org.springblade.modules.csInfo.excel.TjfxMjExcel">
        select
        dept."id市局" sjid,
        dept."dept_name市局名" sj,
        dept."id分局" fjid,
        dept."dept_name分局名" fj,
        dept."id派出所" pcsid,
        dept."dept_name派出所名" pcs,
        t1.*
        from public.tjfxmjtj as t1
        JOIN public.v_dept_all dept ON dept.ID社区 :: TEXT = (string_to_array( T1.dept_id, ','))[1]
        --where dept."dept_name市局名" IS NOT NULL
        where 1 = 1
        <choose>
            <when test="tjfxVO.jglx == 14">
                And 1 = 1
            </when>
            <when test="tjfxVO.jglx == 4">
                And dept."id市局" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <when test="tjfxVO.jglx == 5">
                And dept."id分局" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <when test="tjfxVO.jglx == 6">
                And dept."id派出所" :: TEXT =#{tjfxVO.jgdm}
            </when>
            <otherwise>
                And 1 = 2
            </otherwise>
        </choose>
        ORDER BY
        dept."id市局",
        dept."id分局",
        dept."id派出所",
        T1.jh
    </select>

    <select id="getPublicCsInfo" resultType="org.springblade.modules.csInfo.entity.CsInfoEntity">
        select * from cs_info where id = #{id}
    </select>

    <insert id="addCsRl" parameterType="org.springblade.modules.csInfo.entity.CsRlb">
        INSERT INTO public.csrlb (id,cszj,rlsq ,csids)
        VALUES (#{id}, #{cszj}, #{rlsq},#{csids})
    </insert>


    <delete id="deleteByIds">
        delete from cs_info where id :: text = #{ids}
    </delete>
    <delete id="deleteRlIds">
        delete from public.csrlb where csids :: text = #{ids}
    </delete>

    <select id="selectCsById" resultMap="infoResultMap">
        with jc as (
        select *, row_number() over(partition by cs_id order by create_time desc) rn
        from csjc_info
        )
        SELECT
        temp.id,
        temp.csmc,
        temp.fzr,
        temp.fzr_dh,
        temp.xffzr,
        temp.xffzrlxdh,
        temp.xffzrsfzh,
        temp.dwlb,
        temp.tyxydm,
        temp.sssq_bm,
        temp.jg_id,
        temp.sssq_mc AS sssqName,
        temp.sspcs_mc AS sspcsName,
        temp.ssfj_mc AS ssfjName,
        temp.dz,
        temp.jzmj,
        temp.jzcs,
        temp.gd,
        temp.dwxl,
        temp.zczb,
        temp.clrq,
        temp.jyqx,
        temp.jyfw,
        temp.djjg,
        temp.djrq,
        temp.fwlx,
        temp.create_time,
        sj.dept_name as sssjName,
        jc.yjzt yjzt,
        CASE
        WHEN jc.zgzt IS NULL THEN '-1'::integer
        ELSE jc.zgzt
        END AS zgzt,
        CASE
        WHEN jc.zgzt IS NULL THEN '未检查'::text
        WHEN jc.zgzt = 0 THEN '不需要整改'::text
        WHEN jc.zgzt = 1 THEN '当场整改'::text
        WHEN jc.zgzt = 2 THEN '限期整改'::text
        WHEN jc.zgzt = 3 THEN '拒不整改'::text
        WHEN jc.zgzt = 4 THEN '逾期未整改'::text
        WHEN jc.zgzt = 5 THEN '重大火灾风险隐患'::text
        END AS jczt
        FROM
        cs_info temp
        LEFT JOIN blade_dept sq ON sq.id = temp.sssq_bm
        left join blade_dept fj on fj.id = temp.ssfj_bm
        left join blade_dept sj on fj.parent_id = sj.id
        left join jc on temp.id = jc.cs_id and jc.rn = 1
        where temp.is_deleted = '0' and temp.id = #{id}
    </select>

    <select id="getRlByCszjAndPcsbm" resultType="org.springblade.modules.csInfo.entity.CsRlb">
        select * from public.csrlb where cszj = #{param.id}::bigint and rlsq = #{param.sspcsBm}::bigint
    </select>

    <update id="getJxcsXf">
        update jdrh.cs_info set
        <if test="ssfjBm != null and ssfjBm != ''">ssfj_bm = #{ssfjBm},</if>
        <if test="ssfjMc != null and ssfjMc != ''">ssfj_mc = #{ssfjMc},</if>
        <if test="sspcsBm != null and sspcsBm != ''">sspcs_bm = #{sspcsBm},</if>
        <if test="sspcsMc != null and sspcsMc != ''">sspcs_mc = #{sspcsMc},</if>
        <if test="sssqBm != null and sssqBm != ''">sssq_bm = #{sssqBm},</if>
        <if test="sssqMc != null and sssqMc != ''">sssq_mc = #{sssqMc} </if>
        where id :: text = #{csId}
    </update>

    <select id="getCsLb" resultMap="infoResultMap">
        select
            info.id,
            info.csmc,
            info.fzr,
            info.fzr_dh,
            info.dwlb,
            info.ssfj_mc as ssfjName,
            info.sspcs_mc as sspcsName,
            info.sssq_mc as sssqName,
            sb.sbzt,
            bd.dept_name as deptName
        from jdrh.cs_info_sb sb
        left join jdrh.cs_info info on sb.cs_id = info.id
        left join jdrh.blade_dept bd on bd.id = sb.sbpcs
        where sb.sbdw = #{id} and sb.sbzt = '0'
        <if test="csInfoEntity.sbzt != null and csInfoEntity.sbzt != ''">
            and sb.sbzt = #{csInfoEntity.sbzt}
        </if>
    </select>

    <select id="selectSb" resultMap="infoResultMap">
        select
            info.csmc
        from jdrh.cs_info_sb sb
        left join jdrh.cs_info info on sb.cs_id = info.id
        where sb.sbzt = '0' and sb.cs_id :: text in
        <foreach collection="array" item="csIds" index="index" open="(" close=")" separator=",">
            #{csIds}
        </foreach>
    </select>

    <select id="getJxcsSc" resultType="org.springblade.modules.csInfo.vo.CsInfoVO">
        with sq as (
        select distinct id from jdrh.blade_dept
        where #{params.deptId} like '%' || id || '%'
        or ancestors like '%' || #{params.deptId} || '%'
        ), jc as (
        select csjc_info.*,
        row_number() over (partition by csjc_info.cs_id order by csjc_info.create_time desc) rn
        from jdrh.csjc_info
        left join sq on sq.id = csjc_info.create_dept
        where sq.id is not null
        )
        select
        temp.id,
        temp.csmc,
        temp.fzr,
        temp.fzr_dh,
        temp.xffzr,
        temp.xffzrlxdh,
        temp.xffzrsfzh,
        temp.dwlb,
        temp.tyxydm,
        temp.sssq_bm,
        temp.jg_id,
        temp.sssq_mc AS sssqName,
        temp.sspcs_mc AS sspcsName,
        temp.ssfj_mc AS ssfjName,
        temp.dz,
        temp.jzmj,
        temp.jzcs,
        temp.gd,
        temp.dwxl,
        temp.zczb,
        temp.clrq,
        temp.jyqx,
        temp.jyfw,
        temp.djjg,
        temp.djrq,
        temp.fwlx,
        jc.zgqx,
        jc.create_time AS time,
        sj.dept_name as sssjName, jc.yjzt yjzt,
        case
        when jc.zgzt is null then '-1'::integer
        else jc.zgzt
        end as zgzt,
        case
        when jc.zgzt is null then '未检查'::text
        when jc.zgzt = 0 then '不需要整改'::text
        when jc.zgzt = 1 then '当场整改'::text
        when jc.zgzt = 2 then '限期整改'::text
        when jc.zgzt = 3 then '拒不整改'::text
        when jc.zgzt = 4 then '逾期未整改'::text
        when jc.zgzt = 5 then '重大火灾风险隐患'::text
        end as jczt
        from sq
        left join (
        select * from jdrh.cs_info temp
        where
        is_deleted = 1
        <if test="null != params.csmc and '' != params.csmc">
            AND temp.csmc LIKE '%' || #{params.csmc} || '%'
        </if>
        <if test="null != params.cjr">
            AND temp.cjr = #{params.cjr} :: text
        </if>
        <if test="null != params.fzr and '' != params.fzr">
            AND temp.fzr LIKE '%' || #{params.fzr} || '%'
        </if>
        <if test="null != params.dz and '' != params.dz">
            AND temp.dz LIKE '%' || #{params.dz} || '%'
        </if>
        <if test="null != params.dwlb and '' != params.dwlb">
            AND temp.dwlb LIKE '%' || #{params.dwlb} || '%'
        </if>
        <if test="null != params.zgzt and '' != params.zgzt">
            AND temp.zgzt :: text = #{params.zgzt} :: text
            AND temp.dwlb = #{params.dwlb}
        </if>
        <if test="null != params.dwxl and '' != params.dwxl">
            AND temp.dwxl = #{params.dwxl}
        </if>
        <if test="null != params.fwlx and '' != params.fwlx">
            AND temp.fwlx = #{params.fwlx}
        </if>
        <if test="null != params.xffzr and '' != params.xffzr">
            AND temp.xffzr LIKE '%' || #{params.xffzr} || '%'
        </if>
        <if test="null != params.ssfjName and '' != params.ssfjName">
            AND temp.ssfj_bm :: text LIKE '%' || #{params.ssfjName} || '%'
        </if>
        <if test="null != params.sspcsName and '' != params.sspcsName">
            AND temp.sspcs_bm :: text LIKE '%' || #{params.sspcsName} || '%'
        </if>
        <if test="null != params.sssqName and '' != params.sssqName">
            AND temp.sssq_bm :: text LIKE '%' || #{params.sssqName} || '%'
        </if>
        ) temp on sq.id = temp.sssq_bm
        left join jdrh.blade_dept fj on	fj.id = temp.ssfj_bm
        left join jdrh.blade_dept sj on	fj.parent_id = sj.id
        left join jc on	temp.id = jc.cs_id	and jc.rn = 1
        where 1 = 1 AND temp.id IS NOT NULL
        <if test="null != params.sssjName and '' != params.sssjName">
            AND sj.id :: text LIKE '%' || #{params.sssjName} || '%'
        </if>
        <if test="null != params.jczt and '' != params.jczt and '-1' != params.jczt">
            AND jc.zgzt :: text = #{params.jczt} :: text
        </if>
        <if test="'-1' == params.jczt">
            AND jc.zgzt is null
        </if>
    </select>

    <update id="getJxcsHf">
        update jdrh.cs_info set is_deleted = 0 where
        <if test="ids != null">
            id :: text IN
            <foreach collection="ids" item="ids" separator="," open="(" close=")">
                #{ids}
            </foreach>
        </if>
    </update>

    <select id="selectShtyxydm" resultMap="infoResultMap">
        select * from jdrh.cs_info where tyxydm = #{tyxydm} and is_deleted = 0 and yyzt != 2 limit 1
    </select>

    <update id="updateLastJcId">
        update jdrh.cs_info set jc_id = #{jcId}, update_time = now() where id = #{csId}
    </update>

    <select id="getLdList" resultType="java.lang.Long">
        SELECT
            ci.ldphxlh
        FROM
            jdrh.cs_info ci
        LEFT JOIN jdrh.blade_dept sq ON sq.id = ci.sssq_bm
        WHERE
            ci.is_deleted = 0
        AND (( #{params.deptId} LIKE '%' || sq.id || '%' OR sq.ancestors LIKE '%' || #{params.deptId} || '%' ) OR ci.sssq_bm = #{params.gajgdm} :: int8)
        GROUP BY
            ci.ldphxlh
    </select>

    <update id="csLdGl">
        UPDATE jdrh.cs_info
        SET ldphxlh = #{ldphxlh} :: int8, update_time = now()
        WHERE
            id IN
        <foreach collection="ids" item="ids" separator="," open="(" close=")">
            #{ids} :: int8
        </foreach>
    </update>

    <update id="updateSspcs">
        update jdrh.cs_info set sspcs_bm = #{id},sspcs_mc = #{deptName} where sspcs_bm :: text = #{str}
    </update>

    <update id="getQxgl">
        update jdrh.cs_info set ldphxlh = null, update_time = now() where id = #{id}
    </update>

    <select id="getDztj" resultType="org.springblade.modules.csInfo.vo.InfoVO">
        select
            bd.id,
            bd.dept_name,
            count(ci.*) as cszs,
            count(ci.*) filter(where ci.ldphxlh is null) as wgls
        from
            (select * from jdrh.blade_dept where dept_category = '6'
            and ancestors like '%' || #{deptId} || '%'
            and is_deleted = 0) bd
           left join jdrh.cs_info ci on bd.id = ci.sspcs_bm and ci.is_deleted = 0
        group by bd.id,bd.dept_name
    </select>

    <select id="getDzxq" resultType="org.springblade.modules.csInfo.vo.CsInfoVO">
        SELECT
        ci.id,
        ci.csmc,
        ci.fzr,
        ci.fzr_dh,
        ci.xffzr,
        ci.xffzrlxdh,
        ci.xffzrsfzh,
        ci.dwlb,
        ci.tyxydm,
        ci.sssq_bm,
        ci.jg_id,
        ci.sssq_mc AS sssqName,
        ci.sspcs_mc AS sspcsName,
        ci.ssfj_mc AS ssfjName,
        ci.dz,
        ci.jzmj,
        ci.jzcs,
        ci.gd,
        ci.dwxl,
        ci.zczb,
        ci.clrq,
        ci.jyqx,
        ci.jyfw,
        ci.djjg,
        ci.djrq,
        ci.fwlx,
        ci.ldphxlh,
        jc.zgqx,
        jc.create_time AS time,
        sj.dept_name AS sssjName,
        jc.yjzt,
        case when jc.zgzt is null then '-1' :: integer else jc.zgzt end as zgzt,
        case
        when jc.zgzt is null then '未检查' :: text
        when jc.zgzt = 0 then '不需要整改' :: text
        when jc.zgzt = 1 then '当场整改' :: text
        when jc.zgzt = 2 then '限期整改' :: text
        when jc.zgzt = 3 then '拒不整改' :: text
        when jc.zgzt = 4 then '逾期未整改' :: text
        when jc.zgzt = 5 then '重大火灾风险隐患' :: text
        end as jczt,
        ld.sqjcwhmc,
        ld.jlxmc AS jlxxqzdzrcmc,
        ld.mlph,
        ld.ldph
        FROM
        jdrh.cs_info ci
        LEFT JOIN jdrh.csjc_info jc ON jc.ID = ci.jc_id
        LEFT JOIN jdrh.blade_dept sq ON sq.ID = ci.sspcs_bm
        LEFT JOIN jdrh.blade_dept fj on	fj.id = ci.ssfj_bm
        LEFT JOIN jdrh.blade_dept sj on	fj.parent_id = sj.id
        LEFT JOIN sspt.rk_ldphxx ld ON ld.xlh = ci.ldphxlh
        WHERE
        ci.is_deleted = 0 and ci.ldphxlh is null
        AND ( #{params.deptId} LIKE '%' || sq.ID || '%' OR sq.ancestors LIKE '%' || #{params.deptId} || '%' )
        <if test="null != params.csmc and '' != params.csmc">
            AND ci.csmc LIKE '%' || #{params.csmc} || '%'
        </if>
        <if test="null != params.cjr">
            AND ci.cjr = #{params.cjr} :: text
        </if>
        <if test="null != params.fzr and '' != params.fzr">
            AND ci.fzr LIKE '%' || #{params.fzr} || '%'
        </if>
        <if test="null != params.dz and '' != params.dz">
            AND ci.dz LIKE '%' || #{params.dz} || '%'
        </if>
        <if test="null != params.dwlb and '' != params.dwlb">
            AND ci.dwlb LIKE '%' || #{params.dwlb} || '%'
        </if>
        <if test="null != params.zgzt and '' != params.zgzt">
            AND ci.zgzt :: text = #{params.zgzt} :: text
            AND ci.dwlb = #{params.dwlb}
        </if>
        <if test="null != params.dwxl and '' != params.dwxl">
            AND ci.dwxl = #{params.dwxl}
        </if>
        <if test="null != params.fwlx and '' != params.fwlx">
            AND ci.fwlx = #{params.fwlx}
        </if>
        <if test="null != params.xffzr and '' != params.xffzr">
            AND ci.xffzr LIKE '%' || #{params.xffzr} || '%'
        </if>
        <if test="null != params.ssfjName and '' != params.ssfjName">
            AND ci.ssfj_bm = #{params.ssfjName} :: int8
        </if>
        <if test="null != params.sspcsName and '' != params.sspcsName">
            AND ci.sspcs_bm = #{params.sspcsName} :: int8
        </if>
        <if test="null != params.sssqName and '' != params.sssqName">
            AND ci.sssq_bm = #{params.sssqName} :: int8
        </if>
        <if test="null != params.sssjName and '' != params.sssjName">
            AND sj.id = #{params.sssjName} :: int8
        </if>
        <if test="null != params.jczt and '' != params.jczt and '-1' != params.jczt">
            AND jc.zgzt :: text = #{params.jczt} :: text
        </if>
        <if test="'-1' == params.jczt">
            AND jc.zgzt is null
        </if>
        <if test="null != params.ldphxlh and '' != params.ldphxlh">
            AND ci.ldphxlh = #{params.ldphxlh} :: int8
        </if>
        <if test="null != params.ld and 1 == params.ld">
            AND ci.ldphxlh IS NULL
        </if>
        ORDER BY
        jc.create_time,
        ci.csmc
    </select>

    <select id="exportDztj" resultType="org.springblade.modules.csInfo.excel.DztjExcel">
        select
            *
        from
            (
            select
                bd.sjname,
                bd.fjname,
                bd.pcsname,
                count(ci.*) as cszs,
                count(ci.*) filter(
                where ci.ldphxlh is null) as wgls
            from
                (
                select
                    pcs.id,
                    pcs.dept_name as pcsname,
                    fj.dept_name as fjname,
                    sj.dept_name as sjname
                from
                    jdrh.blade_dept pcs
                left join jdrh.blade_dept fj on
                    pcs.parent_id = fj.id
                left join jdrh.blade_dept sj on
                    fj.parent_id = sj.id
                where
                    pcs.dept_category = '6'
                    and pcs.ancestors like '%' || #{param.deptId} || '%'
                    and pcs.is_deleted = 0) bd
            left join jdrh.cs_info ci on
                bd.id = ci.sspcs_bm
                and ci.is_deleted = 0
            group by
                bd.id,
                bd.sjname,
                bd.fjname,
                bd.pcsname ) a
        where
            a.wgls != 0
    </select>

    <select id="getJxcsSl" parameterType="java.lang.Long" resultType="int">
        SELECT
            COUNT(id)
        FROM
            jdrh.cs_info
        WHERE
            create_user = #{userId}
          AND is_deleted = 0
    </select>

    <select id="getBySqBm" parameterType="java.lang.Long" resultType="org.springblade.modules.sqjw.jwtdzrq.entity.JwtdZrqEntity">
        SELECT
            *
        FROM
            jdrh.jwtd_zrq
        WHERE
            sssqbm = 220722109200
          AND is_deleted = 0
    </select>

    <select id="getTyxydm" parameterType="java.lang.String" resultType="org.springblade.modules.csInfo.entity.CsInfoEntity">
        SELECT
            id,
            csmc
        FROM
            jdrh.cs_info
        WHERE
            tyxydm = #{tyxydm}
          AND is_deleted = 0
          AND yyzt != 2 limit 1
    </select>

    <update id="deleteBySp" parameterType="org.springblade.modules.csInfo.entity.CsInfoEntity">
        UPDATE jdrh.cs_info
        <set>
            is_deleted = #{isDeleted},
            update_time = now(),
            <if test="updateUser != null">update_user = #{updateUser},</if>
        </set>
        WHERE id = #{id}
    </update>

    <update id="deleteBySpToJl" parameterType="org.springblade.modules.csInfo.entity.CsInfoEntity">
        UPDATE jdrh.cs_info
        <set>
            is_deleted = #{isDeleted},
            update_time = now(),
            <if test="updateUser != null">update_user = #{updateUser},</if>
        </set>
        WHERE id = #{id}
    </update>

    <update id="hfDeleted" parameterType="java.lang.Long">
        UPDATE jdrh.cs_info
        SET is_deleted = 0, yyzt = 2, update_time = now()
        WHERE id = #{id}
    </update>

    <insert id="saveToJl" parameterType="org.springblade.modules.csInfo.entity.CsInfoEntity">
        INSERT INTO "jdrh"."cs_info" (
            "id",
            "csmc",
            "dz",
            "dwlb",
            "dwxl",
            "fzr",
            "fzr_dh",
            "xffzr",
            "xffzrsfzh",
            "xffzrlxdh",
            "fwlx",
            "jzmj",
            "jzcs",
            "gd",
            "zczb",
            "clrq",
            "jyqx",
            "jyfw",
            "djjg",
            "djrq",
            "cjsj",
            "cjdw",
            "cjr",
            "ssfj_bm",
            "ssfj_mc",
            "sspcs_bm",
            "sspcs_mc",
            "sssq_bm",
            "sssq_mc",
            "create_user",
            "create_dept",
            "update_user",
            "update_time",
            "status",
            "is_deleted",
            "jd",
            "wd",
            "jg_id",
            "tyxydm",
            "jc_id",
            "ldphxlh",
            "yyzt"
        )
        VALUES
            (
                #{id},
                #{csmc},
                #{dz},
                #{dwlb},
                #{dwxl},
                #{fzr},
                #{fzrDh},
                #{xffzr},
                #{xffzrsfzh},
                #{xffzrlxdh},
                #{fwlx},
                #{jzmj},
                #{jzcs},
                #{gd},
                #{zczb},
                #{clrq},
                #{jyqx},
                #{jyfw},
                #{djjg},
                #{djrq},
                #{cjsj},
                #{cjdw},
                #{cjr},
                #{ssfjBm},
                #{ssfjMc},
                #{sspcsBm},
                #{sspcsMc},
                #{sssqBm},
                #{sssqMc},
                #{createUser},
                #{createDept},
                #{updateUser},
                #{updateTime},
                #{status},
                #{isDeleted},
                #{jd},
                #{wd},
                #{jgId},
                #{tyxydm},
                #{jcId},
                #{ldphxlh},
                #{yyzt}
            )
    </insert>

    <update id="updateByIdToJl" parameterType="org.springblade.modules.csInfo.entity.CsInfoEntity">
        UPDATE jdrh.cs_info
        <set>
            update_time = now(),
            <if test="null != csmc">csmc = #{csmc},</if>
            <if test="null != dz">dz = #{dz},</if>
            <if test="null != dwlb">dwlb = #{dwlb},</if>
            <if test="null != dwxl">dwxl = #{dwxl},</if>
            <if test="null != fzr">fzr = #{fzr},</if>
            <if test="null != fzrDh">fzr_dh = #{fzrDh},</if>
            <if test="null != xffzr">xffzr = #{xffzr},</if>
            <if test="null != xffzrsfzh">xffzrsfzh = #{xffzrsfzh},</if>
            <if test="null != xffzrlxdh">xffzrlxdh = #{xffzrlxdh},</if>
            <if test="null != fwlx">fwlx = #{fwlx},</if>
            <if test="null != jzmj">jzmj = #{jzmj},</if>
            <if test="null != jzcs">jzcs = #{jzcs},</if>
            <if test="null != gd">gd = #{gd},</if>
            <if test="null != zczb">zczb = #{zczb},</if>
            <if test="null != clrq">clrq = #{clrq},</if>
            <if test="null != jyqx">jyqx = #{jyqx},</if>
            <if test="null != jyfw">jyfw = #{jyfw},</if>
            <if test="null != djjg">djjg = #{djjg},</if>
            <if test="null != djrq">djrq = #{djrq},</if>
            <if test="null != cjsj">cjsj = #{cjsj},</if>
            <if test="null != cjdw">cjdw = #{cjdw},</if>
            <if test="null != cjr">cjr = #{cjr},</if>
            <if test="null != ssfjBm">ssfj_bm = #{ssfjBm},</if>
            <if test="null != ssfjMc">ssfj_mc = #{ssfjMc},</if>
            <if test="null != sspcsBm">sspcs_bm = #{sspcsBm},</if>
            <if test="null != sspcsMc">sspcs_mc = #{sspcsMc},</if>
            <if test="null != sssqBm">sssq_bm = #{sssqBm},</if>
            <if test="null != sssqMc">sssq_mc = #{sssqMc},</if>
            <if test="null != updateUser">update_user = #{updateUser},</if>
            <if test="null != isDeleted">is_deleted = #{isDeleted},</if>
            <if test="null != jd">jd = #{jd},</if>
            <if test="null != wd">wd = #{wd},</if>
            <if test="null != jgId">jg_id = #{jgId},</if>
            <if test="null != tyxydm">tyxydm = #{tyxydm},</if>
            <if test="null != jcId">jc_id = #{jcId},</if>
            <if test="null != ldphxlh">ldphxlh = #{ldphxlh},</if>
            <if test="null != yyzt">yyzt = #{yyzt},</if>
        </set>
        WHERE
            id = #{id}
    </update>

    <select id="countBySqBm" resultType="int">
        SELECT
            COUNT(*)
        FROM
            jdrh.cs_info ci
        LEFT JOIN jdrh.blade_dept sq ON sq.ID = ci.sssq_bm
        WHERE
            ci.is_deleted = 0
        AND (#{params.deptId} LIKE '%' || sq.id || '%' OR sq.ancestors LIKE '%' || #{params.deptId} || '%')
    </select>
</mapper>
