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

    <resultMap type="ProAllocationMaterial" id="ProAllocationMaterialResult">
        <result property="allocationMaterialId" column="allocation_material_id"/>
        <result property="applyNo" column="apply_no"/>
        <result property="sapItemCode" column="sap_item_code"/>
        <result property="itemName" column="item_name"/>
        <result property="workstationId" column="workstation_id"/>
        <result property="workstationCode" column="workstation_code"/>
        <result property="taskId" column="task_id"/>
        <result property="taskCode" column="task_code"/>
        <result property="taskWorkunitId" column="task_workunit_id"/>
        <result property="applyNum" column="apply_num"/>
        <result property="allocationNum" column="allocation_num"/>
        <result property="windCase" column="wind_case"/>
        <result property="endTime" column="end_time"/>
        <result property="startTime" column="start_time"/>
        <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="buildMethod" column="build_method"/>
        <result property="materialType" column="material_type"/>
    </resultMap>

    <sql id="selectProAllocationMaterialVo">
        select   a.allocation_material_id,
                 a.apply_no,
                 a.build_method,
                 a.sap_item_code,
                 a.item_name,
                 a.workstation_id,
                 a.workstation_code,
                 a.task_id,
                 a.task_code,
                 a.apply_num,
                 a.allocation_num,
                 a.wind_case,
                 a.end_time,
                 a.start_time,
                 a.create_by,
                 a.create_time,
                 a.update_by,
                 a.update_time,
                 a.material_type,
                 w.workstation_name
        from pro_allocation_material  a
                 LEFT JOIN md_workstation w on a.workstation_id=w.workstation_id
    </sql>

    <select id="selectProAllocationMaterialList" parameterType="ProAllocationMaterial"
            resultMap="ProAllocationMaterialResult">
        <include refid="selectProAllocationMaterialVo"/>
        <where>
            <if test="applyNo != null  and applyNo != ''">and apply_no = #{applyNo}</if>
            <if test="sapItemCode != null  and sapItemCode != ''">and sap_item_code = #{sapItemCode}</if>
            <if test="itemName != null  and itemName != ''">and item_name like concat('%', #{itemName}, '%')</if>
            <if test="workstationId != null ">and workstation_id = #{workstationId}</if>
            <if test="workstationCode != null  and workstationCode != ''">and workstation_code = #{workstationCode}</if>
            <if test="taskId != null ">and task_id = #{taskId}</if>
            <if test="taskCode != null  and taskCode != ''">and task_code = #{taskCode}</if>
            <if test="applyNum != null ">and apply_num = #{applyNum}</if>
            <if test="allocationNum != null ">and allocation_num = #{allocationNum}</if>
            <if test="windCase != null  and windCase != ''">and wind_case = #{windCase}</if>
            <if test="endTime != null ">and end_time = #{endTime}</if>
            <if test="params.beginPurchaseDate != null and params.beginPurchaseDate != '' and params.endPurchaseDate != null and params.endPurchaseDate != ''">
                and start_time between #{params.beginPurchaseDate} and #{params.endPurchaseDate}
            </if>
        </where>
    </select>

    <select id="selectProAllocationMaterialByAllocationMaterialId" parameterType="Long"
            resultMap="ProAllocationMaterialResult">
        <include refid="selectProAllocationMaterialVo"/>
        where allocation_material_id = #{allocationMaterialId}
    </select>

    <insert id="insertProAllocationMaterial" parameterType="ProAllocationMaterial">
        insert into pro_allocation_material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="allocationMaterialId != null">allocation_material_id,</if>
            <if test="applyNo != null">apply_no,</if>
            <if test="sapItemCode != null">sap_item_code,</if>
            <if test="itemName != null">item_name,</if>
            <if test="workstationId != null">workstation_id,</if>
            <if test="workstationCode != null">workstation_code,</if>
            <if test="taskId != null">task_id,</if>
            <if test="taskCode != null">task_code,</if>
            <if test="taskWorkunitId != null">task_workunit_id,</if>
            <if test="applyNum != null">apply_num,</if>
            <if test="allocationNum != null">allocation_num,</if>
            <if test="windCase != null">wind_case,</if>
            <if test="endTime != null">end_time,</if>
            <if test="startTime != null">start_time,</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="buildMethod != null">build_method,</if>
            <if test="materialType != null">material_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="allocationMaterialId != null">#{allocationMaterialId},</if>
            <if test="applyNo != null">#{applyNo},</if>
            <if test="sapItemCode != null">#{sapItemCode},</if>
            <if test="itemName != null">#{itemName},</if>
            <if test="workstationId != null">#{workstationId},</if>
            <if test="workstationCode != null">#{workstationCode},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="taskCode != null">#{taskCode},</if>
            <if test="taskWorkunitId != null">#{taskWorkunitId},</if>
            <if test="applyNum != null">#{applyNum},</if>
            <if test="allocationNum != null">#{allocationNum},</if>
            <if test="windCase != null">#{windCase},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="startTime != null">#{startTime},</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="buildMethod != null">#{buildMethod},</if>
            <if test="materialType != null">#{materialType},</if>
        </trim>
    </insert>

    <update id="updateProAllocationMaterial" parameterType="ProAllocationMaterial">
        update pro_allocation_material
        <trim prefix="SET" suffixOverrides=",">
            <if test="applyNo != null">apply_no = #{applyNo},</if>
            <if test="sapItemCode != null">sap_item_code = #{sapItemCode},</if>
            <if test="itemName != null">item_name = #{itemName},</if>
            <if test="workstationId != null">workstation_id = #{workstationId},</if>
            <if test="workstationCode != null">workstation_code = #{workstationCode},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="taskCode != null">task_code = #{taskCode},</if>
            <if test="taskWorkunitId != null">task_workunit_id = #{taskWorkunitId},</if>
            <if test="applyNum != null">apply_num = #{applyNum},</if>
            <if test="allocationNum != null">allocation_num = #{allocationNum},</if>
            <if test="windCase != null">wind_case = #{windCase},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="startTime != null">start_time = #{startTime},</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="buildMethod != null">build_method = #{buildMethod},</if>
            <if test="materialType != null">material_type = #{materialType},</if>
        </trim>
        where allocation_material_id = #{allocationMaterialId}
    </update>

    <delete id="deleteProAllocationMaterialByAllocationMaterialId" parameterType="Long">
        delete
        from pro_allocation_material
        where allocation_material_id = #{allocationMaterialId}
    </delete>

    <delete id="deleteProAllocationMaterialByAllocationMaterialIds" parameterType="String">
        delete from pro_allocation_material where allocation_material_id in
        <foreach item="allocationMaterialId" collection="array" open="(" separator="," close=")">
            #{allocationMaterialId}
        </foreach>
    </delete>
</mapper>