<?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.project.ehs.mapper.TSafetyapprovalMapper">

    <resultMap type="TSafetyapproval" id="TSafetyapprovalResult">
        <result property="id"    column="id"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="approvalname"    column="approvalname"    />
        <result property="certno"    column="certno"    />
        <result property="effetivedate"    column="effetivedate"    />
        <result property="validityBefore"    column="validity_before"    />
        <result property="validityAfter"    column="validity_after"    />
        <result property="validity"    column="validity"    />
        <result property="relatedlaw"    column="relatedlaw"    />
        <result property="responsauth"    column="responsauth"    />
        <result property="owner"    column="owner"    />
        <result property="reviewdate"    column="reviewdate"    />
        <result property="nextreviewdate"    column="nextreviewdate"    />
        <result property="conttype"    column="conttype"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createrCode"    column="creater_code"    />
        <result property="createdate"    column="createdate"    />
        <result property="updaterCode"    column="updater_code"    />
        <result property="updatedate"    column="updatedate"    />
        <result property="deptId"    column="dept_id"    />
        <result property="remarks"    column="remarks"    />
        <result property="scope"    column="scope"    />
        <result property="follow"    column="follow"    />
        <result property="allowance"    column="allowance"    />
        <result property="requirements"    column="requirements"    />
        <result property="reviewer"    column="reviewer"    />
        <result property="reviewerName"    column="name"    />
        <result property="reviewResult"    column="review_result"    />
        <result property="content"    column="content"    />
        <result property="deptName" column="dept_name" />
        <result property="isPermanent" column="is_permanent"    />
        <result property="conPic"    column="con_pic"    />
        <result property="conUrl"    column="con_url"    />
    </resultMap>

    <sql id="selectTSafetyapprovalVo">
        select d.id, d.plant_code, d.approvalname, t.name, d.certno, d.effetivedate, d.validity_before, CONCAT(TO_CHAR(d.validity_before,'YYYY.MM.dd-'), TO_CHAR(d.validity_after,'YYYY.MM.dd')) AS validity, d.relatedlaw, d.responsauth, d.owner, d.reviewdate, d.nextreviewdate, d.conttype, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id, d.remarks, d.scope, d.follow, d.allowance, d.requirements, d.reviewer, d.validity_after, d.review_result , d.content, d.con_pic, d.con_url, d.is_permanent, s.dept_name from t_safetyapproval d
      left join sys_dept s on s.dept_id = d.dept_id
      left join t_staffmgr t on t.staffid = d.reviewer and t.del_flag = 0
    </sql>

    <select id="selectTSafetyapprovalList" parameterType="TSafetyapproval" resultMap="TSafetyapprovalResult">
        <include refid="selectTSafetyapprovalVo"/>
        <where>
            <if test="approvalname != null  and approvalname != ''"> and approvalname like concat(concat('%', #{approvalname}), '%')</if>
            <if test="certno != null  and certno != ''"> and certno like concat(concat('%', #{certno}), '%')</if>
            <if test="effetiveYear != null ">  and to_char(effetivedate,'yyyy') = #{effetiveYear}</if>
            <if test="reviewer != null">and reviewer = #{reviewer}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by d.id desc
    </select>

    <select id="selectList" parameterType="TSafetyapproval" resultMap="TSafetyapprovalResult">
        <include refid="selectTSafetyapprovalVo"/>
        <where>
            and d.del_flag = 0
            <if test="deptId != null "> and d.dept_id = #{deptId}</if>
            <if test="reviewer != null "> and d.reviewer = #{reviewer}</if>
        </where>
        <!-- 数据范围过滤 -->
        order by d.id desc
    </select>

    <select id="selectTSafetyapprovalById" parameterType="Long" resultMap="TSafetyapprovalResult">
        <include refid="selectTSafetyapprovalVo"/>
        where d.id = #{id}
    </select>

    <insert id="insertTSafetyapproval" parameterType="TSafetyapproval">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_safetyapproval.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_safetyapproval
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="plantCode != null and plantCode != ''">plant_code,</if>
            <if test="approvalname != null and approvalname != ''">approvalname,</if>
            <if test="certno != null and certno != ''">certno,</if>
            <if test="effetivedate != null">effetivedate,</if>
            <if test="validityBefore != null">validity_before,</if>
            <if test="relatedlaw != null">relatedlaw,</if>
            <if test="responsauth != null">responsauth,</if>
            <if test="owner != null">owner,</if>
            <if test="reviewdate != null">reviewdate,</if>
            <if test="nextreviewdate != null">nextreviewdate,</if>
            <if test="conttype != null">conttype,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createrCode != null">creater_code,</if>
            <if test="createdate != null">createdate,</if>
            <if test="updaterCode != null">updater_code,</if>
            <if test="updatedate != null">updatedate,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="remarks != null">remarks,</if>
            <if test="scope != null">scope,</if>
            <if test="follow != null">follow,</if>
            <if test="allowance != null">allowance,</if>
            <if test="requirements != null">requirements,</if>
            <if test="reviewer != null">reviewer,</if>
            <if test="validityAfter != null">validity_after,</if>
            <if test="reviewResult != null">review_result,</if>
            <if test="content != null">content,</if>
            <if test="isPermanent != null">is_permanent,</if>
            <if test="conPic != null">con_pic,</if>
            <if test="conUrl != null">con_url,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="plantCode != null and plantCode != ''">#{plantCode},</if>
            <if test="approvalname != null and approvalname != ''">#{approvalname},</if>
            <if test="certno != null and certno != ''">#{certno},</if>
            <if test="effetivedate != null">#{effetivedate},</if>
            <if test="validityBefore != null">#{validityBefore},</if>
            <if test="relatedlaw != null">#{relatedlaw},</if>
            <if test="responsauth != null">#{responsauth},</if>
            <if test="owner != null">#{owner},</if>
            <if test="reviewdate != null">#{reviewdate},</if>
            <if test="nextreviewdate != null">#{nextreviewdate},</if>
            <if test="conttype != null">#{conttype},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createrCode != null">#{createrCode},</if>
            <if test="createdate != null">#{createdate},</if>
            <if test="updaterCode != null">#{updaterCode},</if>
            <if test="updatedate != null">#{updatedate},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="scope != null">#{scope},</if>
            <if test="follow != null">#{follow},</if>
            <if test="allowance != null">#{allowance},</if>
            <if test="requirements != null">#{requirements},</if>
            <if test="reviewer != null">#{reviewer},</if>
            <if test="validityAfter != null">#{validityAfter},</if>
            <if test="reviewResult != null">#{reviewResult},</if>
            <if test="content != null">#{content},</if>
            <if test="isPermanent != null">#{isPermanent},</if>
            <if test="conPic != null">#{conPic},</if>
            <if test="conUrl != null">#{conUrl},</if>

        </trim>
    </insert>

    <update id="updateTSafetyapproval" parameterType="TSafetyapproval">
        update t_safetyapproval
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantCode != null and plantCode != ''">plant_code = #{plantCode},</if>
            <if test="approvalname != null and approvalname != ''">approvalname = #{approvalname},</if>
            <if test="certno != null and certno != ''">certno = #{certno},</if>
            <if test="effetivedate != null">effetivedate = #{effetivedate},</if>
            <if test="validityBefore != null">validity_before = #{validityBefore},</if>
            <if test="relatedlaw != null">relatedlaw = #{relatedlaw},</if>
            <if test="responsauth != null">responsauth = #{responsauth},</if>
            <if test="owner != null">owner = #{owner},</if>
            <if test="reviewdate != null">reviewdate = #{reviewdate},</if>
            <if test="nextreviewdate != null">nextreviewdate = #{nextreviewdate},</if>
            <if test="conttype != null">conttype = #{conttype},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createrCode != null">creater_code = #{createrCode},</if>
            <if test="createdate != null">createdate = #{createdate},</if>
            <if test="updaterCode != null">updater_code = #{updaterCode},</if>
            <if test="updatedate != null">updatedate = #{updatedate},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="scope != null">scope = #{scope},</if>
            <if test="follow != null">follow = #{follow},</if>
            <if test="allowance != null">allowance = #{allowance},</if>
            <if test="requirements != null">requirements = #{requirements},</if>
            <if test="reviewer != null">reviewer = #{reviewer},</if>
            <if test="validityAfter != null">validity_after = #{validityAfter},</if>
            <if test="reviewResult != null">review_result = #{reviewResult},</if>
            <if test="content != null">content = #{content},</if>
            <if test="conPic != null">con_pic = #{conPic},</if>
            <if test="conUrl != null">con_url = #{conUrl},</if>
            <if test="isPermanent != null">is_permanent = #{isPermanent},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteTSafetyapprovalById" parameterType="Long">
        update t_safetyapproval set del_flag = 2 where id = #{id}
    </update>

    <update id="deleteTSafetyapprovalByIds" parameterType="String">
        update t_safetyapproval set del_flag = 2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
