<?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.ximai.mes.pro.mapper.task.ProTaskWorkunitMapper">

    <resultMap type="com.ximai.mes.pro.domain.task.ProTaskWorkunit" id="ProTaskWorkunitResult">
        <result property="taskWorkunitId" column="task_workunit_id"/>
        <result property="taskWorkunitCode" column="task_workunit_code"/>
        <result property="taskId" column="task_id"/>
        <result property="idx" column="idx"/>
        <result property="workunitId" column="workunit_id"/>
        <result property="workunitCode" column="workunit_code"/>
        <result property="workunitName" column="workunit_name"/>
        <result property="unitOfMeasure" column="unit_of_measure"/>
        <result property="quantity" column="quantity"/>
        <result property="quantityProduced" column="quantity_produced"/>
        <result property="quantityQualify" column="quantity_qualify"/>
        <result property="quantityUnqualify" column="quantity_unqualify"/>
        <result property="quantityChanged" column="quantity_changed"/>
        <result property="status" column="status"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectProTaskWorkunitVo">
        select ptw.task_workunit_id,
               ptw.task_id,
               ptw.idx,
               ptw.workunit_id,
               mw.workunit_code,
               mw.workunit_name,
               ptw.unit_of_measure,
               ptw.quantity,
               ptw.quantity_produced,
               ptw.quantity_qualify,
               ptw.quantity_unqualify,
               ptw.quantity_changed,
               ptw.schedule_start_date,
               ptw.schedule_end_date,
               ptw.status,
               ptw.create_by,
               ptw.create_time,
               ptw.remark
        from pro_task_workunit ptw
                 left join md_workunit mw
                           on mw.workunit_id = ptw.workunit_id
    </sql>


    <select id="selectProTaskWorkunitList" resultMap="ProTaskWorkunitResult">
        <include refid="selectProTaskWorkunitVo"/>
        <where>
            <if test="taskId != null ">and ptw.task_id = #{taskId}</if>
            <if test="taskWorkunitId != null ">and ptw.task_workunit_id = #{taskWorkunitId}</if>
            <if test="taskIds != null  ">and ptw.task_id IN
                <foreach item="taskId" collection="taskIds" open="(" separator="," close=")">
                    #{taskId}
                </foreach>
            </if>
            <if test="idx != null ">and ptw.idx = #{idx}</if>
            <if test="workunitId != null ">and ptw.workunit_id = #{workunitId}</if>
            <if test="unitOfMeasure != null  and unitOfMeasure != ''">and ptw.unit_of_measure = #{unitOfMeasure}</if>
            <if test="quantity != null ">and ptw.quantity = #{quantity}</if>
            <if test="quantityProduced != null ">and ptw.quantity_produced = #{quantityProduced}</if>
            <if test="quantityQualify != null ">and ptw.quantity_qualify = #{quantityQualify}</if>
            <if test="quantityUnqualify != null ">and ptw.quantity_unqualify = #{quantityUnqualify}</if>
            <if test="quantityChanged != null ">and ptw.quantity_changed = #{quantityChanged}</if>
        </where>
    </select>

    <select id="selectProTaskWorkunitByTaskWorkunitId" parameterType="Long" resultMap="ProTaskWorkunitResult">
        <include refid="selectProTaskWorkunitVo"/>
        where ptw.task_workunit_id = #{taskWorkunitId}
    </select>

    <select id="selectProTaskWorkunitByTaskId" parameterType="Long" resultMap="ProTaskWorkunitResult">
        <include refid="selectProTaskWorkunitVo"/>
        where ptw.task_id = #{taskId}
    </select>

    <select id="selectProTaskWorkunitTaskId" parameterType="Long" resultMap="ProTaskWorkunitResult">
        <include refid="selectProTaskWorkunitVo"/>
        where ptw.task_id = #{taskId}
    </select>

    <insert id="insertProTaskWorkunit" parameterType="ProTaskWorkunit" useGeneratedKeys="true"
            keyProperty="taskWorkunitId">
        insert into pro_task_workunit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskId != null">task_id,</if>
            <if test="idx != null">idx,</if>
            <if test="workunitId != null">workunit_id,</if>
            <if test="unitOfMeasure != null">unit_of_measure,</if>
            <if test="quantity != null">quantity,</if>
            <if test="quantityProduced != null">quantity_produced,</if>
            <if test="quantityQualify != null">quantity_qualify,</if>
            <if test="quantityUnqualify != null">quantity_unqualify,</if>
            <if test="quantityChanged != null">quantity_changed,</if>
            <if test="status != null">status,</if>
            <if test="scheduleStartDate != null">schedule_start_date,</if>
            <if test="scheduleEndDate != null">schedule_end_date,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskId != null">#{taskId},</if>
            <if test="idx != null">#{idx},</if>
            <if test="workunitId != null">#{workunitId},</if>
            <if test="unitOfMeasure != null">#{unitOfMeasure},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="quantityProduced != null">#{quantityProduced},</if>
            <if test="quantityQualify != null">#{quantityQualify},</if>
            <if test="quantityUnqualify != null">#{quantityUnqualify},</if>
            <if test="quantityChanged != null">#{quantityChanged},</if>
            <if test="status != null">#{status},</if>
            <if test="scheduleStartDate != null">#{scheduleStartDate},</if>
            <if test="scheduleEndDate != null">#{scheduleEndDate},</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updateProTaskWorkunit" parameterType="ProTaskWorkunit">
        update pro_task_workunit
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="idx != null">idx = #{idx},</if>
            <if test="workunitId != null">workunit_id = #{workunitId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="unitOfMeasure != null">unit_of_measure = #{unitOfMeasure},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="quantityProduced != null">quantity_produced = #{quantityProduced},</if>
            <if test="quantityQualify != null">quantity_qualify = #{quantityQualify},</if>
            <if test="quantityUnqualify != null">quantity_unqualify = #{quantityUnqualify},</if>
            <if test="quantityChanged != null">quantity_changed = #{quantityChanged},</if>
            <if test="status != null">status = #{status},</if>
            <if test="scheduleStartDate != null">schedule_start_date = #{scheduleStartDate},</if>
            <if test="scheduleEndDate != null">schedule_end_date = #{scheduleEndDate},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where task_workunit_id = #{taskWorkunitId}
    </update>

    <delete id="deleteProTaskWorkunitByTaskWorkunitId" parameterType="Long">
        delete
        from pro_task_workunit
        where task_workunit_id = #{taskWorkunitId}
    </delete>

    <delete id="deleteProTaskWorkunitByTaskWorkunitIds" parameterType="String">
        delete from pro_task_workunit where task_workunit_id in
        <foreach item="taskWorkunitId" collection="array" open="(" separator="," close=")">
            #{taskWorkunitId}
        </foreach>
    </delete>
</mapper>
