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

<!--    主数据定义-->
    <resultMap id="ReturnOrdersResult" type="ReturnOrders">
        <result property="id"    column="id"    />
        <result property="orderId"    column="order_id"    />
        <result property="returnDate"    column="return_date"    />
        <result property="returnType"    column="return_type"    />
        <result property="ownerId"    column="owner_id"    />
        <result property="ownerName"    column="owner_name"    />
        <result property="originalOrderId"    column="original_order_id"    />
        <result property="returnReason"    column="return_reason"    />
        <result property="orderStatus"    column="order_status"    />
        <result property="totalPlannedQuantity"    column="total_planned_quantity"    />
        <result property="totalActualQuantity"    column="total_actual_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>
<!--    关联设置-->
    <resultMap id="ReturnOrdersReturnOrderItemsResult" type="ReturnOrders" extends="ReturnOrdersResult">
        <collection property="returnOrderItemsList" ofType="ReturnOrderItems" column="id" select="selectReturnOrderItemsList" />
    </resultMap>
<!--    子数据定义-->
    <resultMap id="ReturnOrderItemsResult" type="ReturnOrderItems">
        <result property="id"    column="id"    />
        <result property="orderId"    column="order_id"    />
        <result property="returnOrderId"    column="return_order_id"    />
        <result property="materialId"    column="material_id"    />
        <result property="materialName"    column="material_name"    />
        <result property="batchCode"    column="batch_code"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehouseName"    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="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"    />
    </resultMap>

    <!--    子表信息查询-->
    <select id="selectReturnOrderItemsList" resultMap="ReturnOrderItemsResult">
        select roi.id, roi.return_order_id, roi.order_id, roi.material_id, m.material_name, roi.batch_code, roi.warehouse_id, w.warehouses_name, roi.location_id, sl.location_name, 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
        from return_order_items roi 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
        where roi.return_order_id = #{id}
    </select>

<!--    查询定义，查询退库单主信息所有数据-->
    <sql id="selectReturnOrdersVo">
        select ro.id, ro.order_id, ro.return_date, ro.return_type, ro.owner_id, o.owner_name, ro.original_order_id, ro.return_reason, ro.order_status,
        ro.total_planned_quantity, ro.total_actual_quantity, ro.remark, ro.create_time, ro.create_user_code, ro.update_time, ro.update_user_code
        from return_orders ro left join owners o on ro.owner_id = o.id
    </sql>

<!--    查询退库单主表信息-->
    <select id="selectReturnOrdersList" parameterType="ReturnOrders" resultMap="ReturnOrdersResult">
        <include refid="selectReturnOrdersVo"/>
        <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="returnDate != null "> and date_format(return_date,'%y%m%d') = date_format(#{returnDate},'%y%m%d')</if>
            <if test="returnType != null  and returnType != ''"> and return_type = #{returnType}</if>
            <if test="ownerId != null  and ownerId != ''"> and owner_id = #{ownerId}</if>
            <if test="originalOrderId != null  and originalOrderId != ''"> and original_order_id like concat('%', #{originalOrderId}, '%')</if>
            <if test="returnReason != null  and returnReason != ''"> and return_reason like concat('%', #{returnReason}, '%')</if>
            <if test="orderStatus != null "> and order_status = #{orderStatus}</if>
            <if test="totalPlannedQuantity != null "> and total_planned_quantity = #{totalPlannedQuantity}</if>
            <if test="totalActualQuantity != null "> and total_actual_quantity = #{totalActualQuantity}</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 create_time desc
    </select>
<!--    根据ID查询退库单主子信息-->
    <select id="selectReturnOrdersById" parameterType="String" resultMap="ReturnOrdersReturnOrderItemsResult">
        select ro.id, ro.order_id, ro.return_date, ro.return_type, ro.owner_id, o.owner_name, ro.original_order_id, ro.return_reason, ro.order_status,
        ro.total_planned_quantity, ro.total_actual_quantity, ro.remark, ro.create_time, ro.create_user_code, ro.update_time, ro.update_user_code
        from return_orders ro left join owners o on ro.owner_id = o.id
        where ro.id = #{id}
    </select>

<!--    保存主表信息-->
    <insert id="insertReturnOrders" parameterType="ReturnOrders">
        insert into return_orders
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="orderId != null">order_id,</if>
            <if test="returnDate != null">return_date,</if>
            <if test="returnType != null">return_type,</if>
            <if test="ownerId != null">owner_id,</if>
            <if test="originalOrderId != null">original_order_id,</if>
            <if test="returnReason != null">return_reason,</if>
            <if test="orderStatus != null">order_status,</if>
            <if test="totalPlannedQuantity != null">total_planned_quantity,</if>
            <if test="totalActualQuantity != null">total_actual_quantity,</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="returnDate != null">#{returnDate},</if>
            <if test="returnType != null">#{returnType},</if>
            <if test="ownerId != null">#{ownerId},</if>
            <if test="originalOrderId != null">#{originalOrderId},</if>
            <if test="returnReason != null">#{returnReason},</if>
            <if test="orderStatus != null">#{orderStatus},</if>
            <if test="totalPlannedQuantity != null">#{totalPlannedQuantity},</if>
            <if test="totalActualQuantity != null">#{totalActualQuantity},</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="updateReturnOrders" parameterType="ReturnOrders">
        update return_orders
        <trim prefix="SET" suffixOverrides=",">
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="returnDate != null">return_date = #{returnDate},</if>
            <if test="returnType != null">return_type = #{returnType},</if>
            <if test="ownerId != null">owner_id = #{ownerId},</if>
            <if test="originalOrderId != null">original_order_id = #{originalOrderId},</if>
            <if test="returnReason != null">return_reason = #{returnReason},</if>
            <if test="orderStatus != null">order_status = #{orderStatus},</if>
            <if test="totalPlannedQuantity != null">total_planned_quantity = #{totalPlannedQuantity},</if>
            <if test="totalActualQuantity != null">total_actual_quantity = #{totalActualQuantity},</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="deleteReturnOrdersById" parameterType="String">
        delete from return_orders where id = #{id}
    </delete>
<!--    批量删除主表信息-->
    <delete id="deleteReturnOrdersByIds" parameterType="String">
        delete from return_orders 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 return_order_id = #{id}
    </delete>
<!--    批量删除子表信息-->
    <delete id="deleteReturnOrderItemsByOrderIds" parameterType="String">
        delete from return_order_items where return_order_id in
        (select id from return_orders where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
        )
    </delete>

<!--    插入子表信息-->
    <insert id="batchReturnOrderItems">
        insert into return_order_items( id, return_order_id, order_id, material_id, batch_code, warehouse_id, location_id, planned_quantity, actual_quantity, planned_packages, actual_packages, divisor, label_color, voucher_number, unit_price, total_amount, remark, is_used, sort_no, create_time, create_user_code, update_time, update_user_code) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.returnOrderId}, #{item.orderId}, #{item.materialId}, #{item.batchCode}, #{item.warehouseId}, #{item.locationId}, #{item.plannedQuantity}, #{item.actualQuantity}, #{item.plannedPackages}, #{item.actualPackages}, #{item.divisor}, #{item.labelColor}, #{item.voucherNumber}, #{item.unitPrice}, #{item.totalAmount}, #{item.remark}, #{item.isUsed}, #{item.sortNo}, #{item.createTime}, #{item.createUserCode}, #{item.updateTime}, #{item.updateUserCode})
        </foreach>
    </insert>
<!--    退库确认-->
    <update id="confirmReturnOrdersByIds" parameterType="String">
        update return_orders set order_status = 2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>

