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

    <resultMap type="ProWorkstationMaterial" id="ProWorkstationMaterialResult">
        <result property="workstationMaterialId"    column="workstation_material_id"    />
        <result property="applyNo"    column="apply_no"    />
        <result property="itemId"    column="item_id"    />
        <result property="taskId"    column="task_id"    />
        <result property="taskWorkunitId"    column="task_workunit_id"    />
        <result property="applyType"    column="apply_type"    />
        <result property="applyNum"    column="apply_num"    />
        <result property="materialType"    column="material_type"    />
        <result property="pickingReason"    column="picking_reason"    />
        <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="remak"    column="remak"    />
        <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="workunitId"    column="workunit_id"    />
        <result property="workunitName"    column="workunit_name"    />
        <result property="workunitCode"    column="workunit_code"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="workstationId"    column="workstation_id"    />
    </resultMap>
    <resultMap type="com.ximai.mes.pro.domain.vo.ProWorkstationMaterialVo" id="ProWorkstationMaterialResults">
        <result property="workstationMaterialId"    column="workstation_material_id"    />
        <result property="applyNo"    column="apply_no"    />
        <result property="itemId"    column="item_id"    />
        <result property="taskId"    column="task_id"    />
        <result property="taskWorkunitId"    column="task_workunit_id"    />
        <result property="applyType"    column="apply_type"    />
        <result property="applyNum"    column="apply_num"    />
        <result property="materialType"    column="material_type"    />
        <result property="pickingReason"    column="picking_reason"    />
        <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="remak"    column="remak"    />
        <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="workunitId"    column="workunit_id"    />
        <result property="workunitName"    column="workunit_name"    />
        <result property="workunitCode"    column="workunit_code"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="sapItemCode"    column="sap_item_code"    />
        <result property="itemName"    column="item_name"    />
        <result property="arrangeCode"    column="arrange_code"    />
        <result property="workstationId"    column="workstation_id"    />
    </resultMap>

    <sql id="selectProWorkstationMaterialVo">
        select
            p.workstation_material_id,
            p.grant_num,
            p.wind_case,
            p.workstation_id,
            p.workstation_code,
            p.workstation_name,
            p.apply_no,
            p.item_id,
            p.material_type,
            p.task_id,
            p.apply_num,
            p.remak,
            p.attr1,
            p.attr2,
            p.attr3,
            p.attr4,
            p.create_by,
            p.create_time,
            p.update_by,
            p.update_time,
            p.start_time,
            p.end_time,
            p.workunit_name,
            p.workunit_code,
            p.apply_type,
            m.item_code,
            m.sap_item_code,
            m.item_name,
            pr.arrange_code ,
            pr.task_code
        from pro_workstation_material p
                 left join md_item m on p.item_id=m.item_id
                 left join pro_task pr on p.task_id=pr.task_id
    </sql>

    <select id="selectProWorkstationMaterialList" parameterType="com.ximai.mes.pro.domain.vo.ProWorkstationMaterialVo" resultMap="ProWorkstationMaterialResults">
        <include refid="selectProWorkstationMaterialVo"/>
        <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="applyType != null "> and p.apply_type = #{applyType}</if>
            <if test="applyNum != null "> and p.apply_num = #{applyNum}</if>
            <if test="materialType != null "> and p.material_type = #{materialType}</if>
            <if test="pickingReason != null  and pickingReason != ''"> and p.picking_reason = #{pickingReason}</if>
            <if test="workstationCode != null  and workstationCode != ''"> and p.workstation_code = #{workstationCode}</if>
            <if test="workstationName != null  and workstationName != ''"> and p.workstation_name like concat('%', #{workstationName}, '%')</if>
            <if test="windCase != null  and windCase != ''"> and p.wind_case = #{windCase}</if>
            <if test="grantNum != null "> and p.grant_num = #{grantNum}</if>
            <if test="remak != null  and remak != ''"> and p.remak = #{remak}</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="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="attr4 != null "> and p.attr4 = #{attr4}</if>
            <if test="workunitId != null "> and p.workunit_id = #{workunitId}</if>
            <if test="workunitName != null  and workunitName != ''"> and p.workunit_name like concat('%', #{workunitName}, '%')</if>
            <if test="workunitCode != null  and workunitCode != ''"> and p.workunit_code = #{workunitCode}</if>
            <if test="startTime != null "> and p.start_time = #{startTime}</if>
            <if test="endTime != null "> and p.end_time = #{endTime}</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="arrangeCode != null and arrangeCode != '' "> and  pr.arrange_code = #{arrangeCode}</if>
        </where>
    </select>

    <select id="selectProWorkstationMaterialByWorkstationMaterialId" parameterType="Long" resultMap="ProWorkstationMaterialResults">
        <include refid="selectProWorkstationMaterialVo"/>
        where workstation_material_id = #{workstationMaterialId}
    </select>

    <insert id="insertProWorkstationMaterial" parameterType="ProWorkstationMaterial" useGeneratedKeys="true" keyProperty="workstationMaterialId">
        insert into pro_workstation_material
        <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="applyType != null">apply_type,</if>
            <if test="applyNum != null">apply_num,</if>
            <if test="materialType != null">material_type,</if>
            <if test="pickingReason != null">picking_reason,</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="remak != null">remak,</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="workunitId != null">workunit_id,</if>
            <if test="workunitName != null">workunit_name,</if>
            <if test="workunitCode != null">workunit_code,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="workstationId != null">workstation_id,</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="applyType != null">#{applyType},</if>
            <if test="applyNum != null">#{applyNum},</if>
            <if test="materialType != null">#{materialType},</if>
            <if test="pickingReason != null">#{pickingReason},</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="remak != null">#{remak},</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="workunitId != null">#{workunitId},</if>
            <if test="workunitName != null">#{workunitName},</if>
            <if test="workunitCode != null">#{workunitCode},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="workstationId != null">#{workstationId},</if>
        </trim>
    </insert>

    <update id="updateProWorkstationMaterial" parameterType="ProWorkstationMaterial">
        update pro_workstation_material
        <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="applyType != null">apply_type = #{applyType},</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="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="remak != null">remak = #{remak},</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="workunitId != null">workunit_id = #{workunitId},</if>
            <if test="workunitName != null">workunit_name = #{workunitName},</if>
            <if test="workunitCode != null">workunit_code = #{workunitCode},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="workstationId != null">workstation_id = #{workstationId},</if>
        </trim>
        where workstation_material_id = #{workstationMaterialId}
    </update>

    <delete id="deleteProWorkstationMaterialByWorkstationMaterialId" parameterType="Long">
        delete from pro_workstation_material where workstation_material_id = #{workstationMaterialId}
    </delete>

    <delete id="deleteProWorkstationMaterialByWorkstationMaterialIds" parameterType="String">
        delete from pro_workstation_material where workstation_material_id in
        <foreach item="workstationMaterialId" collection="array" open="(" separator="," close=")">
            #{workstationMaterialId}
        </foreach>
    </delete>
</mapper>