<?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"/>
        <result property="outsourceUnitPrice" column="outsource_unit_price"/>
        <result property="scheduleStartDate" column="schedule_start_date"/>
        <result property="scheduleEndDate" column="schedule_end_date"/>
        <result property="actualStartDate" column="actual_start_date"/>
        <result property="actualEndDate" column="actual_end_date"/>
        <result property="vendorId" column="vendor_id"/>
        <result property="vendorName" column="vendor_name"/>
        <result property="outsourced" column="outsourced"/>
        <result property="stdWorkingTime" column="std_working_time"/>
        <result property="stdWorkingTimeUom" column="std_working_time_uom"/>
        <result property="unitPrice" column="unit_price"/>
    </resultMap>

    <sql id="selectProTaskWorkunitVo">
        select ptw.task_workunit_id,
               ptw.task_id,
               ptw.idx,
               ptw.workunit_id,
               ptw.vendor_id,
               ptw.vendor_name,
               ptw.outsource_unit_price,
               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.actual_start_date,
               ptw.actual_end_date,
               ptw.status,
               ptw.create_by,
               ptw.create_time,
               ptw.remark,
               ptw.outsourced
        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>
            <if test="outsourceUnitPrice != null">outsource_unit_price,</if>
            <if test="vendorId != null">vendor_id,</if>
            <if test="vendorName != null">vendor_name,</if>
            <if test="outsourced != null">outsourced,</if>
            <if test="stdWorkingTime != null">std_working_time,</if>
            <if test="stdWorkingTimeUom != null">std_working_time_uom,</if>
            <if test="unitPrice != null">unit_price,</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>
            <if test="outsourceUnitPrice != null">#{outsourceUnitPrice},</if>
            <if test="vendorId != null">#{vendorId},</if>
            <if test="vendorName != null">#{vendorName},</if>
            <if test="outsourced != null">#{outsourced},</if>
            <if test="stdWorkingTime != null">#{stdWorkingTime},</if>
            <if test="stdWorkingTimeUom != null">#{stdWorkingTimeUom},</if>
            <if test="unitPrice != null">#{unitPrice},</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="actualStartDate != null">actual_start_date = #{actualStartDate},</if>
            <if test="actualEndDate != null">actual_end_date = #{actualEndDate},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="outsourceUnitPrice != null">outsource_unit_price = #{outsourceUnitPrice},</if>
            <if test="vendorId != null">vendor_id = #{vendorId},</if>
            <if test="vendorName != null">vendor_name = #{vendorName},</if>
            <if test="outsourced != null">outsourced = #{outsourced},</if>
            <if test="stdWorkingTime != null">std_working_time = #{stdWorkingTime},</if>
            <if test="stdWorkingTimeUom != null">std_working_time_uom = #{stdWorkingTimeUom},</if>
            <if test="unitPrice != null">unit_price = #{unitPrice},</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>
    <delete id="deleteByTaskIds">
        delete from pro_task_workunit where task_id in
        <foreach item="taskId" collection="taskIds" open="(" separator="," close=")">
            #{taskId}
        </foreach>
    </delete>
</mapper>
