<?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.TSaiMapper">
    
    <resultMap type="TSai" id="TSaiResult">
        <result property="saiId"    column="sai_id"    />
        <result property="plantId"    column="plant_id"    />
        <result property="inspectionDate"    column="inspection_date"    />
        <result property="dificiency"    column="dificiency"    />
        <result property="saiLevel"    column="sai_level"    />
        <result property="category"    column="category"    />
        <result property="actions"    column="actions"    />
        <result property="userDept"    column="user_dept"    />
        <result property="source"    column="source"    />
        <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="deptName" column="dept_name" />
        <result property="saiApplyId"    column="sai_apply_id"    />
        <result property="applicant"    column="applicant"    />
    </resultMap>

    <sql id="selectTSaiVo">
        select d.sai_id, d.plant_id, d.inspection_date, d.dificiency, d.sai_level, d.category, d.actions, d.user_dept, d.source, d.del_flag, d.create_by, d.create_time, d.update_by, d.update_time ,d.dept_id, d.sai_apply_id, d.applicant, s.dept_name from t_sai d
      left join sys_dept s on s.dept_id = d.dept_id
    </sql>

    <select id="selectTSaiListByYear" parameterType="com.ruoyi.project.production.controller.vo.SaiQueryVO" resultMap="TSaiResult">
        <include refid="selectTSaiVo"/>
        <where>
            <if test="saiYear != null "> and extract(year from inspection_date) = #{saiYear} </if>
            <if test="source != null  and source != ''"> and source like concat(concat('%', #{source}), '%') </if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by d.inspection_date asc
    </select>

    <select id="selectTSaiList" parameterType="TSai" resultMap="TSaiResult">
        <include refid="selectTSaiVo"/>
        <where>  
            <if test="saiId != null "> and sai_id = #{saiId}</if>
            <if test="plantId != null  and plantId != ''"> and plant_id = #{plantId}</if>
            <if test="inspectionDate != null "> and inspection_date = #{inspectionDate}</if>
            <if test="dificiency != null  and dificiency != ''"> and dificiency = #{dificiency}</if>
            <if test="saiLevel != null "> and sai_level = #{saiLevel}</if>
            <if test="category != null  and category != ''"> and category = #{category}</if>
            <if test="actions != null  and actions != ''"> and actions = #{actions}</if>
            <if test="userDept != null  and userDept != ''"> and user_dept = #{userDept}</if>
            <if test="source != null  and source != ''"> and source = #{source}</if>
            <if test="saiApplyId != null "> and sai_apply_id = #{saiApplyId}</if>
            <if test="applicant != null "> and applicant = #{applicant}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    
    <select id="selectTSaiById" parameterType="Long" resultMap="TSaiResult">
        <include refid="selectTSaiVo"/>
        where sai_id = #{saiId}
    </select>
        
    <insert id="insertTSai" parameterType="TSai">
        <selectKey keyProperty="saiId" resultType="long" order="BEFORE">
            SELECT seq_t_sai.NEXTVAL as saiId FROM DUAL
        </selectKey>
        insert into t_sai
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="saiId != null">sai_id,</if>
            <if test="plantId != null">plant_id,</if>
            <if test="inspectionDate != null">inspection_date,</if>
            <if test="dificiency != null">dificiency,</if>
            <if test="saiLevel != null">sai_level,</if>
            <if test="category != null">category,</if>
            <if test="actions != null">actions,</if>
            <if test="userDept != null">user_dept,</if>
            <if test="source != null">source,</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="saiApplyId != null">sai_apply_id,</if>
            <if test="applicant != null">applicant,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="saiId != null">#{saiId},</if>
            <if test="plantId != null">#{plantId},</if>
            <if test="inspectionDate != null">#{inspectionDate},</if>
            <if test="dificiency != null">#{dificiency},</if>
            <if test="saiLevel != null">#{saiLevel},</if>
            <if test="category != null">#{category},</if>
            <if test="actions != null">#{actions},</if>
            <if test="userDept != null">#{userDept},</if>
            <if test="source != null">#{source},</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="saiApplyId != null">#{saiApplyId},</if>
            <if test="applicant != null">#{applicant},</if>
         </trim>
    </insert>

    <update id="updateTSai" parameterType="TSai">
        update t_sai
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantId != null">plant_id = #{plantId},</if>
            <if test="inspectionDate != null">inspection_date = #{inspectionDate},</if>
            <if test="dificiency != null">dificiency = #{dificiency},</if>
            <if test="saiLevel != null">sai_level = #{saiLevel},</if>
            <if test="category != null">category = #{category},</if>
            <if test="actions != null">actions = #{actions},</if>
            <if test="userDept != null">user_dept = #{userDept},</if>
            <if test="source != null">source = #{source},</if>
            <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="saiApplyId != null">sai_apply_id = #{saiApplyId},</if>
            <if test="applicant != null">applicant = #{applicant},</if>
        </trim>
        where sai_id = #{saiId}
    </update>

    <update id="deleteTSaiById" parameterType="Long">
        update t_sai set del_flag = 2 where sai_id = #{saiId}
    </update>

    <update id="deleteTSaiByIds" parameterType="String">
        update t_sai set del_flag = 2 where sai_id in
        <foreach item="saiId" collection="array" open="(" separator="," close=")">
            #{saiId}
        </foreach>
    </update>
    
</mapper>