<?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.ygtx.gxt.mapper.GxtMonthScoreMapper">

    <resultMap type="GxtMonthScore" id="GxtMonthScoreResult">
        <result property="id" column="id"/>
        <result property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="monthPeriod" column="month_period"/>
        <result property="status" column="status"/>
        <result property="feedbackContent" column="feedback_content"/>
        <result property="persionId" column="persion_id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
        <result property="plannedCount" column="planned_count"/>
        <result property="actualCount" column="actual_count"/>
        <result property="groupDeduction" column="group_deduction"/>
        <result property="explainCode" column="explain_code"/>
        <result property="repairOrderCoefficient" column="repair_order_coefficient"/>
        <result property="workOrderCoefficient" column="work_order_coefficient"/>
    </resultMap>

    <sql id="selectGxtMonthScoreVo">
        select id, dept_id,(SELECT dept_name FROM sys_dept WHERE dept_id=gxt_month_score.dept_id) AS dept_name, month_period, status, feedback_content, persion_id, create_by, create_time, update_by, update_time, remark from gxt_month_score
    </sql>

    <select id="selectGxtMonthScoreList" parameterType="GxtMonthScore" resultMap="GxtMonthScoreResult">
        <include refid="selectGxtMonthScoreVo"/>
        <where>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="monthPeriod != null  and monthPeriod != ''"> and month_period = #{monthPeriod}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="feedbackContent != null  and feedbackContent != ''"> and feedback_content = #{feedbackContent}</if>
            <if test="persionId != null "> and persion_id = #{persionId}</if>
            <if test="createBy != null  and createBy != ''"> and create_by = #{createBy}</if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != ''"> and create_time &gt;= #{params.beginCreateTime}</if>
            <if test="params.endCreateTime != null and params.endCreateTime != ''"> and create_time &lt;= #{params.endCreateTime}</if>
            <if test="updateBy != null  and updateBy != ''"> and update_by = #{updateBy}</if>
            <if test="params.beginUpdateTime != null and params.beginUpdateTime != ''"> and update_time &gt;= #{params.beginUpdateTime}</if>
            <if test="params.endUpdateTime != null and params.endUpdateTime != ''"> and update_time &lt;= #{params.endUpdateTime}</if>
            <if test="remark != null  and remark != ''"> and remark = #{remark}</if>
        </where>
        ORDER BY create_time DESC
    </select>

    <select id="selectGxtMonthScoreListByStatus" parameterType="GxtMonthScore" resultMap="GxtMonthScoreResult">
        SELECT ms.id,ms.month_period,ms.dept_id,cs.detp_name AS dept_name,ms.status,cs.planned_count,cs.actual_count,cs.group_deduction,cs.explain_code,
               COALESCE(cs2.repair_order_coefficient, 1) AS repair_order_coefficient,COALESCE(cs2.work_order_coefficient, 1) AS work_order_coefficient
        FROM gxt_month_score ms
            LEFT JOIN gxt_coefficient_score cs ON ms.month_period=cs.month_period AND ms.dept_id=cs.dept_id
            LEFT JOIN gxt_coefficient_score cs2 ON cs2.dept_id=ms.dept_id AND cs2.month_period=DATE_FORMAT(DATE_SUB(STR_TO_DATE(ms.month_period, '%Y-%m'), INTERVAL 1 MONTH), '%Y-%m')
        <where>
            <if test="deptId != null "> and ms.dept_id = #{deptId}</if>
            <if test="monthPeriod != null  and monthPeriod != ''"> and ms.month_period = #{monthPeriod}</if>
            <if test="status != null "> and ms.status = #{status}</if>
            <if test="statusList != null and statusList != ''">
                AND ms.status IN
                <foreach item="item" collection="statusList.split(',')" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="feedbackContent != null  and feedbackContent != ''"> and ms.feedback_content = #{feedbackContent}</if>
            <if test="persionId != null "> and ms.persion_id = #{persionId}</if>
            <if test="createBy != null  and createBy != ''"> and ms.create_by = #{createBy}</if>
            <if test="params.beginCreateTime != null and params.beginCreateTime != ''"> and ms.create_time &gt;= #{params.beginCreateTime}</if>
            <if test="params.endCreateTime != null and params.endCreateTime != ''"> and ms.create_time &lt;= #{params.endCreateTime}</if>
            <if test="updateBy != null  and updateBy != ''"> and ms.update_by = #{updateBy}</if>
            <if test="params.beginUpdateTime != null and params.beginUpdateTime != ''"> and ms.update_time &gt;= #{params.beginUpdateTime}</if>
            <if test="params.endUpdateTime != null and params.endUpdateTime != ''"> and ms.update_time &lt;= #{params.endUpdateTime}</if>
            <if test="remark != null  and remark != ''"> and ms.remark = #{remark}</if>
        </where>
        ORDER BY ms.create_time DESC
    </select>

    <select id="selectGxtMonthScoreById" parameterType="Long" resultMap="GxtMonthScoreResult">
        <include refid="selectGxtMonthScoreVo"/>
        where id = #{id}
    </select>

    <insert id="insertGxtMonthScore" parameterType="GxtMonthScore" useGeneratedKeys="true" keyProperty="id">
        insert into gxt_month_score
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deptId != null">dept_id,</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period,</if>
            <if test="status != null">status,</if>
            <if test="feedbackContent != null and feedbackContent != ''">feedback_content,</if>
            <if test="persionId != null">persion_id,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deptId != null">#{deptId},</if>
            <if test="monthPeriod != null and monthPeriod != ''">#{monthPeriod},</if>
            <if test="status != null">#{status},</if>
            <if test="feedbackContent != null and feedbackContent != ''">#{feedbackContent},</if>
            <if test="persionId != null">#{persionId},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateGxtMonthScore" parameterType="GxtMonthScore">
        update gxt_month_score
        <trim prefix="SET" suffixOverrides=",">
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period = #{monthPeriod},</if>
            <if test="status != null">status = #{status},</if>
            <if test="feedbackContent != null and feedbackContent != ''">feedback_content = #{feedbackContent},</if>
            <if test="persionId != null">persion_id = #{persionId},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGxtMonthScoreById" parameterType="Long">
        delete from gxt_month_score where id = #{id}
    </delete>

    <delete id="deleteGxtMonthScoreByIds" parameterType="Long">
        delete from gxt_month_score where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>