<?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.inventory.mapper.InboundOrdersMapper">
    
    <resultMap type="InboundOrders" id="InboundOrdersResult">
        <result property="id"    column="id"    />
        <result property="orderId"    column="order_id"    />
        <result property="systemNo"    column="system_no"    />
        <result property="orderTypeId"    column="order_type_id"    />
        <result property="batchId"    column="batch_id"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="ownerId"    column="owner_id"    />
        <result property="ownerName"    column="owner_name"    />
        <result property="orderStatus"    column="order_status"    />
        <result property="inboundDate"    column="inbound_date"    />
        <result property="orderType"    column="order_type"    />
        <result property="totalPlannedQuantity"    column="total_planned_quantity"    />
        <result property="totalActualQuantity"    column="total_actual_quantity"    />
        <result property="totalPackages"    column="total_packages"    />
        <result property="remark"    column="remark"    />
        <result property="opUserName"    column="op_user_name"    />
        <result property="isUsed"    column="is_used"    />
        <result property="sortNo"    column="sort_no"    />
        <result property="createTime"    column="create_time"    />
        <result property="createUserCode"    column="create_user_code"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateUserCode"    column="update_user_code"    />
    </resultMap>

    <resultMap id="InboundOrdersInboundOrderItemsResult" type="InboundOrders" extends="InboundOrdersResult">
        <collection property="inboundOrderItemsList" ofType="InboundOrderItems" column="id" select="selectInboundOrderItemsList" />
    </resultMap>

    <resultMap type="com.ruoyi.inventory.domain.TO.InboundItemsTO" id="InboundOrderItemsResult">
        <result property="id"    column="id"    />
        <result property="orderId"    column="order_id"    />
        <result property="materialId" column="material_id"/>
        <result property="sapNo"    column="sap_no"    />
        <result property="materialName"    column="material_name"    />
        <result property="batchId"    column="batch_id"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehousesName"    column="warehouses_name"    />
        <result property="locationId"    column="location_id"    />
        <result property="locationName"    column="location_name"    />
        <result property="plannedQuantity"    column="planned_quantity"    />
        <result property="actualQuantity"    column="actual_quantity"    />
        <result property="plannedPackages"    column="planned_packages"    />
        <result property="actualPackages"    column="actual_packages"    />
        <result property="divisor"    column="divisor"    />
        <result property="labelColor"    column="label_color"    />
        <result property="voucherNumber"    column="voucher_number"    />
        <result property="unitPrice"    column="unit_price"    />
        <result property="itemStatus"    column="item_status"    />
        <result property="receivedAt"    column="received_at"    />
        <result property="receivedBy"    column="received_by"    />
        <result property="labelQuantity"    column="label_quantity"    />
        <result property="remark"    column="remark"    />
        <result property="isUsed"    column="is_used"    />
        <result property="sortNo"    column="sort_no"    />
        <result property="createTime"    column="create_time"    />
        <result property="createUserCode"    column="create_user_code"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateUserCode"    column="update_user_code"    />
    </resultMap>

    <sql id="selectInboundOrdersVo">
        select io.id, io.order_id, io.system_no, io.order_type_id, io.batch_id, io.warehouse_id, io.owner_id, o.owner_name,
        io.order_status, io.inbound_date, io.order_type, io.total_planned_quantity, io.total_actual_quantity, io.total_packages,
        io.remark, io.op_user_name, io.is_used, io.sort_no, io.create_time, io.create_user_code, io.update_time, io.update_user_code
        from inbound_orders io left join owners o on io.owner_id = o.id
    </sql>

    <select id="selectInboundOrdersList" parameterType="InboundOrders" resultMap="InboundOrdersResult">
        <include refid="selectInboundOrdersVo"/>
        <where>
            <if test="id != null  and id != ''"> and id = #{id}</if>
            <if test="orderId != null  and orderId != ''"> and order_id like concat('%', #{orderId}, '%')</if>
            <if test="systemNo != null  and systemNo != ''"> and system_no like concat('%', #{systemNo}, '%')</if>
            <if test="orderTypeId != null  and orderTypeId != ''"> and order_type_id = #{orderTypeId}</if>
            <if test="batchId != null  and batchId != ''"> and batch_id like concat('%', #{batchId}, '%') </if>
            <if test="warehouseId != null  and warehouseId != ''"> and warehouse_id = #{warehouseId}</if>
            <if test="ownerId != null  and ownerId != ''"> and owner_id = #{ownerId}</if>
            <if test="orderStatus != null "> and order_status = #{orderStatus}</if>
<!--            新增按照日期范围查询-->
            <if test="inboundDateStart != null">
                AND date(inbound_date) &gt;= #{inboundDateStart}
            </if>
            <if test="inboundDateEnd != null">
                AND date(inbound_date) &lt;= #{inboundDateEnd}
            </if>
            <if test="orderType != null  and orderType != ''"> and order_type = #{orderType}</if>
            <if test="totalPlannedQuantity != null "> and total_planned_quantity = #{totalPlannedQuantity}</if>
            <if test="totalActualQuantity != null "> and total_actual_quantity = #{totalActualQuantity}</if>
            <if test="totalPackages != null "> and total_packages = #{totalPackages}</if>
            <if test="opUserName != null  and opUserName != ''"> and op_user_name like concat('%', #{opUserName}, '%')</if>
            <if test="isUsed != null "> and is_used = #{isUsed}</if>
            <if test="sortNo != null "> and sort_no = #{sortNo}</if>
            <if test="createUserCode != null  and createUserCode != ''"> and create_user_code = #{createUserCode}</if>
            <if test="updateUserCode != null  and updateUserCode != ''"> and update_user_code = #{updateUserCode}</if>
        </where>
        order by order_status asc, date(inbound_date) desc,create_time desc
    </select>
    
    <select id="selectInboundOrdersById" parameterType="String" resultMap="InboundOrdersInboundOrderItemsResult">
        select io.id, io.order_id, io.system_no, io.order_type_id, io.batch_id, io.warehouse_id, io.owner_id, o.owner_name,
        io.order_status, io.inbound_date, io.order_type, io.total_planned_quantity, io.total_actual_quantity, io.total_packages,
        io.remark, io.op_user_name, io.is_used, io.sort_no, io.create_time, io.create_user_code, io.update_time, io.update_user_code
        from inbound_orders as io
        left join owners as o on io.owner_id = o.id
        where io.id = #{id}
    </select>

    <select id="selectInboundOrderItemsList" resultMap="InboundOrderItemsResult">
        select ioi.id, ioi.order_id, ioi.material_id, m.sap_no, m.material_name, ioi.batch_id, ioi.warehouse_id, w.warehouses_name,
        ioi.location_id, sl.location_name, ioi.planned_quantity, ioi.actual_quantity, ioi.planned_packages,
        ioi.actual_packages, ioi.divisor, ioi.label_color, ioi.voucher_number, ioi.unit_price, ioi.item_status,
        ioi.received_at, ioi.received_by, ioi.label_quantity, ioi.remark, ioi.is_used, ioi.sort_no, ioi.create_time, ioi.create_user_code,
        ioi.update_time, ioi.update_user_code, ioi.inbound_order_id
        from inbound_order_items as ioi
        left join warehouses as w on ioi.warehouse_id = w.id
        left join storage_locations as sl on ioi.location_id = sl.id
        left join materials as m on ioi.material_id = m.id
        where ioi.inbound_order_id = #{id}
    </select>


    <select id="selectInboundOrdersByOrderId" resultType="InboundOrders">
        select id, order_id
        from inbound_orders
        where order_id = #{orderId}
    </select>

    <insert id="insertInboundOrders" parameterType="InboundOrders">
        insert into inbound_orders
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="orderId != null">order_id,</if>
            <if test="systemNo != null">system_no,</if>
            <if test="orderTypeId != null">order_type_id,</if>
            <if test="batchId != null">batch_id,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="ownerId != null">owner_id,</if>
            <if test="orderStatus != null">order_status,</if>
            <if test="inboundDate != null">inbound_date,</if>
            <if test="orderType != null">order_type,</if>
            <if test="totalPlannedQuantity != null">total_planned_quantity,</if>
            <if test="totalActualQuantity != null">total_actual_quantity,</if>
            <if test="totalPackages != null">total_packages,</if>
            <if test="remark != null">remark,</if>
            <if test="opUserName != null">op_user_name,</if>
            <if test="isUsed != null">is_used,</if>
            <if test="sortNo != null">sort_no,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createUserCode != null">create_user_code,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateUserCode != null">update_user_code,</if>
            <if test="isImport != null">is_import,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="systemNo != null">#{systemNo},</if>
            <if test="orderTypeId != null">#{orderTypeId},</if>
            <if test="batchId != null">#{batchId},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="ownerId != null">#{ownerId},</if>
            <if test="orderStatus != null">#{orderStatus},</if>
            <if test="inboundDate != null">#{inboundDate},</if>
            <if test="orderType != null">#{orderType},</if>
            <if test="totalPlannedQuantity != null">#{totalPlannedQuantity},</if>
            <if test="totalActualQuantity != null">#{totalActualQuantity},</if>
            <if test="totalPackages != null">#{totalPackages},</if>
            <if test="remark != null">#{remark},</if>
            <if test="opUserName != null">#{opUserName},</if>
            <if test="isUsed != null">#{isUsed},</if>
            <if test="sortNo != null">#{sortNo},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createUserCode != null">#{createUserCode},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateUserCode != null">#{updateUserCode},</if>
            <if test="isImport != null">#{isImport},</if>
         </trim>
    </insert>

    <update id="updateInboundOrders" parameterType="InboundOrders">
        update inbound_orders
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="systemNo != null">system_no = #{systemNo},</if>
            <if test="orderTypeId != null">order_type_id = #{orderTypeId},</if>
            <if test="batchId != null">batch_id = #{batchId},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="ownerId != null">owner_id = #{ownerId},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="inboundDate != null">inbound_date = #{inboundDate},</if>
            <if test="orderType != null">order_type = #{orderType},</if>
            <if test="totalPlannedQuantity != null">total_planned_quantity = #{totalPlannedQuantity},</if>
            <if test="totalActualQuantity != null">total_actual_quantity = #{totalActualQuantity},</if>
            <if test="totalPackages != null">total_packages = #{totalPackages},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="opUserName != null">op_user_name = #{opUserName},</if>
            <if test="isUsed != null">is_used = #{isUsed},</if>
            <if test="sortNo != null">sort_no = #{sortNo},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createUserCode != null">create_user_code = #{createUserCode},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateUserCode != null">update_user_code = #{updateUserCode},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteInboundOrdersById" parameterType="String">
        delete from inbound_orders where id = #{id}
    </delete>

    <delete id="deleteInboundOrdersByIds" parameterType="String">
        delete from inbound_orders where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteInboundOrderItemsByOrderIds" parameterType="String">
        delete from inbound_order_items where inbound_order_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteInboundOrderItemsByOrderId" parameterType="String">
        delete from inbound_order_items where inbound_order_id = #{id}
    </delete>

    <insert id="batchInboundOrders">
        insert into inbound_orders
        ( id, order_id, system_no, order_type_id, batch_id, warehouse_id, owner_id, order_status, inbound_date,
        order_type, total_planned_quantity, total_actual_quantity, total_packages, remark, op_user_name,
        is_used, sort_no, create_time, create_user_code, update_time, update_user_code, is_import)
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.orderId}, #{item.systemNo}, #{item.orderTypeId}, #{item.batchId}, #{item.warehouseId},
            #{item.ownerId}, #{item.orderStatus}, #{item.inboundDate}, #{item.orderType}, #{item.totalPlannedQuantity},
            #{item.totalActualQuantity}, #{item.totalPackages}, #{item.remark}, #{item.opUserName}, #{item.isUsed},
            #{item.sortNo}, #{item.createTime}, #{item.createUserCode}, #{item.updateTime}, #{item.updateUserCode},
            #{item.isImport} )
        </foreach>
    </insert>
<!--    批量插入入库明细-->
    <insert id="batchInboundOrderItems">
        insert into inbound_order_items( id, order_id, material_id, batch_id, warehouse_id, location_id, planned_quantity, actual_quantity, planned_packages, actual_packages, divisor, label_color, voucher_number, unit_price, item_status, received_at, received_by, label_quantity, remark, is_used, sort_no, create_time, create_user_code, update_time, update_user_code, inbound_order_id) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.orderId}, #{item.materialId}, #{item.batchId}, #{item.warehouseId}, #{item.locationId}, #{item.plannedQuantity}, #{item.actualQuantity}, #{item.plannedPackages}, #{item.actualPackages}, #{item.divisor}, #{item.labelColor}, #{item.voucherNumber}, #{item.unitPrice}, #{item.itemStatus}, #{item.receivedAt}, #{item.receivedBy}, #{item.labelQuantity}, #{item.remark}, #{item.isUsed}, #{item.sortNo}, #{item.createTime}, #{item.createUserCode}, #{item.updateTime}, #{item.updateUserCode}, #{item.inboundOrderId})
        </foreach>
    </insert>

<!--    统计入库次数-->
    <select id="countInboundOrders" resultType="int" parameterType="String">
        select count(id)
        from inbound_orders
        where DATE_FORMAT(inbound_date, '%Y-%m') = #{monthParam};
    </select>

<!--    统计入库物料数量Top10-->
    <resultMap id="InboundMaterialTotalResultMap" type="com.ruoyi.inventory.domain.vo.InboundMaterialTotalVO">
        <result column="material_name" property="materialName" jdbcType="VARCHAR"/>
        <result column="total_quantity" property="totalQuantity" jdbcType="BIGINT"/>
        <result column="total_money" property="totalMoney" jdbcType="DECIMAL"/>
    </resultMap>
    <select id="countInboundMaterialQuantity" resultMap="InboundMaterialTotalResultMap" parameterType="String">
        select t.material_name,t.total_quantity from (
        SELECT
        m.material_name,
        COALESCE(SUM(ioi.actual_quantity), 0) AS total_quantity
        FROM materials AS m
        LEFT JOIN (
        SELECT ioi.material_id, ioi.actual_quantity
        FROM
        inbound_order_items AS ioi
        INNER JOIN
        inbound_orders AS io
        ON
        io.id = ioi.inbound_order_id
        AND
        io.order_status = 2
        ) AS ioi ON m.id = ioi.material_id
        WHERE
        m.is_used = 1
        AND
        m.is_active = 1
        GROUP BY
        m.id, m.material_name
        ORDER BY
        total_quantity DESC
        ) as t
        order by total_quantity asc
    </select>
    <select id="countInboundMaterialMoney" resultMap="InboundMaterialTotalResultMap" parameterType="String">
        select t.material_name,t.total_money
        from(
            SELECT
            m.material_name,
            COALESCE(SUM(ioi.actual_quantity * ioi.unit_price), 0) AS total_money
            FROM materials AS m
            LEFT JOIN (
                SELECT ioi.material_id, ioi.actual_quantity,ioi.unit_price
                FROM
                inbound_order_items AS ioi
                INNER JOIN
                inbound_orders AS io
                ON
                io.id = ioi.inbound_order_id
                AND
                io.order_status = 2
            ) AS ioi ON m.id = ioi.material_id
            WHERE
            m.is_used = 1
            AND
            m.is_active = 1
            GROUP BY
            m.id, m.material_name
            ORDER BY
            total_money DESC
        ) as t
        order by total_money asc
    </select>

    <!--导出-->
    <select id="selectInboundOrdersAndItems" resultMap="InboundOrderAndItemsMap" parameterType="com.ruoyi.inventory.domain.InboundOrders">
        select io.inbound_date, m.sap_no, m.material_name,
        m.ts_code, io.batch_id, ioi.planned_quantity,
        ioi.divisor, ioi.actual_packages, ioi.actual_quantity,
        m.total_weight, sl.location_name, ioi.warehouse_id,
        ioi.label_color, ioi.voucher_number, m.storage_temperature,
        m.hazard_id, io.order_id, io.system_no, io.owner_id,
        ioi.label_quantity, io.order_type, io.order_type_id, ioi.remark
        from inbound_order_items as ioi
        left join materials as m on ioi.material_id = m.id
        left join inbound_orders as io on ioi.inbound_order_id = io.id
        left join storage_locations as sl on ioi.location_id = sl.id
        <where>
            <if test="id != null  and id != ''"> and io.id = #{id}</if>
            <if test="orderId != null  and orderId != ''"> and io.order_id like concat('%', #{orderId}, '%')</if>
            <if test="systemNo != null  and systemNo != ''"> and io.system_no like concat('%', #{systemNo}, '%')</if>
            <if test="orderTypeId != null  and orderTypeId != ''"> and io.order_type_id = #{orderTypeId}</if>
            <if test="batchId != null  and batchId != ''"> and ioi.batch_id like concat('%', #{batchId}, '%') </if>
            <if test="ownerId != null  and ownerId != ''"> and io.owner_id = #{ownerId}</if>
            <if test="orderStatus != null "> and io.order_status = #{orderStatus}</if>
            <!--            新增按照日期范围查询-->
            <if test="inboundDateStart != null">
                AND date(inbound_date) &gt;= #{inboundDateStart}
            </if>
            <if test="inboundDateEnd != null">
                AND date(inbound_date) &lt;= #{inboundDateEnd}
            </if>
            <if test="orderType != null  and orderType != ''"> and order_type = #{orderType}</if>
        </where>
        order by order_status asc, date(io.inbound_date) desc, io.create_time desc
    </select>

    <resultMap id="InboundOrderAndItemsMap" type="com.ruoyi.inventory.domain.TO.InboundItemsTO">
        <!-- 基础字段映射：SQL字段名 -> Java实体类字段名 -->
        <result column="inbound_date" property="inboundDate" />
        <result column="sap_no" property="sapNo" />
        <result column="material_name" property="materialName" />
        <result column="ts_code" property="tsCode" />
        <result column="batch_id" property="batchId" />
        <result column="planned_quantity" property="plannedQuantity" />

        <result column="remark" property="remark" />
        <result column="divisor" property="divisor" />
        <result column="actual_packages" property="actualPackages" />
        <result column="actual_quantity" property="actualQuantity" />

        <!-- 重量(成品)：映射total_weight到weight -->
        <result column="total_weight" property="weight" />

        <result column="location_name" property="locationName" />
        <result column="warehouse_id" property="warehouseId" />

        <result column="label_color" property="labelColor" />
        <result column="voucher_number" property="voucherNumber" />

        <!-- 保温：映射storage_temperature到keepWarm -->
        <result column="storage_temperature" property="keepWarm" />

        <result column="hazard_id" property="hazardId" />
        <result column="order_id" property="orderId" />
        <result column="system_no" property="systemNo" />

        <result column="owner_id" property="ownerId" />
        <result column="label_quantity" property="labelQuantity" />
        <result column="order_type" property="orderType" />
        <result column="order_type_id" property="orderTypeId"/>
    </resultMap>
</mapper>