<?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.sems.safecheck.mapper.TSpecPersonweekcheckMapper">

    <resultMap type="TSpecPersonweekcheck" id="TSpecPersonweekcheckResult">
        <result property="id"    column="id"    />
        <result property="plantCode"    column="plant_code"    />
        <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="checkDate"    column="check_date"    />
        <result property="checkerId"    column="checker_id"    />
        <result property="weekNo"    column="week_no"    />
        <result property="lastWeekRisk"    column="last_week_risk"    />
        <result property="thisWeekRisk"    column="this_week_risk"    />
        <result property="safeCondition"    column="safe_condition"    />
        <result property="nextWeekWork"    column="next_week_work"    />
        <result property="isConfirm"    column="is_confirm"    />
        <result property="agentId"    column="agent_id"    />
    </resultMap>

    <sql id="selectTSpecPersonweekcheckVo">
        select d.id,s.nick_name as checkerName, d.agent_id,s1.nick_name as agentName,d.plant_code,d.is_confirm, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.check_date, d.checker_id, d.week_no, d.last_week_risk, d.this_week_risk, d.safe_condition, d.next_week_work from t_spec_personweekcheck d
          left join sys_user s on d.checker_id = s.user_id
          left join sys_user s1 on d.agent_id = s1.user_id
    </sql>

    <select id="selectTSpecPersonweekcheckList" parameterType="TSpecPersonweekcheck" resultMap="TSpecPersonweekcheckResult">
        <include refid="selectTSpecPersonweekcheckVo"/>
        <where>
            <if test="plantCode != null  and plantCode != ''"> and plant_code like concat(concat('%',  #{plantCode}), '%')</if>
            <if test="startDate != null ">and check_date <![CDATA[>=]]> #{startDate}</if>
            <if test="endDate != null">and check_date <![CDATA[<=]]>#{endDate}</if>
            <if test="weekNo != null  and weekNo != ''"> and week_no = #{weekNo}</if>
            <if test="checkDate != null "> and check_date = #{checkDate}</if>
            <if test="lastWeekRisk != null  and lastWeekRisk != ''"> and last_week_risk = #{lastWeekRisk}</if>
            <if test="thisWeekRisk != null  and thisWeekRisk != ''"> and this_week_risk = #{thisWeekRisk}</if>
            <if test="safeCondition != null  and safeCondition != ''"> and safe_condition = #{safeCondition}</if>
            <if test="nextWeekWork != null  and nextWeekWork != ''"> and next_week_work = #{nextWeekWork}</if>
            <if test="isConfirm != null  and isConfirm != ''"> and is_confirm = #{isConfirm}</if>
            <if test="plants != null  and plants != ''">
                and
                d.plant_code in
                <foreach collection="plants.split(',')" item="item" index="index"
                         open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScopePlant}
        order by d.check_date desc
    </select>

    <select id="selectMyList" parameterType="TSpecPersonweekcheck" resultMap="TSpecPersonweekcheckResult">
        <include refid="selectTSpecPersonweekcheckVo"/>
        <where>
            <if test="plantCode != null  and plantCode != ''"> and plant_code like concat(concat('%',  #{plantCode}), '%')</if>
            <if test="startDate != null ">and check_date <![CDATA[>=]]> #{startDate}</if>
            <if test="endDate != null">and check_date <![CDATA[<=]]>#{endDate}</if>
            <if test="weekNo != null  and weekNo != ''"> and week_no = #{weekNo}</if>
            <if test="checkDate != null "> and check_date = #{checkDate}</if>
            <if test="lastWeekRisk != null  and lastWeekRisk != ''"> and last_week_risk = #{lastWeekRisk}</if>
            <if test="thisWeekRisk != null  and thisWeekRisk != ''"> and this_week_risk = #{thisWeekRisk}</if>
            <if test="safeCondition != null  and safeCondition != ''"> and safe_condition = #{safeCondition}</if>
            <if test="nextWeekWork != null  and nextWeekWork != ''"> and next_week_work = #{nextWeekWork}</if>
            <if test="isConfirm != null  and isConfirm != ''"> and is_confirm = #{isConfirm}</if>
            <if test="checkerId != null  and checkerId != ''"> and checker_id = #{checkerId}</if>
            and d.del_flag = 0 and d.agent_id is not null
        </where>
        order by d.check_date desc
    </select>

    <select id="selectTSpecPersonweekcheckListByWeek" parameterType="TSpecPersonweekcheck" resultMap="TSpecPersonweekcheckResult">
        <include refid="selectTSpecPersonweekcheckVo"/>
        <where>
            <if test="weekNo != null  and weekNo != ''"> and d.week_no = #{weekNo}</if>
                    <if test="checkDate != null "> and TO_CHAR(d.check_date, 'YYYY') = TO_CHAR(#{checkDate}, 'YYYY')</if>
            and d.del_flag = 0
        </where>
    </select>

    <select id="selectTSpecPersonweekcheckById" parameterType="Long" resultMap="TSpecPersonweekcheckResult">
        <include refid="selectTSpecPersonweekcheckVo"/>
        where id = #{id}
    </select>

    <insert id="insertTSpecPersonweekcheck" parameterType="TSpecPersonweekcheck">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_spec_personweekcheck.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_spec_personweekcheck
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="plantCode != null">plant_code,</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="checkDate != null">check_date,</if>
            <if test="checkerId != null">checker_id,</if>
            <if test="weekNo != null">week_no,</if>
            <if test="lastWeekRisk != null">last_week_risk,</if>
            <if test="thisWeekRisk != null">this_week_risk,</if>
            <if test="safeCondition != null">safe_condition,</if>
            <if test="nextWeekWork != null">next_week_work,</if>
            <if test="agentId != null">agent_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="plantCode != null">#{plantCode},</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="checkDate != null">#{checkDate},</if>
            <if test="checkerId != null">#{checkerId},</if>
            <if test="weekNo != null">#{weekNo},</if>
            <if test="lastWeekRisk != null">#{lastWeekRisk},</if>
            <if test="thisWeekRisk != null">#{thisWeekRisk},</if>
            <if test="safeCondition != null">#{safeCondition},</if>
            <if test="nextWeekWork != null">#{nextWeekWork},</if>
            <if test="agentId != null">#{agentId},</if>
         </trim>
    </insert>

    <update id="updateTSpecPersonweekcheck" parameterType="TSpecPersonweekcheck">
        update t_spec_personweekcheck
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantCode != null">plant_code = #{plantCode},</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="checkDate != null">check_date = #{checkDate},</if>
            <if test="checkerId != null">checker_id = #{checkerId},</if>
            <if test="weekNo != null">week_no = #{weekNo},</if>
            <if test="lastWeekRisk != null">last_week_risk = #{lastWeekRisk},</if>
            <if test="thisWeekRisk != null">this_week_risk = #{thisWeekRisk},</if>
            <if test="safeCondition != null">safe_condition = #{safeCondition},</if>
            <if test="nextWeekWork != null">next_week_work = #{nextWeekWork},</if>
            <if test="agentId != null">agent_id = #{agentId},</if>
        </trim>
        where id = #{id}
    </update>

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

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

    <update id="batchConfirmByids" parameterType="String">
        update t_spec_personweekcheck set is_confirm = 1 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <update id="batchCancel" parameterType="String">
        update t_spec_personweekcheck set is_confirm = 0 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

</mapper>
