<?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.usual.mapper.OrderGeneralTransportMapper">

    <sql id="orderListWhere">
        ko.del_flag = 0
        <if test="orderListParam.startTime != null">
            and ko.create_time &gt;= #{orderListParam.startTime,jdbcType=TIMESTAMP}
        </if>
        <if test="orderListParam.endTime != null">
            and ko.create_time &lt;= #{orderListParam.endTime,jdbcType=TIMESTAMP}
        </if>
        <if test="orderListParam.orderNo != null and orderListParam.orderNo != ''">
            and (ko.`order_no` like concat('%', #{orderListParam.orderNo,jdbcType=VARCHAR}, '%'))
        </if>
        <if test="orderListParam.status != null">
            and ko.status = #{orderListParam.status}
        </if>
        <if test="orderListParam.owCompany != null and orderListParam.owCompany != ''">
            and ko.cy_company_id = #{owCompany}
        </if>
        <if test="orderListParam.orderSource != null">
            and ko.order_source = #{orderListParam.orderSource}
        </if>
        <if test="orderListParam.typeHandling != null">
            and ko.type_handling = #{orderListParam.typeHandling}
        </if>
        <if test="orderListParam.tenantId != null and orderListParam.tenantId != ''">
            and ko.tenant_id = #{orderListParam.tenantId}
        </if>
    </sql>


    <select id="orderList" parameterType="com.sckw.usual.entity.req.OrderListParam" resultType="com.sckw.usual.entity.res.KwOrderRes">
        select *
        from kw_order ko
        <where>
            <include refid="orderListWhere" />
        </where>
        order by ko.create_time desc
    </select>

    <select id="orderDetailbyId" resultType="com.sckw.usual.entity.res.KwOrderDetailRes">
        select
<!--            header-->
            o.id,
            o.order_source orderSource,
            o.order_no orderNo,
            o.create_time createTime,
            o.create_by createBy,
            o.status,
            o.pid,
            o.type_handling typeHandling,
<!--            企业信息-->
            o.ty_company_id tyCompanId,
            o.ty_company tyCompany,
            o.ty_phone tyPhone,
            o.cy_company_id cyCompanyId,
            o.cy_company cyCompany,
            o.cy_phone cyPhone,
            o.fh_company_id fhCompanyId,
            o.fh_company fhCompany,
            o.fh_phone fhPhone,
            o.sh_company_id shCompanyId,
            o.sh_company shCompany,
            o.sh_phone shPhone,
            o.remain_num remainNum,   #剩余代运量
            o.remain_allocation remainAllocation,   #剩余代分配量
            o.total_load_amount totalLoadAmount,
            o.total_unload_amount totalUnLoadAmount,
            o.tenant_id tenantId,
<!--            货物信息-->
            tg.goods_id goodsId,
            tg.goods_name goodsName,
            tg.goods_type goodsType,
            tg.goods_unit goodsUnit,
            tg.goods_num  goodsNum,
            tg.goods_volume goodsVolume,
            tg.goods_weight goodsWeight,
<!--            履约信息-->
            tpr.per_start_time perStartTime,
            tpr.per_end_time perEndTime,
            tpr.billing_cycle settlementCycle,
            tpr.pay_method payment,
            tpr.pay_company payCompany,
            tpr.billing_mode billingMode,
            tpr.gathering_company gatheringCompany,
            tpr.price,
<!--            扣亏货-->
            tdg.rational_loss rationalLoss,
            tdg.ded_price dedPrice,
<!--            更多信息-->
            tm.truck_type truckType,
            tm.truck_type_str truckTypeStr,
            tm.estimate_freight estimateFreight,
            tm.remark
        from kw_order o
         left join kw_order_usual_land_transport_more tm on o.id = tm.order_id
         left join kw_order_usual_land_transport_goods tg on o.id = tg.order_id
         left join kw_order_usual_land_transport_def_goods tdg on o.id = tdg.order_id
         left join kw_order_usual_transport_per tpr on o.id = tpr.order_id
        <where>
            o.del_flag = 0
            <if test="orderId != null">
                and o.id = #{orderId}
            </if>
        </where>
    </select>



    <select id="findLogisticsOrder" resultType="com.sckw.usual.entity.KwOrder"
            parameterType="java.util.Map">
        select *
        from kw_order
        where del_flag = 0
        <if test="id != null and id != ''">
            and id = #{id, jdbcType=BIGINT}
        </if>
        <if test="pid != null and pid != ''">
            and pid in (#{pid, jdbcType=VARCHAR})
        </if>
        <if test="status != null and status != ''">
            and status = #{status, jdbcType=INTEGER}
        </if>
        <if test="upperlOrderId != null and upperlOrderId != ''">
            and FIND_IN_SET(#{upperlOrderId, jdbcType=BIGINT}, pids)
        </if>
        <if test="belowlOrderId != null and belowlOrderId != ''">
            and FIND_IN_SET(id, #{belowlOrderId, jdbcType=VARCHAR})
        </if>
        <if test="ownOrderId != null and ownOrderId != ''">
            and id != #{ownOrderId, jdbcType=BIGINT}
        </if>
        <if test="allOrderId != null and allOrderId != ''">
            and (
            FIND_IN_SET(#{plOrderIds, jdbcType=BIGINT}
            , pids)
            or FIND_IN_SET(id
            , #{currentlOrderId, jdbcType=VARCHAR})
            )
        </if>
        <if test="checkEntId != null and checkEntId != ''">
            and id in (select l_order_id from kw_order_usual_land_transport_unit where unit_type = 1
            and top_ent_id = #{checkEntId, jdbcType=BIGINT})
        </if>
    </select>


    <select id="findLogisticsOrderCount" resultType="com.sckw.usual.entity.LogisticsCountVo" parameterType="java.util.Map">
        SELECT
        IFNULL(sum(load_amount), 0.0) loadAmount, IFNULL(sum(unload_amount), 0.0) unloadAmount,
        IFNULL(sum(total_load_amount), 0.0) totalLoadAmount, IFNULL(sum(total_unload_amount), 0.0) totalUnloadAmount
        from kw_order where del_flag = 0
        <if test="tOrderId != null and tOrderId != ''">
            and t_order_id = #{tOrderId, jdbcType=BIGINT}
        </if>
        <if test="lOrderId != null and lOrderId != ''">
            and id = #{lOrderId, jdbcType=BIGINT}
        </if>
        <if test="level != null and level != ''">
            and pid is null
        </if>
        <if test="finishedStatus != null and finishedStatus != ''">
            and status in (4, 5, 6)
        </if>
        <if test="status != null and status != ''">
            and status = #{lOrderId, jdbcType=INTEGER}
        </if>
        <if test="upperlOrderId != null and upperlOrderId != ''">
            and FIND_IN_SET(#{upperlOrderId, jdbcType=BIGINT}, pids)
        </if>
        <if test="belowlOrderId != null and belowlOrderId != ''">
            and FIND_IN_SET(id, #{belowlOrderId, jdbcType=VARCHAR})
        </if>
    </select>

    <select id="countByStatus" resultType="java.util.Map">
        SELECT `status`, COUNT(*) AS count FROM kw_order where del_flag = 0  and tenant_id = #{tenant} GROUP BY `status`
    </select>
    <select id="finishWaybillCount" resultType="Integer">
        select COALESCE(count(id), 0) from kw_waybill_order where order_id = #{#orderId} and status = 5 and del_flag = 0
    </select>
    <select id="finishOrderSum" resultType="BigDecimal">
        select COALESCE(SUM(total_load_amount), 0) from kw_order where id = #{#orderId} and status = 9 and del_flag = 0
    </select>
</mapper>