<?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.oa.mapper.CmcBudgetStaffMapper">
    
    <resultMap type="CmcBudgetStaff" id="CmcBudgetStaffResult">
        <result property="budgetStaffId"    column="budget_staff_id"    />
        <result property="budgetId"    column="budget_id"    />
        <result property="userId"    column="user_id"    />
        <result property="days"    column="days"    />
        <result property="staffCost"    column="staff_cost"    />
        <result property="remark"    column="remark"    />
        <result property="type"    column="type"    />
        <result property="daysAdjust"    column="days_adjust"    />
        <result property="performance"    column="performance"    />
        <result property="performanceAdjust"    column="performance_adjust"    />
        <result property="coefficient"    column="coefficient"    />
        <result property="coefficientAdjust"    column="coefficient_adjust"    />
        <result property="amount"    column="amount"    />
        <result property="amountAdjust"    column="amount_adjust"    />
        <result property="dayCost"    column="day_cost"    />
        <association property="user"    javaType="SysUser"         resultMap="SysUserResult" />
        <association property="salary"    javaType="CmcPostSalary"         resultMap="CmcPostSalaryResult" />
    </resultMap>

    <resultMap type="SysUser" id="SysUserResult">
        <result property="userId"    column="user_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="nickName"    column="nick_name"    />
        <result property="postLevel"    column="post_level"    />
        <result property="salaryLevel"    column="salary_level"    />
    </resultMap>

    <resultMap type="CmcPostSalary" id="CmcPostSalaryResult">
        <result property="salaryId"    column="salary_id"    />
        <result property="postLevel"    column="post_level"    />
        <result property="salaryLevel"    column="salary_level"    />
        <result property="salary"    column="salary"    />
    </resultMap>

    <sql id="selectCmcBudgetStaffVo">
        select bs.budget_staff_id, bs.budget_id, bs.user_id, u.dept_id, u.nick_name, bs.days, ps.salary, bs.staff_cost, bs.remark, bs.day_cost,
               bs.type, bs.days_adjust, bs.performance, bs.performance_adjust, bs.coefficient, bs.coefficient_adjust, bs.amount, bs.amount_adjust from cmc_budget_staff as bs
        left join sys_user as u on u.user_id = bs.user_id
        left join cmc_post_salary as ps on u.salary_level = ps.salary_level and u.post_level = ps.post_level
    </sql>

    <select id="selectCmcBudgetStaffList" parameterType="CmcBudgetStaff" resultMap="CmcBudgetStaffResult">
        <include refid="selectCmcBudgetStaffVo"/>
        <where>  
            <if test="budgetId != null  and budgetId != ''"> and bs.budget_id = #{budgetId}</if>
            <if test="userId != null "> and bs.user_id = #{userId}</if>
            <if test="days != null "> and bs.days = #{days}</if>
            <if test="staffCost != null "> and bs.staff_cost = #{staffCost}</if>
            <if test="type != null "> and bs.type = #{type}</if>
        </where>
    </select>
    
    <select id="selectCmcBudgetStaffByBudgetId" parameterType="String" resultMap="CmcBudgetStaffResult">
        <include refid="selectCmcBudgetStaffVo"/>
        where bs.budget_id = #{budgetId}
    </select>
        
    <insert id="insertCmcBudgetStaff" parameterType="CmcBudgetStaff">
        insert into cmc_budget_staff
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="budgetStaffId != null">budget_staff_id,</if>
            <if test="budgetId != null">budget_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="days != null">days,</if>
            <if test="staffCost != null">staff_cost,</if>
            <if test="remark != null">remark,</if>
            <if test="type != null">type,</if>
            <if test="daysAdjust != null">days_adjust,</if>
            <if test="performance != null">performance,</if>
            <if test="performanceAdjust != null">performance_adjust,</if>
            <if test="coefficient != null">coefficient,</if>
            <if test="coefficientAdjust != null">coefficient_adjust,</if>
            <if test="amount != null">amount,</if>
            <if test="amountAdjust != null">amount_adjust,</if>
            <if test="dayCost != null">day_cost,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="budgetStaffId != null">#{budgetStaffId},</if>
            <if test="budgetId != null">#{budgetId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="days != null">#{days},</if>
            <if test="staffCost != null">#{staffCost},</if>
            <if test="remark != null">#{remark},</if>
            <if test="type != null">#{type},</if>
            <if test="daysAdjust != null">#{daysAdjust},</if>
            <if test="performance != null">#{performance},</if>
            <if test="performanceAdjust != null">#{performanceAdjust},</if>
            <if test="coefficient != null">#{coefficient},</if>
            <if test="coefficientAdjust != null">#{coefficientAdjust},</if>
            <if test="amount != null">#{amount},</if>
            <if test="amountAdjust != null">#{amountAdjust},</if>
            <if test="dayCost != null">#{dayCost},</if>
         </trim>
    </insert>

    <update id="updateCmcBudgetStaff" parameterType="CmcBudgetStaff">
        update cmc_budget_staff
        <trim prefix="SET" suffixOverrides=",">
            <if test="budgetId != null">budget_id = #{budgetId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="days != null">days = #{days},</if>
            <if test="staffCost != null">staff_cost = #{staffCost},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="type != null">type = #{type},</if>
            <if test="daysAdjust != null">days_adjust = #{daysAdjust},</if>
            <if test="performance != null">performance = #{performance},</if>
            <if test="performanceAdjust != null">performance_adjust = #{performanceAdjust},</if>
            <if test="coefficient != null">coefficient = #{coefficient},</if>
            <if test="coefficientAdjust != null">coefficient_adjust = #{coefficientAdjust},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="amountAdjust != null">amount_adjust = #{amountAdjust},</if>
            <if test="dayCost != null">day_cost = #{dayCost},</if>
        </trim>
        where budget_staff_id = #{budgetStaffId}
    </update>

    <delete id="deleteCmcBudgetStaffByBudgetId" parameterType="String">
        delete from cmc_budget_staff where budget_id = #{budgetId}
    </delete>

    <delete id="deleteCmcBudgetStaffByBudgetIds" parameterType="String">
        delete from cmc_budget_staff where budget_id in
        <foreach item="budgetId" collection="array" open="(" separator="," close=")">
            #{budgetId}
        </foreach>
    </delete>
</mapper>