<?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.csjcInfo.mapper.CsjcInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="infoResultMap" type="org.springblade.modules.csjcInfo.entity.CsjcInfoEntity">
    </resultMap>

    <select id="selectInfoPage" resultType="org.springblade.modules.csjcInfo.vo.CsjcInfoVO">
        SELECT
            jc.*,
        ci.sssq_mc AS sssqName,
        ci.sspcs_mc AS sspcsName,
        ci.ssfj_mc AS ssfjName,
        CASE
        jc.zgzt
        WHEN '0' THEN
        '不需要整改'
        WHEN '1' THEN
        '现场整改'
        WHEN '2' THEN
        '限期整改'
        WHEN '3' THEN
        '拒不整改'
        WHEN '4' THEN
        '逾期未整改'
        WHEN '5' THEN
        '重大火灾风险隐患'
            END AS zgzts,
            jc.zgqx,
            jc.create_time AS TIME,
            ci.csmc,
            ci.dwlb,
            bu.phone,
            bu.real_name,
            pcs.dept_name AS jcdw
        FROM
            csjc_info jc
            LEFT JOIN cs_info ci ON ci.id = jc.cs_id
            LEFT JOIN blade_dept sq ON sq.id = ci.sssq_bm
            LEFT JOIN blade_dept pcs ON pcs.id = sq.parent_id
            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 blade_user bu ON bu.id = jc.create_user
        WHERE
            jc.is_deleted = 0 And ci.is_deleted = 0
            AND ((#{params.deptId} LIKE '%' || sq.id || '%') OR (sq.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.type and '' != params.type">
            AND jc.zgzt IN (3, 4, 5)
        </if>
        <if test="null != params.ssfjName and '' != params.ssfjName">
            AND ci.ssfj_bm :: text LIKE '%' || #{params.ssfjName} || '%'
        </if>
        <if test="null != params.sspcsName and '' != params.sspcsName">
            AND ci.sspcs_bm :: text LIKE '%' || #{params.sspcsName} || '%'
        </if>
        <if test="null != params.sssqName and '' != params.sssqName">
            AND ci.sssq_bm :: text LIKE '%' || #{params.sssqName} || '%'
        </if>
        <if test="null != params.sssjName and '' != params.sssjName">
            AND sj.id :: text LIKE '%' || #{params.sssjName} || '%'
        </if>
        <if test="null != params.zgzt and '' != params.zgzt">
            AND jc.zgzt :: text = #{params.zgzt}
        </if>

        <if test="null != params.csmc and '' != params.csmc">
            AND ci.csmc LIKE '%' || #{params.csmc} || '%'
        </if>
        <if test="null != params.realName and '' != params.realName">
            AND bu.real_name LIKE '%' || #{params.realName} || '%'
        </if>
        <if test="null != params.deptName and '' != params.deptName">
            AND bd.dept_name LIKE '%' || #{params.deptName} || '%'
        </if>
        <if test="null != str and '' != str and null != strEnd and '' != strEnd">
            AND jc.create_time :: TEXT BETWEEN #{str} :: TEXT AND #{strEnd} :: TEXT
        </if>
        <if test="null != params.jcdw and '' != params.jcdw">
            AND pcs.dept_name LIKE '%' || #{params.jcdw} || '%'
        </if>
        ORDER BY
            jc.create_time DESC
    </select>

    <select id="exportInfo" resultType="org.springblade.modules.csjcInfo.excel.CsjcInfoExcel">
        SELECT
            ci.sssq_mc AS sssqName,
            ci.sspcs_mc AS sspcsName,
            ci.ssfj_mc AS ssfjName,
            jc.create_time AS time,
            ci.csmc,
            ci.dwlb,
            pcs.dept_name,
            bu.real_name,
            bu.phone,
            jc.stry,
        CASE
        jc.zgzt
        WHEN '0' THEN
        '不需要整改'
        WHEN '1' THEN
        '现场整改'
        WHEN '2' THEN
        '限期整改'
        WHEN '3' THEN
        '拒不整改'
        WHEN '4' THEN
        '逾期未整改'
        WHEN '5' THEN
        '重大火灾风险隐患'
            END AS zgzts,
            jc.zgqx,
            jc.bgsy,
            jc.aqzd,
            jc.aqjypx,
            jc.fhjc,
            jc.ssya,
            jc.xfyl,
            jc.xfaqgl_qtqk,
            jc.xfctd,
            jc.sstd,
            jc.aqck,
            jc.fhm,
            jc.sszsbz,
            jc.yjzm,
            jc.zaw,
            jc.jzfh_qtqk,
            jc.snxhs,
            jc.mhq,
            jc.jzxfss,
            jc.mh,
            jc.wxpcs,
            jc.jzcs,
            jc.bz
        FROM
            csjc_info jc
        LEFT JOIN cs_info ci ON ci.id = jc.cs_id
        LEFT JOIN blade_dept sq ON sq.id = ci.sssq_bm
        LEFT JOIN blade_dept pcs ON pcs.id = sq.parent_id
        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 blade_user bu ON bu.id = jc.create_user
        WHERE
            jc.is_deleted = 0 And ci.is_deleted = 0
            AND ((#{params.deptId} LIKE '%' || sq.id || '%') OR (sq.ancestors LIKE '%' || #{params.deptId} || '%'))
        <if test="null != params.type and '' != params.type">
            AND jc.zgzt IN (3, 4, 5)
        </if>
        <if test="null != params.zgzt and '' != params.zgzt">
            AND jc.zgzt :: text = #{params.zgzt}
        </if>
        <if test="null != params.csmc and '' != params.csmc">
            AND ci.csmc LIKE '%' || #{params.csmc} || '%'
        </if>
        <if test="null != params.realName and '' != params.realName">
            AND bu.real_name LIKE '%' || #{params.realName} || '%'
        </if>
        <if test="null != params.ssfjName and '' != params.ssfjName">
            AND ci.ssfj_bm :: text LIKE '%' || #{params.ssfjName} || '%'
        </if>
        <if test="null != params.sspcsName and '' != params.sspcsName">
            AND ci.sspcs_bm :: text LIKE '%' || #{params.sspcsName} || '%'
        </if>
        <if test="null != params.sssqName and '' != params.sssqName">
            AND ci.sssq_bm :: text LIKE '%' || #{params.sssqName} || '%'
        </if>
        <if test="null != params.sssjName and '' != params.sssjName">
            AND sj.id :: text LIKE '%' || #{params.sssjName} || '%'
        </if>
        <if test="null != params.deptName and '' != params.deptName">
            AND bd.dept_name LIKE '%' || #{params.deptName} || '%'
        </if>
        <if test="null != params.jcdw and '' != params.jcdw">
            AND bd.dept_name LIKE '%' || #{params.jcdw} || '%'
        </if>
        <if test="null != params.timeBegin and '' != params.timeBegin and null != params.timeEnd and '' != params.timeEnd">
            AND jc.create_time :: TEXT BETWEEN #{params.timeBegin} :: TEXT AND #{params.timeEnd} :: TEXT
        </if>
        ORDER BY
            jc.create_time DESC
    </select>

    <select id="getDetail" resultType="org.springblade.modules.csjcInfo.vo.CsjcInfoVO">
        SELECT
            jc.*,
            case jc.zgzt when '0' then '不需要整改' when '1' then '现场整改' when '2' then '限期整改' when '3' then '拒不整改' when '4' then '逾期未整改' when '5' then '重大火灾风险隐患' end as zgzts,
            case jc.fjlzzt when '0' then '未流转' when '1' then '流转至分局' when '2' then '分局已退回' when '3' then '分局移交至县区消防大队' when '4' then '消防大队已签收' when '5' then '消防大队已反馈' when '6' then '消防大队已分配办事员' when '7' then '办事员已签收' when '8' then '办事员已反馈' end as fjlzzts,
            case jc.xjwlzzt when '0' then '未移交' when '1' then '已移交给街道、乡镇消救委' when '2' then '街道、乡镇消救委已签收' when '3' then '街道消救委已整改完成' when '4' then '已移交县区消救委' when '5' then '县区消救委已签收' when '6' then '县区消救委已完成' end as xjwlzzts,
            jc.create_time AS time,
            jc.update_time,
            jc.zgqx,
            jc.zgtsdzp,
		    ci.csmc,
		    ci.dz,
		    ci.dwlb,
		    ci.jzmj,
		    ci.jzcs as jzjtcs,
		    ci.gd,
		    ci.xffzr,
		    ci.xffzrlxdh,
            bu.real_name,
            bu.phone,
            pcs.dept_name as jcdw,
            bde.dept_name AS yjdwName
        FROM
            csjc_info jc
        LEFT JOIN cs_info ci ON ci.id = jc.cs_id
        LEFT JOIN blade_user bu ON bu.id = jc.create_user
        LEFT JOIN blade_dept sq ON sq.id = ci.sssq_bm
        LEFT JOIN blade_dept pcs ON pcs.id = sq.parent_id
        LEFT JOIN blade_dept bde ON bde.id = jc.yjdw
        WHERE
            jc.id = #{id}
    </select>

    <select id="getPageByCsId" resultType="org.springblade.modules.csjcInfo.vo.CsjcInfoVO">
        SELECT
            jc.*,
            ci.tyxydm,
            yh.jcd_id
        FROM
            jdrh.csjc_info jc
        LEFT JOIN jdrh.cs_info ci ON ci.id = jc.cs_id
        LEFT JOIN jdrh.csjc_yhyj yh ON yh.csjc_id = jc.id
        WHERE
            jc.cs_id = #{params.csId} :: int8
          AND jc.is_deleted = 0
        ORDER BY
            jc.create_time DESC
    </select>

    <select id="getQsfkList" resultType="org.springblade.modules.csjcInfo.vo.CsjcInfoVO">
        SELECT
            jc.id,
            jc.yjzt AS thzt,
            jc.create_time,
            jc.update_time,
            ci.csmc,
            ci.dwlb,
            pcs.dept_name,
            pcs.dept_name as jcdw,
            bu.real_name,
            bu.phone,
            jc.zgzt,
            jc.yjrq,
            jc.bgsy,
            jc.fjlzzt,
            jc.xjwlzzt,
            ( SELECT cy.zt FROM csjc_yjjl cy WHERE cy.csjc_id = jc.id AND cy.create_dept :: text = #{params.deptId} ORDER BY cy.create_time DESC LIMIT 1 ) AS yjzt
        FROM
            csjc_info jc
        LEFT JOIN cs_info ci ON ci.ID = jc.cs_id
        LEFT JOIN blade_user bu ON bu.id = jc.create_user
        LEFT JOIN blade_dept sq ON sq.id = ci.sssq_bm
        LEFT JOIN blade_dept pcs ON pcs.id = sq.parent_id
        WHERE
            jc.is_deleted = 0
        AND ((#{params.deptId} LIKE '%' || jc.create_dept || '%') OR (sq.ancestors LIKE '%' || #{params.deptId} || '%')) AND jc.yjzt != 0
        AND jc.zgzt IN ( 3, 4, 5 )
        <if test="null != params.yjzt and '' != params.yjzt">
            AND jc.yjzt :: text = #{params.yjzt} ::text
        </if>
        <if test="null != params.fjlzzt and '' != params.fjlzzt">
            AND jc.fjlzzt :: text = #{params.fjlzzt}
        </if>
        <if test="null != params.xjwlzzt and '' != params.xjwlzzt">
            AND jc.xjwlzzt :: text = #{params.xjwlzzt}
        </if>
        <if test="null != params.csmc and '' != params.csmc">
            AND ci.csmc like '%' || #{params.csmc} || '%'
        </if>
        <if test="null != params.realName and '' != params.realName">
            AND bu.real_name like '%' || #{params.realName} || '%'
        </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 jc.zgzt :: text = #{params.zgzt}
        </if>
        <if test="null != params.timeBegin and '' != params.timeBegin and null != params.timeEnd and '' != params.timeEnd">
            AND jc.yjrq :: text BETWEEN #{params.timeBegin} AND #{params.timeEnd}
        </if>
        ORDER BY
            jc.yjrq DESC
    </select>

    <select id="getZgList" resultType="org.springblade.modules.csjcInfo.vo.CsjcInfoVO">
        SELECT
            jc.id,
            jc.yjzt,
            ci.csmc,
            ci.dwlb,
            pcs.dept_name,
            bu.real_name,
            bu.phone,
            jc.zgzt,
            jc.yjrq,
            jc.bgsy
        FROM
            csjc_info jc
        LEFT JOIN cs_info ci ON ci.ID = jc.cs_id
        LEFT JOIN csjc_yjjl cy ON (cy.csjc_id = jc.id AND cy.yjdw :: text = #{params.deptId} AND cy.zt = 4)
        LEFT JOIN blade_user bu ON bu.id = jc.create_user
        LEFT JOIN blade_dept sq ON sq.id = ci.sssq_bm
        LEFT JOIN blade_dept pcs ON pcs.id = sq.parent_id
        WHERE
            jc.is_deleted = 0
        AND cy.id IS NOT NULL
        <if test="null != params.yjzt and '' != params.yjzt">
            AND jc.yjzt :: text = #{params.yjzt} ::text
        </if>
        <if test="null != params.csmc and '' != params.csmc">
            AND ci.csmc like '%' || #{params.csmc} || '%'
        </if>
        <if test="null != params.realName and '' != params.realName">
            AND bu.real_name like '%' || #{params.realName} || '%'
        </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 jc.zgzt :: text = #{params.zgzt}
        </if>
        <if test="null != params.timeBegin and '' != params.timeBegin and null != params.timeEnd and '' != params.timeEnd">
            AND jc.yjrq :: text BETWEEN #{params.timeBegin} AND #{params.timeEnd}
        </if>
        ORDER BY
            jc.yjrq DESC
    </select>

    <select id="getSqId" resultType="java.lang.Long">
        SELECT
            ci.sssq_bm
        FROM
            csjc_info jc
        LEFT JOIN cs_info ci ON ci.id = jc.cs_id
        WHERE
            jc.id :: text = #{id}
    </select>
    <select id="getPublicCsjcInfo" resultType="org.springblade.modules.csjcInfo.entity.CsjcInfoEntity">
        select * from csjc_info where id = #{id}
    </select>

    <select id="getPublicCsjcList" resultType="org.springblade.modules.csjcInfo.entity.CsjcInfoEntity">
        select * from public.csjc_info where cs_id = #{csId} and is_deleted = '0'
    </select>

    <select id="getJidu" resultType="java.lang.String">
        with recursive nf(this_year, this_month, this_day) as (
        select date_part('year', now()), 1 , 1
        union all
        select this_year, this_month + 1, this_day
        from nf
        where this_month &lt; 12
        )
        select * from (
        select
        *,
        row_number() over(partition by jd order by this_date asc) as rn
        from (
        select
        concat(this_year, '-', this_month, '-', this_day)::date as this_date,
        date_part('quarter',concat(this_year, '-', this_month, '-', this_day)::date) as jd
        from nf
        ) t
        ) t
        where rn = 1
    </select>

    <update id="updateCreateTime">
        UPDATE jdrh.csjc_info
        SET create_time = #{day}
        WHERE
            id = #{id}
    </update>
</mapper>
