<?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.victoriatourist.mapper.logistics.LogisticsInfosMapper">
    <select id="getPage" resultType="com.fjhx.victoriatourist.entity.logistics.vo.LogisticsInfosVo">
        select
            li.id,
            li.business_id,
            li.business_code,
            li.business_type,
            li.warehouse_id,
            li.status,
            li.in_stock_status,
            li.code,
            li.logistics_company_code,
            li.logistics_status,
            li.is_kd100,
            li.create_user,
            li.create_time,
            li.update_user,
            li.update_time,
            li.country_id,
            li.province_id,
            li.city_id,
            li.address,
            li.purchase_id,
            li.arrival_remark,
            de.dept_name,
            dg.status arrivalStatus
        from logistics_infos li
            LEFT JOIN deliver_goods dg on li.business_id = dg.id
            LEFT JOIN bytesailing_purchase.purchase p ON p.id = li.purchase_id
                <!-- 采购到货 采购退货 来源 -->
                <if test="businessType eq 1 or businessType eq 5">
--                     LEFT JOIN bytesailing_purchase.purchase_detail pd ON pd.purchase_id = li.purchase_id
--                     LEFT JOIN bytesailing_item.product_info pi ON pd.bussiness_id = pi.id
                </if>
                <!-- 京东退货来源 -->
                <if test="businessType eq 4">
                    LEFT JOIN jd_back_details jbd ON jbd.jd_back_id = li.business_id
                    LEFT JOIN bytesailing_item.product_info pi ON jbd.product_code = pi.custom_code
                </if>
                <!-- 京东订单来源 -->
                <if test="businessType eq 2">
                    LEFT JOIN jd_order_details jod ON jod.jd_order_id = li.business_id
                    LEFT JOIN bytesailing_item.product_info pi ON jod.product_id = pi.id
                </if>
                <!-- 销售订单来源 -->
                <if test="businessType eq 3">
                    LEFT JOIN order_details od ON od.order_id = li.business_id
                    LEFT JOIN bytesailing_item.product_info pi ON od.product_id = pi.id
                </if>
                LEFT JOIN bytesailing_base.sys_dept de ON
                <if test="businessType neq 1 and businessType neq 5">
                    pi.deptWdly = de.dept_id
                </if>
                <if test="businessType eq 1 or businessType eq 5">
                     p.team_id=de.dept_id
                </if>
            ${ew.customSqlSegment}
    </select>
    <select id="getDepts" resultType="com.ruoyi.common.core.domain.entity.SysDept">
        SELECT
            de.dept_id,
            de.dept_name
        FROM
            logistics_infos li
                JOIN bytesailing_purchase.purchase p ON li.purchase_id = p.id
                JOIN bytesailing_purchase.purchase_detail pd ON pd.purchase_id = p.id
                JOIN bytesailing_item.product_info pi ON pd.bussiness_id = pi.id
                JOIN bytesailing_base.sys_dept de ON CAST(json_unquote(pi.victoriatourist_json -> '$.deptId') AS UNSIGNED) = de.dept_id
        GROUP BY
            de.dept_id
    </select>

</mapper>
