<?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.yhyj.mapper.YhyjMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="yhyjResultMap" type="org.springblade.modules.yhyj.entity.YhyjEntity">
        <result column="id" property="id"/>
        <result column="cs_id" property="csId"/>
        <result column="csjc_id" property="csjcId"/>
        <result column="jcd_id" property="jcdId"/>
        <result column="result" property="result"/>
        <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="selectYhyjPage" resultMap="yhyjResultMap">
        select
            yh.id,
            info.csmc as csmc,
            CASE
            csjc.zgzt
            WHEN '0' THEN
            '不需要整改'
            WHEN '1' THEN
            '现场整改'
            WHEN '2' THEN
            '限期整改'
            WHEN '3' THEN
            '拒不整改'
            WHEN '4' THEN
            '逾期未整改'
            WHEN '5' THEN
            '重大火灾风险隐患'
            END AS zgzt,
            yh.create_time,
            bu.name as userName
        from
            csjc_yhyj yh
        left join jdrh.cs_info info on yh.cs_id = info.id
        left join jdrh.csjc_info csjc on yh.csjc_id = csjc.id
        left join jdrh.blade_user bu on bu.id = yh.create_user
        left join jdrh.blade_dept bd on bd.id = yh.create_dept
        left join jdrh.blade_dept pcs on bd.parent_id = pcs.id
        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 yh.is_deleted = 0 and (bd.id :: text = #{yhyj.deptId} or bd.ancestors like '%' || #{yhyj.deptId} || '%')
        <if test="yhyj.csmc != null and yhyj.csmc != ''">
            and info.csmc like '%' || #{yhyj.csmc} || '%'
        </if>
        <if test="yhyj.zgzt != null and yhyj.zgzt != ''">
            and csjc.zgzt :: text = #{yhyj.zgzt}
        </if>
        <if test="yhyj.startTime != null and yhyj.startTime != '' and yhyj.endTime != null and yhyj.endTime != ''">
            and yh.create_time between #{yhyj.startTime} :: timestamp and #{yhyj.endTime} :: timestamp
        </if>
        <if test="yhyj.userName != null and yhyj.userName != ''">
            and bu.name like '%' || #{yhyj.userName} || '%'
        </if>
        <if test="yhyj.sssjBm != null and yhyj.sssjBm != ''">
            and sj.id = #{yhyj.sssjBm}
        </if>
        <if test="yhyj.ssfjBm != null and yhyj.ssfjBm != ''">
            and fj.id = #{yhyj.ssfjBm}
        </if>
        <if test="yhyj.sspcsBm != null and yhyj.sspcsBm != ''">
            and pcs.id = #{yhyj.sspcsBm}
        </if>
    </select>


    <select id="exportYhyj" resultType="org.springblade.modules.yhyj.excel.YhyjExcel">
        SELECT * FROM csjc_yhyj ${ew.customSqlSegment}
    </select>

    <update id="updateByJcdId" parameterType="org.springblade.modules.yhyj.entity.YhyjEntity">
        UPDATE jdrh.csjc_yhyj
        <set>
            update_time = now(),
            <if test="null != result and '' != result">
                result = #{result},
            </if>
        </set>
        WHERE jcd_id = #{jcdId}
    </update>

    <select id="getJxcsList" parameterType="org.springblade.modules.csInfo.entity.CsInfoEntity" resultType="org.springblade.modules.csInfo.entity.CsInfoEntity">
        SELECT
            jx.csmc,
            jx.fzr,
            jx.fzr_dh,
            jx.shtyxydm AS tyxydm
        FROM
            jdrh.cs_info ci
        LEFT JOIN PUBLIC.jxcs jx ON jx.sssq_bm = ci.sssq_bm :: TEXT
	                                AND jx.shtyxydm != ci.tyxydm
	                                AND (
	                                	jx.fzr_dh = ci.fzr_dh
	                                	OR jx.csmc = ci.csmc
	                                	OR jx.csmc LIKE'%' || ci.csmc || '%'
	                                	OR ci.csmc LIKE'%' || jx.csmc || '%'
	                                )
        WHERE
            ci.is_deleted = 0
          AND ci.sssq_bm = #{sssqBm}
          AND jx.csmc IS NOT NULL
        ORDER BY
            jx.csmc
    </select>
</mapper>
