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

    <resultMap id="AdjustResult" type="com.ruoyi.web.stock.domain.WarehouseStockAdjust">
        <id property="id" column="id"/>
        <result property="adjustDate" column="adjust_date"/>
        <result property="sheetNo" column="sheet_no"/>
        <result property="adjustUser" column="adjust_user"/>
        <result property="totalAmount" column="total_amount"/>
        <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="AdjustGoodsResult" type="com.ruoyi.web.stock.domain.WarehouseStockAdjustGoods">
        <id property="id" column="id"/>
        <result property="adjustId" column="adjust_id"/>
        <result property="sheetNo" column="sheet_no"/>
        <result property="inventoryId" column="inventory_id"/>
        <result property="goodsNum" column="goods_num"/>
        <result property="goodsName" column="goods_name"/>
        <result property="batchNo" column="batch_no"/>
        <result property="warehouseNo" column="warehouse_no"/>
        <result property="warehouseName" column="warehouse_name"/>
        <result property="baseUnit" column="base_unit"/>
        <result property="assistantUnit" column="assistant_unit"/>
        <result property="adjBaseQty" column="adj_base_qty"/>
        <result property="adjAssistQty" column="adj_assist_qty"/>
        <result property="unitCost" column="unit_cost"/>
        <result property="lineAmount" column="line_amount"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
        <result property="orgId" column="org_id"/>
    </resultMap>

    <sql id="adjustSelect">
        select id, adjust_date, sheet_no, adjust_user, remark, total_amount, del_flag, org_id,
               create_by, create_time, update_by, update_time
        from warehouse_stock_adjust
    </sql>

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

    <select id="selectList" parameterType="com.ruoyi.web.stock.domain.WarehouseStockAdjust" resultMap="AdjustResult">
        <include refid="adjustSelect"/>
        <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="adjustUser != null and adjustUser != ''">
                and adjust_user like concat('%', #{adjustUser}, '%')
            </if>
            <if test="adjustDate != null">
                and adjust_date = #{adjustDate}
            </if>
        </where>
        order by id desc
    </select>

    <insert id="insert" parameterType="com.ruoyi.web.stock.domain.WarehouseStockAdjust" useGeneratedKeys="true" keyProperty="id">
        insert into warehouse_stock_adjust (
            adjust_date, sheet_no, adjust_user, remark, total_amount, del_flag, org_id,
            create_by, create_time, update_by, update_time
        ) values (
            #{adjustDate}, #{sheetNo}, #{adjustUser}, #{remark}, #{totalAmount}, #{delFlag}, #{orgId},
            #{createBy}, now(), #{updateBy}, now()
        )
    </insert>

    <update id="updateTotalAmount">
        update warehouse_stock_adjust
        set total_amount = #{totalAmount},
            update_by = #{updateBy},
            update_time = now()
        where id = #{id} and del_flag = '0'
    </update>

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

    <select id="selectGoodsByAdjustId" parameterType="Long" resultMap="AdjustGoodsResult">
        select id, adjust_id, sheet_no, inventory_id, goods_num, goods_name, batch_no,
               warehouse_no, warehouse_name, base_unit, assistant_unit,
               adj_base_qty, adj_assist_qty, unit_cost, line_amount, remark, del_flag, org_id
        from warehouse_stock_adjust_goods
        where adjust_id = #{adjustId} and del_flag = '0'
        order by id asc
    </select>

    <insert id="insertGoodsBatch">
        insert into warehouse_stock_adjust_goods (
            adjust_id, sheet_no, inventory_id, goods_num, goods_name, batch_no,
            warehouse_no, warehouse_name, base_unit, assistant_unit,
            adj_base_qty, adj_assist_qty, unit_cost, line_amount, remark, del_flag, org_id,
            create_by, create_time, update_by, update_time
        ) values
        <foreach collection="list" item="item" separator=",">
            (
                #{item.adjustId}, #{item.sheetNo}, #{item.inventoryId}, #{item.goodsNum}, #{item.goodsName}, #{item.batchNo},
                #{item.warehouseNo}, #{item.warehouseName}, #{item.baseUnit}, #{item.assistantUnit},
                #{item.adjBaseQty}, #{item.adjAssistQty}, #{item.unitCost}, #{item.lineAmount}, #{item.remark}, #{item.delFlag}, #{item.orgId},
                #{item.createBy}, now(), #{item.updateBy}, now()
            )
        </foreach>
    </insert>

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

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