<?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.production.mapper.TSaiApplyMapper">
    
    <resultMap type="TSaiApply" id="TSaiApplyResult">
        <result property="saiApplyId"    column="sai_apply_id"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="deptId"    column="dept_id"    />
        <result property="applyStatus"    column="apply_status"    />
        <result property="apNo"    column="ap_no"    />
        <result property="processId"    column="process_id"    />
        <result property="applicant"    column="applicant"    />
        <result property="assessor"    column="assessor"    />
        <result property="executor"    column="executor"    />
        <result property="inspectors"    column="inspectors"    />
        <result property="applicantDept"    column="applicant_dept"    />
        <result property="applicantTeam"    column="applicant_team"    />
        <result property="description"    column="description"    />
        <result property="unsafeStatus"    column="unsafe_status"    />
        <result property="unsafeAction"    column="unsafe_action"    />
        <result property="applyDate"    column="apply_date"    />
        <result property="taskId"    column="task_id"    />
        <result property="taskName"    column="task_name"    />
        <result property="handler"    column="handler"    />
        <result property="estimateFinishDate"    column="estimate_finish_date"    />
        <result property="actualFinishDate"    column="actual_finish_date"    />
        <result property="isRecorded"    column="is_recorded"    />
        <result property="recordNo"    column="record_no"    />
        <result property="reaction"    column="reaction"    />
        <result property="needVe"    column="need_ve"    />
        <result property="veItems"    column="ve_items"    />
        <result property="veResult"    column="ve_result"    />
        <result property="remarks"    column="remarks"    />
        <result property="veItemOther"    column="ve_item_other"    />
        <result property="saiLevel"    column="sai_level"    />
        <result property="category"    column="category"    />
        <result property="remarksAssess"    column="remarks_assess"    />
        <result property="remarksExecute"    column="remarks_execute"    />
        <result property="workArea"    column="work_area"    />
    </resultMap>

    <sql id="selectTSaiApplyVo">
        select d.sai_apply_id, d.del_flag, d.create_by, d.create_time, d.update_by, d.update_time, d.dept_id, d.apply_status, d.ap_no, d.process_id,
        d.applicant, d.assessor, d.executor, d.inspectors, d.applicant_dept, d.applicant_team, d.description,
        d.unsafe_status, d.unsafe_action, d.apply_date, d.task_id, d.task_name,d.remarks, d.ve_item_other,
        d.handler, d.estimate_finish_date, d.actual_finish_date, d.is_recorded, d.record_no, d.reaction, d.need_ve, d.ve_items, d.ve_result,
        d.sai_level, d.category, d.remarks_assess, d.remarks_execute, d.work_area
        from t_sai_apply d
        left join sys_dept s on s.dept_id = d.dept_id
    </sql>

    <select id="selectTSaiApplyList"
            parameterType="com.ruoyi.project.production.controller.vo.SaiApplyQueryVO"
            resultMap="TSaiApplyResult">
        <include refid="selectTSaiApplyVo"/>
        <where>
            <if test="tab == 1">
                and handler like concat(concat('%', #{currentUser}), '%')
                and apply_status != 4
                and apply_status != 5
            </if>
            <if test="tab == 2">
                and (handler like concat(concat('%', #{currentUser}), '%')
                or assessor like concat(concat('%', #{currentUser}), '%')
                or executor like concat(concat('%', #{currentUser}), '%')
                or inspectors like concat(concat('%', #{currentUser}), '%'))
            </if>
            <if test="isSpecialRole == true">
                and apply_status != 5
            </if>
            <if test="applyDateStart != null ">and apply_date <![CDATA[>=]]> #{applyDateStart}</if>
            <if test="applyDateEnd != null">and apply_date <![CDATA[<=]]>#{applyDateEnd}</if>
            <if test="estimateFinishDateStart != null ">and estimate_finish_date <![CDATA[>=]]> #{estimateFinishDateStart}</if>
            <if test="estimateFinishDateEnd != null">and estimate_finish_date <![CDATA[<=]]>#{estimateFinishDateEnd}</if>
            <if test="saiApplyId != null "> and sai_apply_id = #{saiApplyId}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="applyStatus != null  and applyStatus != ''"> and apply_status = #{applyStatus}</if>
            <if test="apNo != null  and apNo != ''"> and ap_no = #{apNo}</if>
            <if test="processId != null  and processId != ''"> and process_id = #{processId}</if>
            <if test="applicant != null  and applicant != ''"> and applicant = #{applicant}</if>
            <if test="assessor != null  and assessor != ''"> and assessor = #{assessor}</if>
            <if test="executor != null  and executor != ''"> and executor = #{executor}</if>
            <if test="inspectors != null  and inspectors != ''"> and inspectors = #{inspectors}</if>
            <if test="applicantDept != null  and applicantDept != ''"> and applicant_dept = #{applicantDept}</if>
            <if test="applicantTeam != null  and applicantTeam != ''"> and applicant_team = #{applicantTeam}</if>
            <if test="description != null  and description != ''"> and description like concat(concat('%', #{description}), '%') </if>
            <if test="unsafeStatus != null "> and unsafe_status = #{unsafeStatus}</if>
            <if test="unsafeAction != null "> and unsafe_action = #{unsafeAction}</if>
            <if test="applyDate != null "> and apply_date = #{applyDate}</if>
            <if test="taskId != null  and taskId != ''"> and task_id = #{taskId}</if>
            <if test="taskName != null  and taskName != ''"> and task_name like concat(concat('%', #{taskName}), '%')</if>
            <if test="handler != null  and handler != ''"> and handler = #{handler}</if>
            <if test="estimateFinishDate != null "> and estimate_finish_date = #{estimateFinishDate}</if>
            <if test="actualFinishDate != null "> and actual_finish_date = #{actualFinishDate}</if>
            <if test="isRecorded != null "> and is_recorded = #{isRecorded}</if>
            <if test="recordNo != null  and recordNo != ''"> and record_no = #{recordNo}</if>
            <if test="reaction != null  and reaction != ''"> and reaction = #{reaction}</if>
            <if test="needVe != null "> and need_ve = #{needVe}</if>
            <if test="veItems != null  and veItems != ''"> and ve_items = #{veItems}</if>
            <if test="veResult != null  and veResult != ''"> and ve_result = #{veResult}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            <if test="veItemOther != null  and veItemOther != ''"> and ve_item_other = #{veItemOther}</if>
            <if test="saiLevel != null "> and sai_level = #{saiLevel}</if>
            <if test="category != null  and category != ''"> and category = #{category}</if>
            <if test="remarksAssess != null  and remarksAssess != ''"> and remarks_assess = #{remarksAssess}</if>
            <if test="remarksExecute != null  and remarksExecute != ''"> and remarks_execute = #{remarksExecute}</if>
            <if test="workArea != null  and workArea != ''"> and work_area = #{workArea}</if>
            and d.del_flag = 0
        </where>
        order by APPLY_DATE desc
    </select>
    
    <select id="selectTSaiApplyById" parameterType="Long" resultMap="TSaiApplyResult">
        <include refid="selectTSaiApplyVo"/>
        where sai_apply_id = #{saiApplyId}
    </select>
        
    <insert id="insertTSaiApply" parameterType="TSaiApply">
        <selectKey keyProperty="saiApplyId" resultType="long" order="BEFORE">
            SELECT seq_t_sai_apply.NEXTVAL as saiApplyId FROM DUAL
        </selectKey>
        insert into t_sai_apply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="saiApplyId != null">sai_apply_id,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="applyStatus != null">apply_status,</if>
            <if test="apNo != null">ap_no,</if>
            <if test="processId != null">process_id,</if>
            <if test="applicant != null">applicant,</if>
            <if test="assessor != null">assessor,</if>
            <if test="executor != null">executor,</if>
            <if test="inspectors != null">inspectors,</if>
            <if test="applicantDept != null">applicant_dept,</if>
            <if test="applicantTeam != null">applicant_team,</if>
            <if test="description != null">description,</if>
            <if test="unsafeStatus != null">unsafe_status,</if>
            <if test="unsafeAction != null">unsafe_action,</if>
            <if test="applyDate != null">apply_date,</if>
            <if test="taskId != null">task_id,</if>
            <if test="taskName != null">task_name,</if>
            <if test="handler != null">handler,</if>
            <if test="estimateFinishDate != null">estimate_finish_date,</if>
            <if test="actualFinishDate != null">actual_finish_date,</if>
            <if test="isRecorded != null">is_recorded,</if>
            <if test="recordNo != null">record_no,</if>
            <if test="reaction != null">reaction,</if>
            <if test="needVe != null">need_ve,</if>
            <if test="veItems != null">ve_items,</if>
            <if test="veResult != null">ve_result,</if>
            <if test="remarks != null">remarks,</if>
            <if test="veItemOther != null">ve_item_other,</if>
            <if test="saiLevel != null">sai_level,</if>
            <if test="category != null">category,</if>
            <if test="remarksAssess != null">remarks_assess,</if>
            <if test="remarksExecute != null">remarks_execute,</if>
            <if test="workArea != null">work_area,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="saiApplyId != null">#{saiApplyId},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="applyStatus != null">#{applyStatus},</if>
            <if test="apNo != null">#{apNo},</if>
            <if test="processId != null">#{processId},</if>
            <if test="applicant != null">#{applicant},</if>
            <if test="assessor != null">#{assessor},</if>
            <if test="executor != null">#{executor},</if>
            <if test="inspectors != null">#{inspectors},</if>
            <if test="applicantDept != null">#{applicantDept},</if>
            <if test="applicantTeam != null">#{applicantTeam},</if>
            <if test="description != null">#{description},</if>
            <if test="unsafeStatus != null">#{unsafeStatus},</if>
            <if test="unsafeAction != null">#{unsafeAction},</if>
            <if test="applyDate != null">#{applyDate},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="taskName != null">#{taskName},</if>
            <if test="handler != null">#{handler},</if>
            <if test="estimateFinishDate != null">#{estimateFinishDate},</if>
            <if test="actualFinishDate != null">#{actualFinishDate},</if>
            <if test="isRecorded != null">#{isRecorded},</if>
            <if test="recordNo != null">#{recordNo},</if>
            <if test="reaction != null">#{reaction},</if>
            <if test="needVe != null">#{needVe},</if>
            <if test="veItems != null">#{veItems},</if>
            <if test="veResult != null">#{veResult},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="veItemOther != null">#{veItemOther},</if>
            <if test="saiLevel != null">#{saiLevel},</if>
            <if test="category != null">#{category},</if>
            <if test="remarksAssess != null">#{remarksAssess},</if>
            <if test="remarksExecute != null">#{remarksExecute},</if>
            <if test="workArea != null">#{workArea},</if>
         </trim>
    </insert>

    <update id="updateTSaiApply" parameterType="TSaiApply">
        update t_sai_apply
        <trim prefix="SET" suffixOverrides=",">
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="applyStatus != null">apply_status = #{applyStatus},</if>
            <if test="apNo != null">ap_no = #{apNo},</if>
            <if test="processId != null">process_id = #{processId},</if>
            <if test="applicant != null">applicant = #{applicant},</if>
            <if test="assessor != null">assessor = #{assessor},</if>
            <if test="executor != null">executor = #{executor},</if>
            <if test="inspectors != null">inspectors = #{inspectors},</if>
            <if test="applicantDept != null">applicant_dept = #{applicantDept},</if>
            <if test="applicantTeam != null">applicant_team = #{applicantTeam},</if>
            <if test="description != null">description = #{description},</if>
            <if test="unsafeStatus != null">unsafe_status = #{unsafeStatus},</if>
            <if test="unsafeStatus == null">unsafe_status = null,</if>
            <if test="unsafeAction != null">unsafe_action = #{unsafeAction},</if>
            <if test="unsafeAction == null">unsafe_action = null,</if>
            <if test="applyDate != null">apply_date = #{applyDate},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="taskId == null">task_id = null,</if>
            <if test="taskName != null">task_name = #{taskName},</if>
            <if test="taskName == null">task_name = null,</if>
            <if test="handler != null">handler = #{handler},</if>
            <if test="estimateFinishDate != null">estimate_finish_date = #{estimateFinishDate},</if>
            <if test="actualFinishDate != null">actual_finish_date = #{actualFinishDate},</if>
            <if test="isRecorded != null">is_recorded = #{isRecorded},</if>
            <if test="recordNo != null">record_no = #{recordNo},</if>
            <if test="recordNo == null">record_no = null,</if>
            <if test="reaction != null">reaction = #{reaction},</if>
            <if test="needVe != null">need_ve = #{needVe},</if>
            <if test="veItems != null">ve_items = #{veItems},</if>
            <if test="veItems == null">ve_items = null,</if>
            <if test="veResult != null">ve_result = #{veResult},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="veItemOther != null">ve_item_other = #{veItemOther},</if>
            <if test="veItemOther == null">ve_item_other = null,</if>
            <if test="saiLevel != null">sai_level = #{saiLevel},</if>
            <if test="category != null">category = #{category},</if>
            <if test="remarksAssess != null">remarks_assess = #{remarksAssess},</if>
            <if test="remarksExecute != null">remarks_execute = #{remarksExecute},</if>
            <if test="workArea != null">work_area = #{workArea},</if>
        </trim>
        where sai_apply_id = #{saiApplyId}
    </update>

    <update id="deleteTSaiApplyById" parameterType="Long">
        update t_sai_apply set del_flag = 2 where sai_apply_id = #{saiApplyId}
    </update>

    <update id="deleteTSaiApplyByIds" parameterType="String">
        update t_sai_apply set del_flag = 2 where sai_apply_id in
        <foreach item="saiApplyId" collection="array" open="(" separator="," close=")">
            #{saiApplyId}
        </foreach>
    </update>
    
</mapper>