<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.ehsHiddenDanger.mapper.EhsHiddenDangerMapper">

    <resultMap type="EhsHiddenDanger" id="EhsHiddenDangerResult">
            <result property="hiddenDangerId"    column="hidden_danger_id"    />
            <result property="enterpriseName"    column="enterprise_name"    />
            <result property="enterpriseStatus"    column="enterprise_status"    />
            <result property="enterpriseType"    column="enterprise_type"    />
            <result property="hiddenDangerDesc"    column="hidden_danger_desc"    />
            <result property="checkTime"    column="check_time"    />
            <result property="checkType"    column="check_type"    />
            <result property="hiddenDangerImg"    column="hidden_danger_img"    />
            <result property="rectifyMeasure"    column="rectify_measure"    />
            <result property="status"    column="status"    />
            <result property="rectifyTime"    column="rectify_time"    />
            <result property="principalName"    column="principal_name"    />
            <result property="rectifyImg"    column="rectify_img"    />
            <result property="regulatoryDept"    column="regulatory_dept"    />
            <result property="regulatoryName"    column="regulatory_name"    />
            <result property="hiddenDangerGrade"    column="hidden_danger_grade"    />
            <result property="hiddenDangerType"    column="hidden_danger_type"    />
            <result property="hiddenDangerLabel"    column="hidden_danger_label"    />
        <result property="rectifyTermTime"    column="rectify_term_time"    />
        <result property="isSupervise"    column="is_supervise"    />
        <result property="assistDeptName"    column="assist_dept_name"    />
        <result property="assistDept"    column="assist_dept"    />
        <result property="superviseResult"    column="supervise_result"    />
        <result property="areaId"    column="area_id"    />
        <result property="economicType"    column="economic_type"    />
        <result property="enterpriseScale"    column="enterprise_scale"    />
    </resultMap>

    <sql id="selectEhsHiddenDangerVo">
        select h.*, d.dept_name,
        (SELECT GROUP_CONCAT( ft.dept_name SEPARATOR ';' )  FROM
	            sys_dept ft where FIND_IN_SET( ft.DEPT_ID, h.assist_dept ))assist_dept_name
         from ehs_hidden_danger h
           left join sys_dept d on h.dept_id = d.dept_id
    </sql>

    <select id="selectEhsHiddenDangerList" parameterType="EhsHiddenDanger" resultMap="EhsHiddenDangerResult">
        <include refid="selectEhsHiddenDangerVo"/>
        <where>
            <if test="deptId != null "> and h.dept_Id = #{deptId}</if>
            <if test="enterpriseName != null  and enterpriseName != ''"> and enterprise_name like concat('%', #{enterpriseName}, '%')</if>
            <if test="enterpriseStatus != null  and enterpriseStatus != ''"> and enterprise_status = #{enterpriseStatus}</if>
            <if test="enterpriseType != null  and enterpriseType != ''"> and enterprise_type = #{enterpriseType}</if>
            <if test="hiddenDangerDesc != null  and hiddenDangerDesc != ''"> and hidden_danger_desc like concat('%', #{hiddenDangerDesc}, '%')</if>
            <if test="params.beginCheckTime != null and params.beginCheckTime != '' and params.endCheckTime != null and params.endCheckTime != ''">
                    and check_time between #{params.beginCheckTime} and #{params.endCheckTime}</if>
            <if test="checkType != null  and checkType != ''"> and check_type like concat('%', #{checkType}, '%')</if>
            <if test="hiddenDangerImg != null  and hiddenDangerImg != ''"> and hidden_danger_img = #{hiddenDangerImg}</if>
            <if test="rectifyMeasure != null  and rectifyMeasure != ''"> and rectify_measure like concat('%', #{rectifyMeasure}, '%')</if>
            <if test="status != null  and status != ''"> and status like concat('%', #{status}, '%')</if>
            <if test="params.beginRectifyTime != null and params.beginRectifyTime != '' and params.endRectifyTime != null and params.endRectifyTime != ''">
                    and rectify_time between #{params.beginRectifyTime} and #{params.endRectifyTime}</if>
            <if test="principalName != null "> and principal_name like concat('%', #{principalName}, '%')</if>
            <if test="rectifyImg != null  and rectifyImg != ''"> and rectify_img = #{rectifyImg}</if>
            <if test="regulatoryDept != null  and regulatoryDept != ''"> and regulatory_dept = #{regulatoryDept}</if>
            <if test="regulatoryName != null  and regulatoryName != ''"> and regulatory_name like concat('%', #{regulatoryName}, '%')</if>
            <if test="hiddenDangerGrade != null  and hiddenDangerGrade != ''"> and hidden_danger_grade = #{hiddenDangerGrade}</if>
            <if test="hiddenDangerType != null  and hiddenDangerType != ''"> and hidden_danger_type = #{hiddenDangerType}</if>
            <if test="assistDept != null  and assistDept != ''"> and FIND_IN_SET( #{assistDept},  assist_dept )</if>
            <if test="areaId != null  and areaId != ''">and area_id = #{areaId}</if>
            <if test="hiddenDangerLabelArray != null  ">
                AND
                <foreach collection="hiddenDangerLabelArray" item="item" separator="OR" index="index" open="(" close=")">
                    FIND_IN_SET(#{item}, hidden_danger_label)
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectEhsHiddenDangerByHiddenDangerId" parameterType="Long" resultMap="EhsHiddenDangerResult">
            <include refid="selectEhsHiddenDangerVo"/>
            where hidden_danger_id = #{hiddenDangerId}
    </select>

    <update id="superviseEhsHiddenDanger" parameterType="Long">
        update ehs_hidden_danger set is_supervise = 1 where hidden_danger_id in
        <foreach collection="array" item="hiddenDangerId" open="(" separator="," close=")">
            #{hiddenDangerId}
        </foreach>
    </update>
    <update id="assistEhsHiddenDanger"  parameterType="EhsHiddenDanger">
        update ehs_hidden_danger set assist_dept = #{assistDept}  where hidden_danger_id  =#{hiddenDangerId}
    </update>

</mapper>