<?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.fjhx.wms.mapper.stock.StockWaitMapper">
    <select id="getPage1" resultType="com.fjhx.wms.entity.stock.vo.StockWaitVo">
        SELECT
            sw.id,
            swd.id stockWaitDetailsId,
            sw.type,
            sw.business_id,
            sw.business_code,
            sw.business_type,
            swd.product_id,
            swd.quantity,
            swd.receipt_quantity,
            sw.`status`,
            sw.create_user,
            sw.create_time,
            sw.update_user,
            sw.update_time,
            sw.victoriatourist_json
        FROM
            stock_wait sw
                JOIN stock_wait_details swd ON swd.stock_wait_id = sw.id
            ${ew.customSqlSegment}
    </select>

    <select id="getPage" resultType="com.fjhx.wms.entity.stock.vo.StockWaitVo">
        SELECT
            sw.id,
            sw.type,
            sw.business_id,
            sw.business_code,
            sw.business_type,
            sw.`status`,
            sw.create_user,
            sw.create_time,
            sw.update_user,
            sw.update_time,
            sw.victoriatourist_json,
            lis.logistics_company_code,
            lis.`code` AS logisticsCode,
            pb.detailed_address
        FROM
            stock_wait sw
                LEFT JOIN stock_wait_details swd ON swd.stock_wait_id = sw.id
                LEFT JOIN bytesailing_item.product_info pi ON swd.product_id = pi.id
                LEFT JOIN bytesailing_victoriatourist.logistics_infos lis ON lis.business_id = CAST( json_unquote( sw.victoriatourist_json -> '$.deliverGoodsId' ) AS UNSIGNED )
                LEFT JOIN bytesailing_victoriatourist.purchase_back pb ON sw.business_id = pb.id
                AND pb.del_flag = 0
            ${ew.customSqlSegment}
    </select>
    <select id="getStockWaitList" resultType="com.fjhx.wms.entity.stock.vo.StockWaitDetailsVo">
        SELECT
            *
        FROM
            stock_wait_details swd
                LEFT JOIN stock_wait sw ON swd.stock_wait_id = sw.id
            ${ew.customSqlSegment}
    </select>
    <select id="getInStockWaitExportExcel" resultType="com.fjhx.wms.entity.stock.vo.InStockWaitExportExcel">
        SELECT
            sw.business_type,
            sw.business_code,
            ci.`name` AS logisticsCompanyName,
            dg.`code`,
            w.`name` AS warehouseName,
            pi.custom_code AS productCode,
            pi.`name` AS productName,
            dgd.deliver_goods_quantity,
            swd.receipt_quantity,
            swd.quantity - swd.receipt_quantity AS waitQuantity
        FROM
            stock_wait sw
                LEFT JOIN stock_wait_details swd ON swd.stock_wait_id = sw.id
                LEFT JOIN bytesailing_purchase.arrival_detail ad ON swd.business_details_id = ad.id
                AND ad.del_flag = 0
                LEFT JOIN bytesailing_victoriatourist.deliver_goods_details dgd ON ad.deliver_goods_details_id = dgd.id
                AND dgd.del_flag = 0
                LEFT JOIN bytesailing_victoriatourist.deliver_goods dg ON dgd.deliver_goods_id = dg.id
                AND dg.del_flag = 0
                LEFT JOIN bytesailing_item.product_info pi ON swd.product_id = pi.id
                AND pi.del_flag = 0
                LEFT JOIN warehouse w ON cast( json_unquote( sw.victoriatourist_json -> '$.receiptWarehouseId' ) AS UNSIGNED ) = w.id
                LEFT JOIN bytesailing_kd100.company_info ci ON dg.logistics_company_code = ci.`code`
                AND ci.del_flag = 0
            ${ew.customSqlSegment}
    </select>

</mapper>
