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

    <resultMap id="TransferResult" type="com.ruoyi.web.stock.domain.WarehouseStockTransfer">
        <id property="id" column="id"/>
        <result property="transferDate" column="transfer_date"/>
        <result property="sheetNo" column="sheet_no"/>
        <result property="transferUser" column="transfer_user"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
        <result property="orgId" column="org_id"/>
        <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>

    <resultMap id="TransferGoodsResult" type="com.ruoyi.web.stock.domain.WarehouseStockTransferGoods">
        <id property="id" column="id"/>
        <result property="transferId" column="transfer_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="inWarehouseNo" column="in_warehouse_no"/>
        <result property="inWarehouseName" column="in_warehouse_name"/>
        <result property="baseUnit" column="base_unit"/>
        <result property="assistantUnit" column="assistant_unit"/>
        <result property="baseQty" column="base_qty"/>
        <result property="assistQty" column="assist_qty"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
        <result property="orgId" column="org_id"/>
    </resultMap>

    <sql id="transferSelect">
        select id, transfer_date, sheet_no, transfer_user, remark, del_flag, org_id,
               create_by, create_time, update_by, update_time
        from warehouse_stock_transfer
    </sql>

    <select id="selectById" parameterType="Long" resultMap="TransferResult">
        <include refid="transferSelect"/>
        where id = #{id} and del_flag = '0'
    </select>

    <select id="selectList" parameterType="com.ruoyi.web.stock.domain.WarehouseStockTransfer" resultMap="TransferResult">
        <include refid="transferSelect"/>
        <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="transferUser != null and transferUser != ''">
                and transfer_user like concat('%', #{transferUser}, '%')
            </if>
            <if test="transferDate != null">
                and transfer_date = #{transferDate}
            </if>
        </where>
        order by id desc
    </select>

    <insert id="insert" parameterType="com.ruoyi.web.stock.domain.WarehouseStockTransfer" useGeneratedKeys="true" keyProperty="id">
        insert into warehouse_stock_transfer (
            transfer_date, sheet_no, transfer_user, remark, del_flag, org_id,
            create_by, create_time, update_by, update_time
        ) values (
            #{transferDate}, #{sheetNo}, #{transferUser}, #{remark}, #{delFlag}, #{orgId},
            #{createBy}, now(), #{updateBy}, now()
        )
    </insert>

    <update id="deleteById">
        update warehouse_stock_transfer
        set del_flag = '2',
            update_by = #{updateBy},
            update_time = now()
        where id = #{id} and del_flag = '0'
    </update>

    <select id="selectGoodsByTransferId" parameterType="Long" resultMap="TransferGoodsResult">
        select id, transfer_id, sheet_no, source_inventory_id, goods_num, goods_name, goods_spec, batch_no,
               out_warehouse_no, out_warehouse_name, in_warehouse_no, in_warehouse_name,
               base_unit, assistant_unit, base_qty, assist_qty, remark, del_flag, org_id
        from warehouse_stock_transfer_goods
        where transfer_id = #{transferId} and del_flag = '0'
        order by id asc
    </select>

    <insert id="insertGoodsBatch">
        insert into warehouse_stock_transfer_goods (
            transfer_id, sheet_no, source_inventory_id, goods_num, goods_name, goods_spec, batch_no,
            out_warehouse_no, out_warehouse_name, in_warehouse_no, in_warehouse_name,
            base_unit, assistant_unit, base_qty, assist_qty, remark, del_flag, org_id,
            create_by, create_time, update_by, update_time
        ) values
        <foreach collection="list" item="item" separator=",">
            (
                #{item.transferId}, #{item.sheetNo}, #{item.sourceInventoryId}, #{item.goodsNum}, #{item.goodsName}, #{item.goodsSpec}, #{item.batchNo},
                #{item.outWarehouseNo}, #{item.outWarehouseName}, #{item.inWarehouseNo}, #{item.inWarehouseName},
                #{item.baseUnit}, #{item.assistantUnit}, #{item.baseQty}, #{item.assistQty}, #{item.remark}, #{item.delFlag}, #{item.orgId},
                #{item.createBy}, now(), #{item.updateBy}, now()
            )
        </foreach>
    </insert>

    <update id="deleteGoodsByTransferId">
        update warehouse_stock_transfer_goods
        set del_flag = '2',
            update_by = #{updateBy},
            update_time = now()
        where transfer_id = #{transferId} and del_flag = '0'
    </update>

    <select id="selectTodayMaxSeq" resultType="java.lang.Integer">
        select max(cast(right(sheet_no, 4) as unsigned))
        from warehouse_stock_transfer
        where org_id = #{orgId}
          and date_format(transfer_date, '%Y%m%d') = #{dateText}
          and del_flag = '0'
    </select>
</mapper>
