<?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="chargeType" property="chargeType"/>
        <result column="unloadAmount" property="unloadAmount"/>
        <result column="loadAmount" property="loadAmount"/>
        <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"/>
        <result column="addVehicles" property="addVehicles"/>

        <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.charge_type    AS chargeType,
        a.load_amount    AS loadAmount,
        a.unload_amount  AS unloadAmount,
        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,
        a.add_vehicles   AS addVehicles
    </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="query.manager != null and !query.manager">
                    <if test="query.isMain == 1">
                        <if test="query.entList != null and query.entList.size() > 0  and query.entType != null">
                            <if test="query.entType == 1">
                                and
                                    e.top_ent_id in
                                <foreach collection="query.entList" item="authUserId" open="(" close=")" separator=",">
                                    #{authUserId}
                                </foreach>
                            </if>
                            <if test="query.entType == 2">
                                and
                                    d.top_ent_id in
                                <foreach collection="query.entList" item="authUserId" open="(" close=")" separator=",">
                                    #{authUserId}
                                </foreach>
                            </if>
                        </if>
                    </if>
                    <if test="query.isMain == 0">
                        <if test="query.entId != null and query.entType != null">
                            <if test="query.entType == 1">
                                and e.top_ent_id = #{query.entId}
                            </if>
                            <if test="query.entType == 2">
                                and d.top_ent_id = #{query.entId}
                            </if>
                        </if>
                    </if>
                </when>
            </choose>
            <if test="query.buyEntId != null">
                and d.ent_id = #{query.buyEntId}
            </if>
            <if test="query.contractId != null">
                and f.contract_id = #{query.contractId}
            </if>
<!--            <if test="query.dataPermissionForceEmpty != null and query.dataPermissionForceEmpty">-->
<!--                and 1 = 0-->
<!--            </if>-->
<!--            <if test="query.dataPermissionFilter != null and query.dataPermissionFilter">-->
<!--                and exists (-->
<!--                    select 1-->
<!--                    from kwc_contract_trade ct-->
<!--                    where ct.id = f.contract_id-->
<!--                      and ct.del_flag = 0-->
<!--                    <if test="query.dataPermissionAllVisible != null and !query.dataPermissionAllVisible">-->
<!--                        and ct.ent_id in-->
<!--                        <foreach collection="query.dataPermissionEntIds" item="entId" open="(" close=")" separator=",">-->
<!--                            #{entId}-->
<!--                        </foreach>-->
<!--                    </if>-->
<!--                    <if test="query.dataPermissionPersonal != null and query.dataPermissionPersonal">-->
<!--                        and ct.salesman_id = #{query.dataPermissionUserId}-->
<!--                    </if>-->
<!--                )-->
<!--            </if>-->
            <if test="query.saleEntId != null">
                and e.ent_id = #{query.saleEntId}
            </if>
            <if test="query.status != null">
                and a.status = #{query.status}
            </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
                 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="query.manager != null and !query.manager">
                    <if test="query.isMain == 1">
                        <if test="query.entList != null and query.entList.size() > 0  and query.entType != null">
                            <if test="query.entType == 1">
                                and
                                    e.top_ent_id in
                                <foreach collection="query.entList" item="authUserId" open="(" close=")" separator=",">
                                    #{authUserId}
                                </foreach>
                            </if>
                            <if test="query.entType == 2">
                                and
                                    d.top_ent_id in
                                <foreach collection="query.entList" item="authUserId" open="(" close=")" separator=",">
                                    #{authUserId}
                                </foreach>
                            </if>
                        </if>
                    </if>
                    <if test="query.isMain == 0">
                        <if test="query.entId != null and query.entType != null">
                            <if test="query.entType == 1">
                                and e.top_ent_id = #{query.entId}
                            </if>
                            <if test="query.entType == 2">
                                and d.top_ent_id = #{query.entId}
                            </if>
                        </if>
                    </if>
                </when>
            </choose>
            <if test="query.buyEntId != null">
                and d.ent_id = #{query.buyEntId}
            </if>
            <if test="query.saleEntId != null">
                and e.ent_id = #{query.saleEntId}
            </if>
<!--            <if test="query.dataPermissionForceEmpty != null and query.dataPermissionForceEmpty">-->
<!--                and 1 = 0-->
<!--            </if>-->
<!--            <if test="query.dataPermissionFilter != null and query.dataPermissionFilter">-->
<!--                and exists (-->
<!--                    select 1-->
<!--                    from kwc_contract_trade ct-->
<!--                    where ct.id = f.contract_id-->
<!--                      and ct.del_flag = 0-->
<!--                    <if test="query.dataPermissionAllVisible != null and !query.dataPermissionAllVisible">-->
<!--                        and ct.ent_id in-->
<!--                        <foreach collection="query.dataPermissionEntIds" item="entId" open="(" close=")" separator=",">-->
<!--                            #{entId}-->
<!--                        </foreach>-->
<!--                    </if>-->
<!--                    <if test="query.dataPermissionPersonal != null and query.dataPermissionPersonal">-->
<!--                        and ct.salesman_id = #{query.dataPermissionUserId}-->
<!--                    </if>-->
<!--                )-->
<!--            </if>-->
            <if test="query.status != null">
                and a.status = #{query.status}
            </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(0)    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="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>

    <select id="selectData" resultType="com.sckw.order.api.model.TradeOrderVo">
        select a.status,
               a.id         tOrderId,
               a.t_order_no tOrderNo,
               d.firm_name  buyEntName,
               e.firm_name  sellEntName,
               ktog.goods_name,
               ktog.unit_price,
               ktog.unit,
               ktog.goods_type,
               a.amount,
               a.price,
               a.load_amount,
               a.unload_amount,
               a.charge_type,
               a.create_time,
               a.add_vehicles
        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 ktog on a.id = ktog.t_order_id and ktog.del_flag = 0
        <where>
            a.del_flag = 0
            <if test="para.startTime != null and para.endTime != null">
                and a.create_time between #{para.startTime,jdbcType=TIMESTAMP} and #{para.endTime,jdbcType=TIMESTAMP}
            </if>
            <if test="para.orderNo != null and para.orderNo != ''">
                and a.t_order_no = #{para.orderNo}
            </if>
            <if test="para.entId != null and para.entId.size() > 0">
                <if test="para.type != null and para.type == 1">
                    and
                        e.ent_id in
                    <foreach collection="para.entId" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </if>
                <if test="para.type != null and para.type == 2">
                    and d.ent_id in
                    <foreach collection="para.entId" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </if>
            </if>
            limit 10
        </where>
    </select>

    <select id="queryCount" resultType="java.lang.Long">
        select count(1)
        from kwo_trade_order kto
                 inner JOIN kwo_trade_order_unit d ON kto.id = d.t_order_id AND d.unit_type = '2' AND d.del_flag = 0
        <where>
            kto.del_flag = 0
              and kto.status = 1
            <if test="ids != null and ids.size() != 0">
                and d.ent_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="queryList" resultType="com.sckw.order.api.model.TradeVo">
        select kto.id,
               kto.create_time,
               kto.status,
               kto.amount,
               ktog.unit_price,
               ktog.goods_name,
               kto.t_order_no,
               c.firm_name
        from kwo_trade_order kto
                 inner JOIN kwo_trade_order_unit d ON kto.id = d.t_order_id AND d.unit_type = '2' AND d.del_flag = 0
                 inner JOIN kwo_trade_order_unit c ON kto.id = c.t_order_id AND c.unit_type = '1' AND c.del_flag = 0
                 inner 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.status = 1
            <if test="ids != null and ids.size() != 0">
                and d.ent_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            order by kto.create_time desc
            <if test="all != null and all">
                limit 10
            </if>
        </where>
    </select>

    <select id="querySalesReportRawData" resultType="com.sckw.order.model.SalesReportRawData">
        select d.ent_id as customerId,
               d.firm_name as customerName,
               ktog.goods_id as goodsId,
               kto.amount as amount,
               kto.price as price,
               DATE_FORMAT(kto.create_time, '%Y-%m-%d %H:%i:%S') as createTime
        from kwo_trade_order kto
                 inner join kwo_trade_order_unit d on kto.id = d.t_order_id and d.unit_type = '1' and d.del_flag = 0
                 inner join kwo_trade_order_unit e on kto.id = e.t_order_id and e.unit_type = '2' and e.del_flag = 0
                 inner join kwo_trade_order_goods ktog on kto.id = ktog.t_order_id and ktog.del_flag = 0
        where kto.del_flag = 0
          and e.ent_id = #{curEntId}
          <if test="customerId != null">
              and d.ent_id = #{customerId}
          </if>
          <if test="goodsId != null">
              and ktog.goods_id = #{goodsId}
          </if>
    </select>

    <select id="countList" resultType="com.sckw.order.api.model.TradeSaleVo">
        select ktog.goods_id,
               CONCAT(
                       LPAD(HOUR(kto.create_time), 2, '0'),
                       ':00-',
                       LPAD(MOD(HOUR(kto.create_time) + 1, 24), 2, '0'),
                       ':00'
                   )           AS time_period,
               sum(kto.amount) as amount
        from kwo_trade_order kto
                 inner JOIN kwo_trade_order_unit d ON kto.id = d.t_order_id AND d.unit_type = '2' AND d.del_flag = 0
                 inner JOIN kwo_trade_order_unit c ON kto.id = c.t_order_id AND c.unit_type = '1' AND c.del_flag = 0
                 inner 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.status in (1, 2, 3, 4)
            <if test="ids != null and ids.size() != 0">
                and d.ent_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            and kto.create_time between #{para.startTime} and #{para.endTime}
            group by time_period, ktog.goods_id
            order by time_period
        </where>
    </select>

    <select id="countSale" resultType="com.sckw.order.api.model.SaleVo">
        select DATE_FORMAT(kto.create_time, '%Y-%m-%d') as order_day,
               count(kto.id)                               count,
               sum(kto.price)                           as price
        from kwo_trade_order kto
                 inner JOIN kwo_trade_order_unit d
                            ON kto.id = d.t_order_id AND d.unit_type = '2' AND d.del_flag = 0
                 inner JOIN kwo_trade_order_unit c ON kto.id = c.t_order_id AND c.unit_type = '1' AND c.del_flag = 0
        <where>
            kto.del_flag = 0
              and kto.status in (1, 2, 3, 4)
            <if test="ids != null and ids.size() != 0">
                and d.ent_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            and kto.create_time between #{para.startTime}
                and #{para.endTime}
            group by order_day
        </where>
    </select>

    <select id="countBuy" resultType="com.sckw.order.api.model.BuyVo">
        select sum(kto.price) as price,
               c.firm_name,
               c.ent_id
        from kwo_trade_order kto
                 inner JOIN kwo_trade_order_unit d
                            ON kto.id = d.t_order_id AND d.unit_type = '2' AND d.del_flag = 0
                 inner JOIN kwo_trade_order_unit c ON kto.id = c.t_order_id AND c.unit_type = '1' AND c.del_flag = 0
        <where>
            kto.del_flag = 0
              and kto.status in (1, 2, 3, 4)
            <if test="ids != null and ids.size() != 0">
                and d.ent_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            and kto.create_time between #{para.startTime}
                and #{para.endTime}
            group by c.ent_id, c.firm_name
            order by price desc
            limit 5
        </where>
    </select>

    <select id="countGoods" resultType="com.sckw.order.api.model.GoodsVo">
        select sum(kto.amount), ktog.goods_name, ktog.goods_id
        from kwo_trade_order kto
                 inner JOIN kwo_trade_order_unit d ON kto.id = d.t_order_id AND d.unit_type = '2' AND d.del_flag = 0
                 inner 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.status in (1, 2, 3, 4)
            <if test="ids != null and ids.size() != 0">
                and d.ent_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            and kto.create_time between #{para.startTime} and #{para.endTime}
            group by ktog.goods_name, ktog.goods_id
        </where>
    </select>

    <select id="buyRank" resultType="com.sckw.order.api.model.TradeBuyVo">
        select sum(kto.amount) as amount,
               c.firm_name     as firmName
        from kwo_trade_order kto
                 inner JOIN kwo_trade_order_unit d ON kto.id = d.t_order_id AND d.unit_type = '2' AND d.del_flag = 0
                 inner JOIN kwo_trade_order_unit c ON kto.id = c.t_order_id AND c.unit_type = '1' AND c.del_flag = 0
                 inner 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.status in (1, 2, 3, 4)
            <if test="ids != null and ids.size() != 0">
                and d.ent_id in
                <foreach collection="ids" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            and kto.create_time between #{para.startTime} and #{para.endTime}
            group by c.ent_id, c.firm_name
            order by amount desc
            limit 5
        </where>
    </select>

    <select id="saleTop" resultType="com.sckw.order.model.vo.SaleTopVo">
        select ROW_NUMBER() OVER (ORDER BY amount DESC) AS sort,
               sum(kto.price)                           as amount,
               d.firm_name                                 entName
        from kwo_trade_order kto
                 inner JOIN kwo_trade_order_unit d ON kto.id = d.t_order_id AND d.unit_type = '1' AND d.del_flag = 0
        <where>
            kto.del_flag = 0
              and kto.status = 4
            group by kto.id,
                     d.firm_name
        </where>
    </select>

    <select id="saleSum" resultType="java.math.BigDecimal">
        select
        sum(kto.price)                           as amount
        from kwo_trade_order kto
        <where>
            kto.del_flag = 0
            and kto.status = 4
        </where>
    </select>

    <select id="joinSelect" resultType="com.sckw.order.api.model.TradeOrderDto">
        select kto.id,
               ktog.goods_id,
               kto.amount,
               ktog.unit_price
        from kwo_trade_order kto
                 inner 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.archive_flag != 1
              and kto.status = 4
            limit 20
        </where>
    </select>

    <select id="selectDailyShipmentByCategory" resultType="com.sckw.order.model.dto.DailyShipmentStatisticsDTO">
        select DATE_FORMAT(kto.create_time, '%Y-%m-%d') as orderDate,
               ktog.goods_type as goodsType,
               SUM(kto.actual_amount) as actualAmount
        from kwo_trade_order kto
                 inner join kwo_trade_order_goods ktog on kto.id = ktog.t_order_id and ktog.del_flag = 0
                 inner join kwo_trade_order_unit ku on kto.id = ku.t_order_id and ku.unit_type = '2' and ku.del_flag = 0
        where kto.del_flag = 0
          and kto.status = 5
          and ku.top_ent_id = #{entId}
          and kto.create_time >= #{startTime}
          and kto.create_time &lt;= #{endTime}
        group by DATE_FORMAT(kto.create_time, '%Y-%m-%d'), ktog.goods_type
        order by orderDate, goodsType;
    </select>

    <select id="selectDailySalesByCategory" resultType="com.sckw.order.model.dto.DailySalesStatisticsDTO">
        select DATE_FORMAT(kto.create_time, '%Y-%m-%d') as orderDate,
               ktog.goods_type as goodsType,
               SUM(kto.price) as salesAmount
        from kwo_trade_order kto
                 inner join kwo_trade_order_goods ktog on kto.id = ktog.t_order_id and ktog.del_flag = 0
                 inner join kwo_trade_order_unit ku on kto.id = ku.t_order_id and ku.unit_type = '2' and ku.del_flag = 0
        where kto.del_flag = 0
          and kto.status = 5
          and ku.top_ent_id = #{entId}
          and kto.create_time >= #{startTime}
          and kto.create_time &lt;= #{endTime}
        group by DATE_FORMAT(kto.create_time, '%Y-%m-%d'), ktog.goods_type
        order by orderDate, goodsType;
    </select>

    <select id="selectGoodsVolumeTop5" resultType="com.sckw.order.model.dto.GoodsVolumeStatisticsDTO">
        select ktog.goods_name as goodsName,
               SUM(kto.actual_amount) as totalVolume
        from kwo_trade_order kto
                 inner join kwo_trade_order_goods ktog on kto.id = ktog.t_order_id and ktog.del_flag = 0
                 inner join kwo_trade_order_unit ku on kto.id = ku.t_order_id and ku.unit_type = '2' and ku.del_flag = 0
        where kto.del_flag = 0
          and kto.status = 5
          and ku.top_ent_id = #{entId}
        group by ktog.goods_name
        order by totalVolume desc
        limit 5;
    </select>

    <select id="selectGoodsAmountTop5" resultType="com.sckw.order.model.dto.GoodsAmountStatisticsDTO">
        select ktog.goods_name as goodsName,
               SUM(kto.price) as totalAmount
        from kwo_trade_order kto
                 inner join kwo_trade_order_goods ktog on kto.id = ktog.t_order_id and ktog.del_flag = 0
                 inner join kwo_trade_order_unit ku on kto.id = ku.t_order_id and ku.unit_type = '2' and ku.del_flag = 0
        where kto.del_flag = 0
          and kto.status = 5
          and ku.top_ent_id = #{entId}
        group by ktog.goods_name
        order by totalAmount desc
        limit 5;
    </select>
</mapper>
