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

    <resultMap type="ProMaterialRequest" id="ProMaterialRequestResult">
        <result property="materialRequestId" column="material_request_id"/>
        <result property="applyNo" column="apply_no"/>
        <result property="itemId" column="item_id"/>
        <result property="taskId" column="task_id"/>
        <result property="applyNum" column="apply_num"/>
        <result property="applyType" column="apply_type"/>
        <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="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="workorderCode" column="workorder_code"/>
        <result property="taskWorkunitId" column="task_workunit_id"/>
        <result property="materialType" column="material_type"/>
        <result property="pickingReason" column="picking_reason"/>
        <result property="workunitCode" column="workunit_code"/>
        <result property="workunitId" column="workunit_id"/>
        <result property="workunitName" column="workunit_name"/>
        <result property="workshopCode" column="workshop_code"/>
        <result property="workstationCode"    column="workstation_code"    />
        <result property="workstationName"    column="workstation_name"    />
        <result property="windCase"    column="wind_case"    />
        <result property="grantNum"    column="grant_num"    />
        <result property="requirementNumber"    column="requirement_number"    />
        <result property="requirementProjectNumber"    column="requirement_project_number"    />
    </resultMap>

    <sql id="selectProMaterialRequestVo">
        select p.material_request_id,
               p.apply_no,
               p.workstation_code,
               p.workstation_name,
               p.wind_case,
               p.grant_num,
               p.apply_type,
               p.start_time,
               p.workunit_code,
               p.material_type,
               p.picking_reason,
               p.end_time,
               p.item_id,
               p.task_workunit_id,
               p.workorder_code,
               p.task_id,
               p.apply_num,
               p.workunit_code,
               p.remark,
               p.attr1,
               p.attr2,
               p.attr3,
               p.attr4,
               p.create_by,
               p.create_time,
               p.update_by,
               p.update_time,
               m.sap_item_code,
               m.item_name,
               pr.arrange_code
        from pro_material_request p
        left join md_item m on m.item_id=p.item_id
        left join pro_task pr on p.task_id=pr.task_id

    </sql>

    <select id="selectProMaterialRequestList" parameterType="ProMaterialRequest" resultMap="ProMaterialRequestResult">
        <include refid="selectProMaterialRequestVo"/>
        <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="taskWorkunitId != null ">and p.task_workunit_id = #{taskWorkunitId}</if>
            <if test="workorderCode != null ">and p.workorder_code = #{workorderCode}</if>
            <if test="pickingReason != null ">and p.picking_reason = #{pickingReason}</if>
            <if test="materialType != null ">and p.material_type = #{materialType}</if>
            <if test="reason != null ">and p.reason = #{reason}</if>
            <if test="applyNum != null ">and p.apply_num = #{applyNum}</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="applyType != null ">and p.apply_type = #{applyType}</if>
            <if test="createBy != null  and createBy != ''">and p.create_by = #{createBy}</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>
            <if test="attr1 != null  and attr1 != ''">and p.attr1 = #{attr1}</if>
            <if test="attr2 != null  and attr2 != ''">and p.attr2 = #{attr2}</if>
            <if test="attr3 != null ">and p.attr3 = #{attr3}</if>
            <if test="attr4 != null ">and p.attr4 = #{attr4}</if>
        </where>
    </select>


    <select id="selectProMaterialRequestByMaterialRequestId" parameterType="Long" resultMap="ProMaterialRequestResult">
        <include refid="selectProMaterialRequestVo"/>
        where p.material_request_id = #{materialRequestId}
    </select>



    <select id="selectMaterialRequestPrintTab" resultType="com.ximai.mes.pro.domain.vo.ProMaterialRequestVo">
        SELECT * FROM pro_material_request pmr
        LEFT JOIN md_item mi ON pmr.item_id = mi.item_id
        WHERE pmr.material_request_id IN
        <foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>
    <select id="selectMaterialRequestPrintTabByApplyNo" resultType="com.ximai.mes.pro.domain.vo.ProMaterialRequestVo"
            parameterType="java.lang.String">
        SELECT *
        FROM pro_material_request pmr
        LEFT JOIN md_item mi ON pmr.item_id = mi.item_id
        WHERE apply_no = #{applyNo}
    </select>


    <insert id="insertProMaterialRequest" parameterType="ProMaterialRequest" useGeneratedKeys="true"
            keyProperty="materialRequestId">
        insert into pro_material_request
        <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="taskWorkunitId != null ">task_workunit_id,</if>
            <if test="workorderCode != null ">workorder_code,</if>
            <if test="materialType != null ">material_type,</if>
            <if test="pickingReason != null ">picking_reason,</if>
            <if test="workunitId != null ">workunit_id,</if>
            <if test="workunitCode != null ">workunit_code,</if>
            <if test="workunitName != null ">workunit_name,</if>
            <if test="applyNum != null">apply_num,</if>
            <if test="applyType != null">apply_type,</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="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="workstationCode != null">workstation_code,</if>
            <if test="workstationName != null">workstation_name,</if>
            <if test="windCase != null">wind_case,</if>
            <if test="grantNum != null">grant_num,</if>
            <if test="requirementNumber != null ">requirement_number,</if>
            <if test="requirementProjectNumber != null ">requirement_project_number,</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="taskWorkunitId != null ">#{taskWorkunitId},</if>
            <if test="workorderCode != null ">#{workorderCode},</if>
            <if test="materialType != null ">#{materialType},</if>
            <if test="pickingReason != null ">#{pickingReason},</if>
            <if test="workunitId != null ">#{workunitId},</if>
            <if test="workunitCode != null ">#{workunitCode},</if>
            <if test="workunitName != null ">#{workunitName},</if>
            <if test="applyNum != null">#{applyNum},</if>
            <if test="applyType != null">#{applyType},</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="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="workstationCode != null">#{workstationCode},</if>
            <if test="workstationName != null">#{workstationName},</if>
            <if test="windCase != null">#{windCase},</if>
            <if test="grantNum != null">#{grantNum},</if>
            <if test="requirementNumber != null ">#{requirementNumber},</if>
            <if test="requirementProjectNumber != null  ">#{requirementProjectNumber},</if>
        </trim>
    </insert>

    <update id="updateProMaterialRequest" parameterType="ProMaterialRequest">
        update pro_material_request
        <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="taskWorkunitId != null ">task_workunit_id = #{taskWorkunitId},</if>
            <if test="workorderCode != null ">workorder_code = #{workorderCode},</if>
            <if test="workunitId != null ">workunit_id = #{workunitId},</if>
            <if test="workunitName != null ">workunit_name =#{workunitName},</if>
            <if test="workunitCode != null ">workunit_code = #{workunitCode},</if>
            <if test="applyNum != null">apply_num = #{applyNum},</if>
            <if test="materialType != null ">material_type = #{materialType},</if>
            <if test="pickingReason != null ">picking_reason = #{pickingReason},</if>
            <if test="applyType != null ">apply_type = #{applyType},</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="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="workstationCode != null">workstation_code = #{workstationCode},</if>
            <if test="workstationName != null">workstation_name = #{workstationName},</if>
            <if test="windCase != null">wind_case = #{windCase},</if>
            <if test="grantNum != null">grant_num = #{grantNum},</if>
            <if test="requirementNumber != null">requirement_number = #{requirementNumber},</if>
            <if test="requirementProjectNumber != null ">requirement_project_number = #{requirementProjectNumber},</if>
        </trim>
        where material_request_id = #{materialRequestId}
    </update>

    <delete id="deleteProMaterialRequestByMaterialRequestId" parameterType="Long">
        delete
        from pro_material_request
        where material_request_id = #{materialRequestId}
    </delete>

    <delete id="deleteProMaterialRequestByMaterialRequestIds" parameterType="String">
        delete from pro_material_request where material_request_id in
        <foreach item="materialRequestId" collection="array" open="(" separator="," close=")">
            #{materialRequestId}
        </foreach>
    </delete>
</mapper>
