<?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.ReturnOrderItemsMapper">
    
    <resultMap type="ReturnOrderItems" id="ReturnOrderItemsResult">
        <result property="id"    column="id"    />
        <result property="orderId"    column="order_id"    />
        <result property="materialId"    column="material_id"    />
        <result property="batchCode"    column="batch_code"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="locationId"    column="location_id"    />
        <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="totalAmount"    column="total_amount"    />
        <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"    />
        <result property="returnDate"    column="return_date"    />
        <result property="ownerName"    column="owner_name"    />
        <result property="materialName"    column="material_name"    />
        <result property="warehouseName"    column="warehouse_name"    />
        <result property="locationName"    column="location_name"    />
    </resultMap>

    <sql id="selectReturnOrderItemsVo">
        select 
            roi.id, 
            roi.order_id, 
            roi.material_id, 
            roi.batch_code, 
            roi.warehouse_id, 
            roi.location_id, 
            roi.planned_quantity, 
            roi.actual_quantity, 
            roi.planned_packages, 
            roi.actual_packages, 
            roi.divisor, 
            roi.label_color, 
            roi.voucher_number, 
            roi.unit_price, 
            roi.total_amount, 
            roi.remark, 
            roi.is_used, 
            roi.sort_no, 
            roi.create_time, 
            roi.create_user_code, 
            roi.update_time, 
            roi.update_user_code,
            ro.return_date,
            o.owner_name,
            m.material_name,
            w.warehouses_name as warehouse_name,
            sl.location_name
        from return_order_items roi
        left join return_orders ro on roi.return_order_id = ro.id
        left join owners o on ro.owner_id = o.id
        left join materials m on roi.material_id = m.id
        left join warehouses w on roi.warehouse_id = w.id
        left join storage_locations sl on roi.location_id = sl.id
    </sql>

    <select id="selectReturnOrderItemsList" parameterType="ReturnOrderItems" resultMap="ReturnOrderItemsResult">
        <include refid="selectReturnOrderItemsVo"/>
        <where>
            and roi.is_used = 1 and ro.is_used = 1
            <if test="id != null  and id != ''"> and roi.id = #{id}</if>
            <if test="orderId != null  and orderId != ''"> and roi.order_id like concat('%', #{orderId}, '%')</if>
            <if test="materialId != null  and materialId != ''"> and (roi.material_id like concat('%', #{materialId}, '%') or m.material_code like concat('%', #{materialId}, '%'))</if>
            <if test="batchCode != null  and batchCode != ''"> and roi.batch_code = #{batchCode}</if>
            <if test="warehouseId != null  and warehouseId != ''"> and roi.warehouse_id = #{warehouseId}</if>
            <if test="locationId != null  and locationId != ''"> and roi.location_id = #{locationId}</if>
            <if test="ownerId != null  and ownerId != ''"> and ro.owner_id = #{ownerId}</if>
            <if test="startDate != null  and startDate != ''"> and date_format(ro.return_date,'%Y-%m-%d') &gt;= #{startDate}</if>
            <if test="endDate != null  and endDate != ''"> and date_format(ro.return_date,'%Y-%m-%d') &lt;= #{endDate}</if>
            <if test="orderStatus != null  and orderStatus != ''"> and ro.order_status = #{orderStatus}</if>
<!--            <if test="plannedQuantity != null "> and roi.planned_quantity = #{plannedQuantity}</if>-->
<!--            <if test="actualQuantity != null "> and roi.actual_quantity = #{actualQuantity}</if>-->
<!--            <if test="plannedPackages != null "> and roi.planned_packages = #{plannedPackages}</if>-->
<!--            <if test="actualPackages != null "> and roi.actual_packages = #{actualPackages}</if>-->
<!--            <if test="divisor != null "> and roi.divisor = #{divisor}</if>-->
<!--            <if test="labelColor != null "> and roi.label_color = #{labelColor}</if>-->
<!--            <if test="voucherNumber != null  and voucherNumber != ''"> and roi.voucher_number = #{voucherNumber}</if>-->
<!--            <if test="unitPrice != null "> and roi.unit_price = #{unitPrice}</if>-->
<!--            <if test="totalAmount != null "> and roi.total_amount = #{totalAmount}</if>-->
<!--            <if test="isUsed != null "> and roi.is_used = #{isUsed}</if>-->
<!--            <if test="sortNo != null "> and roi.sort_no = #{sortNo}</if>-->
<!--            <if test="createUserCode != null  and createUserCode != ''"> and roi.create_user_code = #{createUserCode}</if>-->
<!--            <if test="updateUserCode != null  and updateUserCode != ''"> and roi.update_user_code = #{updateUserCode}</if>-->
        </where>
        order by ro.return_date desc, roi.sort_no, roi.create_time
    </select>
    
    <select id="selectReturnOrderItemsById" parameterType="String" resultMap="ReturnOrderItemsResult">
        <include refid="selectReturnOrderItemsVo"/>
        where roi.id = #{id}
    </select>
        
    <insert id="insertReturnOrderItems" parameterType="ReturnOrderItems">
        insert into return_order_items
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="orderId != null">order_id,</if>
            <if test="materialId != null">material_id,</if>
            <if test="batchCode != null">batch_code,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="locationId != null">location_id,</if>
            <if test="plannedQuantity != null">planned_quantity,</if>
            <if test="actualQuantity != null">actual_quantity,</if>
            <if test="plannedPackages != null">planned_packages,</if>
            <if test="actualPackages != null">actual_packages,</if>
            <if test="divisor != null">divisor,</if>
            <if test="labelColor != null">label_color,</if>
            <if test="voucherNumber != null">voucher_number,</if>
            <if test="unitPrice != null">unit_price,</if>
            <if test="totalAmount != null">total_amount,</if>
            <if test="remark != null">remark,</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="materialId != null">#{materialId},</if>
            <if test="batchCode != null">#{batchCode},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="locationId != null">#{locationId},</if>
            <if test="plannedQuantity != null">#{plannedQuantity},</if>
            <if test="actualQuantity != null">#{actualQuantity},</if>
            <if test="plannedPackages != null">#{plannedPackages},</if>
            <if test="actualPackages != null">#{actualPackages},</if>
            <if test="divisor != null">#{divisor},</if>
            <if test="labelColor != null">#{labelColor},</if>
            <if test="voucherNumber != null">#{voucherNumber},</if>
            <if test="unitPrice != null">#{unitPrice},</if>
            <if test="totalAmount != null">#{totalAmount},</if>
            <if test="remark != null">#{remark},</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="updateReturnOrderItems" parameterType="ReturnOrderItems">
        update return_order_items
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="batchCode != null">batch_code = #{batchCode},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="locationId != null">location_id = #{locationId},</if>
            <if test="plannedQuantity != null">planned_quantity = #{plannedQuantity},</if>
            <if test="actualQuantity != null">actual_quantity = #{actualQuantity},</if>
            <if test="plannedPackages != null">planned_packages = #{plannedPackages},</if>
            <if test="actualPackages != null">actual_packages = #{actualPackages},</if>
            <if test="divisor != null">divisor = #{divisor},</if>
            <if test="labelColor != null">label_color = #{labelColor},</if>
            <if test="voucherNumber != null">voucher_number = #{voucherNumber},</if>
            <if test="unitPrice != null">unit_price = #{unitPrice},</if>
            <if test="totalAmount != null">total_amount = #{totalAmount},</if>
            <if test="remark != null">remark = #{remark},</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="deleteReturnOrderItemsById" parameterType="String">
        delete from return_order_items where id = #{id}
    </delete>

    <delete id="deleteReturnOrderItemsByIds" parameterType="String">
        delete from return_order_items where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

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

    <resultMap type="ReturnOrdersSummaryVO" id="ReturnOrderCountResult">
        <result property="materialId"    column="material_id"    />
        <result property="materialName"    column="material_name"    />
        <result property="sapNo"    column="sap_no"    />
        <result property="tsCode"    column="ts_code"    />
        <result property="hazardId"    column="hazard_id"    />
        <result property="specification"    column="specification"    />
        <result property="materialUnit"    column="material_unit"    />
        <result property="unitWeight"    column="unit_weight"    />
        <result property="packageWeight"    column="package_weight"    />
        <result property="totalWeight"    column="total_weight"    />
        <result property="volume"    column="volume"    />
        <result property="shelfLifeDays"    column="shelf_life_days"    />
        <result property="storageTemperature"    column="storage_temperature"    />
        <result property="specialRequirements"    column="special_requirements"    />
        <result property="plannedQuantity"    column="planned_quantity"    />
        <result property="actualQuantity"    column="actual_quantity"    />
        <result property="totalAmount"    column="total_amount"    />
    </resultMap>
<!--    退库统计-->
    <select id="selectReturnOrderCount" parameterType="ReturnOrderItems" resultMap="ReturnOrderCountResult">
        select roi.material_id,
        m.material_name,
        m.sap_no,
        m.ts_code,
        m.hazard_id,
        m.specification,
        m.material_unit,
        m.unit_weight,
        m.package_weight,
        m.total_weight,
        m.volume,
        m.shelf_life_days,
        m.storage_temperature,
        m.special_requirements,
        m.sort_no,
        sum(roi.planned_quantity) as planned_quantity,
        sum(roi.actual_quantity) as actual_quantity,
        sum(roi.total_amount) as total_amount
        from return_order_items roi
        left join return_orders ro on roi.return_order_id = ro.id
        left join materials m on roi.material_id = m.id
        <where>
            roi.is_used = 1 and ro.is_used = 1
            <if test="orderId != null  and orderId != ''"> and roi.order_id like concat('%', #{orderId}, '%')</if>
            <if test="materialId != null  and materialId != ''"> and roi.material_id = #{materialId}</if>
            <if test="batchCode != null  and batchCode != ''"> and roi.batch_code = #{batchCode}</if>
            <if test="startDate != null  and startDate != ''"> and date_format(ro.return_date,'%Y-%m-%d') &gt;= #{startDate}</if>
            <if test="endDate != null  and endDate != ''"> and date_format(ro.return_date,'%Y-%m-%d') &lt;= #{endDate}</if>
            <if test="warehouseId != null  and warehouseId != ''"> and roi.warehouse_id = #{warehouseId}</if>
            <if test="locationId != null  and locationId != ''"> and roi.location_id = #{locationId}</if>
            <if test="orderStatus != null "> and ro.order_status = #{orderStatus}</if>
        </where>
        group by roi.material_id, m.material_name, m.sap_no,
        m.ts_code,
        m.hazard_id,
        m.specification,
        m.material_unit,
        m.unit_weight,
        m.package_weight,
        m.total_weight,
        m.volume,
        m.shelf_life_days,
        m.storage_temperature,
        m.special_requirements,
        m.sort_no
        order by sum(roi.total_amount) desc,m.sap_no, m.sort_no
    </select>
</mapper>

