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

    <resultMap type="WmIssueHeader" id="WmIssueHeaderResult">
        <result property="issueId" column="issue_id"/>
        <result property="issueCode" column="issue_code"/>
        <result property="workstationId" column="workstation_id"/>
        <result property="workstationCode" column="workstation_code"/>
        <result property="workstationName" column="workstation_name"/>
        <result property="workorderId" column="workorder_id"/>
        <result property="workorderCode" column="workorder_code"/>
        <result property="taskId" column="task_id"/>
        <result property="taskCode" column="task_code"/>
        <result property="clientId" column="client_id"/>
        <result property="clientCode" column="client_code"/>
        <result property="clientName" column="client_name"/>
        <result property="clientNick" column="client_nick"/>
        <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="areaName" column="area_name"/>
        <result property="issueDate" column="issue_date"/>
        <result property="status" column="status"/>
        <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="issueType" column="issue_type"/>
        <result property="applyNo" column="apply_no"/>
    </resultMap>

    <resultMap id="IssueTxBeanResult" type="IssueTxBean">
        <result property="materialStockId" column="material_stock_id"></result>
        <result property="itemId" column="item_id"></result>
        <result property="itemCode" column="item_code"></result>
        <result property="specification" column="specification"></result>
        <result property="unitOfMeasure" column="unit_of_measure"></result>
        <result property="batchCode" column="batch_code"></result>
        <result property="warehouseId" column="warehouse_id"></result>
        <result property="warehouseCode" column="warehouse_code"></result>
        <result property="warehouseName" column="warehouse_name"></result>
        <result property="locationId" column="location_id"></result>
        <result property="locationCode" column="location_code"></result>
        <result property="locationName" column="location_name"></result>
        <result property="areaId" column="area_id"></result>
        <result property="areaCode" column="area_code"></result>
        <result property="areaName" column="area_name"></result>
        <result property="vendorId" column="vendor_id"></result>
        <result property="vendorCode" column="vendor_code"></result>
        <result property="vendorName" column="vendor_name"></result>
        <result property="vendorNick" column="vendor_nick"></result>
        <result property="sourceDocType" column="source_doc_type"></result>
        <result property="sourceDocId" column="source_doc_id"></result>
        <result property="sourceDocCode" column="source_doc_code"></result>
        <result property="sourceDocLineId" column="source_doc_line_id"></result>
        <result property="transactionQuantity" column="transaction_quantity"></result>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectWmIssueHeaderVo">
        select wih.issue_id,
               wih.issue_code,
               wih.issue_type,
               wih.workstation_id,
               mw.workstation_code,
               mw.workstation_name,
               wih.task_id,
               pt.task_code,
               wih.client_id,
               mc.client_code,
               mc.client_name,
               mc.client_nick,
               wih.warehouse_id,
               wih.warehouse_code,
               wih.warehouse_name,
               wih.location_id,
               wih.location_code,
               wih.location_name,
               wih.area_id,
               wih.area_code,
               wih.area_name,
               wih.issue_date,
               wih.status,
               wih.remark,
               wih.attr1,
               wih.attr2,
               wih.attr3,
               wih.attr4,
               wih.create_by,
               wih.create_time,
               wih.update_by,
               wih.update_time
        from wm_issue_header wih
                 left join pro_task pt
                           on wih.task_id = pt.task_id
                 left join md_workstation mw
                           on wih.workstation_id = mw.workstation_id
                 left join md_client mc
                           on wih.client_id = mc.client_id

    </sql>

    <select id="selectWmIssueHeaderList" parameterType="WmIssueHeader" resultMap="WmIssueHeaderResult">
        <include refid="selectWmIssueHeaderVo"/>
        <where>
            <if test="issueCode != null  and issueCode != ''">and wih.issue_code = #{issueCode}</if>
            <if test="issueType != null  and issueType != ''">and wih.issue_type = #{issueType}</if>
            <if test="workstationId != null ">and wih.workstation_id = #{workstationId}</if>
            <if test="workstationCode != null  and workstationCode != ''">and mw.workstation_code = #{workstationCode}
            </if>
            <if test="workstationName != null  and workstationName != ''">and mw.workstation_name = #{workstationName}
            </if>
            <if test="taskId != null ">and wih.task_id = #{taskId}</if>
            <if test="taskCode != null  and taskCode != ''">and pt.task_code = #{taskCode}</if>
            <if test="clientId != null ">and wih.client_id = #{clientId}</if>
            <if test="clientCode != null  and clientCode != ''">and mc.client_code = #{clientCode}</if>
            <if test="clientName != null  and clientName != ''">and mc.client_name like concat('%', #{clientName}, '%')
            </if>
            <if test="clientNick != null  and clientNick != ''">and mc.client_nick = #{clientNick}</if>
            <if test="warehouseId != null ">and wih.warehouse_id = #{warehouseId}</if>
            <if test="warehouseCode != null  and warehouseCode != ''">and wih.warehouse_code = #{warehouseCode}</if>
            <if test="warehouseName != null  and warehouseName != ''">and wih.warehouse_name like concat('%',
                #{warehouseName}, '%')
            </if>
            <if test="locationId != null ">and wih.location_id = #{locationId}</if>
            <if test="locationCode != null  and locationCode != ''">and wih.location_code = #{locationCode}</if>
            <if test="locationName != null  and locationName != ''">and wih.location_name like concat('%',
                #{locationName},
                '%')
            </if>
            <if test="areaId != null ">and wih.area_id = #{areaId}</if>
            <if test="areaCode != null  and areaCode != ''">and wih.area_code = #{areaCode}</if>
            <if test="areaName != null  and areaName != ''">and wih.area_name like concat('%', #{areaName}, '%')</if>
            <if test="params.beginPurchaseDate != null and params.beginPurchaseDate != '' and params.endPurchaseDate != null and params.endPurchaseDate != ''">
                and wih.issue_date between #{params.beginPurchaseDate} and #{params.endPurchaseDate}
            </if>
            <if test="status != null  and status != ''">and wih.status = #{status}</if>
        </where>
        order by wih.create_time desc
    </select>

    <select id="selectWmIssueHeaderByIssueId" parameterType="Long" resultMap="WmIssueHeaderResult">
        <include refid="selectWmIssueHeaderVo"/>
        where wih.issue_id = #{issueId}
    </select>

    <select id="checkIssueCodeUnique" parameterType="WmIssueHeader" resultMap="WmIssueHeaderResult">
        <include refid="selectWmIssueHeaderVo"/>
        where wih.issue_code = #{issueCode} limit 1
    </select>

    <select id="getTxBeans" parameterType="Long" resultMap="IssueTxBeanResult">
        SELECT irl.material_stock_id,
               irl.`item_id`,
               irl.`item_code`,
               irl.`item_name`,
               irl.`specification`,
               irl.`unit_of_measure`,
               irl.`batch_code`,
               irl.`warehouse_id`,
               irl.`warehouse_code`,
               irl.`warehouse_name`,
               irl.`location_id`,
               irl.`location_code`,
               irl.`location_name`,
               irl.`area_id`,
               irl.`area_code`,
               irl.`area_name`,
               'ISSUE'               AS source_doc_type,
               ir.`issue_id`         AS source_doc_id,
               ir.`issue_code`       AS source_doc_code,
               irl.`line_id`         AS source_doc_line_id,
               irl.`quantity_issued` AS transaction_quantity,
               ir.workorder_id,
               ir.workorder_code,
               ir.`create_by`,
               ir.`create_time`,
               ir.`update_by`,
               ir.`update_time`
        FROM wm_issue_header ir
                 LEFT JOIN wm_issue_line irl
                           ON ir.issue_id = irl.`issue_id`
        WHERE ir.`issue_id` = #{issueId}
    </select>

    <insert id="insertWmIssueHeader" parameterType="WmIssueHeader" useGeneratedKeys="true" keyProperty="issueId">
        insert into wm_issue_header
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="issueCode != null and issueCode != ''">issue_code,</if>
            <if test="issueType != null">issue_type,</if>
            <if test="workstationId != null">workstation_id,</if>
            <if test="workstationCode != null">workstation_code,</if>
            <if test="workstationName != null">workstation_name,</if>
            <if test="taskId != null">task_id,</if>
            <if test="taskCode != null">task_code,</if>
            <if test="clientId != null">client_id,</if>
            <if test="clientCode != null">client_code,</if>
            <if test="clientName != null">client_name,</if>
            <if test="clientNick != null">client_nick,</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="issueDate != null">issue_date,</if>
            <if test="status != null">status,</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="issueCode != null and issueCode != ''">#{issueCode},</if>
            <if test="issueType != null">#{issueType},</if>
            <if test="workstationId != null">#{workstationId},</if>
            <if test="workstationCode != null">#{workstationCode},</if>
            <if test="workstationName != null">#{workstationName},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="taskCode != null">#{taskCode},</if>
            <if test="clientId != null">#{clientId},</if>
            <if test="clientCode != null">#{clientCode},</if>
            <if test="clientName != null">#{clientName},</if>
            <if test="clientNick != null">#{clientNick},</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="issueDate != null">#{issueDate},</if>
            <if test="status != null">#{status},</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="updateWmIssueHeader" parameterType="WmIssueHeader">
        update wm_issue_header
        <trim prefix="SET" suffixOverrides=",">
            <if test="issueCode != null and issueCode != ''">issue_code = #{issueCode},</if>
            <if test="issueType != null  and issueType != ''">issue_type = #{issueType},</if>
            <if test="workstationId != null">workstation_id = #{workstationId},</if>
            <if test="workstationCode != null">workstation_code = #{workstationCode},</if>
            <if test="workstationName != null  and workstationName != ''">workstation_name = #{workstationName},</if>
            <if test="taskId != null">task_id = #{taskId},</if>
            <if test="taskCode != null">task_code = #{taskCode},</if>
            <if test="clientId != null">client_id = #{clientId},</if>
            <if test="clientCode != null">client_code = #{clientCode},</if>
            <if test="clientName != null">client_name = #{clientName},</if>
            <if test="clientNick != null">client_nick = #{clientNick},</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="areaName != null">area_name = #{areaName},</if>
            <if test="issueDate != null">issue_date = #{issueDate},</if>
            <if test="status != null">status = #{status},</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>
        </trim>
        where issue_id = #{issueId}
    </update>

    <delete id="deleteWmIssueHeaderByIssueId" parameterType="Long">
        delete
        from wm_issue_header
        where issue_id = #{issueId}
    </delete>
    <delete id="deleteWmIssueHeaderByIssueCode">
        delete
        from wm_issue_line
        where issue_id = (select issue_id
                          from wm_issue_header
                          where issue_code = #{issueCode})

    </delete>

    <delete id="deleteWmIssueHeaderByIssueIds" parameterType="String">
        delete from wm_issue_header where issue_id in
        <foreach item="issueId" collection="array" open="(" separator="," close=")">
            #{issueId}
        </foreach>
    </delete>
</mapper>
