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

    <resultMap type="GxtMonthFanInspection" id="GxtMonthFanInspectionResult">
        <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="fanInspectionId" column="fan_inspection_id"/>
        <result property="deviceCount" column="device_count"/>
        <result property="fanInspectionScore" column="fan_inspection_score"/>
        <result property="calculatedScore" column="calculated_score"/>
        <result property="monthPeriod" column="month_period"/>
        <result property="status" column="status"/>
        <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="inspectionTypeId" column="inspection_type_id"/>
    </resultMap>

    <sql id="selectGxtMonthFanInspectionVo">
        select id, month_score_id, dept_id, dept_name, fan_inspection_id,(SELECT inspection_type_id FROM gxt_score_fan_inspection WHERE id=fan_inspection_id) AS inspection_type_id,
               (SELECT fan_type FROM gxt_score_fan_inspection WHERE id=fan_inspection_id) AS model,
               device_count, fan_inspection_score, calculated_score, month_period, status, create_by, create_time, update_by, update_time, remark from gxt_month_fan_inspection
    </sql>

    <select id="selectGxtMonthFanInspectionList" parameterType="GxtMonthFanInspection" resultMap="GxtMonthFanInspectionResult">
        <include refid="selectGxtMonthFanInspectionVo"/>
        <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="fanInspectionId != null "> and fan_inspection_id = #{fanInspectionId}</if>
            <if test="deviceCount != null "> and device_count = #{deviceCount}</if>
            <if test="fanInspectionScore != null "> and fan_inspection_score = #{fanInspectionScore}</if>
            <if test="calculatedScore != null "> and calculated_score = #{calculatedScore}</if>
            <if test="monthPeriod != null  and monthPeriod != ''"> and month_period = #{monthPeriod}</if>
            <if test="status != null "> and status = #{status}</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>
    </select>

    <select id="selectGxtMonthFanInspectionListByTwice" parameterType="GxtMonthFanInspection" resultMap="GxtMonthFanInspectionResult">
        SELECT
        model,
        inspection_type AS inspection_type_id,
        count(*) AS device_count
        FROM
        gxt_work_order
        <where>
            <if test="deptId != null "> and pcs_station_pid = #{deptId}</if>
            <if test="monthPeriod != null  and monthPeriod != ''"> and DATE_FORMAT( create_time, '%Y-%m' ) = #{monthPeriod}</if>
            AND inspection_type IS NOT NULL AND work_order_status != 'invalid'
        </where>
        GROUP BY
        model,
        inspection_type
        ORDER BY
        model,
        inspection_type
    </select>

    <select id="selectGxtMonthFanInspectionListByAdd" parameterType="GxtMonthFanInspection" resultMap="GxtMonthFanInspectionResult">
        SELECT
            sfi.id AS fan_inspection_id,
            sfi.inspection_type_id,
            sfi.fan_type AS model,
            COUNT(wo.id) AS device_count,
            sfi.score AS fan_inspection_score,
            sfi.score*COUNT(wo.id) AS calculated_score
        FROM
            gxt_work_order wo
                LEFT JOIN gxt_score_fan_inspection sfi ON sfi.inspection_type_id = wo.inspection_type
                AND sfi.fan_type = wo.model AND sfi.status = 0
                LEFT JOIN sys_user u ON wo.accept_user_id = u.user_id
        WHERE
            u.dept_id = #{deptId}
            AND (wo.work_order_status='archived' or wo.scoring_status='to_archive')
            AND ((wo.create_time >= STR_TO_DATE(CONCAT(#{monthPeriod}, '-01 00:00:00'), '%Y-%m-%d %H:%i:%s')
                AND wo.create_time <![CDATA[<]]> DATE_ADD(STR_TO_DATE(CONCAT(#{monthPeriod}, '-01 00:00:00'), '%Y-%m-%d %H:%i:%s'), INTERVAL 1 MONTH)
            ) OR wo.is_deferred=1)
            <!-- AND wo.item_completion_factor_sum=1 -->
            AND (wo.repair_method IS NULL OR wo.repair_method != '2')
        GROUP BY
            sfi.id
    </select>

<select id="selectGxtMonthFanInspectionById" parameterType="Long" resultMap="GxtMonthFanInspectionResult">
    <include refid="selectGxtMonthFanInspectionVo"/>
    where id = #{id}
</select>

<insert id="insertGxtMonthFanInspection" parameterType="GxtMonthFanInspection" useGeneratedKeys="true" keyProperty="id">
    insert into gxt_month_fan_inspection
    <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="fanInspectionId != null">fan_inspection_id,</if>
        <if test="deviceCount != null">device_count,</if>
        <if test="fanInspectionScore != null">fan_inspection_score,</if>
        <if test="calculatedScore != null">calculated_score,</if>
        <if test="monthPeriod != null and monthPeriod != ''">month_period,</if>
        <if test="status != null">status,</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="monthScoreId != null">#{monthScoreId},</if>
        <if test="deptId != null">#{deptId},</if>
        <if test="deptName != null and deptName != ''">#{deptName},</if>
        <if test="fanInspectionId != null">#{fanInspectionId},</if>
        <if test="deviceCount != null">#{deviceCount},</if>
        <if test="fanInspectionScore != null">#{fanInspectionScore},</if>
        <if test="calculatedScore != null">#{calculatedScore},</if>
        <if test="monthPeriod != null and monthPeriod != ''">#{monthPeriod},</if>
        <if test="status != null">#{status},</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="updateGxtMonthFanInspection" parameterType="GxtMonthFanInspection">
    update gxt_month_fan_inspection
    <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="fanInspectionId != null">fan_inspection_id = #{fanInspectionId},</if>
        <if test="deviceCount != null">device_count = #{deviceCount},</if>
        <if test="fanInspectionScore != null">fan_inspection_score = #{fanInspectionScore},</if>
        <if test="calculatedScore != null">calculated_score = #{calculatedScore},</if>
        <if test="monthPeriod != null and monthPeriod != ''">month_period = #{monthPeriod},</if>
        <if test="status != null">status = #{status},</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="deleteGxtMonthFanInspectionById" parameterType="Long">
    delete from gxt_month_fan_inspection where id = #{id}
</delete>

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

</mapper>