<?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.TTargetlistMapper">

    <resultMap type="TTargetlist" id="TTargetlistResult">
        <result property="id"    column="id"    />
        <result property="parentId"       column="parent_id"      />
        <result property="plantCode"    column="plant_code"    />
        <result property="item"    column="item"    />
        <result property="description"    column="description"    />
        <result property="targets"    column="targets"    />
        <result property="year"    column="year"    />
        <result property="measuresId"    column="measuresId"    />
        <result property="measures"    column="measures"    />
        <result property="responsible"    column="responsible"    />
        <result property="expectedDate"    column="expected_date"    />
        <result property="actionItem"    column="action_item"    />
        <result property="principal"    column="principal"    />
        <result property="expected"    column="expected"    />
        <result property="completionDate"    column="completion_date"    />
        <result property="firstquarter"    column="firstquarter"    />
        <result property="halfyear"    column="halfyear"    />
        <result property="threequarter"    column="threequarter"    />
        <result property="annual"    column="annual"    />
        <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="firstChecker"    column="first_checker"    />
        <result property="secondChecker"    column="second_checker"    />
        <result property="threeChecker"    column="three_checker"    />
        <result property="annualChecker"    column="annual_checker"    />
        <result property="yardstick"    column="yardstick"    />
        <result property="partners"    column="partners"    />
        <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="itemTop"    column="item_top"    />
        <result property="itemLast"    column="item_last"    />
        <result property="deptName" column="dept_name" />
    </resultMap>

    <sql id="selectTTargetlistVo">
        select d.id, d.parent_id, d.plant_code, d.item, d.description, d.targets, d.year, d.yardstick, d.partners, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id, d.remarks ,s.dept_name from t_targetlist d
      left join sys_dept s on s.dept_id = d.dept_id
    </sql>

    <select id="selectTTargetlistList" parameterType="TTargetlist" resultMap="TTargetlistResult">
        <include refid="selectTTargetlistVo"/>
        <where>
            <if test="item != null  and item != ''"> and item like concat(concat('%', #{item}), '%')</if>
            <if test="year != null "> and year = #{year}</if>
            <if test="parentId != null "> and d.parent_id = #{parentId}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by d.year desc, d.item_top asc, d.item_last asc
    </select>

    <select id="selectAllContent" parameterType="TTargetlist" resultMap="TTargetlistResult">
        select  d.description from t_targetlist d
        <where>
            <if test="year != null "> and year = #{year}</if>
            and d.parent_id != 0
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by d.year desc, d.item_top asc, d.item_last asc
    </select>

    <select id="selectAllMeasures" parameterType="TTargetlist" resultMap="TTargetlistResult">
        select  m.measures, m.responsible, m.expected_date,d.description from t_targetlist d  left join t_targetmeasures m on m.targetlist_id = d.id and m.del_flag = 0
        <where>
            and description =#{description}
            and d.parent_id != 0
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by d.year desc, d.item_top asc, d.item_last asc
    </select>
    <select id="selectListMeasures" parameterType="TTargetlist" resultMap="TTargetlistResult">
        select d.id, d.parent_id, d.yardstick, d.partners, d.plant_code, d.item, d.description, d.targets, d.year, m.id as measuresId, m.measures, m.responsible, m.expected_date, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.dept_id, d.remarks ,s.dept_name from t_targetlist d
        left join sys_dept s on s.dept_id = d.dept_id
        left join t_targetmeasures m on m.targetlist_id = d.id and m.del_flag = 0
        <where>
            <if test="year != null "> and year = #{year}</if>
            <if test="responsible != null  and responsible != ''"> and m.responsible like concat(concat('%', #{responsible}), '%')</if>
            and d.parent_id != 0
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by d.item_top asc, d.item_last asc, m.measures asc
    </select>

    <select id="selectTTargetlistById" parameterType="Long" resultMap="TTargetlistResult">
        <include refid="selectTTargetlistVo"/>
        where id = #{id}
    </select>

    <insert id="insertTTargetlist" parameterType="TTargetlist" useGeneratedKeys="true" keyProperty="id">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_targetlist.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_targetlist
        <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="year != null">year,</if>
            <if test="yardstick != null">yardstick,</if>
            <if test="partners != null">partners,</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="parentId != null">parent_id,</if>
            <if test="itemTop != null">item_top,</if>
            <if test="itemLast != null">item_last,</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="year != null">#{year},</if>
            <if test="yardstick != null">#{yardstick},</if>
            <if test="partners != null">#{partners},</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="parentId != null">#{parentId},</if>
            <if test="itemTop != null">#{itemTop},</if>
            <if test="itemLast != null">#{itemLast},</if>
        </trim>
    </insert>

    <update id="updateTTargetlist" parameterType="TTargetlist">
        update t_targetlist
        <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="year != null">year = #{year},</if>
            <if test="yardstick != null">yardstick = #{yardstick},</if>
            <if test="partners != null">partners = #{partners},</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="parentId != null">parent_id = #{parentId},</if>
            <if test="itemTop != null">item_top = #{itemTop},</if>
            <if test="itemLast != null">item_last = #{itemLast},</if>
        </trim>
        where id = #{id}
    </update>

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

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

</mapper>
