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

    <resultMap type="GxtWorkOrderPerson" id="GxtWorkOrderPersonResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
        <result property="orderId"    column="order_id"    />
        <result property="orderCode"    column="order_code"    />
        <result property="status"    column="status"    />
        <result property="misNo"    column="mis_no"    />
        <result property="userName"    column="user_name"    />
        <result property="score"    column="score"    />
        <result property="selfScore"    column="self_score"    />
        <result property="reviewScore"    column="review_score"    />
        <result property="finalScore"    column="final_score"    />
        <result property="isLeader"    column="is_leader"    />
        <result property="feedbackReason"    column="feedback_reason"    />
        <result property="confirmStatus"    column="confirm_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="extraScore"    column="extra_score"    />
        <result property="totalScore"    column="total_score"    />
        <result property="runScore"    column="run_score"    />
        <result property="stopScore"    column="stop_score"    />
    </resultMap>

    <sql id="selectGxtWorkOrderPersonVo">
        select id, user_id, nick_name, order_id, order_code, status, mis_no, user_name, score, self_score, review_score, final_score, is_leader, feedback_reason, confirm_status, create_by, create_time, update_by, update_time, remark, extra_score, total_score, run_score, stop_score
        from gxt_work_order_person
    </sql>

    <select id="selectGxtWorkOrderPersonList" parameterType="GxtWorkOrderPerson" resultMap="GxtWorkOrderPersonResult">
        <include refid="selectGxtWorkOrderPersonVo"/>
        <where>
            <if test="userId != null"> and user_id = #{userId}</if>
            <if test="nickName != null  and nickName != ''"> and nick_name like concat('%', #{nickName}, '%')</if>
            <if test="orderId != null"> and order_id = #{orderId}</if>
            <if test="orderCode != null  and orderCode != ''"> and order_code like concat('%', #{orderCode}, '%')</if>
            <if test="status != null"> and status = #{status}</if>
            <if test="misNo != null  and misNo != ''"> and mis_no like concat('%', #{misNo}, '%')</if>
            <if test="selfScore != null "> and self_score = #{selfScore}</if>
            <if test="reviewScore != null "> and review_score = #{reviewScore}</if>
            <if test="finalScore != null "> and final_score = #{finalScore}</if>
            <if test="isLeader != null "> and is_leader = #{isLeader}</if>
            <if test="feedbackReason != null  and feedbackReason != ''"> and feedback_reason = #{feedbackReason}</if>
            <if test="confirmStatus != null "> and confirm_status = #{confirmStatus}</if>
        </where>
        order by create_time desc
    </select>

    <select id="selectGxtWorkOrderPersonListByScore" parameterType="GxtWorkOrderPerson" resultMap="GxtWorkOrderPersonResult">
        SELECT
        wop.*
        FROM
        gxt_work_order_person wop
        LEFT JOIN gxt_work_order wo ON wop.order_id = wo.id
        JOIN (
        SELECT order_id, COUNT(*) AS cnt
        FROM gxt_work_order_person
        GROUP BY order_id
        ) wop_counts ON wop.order_id = wop_counts.order_id
        WHERE
        wop.user_id = #{userId}
        AND wo.work_order_status='archived'
        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')
        ORDER BY wop.create_time DESC
    </select>

    <select id="selectGxtWorkOrderPersonById" parameterType="Long" resultMap="GxtWorkOrderPersonResult">
        <include refid="selectGxtWorkOrderPersonVo"/>
        where id = #{id}
    </select>

    <select id="selectGxtWorkOrderPersonByOrderId" parameterType="Long" resultMap="GxtWorkOrderPersonResult">
        <include refid="selectGxtWorkOrderPersonVo"/>
        where order_id = #{orderId}
        order by create_time asc
    </select>

    <insert id="insertGxtWorkOrderPerson" parameterType="GxtWorkOrderPerson" useGeneratedKeys="true" keyProperty="id">
        insert into gxt_work_order_person
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="nickName != null">nick_name,</if>
            <if test="orderId != null">order_id,</if>
            <if test="orderCode != null">order_code,</if>
            <if test="status != null">status,</if>
            <if test="misNo != null">mis_no,</if>
            <if test="userName != null">user_name,</if>
            <if test="score != null">score,</if>
            <if test="selfScore != null">self_score,</if>
            <if test="reviewScore != null">review_score,</if>
            <if test="finalScore != null">final_score,</if>
            <if test="isLeader != null">is_leader,</if>
            <if test="feedbackReason != null">feedback_reason,</if>
            <if test="confirmStatus != null">confirm_status,</if>
            <if test="extraScore != null">extra_score,</if>
            <if test="totalScore != null">total_score,</if>
            <if test="runScore != null">run_score,</if>
            <if test="stopScore != null">stop_score,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="nickName != null">#{nickName},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="orderCode != null">#{orderCode},</if>
            <if test="status != null">#{status},</if>
            <if test="misNo != null">#{misNo},</if>
            <if test="userName != null">#{userName},</if>
            <if test="score != null">#{score},</if>
            <if test="selfScore != null">#{selfScore},</if>
            <if test="reviewScore != null">#{reviewScore},</if>
            <if test="finalScore != null">#{finalScore},</if>
            <if test="isLeader != null">#{isLeader},</if>
            <if test="feedbackReason != null">#{feedbackReason},</if>
            <if test="confirmStatus != null">#{confirmStatus},</if>
            <if test="extraScore != null">#{extraScore},</if>
            <if test="totalScore != null">#{totalScore},</if>
            <if test="runScore != null">#{runScore},</if>
            <if test="stopScore != null">#{stopScore},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateGxtWorkOrderPerson" parameterType="GxtWorkOrderPerson">
        update gxt_work_order_person
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="nickName != null">nick_name = #{nickName},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="orderCode != null">order_code = #{orderCode},</if>
            <if test="status != null">status = #{status},</if>
            <if test="misNo != null">mis_no = #{misNo},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="score != null">score = #{score},</if>
            <if test="selfScore != null">self_score = #{selfScore},</if>
            <if test="reviewScore != null">review_score = #{reviewScore},</if>
            <if test="finalScore != null">final_score = #{finalScore},</if>
            <if test="isLeader != null">is_leader = #{isLeader},</if>
            <if test="feedbackReason != null">feedback_reason = #{feedbackReason},</if>
            <if test="confirmStatus != null">confirm_status = #{confirmStatus},</if>
            <if test="extraScore != null">extra_score = #{extraScore},</if>
            <if test="totalScore != null">total_score = #{totalScore},</if>
            <if test="runScore != null">run_score = #{runScore},</if>
            <if test="stopScore != null">stop_score = #{stopScore},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">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="deleteGxtWorkOrderPersonById" parameterType="Long">
        delete from gxt_work_order_person where id = #{id}
    </delete>

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

    <delete id="deleteGxtWorkOrderPersonByOrderId" parameterType="Long">
        delete from gxt_work_order_person where order_id = #{orderId}
    </delete>
</mapper>