<?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.lz.modules.flow.dao.EvaluationStartStaffMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.lz.modules.flow.entity.EvaluationStartStaff">
        <id column="id" property="id"/>
        <result column="is_delete" property="isDelete"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="evaluation_id" property="evaluationId"/>
        <result column="start_id" property="startId"/>
        <result column="staff_id" property="staffId"/>
        <result column="type" property="type"/>
        <result column="score" property="score"/>
        <result column="evaluation_name" property="evaluationName"/>
        <result column="department_id" property="departmentId"/>
    </resultMap>


    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id AS id, is_delete AS isDelete, gmt_create AS gmtCreate, gmt_modified AS gmtModified, evaluation_id AS evaluationId, start_id AS startId, staff_id AS staffId, type AS type, score AS score, evaluation_name AS evaluationName, department_id AS departmentId
    </sql>




    <select id="selectEvaluationStartStaffById" resultType="EvaluationStartStaff" >
        select * from lz_evaluation_start_staff  where id=#{id} and is_delete = 0 limit 1
    </select>


    <insert id="insertEvaluationStartStaff" parameterType="EvaluationStartStaff" useGeneratedKeys="true" keyProperty="id" >
        insert into lz_evaluation_start_staff(
        <if test="evaluationId != null">evaluation_id, </if>
        <if test="startId != null">start_id, </if>
        <if test="staffId != null">staff_id, </if>
        <if test="type != null">type, </if>
        <if test="score != null">score, </if>
        <if test="evaluationName != null">evaluation_name, </if>
        <if test="departmentId != null">department_id, </if>
        is_delete,
        gmt_create,
        gmt_modified
        )values(
        <if test="evaluationId != null">#{ evaluationId}, </if>
        <if test="startId != null">#{ startId}, </if>
        <if test="staffId != null">#{ staffId}, </if>
        <if test="type != null">#{ type}, </if>
        <if test="score != null">#{ score}, </if>
        <if test="evaluationName != null">#{ evaluationName}, </if>
        <if test="departmentId != null">#{ departmentId}, </if>
        0,
        now(),
        now()
        )
    </insert>


    <update id="updateEvaluationStartStaffById" parameterType="EvaluationStartStaff" >
        update
        lz_evaluation_start_staff
        <trim prefix="set" suffixOverrides=",">
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="gmtCreate != null">gmt_create = #{gmtCreate},</if>
            <if test="evaluationId != null">evaluation_id = #{evaluationId},</if>
            <if test="startId != null">start_id = #{startId},</if>
            <if test="staffId != null">staff_id = #{staffId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="score != null">score = #{score},</if>
            <if test="evaluationName != null">evaluation_name = #{evaluationName},</if>
            <if test="departmentId != null">department_id = #{departmentId}</if>
        </trim>
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="updateCoverEvaluationStartStaffById" parameterType="EvaluationStartStaff" >
        update
            lz_evaluation_start_staff
        set
            is_delete = #{isDelete},
            gmt_create = #{gmtCreate},
            evaluation_id = #{evaluationId},
            start_id = #{startId},
            staff_id = #{staffId},
            type = #{type},
            score = #{score},
            evaluation_name = #{evaluationName},
            department_id = #{departmentId}
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="deleteEvaluationStartStaffById" parameterType="java.lang.Long">
        update lz_evaluation_start_staff set is_delete = 1 where id=#{id} limit 1
    </update>

    <insert id="insertEvaluationStartStaffs" parameterType="EvaluationStartStaff" useGeneratedKeys="true" keyProperty="id" >

        insert into lz_evaluation_start_staff(
        evaluation_id,
        start_id,
        staff_id,
        type,
        evaluation_name,
        department_id,
        is_delete
        )values
        <foreach collection="list" item="item" separator=",">(
            #{ item.evaluationId},
            #{ item.startId},
            #{ item.staffId},
            #{ item.type},
            #{ item.evaluationName},
            #{ item.departmentId},
            0
            )
        </foreach>
        ;
    </insert>


    <select id="selectOneByStartIdAndStaffId" resultType="EvaluationStartStaff">
        select * from lz_evaluation_start_staff  where start_id=#{startId} AND staff_id =#{staffId} and is_delete = 0 limit 1
    </select>

    <select id="selectManagerEvaluationStartStaff" resultType="EvaluationStartStaff">
        select * from lz_evaluation_start_staff  where evaluation_id=#{evaluationId}
        AND staff_id =#{staffId} and type = 1 is_delete = 0 and limit 1
    </select>

    <select id="existByStartIdAndEvaluationId" resultType="Integer">
        select count(1) from lz_evaluation_start_staff  where is_delete = 0 and evaluation_id=#{evaluationId}
        AND start_id =#{startId} and score = 1  limit 1
    </select>

    <update id="updateBatchToScore">
        update
            lz_evaluation_start_staff
        set
        score = 1,
        gmt_modified = now()
        where is_delete = 0 and score = 0 and type = 0
        and  evaluation_id = #{evaluationId} and start_id = #{startId}
    </update>


    <update id="deleteEvaluationStartStaffChangeAssess">
        update
            lz_evaluation_start_staff
        set
        is_delete = 1,
        gmt_modified = now()
        where is_delete = 0  and type = 0
        and start_id = #{startId}
        and staff_id in (
        <foreach collection="staffIds" item="staff_id" separator=",">
            #{staff_id}
        </foreach>
        )
    </update>

    <select id="selectStaffIdsByStart" resultType="long">
        select staff_id from lz_evaluation_start_staff  where start_id=#{startId}  and is_delete = 0
    </select>

    <select id="selectStaffIdsByStartAndStaffId" resultType="long">
        select staff_id from lz_evaluation_start_staff  where start_id=#{startId}  and is_delete = 0
        and staff_id in (
        <foreach collection="list" item="item" separator=",">
            #{item.id}
        </foreach>
        )
        group by staff_id
    </select>


    <select id="selectEvaluationStartById" resultType="com.lz.modules.flow.entity.EvaluationStartStaff">
         select * from lz_evaluation_start_staff  where start_id=#{startId} and evaluation_id=#{evaluationId}  and is_delete = 0 limit 1
    </select>

    <update id="deleteByStartId">
        UPDATE lz_evaluation_start_staff
        SET is_delete = 1
        where is_delete = 0 and start_id = #{startId}
    </update>
</mapper>

