<?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.InboundItemsAndMaterialName" 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="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 = #{orderId}</if>
            <if test="systemNo != null  and systemNo != ''"> and system_no = #{systemNo}</if>
            <if test="orderTypeId != null  and orderTypeId != ''"> and order_type_id = #{orderTypeId}</if>
            <if test="batchId != null  and batchId != ''"> and batch_id = #{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="inboundDate != null "> and inbound_date = #{inboundDate}</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>
    </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.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>
         </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>
         </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="orderId" collection="array" open="(" separator="," close=")">
            #{orderId}
        </foreach>
    </delete>

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

    <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, 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.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 m.material_name,sum(ioi.actual_quantity) as total_quantity
        from inbound_orders as io
        inner join inbound_order_items as ioi on io.id = ioi.inbound_order_id
        inner join materials as m on ioi.material_id = m.id
        where io.order_status = 2
        group by m.id
        order by total_quantity asc
    </select>
    <select id="countInboundMaterialMoney" resultMap="InboundMaterialTotalResultMap" parameterType="String">
        select m.material_name,sum(ioi.actual_quantity * ioi.unit_price) as total_money
        from inbound_orders as io
        inner join inbound_order_items as ioi on io.id = ioi.inbound_order_id
        inner join materials as m on ioi.material_id = m.id
        where io.order_status = 2
        group by m.id
        order by total_money asc
    </select>
</mapper>