<?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.TTargetagreementMapper">
    
    <resultMap type="TTargetagreement" id="TTargetagreementResult">
        <result property="id"    column="id"    />
        <result property="year"    column="year"    />
        <result property="employeeid"    column="employeeid"    />
        <result property="employeeName"    column="employeeName"    />
        <result property="actualpost"    column="actualpost"    />
        <result property="appraiser"    column="appraiser"    />
        <result property="appraiserName"    column="appraiserName"    />
        <result property="overall"    column="overall"    />
        <result property="remarks"    column="remarks"    />
        <result property="deptId"    column="dept_id"    />
        <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="deptName" column="dept_name" />
    </resultMap>

    <sql id="selectTTargetagreementVo">
        select d.id, d.year, d.employeeid, t.name as employeeName, t.actualpost, d.appraiser, a.name as appraiserName, d.overall, d.remarks, d.dept_id, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate ,s.dept_name from t_targetagreement d
      left join sys_dept s on s.dept_id = d.dept_id
      left join t_staffmgr t on t.staffid = d.employeeid
      left join t_staffmgr a on a.staffid = d.appraiser
    </sql>

    <select id="selectTTargetagreementList" parameterType="TTargetagreement" resultMap="TTargetagreementResult">
        <include refid="selectTTargetagreementVo"/>
        <where>  
            <if test="year != null "> and year = #{year}</if>
            <if test="employeeid != null  and employeeid != ''"> and employeeid = #{employeeid}</if>
            <if test="appraiser != null  and appraiser != ''"> and appraiser = #{appraiser}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    
    <select id="selectTTargetagreementById" parameterType="Long" resultMap="TTargetagreementResult">
        <include refid="selectTTargetagreementVo"/>
        where d.id = #{id}
    </select>

    <select id="selectTTargetagreementByEmployeeid" parameterType="String" resultMap="TTargetagreementResult">
        <include refid="selectTTargetagreementVo"/>
        where d.employeeid = #{employeeId}
    </select>

    <select id="selectTTargetagreementByEmployeeidAndYear" parameterType="TTargetagreement" resultMap="TTargetagreementResult">
        <include refid="selectTTargetagreementVo"/>
        where d.employeeid = #{employeeid}
        and d.year = #{year}
        and d.del_flag = 0
    </select>
        
    <insert id="insertTTargetagreement" parameterType="TTargetagreement" useGeneratedKeys="true" keyProperty="id">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_targetagreement.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_targetagreement
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="year != null">year,</if>
            <if test="employeeid != null and employeeid != ''">employeeid,</if>
            <if test="appraiser != null">appraiser,</if>
            <if test="overall != null">overall,</if>
            <if test="remarks != null">remarks,</if>
            <if test="deptId != null">dept_id,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="year != null">#{year},</if>
            <if test="employeeid != null and employeeid != ''">#{employeeid},</if>
            <if test="appraiser != null">#{appraiser},</if>
            <if test="overall != null">#{overall},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="deptId != null">#{deptId},</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>
         </trim>
    </insert>

    <update id="updateTTargetagreement" parameterType="TTargetagreement">
        update t_targetagreement
        <trim prefix="SET" suffixOverrides=",">
            <if test="year != null">year = #{year},</if>
            <if test="employeeid != null and employeeid != ''">employeeid = #{employeeid},</if>
            <if test="appraiser != null">appraiser = #{appraiser},</if>
            <if test="overall != null">overall = #{overall},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="deptId != null">dept_id = #{deptId},</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>
        </trim>
        where id = #{id}
    </update>

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

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