<?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.wansenai.mappers.stock.BasStockAccountMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.wansenai.entities.stock.BasStockAccount">
        <id column="id" property="id"/>
        <result column="warehouse_id" property="warehouseId"/>
        <result column="warehouse_type_id" property="warehouseTypeId"/>
        <result column="stock_type" property="stockType"/>
        <result column="goods_id" property="goodsId"/>
        <result column="sku_id" property="skuId"/>
        <result column="qty_lock" property="qtyLock"/>
        <result column="qty_tran" property="qtyTran"/>
        <result column="qty" property="qty"/>
        <result column="qty_tran_out" property="qtyTranOut"/>
        <result column="qty_tran_in" property="qtyTranIn"/>
        <result column="create_time" property="createTime"/>
        <result column="create_by" property="createBy"/>
        <result column="update_time" property="updateTime"/>
        <result column="update_by" property="updateBy"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, warehouse_id, warehouse_type_id, stock_type, goods_id, sku_id,
        qty_lock, qty_tran, qty, qty_tran_out, qty_tran_in,
        create_time, create_by, update_time, update_by, remark
    </sql>

    <!-- 根据仓库ID和商品ID查询库存信息 -->
    <select id="getStockByWarehouseAndGoods" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM bas_stock_account
        WHERE warehouse_id = #{warehouseId} AND goods_id = #{goodsId}
    </select>

    <!-- 更新库存数量 -->
    <update id="updateStockQty">
        UPDATE bas_stock_account
        SET qty = #{qty},
            update_time = NOW()
        WHERE id = #{id}
    </update>

    <!-- 更新锁定数量 -->
    <update id="updateLockQty">
        UPDATE bas_stock_account
        SET qty_lock = #{qtyLock},
            update_time = NOW()
        WHERE id = #{id}
    </update>

    <!-- 更新在途数量 -->
    <update id="updateTranQty">
        UPDATE bas_stock_account
        SET qty_tran = #{qtyTran},
            update_time = NOW()
        WHERE id = #{id}
    </update>

    <!-- 批量插入库存信息 -->
    <insert id="batchInsert">
        INSERT INTO bas_stock_account (
            id, warehouse_id, warehouse_type_id, stock_type, goods_id, sku_id,
            qty_lock, qty_tran, qty, qty_tran_out, qty_tran_in,
            create_time, create_by, remark
        ) VALUES
        <foreach collection="stockList" item="stock" separator=",">
            (
                #{stock.id}, #{stock.warehouseId}, #{stock.warehouseTypeId},
                #{stock.stockType}, #{stock.goodsId}, #{stock.skuId},
                #{stock.qtyLock}, #{stock.qtyTran}, #{stock.qty},
                #{stock.qtyTranOut}, #{stock.qtyTranIn},
                #{stock.createTime}, #{stock.createBy}, #{stock.remark}
            )
        </foreach>
    </insert>

</mapper>
