<?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.GxtMonthRepairProjectMapper">

    <resultMap type="GxtMonthRepairProject" id="GxtMonthRepairProjectResult">
        <result property="id" column="id"/>
        <result property="monthScoreId" column="month_score_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="maintenanceType" column="maintenance_type"/>
        <result property="orderCount" column="order_count"/>
        <result property="projectScore" column="project_score"/>
        <result property="calculatedScore" column="calculated_score"/>
        <result property="monthPeriod" column="month_period"/>
    </resultMap>

    <sql id="selectGxtMonthRepairProjectVo">
        select id, month_score_id, dept_id, dept_name, maintenance_type, order_count, project_score, calculated_score, month_period from gxt_month_repair_project
    </sql>

    <select id="selectGxtMonthRepairProjectList" parameterType="GxtMonthRepairProject" resultMap="GxtMonthRepairProjectResult">
        <include refid="selectGxtMonthRepairProjectVo"/>
        <where>
            <if test="monthScoreId != null "> and month_score_id = #{monthScoreId}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="deptName != null  and deptName != ''"> and dept_name like concat('%', #{deptName}, '%')</if>
            <if test="maintenanceType != null "> and maintenance_type = #{maintenanceType}</if>
            <if test="orderCount != null "> and order_count = #{orderCount}</if>
            <if test="projectScore != null "> and project_score = #{projectScore}</if>
            <if test="calculatedScore != null "> and calculated_score = #{calculatedScore}</if>
            <if test="monthPeriod != null  and monthPeriod != ''"> and month_period = #{monthPeriod}</if>
        </where>
    </select>

    <select id="selectGxtMonthRepairProjectListByTwice" parameterType="GxtMonthRepairProject" resultMap="GxtMonthRepairProjectResult">
        SELECT
        maintenance_type,
        count(*) AS order_count
        FROM
        gxt_repair_order
        <where>
            <if test="deptId != null "> and pcs_station_pid = #{deptId}</if>
            <if test="monthPeriod != null  and monthPeriod != ''"> and DATE_FORMAT( occur_time, '%Y-%m' ) = #{monthPeriod}</if>
            AND maintenance_type IS NOT NULL AND work_order_status != 'invalid'
        </where>
        GROUP BY
        maintenance_type
        ORDER BY
        maintenance_type
    </select>

    <select id="selectGxtMonthRepairProjectListByAdd" parameterType="GxtMonthRepairProject" resultMap="GxtMonthRepairProjectResult">
        SELECT
            sp.maintenance_type,
            COUNT(ro.id) AS order_count,
            sp.score_per_completion AS project_score,
            sp.score_per_completion*COUNT(ro.id) AS calculated_score
        FROM
            gxt_repair_order ro
                LEFT JOIN gxt_score_project sp ON ro.maintenance_type = sp.maintenance_type AND sp.status = 0
                LEFT JOIN sys_user u ON ro.accept_user_id = u.user_id
        WHERE
            u.dept_id = #{deptId}
            AND (ro.work_order_status='archived' or ro.scoring_status='to_archive')
            AND ro.occur_time >= STR_TO_DATE(CONCAT(#{monthPeriod}, '-01 00:00:00'), '%Y-%m-%d %H:%i:%s')
            AND ro.occur_time <![CDATA[<]]> DATE_ADD(STR_TO_DATE(CONCAT(#{monthPeriod}, '-01 00:00:00'), '%Y-%m-%d %H:%i:%s'), INTERVAL 1 MONTH)
            AND (ro.repair_method IS NULL OR ro.repair_method != '2')
        GROUP BY
            sp.id
    </select>

    <select id="selectGxtMonthRepairProjectById" parameterType="Long" resultMap="GxtMonthRepairProjectResult">
        <include refid="selectGxtMonthRepairProjectVo"/>
        where id = #{id}
    </select>

    <insert id="insertGxtMonthRepairProject" parameterType="GxtMonthRepairProject" useGeneratedKeys="true" keyProperty="id">
        insert into gxt_month_repair_project
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="monthScoreId != null">month_score_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="deptName != null and deptName != ''">dept_name,</if>
            <if test="maintenanceType != null">maintenance_type,</if>
            <if test="orderCount != null">order_count,</if>
            <if test="projectScore != null">project_score,</if>
            <if test="calculatedScore != null">calculated_score,</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="monthScoreId != null">#{monthScoreId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="deptName != null and deptName != ''">#{deptName},</if>
            <if test="maintenanceType != null">#{maintenanceType},</if>
            <if test="orderCount != null">#{orderCount},</if>
            <if test="projectScore != null">#{projectScore},</if>
            <if test="calculatedScore != null">#{calculatedScore},</if>
            <if test="monthPeriod != null and monthPeriod != ''">#{monthPeriod},</if>
         </trim>
    </insert>

    <update id="updateGxtMonthRepairProject" parameterType="GxtMonthRepairProject">
        update gxt_month_repair_project
        <trim prefix="SET" suffixOverrides=",">
            <if test="monthScoreId != null">month_score_id = #{monthScoreId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
            <if test="maintenanceType != null">maintenance_type = #{maintenanceType},</if>
            <if test="orderCount != null">order_count = #{orderCount},</if>
            <if test="projectScore != null">project_score = #{projectScore},</if>
            <if test="calculatedScore != null">calculated_score = #{calculatedScore},</if>
            <if test="monthPeriod != null and monthPeriod != ''">month_period = #{monthPeriod},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGxtMonthRepairProjectById" parameterType="Long">
        delete from gxt_month_repair_project where id = #{id}
    </delete>

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

</mapper>