<?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.asset.mapper.AmsInboundOrderMapper">

    <resultMap type="AmsInboundOrder" id="AmsInboundOrderResult">
        <result property="orderId" column="order_id" />
        <result property="inboundNo" column="inbound_no" />
        <result property="warehouseId" column="warehouse_id" />
        <result property="warehouseCode" column="warehouse_code" />
        <result property="warehouseName" column="warehouse_name" />
        <result property="inboundUserId" column="inbound_user_id" />
        <result property="inboundUserName" column="inbound_user_name" />
        <result property="inboundTime" column="inbound_time" />
        <result property="orderStatus" column="order_status" />
        <result property="createBy" column="create_by" />
        <result property="createTime" column="create_time" />
        <result property="updateBy" column="update_by" />
        <result property="updateTime" column="update_time" />
        <result property="remark" column="remark" />
        <result property="delFlag" column="del_flag" />
    </resultMap>

    <resultMap id="AmsInboundOrderItemCollectionResult" type="AmsInboundOrder" extends="AmsInboundOrderResult">
        <collection property="amsInboundOrderItemList" ofType="AmsInboundOrderItem"
                    column="order_id" select="selectAmsInboundOrderItemList" />
    </resultMap>

    <resultMap type="AmsInboundOrderItem" id="AmsInboundOrderItemResult">
        <result property="itemId" column="item_id" />
        <result property="orderId" column="order_id" />
        <result property="inboundNo" column="inbound_no" />
        <result property="assetId" column="asset_id" />
        <result property="assetCode" column="asset_code" />
        <result property="assetName" column="asset_name" />
        <result property="categoryId" column="category_id" />
        <result property="categoryCode" column="category_code" />
        <result property="categoryName" column="category_name" />
        <result property="specModel" column="spec_model" />
        <result property="brand" column="brand" />
        <result property="locationId" column="location_id" />
        <result property="locationCode" column="location_code" />
        <result property="locationName" column="location_name" />
        <result property="inboundQuantity" column="inbound_quantity" />
        <result property="createBy" column="create_by" />
        <result property="createTime" column="create_time" />
        <result property="updateBy" column="update_by" />
        <result property="updateTime" column="update_time" />
        <result property="remark" column="remark" />
        <result property="delFlag" column="del_flag" />
    </resultMap>

    <!-- 入库单明细弹窗选择器使用的精简结果映射，只返回资产基本信息 -->
    <resultMap type="AmsAsset" id="AvailableInboundAssetResult">
        <result property="assetId" column="asset_id" />
        <result property="assetCode" column="asset_code" />
        <result property="assetName" column="asset_name" />
        <result property="categoryId" column="category_id" />
        <result property="categoryCode" column="category_code" />
        <result property="categoryName" column="category_name" />
        <result property="specModel" column="spec_model" />
        <result property="brand" column="brand" />
        <result property="assetStatus" column="asset_status" />
        <result property="tagCode" column="tag_code" />
    </resultMap>

    <sql id="selectAmsInboundOrderVo">
        select order_id, inbound_no, warehouse_id, warehouse_code, warehouse_name,
               inbound_user_id, inbound_user_name, inbound_time, order_status,
               create_by, create_time, update_by, update_time, remark, del_flag
        from ams_inbound_order
    </sql>

    <select id="selectAmsInboundOrderList" parameterType="AmsInboundOrder" resultMap="AmsInboundOrderResult">
        <include refid="selectAmsInboundOrderVo"/>
        <where>
            del_flag = '0'
            <if test="inboundNo != null and inboundNo != ''">and inbound_no = #{inboundNo}</if>
            <if test="warehouseId != null">and warehouse_id = #{warehouseId}</if>
            <if test="inboundUserName != null and inboundUserName != ''">
                and inbound_user_name like concat('%', #{inboundUserName}, '%')
            </if>
            <if test="params.beginInboundTime != null and params.beginInboundTime != ''">
                and inbound_time &gt;= #{params.beginInboundTime}
            </if>
            <if test="params.endInboundTime != null and params.endInboundTime != ''">
                and inbound_time &lt; date_add(#{params.endInboundTime}, interval 1 day)
            </if>
            <if test="orderStatus != null and orderStatus != ''">and order_status = #{orderStatus}</if>
            <if test="assetCode != null and assetCode != ''">
                and exists (
                    select 1 from ams_inbound_order_item item
                    where item.order_id = ams_inbound_order.order_id
                      and item.del_flag = '0'
                      and item.asset_code like concat(#{assetCode}, '%')
                )
            </if>
        </where>
        order by create_time desc, order_id desc
    </select>

    <select id="selectAmsInboundOrderByOrderId" parameterType="Long" resultMap="AmsInboundOrderItemCollectionResult">
        <include refid="selectAmsInboundOrderVo"/>
        where order_id = #{orderId} and del_flag = '0'
    </select>

    <select id="selectAmsInboundOrderByOrderIdForUpdate" parameterType="Long"
            resultMap="AmsInboundOrderItemCollectionResult">
        <include refid="selectAmsInboundOrderVo"/>
        where order_id = #{orderId} and del_flag = '0'
        for update
    </select>

    <!-- 查询可入库资产候选列表：只筛选待入库状态，并通过 NOT EXISTS 排除已被其他有效草稿占用的资产 -->
    <select id="selectAvailableInboundAssetList" resultMap="AvailableInboundAssetResult">
        select asset.asset_id, asset.asset_code, asset.asset_name, asset.category_id,
               asset.category_code, asset.category_name, asset.spec_model, asset.brand,
               asset.asset_status, asset.tag_code
        from ams_asset asset
        where asset.del_flag = '0'
          and asset.asset_status = #{pendingStatus}
          and not exists (
              select 1
              from ams_inbound_order_item item
              inner join ams_inbound_order inbound on inbound.order_id = item.order_id
                  and inbound.del_flag = '0'
                  and inbound.order_status = #{draftStatus}
              where item.asset_id = asset.asset_id
                and item.del_flag = '0'
                <if test="currentOrderId != null">
                    and inbound.order_id != #{currentOrderId}
                </if>
          )
        <if test="asset.assetCode != null and asset.assetCode != ''">
            and asset.asset_code like concat('%', #{asset.assetCode}, '%')
        </if>
        <if test="asset.assetName != null and asset.assetName != ''">
            and asset.asset_name like concat('%', #{asset.assetName}, '%')
        </if>
        <if test="asset.categoryName != null and asset.categoryName != ''">
            and asset.category_name like concat('%', #{asset.categoryName}, '%')
        </if>
        order by asset.asset_id
    </select>

    <!-- 保存草稿时防重复占用校验：统计同一资产在其他有效草稿入库单中的引用次数 -->
    <select id="countOtherDraftInboundOrderByAssetId" resultType="int">
        select count(1)
        from ams_inbound_order_item item
        inner join ams_inbound_order inbound on inbound.order_id = item.order_id
            and inbound.del_flag = '0'
            and inbound.order_status = #{draftStatus}
        where item.asset_id = #{assetId}
          and item.del_flag = '0'
          <if test="currentOrderId != null">
              and inbound.order_id != #{currentOrderId}
          </if>
    </select>

    <select id="selectAmsInboundOrderItemList" resultMap="AmsInboundOrderItemResult">
        select item_id, order_id, inbound_no, asset_id, asset_code, asset_name, category_id,
               category_code, category_name, spec_model, brand, location_id, location_code,
               location_name, inbound_quantity, create_by, create_time, update_by, update_time,
               remark, del_flag
        from ams_inbound_order_item
        where order_id = #{order_id} and del_flag = '0'
        order by item_id
    </select>

    <insert id="insertAmsInboundOrder" parameterType="AmsInboundOrder" useGeneratedKeys="true" keyProperty="orderId">
        insert into ams_inbound_order (
            inbound_no, warehouse_id, warehouse_code, warehouse_name, order_status,
            create_by, create_time, remark, del_flag
        ) values (
            #{inboundNo}, #{warehouseId}, #{warehouseCode}, #{warehouseName}, #{orderStatus},
            #{createBy}, #{createTime}, #{remark}, #{delFlag}
        )
    </insert>

    <update id="updateAmsInboundOrder" parameterType="AmsInboundOrder">
        update ams_inbound_order
        set warehouse_id = #{warehouseId},
            warehouse_code = #{warehouseCode},
            warehouse_name = #{warehouseName},
            update_by = #{updateBy},
            update_time = #{updateTime},
            remark = #{remark}
        where order_id = #{orderId} and del_flag = '0' and order_status = 'DRAFT'
    </update>

    <update id="confirmAmsInboundOrder" parameterType="AmsInboundOrder">
        update ams_inbound_order
        set order_status = #{orderStatus},
            inbound_user_id = #{inboundUserId},
            inbound_user_name = #{inboundUserName},
            inbound_time = #{inboundTime},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where order_id = #{orderId} and del_flag = '0' and order_status = 'DRAFT'
    </update>

    <update id="deleteAmsInboundOrderByOrderId" parameterType="Long">
        update ams_inbound_order
        set del_flag = '1'
        where order_id = #{orderId} and del_flag = '0' and order_status = 'DRAFT'
    </update>

    <update id="deleteAmsInboundOrderByOrderIds" parameterType="String">
        update ams_inbound_order set del_flag = '1' where order_id in
        <foreach item="orderId" collection="array" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        and del_flag = '0' and order_status = 'DRAFT'
    </update>

    <update id="deleteAmsInboundOrderItemByOrderIds" parameterType="String">
        update ams_inbound_order_item set del_flag = '1' where order_id in
        <foreach item="orderId" collection="array" open="(" separator="," close=")">
            #{orderId}
        </foreach>
        and del_flag = '0'
    </update>

    <update id="deleteAmsInboundOrderItemByOrderId" parameterType="Long">
        update ams_inbound_order_item
        set del_flag = '1'
        where order_id = #{orderId} and del_flag = '0'
    </update>

    <insert id="batchAmsInboundOrderItem">
        insert into ams_inbound_order_item (
            order_id, inbound_no, asset_id, asset_code, asset_name, category_id, category_code,
            category_name, spec_model, brand, location_id, location_code, location_name,
            inbound_quantity, create_by, create_time, remark, del_flag
        ) values
        <foreach item="item" collection="list" separator=",">
            (
                #{item.orderId}, #{item.inboundNo}, #{item.assetId}, #{item.assetCode},
                #{item.assetName}, #{item.categoryId}, #{item.categoryCode}, #{item.categoryName},
                #{item.specModel}, #{item.brand}, #{item.locationId}, #{item.locationCode},
                #{item.locationName}, #{item.inboundQuantity}, #{item.createBy}, #{item.createTime},
                #{item.remark}, #{item.delFlag}
            )
        </foreach>
    </insert>

</mapper>
