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

    <resultMap type="ProMaterialReturn" id="ProMaterialReturnResult">
        <result property="materialReturnId" column="material_return_id"/>
        <result property="applyNo" column="apply_no"/>
        <result property="itemId" column="item_id"/>
        <result property="taskId" column="task_id"/>
        <result property="issueLineId" column="issue_line_id"/>
        <result property="workorderId" column="workorder_id"/>
        <result property="backNum" column="back_num"/>
        <result property="batchNo" column="batch_no"/>
        <result property="packNo" column="pack_no"/>
        <result property="remark" column="remark"/>
        <result property="attr1" column="attr1"/>
        <result property="attr2" column="attr2"/>
        <result property="attr3" column="attr3"/>
        <result property="attr4" column="attr4"/>
        <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="returnReason"    column="return_reason"    />
        <result property="itemName" column="item_name"/>
        <result property="itemCode" column="item_code"/>
        <result property="workorderCode" column="workorder_code"/>
        <result property="isQualified" column="is_qualified"/>
        <result property="taskWorkunitId" column="task_workunit_id"/>
        <result property="windCase" column="wind_case"/>
        <result property="returnNum" column="return_num"/>
        <result property="requirementNumber" column="requirement_number"/>
        <result property="requirementProjectNumber" column="requirement_project_number"/>

    </resultMap>

    <sql id="selectProMaterialReturnVo">
        select p.material_return_id,
               p.apply_no,
               p.item_id,
               p.wind_case,
               p.requirement_number,
               p.requirement_project_number,
               p.return_num,
               p.task_id,
               p.issue_line_id,
               p.workorder_id,
               p.workorder_code,
               p.task_workunit_id,
               p.back_num,
               p.return_reason,
               p.is_qualified,
               p.batch_no,
               p.pack_no,
               p.remark,
               p.attr1,
               p.attr2,
               p.attr3,
               p.attr4,
               p.create_by,
               p.create_time,
               p.update_by,
               p.update_time,
               mw.workunit_code,
               mw.workunit_name,
               m.item_name,
               m.sap_item_code
        from pro_material_return p
                left join pro_task_workunit ptw on p.task_workunit_id = ptw.task_workunit_id
                left join md_workunit mw on ptw.workunit_id = mw.workunit_id
                 left join md_item m on p.item_id = m.item_id
    </sql>

    <select id="selectProMaterialReturnList" parameterType="ProMaterialReturn" resultMap="ProMaterialReturnResult">
        <include refid="selectProMaterialReturnVo"/>
        <where>
            <if test="applyNo != null  and applyNo != ''">and p.apply_no = #{applyNo}</if>
            <if test="itemId != null ">and p.item_id = #{itemId}</if>
            <if test="taskId != null ">and p.task_id = #{taskId}</if>
            <if test="taskId != null ">and p.task_id = #{taskId}</if>
            <if test="itemCode != null ">and m.item_code = #{itemCode}</if>
            <if test="sapItemCode != null and sapItemCode != '' ">and m.sap_item_code = #{sapItemCode}</if>
            <if test="itemName != null and itemName != ''">and m.item_name = #{itemName}</if>
            <if test="params.beginPurchaseDate != null and params.beginPurchaseDate != '' and params.endPurchaseDate != null and params.endPurchaseDate != ''">
                and p.create_time between #{params.beginPurchaseDate} and #{params.endPurchaseDate}
            </if>
            <if test="issueLineId != null ">and p.issue_line_id = #{issueLineId}</if>
            <if test="workorderId != null ">and p.workorder_id = #{workorderId}</if>
            <if test="taskWorkunitId != null ">and p.task_workunit_id = #{taskWorkunitId}</if>
            <if test="backNum != null ">and p.back_num = #{backNum}</if>
            <if test="batchNo != null  and batchNo != ''">and p.batch_no = #{batchNo}</if>
            <if test="packNo != null  and packNo != ''">and p.pack_no = #{packNo}</if>
            <if test="createBy != null  and createBy != ''">and p.create_by = #{createBy}</if>
            <if test="workorderCode != null  and workorderCode != ''">and p.workorder_code = #{workorderCode}</if>
            <if test="windCase != null  and windCase != ''">and p.wind_case = #{windCase}</if>
            <if test="returnNum != null  and returnNum != ''">and p.return_num = #{returnNum}</if>
            <if test="requirementNumber != null  and requirementNumber != ''">and p.requirement_number = #{requirementNumber}</if>
            <if test="requirementProjectNumber != null  and requirementProjectNumber != ''">and p.requirement_project_number = #{requirementProjectNumber}</if>
        </where>
    </select>

    <select id="selectProMaterialReturnByMaterialReturnId" parameterType="Long" resultMap="ProMaterialReturnResult">
        <include refid="selectProMaterialReturnVo"/>
        where material_return_id = #{materialReturnId}
    </select>


    <insert id="insertProMaterialReturn" parameterType="ProMaterialReturn" useGeneratedKeys="true"
            keyProperty="materialReturnId">
        insert into pro_material_return
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="applyNo != null">apply_no,</if>
            <if test="itemId != null">item_id,</if>
            <if test="taskId != null">task_id,</if>
            <if test="issueLineId != null">issue_line_id,</if>
            <if test="workorderId != null">workorder_id,</if>
            <if test="taskWorkunitId != null">task_workunit_id,</if>
            <if test="backNum != null">back_num,</if>
            <if test="batchNo != null">batch_no,</if>
            <if test="packNo != null">pack_no,</if>
            <if test="remark != null">remark,</if>
            <if test="attr1 != null">attr1,</if>
            <if test="attr2 != null">attr2,</if>
            <if test="attr3 != null">attr3,</if>
            <if test="attr4 != null">attr4,</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="workorderCode != null">workorder_code,</if>
            <if test="isQualified != null">is_qualified,</if>
            <if test="windCase != null">wind_case,</if>
            <if test="returnNum != null">return_num,</if>
            <if test="requirementNumber != null">requirement_number,</if>
            <if test="requirementProjectNumber != null">requirement_project_number,</if>
            <if test="returnReason != null">return_reason,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="applyNo != null">#{applyNo},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="issueLineId != null">#{issueLineId},</if>
            <if test="workorderId != null">#{workorderId},</if>
            <if test="taskWorkunitId != null">#{taskWorkunitId},</if>
            <if test="backNum != null">#{backNum},</if>
            <if test="batchNo != null">#{batchNo},</if>
            <if test="packNo != null">#{packNo},</if>
            <if test="remark != null">#{remark},</if>
            <if test="attr1 != null">#{attr1},</if>
            <if test="attr2 != null">#{attr2},</if>
            <if test="attr3 != null">#{attr3},</if>
            <if test="attr4 != null">#{attr4},</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="workorderCode != null">#{workorderCode},</if>
            <if test="isQualified != null">#{isQualified},</if>
            <if test="windCase != null ">#{windCase},</if>
            <if test="returnNum != null">#{returnNum},</if>
            <if test="requirementNumber != null">#{requirementNumber},</if>
            <if test="requirementProjectNumber != null">#{requirementProjectNumber},</if>
            <if test="returnReason != null">#{returnReason},</if>
        </trim>
    </insert>

    <update id="updateProMaterialReturn" parameterType="ProMaterialReturn">
        update pro_material_return
        <trim prefix="SET" suffixOverrides=",">
            <if test="applyNo != null">apply_no = #{applyNo},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="issueLineId != null">issue_line_id = #{issueLineId},</if>
            <if test="workorderId != null">workorder_id = #{workorderId},</if>
            <if test="taskWorkunitId != null ">task_workunit_id = #{taskWorkunitId},</if>
            <if test="backNum != null">back_num = #{backNum},</if>
            <if test="workorderCode != null">workorder_code = #{workorderCode},</if>
            <if test="isQualified != null">is_qualified = #{isQualified},</if>
            <if test="batchNo != null">batch_no = #{batchNo},</if>
            <if test="packNo != null">pack_no = #{packNo},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="attr1 != null">attr1 = #{attr1},</if>
            <if test="attr2 != null">attr2 = #{attr2},</if>
            <if test="attr3 != null">attr3 = #{attr3},</if>
            <if test="attr4 != null">attr4 = #{attr4},</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="windCase != null ">wind_case = #{windCase},</if>
            <if test="returnNum != null">return_num = #{returnNum},</if>
            <if test="requirementNumber != null">requirement_number = #{requirementNumber},</if>
            <if test="requirementProjectNumber != null ">requirement_project_number = #{requirementProjectNumber},</if>
            <if test="returnReason != null">return_reason = #{returnReason},</if>
        </trim>
        where material_return_id = #{materialReturnId}
    </update>

    <delete id="deleteProMaterialReturnByMaterialReturnId" parameterType="Long">
        delete
        from pro_material_return
        where material_return_id = #{materialReturnId}
    </delete>

    <delete id="deleteProMaterialReturnByMaterialReturnIds" parameterType="String">
        delete from pro_material_return where material_return_id in
        <foreach item="materialReturnId" collection="array" open="(" separator="," close=")">
            #{materialReturnId}
        </foreach>
    </delete>
</mapper>
