<?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.KwpSettlementRecordMapper">
    <resultMap id="BaseResultMap" type="com.sckw.payment.model.KwpSettlementRecord">
        <!--@mbg.generated-->
        <!--@Table kwp_settlement_record-->
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="ent_id" jdbcType="BIGINT" property="entId"/>
        <result column="total_receive_money" jdbcType="DECIMAL" property="totalReceiveMoney"/>
        <result column="exact_money" jdbcType="DECIMAL" property="exactMoney"/>
        <result column="received_money" jdbcType="DECIMAL" property="receivedMoney"/>
        <result column="overdue_exact_money" jdbcType="DECIMAL" property="overdueExactMoney"/>
        <result column="total_pay_money" jdbcType="DECIMAL" property="totalPayMoney"/>
        <result column="unpaid_money" jdbcType="DECIMAL" property="unpaidMoney"/>
        <result column="paid_money" jdbcType="DECIMAL" property="paidMoney"/>
        <result column="overdue_paid_money" jdbcType="DECIMAL" property="overduePaidMoney"/>
        <result column="create_time" jdbcType="DATE" property="createTime"/>
        <result column="order_type" jdbcType="INTEGER" property="orderType"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id,
        ent_id,
        total_receive_money,
        exact_money,
        received_money,
        overdue_exact_money,
        total_pay_money,
        unpaid_money,
        paid_money,
        overdue_paid_money,
        create_time,
        order_type
    </sql>

    <insert id="insertBatch">
        insert into kwp_settlement_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            id, ent_id, total_receive_money, exact_money, received_money, overdue_exact_money,
            total_pay_money, unpaid_money, paid_money, overdue_paid_money, create_time, order_type
        </trim>
        values
        <foreach collection="list" item="item" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                #{item.id,jdbcType=BIGINT},
                #{item.entId,jdbcType=BIGINT},
                #{item.totalReceiveMoney,jdbcType=DECIMAL},
                #{item.exactMoney,jdbcType=DECIMAL},
                #{item.receivedMoney,jdbcType=DECIMAL},
                #{item.overdueExactMoney,jdbcType=DECIMAL},
                #{item.totalPayMoney,jdbcType=DECIMAL},
                #{item.unpaidMoney,jdbcType=DECIMAL},
                #{item.paidMoney,jdbcType=DECIMAL},
                #{item.overduePaidMoney,jdbcType=DECIMAL},
                #{item.createTime,jdbcType=TIMESTAMP},
                #{item.orderType,jdbcType=INTEGER}
            </trim>
        </foreach>
    </insert>
    <select id="countByDay" resultType="com.sckw.payment.model.vo.SettlementRecord">
        SELECT temp.date                               createTime,
               ifnull(sum(ksr.total_receive_money), 0) totalReceiveMoney,
               ifnull(sum(ksr.exact_money), 0)         exactMoney,
               ifnull(sum(ksr.received_money), 0)      receivedMoney,
               ifnull(sum(ksr.overdue_exact_money), 0) overdueExactMoney,
               ifnull(sum(ksr.total_pay_money), 0)     totalPayMoney,
               ifnull(sum(ksr.unpaid_money), 0)        unpaidMoney,
               ifnull(sum(ksr.paid_money), 0)          paidMoney,
               ifnull(sum(ksr.overdue_paid_money), 0)  overduePaidMoney
        FROM (
        <foreach collection="timeAll" item="date" separator="UNION ALL">
            SELECT #{date} AS date
        </foreach>
        ) AS temp
            left join
        kwp_settlement_record ksr on temp.date = ksr.create_time and ksr.ent_id = #{entId,jdbcType=BIGINT}
        <where>
            <if test="time != null and time.size() != 0">
                temp.date between
                <foreach collection="time" open=" " separator="and" close=" " item="item">
                    #{item,jdbcType=TIMESTAMP}
                </foreach>
            </if>
        </where>
        group by temp.date
        order by temp.date
    </select>
    <!--原方案 按月统计-->
    <select id="countMonth2" resultType="com.sckw.payment.model.vo.SettlementRecord">
        SELECT temp.date                               createTime,
               ifnull(sum(ksr.total_receive_money), 0) totalReceiveMoney,
               ifnull(sum(ksr.exact_money), 0)         exactMoney,
               ifnull(sum(ksr.received_money), 0)      receivedMoney,
               ifnull(sum(ksr.overdue_exact_money), 0) overdueExactMoney,
               ifnull(sum(ksr.total_pay_money), 0)     totalPayMoney,
               ifnull(sum(ksr.unpaid_money), 0)        unpaidMoney,
               ifnull(sum(ksr.paid_money), 0)          paidMoney,
               ifnull(sum(ksr.overdue_paid_money), 0)  overduePaidMoney
        FROM (
        <foreach collection="timeAll" item="date" separator="UNION ALL">
            SELECT #{date} AS date
        </foreach>
        ) AS temp
            left join
        kwp_settlement_record ksr
        on temp.date = date_format(ksr.create_time, '%Y-%m') and ksr.ent_id = #{entId,jdbcType=BIGINT}
        <where>
            <if test="time != null and time.size() != 0">
                temp.date between
                <foreach collection="time" open=" " separator="and" close=" " item="item">
                    #{item,jdbcType=TIMESTAMP}
                </foreach>
            </if>
        </where>
        group by temp.date
        order by temp.date
    </select>
    <!--    支持时间范围筛选的按月统计-->
    <select id="countMonth" resultType="com.sckw.payment.model.vo.SettlementRecord">
        select temp.date as createTime,
               ifnull(tmpd.totalReceiveMoney, 0),
               ifnull(tmpd.exactMoney, 0),
               ifnull(tmpd.receivedMoney, 0),
               ifnull(tmpd.overdueExactMoney, 0),
               ifnull(tmpd.totalPayMoney, 0),
               ifnull(tmpd.unpaidMoney, 0),
               ifnull(tmpd.paidMoney, 0),
               ifnull(tmpd.overduePaidMoney, 0)
        FROM (
        <foreach collection="timeAll" item="date" separator="UNION ALL">
            SELECT #{date} AS date
        </foreach>
        ) AS temp
            left join
        (SELECT date_format(ksr.create_time, '%Y-%m')   createTime,
                ifnull(sum(ksr.total_receive_money), 0) totalReceiveMoney,
                ifnull(sum(ksr.exact_money), 0)         exactMoney,
                ifnull(sum(ksr.received_money), 0)      receivedMoney,
                ifnull(sum(ksr.overdue_exact_money), 0) overdueExactMoney,
                ifnull(sum(ksr.total_pay_money), 0)     totalPayMoney,
                ifnull(sum(ksr.unpaid_money), 0)        unpaidMoney,
                ifnull(sum(ksr.paid_money), 0)          paidMoney,
                ifnull(sum(ksr.overdue_paid_money), 0)  overduePaidMoney
         FROM kwp_settlement_record ksr
        <where>
            ksr.ent_id = #{entId,jdbcType=BIGINT}
            <if test="time != null and time.size() != 0">
                and ksr.create_time between
                <foreach collection="time" open=" " separator="and" close=" " item="item">
                    #{item,jdbcType=TIMESTAMP}
                </foreach>
            </if>
        </where>
        group by date_format(ksr.create_time, '%Y-%m')) as tmpd on temp.date = tmpd.createTime
        order by temp.date
    </select>
</mapper>