<?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.KwpLedgerTradeMapper">
    <resultMap id="BaseResultMap" type="com.sckw.payment.model.KwpLedgerTrade">
        <!--@mbg.generated-->
        <!--@Table kwp_ledger_trade-->
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="ent_id" jdbcType="BIGINT" property="entId"/>
        <result column="t_ledger_no" jdbcType="VARCHAR" property="tLedgerNo"/>
        <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="audit_user" jdbcType="VARCHAR" property="auditUser"/>
        <result column="audit_phone" jdbcType="VARCHAR" property="auditPhone"/>
        <result column="generate_time" jdbcType="TIMESTAMP" property="generateTime"/>
        <result column="receipt_time" jdbcType="TIMESTAMP" property="receiptTime"/>
        <result column="url" jdbcType="VARCHAR" property="url"/>
        <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,
        t_ledger_no,
        `name`,
        start_time,
        end_time,
        tax_rate,
        trading,
        total_price,
        ex_tax_price,
        settle_price,
        actual_price,
        audit_user,
        audit_phone,
        generate_time,
        receipt_time,
        url,
        order_count,
        remark,
        `status`,
        create_by,
        create_time,
        update_by,
        update_time,
        del_flag
    </sql>
    <sql id="sell_col">
        klt.id,
        klt.t_ledger_no   tLedgerNo,
        klt.name,
        klt.start_time    startTime,
        klt.end_time      endTime,
        klt.tax_rate      taxRate,
        klt.trading,
        klt.total_price   totalPrice,
        klt.ex_tax_price  exTaxPrice,
        klt.settle_price  settlePrice,
        klt.actual_price  actualPrice,
        klt.audit_phone   auditPhone,
        klt.audit_user    auditUser,
        klt.url,
        klt.generate_time generateTime,
        klt.receipt_time  receiptTime,
        klt.remark,
        klt.status,
        kltu.contacts,
        kltu.phone,
        kltu.firm_name    firmName,
        klt.order_count   orderCount
    </sql>
    <sql id="purchase_col">
        klt.id,
        klt.t_ledger_no   tLedgerNo,
        klt.name,
        klt.start_time    startTime,
        klt.end_time      endTime,
        klt.tax_rate      taxRate,
        klt.trading,
        klt.total_price   totalPrice,
        klt.ex_tax_price  exTaxPrice,
        klt.settle_price  settlePrice,
        klt.actual_price  actualPrice,
        klt.audit_phone   auditPhone,
        klt.audit_user    auditUser,
        klt.url,
        klt.generate_time generateTime,
        klt.receipt_time  receiptTime,
        klt.remark,
        klt.status,
        kltu.contacts,
        kltu.phone,
        kltu.firm_name    firmName,
        klt.order_count   orderCount
    </sql>
    <!--销售2 采购1-->
    <select id="sellList" resultType="com.sckw.payment.model.dto.LedgerSellDto">
        select
        <include refid="sell_col"/>
        from kwp_ledger_trade klt
                 inner join kwp_ledger_trade_unit kltu
                            on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                               kltu.unit_type = #{tradeReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_trade_unit kltu2
                            on klt.id = kltu2.t_ledger_id and kltu2.del_flag = 0 and kltu2.unit_type =
                                                                                     #{tradeReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            klt.del_flag = 0
              and kltu2.top_ent_id = #{tradeReq.entId,jdbcType=BIGINT}
            <if test="tradeReq.status != null">
                and klt.status = #{tradeReq.status}
            </if>
            <if test="tradeReq.trading != null">
                and klt.trading = #{tradeReq.trading,jdbcType=INTEGER}
            </if>
            <if test="tradeReq.endCreateTime != null and tradeReq.endCreateTime != '' and tradeReq.startCreateTime != null and tradeReq.startCreateTime != ''">
                and klt.generate_time between #{tradeReq.startCreateTime,jdbcType=TIMESTAMP}
                    and #{tradeReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="tradeReq.keywords != null and tradeReq.keywords != ''">
                and (
                            klt.t_ledger_no like concat('%'
                            , #{tradeReq.keywords,jdbcType=VARCHAR}
                            , '%')
                        or kltu.firm_name like concat('%'
                        , #{tradeReq.keywords,jdbcType=VARCHAR}
                        , '%')
                        or kltu.contacts like concat('%'
                        , #{tradeReq.keywords,jdbcType=VARCHAR}
                        , '%')
                    )
            </if>
        </where>
        order by klt.generate_time desc
    </select>
    <select id="purchaseList" resultType="com.sckw.payment.model.dto.LedgerPurchaseDto">
        select
        <include refid="purchase_col"/>
        from kwp_ledger_trade klt
                 inner join kwp_ledger_trade_unit kltu
                            on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                               kltu.unit_type = #{tradeReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_trade_unit kltu2
                            on klt.id = kltu2.t_ledger_id and kltu2.del_flag = 0 and kltu2.unit_type =
                                                                                     #{tradeReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            klt.del_flag = 0
              and kltu2.top_ent_id = #{tradeReq.entId,jdbcType=BIGINT}
            <if test="tradeReq.status != null">
                and klt.status = #{tradeReq.status}
            </if>
            <if test="tradeReq.trading != null">
                and klt.trading = #{tradeReq.trading,jdbcType=INTEGER}
            </if>
            <if test="tradeReq.endCreateTime != null and tradeReq.endCreateTime != '' and tradeReq.startCreateTime != null and tradeReq.startCreateTime != ''">
                and klt.generate_time between #{tradeReq.startCreateTime,jdbcType=TIMESTAMP}
                    and #{tradeReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="tradeReq.keywords != null and tradeReq.keywords != ''">
                and (
                            klt.t_ledger_no like concat('%'
                            , #{tradeReq.keywords,jdbcType=VARCHAR}
                            , '%')
                        or kltu.firm_name like concat('%'
                        , #{tradeReq.keywords,jdbcType=VARCHAR}
                        , '%')
                        or kltu.contacts like concat('%'
                        , #{tradeReq.keywords,jdbcType=VARCHAR}
                        , '%')
                    )
            </if>
        </where>
        order by klt.generate_time desc
    </select>
    <select id="countOrder" resultType="java.util.Map">
        SELECT count(1)                           "0",
               count(IF(klt.status = 1, 1, NULL)) "1",
               count(IF(klt.status = 2, 1, NULL)) "2",
               count(IF(klt.status = 3, 1, NULL)) "3",
               count(IF(klt.status = 4, 1, NULL)) "4",
               count(IF(klt.status = 5, 1, NULL)) "5",
               count(IF(klt.status = 6, 1, NULL)) "6"
        from kwp_ledger_trade klt
                 inner join kwp_ledger_trade_unit kltu
                            on klt.id = kltu.t_ledger_id and kltu.del_flag = 0 and
                               kltu.unit_type = #{tradeReq.unitType,jdbcType=INTEGER}
                 inner join kwp_ledger_trade_unit kltu2
                            on klt.id = kltu2.t_ledger_id and kltu2.del_flag = 0 and kltu2.unit_type =
                                                                                     #{tradeReq.unitTypeTwo,jdbcType=INTEGER}
        <where>
            klt.del_flag = 0
              and kltu2.top_ent_id = #{tradeReq.entId,jdbcType=BIGINT}
            <if test="tradeReq.status != null">
                and klt.status = #{tradeReq.status}
            </if>
            <if test="tradeReq.trading != null">
                and klt.trading = #{tradeReq.trading,jdbcType=INTEGER}
            </if>
            <if test="tradeReq.endCreateTime != null and tradeReq.endCreateTime != '' and tradeReq.startCreateTime != null and tradeReq.startCreateTime != ''">
                and klt.generate_time between #{tradeReq.startCreateTime,jdbcType=TIMESTAMP}
                    and #{tradeReq.endCreateTime,jdbcType=TIMESTAMP}
            </if>
            <if test="tradeReq.keywords != null and tradeReq.keywords != ''">
                and (
                            klt.t_ledger_no like concat('%'
                            , #{tradeReq.keywords,jdbcType=VARCHAR}
                            , '%')
                        or kltu.firm_name like concat('%'
                        , #{tradeReq.keywords,jdbcType=VARCHAR}
                        , '%')
                        or kltu.contacts like concat('%'
                        , #{tradeReq.keywords,jdbcType=VARCHAR}
                        , '%')
                    )
            </if>
        </where>
    </select>

    <select id="selectSellIds" resultType="com.sckw.payment.model.dto.LedgerSellDto">
        select klt.id,
               klt.t_ledger_no   tLedgerNo,
               klt.name,
               klt.start_time    startTime,
               klt.end_time      endTime,
               klt.tax_rate      taxRate,
               klt.trading,
               klt.total_price   totalPrice,
               klt.ex_tax_price  exTaxPrice,
               klt.settle_price  settlePrice,
               klt.actual_price  actualPrice,
               klt.audit_phone   auditPhone,
               klt.audit_user    auditUser,
               klt.url,
               klt.generate_time generateTime,
               klt.receipt_time  receiptTime,
               klt.remark,
               klt.status,
               kltu.contacts,
               kltu.phone,
               kltu.firm_name    firmName,
               klt.order_count   orderCount
        from kwp_ledger_trade klt
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0
        <where>
            klt.del_flag = 0
              and kltu.unit_type = 1
              and klt.id in
            <foreach collection="ids" item="id" close=")" open="(" separator=",">
                #{id,jdbcType=BIGINT}
            </foreach>
        </where>
        order by klt.generate_time desc
    </select>
    <select id="selectPurchaseIds" resultType="com.sckw.payment.model.dto.LedgerPurchaseDto">
        select klt.id,
               klt.t_ledger_no   tLedgerNo,
               klt.name,
               klt.start_time    startTime,
               klt.end_time      endTime,
               klt.tax_rate      taxRate,
               klt.trading,
               klt.total_price   totalPrice,
               klt.ex_tax_price  exTaxPrice,
               klt.settle_price  settlePrice,
               klt.actual_price  actualPrice,
               klt.audit_phone   auditPhone,
               klt.audit_user    auditUser,
               klt.url,
               klt.generate_time generateTime,
               klt.receipt_time  receiptTime,
               klt.remark,
               klt.status,
               kltu.contacts,
               kltu.phone,
               kltu.firm_name    firmName,
               klt.order_count   orderCount
        from kwp_ledger_trade klt
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0
        <where>
            klt.del_flag = 0
              and kltu.unit_type = 2
              and klt.id in
            <foreach collection="ids" item="id" close=")" open="(" separator=",">
                #{id,jdbcType=BIGINT}
            </foreach>
        </where>
        order by klt.generate_time desc
    </select>
    <select id="selectId" resultType="com.sckw.payment.model.dto.LedgerTradeDto">
        select klt.id,
               klt.t_ledger_no   tLedgerNo,
               klt.name,
               klt.start_time    startTime,
               klt.end_time      endTime,
               klt.tax_rate      taxRate,
               klt.trading,
               klt.total_price   totalPrice,
               klt.ex_tax_price  exTaxPrice,
               klt.settle_price  settlePrice,
               klt.actual_price  actualPrice,
               klt.audit_phone   auditPhone,
               klt.audit_user    auditUser,
               klt.url,
               klt.generate_time generateTime,
               klt.receipt_time  receiptTime,
               klt.remark,
               klt.status,
               kltu.contacts,
               kltu.top_ent_id purchaseEntId,
               kltu.phone,
               kltu.firm_name    firmName,
               klt.order_count   orderCount
        from kwp_ledger_trade klt
                 inner join kwp_ledger_trade_unit kltu on klt.id = kltu.t_ledger_id and kltu.del_flag = 0
        <where>
            klt.del_flag = 0
              and kltu.unit_type = #{unitType,jdbcType=INTEGER}
              and klt.id = #{id,jdbcType=BIGINT}
        </where>
    </select>
</mapper>