<?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.wm.mapper.WmIssueLineMapper">

    <resultMap type="WmIssueLine" id="WmIssueLineResult">
        <result property="lineId" column="line_id"/>
        <result property="issueId" column="issue_id"/>
        <result property="materialStockId" column="material_stock_id"/>
        <result property="itemId" column="item_id"/>
        <result property="itemCode" column="item_code"/>
        <result property="itemName" column="item_name"/>
        <result property="specification" column="specification"/>
        <result property="unitOfMeasure" column="unit_of_measure"/>
        <result property="quantityIssued" column="quantity_issued"/>
        <result property="batchCode" column="batch_code"/>
        <result property="warehouseId" column="warehouse_id"/>
        <result property="warehouseCode" column="warehouse_code"/>
        <result property="warehouseName" column="warehouse_name"/>
        <result property="locationId" column="location_id"/>
        <result property="locationCode" column="location_code"/>
        <result property="locationName" column="location_name"/>
        <result property="areaId" column="area_id"/>
        <result property="areaCode" column="area_code"/>
        <result property="plNum" column="pl_num"/>
        <result property="areaName" column="area_name"/>
        <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="applyNo" column="apply_no"/>
        <result property="workorderCode" column="workorder_code"/>
        <result property="packNo" column="pack_no"/>
    </resultMap>

    <sql id="selectWmIssueLineVo">
        select wil.line_id,
               wil.issue_id,
               wil.material_stock_id,
               wil.item_id,
               wil.item_code,
               wil.pl_num,
               wil.apply_no,
               wil.workorder_code,
               wil.item_name,
               wil.specification,
               wil.unit_of_measure,
               wil.quantity_issued,
               wil.batch_code,
               wil.warehouse_id,
               wil.warehouse_code,
               wil.warehouse_name,
               wil.location_id,
               wil.location_code,
               wil.location_name,
               wil.area_id,
               wil.area_code,
               wil.area_name,
               wil.remark,
               wil.attr1,
               wil.attr2,
               wil.attr3,
               wil.attr4,
               wil.create_by,
               wil.create_time,
               wil.update_by,
               wil.update_time
        from wm_issue_line wil
                 left join md_item mi on mi.item_id = wil.item_id

    </sql>

    <select id="selectWmIssueLineList" parameterType="WmIssueLine" resultMap="WmIssueLineResult">
        <include refid="selectWmIssueLineVo"/>
        <where>
            <if test="issueId != null ">and wil.issue_id = #{issueId}</if>
            <if test="materialStockId != null">and wil.material_stock_id = #{materialStockId}</if>
            <if test="itemId != null ">and wil.item_id = #{itemId}</if>
            <if test="itemCode != null  and itemCode != ''">and mi.item_code = #{itemCode}</if>
            <if test="itemName != null  and itemName != ''">and mi.item_name like concat('%', #{itemName}, '%')</if>
            <if test="specification != null  and specification != ''">and wil.specification = #{specification}</if>
            <if test="unitOfMeasure != null  and unitOfMeasure != ''">and wil.unit_of_measure = #{unitOfMeasure}</if>
            <if test="quantityIssued != null ">and wil.quantity_issued = #{quantityIssued}</if>
            <if test="batchCode != null  and batchCode != ''">and wil.batch_code = #{batchCode}</if>
            <if test="warehouseId != null ">and wil.warehouse_id = #{warehouseId}</if>
            <if test="warehouseCode != null  and warehouseCode != ''">and wil.warehouse_code = #{warehouseCode}</if>
            <if test="warehouseName != null  and warehouseName != ''">and wil.warehouse_name like concat('%',
                #{warehouseName}, '%')
            </if>
            <if test="locationId != null ">and wil.location_id = #{locationId}</if>
            <if test="locationCode != null  and locationCode != ''">and wil.location_code = #{locationCode}</if>
            <if test="locationName != null  and locationName != ''">and wil.location_name like concat('%',
                #{locationName},
                '%')
            </if>
            <if test="areaId != null ">and wil.area_id = #{areaId}</if>
            <if test="areaCode != null  and areaCode != ''">and wil.area_code = #{areaCode}</if>
            <if test="areaName != null  and areaName != ''">and wil.area_name like concat('%', #{areaName}, '%')</if>
        </where>
        order by wil.create_time desc
    </select>


    <select id="selectWmIssueLineByIssueIdsList" parameterType="WmIssueLine" resultMap="WmIssueLineResult">
        SELECT
        l.item_id,
        l.item_code,
        l.item_name,
        l.specification,
        l.unit_of_measure,
        l.warehouse_id,
        l.warehouse_code,
        l.warehouse_name,
        l.location_id,
        l.location_code,
        l.location_name,
        l.area_id,
        l.area_code,
        l.area_name,
        sum( l.quantity_issued ) quantity_issued
        FROM
        wm_issue_header t
        LEFT JOIN wm_issue_line l ON t.issue_Id = l.issue_id and l.item_code is not null
        <where>
            <if test="workorderCodes != null">
                and t.workorder_code in
                <foreach item="code" collection="workorderCodes" open="(" separator="," close=")">
                    #{code}
                </foreach>
            </if>
        </where>
        GROUP BY
        l.item_id,
        l.item_code,
        l.item_name,
        l.specification,
        l.unit_of_measure,
        l.warehouse_id,
        l.warehouse_code,
        l.warehouse_name,
        l.location_id,
        l.location_code,
        l.location_name,
        l.area_id,
        l.area_code,
        l.area_name
    </select>


    <select id="selectWmIssueLineByLineId" parameterType="Long" resultMap="WmIssueLineResult">
        <include refid="selectWmIssueLineVo"/>
        where wil.line_id = #{lineId}
    </select>

    <insert id="insertWmIssueLine" parameterType="WmIssueLine" useGeneratedKeys="true" keyProperty="lineId">
        insert into wm_issue_line
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="issueId != null">issue_id,</if>
            <if test="materialStockId != null">material_stock_id,</if>
            <if test="itemId != null">item_id,</if>
            <if test="itemCode != null">item_code,</if>
            <if test="itemName != null">item_name,</if>
            <if test="specification != null">specification,</if>
            <if test="applyNo != null">apply_no,</if>
            <if test="workorderCode != null">workorder_code,</if>
            <if test="unitOfMeasure != null">unit_of_measure,</if>
            <if test="quantityIssued != null">quantity_issued,</if>
            <if test="batchCode != null">batch_code,</if>
            <if test="packNo != null">pack_no,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="warehouseCode != null">warehouse_code,</if>
            <if test="warehouseName != null">warehouse_name,</if>
            <if test="locationId != null">location_id,</if>
            <if test="locationCode != null">location_code,</if>
            <if test="locationName != null">location_name,</if>
            <if test="areaId != null">area_id,</if>
            <if test="areaCode != null">area_code,</if>
            <if test="areaName != null">area_name,</if>
            <if test="plNum != null">pl_num,</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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="issueId != null">#{issueId},</if>
            <if test="materialStockId != null">#{materialStockId},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="itemCode != null">#{itemCode},</if>
            <if test="itemName != null">#{itemName},</if>
            <if test="specification != null">#{specification},</if>
            <if test="applyNo != null">#{applyNo},</if>
            <if test="workorderCode != null">#{workorderCode},</if>
            <if test="unitOfMeasure != null">#{unitOfMeasure},</if>
            <if test="quantityIssued != null">#{quantityIssued},</if>
            <if test="batchCode != null">#{batchCode},</if>
            <if test="packNo != null">#{packNo},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="warehouseCode != null">#{warehouseCode},</if>
            <if test="warehouseName != null">#{warehouseName},</if>
            <if test="locationId != null">#{locationId},</if>
            <if test="locationCode != null">#{locationCode},</if>
            <if test="locationName != null">#{locationName},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="areaCode != null">#{areaCode},</if>
            <if test="areaName != null">#{areaName},</if>
            <if test="plNum != null">#{plNum},</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>
        </trim>
    </insert>

    <update id="updateWmIssueLine" parameterType="WmIssueLine">
        update wm_issue_line
        <trim prefix="SET" suffixOverrides=",">
            <if test="issueId != null">issue_id = #{issueId},</if>
            <if test="materialStockId != null">material_stock_id = #{materialStockId},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="itemCode != null">item_code = #{itemCode},</if>
            <if test="itemName != null">item_name = #{itemName},</if>
            <if test="specification != null">specification = #{specification},</if>
            <if test="unitOfMeasure != null">unit_of_measure = #{unitOfMeasure},</if>
            <if test="quantityIssued != null">quantity_issued = #{quantityIssued},</if>
            <if test="batchCode != null">batch_code = #{batchCode},</if>
            <if test="batchCode != null">batch_code = #{batchCode},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="warehouseCode != null">warehouse_code = #{warehouseCode},</if>
            <if test="warehouseName != null">warehouse_name = #{warehouseName},</if>
            <if test="locationId != null">location_id = #{locationId},</if>
            <if test="locationCode != null">location_code = #{locationCode},</if>
            <if test="locationName != null">location_name = #{locationName},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="areaCode != null">area_code = #{areaCode},</if>
            <if test="plNum != null">pl_num = #{plNum},</if>
            <if test="areaName != null">area_name = #{areaName},</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="applyNo != null">apply_no = #{applyNo},</if>
            <if test="workorderCode != null">workorder_code = #{workorderCode},</if>
        </trim>
        where line_id = #{lineId}
    </update>

    <delete id="deleteWmIssueLineByLineId" parameterType="Long">
        delete
        from wm_issue_line
        where line_id = #{lineId}
    </delete>

    <delete id="deleteWmIssueLineByLineIds" parameterType="String">
        delete from wm_issue_line where line_id in
        <foreach item="lineId" collection="array" open="(" separator="," close=")">
            #{lineId}
        </foreach>
    </delete>

    <delete id="deleteByIssueHeaderId" parameterType="Long">
        delete
        from wm_issue_line
        where issue_id = #{issueId}
    </delete>

</mapper>
