<?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.plant.mapper.TTargetreviewMapper">
    
    <resultMap type="TTargetreview" id="TTargetreviewResult">
        <result property="id"    column="id"    />
        <result property="plantCode"    column="plant_code"    />
        <result property="item"    column="item"    />
        <result property="description"    column="description"    />
        <result property="targets"    column="targets"    />
        <result property="firstquarter"    column="firstquarter"    />
        <result property="halfyear"    column="halfyear"    />
        <result property="threequarter"    column="threequarter"    />
        <result property="annual"    column="annual"    />
        <result property="year"    column="year"    />
        <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="firstquarterStatus"    column="firstquarter_status"    />
        <result property="halfyearStatus"    column="halfyear_status"    />
        <result property="threequarterStatus"    column="threequarter_status"    />
        <result property="annualStatus"    column="annual_status"    />
        <result property="itemTop"    column="item_top"    />
        <result property="itemLast"    column="item_last"    />
        <result property="actionItem"    column="action_item"    />
        <result property="principal"    column="principal"    />
        <result property="expectedDate"    column="expected_date"    />
        <result property="completionDate"    column="completion_date"    />
        <result property="deviationInfo"    column="deviation_info"    />
        <result property="listId"    column="list_id"    />
        <result property="deptName" column="dept_name" />
        <result property="parentId"       column="parent_id"      />
        <result property="total"    column="total"    />
        <result property="totalStatus"    column="total_status"    />
    </resultMap>

    <sql id="selectTTargetreviewVo">
        select d.id, l.item, l.description, l.targets, d.total, d.total_status, d.firstquarter, d.halfyear, d.threequarter, d.annual, l.year, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id, d.remarks, d.firstquarter_status, d.halfyear_status, d.threequarter_status, d.annual_status, l.parent_id, s.dept_name, a.action_item, a.principal, a.expected_date, a.completion_date, d.deviation_info, d.list_id from t_targetreview d
      left join sys_dept s on s.dept_id = d.dept_id
      left join t_targetlist l on l.id = d.list_id
      left join t_targetaction a on a.targetreview_id = d.id
    </sql>

    <select id="selectTTargetreviewList" parameterType="TTargetreview" resultMap="TTargetreviewResult">
        <include refid="selectTTargetreviewVo"/>
        <where>  
            <if test="item != null  and item != ''"> and item like concat(concat('%', #{item}), '%')</if>
            <if test="year != null "> and l.year = #{year}</if>
            <if test="parentId != null "> and d.parent_id = #{parentId}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by l.year desc, l.item_top asc, l.item_last asc
    </select>
    
    <select id="selectTTargetreviewById" parameterType="Long" resultMap="TTargetreviewResult">
        select d.id, l.item, l.description, l.targets, d.firstquarter, d.halfyear, d.threequarter, d.annual, l.year, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id, d.remarks, d.firstquarter_status, d.halfyear_status, d.threequarter_status, d.annual_status, l.parent_id, s.dept_name, d.deviation_info, d.list_id, d.deviation_info from t_targetreview d
      left join sys_dept s on s.dept_id = d.dept_id
      left join t_targetlist l on l.id = d.list_id
        where d.id = #{id}
    </select>
        
    <insert id="insertTTargetreview" parameterType="TTargetreview">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_targetreview.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_targetreview
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="plantCode != null and plantCode != ''">plant_code,</if>
            <if test="item != null and item != ''">item,</if>
            <if test="description != null">description,</if>
            <if test="targets != null">targets,</if>
            <if test="firstquarter != null">firstquarter,</if>
            <if test="halfyear != null">halfyear,</if>
            <if test="threequarter != null">threequarter,</if>
            <if test="annual != null">annual,</if>
            <if test="year != null">year,</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="firstquarterStatus != null">firstquarter_status,</if>
            <if test="halfyearStatus != null">halfyear_status,</if>
            <if test="threequarterStatus != null">threequarter_status,</if>
            <if test="annualStatus != null">annual_status,</if>
            <if test="itemTop != null">item_top,</if>
            <if test="itemLast != null">item_last,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="deviationInfo != null">deviation_info,</if>
            <if test="listId != null">list_id,</if>
            <if test="total != null">total,</if>
            <if test="totalStatus != null">total_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="plantCode != null and plantCode != ''">#{plantCode},</if>
            <if test="item != null and item != ''">#{item},</if>
            <if test="description != null">#{description},</if>
            <if test="targets != null">#{targets},</if>
            <if test="firstquarter != null">#{firstquarter},</if>
            <if test="halfyear != null">#{halfyear},</if>
            <if test="threequarter != null">#{threequarter},</if>
            <if test="annual != null">#{annual},</if>
            <if test="year != null">#{year},</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="firstquarterStatus != null">#{firstquarterStatus},</if>
            <if test="halfyearStatus != null">#{halfyearStatus},</if>
            <if test="threequarterStatus != null">#{threequarterStatus},</if>
            <if test="annualStatus != null">#{annualStatus},</if>
            <if test="itemTop != null">#{itemTop},</if>
            <if test="itemLast != null">#{itemLast},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="deviationInfo != null">#{deviationInfo},</if>
            <if test="listId != null">#{listId},</if>
            <if test="total != null">#{total},</if>
            <if test="totalStatus != null">#{totalStatus},</if>
         </trim>
    </insert>

    <update id="updateTTargetreview" parameterType="TTargetreview">
        update t_targetreview
        <trim prefix="SET" suffixOverrides=",">
            <if test="plantCode != null and plantCode != ''">plant_code = #{plantCode},</if>
            <if test="item != null and item != ''">item = #{item},</if>
            <if test="description != null">description = #{description},</if>
            <if test="targets != null">targets = #{targets},</if>
            <if test="firstquarter != null">firstquarter = #{firstquarter},</if>
            <if test="halfyear != null">halfyear = #{halfyear},</if>
            <if test="threequarter != null">threequarter = #{threequarter},</if>
            <if test="annual != null">annual = #{annual},</if>
            <if test="year != null">year = #{year},</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="firstquarterStatus == null">firstquarter_status = NULL,</if>
            <if test="halfyearStatus == null">halfyear_status = NULL,</if>
            <if test="threequarterStatus == null">threequarter_status = NULL,</if>
            <if test="annualStatus == null">annual_status = NULL,</if>
            <if test="firstquarterStatus != null">firstquarter_status = #{firstquarterStatus},</if>
            <if test="halfyearStatus != null">halfyear_status = #{halfyearStatus},</if>
            <if test="threequarterStatus != null">threequarter_status = #{threequarterStatus},</if>
            <if test="annualStatus != null">annual_status = #{annualStatus},</if>
            <if test="itemTop != null">item_top = #{itemTop},</if>
            <if test="itemLast != null">item_last = #{itemLast},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="deviationInfo != null">deviation_info = #{deviationInfo},</if>
            <if test="listId != null">list_id = #{listId},</if>
            <if test="total != null">total = #{total},</if>
            <if test="totalStatus != null">total_status = #{totalStatus},</if>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteTTargetreviewByListId" parameterType="Long">
        update t_targetreview set del_flag = 2 where list_id = #{listId}
    </update>

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

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