<?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.CmcPerformanceStaffMapper">
    
    <resultMap type="CmcPerformanceStaff" id="CmcPerformanceStaffResult">
        <result property="performanceStaffId"    column="performance_staff_id"    />
        <result property="performanceId"    column="performance_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
        <result property="workType"    column="work_type"    />
        <result property="content"    column="content"    />
        <result property="scaleGrade"    column="scale_grade"    />
        <result property="workload"    column="workload"    />
        <result property="unit"    column="unit"    />
        <result property="price"    column="price"    />
        <result property="coefficient"    column="coefficient"    />
        <result property="producePerformance"    column="produce_performance"    />
        <result property="managePerformance"    column="manage_performance"    />
        <result property="performanceMonth"    column="performance_month"    />
        <result property="remark"    column="remark"    />
        <association property="project"    javaType="CmcProject"         resultMap="CmcProjectResult" />
        <association property="user"    javaType="SysUser"         resultMap="SysUserResult" />
        <association property="dept"    javaType="SysDept"         resultMap="SysDeptResult" />
    </resultMap>

    <resultMap type="SysUser" id="SysUserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
    </resultMap>

    <resultMap type="CmcProject" id="CmcProjectResult">
    <result property="projectId"    column="project_id"    />
    <result property="projectNumber"    column="project_number"    />
    <result property="projectName"    column="project_name"    />
    </resultMap>

    <resultMap type="SysDept" id="SysDeptResult">
    <result property="deptId"    column="dept_id"    />
    <result property="deptName"    column="dept_name"    />
    </resultMap>

    <sql id="selectCmcPerformanceStaffVo">
        select ps.performance_staff_id, ps.performance_id, ps.project_id, p.project_number, p.project_name, ps.user_id, u.nick_name, d.dept_name, ps.work_type, ps.content, ps.scale_grade, ps.workload, ps.unit, ps.price,
               ps.coefficient, ps.produce_performance, ps.manage_performance, ps.performance_month, ps.remark from cmc_performance_staff as ps
        left join cmc_project as p on p.project_id = ps.project_id
        left join sys_user as u on u.user_id = ps.user_id
        left join sys_dept as d on d.dept_id = u.dept_id
    </sql>

    <select id="selectCmcPerformanceStaffList" parameterType="CmcPerformanceStaff" resultMap="CmcPerformanceStaffResult">
        <include refid="selectCmcPerformanceStaffVo"/>
        <where>  
            <if test="performanceId != null  and performanceId != ''"> and ps.performance_id = #{performanceId}</if>
            <if test="projectId != null  and projectId != ''"> and ps.project_id = #{projectId}</if>
            <if test="userId != null "> and ps.user_id = #{userId}</if>
            <if test="workType != null  and workType != ''"> and ps.work_type = #{workType}</if>
            <if test="content != null  and content != ''"> and ps.content = #{content}</if>
            <if test="scaleGrade != null  and scaleGrade != ''"> and ps.scale_grade = #{scaleGrade}</if>
            <if test="workload != null "> and ps.workload = #{workload}</if>
            <if test="unit != null  and unit != ''"> and ps.unit = #{unit}</if>
            <if test="coefficient != null "> and ps.coefficient = #{coefficient}</if>
            <if test="producePerformance != null "> and ps.produce_performance = #{producePerformance}</if>
            <if test="performanceMonth != null "> and Month(ps.performance_month) = Month(#{performanceMonth})</if>
        </where>
    </select>

    <select id="selectMonthPerformanceList" parameterType="String" resultMap="CmcPerformanceStaffResult">
        select cast(avg(t1.produce_performance) * 1.6 as decimal(10,2)) as manage_performance, t2.produce_performance, t2.user_id, t2.nick_name, t2.dept_name, t2.performance_month
        from
        (select sum(ps.produce_performance) as produce_performance, u.nick_name, d.dept_name, p.post_name, ps.user_id, ps.performance_month from cmc_performance_staff as ps
        left join sys_user as u on u.user_id = ps.user_id
        left join sys_dept as d on d.dept_id = u.dept_id
        left join sys_user_post as up on u.user_id = up.user_id
        left join sys_post as p on p.post_id = up.post_id
        where p.post_name != '主任' and p.post_name != '副主任' and ps.performance_id = #{param1} and u.dept_id = #{param2}
        group by user_id) as t1,
        (select sum(ps.produce_performance) as produce_performance, u.nick_name, d.dept_name, p.post_name, ps.user_id, ps.performance_month from cmc_performance_staff as ps
        left join sys_user as u on u.user_id = ps.user_id
        left join sys_dept as d on d.dept_id = u.dept_id
        left join sys_user_post as up on u.user_id = up.user_id
        left join sys_post as p on p.post_id = up.post_id
        where p.post_name = '主任' and ps.performance_id = #{param1} and u.dept_id = #{param2}
        group by user_id) as t2
        union all
        select cast(avg(t1.produce_performance) * 1.2 as decimal(10,2)) as manage_performance, t2.produce_performance, t2.user_id, t2.nick_name, t2.dept_name, t2.performance_month
        from
        (select sum(ps.produce_performance) as produce_performance, u.nick_name, d.dept_name, p.post_name, ps.user_id, ps.performance_month from cmc_performance_staff as ps
        left join sys_user as u on u.user_id = ps.user_id
        left join sys_dept as d on d.dept_id = u.dept_id
        left join sys_user_post as up on u.user_id = up.user_id
        left join sys_post as p on p.post_id = up.post_id
        where p.post_name != '主任' and p.post_name != '副主任' and ps.performance_id = #{param1} and u.dept_id = #{param2}
        group by user_id) as t1,
        (select sum(ps.produce_performance) as produce_performance, u.nick_name, d.dept_name, p.post_name, ps.user_id, ps.performance_month from cmc_performance_staff as ps
        left join sys_user as u on u.user_id = ps.user_id
        left join sys_dept as d on d.dept_id = u.dept_id
        left join sys_user_post as up on u.user_id = up.user_id
        left join sys_post as p on p.post_id = up.post_id
        where p.post_name = '副主任' and ps.performance_id = #{param1} and u.dept_id = #{param2}
        group by user_id) as t2
        union all
        select ps.produce_performance * 0 as manage_performance, sum(ps.produce_performance) as produce_performance, ps.user_id, u.nick_name, d.dept_name, ps.performance_month from cmc_performance_staff as ps
        left join sys_user as u on u.user_id = ps.user_id
        left join sys_dept as d on d.dept_id = u.dept_id
        left join sys_user_post as up on u.user_id = up.user_id
        left join sys_post as p on p.post_id = up.post_id
        where p.post_name != '主任' and p.post_name != '副主任' and ps.performance_id = #{param1}
        group by user_id
    </select>

    <select id="selectCmcPerformanceStaffByPerformanceStaffId" parameterType="Long" resultMap="CmcPerformanceStaffResult">
        <include refid="selectCmcPerformanceStaffVo"/>
        where performance_staff_id = #{performanceStaffId}
    </select>
        
    <insert id="insertCmcPerformanceStaff" parameterType="CmcPerformanceStaff" useGeneratedKeys="true" keyProperty="performanceStaffId">
        insert into cmc_performance_staff
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="performanceId != null">performance_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="workType != null">work_type,</if>
            <if test="content != null">content,</if>
            <if test="scaleGrade != null">scale_grade,</if>
            <if test="workload != null">workload,</if>
            <if test="unit != null">unit,</if>
            <if test="price != null">price,</if>
            <if test="coefficient != null">coefficient,</if>
            <if test="producePerformance != null">produce_performance,</if>
            <if test="performanceMonth != null">performance_month,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="performanceId != null">#{performanceId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="workType != null">#{workType},</if>
            <if test="content != null">#{content},</if>
            <if test="scaleGrade != null">#{scaleGrade},</if>
            <if test="workload != null">#{workload},</if>
            <if test="unit != null">#{unit},</if>
            <if test="price != null">#{price},</if>
            <if test="coefficient != null">#{coefficient},</if>
            <if test="producePerformance != null">#{producePerformance},</if>
            <if test="performanceMonth != null">#{performanceMonth},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateCmcPerformanceStaff" parameterType="CmcPerformanceStaff">
        update cmc_performance_staff
        <trim prefix="SET" suffixOverrides=",">
            <if test="performanceId != null">performance_id = #{performanceId},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="workType != null">work_type = #{workType},</if>
            <if test="content != null">content = #{content},</if>
            <if test="scaleGrade != null">scale_grade = #{scaleGrade},</if>
            <if test="workload != null">workload = #{workload},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="price != null">price = #{price},</if>
            <if test="coefficient != null">coefficient = #{coefficient},</if>
            <if test="producePerformance != null">produce_performance = #{producePerformance},</if>
            <if test="performanceMonth != null">performance_month = #{performanceMonth},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where performance_staff_id = #{performanceStaffId}
    </update>

    <delete id="deleteCmcPerformanceStaffByPerformanceStaffId" parameterType="Long">
        delete from cmc_performance_staff where performance_staff_id = #{performanceStaffId}
    </delete>

    <delete id="deleteCmcPerformanceStaffByPerformanceId" parameterType="String">
        delete from cmc_performance_staff where performance_id in
        <foreach item="performanceId" collection="array" open="(" separator="," close=")">
            #{performanceId}
        </foreach>
    </delete>
</mapper>