<?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.ruoyi.web.stock.mapper.WarehouseStockIssueMapper">
    <resultMap id="IssueResult" type="com.ruoyi.web.stock.domain.WarehouseStockIssue">
        <id property="id" column="id"/>
        <result property="sheetDate" column="sheet_date"/>
        <result property="sheetNo" column="sheet_no"/>
        <result property="productionType" column="production_type"/>
        <result property="productionWorkshop" column="production_workshop"/>
        <result property="issueUser" column="issue_user"/>
        <result property="totalSubtotal" column="total_subtotal"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
        <result property="orgId" column="org_id"/>
    </resultMap>
    <resultMap id="IssueGoodsResult" type="com.ruoyi.web.stock.domain.WarehouseStockIssueGoods">
        <id property="id" column="id"/>
        <result property="issueId" column="issue_id"/>
        <result property="sheetNo" column="sheet_no"/>
        <result property="sourceInventoryId" column="source_inventory_id"/>
        <result property="goodsNum" column="goods_num"/>
        <result property="goodsName" column="goods_name"/>
        <result property="goodsSpec" column="goods_spec"/>
        <result property="batchNo" column="batch_no"/>
        <result property="outWarehouseNo" column="out_warehouse_no"/>
        <result property="outWarehouseName" column="out_warehouse_name"/>
        <result property="baseUnit" column="base_unit"/>
        <result property="baseQty" column="base_qty"/>
        <result property="assistantUnit" column="assistant_unit"/>
        <result property="assistantQty" column="assistant_qty"/>
        <result property="processingFee" column="processing_fee"/>
        <result property="subtotal" column="subtotal"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
        <result property="orgId" column="org_id"/>
    </resultMap>
    <sql id="issueSelect">
        select id, sheet_date, sheet_no, production_type, production_workshop, issue_user, remark,
               total_subtotal, del_flag, org_id, create_by, create_time, update_by, update_time
        from warehouse_stock_issue
    </sql>
    <select id="selectById" parameterType="Long" resultMap="IssueResult">
        <include refid="issueSelect"/>
        where id = #{id} and del_flag = '0'
    </select>
    <select id="selectList" parameterType="com.ruoyi.web.stock.domain.WarehouseStockIssue" resultMap="IssueResult">
        <include refid="issueSelect"/>
        <where>
            del_flag = '0'
            <if test="orgId != null and orgId != ''">and org_id = #{orgId}</if>
            <if test="sheetNo != null and sheetNo != ''">and sheet_no like concat('%', #{sheetNo}, '%')</if>
            <if test="productionType != null and productionType != ''">and production_type like concat('%', #{productionType}, '%')</if>
            <if test="sheetDate != null">and sheet_date = #{sheetDate}</if>
        </where>
        order by id desc
    </select>
    <insert id="insert" parameterType="com.ruoyi.web.stock.domain.WarehouseStockIssue" useGeneratedKeys="true" keyProperty="id">
        insert into warehouse_stock_issue (
            sheet_date, sheet_no, production_type, production_workshop, issue_user, remark,
            total_subtotal, del_flag, org_id, create_by, create_time, update_by, update_time
        ) values (
            #{sheetDate}, #{sheetNo}, #{productionType}, #{productionWorkshop}, #{issueUser}, #{remark},
            #{totalSubtotal}, #{delFlag}, #{orgId}, #{createBy}, now(), #{updateBy}, now()
        )
    </insert>
    <update id="updateTotalSubtotal">
        update warehouse_stock_issue
        set total_subtotal = #{totalSubtotal}, update_by = #{updateBy}, update_time = now()
        where id = #{id} and del_flag = '0'
    </update>
    <update id="deleteById">
        update warehouse_stock_issue
        set del_flag = '2', update_by = #{updateBy}, update_time = now()
        where id = #{id} and del_flag = '0'
    </update>
    <select id="selectGoodsByIssueId" parameterType="Long" resultMap="IssueGoodsResult">
        select id, issue_id, sheet_no, source_inventory_id, goods_num, goods_name, goods_spec, batch_no,
               out_warehouse_no, out_warehouse_name,
               base_unit, base_qty, assistant_unit, assistant_qty, processing_fee, subtotal, remark, del_flag, org_id
        from warehouse_stock_issue_goods
        where issue_id = #{issueId} and del_flag = '0'
        order by id asc
    </select>
    <insert id="insertGoodsBatch">
        insert into warehouse_stock_issue_goods (
            issue_id, sheet_no, source_inventory_id, goods_num, goods_name, goods_spec, batch_no,
            out_warehouse_no, out_warehouse_name,
            base_unit, base_qty, assistant_unit, assistant_qty, processing_fee, subtotal, remark,
            del_flag, org_id, create_by, create_time, update_by, update_time
        ) values
        <foreach collection="list" item="item" separator=",">
            (
                #{item.issueId}, #{item.sheetNo}, #{item.sourceInventoryId}, #{item.goodsNum}, #{item.goodsName}, #{item.goodsSpec}, #{item.batchNo},
                #{item.outWarehouseNo}, #{item.outWarehouseName},
                #{item.baseUnit}, #{item.baseQty}, #{item.assistantUnit}, #{item.assistantQty}, #{item.processingFee}, #{item.subtotal}, #{item.remark},
                #{item.delFlag}, #{item.orgId}, #{item.createBy}, now(), #{item.updateBy}, now()
            )
        </foreach>
    </insert>
    <update id="deleteGoodsByIssueId">
        update warehouse_stock_issue_goods
        set del_flag = '2', update_by = #{updateBy}, update_time = now()
        where issue_id = #{issueId} and del_flag = '0'
    </update>
    <select id="selectTodayMaxSeq" resultType="java.lang.Integer">
        select max(cast(right(sheet_no, 4) as unsigned))
        from warehouse_stock_issue
        where org_id = #{orgId}
          and date_format(sheet_date, '%Y%m%d') = #{dateText}
          and del_flag = '0'
    </select>
</mapper>
