<?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.ho.business.mapper.IncomeTotalMapper">

    <resultMap id="BaseResultMap" type="com.ho.business.entity.IncomeTotal">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="templateId" column="template_id" jdbcType="VARCHAR"/>
        <result property="rateId" column="rate_id" jdbcType="INTEGER"/>
        <result property="ele" column="ele" jdbcType="DECIMAL"/>
        <result property="eleTotalFee" column="ele_total_fee" jdbcType="DECIMAL"/>
        <result property="incomeFee" column="income_fee" jdbcType="DECIMAL"/>
        <result property="totalFee" column="total_fee" jdbcType="DECIMAL"/>
        <result property="flag" column="flag" jdbcType="INTEGER"/>
        <result property="date" column="date" jdbcType="DATE"/>
        <result property="srcId" column="src_id" jdbcType="INTEGER"/>
        <result property="stationId" column="station_id" jdbcType="INTEGER"/>
        <result property="dataDate" column="data_date" jdbcType="TIMESTAMP"/>
        <result property="groupId" column="group_id" jdbcType="INTEGER"/>
        <result property="type" column="type" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        ,template_id,rate_id,
        ele,ele_total_fee,income_fee,
        total_fee,flag,date,
        src_id,station_id,data_date,
        group_id,type
    </sql>

    <select id="selectByDate" resultType="com.ho.business.entity.IncomeTotal">
        select
        <include refid="Base_Column_List"/>
        from income_total
        <where>
            <if test="stationId != null">
                and station_id = #{stationId}
            </if>
            <if test="list !=null and list.size() !=0">
                and src_id in
                <foreach collection="list" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>

            <if test="startDayTime != null ">
                and data_date > #{startDayTime}
            </if>
            <if test="endDayTime != null">
                and data_date &lt;= #{endDayTime}
            </if>
            and type = 2
        </where>
    </select>

    <select id="selectByStations" resultType="com.ho.business.entity.IncomeTotal">
        select
        <include refid="Base_Column_List"/>
        from income_total
        <where>
            <if test="list !=null and list.size() !=0">
                and station_id in
                <foreach collection="list" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="startDayTime != null and endDayTime != null">
                and date between #{startDayTime} and #{endDayTime}
            </if>
            and type = 2
        </where>
    </select>

    <select id="selectDailySumByStationId"
            resultType="com.ho.business.vo.resp.report.CurrentMonthReportRespVO">
        select sum(ele) ele,
               sum(ele) powerGeneration,
            date
        from income_total
        where station_id = #{stationId}
        <if test="beginTime != null and endTime != null">
            and date BETWEEN #{beginTime} and #{endTime}
        </if>
          and type = 2
        GROUP BY date
    </select>

    <select id="selectFeeByStationId" resultType="com.ho.business.vo.resp.report.HistoryReportRespVO">
        select sum(ele_total_fee) eleTotalFee,
               sum(income_fee)    incomeFee,
               sum(total_fee)     totalFee,
               sum(ele)           powerGeneration,
            date
        from income_total
        where  type = 2
        <if test="stationId != null">
            and station_id = #{stationId}
        </if>
        <if test="groupId != null">
            and group_id = #{groupId}
        </if>
        <if test="beginTime != null and endTime != null ">
            and  date BETWEEN #{beginTime} and #{endTime}
        </if>
        GROUP BY date
        order by date asc
    </select>
    <select id="selectSumIncomeByStationId" resultType="java.math.BigDecimal">
        select
        sum(total_fee)     totalFee
        from income_total
        where  type = 2
        <if test="stationId != null">
            and station_id = #{stationId}
        </if>
    </select>
    <select id="selectSumIncomeByGroupId" resultType="java.math.BigDecimal">
        select
        sum(total_fee)     totalFee
        from income_total
        where  type = 2
        <if test="groupId != null">
            and group_id = #{groupId}
        </if>
    </select>


    <insert id="insertRealDataBatch">
        insert into income_total ( template_id,rate_name,rate_id,price,ele,ele_total_fee,total_fee,flag,date,src_id,
        station_id,data_date,group_id,type,discount,start_time,end_time )
        values
        <foreach item="item" collection="list" index="index" separator=",">
            (
            #{item.templateId}, #{item.rateName}, #{item.rateId},#{item.price},#{item.ele},
            #{item.eleTotalFee}, #{item.totalFee}, 0, #{item.date},#{item.srcId},
            #{item.stationId},#{item.dataDate},#{item.groupId}, 2, #{item.discount},
            #{item.startTime},#{item.endTime}
            )
        </foreach>
    </insert>
</mapper>
