<?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.sckw.order.dao.KwoTradeOrderMapper">
    <resultMap id="mainEntityMap" type="com.sckw.order.model.dto.OrderListResDTO">
        <id column="tOrderId" property="tOrderId"/>
        <result column="status" property="status"/>
        <result column="tOrderNo" property="tOrderNo"/>
        <result column="procureEntId" property="procureEntId"/>
        <result column="procureFirmName" property="procureFirmName"/>
        <result column="supplyEntId" property="supplyEntId"/>
        <result column="supplyFirmName" property="supplyFirmName"/>
        <result column="trading" property="trading"/>
        <result column="deliveryType" property="deliveryType"/>
        <result column="pickupType" property="pickupType"/>
        <result column="source" property="source"/>
        <result column="contractId" property="contractId"/>
        <result column="contractNo" property="contractNo"/>
        <result column="goodsId" property="goodsId"/>
        <result column="unitPrice" property="unitPrice"/>
        <result column="price" property="price"/>
        <result column="amount" property="amount"/>
        <result column="entrustAmount" property="entrustAmount"/>
        <result column="actualAmount" property="actualAmount"/>
        <result column="startTime" property="startTime"/>
        <result column="endTime" property="endTime"/>
        <result column="remark" property="remark"/>
        <result column="unit" property="unit"/>
        <result column="createBy" property="createBy"/>
        <result column="createTime" property="createTime"/>

        <collection property="loadAddresses" ofType="com.sckw.order.model.dto.LoadAddressDTO">
            <id column="loadAddressId" property="loadAddressId"/>
            <result column="loadName" property="loadName"/>
            <result column="loadCityName" property="loadCityName"/>
            <result column="loadDetailAddress" property="loadDetailAddress"/>
        </collection>

        <collection property="unloadAddresses" ofType="com.sckw.order.model.dto.UnloadAddressDTO">
            <id column="unloadAddressId" property="unloadAddressId"/>
            <result column="unloadName" property="unloadName"/>
            <result column="unloadCityName" property="unloadCityName"/>
            <result column="unloadDetailAddress" property="unloadDetailAddress"/>
        </collection>
    </resultMap>
    <sql id="Query_Column_List_Accept_Carriage">
        a.id             AS tOrderId,
        a.status         AS status,
        a.t_order_no     AS tOrderNo,
        d.ent_id         AS procureEntId,
        d.firm_name      AS procureFirmName,
        e.ent_id         AS supplyEntId,
        e.firm_name      AS supplyFirmName,
        a.trading        AS trading,
        a.delivery_type  AS deliveryType,
        a.pickup_type    AS pickupType,
        a.source         AS source,
        f.contract_id    AS contractId,
        f.contract_no    AS contractNo,
        g.goods_id       AS goodsId,
        g.unit_price     AS unitPrice,
        a.price          AS price,
        a.amount         AS amount,
        a.entrust_amount AS entrustAmount,
        a.actual_amount  AS actualAmount,
        a.start_time     AS startTime,
        a.end_time       AS endTime,
        a.remark         AS remark,
        a.unit           AS unit,
        a.create_by      AS createBy,
        a.create_time    AS createTime
    </sql>

    <select id="tradeOrderSelect" resultMap="mainEntityMap">
        SELECT
        <include refid="Query_Column_List_Accept_Carriage">
        </include>
        FROM kwo_trade_order a
                 LEFT JOIN kwo_trade_order_unit d ON a.id = d.t_order_id AND d.unit_type = "1" AND d.del_flag = 0
                 LEFT JOIN kwo_trade_order_unit e ON a.id = e.t_order_id AND e.unit_type = "2" AND e.del_flag = 0
                 LEFT JOIN kwo_trade_order_goods g ON a.id = g.t_order_id AND g.del_flag = 0
                 LEFT JOIN kwo_trade_order_contract f ON a.id = f.t_order_id AND f.del_flag = 0
        <where>
            a.del_flag = 0
--          数据权限匹配
<!--            <choose>-->
<!--                <when test="entIds == null or entIds.size == 0">-->
<!--                    <if test="query.isMain == 0">-->
<!--                        and (-->
<!--                        <if test="query.orderType == 1">-->
<!--                            <if test="query.entId != null">-->
<!--                                    d.top_ent_id = #{query.entId}-->
<!--                            </if>-->
<!--                            <if test="authUserIds != null and authUserIds.size() > 0">-->
<!--                                and d.contacts_id in-->
<!--                                <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">-->
<!--                                    #{authUserId}-->
<!--                                </foreach>-->
<!--                                or d.create_by in-->
<!--                                <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">-->
<!--                                    #{authUserId}-->
<!--                                </foreach>-->
<!--                            </if>-->
<!--                        </if>-->
<!--                        <if test="query.orderType == 2">-->
<!--                            <if test="query.entId != null">-->
<!--                                e.top_ent_id = #{query.entId}-->
<!--                            </if>-->
<!--                            <if test="authUserIds != null and authUserIds.size() > 0">-->
<!--                                and e.contacts_id in-->
<!--                                <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">-->
<!--                                    #{authUserId}-->
<!--                                </foreach>-->
<!--                                or e.create_by in-->
<!--                                <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">-->
<!--                                    #{authUserId}-->
<!--                                </foreach>-->
<!--                            </if>-->
<!--                        </if>-->
<!--                        )-->
<!--                    </if>-->
<!--                    <if test="query.isMain == 1">-->
<!--                        and (-->
<!--                        <if test="query.orderType == 1">-->
<!--                            <if test="query.entId != null">-->
<!--                                d.top_ent_id = #{query.entId}-->
<!--                            </if>-->
<!--                        </if>-->
<!--                        <if test="query.orderType == 2">-->
<!--                            <if test="query.entId != null">-->
<!--                                e.top_ent_id = #{query.entId}-->
<!--                            </if>-->
<!--                        </if>-->
<!--                        )-->
<!--                    </if>-->
<!--                </when>-->
<!--                <otherwise>-->
<!--                    and a.ent_id in-->
<!--                    <foreach collection="entIds" separator="," open="(" close=")" item="item">-->
<!--                        #{item}-->
<!--                    </foreach>-->
<!--                </otherwise>-->
<!--            </choose>-->

            --           状态匹配
            <if test="query.status != null">
                and a.status = #{query.status}
                <if test="query.status == 0">
                    --          草稿状态只能创建订单的人能看
                      and a.create_by = #{query.userId}
                </if>
            </if>
            <if test="query.status == null">
                --           非草稿状态or草稿状态且是当前人
                  and (
                            a.status != 0
                        or (a.status = 0 and a.create_by = #{query.userId})
                    )
            </if>
            --          关键词匹配
            <if test="query.keywords != null and query.keywords != ''">
                and (
                        a.t_order_no like concat('%', #{query.keywords}, '%')
                    or d.firm_name like concat('%', #{query.keywords}, '%')
                    or e.firm_name like concat('%', #{query.keywords}, '%')
                <if test="goodIds != null and goodIds.size() > 0">
                    or g.goods_id in
                    <foreach collection="goodIds" item="goodId" open="(" close=")" separator=",">
                        #{goodId}
                    </foreach>
                </if>
                )
            </if>
            <if test="query.startCreateTime != null">
                and a.create_time &gt;= #{query.startCreateTime}
            </if>
            <if test="query.endCreateTime != null">
                and a.create_time &lt;= #{query.endCreateTime}
            </if>
            <if test="(query.loadAddressCode != null and query.loadAddressCode != '') or (query.unloadAddressCode != null and query.unloadAddressCode != '')">
                and a.id in (
                SELECT t_order_id
                from kwo_trade_order_address where del_flag = 0
                <if test="query.loadAddressCode != null and query.loadAddressCode != ''">
                    and address_type = 1
                    and left(city_code, #{query.loadAddressLevel, jdbcType=INTEGER}) =
                        left(#{query.loadAddressCode, jdbcType=INTEGER}, #{query.loadAddressLevel, jdbcType=INTEGER})
                </if>
                <if test="query.unloadAddressCode != null and query.unloadAddressCode != ''">
                    and address_type = 2
                    and left(city_code, #{query.unloadAddressLevel, jdbcType=INTEGER}) =
                        left(#{query.unloadAddressCode, jdbcType=INTEGER},
                             #{query.unloadAddressLevel, jdbcType=INTEGER})
                </if>
                )
            </if>
            <if test="query.trading != null and query.trading != ''">
                and a.trading = #{query.trading}
            </if>
            <if test="query.source != null and query.source != ''">
                and a.source = #{query.source}
            </if>
            <if test="query.deliveryType != null and query.deliveryType != ''">
                and a.delivery_type = #{query.deliveryType}
            </if>
            <if test="query.deliveryType != null and query.deliveryType != ''">
                and a.delivery_type = #{query.deliveryType}
            </if>
            <if test="query.pickupType != null and query.pickupType != ''">
                and a.pickup_type = #{query.pickupType}
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>

    <select id="tradeOrderExport" resultMap="mainEntityMap">
        SELECT
        <include refid="Query_Column_List_Accept_Carriage">
        </include>
        FROM kwo_trade_order a
                 LEFT JOIN kwo_trade_order_unit d ON a.id = d.t_order_id AND d.unit_type = "1" AND d.del_flag = 0
                 LEFT JOIN kwo_trade_order_unit e ON a.id = e.t_order_id AND e.unit_type = "2" AND e.del_flag = 0
                 LEFT JOIN kwo_trade_order_goods g ON a.id = g.t_order_id AND g.del_flag = 0
                 LEFT JOIN kwo_trade_order_contract f ON a.id = f.t_order_id AND f.del_flag = 0
        <where>
            a.del_flag = 0
--             数据权限匹配
            <if test="query.isMain == 0">
                and (
                <if test="query.orderType == 1">
                    <if test="query.entId != null">
                            d.top_ent_id = #{query.entId}
                    </if>
                    <if test="authUserIds != null and authUserIds.size() > 0">
                        and d.contacts_id in
                        <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">
                            #{authUserId}
                        </foreach>
                        or d.create_by in
                        <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">
                            #{authUserId}
                        </foreach>
                    </if>
                </if>
                <if test="query.orderType == 2">
                    <if test="query.entId != null">
                        e.top_ent_id = #{query.entId}
                    </if>
                    <if test="authUserIds != null and authUserIds.size() > 0">
                        and e.contacts_id in
                        <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">
                            #{authUserId}
                        </foreach>
                        or e.create_by in
                        <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">
                            #{authUserId}
                        </foreach>
                    </if>
                </if>
                )
            </if>
            <if test="query.isMain == 1">
                and (
                <if test="query.orderType == 1">
                    <if test="query.entId != null">
                        d.top_ent_id = #{query.entId}
                    </if>
                </if>
                <if test="query.orderType == 2">
                    <if test="query.entId != null">
                        e.top_ent_id = #{query.entId}
                    </if>
                </if>
                )
            </if>
            --             状态匹配
            <if test="query.status != null">
                and a.status = #{query.status}
                <if test="query.status == 0">
                    --                     草稿状态只能创建订单的人能看
                      and a.create_by = #{query.userId}
                </if>
            </if>
            <if test="query.status == null">
                --                 非草稿状态or草稿状态且是当前人
                  and (
                            a.status != 0
                        or (a.status = 0 and a.create_by = #{query.userId})
                    )
            </if>
            --             关键词匹配
            <if test="query.keywords != null and query.keywords != ''">
                and (
                        a.t_order_no like concat('%', #{query.keywords}, '%')
                    or d.firm_name like concat('%', #{query.keywords}, '%')
                    or e.firm_name like concat('%', #{query.keywords}, '%')
                <if test="goodIds != null and goodIds.size() > 0">
                    or g.goods_id in
                    <foreach collection="goodIds" item="goodId" open="(" close=")" separator=",">
                        #{goodId}
                    </foreach>
                </if>
                )
            </if>
            <if test="query.startCreateTime != null">
                and a.create_time &gt;= #{query.startCreateTime}
            </if>
            <if test="query.endCreateTime != null">
                and a.create_time &lt;= #{query.endCreateTime}
            </if>
            <if test="(query.loadAddressCode != null and query.loadAddressCode != '') or (query.unloadAddressCode != null and query.unloadAddressCode != '')">
                and a.id in (
                SELECT t_order_id
                from kwo_trade_order_address where del_flag = 0
                <if test="query.loadAddressCode != null and query.loadAddressCode != ''">
                    and address_type = 1
                    and left(city_code, #{query.loadAddressLevel, jdbcType=INTEGER}) =
                        left(#{query.loadAddressCode, jdbcType=INTEGER}, #{query.loadAddressLevel, jdbcType=INTEGER})
                </if>
                <if test="query.unloadAddressCode != null and query.unloadAddressCode != ''">
                    and address_type = 2
                    and left(city_code, #{query.unloadAddressLevel, jdbcType=INTEGER}) =
                        left(#{query.unloadAddressCode, jdbcType=INTEGER},
                             #{query.unloadAddressLevel, jdbcType=INTEGER})
                </if>
                )
            </if>
            <if test="query.trading != null and query.trading != ''">
                and a.trading = #{query.trading}
            </if>
            <if test="query.source != null and query.source != ''">
                and a.source = #{query.source}
            </if>
            <if test="query.deliveryType != null and query.deliveryType != ''">
                and a.delivery_type = #{query.deliveryType}
            </if>
            <if test="query.deliveryType != null and query.deliveryType != ''">
                and a.delivery_type = #{query.deliveryType}
            </if>
            --          指定贸易订单id导出
            <if test="ids != null and ids.size() > 0">
                and a.id in
                <foreach collection="ids" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
                <if test="query.orderType == 1">
                    <if test="query.entId != null">
                        and d.top_ent_id = #{query.entId}
                    </if>
                </if>
                <if test="query.orderType == 2">
                    <if test="query.entId != null">
                        and e.top_ent_id = #{query.entId}
                    </if>
                </if>
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>

    <select id="tradeOrderStatementList" resultMap="mainEntityMap">
        SELECT
        <include refid="Query_Column_List_Accept_Carriage">
        </include>
        FROM kwo_trade_order a
                 LEFT JOIN kwo_trade_order_address b ON a.id = b.t_order_id
            AND b.address_type = "1" AND b.del_flag = 0
                 LEFT JOIN kwo_trade_order_address c ON a.id = c.t_order_id
            AND c.address_type = "2" AND c.del_flag = 0
                 LEFT JOIN kwo_trade_order_unit d ON a.id = d.t_order_id
            AND d.unit_type = "1" AND d.del_flag = 0
                 LEFT JOIN kwo_trade_order_unit e ON a.id = e.t_order_id
            AND e.unit_type = "2" AND e.del_flag = 0
                 LEFT JOIN kwo_trade_order_goods g ON a.id = g.t_order_id AND g.del_flag = 0
                 LEFT JOIN kwo_trade_order_contract f ON a.id = f.t_order_id AND f.del_flag = 0
        <where>
            a.status = 5
              and a.associate_statement <![CDATA[ <> ]]> 1
              and a.del_flag = 0
            <if test="query.orderType == 1">
                <if test="query.entId != null">
                    and d.top_ent_id = #{query.entId}
                </if>
            </if>
            <if test="query.orderType == 2">
                <if test="query.entId != null">
                    and e.top_ent_id = #{query.entId}
                </if>
            </if>
            <if test="excludeIds != null and excludeIds.size() > 0">
                and a.id not in
                <foreach collection="excludeIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="goodIds != null and goodIds.size() > 0">
                and g.goods_id in
                <foreach collection="goodIds" item="goodId" open="(" close=")" separator=",">
                    #{goodId}
                </foreach>
            </if>
            <if test="query.procureEntId != null">
                and d.ent_id = #{query.procureEntId}
            </if>
            <if test="query.trading != null and query.trading != ''">
                and a.trading = #{query.trading}
            </if>
            <if test="query.startTime != null">
                and a.start_time &gt;= #{query.startTime}
            </if>
            <if test="query.endTime != null">
                and a.end_time &lt;= #{query.endTime}
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>

    <select id="tradeOrderContractList" resultMap="mainEntityMap">
        SELECT
        <include refid="Query_Column_List_Accept_Carriage">
        </include>
        FROM kwo_trade_order a
                 LEFT JOIN kwo_trade_order_address b ON a.id = b.t_order_id
            AND b.address_type = "1" AND b.del_flag = 0
                 LEFT JOIN kwo_trade_order_address c ON a.id = c.t_order_id
            AND c.address_type = "2" AND c.del_flag = 0
                 LEFT JOIN kwo_trade_order_unit d ON a.id = d.t_order_id
            AND d.unit_type = "1" AND d.del_flag = 0
                 LEFT JOIN kwo_trade_order_unit e ON a.id = e.t_order_id
            AND e.unit_type = "2" AND e.del_flag = 0
                 LEFT JOIN kwo_trade_order_goods g ON a.id = g.t_order_id AND g.del_flag = 0
                 LEFT JOIN kwo_trade_order_contract f ON a.id = f.t_order_id AND f.del_flag = 0
        <where>
            a.del_flag = 0
            <if test="query.orderType == 1">
                <if test="query.entId != null">
                    and d.top_ent_id = #{query.entId}
                </if>
            </if>
            <if test="query.orderType == 2">
                <if test="query.entId != null">
                    and e.top_ent_id = #{query.entId}
                </if>
            </if>
            <if test="contractIds != null and contractIds.size() > 0">
                and f.contract_id in
                <foreach collection="contractIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY f.contract_id, a.create_time DESC
    </select>

    <select id="workbenchPurchaseList" resultType="com.sckw.order.model.dto.WorkbenchPurchaseDTO">
        SELECT a.id          as tOrderId,
               a.amount      as amount,
               a.unit        as unit,
               a.create_time as createTime,
               g.goods_id    as goodsId,
               d.firm_name   as procureFirmName
        FROM kwo_trade_order a
                 LEFT JOIN kwo_trade_order_unit d ON a.id = d.t_order_id
            AND d.unit_type = "1" AND d.del_flag = 0
                 LEFT JOIN kwo_trade_order_goods g ON a.id = g.t_order_id AND g.del_flag = 0
        <where>
            a.del_flag = 0
            <if test="procureTopEntId != null">
                and d.top_ent_id = #{procureTopEntId}
            </if>
            <if test="excludeStatuses != null and excludeStatuses.size() > 0">
                and a.status not in
                <foreach collection="excludeStatuses" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY a.create_time DESC
        LIMIT 10;
    </select>

    <select id="tradeOrderStatistic" resultType="com.sckw.core.model.vo.TableTop">
        SELECT a.status AS `value`, COUNT(a.status) AS total
        FROM kwo_trade_order a
                 LEFT JOIN kwo_trade_order_unit d ON a.id = d.t_order_id AND d.unit_type = "1" AND d.del_flag = 0
                 LEFT JOIN kwo_trade_order_unit e ON a.id = e.t_order_id AND e.unit_type = "2" AND e.del_flag = 0
                 LEFT JOIN kwo_trade_order_goods g ON a.id = g.t_order_id AND g.del_flag = 0
        <where>
            a.del_flag = 0
--             数据权限匹配
            <if test="query.isMain == 0">
                and (
                <if test="query.orderType == 1">
                    <if test="query.entId != null">
                            d.top_ent_id = #{query.entId}
                    </if>
                    <if test="authUserIds != null and authUserIds.size() > 0">
                        and d.contacts_id in
                        <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">
                            #{authUserId}
                        </foreach>
                        or d.create_by in
                        <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">
                            #{authUserId}
                        </foreach>
                    </if>
                </if>
                <if test="query.orderType == 2">
                    <if test="query.entId != null">
                        e.top_ent_id = #{query.entId}
                    </if>
                    <if test="authUserIds != null and authUserIds.size() > 0">
                        and e.contacts_id in
                        <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">
                            #{authUserId}
                        </foreach>
                        or e.create_by in
                        <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">
                            #{authUserId}
                        </foreach>
                    </if>
                </if>
                )
            </if>
            <if test="query.isMain == 1">
                and (
                <if test="query.orderType == 1">
                    <if test="query.entId != null">
                        d.top_ent_id = #{query.entId}
                    </if>
                </if>
                <if test="query.orderType == 2">
                    <if test="query.entId != null">
                        e.top_ent_id = #{query.entId}
                    </if>
                </if>
                )
            </if>
            --             状态匹配
            <if test="query.status != null">
                and a.status = #{query.status}
                <if test="query.status == 0">
                    --                     草稿状态只能创建订单的人能看
                      and a.create_by = #{query.userId}
                </if>
            </if>
            <if test="query.status == null">
                --                 非草稿状态or草稿状态且是当前人
                  and (
                            a.status != 0
                        or (a.status = 0 and a.create_by = #{query.userId})
                    )
            </if>
            --                      关键词匹配
            <if test="query.keywords != null and query.keywords != ''">
                and (
                        a.t_order_no like concat('%', #{query.keywords}, '%')
                    or d.firm_name like concat('%', #{query.keywords}, '%')
                    or e.firm_name like concat('%', #{query.keywords}, '%')
                <if test="goodIds != null and goodIds.size() > 0">
                    or g.goods_id in
                    <foreach collection="goodIds" item="goodId" open="(" close=")" separator=",">
                        #{goodId}
                    </foreach>
                </if>
                )
            </if>
            <if test="query.startCreateTime != null">
                and a.create_time &gt;= #{query.startCreateTime}
            </if>
            <if test="query.endCreateTime != null">
                and a.create_time &lt;= #{query.endCreateTime}
            </if>
            <if test="(query.loadAddressCode != null and query.loadAddressCode != '') or (query.unloadAddressCode != null and query.unloadAddressCode != '')">
                and a.id in (
                SELECT t_order_id
                from kwo_trade_order_address where del_flag = 0
                <if test="query.loadAddressCode != null and query.loadAddressCode != ''">
                    and address_type = 1
                    and left(city_code, #{query.loadAddressLevel, jdbcType=INTEGER}) =
                        left(#{query.loadAddressCode, jdbcType=INTEGER}, #{query.loadAddressLevel, jdbcType=INTEGER})
                </if>
                <if test="query.unloadAddressCode != null and query.unloadAddressCode != ''">
                    and address_type = 2
                    and left(city_code, #{query.unloadAddressLevel, jdbcType=INTEGER}) =
                        left(#{query.unloadAddressCode, jdbcType=INTEGER},
                             #{query.unloadAddressLevel, jdbcType=INTEGER})
                </if>
                )
            </if>
            <if test="query.trading != null and query.trading != ''">
                and a.trading = #{query.trading}
            </if>
            <if test="query.source != null and query.source != ''">
                and a.source = #{query.source}
            </if>
            <if test="query.deliveryType != null and query.deliveryType != ''">
                and a.delivery_type = #{query.deliveryType}
            </if>
            <if test="query.deliveryType != null and query.deliveryType != ''">
                and a.delivery_type = #{query.deliveryType}
            </if>
        </where>
        GROUP BY a.status
    </select>

    <select id="appStatistic" resultType="com.sckw.order.model.vo.res.TradeOrderAppStatisticVO">
        SELECT a.status AS `value`, COUNT(a.status) AS total
        FROM kwo_trade_order a
                 LEFT JOIN kwo_trade_order_unit d ON a.id = d.t_order_id AND d.unit_type = "1" AND d.del_flag = 0
                 LEFT JOIN kwo_trade_order_unit e ON a.id = e.t_order_id AND e.unit_type = "2" AND e.del_flag = 0
        <where>
            a.del_flag = 0
--             数据权限匹配
            <if test="query.isMain == 0">
                and (
                <if test="query.orderType == 1">
                    <if test="query.entId != null">
                            d.top_ent_id = #{query.entId}
                    </if>
                    <if test="authUserIds != null and authUserIds.size() > 0">
                        and d.contacts_id in
                        <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">
                            #{authUserId}
                        </foreach>
                    </if>
                </if>
                <if test="query.orderType == 2">
                    <if test="query.entId != null">
                        e.top_ent_id = #{query.entId}
                    </if>
                    <if test="authUserIds != null and authUserIds.size() > 0">
                        and e.contacts_id in
                        <foreach collection="authUserIds" item="authUserId" open="(" close=")" separator=",">
                            #{authUserId}
                        </foreach>
                    </if>
                </if>
                )
            </if>
            <if test="query.isMain == 1">
                and (
                <if test="query.orderType == 1">
                    <if test="query.entId != null">
                        d.top_ent_id = #{query.entId}
                    </if>
                </if>
                <if test="query.orderType == 2">
                    <if test="query.entId != null">
                        e.top_ent_id = #{query.entId}
                    </if>
                </if>
                )
            </if>
            <if test="query.orderType == 1">
                <if test="query.entId != null">
                    and d.top_ent_id = #{query.entId}
                </if>
            </if>
            <if test="query.orderType == 2">
                <if test="query.entId != null">
                    and e.top_ent_id = #{query.entId}
                </if>
            </if>
            --             状态匹配
            <if test="statuses != null and statuses.size() > 0">
                and a.status in
                <foreach collection="statuses" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY a.status
    </select>

    <select id="getOrderStsInfo" resultType="com.sckw.order.model.dto.WorkbenchOrderStsDTO">
        SELECT SUM(a.amount)        AS orderAmount,
               SUM(a.price)         AS orderPrice,
               SUM(a.actual_amount) AS orderActualAmount
        FROM kwo_trade_order a
                 LEFT JOIN kwo_trade_order_unit d ON a.id = d.t_order_id
            AND d.unit_type = "1" AND d.del_flag = 0
                 LEFT JOIN kwo_trade_order_unit e ON a.id = e.t_order_id
            AND e.unit_type = "2" AND e.del_flag = 0
        <where>
            a.del_flag = 0
            <if test="query.unit != null and query.unit != ''">
                and a.unit = #{query.unit}
            </if>
            <if test="query.startCreateTime != null">
                and a.create_time &gt;= #{query.startCreateTime}
            </if>
            <if test="query.endCreateTime != null">
                and a.create_time &lt;= #{query.endCreateTime}
            </if>
            <if test="excludeStatuses != null and excludeStatuses.size() > 0">
                and a.status not in
                <foreach collection="excludeStatuses" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="query.orderType == 1">
                <if test="query.entId != null">
                    and d.top_ent_id = #{query.entId}
                </if>
            </if>
            <if test="query.orderType == 2">
                <if test="query.entId != null">
                    and e.top_ent_id = #{query.entId}
                </if>
            </if>
        </where>
    </select>

    <select id="getMaxOrderNoByDate" resultType="java.lang.String">
        SELECT MAX(t_order_no)
        FROM kwo_trade_order
        WHERE create_time &gt;= #{date};
    </select>

    <select id="getOrderNumByTopEntId" resultType="com.sckw.order.api.model.TradeOrderCountStatisticsDTO">
        SELECT u.unit_type AS orderType,
               COUNT(*)    AS num
        FROM kwo_trade_order o
                 LEFT JOIN kwo_trade_order_unit u
                           ON o.id = u.t_order_id
        <where>
            o.del_flag = 0
              AND o.status NOT IN (0, 2)
              AND u.del_flag = 0
            <if test="topEntId != null">
                and u.top_ent_id = #{topEntId}
            </if>
            <if test="enterpriseIds != null and enterpriseIds.size() > 0">
                and o.ent_id in
                <foreach collection="enterpriseIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY u.unit_type
    </select>

    <select id="workbenchExcel" resultType="com.sckw.order.model.dto.WbTOrderExcelStsResDTO">
        SELECT DATE_FORMAT(create_time, #{item.dateFormat}) AS formatDate,
               SUM(CASE
                       WHEN #{item.stsFactorsOne} = 1 THEN price
                       WHEN #{item.stsFactorsOne} = 2 THEN 1
                       ELSE 0
                   END)                                     AS stsFactorsOneValue,
               SUM(CASE
                       WHEN #{item.stsFactorsTwo} = 1 THEN price
                       WHEN #{item.stsFactorsTwo} = 2 THEN 1
                       ELSE 0
                   END)                                     AS stsFactorsTwoValue
        FROM kwo_trade_order
        WHERE id IN (SELECT t_order_id
                     FROM kwo_trade_order_unit
                     WHERE del_flag = 0
                       AND unit_type = #{item.unitType}
                       AND top_ent_id = #{item.topEntId})
          AND create_time >= #{item.startTime}
          AND status NOT IN (0, 2)
          AND del_flag = 0
        GROUP BY DATE_FORMAT(create_time, #{item.dateFormat});
    </select>

    <select id="selectGoodsId" resultType="java.lang.Long">
        select distinct ktog.goods_id
        from kwo_trade_order kto
                 left join kwo_trade_order_goods ktog on kto.id = ktog.t_order_id and ktog.del_flag = 0
        where kto.del_flag = 0
          and kto.ent_id = #{entId}
    </select>

    <select id="querySaleOrder" resultType="com.sckw.order.api.model.OrderSaleVo">
        select ktog.goods_id,
               sum(ifnull(kto.amount, 0)) as amount
        from kwo_trade_order kto
                 left join kwo_trade_order_goods ktog on kto.id = ktog.t_order_id and ktog.del_flag = 0
        <where>
            kto.del_flag = 0
            <if test="start != null and end != null">
                and kto.create_time between #{start} and #{end}
            </if>
            group by ktog.goods_id
            order by amount desc
        </where>
    </select>
</mapper>
