<?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.kym.mapper.miniapp.ChargeOrderMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.kym.entity.miniapp.ChargeOrder">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="station_id" property="stationId"/>
        <result column="start_charge_seq" property="startChargeSeq"/>
        <result column="connector_id" property="connectorId"/>
        <result column="is_booking" property="isBooking"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="soc" property="soc"/>
        <result column="total_power" property="totalPower"/>
        <result column="total_money" property="totalMoney"/>
        <result column="elec_money" property="elecMoney"/>
        <result column="service_money" property="serviceMoney"/>
        <result column="pay_amount" property="payAmount"/>
        <result column="discount_type" property="discountType"/>
        <result column="discount_amount" property="discountAmount"/>
        <result column="service_money_discount" property="serviceMoneyDiscount"/>
        <result column="pay_service_amount" property="payServiceAmount"/>
        <result column="sum_period" property="sumPeriod"/>
        <result column="charge_detail" property="chargeDetail"/>
        <result column="order_status" property="orderStatus"/>
        <result column="charge_status" property="chargeStatus"/>
        <result column="stop_reason" property="stopReason"/>
        <result column="invoice_status" property="invoiceStatus"/>
        <result column="invoice_id" property="invoiceId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <resultMap id="CustomChargeOrderMap" type="com.kym.entity.admin.vo.CustomOrderVo">
        <result column="user_id" property="userId"/>
        <result column="mobile_phone" property="mobilePhone"/>
        <result column="start_charge_seq" property="startChargeSeq"/>
        <result column="station_id" property="stationId"/>
        <result column="connector_id" property="connectorId"/>
        <result column="is_booking" property="isBooking"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="total_power" property="totalPower"/>
        <result column="total_money" property="totalMoney"/>
        <result column="elec_money" property="elecMoney"/>
        <result column="service_money" property="serviceMoney"/>
        <result column="pay_amount" property="payAmount"/>
        <result column="discount_type" property="discountType"/>
        <result column="discount_amount" property="discountAmount"/>
        <result column="service_money_discount" property="serviceMoneyDiscount"/>
        <result column="pay_service_amount" property="payServiceAmount"/>
        <result column="order_status" property="orderStatus"/>
        <result column="charge_status" property="chargeStatus"/>
        <result column="stop_reason" property="stopReason"/>
        <result column="invoice_status" property="invoiceStatus"/>
        <result column="invoice_id" property="invoiceId"/>
    </resultMap>

    <resultMap id="StationStatMap" type="com.kym.entity.admin.vo.StationStatVo">
<!--        <result column="station_id" property="stationId"/>-->
        <result column="start_time" property="startTime"/>
        <result column="total_orders" property="totalOrders"/>
        <result column="total_power" property="totalPower"/>
        <result column="total_money" property="totalMoney"/>
        <result column="elec_money" property="elecMoney"/>
        <result column="service_money" property="serviceMoney"/>
        <result column="pay_service_amount" property="payServiceAmount"/>

    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, user_id, station_id, start_charge_seq, connector_id, is_booking,start_time, end_time, soc, total_power, total_money, elec_money, service_money,pay_amount,discount_type,discount_amount,service_money_discount, sum_period, charge_detail, order_status, charge_status, stop_reason, invoice_status,invoice_id,create_time, update_time
    </sql>

    <select id="statChargeOrders" resultType="java.util.Map"
            parameterType="com.kym.entity.admin.queryParams.CustomChargeOrdersQueryParam">
        SELECT
        count(*) totalOrders,
        sum(t1.total_power) totalPower,
        sum(t1.total_money) totalMoney,
        sum(t1.elec_money) elecMoney,
        sum(t1.service_money) serviceMoney,
        sum(t1.service_money_discount) serviceMoneyDiscount,
        (sum(t1.service_money) - sum(t1.service_money_discount)) payServiceAmount
        FROM
        t_charge_order t1
        LEFT JOIN t_user t2
        ON t1.user_id = t2.id
        <where>
            <if test="params.userId != null">
                and t1.user_id = #{params.userId}
            </if>
            <if test="params.mobilePhone != null and params.mobilePhone != ''">
                and t2.mobile_phone = #{params.mobilePhone}
            </if>
            <if test="params.connectorId != null and params.connectorId != ''">
                and t1.connector_id = #{params.connectorId}
            </if>
            <if test="params.startChargeSeq != null and params.startChargeSeq != ''">
                and t1.start_charge_seq = #{params.startChargeSeq}
            </if>
            <if test="params.orderStatus != null">
                and t1.order_status = #{params.orderStatus}
            </if>
            <if test="params.startDate != null">
                and t1.start_time &gt;= #{params.startDate}
            </if>
            <if test="params.endDate != null">
                and t1.start_time &lt;= #{params.endDate}
            </if>
            <if test="params.stationIds != null and params.stationIds.size() > 0">
                and t1.station_id IN
                <foreach collection="params.stationIds" item="stationId" index="index" open="(" close=")" separator=",">
                    #{stationId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="listChargeOrders" resultMap="CustomChargeOrderMap"
            parameterType="com.kym.entity.admin.queryParams.CustomChargeOrdersQueryParam">
        SELECT
        t1.user_id,
        t2.mobile_phone,
        t1.start_charge_seq,
        t1.station_id,
        t1.connector_id,
        t1.start_time,
        t1.end_time,
        t1.total_power,
        t1.total_money,
        t1.elec_money,
        t1.service_money,
        t1.pay_amount,
        t1.discount_amount,
        t1.service_money_discount,
        (t1.service_money - t1.service_money_discount) pay_service_amount,
        t1.order_status,
        t1.charge_status,
        t1.invoice_status,
        t1.stop_reason
        FROM
        t_charge_order t1
        LEFT JOIN t_user t2
        ON t1.user_id = t2.id
        <where>
            <if test="params.userId != null">
                and t1.user_id = #{params.userId}
            </if>
            <if test="params.mobilePhone != null and params.mobilePhone != ''">
                and t2.mobile_phone = #{params.mobilePhone}
            </if>
            <if test="params.connectorId != null and params.connectorId != ''">
                and t1.connector_id = #{params.connectorId}
            </if>
            <if test="params.startChargeSeq != null and params.startChargeSeq != ''">
                and t1.start_charge_seq = #{params.startChargeSeq}
            </if>
            <if test="params.chargeStatus != null">
                and t1.charge_status = #{params.chargeStatus}
            </if>
               <if test="params.orderStatus != null">
                and t1.order_status = #{params.orderStatus}
            </if>
            <if test="params.startDate != null">
                and t1.start_time &gt;= #{params.startDate}
            </if>
            <if test="params.endDate != null">
                and t1.start_time &lt;= #{params.endDate}
            </if>
            <if test="params.invoiceStatus != null">
                and t1.invoice_status = #{params.invoiceStatus}
            </if>
            <if test="params.stopReason != null">
                and t1.stop_reason = #{params.stopReason}
            </if>
            <if test="params.stationIds != null and params.stationIds.size() > 0">
                and t1.station_id IN
                <foreach collection="params.stationIds" item="stationId" index="index" open="(" close=")" separator=",">
                    #{stationId}
                </foreach>
            </if>
        </where>
        order by t1.id desc
    </select>

    <select id="stationStat" resultMap="StationStatMap" parameterType="com.kym.entity.admin.queryParams.StatQueryParam">
        SELECT
        <if test="params.stationIds != null and params.stationId != ''">
            t1.station_id,
        </if>
        <if test="params.type == null or params.type == 'day'">
            DATE_FORMAT(t1.start_time, '%Y-%m-%d')
        </if>
        <if test="params.type != null and params.type == 'month'">
            DATE_FORMAT(t1.start_time, '%Y-%m')
        </if>
        stat_time,
        count(t1.id) total_orders,
        SUM(t1.total_power) total_power,
        SUM(t1.total_money) total_money,
        SUM(t1.elec_money) elec_money,
        SUM(t1.service_money) service_money,
        (SUM(t1.service_money) - SUM(t1.service_money_discount)) pay_service_amount
        FROM
        (SELECT
        *
        FROM
        t_charge_order
        <where>
            <if test="params.stationId != null and params.stationId != ''">
                and station_id = #{params.stationId}
            </if>
        </where>
        ) t1
        GROUP BY stat_time
        <trim prefix="having" suffixOverrides="and">
            <if test="params.startTime != null">
                stat_time &gt;= #{params.startTime} and
            </if>
            <if test="params.endTime != null">
                stat_time &lt;= #{params.endTime} and
            </if>
        </trim>
        order by stat_time desc
    </select>

    <select id="stationTodayStat" resultType="java.util.Map" parameterType="String">
        SELECT
        COUNT(DISTINCT (t1.user_id)) chargeUsers,
        COUNT(t1.id) totalOrders,
        IFNULL (SUM(t1.total_power),0) totalPower,
        IFNULL(SUM(t1.total_money),0) totalMoney,
        IFNULL(SUM(t1.pay_amount),0) payAmount,
        IFNULL(SUM(t1.elec_money),0) elecMoney,
        IFNULL(SUM(t1.service_money),0) serviceMoney,
        (IFNULL(SUM(t1.service_money),0) - IFNULL(SUM(t1.service_money_discount),0)) payServiceAmount
        FROM
        (SELECT
        *
        FROM
        t_charge_order
        <where>
            <if test="stationId != null and stationId != ''">
                AND station_id = #{stationId}
            </if>
            AND start_time &gt;= DATE_FORMAT(NOW(), '%Y-%m-%d 00:00:00')
            AND start_time &lt;= DATE_FORMAT(NOW(), '%Y-%m-%d 23:59:59')) t1
        </where>
    </select>

</mapper>
