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

    <resultMap id="WarehouseInResult" type="com.ruoyi.web.stock.domain.WarehouseIn">
        <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="productionPlantNo" column="production_plant_no"/>
        <result property="productionPlantName" column="production_plant_name"/>
        <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="WarehouseInGoodsResult" type="com.ruoyi.web.stock.domain.WarehouseInGoods">
        <id property="id" column="id"/>
        <result property="inId" column="in_id"/>
        <result property="sheetNo" column="sheet_no"/>
        <result property="goodsNum" column="goods_num"/>
        <result property="goodsName" column="goods_name"/>
        <result property="goodsSpec" column="goods_spec"/>
        <result property="goodsType" column="goods_type"/>
        <result property="goodsLevel" column="goods_level"/>
        <result property="warehouseNo" column="warehouse_no"/>
        <result property="warehouseName" column="warehouse_name"/>
        <result property="batchNo" column="batch_no"/>
        <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="amount" column="amount"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
        <result property="orgId" column="org_id"/>
    </resultMap>

    <resultMap id="MaterialOptionResult" type="com.ruoyi.web.stock.domain.vo.MaterialOptionVo">
        <result property="goodsNum" column="goods_num"/>
        <result property="goodsName" column="goods_name"/>
        <result property="goodsSpec" column="goods_spec"/>
        <result property="goodsType" column="goods_type"/>
        <result property="goodsLevel" column="goods_level"/>
        <result property="warehouseNo" column="warehouse_no"/>
        <result property="warehouseName" column="warehouse_name"/>
        <result property="baseUnit" column="base_unit"/>
        <result property="assistantUnit" column="assistant_unit"/>
    </resultMap>

    <sql id="warehouseInSelect">
        select id, sheet_date, sheet_no, production_type, production_plant_no, production_plant_name,
               remark, total_amount, del_flag, create_by, create_time, update_by, update_time, org_id
        from warehouse_in
    </sql>

    <sql id="warehouseInGoodsSelect">
        select id, in_id, sheet_no, goods_num, goods_name, goods_spec, goods_type, goods_level,
               warehouse_no, warehouse_name, batch_no, base_unit, base_qty, assistant_unit, assistant_qty,
               amount, remark, del_flag, org_id
        from warehouse_in_goods
    </sql>

    <select id="selectWarehouseInById" parameterType="Long" resultMap="WarehouseInResult">
        <include refid="warehouseInSelect"/>
        where id = #{id}
          and del_flag = '0'
    </select>

    <select id="selectWarehouseInList" parameterType="com.ruoyi.web.stock.domain.WarehouseIn" resultMap="WarehouseInResult">
        <include refid="warehouseInSelect"/>
        <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="insertWarehouseIn" parameterType="com.ruoyi.web.stock.domain.WarehouseIn" useGeneratedKeys="true" keyProperty="id">
        insert into warehouse_in (
            sheet_date, sheet_no, production_type, production_plant_no, production_plant_name,
            remark, total_amount, del_flag, create_by, create_time, update_by, update_time, org_id
        ) values (
            #{sheetDate}, #{sheetNo}, #{productionType}, #{productionPlantNo}, #{productionPlantName},
            #{remark}, #{totalAmount}, #{delFlag}, #{createBy}, now(), #{updateBy}, now(), #{orgId}
        )
    </insert>

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

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

    <select id="selectWarehouseInGoodsByInId" parameterType="Long" resultMap="WarehouseInGoodsResult">
        <include refid="warehouseInGoodsSelect"/>
        where in_id = #{inId}
          and del_flag = '0'
        order by id asc
    </select>

    <insert id="insertWarehouseInGoodsBatch">
        insert into warehouse_in_goods (
            in_id, sheet_no, goods_num, goods_name, goods_spec, goods_type, goods_level,
            warehouse_no, warehouse_name, batch_no, base_unit, base_qty, assistant_unit, assistant_qty,
            amount, remark, del_flag, create_by, create_time, update_by, update_time, org_id
        ) values
        <foreach collection="list" item="item" separator=",">
            (
                #{item.inId}, #{item.sheetNo}, #{item.goodsNum}, #{item.goodsName}, #{item.goodsSpec}, #{item.goodsType}, #{item.goodsLevel},
                #{item.warehouseNo}, #{item.warehouseName}, #{item.batchNo}, #{item.baseUnit}, #{item.baseQty}, #{item.assistantUnit}, #{item.assistantQty},
                #{item.amount}, #{item.remark}, #{item.delFlag}, #{item.createBy}, now(), #{item.updateBy}, now(), #{item.orgId}
            )
        </foreach>
    </insert>

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

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

    <select id="selectMaterialDetailOptions" resultMap="MaterialOptionResult">
        select goods_num,
               goods_name,
               goods_spec,
               ifnull(goods_type_name, goods_type) as goods_type,
               goods_level,
               warehouse_no,
               warehouse_name,
               unit as base_unit,
               assistant_unit
        from base_material
        where del_flag = '0'
          and org_id = #{orgId}
        order by goods_num asc
    </select>
</mapper>
