<?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.payment.dao.KwpLedgerLogisticsMapper">
    <resultMap id="BaseResultMap" type="com.sckw.payment.model.KwpLedgerLogistics">
        <!--@mbg.generated-->
        <!--@Table kwp_ledger_logistics-->
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="ent_id" jdbcType="BIGINT" property="entId"/>
        <result column="l_ledger_no" jdbcType="VARCHAR" property="lLedgerNo"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="start_time" jdbcType="TIMESTAMP" property="startTime"/>
        <result column="end_time" jdbcType="TIMESTAMP" property="endTime"/>
        <result column="tax_rate" jdbcType="INTEGER" property="taxRate"/>
        <result column="trading" jdbcType="VARCHAR" property="trading"/>
        <result column="total_price" jdbcType="DECIMAL" property="totalPrice"/>
        <result column="ex_tax_price" jdbcType="DECIMAL" property="exTaxPrice"/>
        <result column="settle_price" jdbcType="DECIMAL" property="settlePrice"/>
        <result column="actual_price" jdbcType="DECIMAL" property="actualPrice"/>
        <result column="deduct_price" jdbcType="DECIMAL" property="deductPrice"/>
        <result column="audit_user_id" jdbcType="VARCHAR" property="auditUserId"/>
        <result column="audit_user" jdbcType="VARCHAR" property="auditUser"/>
        <result column="audit_phone" jdbcType="VARCHAR" property="auditPhone"/>
        <result column="success_user_id" jdbcType="VARCHAR" property="successUserId"/>
        <result column="success_user" jdbcType="VARCHAR" property="successUser"/>
        <result column="success_phone" jdbcType="VARCHAR" property="successPhone"/>
        <result column="url" jdbcType="VARCHAR" property="url"/>
        <result column="generate_time" jdbcType="TIMESTAMP" property="generateTime"/>
        <result column="receipt_time" jdbcType="TIMESTAMP" property="receiptTime"/>
        <result column="order_count" jdbcType="INTEGER" property="orderCount"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="create_by" jdbcType="BIGINT" property="createBy"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_by" jdbcType="BIGINT" property="updateBy"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="del_flag" jdbcType="INTEGER" property="delFlag"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id,
        ent_id,
        l_ledger_no,
        `name`,
        start_time,
        end_time,
        tax_rate,
        trading,
        total_price,
        ex_tax_price,
        settle_price,
        actual_price,
        deduct_price,
        audit_user_id,
        audit_user,
        audit_phone,
        success_user_id,
        success_user,
        success_phone,
        url,
        generate_time,
        receipt_time,
        order_count,
        remark,
        `status`,
        create_by,
        create_time,
        update_by,
        update_time,
        del_flag
    </sql>
    <sql id="list_col">
        kll.id,
        kll.l_ledger_no   lLedgerNo,
        kll.name,
        kll.start_time    startTime,
        kll.end_time      endTime,
        kll.tax_rate      taxRate,
        kll.trading,
        kll.total_price   totalPrice,
        kll.ex_tax_price  exTaxPrice,
        kll.settle_price  settlePrice,
        kll.actual_price  actualPrice,
        kll.deduct_price  deductPrice,
        kll.audit_phone   auditPhone,
        kll.audit_user    auditUser,
        kll.audit_user_id    auditUserId,
        kll.success_user_id  successUserId,
        kll.success_phone successPhone,
        kll.success_user  successUser,
        kll.url,
        kll.generate_time generateTime,
        kll.receipt_time  receiptTime,
        kll.remark,
        kll.status,
        kll.create_by     createBy,
        kllu.top_ent_id   checkEntId,
        kllu.contacts,
        kllu.phone,
        kllu.firm_name    firmName,
        kll.order_count   orderCount
    </sql>
    <select id="shipperSelect" resultType="com.sckw.payment.model.dto.LedgerShipperDto">
        select kll.id,
               kll.l_ledger_no     lLedgerNo,
               kll.name,
               kll.start_time      startTime,
               kll.end_time        endTime,
               kll.tax_rate        taxRate,
               kll.trading,
               kll.total_price     totalPrice,
               kll.ex_tax_price    exTaxPrice,
               kll.settle_price    settlePrice,
               kll.actual_price    actualPrice,
               kll.deduct_price    deductPrice,
               kll.success_user_id successUserId,
               kll.success_phone   successPhone,
               kll.success_user    successUser,
               kll.url,
               kll.generate_time   generateTime,
               kll.receipt_time    receiptTime,
               kll.remark,
               kll.status,
               kll.create_by       createBy,
               kllu.top_ent_id     checkEntId,
               kllu.contacts,
               kllu.phone,
               kllu.firm_name      firmName,
               kll.order_count     orderCount
        from kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu
                            on kll.id = kllu.l_ledger_id and kllu.del_flag = 0 and
                               kllu.unit_type = #{logisticsReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_logistics_unit kllu2
                            on kll.id = kllu2.l_ledger_id and kllu2.del_flag = 0 and
                               kllu2.unit_type = #{logisticsReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kll.del_flag = 0
              and kllu2.top_ent_id = #{logisticsReq.entId,jdbcType=BIGINT}
            <choose>
                <when test="logisticsReq.status != null">
                    and kll.status = #{logisticsReq.status}
                </when>
                <otherwise>
                    and kll.status not in (1, 6)
                </otherwise>
            </choose>
            <if test="logisticsReq.trading != null">
                and kll.trading = #{logisticsReq.trading,jdbcType=INTEGER}
            </if>
            <if test="logisticsReq.startCreateTime != null and logisticsReq.startCreateTime != '' and logisticsReq.endCreateTime != null and logisticsReq.endCreateTime != ''">
                and kll.generate_time between #{logisticsReq.startCreateTime,jdbcType=TIMESTAMP}
                    and #{logisticsReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="logisticsReq.keywords != null and logisticsReq.keywords != ''">
                and (
                            kll.l_ledger_no like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.firm_name like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.contacts like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
            <if test="authList != null and authList.size() != 0">
                and (
                case
                    when kll.status = 2 then
                        true
                when kll.status in (3, 4, 5) then
                (
                (
                    kllu2.contacts_id in
                <foreach collection="authList" item="item" open="(" close=")" separator=",">
                    #{item,jdbcType=BIGINT}
                </foreach>)
                or
            (
            select kllt.id
            from kwp_ledger_logistics_track kllt where kllt.del_flag = 0
                                                   and kllt.l_ledger_id = kll.id
                                                   and kllt.status in (3, 5)
                                                   and kllt.create_by in
                <foreach collection="authList" item="item" open="(" close=")" separator=",">
                    #{item,jdbcType=BIGINT}
                </foreach>
                )
                    )
                    end
                    )
            </if>
        </where>
        order by kll.generate_time desc
    </select>
    <select id="list" resultType="com.sckw.payment.model.vo.LedgerLogistics">
        SELECT kll.id,
               kll.l_ledger_no   AS lLedgerNo,
               kll.name          AS name,
               kll.start_time    AS startTime,
               kll.end_time      AS endTime,
               kll.tax_rate      AS taxRate,
               kll.trading       AS trading,
               kll.total_price   AS totalPrice,
               kll.ex_tax_price  AS exTaxPrice,
               kll.settle_price  AS settlePrice,
               kll.actual_price  AS actualPrice,
               kll.deduct_price  AS deductPrice,
               kll.audit_phone   AS auditPhone,
               kll.audit_user    AS auditUser,
               kll.audit_user_id    auditUserId,
               kll.success_user_id  successUserId,
               kll.success_phone AS successPhone,
               kll.success_user  AS successUser,
               kll.url           AS url,
               kll.generate_time AS generateTime,
               kll.receipt_time  AS receiptTime,
               kll.remark        AS remark,
               kll.status        AS status,
               kll.order_count   AS orderCount,
               kll.create_by     AS createBy,
               kllu.top_ent_id   AS shipperEntId,
               kllu.firm_name    AS shipperFirmName,
               kllu2.top_ent_id  AS carrierEntId,
               kllu2.firm_name   AS carrierFirmName
        FROM kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu
                            on kll.id = kllu.l_ledger_id and kllu.del_flag = 0 and
                               kllu.unit_type = #{logisticsReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_logistics_unit kllu2
                            on kll.id = kllu2.l_ledger_id and kllu2.del_flag = 0 and
                               kllu2.unit_type = #{logisticsReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kll.del_flag = 0
            <if test="logisticsReq.status != null">
                and kll.status = #{logisticsReq.status}
            </if>
            <if test="logisticsReq.trading != null">
                and kll.trading = #{logisticsReq.trading,jdbcType=INTEGER}
            </if>
            <if test="logisticsReq.startCreateTime != null and logisticsReq.startCreateTime != '' and logisticsReq.endCreateTime != null and logisticsReq.endCreateTime != ''">
                and kll.generate_time between #{logisticsReq.startCreateTime,jdbcType=TIMESTAMP}
                    and #{logisticsReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="logisticsReq.keywords != null and logisticsReq.keywords != ''">
                and (
                            kll.l_ledger_no like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.firm_name like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu2.firm_name like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.contacts like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
        </where>
        order by kll.generate_time desc
    </select>
    <select id="selectIds" resultType="com.sckw.payment.model.vo.LedgerLogistics">
        SELECT kll.id,
               kll.l_ledger_no   AS lLedgerNo,
               kll.name          AS name,
               kll.start_time    AS startTime,
               kll.end_time      AS endTime,
               kll.tax_rate      AS taxRate,
               kll.trading       AS trading,
               kll.total_price   AS totalPrice,
               kll.ex_tax_price  AS exTaxPrice,
               kll.settle_price  AS settlePrice,
               kll.actual_price  AS actualPrice,
               kll.deduct_price  AS deductPrice,
               kll.audit_phone      auditPhone,
               kll.audit_user       auditUser,
               kll.audit_user_id    auditUserId,
               kll.success_user_id  successUserId,
               kll.success_phone AS successPhone,
               kll.success_user  AS successUser,
               kll.url           AS url,
               kll.generate_time AS generateTime,
               kll.receipt_time  AS receiptTime,
               kll.remark        AS remark,
               kll.status        AS status,
               kll.order_count   AS orderCount,
               kll.create_by     AS createBy,
               kllu.top_ent_id   AS shipperEntId,
               kllu.firm_name    AS shipperFirmName,
               kllu2.top_ent_id  AS carrierEntId,
               kllu2.firm_name   AS carrierFirmName
        FROM kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu
                            on kll.id = kllu.l_ledger_id and kllu.del_flag = 0 and
                               kllu.unit_type = 1
                 inner join kwp_ledger_logistics_unit kllu2
                            on kll.id = kllu2.l_ledger_id and kllu2.del_flag = 0 and
                               kllu2.unit_type = 2
        <where>
            kll.del_flag = 0
              and kll.id in
            <foreach collection="ids" item="id" close=")" open="(" separator=",">
                #{id,jdbcType=BIGINT}
            </foreach>
        </where>
    </select>
    <!--金额统计规则：未筛选状态，统计已完成的对账单，结算金额。筛选状态统计具体的状态-->
    <select id="countSumMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(kll.settle_price), 0)
        from kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu
                            on kll.id = kllu.l_ledger_id and kllu.del_flag = 0 and
                               kllu.unit_type = #{logisticsReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_logistics_unit kllu2
                            on kll.id = kllu2.l_ledger_id and kllu2.del_flag = 0 and
                               kllu2.unit_type = #{logisticsReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kll.del_flag = 0
              and kllu2.top_ent_id = #{logisticsReq.entId,jdbcType=BIGINT}
            <if test="logisticsReq.status != null">
                and kll.status = #{logisticsReq.status}
            </if>
            <if test="logisticsReq.status == null">
                and kll.status = 4
            </if>
            <if test="status != null and status.size() > 0">
                and kll.status not in
                <foreach collection="status" item="id" close=")" open="(" separator=",">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="logisticsReq.trading != null">
                and kll.trading = #{logisticsReq.trading,jdbcType=INTEGER}
            </if>
            <if test="logisticsReq.startCreateTime != null and logisticsReq.startCreateTime != '' and logisticsReq.endCreateTime != null and logisticsReq.endCreateTime != ''">
                and kll.generate_time between #{logisticsReq.startCreateTime,jdbcType=TIMESTAMP}
                    and #{logisticsReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="logisticsReq.keywords != null and logisticsReq.keywords != ''">
                and (
                            kll.l_ledger_no like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.firm_name like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.contacts like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
        </where>
    </select>
    <!--数量统计规则：未筛选，统计所有。需要根据企业方，排除已保存，已撤回两个状态-->
    <select id="countSum" resultType="java.lang.Long">
        select IFNULL(count(1), 0)
        from kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu
                            on kll.id = kllu.l_ledger_id and kllu.del_flag = 0 and
                               kllu.unit_type = #{logisticsReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_logistics_unit kllu2
                            on kll.id = kllu2.l_ledger_id and kllu2.del_flag = 0 and
                               kllu2.unit_type = #{logisticsReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kll.del_flag = 0
              and kllu2.top_ent_id = #{logisticsReq.entId,jdbcType=BIGINT}
            <if test="logisticsReq.status != null">
                and kll.status = #{logisticsReq.status}
            </if>
            <if test="status != null and status.size() > 0">
                and kll.status not in
                <foreach collection="status" item="id" close=")" open="(" separator=",">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </if>
            <if test="logisticsReq.trading != null">
                and kll.trading = #{logisticsReq.trading,jdbcType=INTEGER}
            </if>
            <if test="logisticsReq.startCreateTime != null and logisticsReq.startCreateTime != '' and logisticsReq.endCreateTime != null and logisticsReq.endCreateTime != ''">
                and kll.generate_time between #{logisticsReq.startCreateTime,jdbcType=TIMESTAMP}
                    and #{logisticsReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="logisticsReq.keywords != null and logisticsReq.keywords != ''">
                and (
                            kll.l_ledger_no like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.firm_name like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.contacts like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
        </where>
    </select>
    <!--托运方1 承运方2-->
    <select id="carrierSelect" resultType="com.sckw.payment.model.dto.LedgerCarrierDto">
        select
        <include refid="list_col"/>
        from kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu
                            on kll.id = kllu.l_ledger_id and kllu.del_flag = 0 and
                               kllu.unit_type = #{logisticsReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_logistics_unit kllu2
                            on kll.id = kllu2.l_ledger_id and kllu2.del_flag = 0 and
                               kllu2.unit_type = #{logisticsReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kll.del_flag = 0
              and kllu2.top_ent_id = #{logisticsReq.entId,jdbcType=BIGINT}
            <if test="logisticsReq.status != null">
                and kll.status = #{logisticsReq.status}
            </if>
            <if test="logisticsReq.trading != null">
                and kll.trading = #{logisticsReq.trading,jdbcType=INTEGER}
            </if>
            <if test="logisticsReq.startCreateTime != null and logisticsReq.startCreateTime != '' and logisticsReq.endCreateTime != null and logisticsReq.endCreateTime != ''">
                and kll.generate_time between #{logisticsReq.startCreateTime,jdbcType=TIMESTAMP}
                    and #{logisticsReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="logisticsReq.keywords != null and logisticsReq.keywords != ''">
                and (
                            kll.l_ledger_no like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.firm_name like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.contacts like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
            <if test="authList != null and authList.size() != 0">
                and (
                case when kll.status in (1, 6)
                    then
                    kll.create_by in
                <foreach collection="authList" item="item" open="(" close=")" separator=",">
                    #{item,jdbcType=BIGINT}
                </foreach>
                when kll.status in (2, 3, 4, 5) then
                (
                    kll.create_by in
                <foreach collection="authList" item="item" open="(" close=")" separator=",">
                    #{item,jdbcType=BIGINT}
                </foreach>
                or kllu2.contacts_id in
                <foreach collection="authList" item="item" open="(" close=")" separator=",">
                    #{item,jdbcType=BIGINT}
                </foreach>
                )
                    end
                    )
            </if>
        </where>
        order by kll.generate_time desc
    </select>
    <!--对账发起方-->
    <select id="countSendOrder" resultType="java.util.Map">
        SELECT count(1)                           "0",
               count(IF(kll.status = 1, 1, NULL)) "1",
               count(IF(kll.status = 2, 1, NULL)) "2",
               count(IF(kll.status = 3, 1, NULL)) "3",
               count(IF(kll.status = 4, 1, NULL)) "4",
               count(IF(kll.status = 5, 1, NULL)) "5",
               count(IF(kll.status = 6, 1, NULL)) "6"
        FROM kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu
                            on kll.id = kllu.l_ledger_id and kllu.del_flag = 0 and
                               kllu.unit_type = #{logisticsReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_logistics_unit kllu2
                            on kll.id = kllu2.l_ledger_id and kllu2.del_flag = 0 and
                               kllu2.unit_type = #{logisticsReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kll.del_flag = 0
              and kllu2.top_ent_id = #{logisticsReq.entId,jdbcType=BIGINT}
            <if test="logisticsReq.trading != null">
                and kll.trading = #{logisticsReq.trading,jdbcType=INTEGER}
            </if>
            <if test="logisticsReq.startCreateTime != null and logisticsReq.startCreateTime != '' and logisticsReq.endCreateTime != null and logisticsReq.endCreateTime != ''">
                and kll.generate_time between #{logisticsReq.startCreateTime,jdbcType=TIMESTAMP}
                    and #{logisticsReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="logisticsReq.keywords != null and logisticsReq.keywords != ''">
                and (
                            kll.l_ledger_no like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.firm_name like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.contacts like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
            <if test="authList != null and authList.size() != 0">
                and (
                case when kll.status in (1, 6)
                    then
                    kll.create_by in
                <foreach collection="authList" item="item" open="(" close=")" separator=",">
                    #{item,jdbcType=BIGINT}
                </foreach>
                when kll.status in (2, 3, 4, 5) then
                (
                    kll.create_by in
                <foreach collection="authList" item="item" open="(" close=")" separator=",">
                    #{item,jdbcType=BIGINT}
                </foreach>
                or kllu2.contacts_id in
                <foreach collection="authList" item="item" open="(" close=")" separator=",">
                    #{item,jdbcType=BIGINT}
                </foreach>
                )
                    end
                    )
            </if>
        </where>
    </select>
    <!--对账审核方-->
    <select id="countAuditOrder" resultType="java.util.Map">
        SELECT count(1)                           "0",
               count(IF(kll.status = 1, 1, NULL)) "1",
               count(IF(kll.status = 2, 1, NULL)) "2",
               count(IF(kll.status = 3, 1, NULL)) "3",
               count(IF(kll.status = 4, 1, NULL)) "4",
               count(IF(kll.status = 5, 1, NULL)) "5",
               count(IF(kll.status = 6, 1, NULL)) "6"
        FROM kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu
                            on kll.id = kllu.l_ledger_id and kllu.del_flag = 0 and
                               kllu.unit_type = #{logisticsReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_logistics_unit kllu2
                            on kll.id = kllu2.l_ledger_id and kllu2.del_flag = 0 and
                               kllu2.unit_type = #{logisticsReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kll.del_flag = 0
              and kllu2.top_ent_id = #{logisticsReq.entId,jdbcType=BIGINT}
              and kll.status not in (1, 6)
            <if test="logisticsReq.trading != null">
                and kll.trading = #{logisticsReq.trading,jdbcType=INTEGER}
            </if>
            <if test="logisticsReq.startCreateTime != null and logisticsReq.startCreateTime != '' and logisticsReq.endCreateTime != null and logisticsReq.endCreateTime != ''">
                and kll.generate_time between #{logisticsReq.startCreateTime,jdbcType=TIMESTAMP}
                    and #{logisticsReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="logisticsReq.keywords != null and logisticsReq.keywords != ''">
                and (
                            kll.l_ledger_no like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.firm_name like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.contacts like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
            <if test="authList != null and authList.size() != 0">
                and (
                case
                    when kll.status = 2 then
                        true
                when kll.status in (3, 4, 5) then
                (
                (
                    kllu2.contacts_id in
                <foreach collection="authList" item="item" open="(" close=")" separator=",">
                    #{item,jdbcType=BIGINT}
                </foreach>)
                or
            (
            select kllt.id
            from kwp_ledger_logistics_track kllt where kllt.del_flag = 0
                                                   and kllt.l_ledger_id = kll.id
                                                   and kllt.status in (3, 5)
                                                   and kllt.create_by in
                <foreach collection="authList" item="item" open="(" close=")" separator=",">
                    #{item,jdbcType=BIGINT}
                </foreach>
                )
                    )
                    end
                    )
            </if>
        </where>
    </select>
    <select id="countOrder" resultType="java.util.Map">
        SELECT count(1)                           "0",
               count(IF(kll.status = 1, 1, NULL)) "1",
               count(IF(kll.status = 2, 1, NULL)) "2",
               count(IF(kll.status = 3, 1, NULL)) "3",
               count(IF(kll.status = 4, 1, NULL)) "4",
               count(IF(kll.status = 5, 1, NULL)) "5",
               count(IF(kll.status = 6, 1, NULL)) "6"
        FROM kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu
                            on kll.id = kllu.l_ledger_id and kllu.del_flag = 0 and
                               kllu.unit_type = #{logisticsReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_logistics_unit kllu2
                            on kll.id = kllu2.l_ledger_id and kllu2.del_flag = 0 and
                               kllu2.unit_type = #{logisticsReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kll.del_flag = 0
              and kllu2.top_ent_id = #{logisticsReq.entId,jdbcType=BIGINT}
            <if test="logisticsReq.unitType != null and logisticsReq.unitType == 2">
                and kll.status not in (1, 6)
            </if>
            <if test="logisticsReq.trading != null">
                and kll.trading = #{logisticsReq.trading,jdbcType=INTEGER}
            </if>
            <if test="logisticsReq.startCreateTime != null and logisticsReq.startCreateTime != '' and logisticsReq.endCreateTime != null and logisticsReq.endCreateTime != ''">
                and kll.generate_time between #{logisticsReq.startCreateTime,jdbcType=TIMESTAMP}
                    and #{logisticsReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="logisticsReq.keywords != null and logisticsReq.keywords != ''">
                and (
                            kll.l_ledger_no like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.firm_name like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        or kllu.contacts like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
        </where>
    </select>

    <select id="countOrder2" resultType="java.util.Map">
        SELECT count(1)                                    "0",
               count(IF(kll_distinct.status = 1, 1, NULL)) "1",
               count(IF(kll_distinct.status = 2, 1, NULL)) "2",
               count(IF(kll_distinct.status = 3, 1, NULL)) "3",
               count(IF(kll_distinct.status = 4, 1, NULL)) "4",
               count(IF(kll_distinct.status = 5, 1, NULL)) "5",
               count(IF(kll_distinct.status = 6, 1, NULL)) "6"
        FROM (
        SELECT DISTINCT kll.id, kll.status
        FROM kwp_ledger_logistics kll
                 INNER JOIN kwp_ledger_logistics_unit kllu
                            ON kll.id = kllu.l_ledger_id AND kllu.del_flag = 0
        <where>
            kll.del_flag = 0
            <if test="logisticsReq.trading != null">
                AND kll.trading = #{logisticsReq.trading,jdbcType=INTEGER}
            </if>
            <if test="logisticsReq.startCreateTime != null and logisticsReq.startCreateTime != '' and logisticsReq.endCreateTime != null and logisticsReq.endCreateTime != ''">
                AND kll.generate_time BETWEEN #{logisticsReq.startCreateTime,jdbcType=TIMESTAMP}
                    AND #{logisticsReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="logisticsReq.keywords != null and logisticsReq.keywords != ''">
                AND (
                            kll.l_ledger_no LIKE CONCAT('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        OR kllu.firm_name LIKE CONCAT('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                        OR kllu.contacts LIKE CONCAT('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                    )
            </if>
        </where>
        ) kll_distinct
    </select>

    <select id="selectCarrierIds" resultType="com.sckw.payment.model.dto.LedgerCarrierDto">
        select
        <include refid="list_col"/>
        from kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu on kll.id = kllu.l_ledger_id and kllu.del_flag = 0
        <where>
            kll.del_flag = 0
              and kllu.unit_type = 1
              and kll.id in
            <foreach collection="ids" item="id" close=")" open="(" separator=",">
                #{id,jdbcType=BIGINT}
            </foreach>
        </where>
        order by kll.generate_time desc
    </select>
    <select id="selectShipperIds" resultType="com.sckw.payment.model.dto.LedgerShipperDto">
        select
        <include refid="list_col"/>
        from kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu on kll.id = kllu.l_ledger_id and kllu.del_flag = 0
        <where>
            kll.del_flag = 0
              and kllu.unit_type = 2
              and kll.id in
            <foreach collection="ids" item="id" close=")" open="(" separator=",">
                #{id,jdbcType=BIGINT}
            </foreach>
        </where>
        order by kll.generate_time desc
    </select>
    <select id="selectId" resultType="com.sckw.payment.model.dto.LedgerLogisticsDto">
        select
        <include refid="list_col"/>
        from kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu on kll.id = kllu.l_ledger_id and kllu.del_flag = 0
        <where>
            kll.del_flag = 0
              and kllu.unit_type = #{unitType,jdbcType=INTEGER}
              and kll.id = #{id}
        </where>
    </select>

    <select id="selectJoin" resultType="java.lang.String">
        select kll.l_ledger_no
        from kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu
                            on kll.id = kllu.l_ledger_id and kllu.del_flag = 0 and
                               kllu.unit_type = 1
                 inner join kwp_ledger_logistics_unit kllu2
                            on kll.id = kllu2.l_ledger_id and kllu2.del_flag = 0 and
                               kllu2.unit_type = 2
        <where>
            kll.del_flag = 0
              and kll.status != 4
              and kllu.top_ent_id = #{entId,jdbcType=BIGINT}
              and kllu2.top_ent_id = #{entTarget,jdbcType=BIGINT}
        </where>
    </select>

    <select id="countSize" resultType="com.sckw.payment.model.dto.LedgerSize">
        select count(distinct if(kllu.unit_type = 2, kll.id, null)) send,
               count(distinct if(kllu.unit_type = 1, kll.id, null)) receive
        from kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu on kll.id = kllu.l_ledger_id and kllu.del_flag = 0
        <where>
            kll.del_flag = 0
              and kll.status != 1
              and kllu.top_ent_id = #{entId,jdbcType=BIGINT}
        </where>
    </select>

    <select id="exportList" resultType="com.sckw.payment.model.dto.LedgerLogisticsDto">
        select kll.id,
               kll.l_ledger_no                     lLedgerNo,
               kll.name,
               kll.start_time                      startTime,
               kll.end_time                        endTime,
               kll.tax_rate                        taxRate,
               kll.trading,
               kll.total_price                     totalPrice,
               kll.ex_tax_price                    exTaxPrice,
               kll.settle_price                    settlePrice,
               kll.actual_price                    actualPrice,
               kll.deduct_price                 AS deductPrice,
               kll.url,
               kll.generate_time                   generateTime,
               kll.receipt_time                    receiptTime,
               kll.remark,
               kll.status,
               kll.create_by                       createBy,
               kllu.top_ent_id                     checkEntId,
               (case
                    when #{logisticsReq.unitType,jdbcType=INTEGER} = 1 then kll.audit_user
                    else kll.success_user end)  as contacts,
               (case
                    when #{logisticsReq.unitType,jdbcType=INTEGER} = 1 then kll.audit_phone
                    else kll.success_phone end) as phone,
               kllu.firm_name                      firmName,
               kll.order_count                     orderCount
        from kwp_ledger_logistics kll
                 inner join kwp_ledger_logistics_unit kllu
                            on kll.id = kllu.l_ledger_id and kllu.del_flag = 0 and
                               kllu.unit_type = #{logisticsReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_logistics_unit kllu2
                            on kll.id = kllu2.l_ledger_id and kllu2.del_flag = 0 and
                               kllu2.unit_type = #{logisticsReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            kll.del_flag = 0
            <choose>
                <when test="idList != null and idList.size() != 0">
                    and kll.id in
                    <foreach collection="idList" open="(" separator="," close=")" item="item">
                        #{item,jdbcType=BIGINT}
                    </foreach>
                </when>
                <otherwise>
                    and kllu2.top_ent_id = #{logisticsReq.entId,jdbcType=BIGINT}
                    <if test="logisticsReq.status != null and logisticsReq.status != 0">
                        and kll.status = #{logisticsReq.status}
                    </if>
                    <if test="logisticsReq.trading != null">
                        and kll.trading = #{logisticsReq.trading,jdbcType=INTEGER}
                    </if>
                    <if test="logisticsReq.startCreateTime != null and logisticsReq.startCreateTime != '' and logisticsReq.endCreateTime != null and logisticsReq.endCreateTime != ''">
                        and kll.generate_time between #{logisticsReq.startCreateTime,jdbcType=TIMESTAMP}
                            and #{logisticsReq.endCreateTime,jdbcType=TIMESTAMP}
                    </if>
                    <if test="logisticsReq.keywords != null and logisticsReq.keywords != ''">
                        and (
                                    kll.l_ledger_no like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                                or kllu.firm_name like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                                or kllu.contacts like concat('%', #{logisticsReq.keywords,jdbcType=VARCHAR}, '%')
                            )
                    </if>
                    <if test="authList != null and authList.size() != 0">
                        <if test="logisticsReq.unitType == 1">
                            and (
                            case when kll.status in (1, 6)
                                then
                                kll.create_by in
                            <foreach collection="authList" item="item" open="(" close=")" separator=",">
                                #{item,jdbcType=BIGINT}
                            </foreach>
                            when kll.status in (2, 3, 4, 5) then
                            (
                                kll.create_by in
                            <foreach collection="authList" item="item" open="(" close=")" separator=",">
                                #{item,jdbcType=BIGINT}
                            </foreach>
                            or kllu2.contacts_id in
                            <foreach collection="authList" item="item" open="(" close=")" separator=",">
                                #{item,jdbcType=BIGINT}
                            </foreach>
                            )
                                end
                                )
                        </if>
                        <if test="logisticsReq.unitType == 2">
                            and (
                            case
                                when kll.status = 2 then
                                    true
                            when kll.status in (3, 4, 5) then
                            (
                            (
                                kllu2.contacts_id in
                            <foreach collection="authList" item="item" open="(" close=")" separator=",">
                                #{item,jdbcType=BIGINT}
                            </foreach>)
                            or
                        (
                        select kllt.id
                        from kwp_ledger_logistics_track kllt where kllt.del_flag = 0
                                                               and kllt.l_ledger_id = kll.id
                                                               and kllt.status in (3, 5)
                                                               and kllt.create_by in
                            <foreach collection="authList" item="item" open="(" close=")" separator=",">
                                #{item,jdbcType=BIGINT}
                            </foreach>
                            )
                                )
                                end
                                )
                        </if>
                    </if>
                </otherwise>
            </choose>
        </where>
    </select>
</mapper>
