<?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.EarningsCalculateMapper">

    <sql id="Base_Column_List">
        id
        ,group_id,pid,
        station_id,src_id,dept_id,
        create_time,day,type,
        digital,elec,rate_type,price,total
    </sql>

    <select id="selectList" resultType="com.ho.business.entity.EarningsCalculate">
        SELECT
        <include refid="Base_Column_List"/>
        from earnings_calculate
        <where>
            and station_id = #{ec.stationId}
            <if test="ec.typeList!=null and ec.typeList.size() !=0">
                and type in
                <foreach collection="ec.typeList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            and day BETWEEN #{ec.beginTime}
            AND #{ec.endTime}
        </where>
        order by `day`
    </select>

    <select id="totalList" resultType="com.ho.business.entity.EarningsCalculate">
        SELECT ifnull(sum(digital),0) digital,ifnull(sum(elec),0) elec,ifnull(sum(total),0) total,type,rate_type
        from earnings_calculate
        <where>
            and station_id = #{ec.stationId}
            <if test="ec.beginTime!=null and  ec.endTime != null">
                and day BETWEEN #{ec.beginTime} AND #{ec.endTime}
            </if>
            group by `type`,rate_type
        </where>
    </select>

    <select id="totalListByDay" resultType="com.ho.business.entity.EarningsCalculate">
        SELECT sum(digital) digital,sum(elec) elec,sum(total) total,day,type,rate_type
        from earnings_calculate
        <where>
            and station_id = #{ec.stationId}
            <if test="ec.beginTime!=null and  ec.endTime != null">
                and day BETWEEN #{ec.beginTime} AND #{ec.endTime}
            </if>
            group by day,`type`,rate_type
        </where>
    </select>

    <insert id="insertSelective" keyColumn="id" keyProperty="id"
            parameterType="com.ho.business.entity.EarningsCalculate"
            useGeneratedKeys="true">
        insert into earnings_calculate
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="groupId != null">group_id,</if>
            <if test="pid != null">pid,</if>
            <if test="stationId != null">station_id,</if>
            <if test="srcId != null">src_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="day != null">day,</if>
            <if test="type != null">type,</if>
            <if test="digital != null">digital,</if>
            <if test="elec != null">elec,</if>
            <if test="rateType != null">rate_type,</if>
            <if test="price != null">price,</if>
            <if test="total != null">total,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id,jdbcType=INTEGER},</if>
            <if test="groupId != null">#{groupId,jdbcType=INTEGER},</if>
            <if test="pid != null">#{pid,jdbcType=INTEGER},</if>
            <if test="stationId != null">#{stationId,jdbcType=INTEGER},</if>
            <if test="srcId != null">#{srcId,jdbcType=INTEGER},</if>
            <if test="deptId != null">#{deptId,jdbcType=INTEGER},</if>
            <if test="createTime != null">#{createTime,jdbcType=TIMESTAMP},</if>
            <if test="day != null">#{day,jdbcType=VARCHAR},</if>
            <if test="type != null">#{type,jdbcType=VARCHAR},</if>
            <if test="digital != null">#{digital,jdbcType=VARCHAR},</if>
            <if test="elec != null">#{elec},</if>
            <if test="rateType != null">#{rateType},</if>
            <if test="price != null">#{price},</if>
            <if test="total != null">#{total},</if>
        </trim>
    </insert>


    <delete id="deleteByDay">
        delete
        from earnings_calculate
        <where>
            and day = #{day}
            <if test="typeList !=null and typeList.size() !=0">
                and type in
                <foreach collection="typeList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </delete>

    <delete id="deleteByStationAndDay">
        delete
        from earnings_calculate
        <where>
            and day = #{day}
            <if test="typeList !=null and typeList.size() !=0">
                and type in
                <foreach collection="typeList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            and station_id=#{stationId}
        </where>
    </delete>

    <insert id="insertBatch">
        INSERT INTO earnings_calculate
        (group_id, pid, station_id, src_id, dept_id, create_time, day, type,
        digital,elec,rate_type,price,total,discount)
        VALUES
        <foreach item="item" collection="list" index="index" separator=",">
            (
            #{item.groupId},#{item.pid},#{item.stationId},#{item.srcId},#{item.deptId}, now(),
            #{item.day},#{item.type},#{item.digital},#{item.elec},#{item.rateType},#{item.price},#{item.total},#{item.discount}
            )
        </foreach>
    </insert>

    <select id="queryElecTemplateSubByStationId" resultType="com.ho.business.entity.EarningsCalculateSub">
        select sub.id,sub.begin_time,sub.end_time,sub.type,sub.price,tem.discount from elec_template_sub sub
        right join elec_template tem on tem.id = sub.pid
        where station_id = #{stationId} and tem.is_enable = 1
        <if test="date == null">
            and date_format(date_sub(now(),interval 1 day),'%Y-%m-%d') >= tem.validity_start_time and
            date_format(date_sub(now(),interval 1 day),'%Y-%m-%d') &lt;= tem.validity_end_time
        </if>
        <if test="date != null">
            and date_format(#{date},'%Y-%m-%d') >= tem.validity_start_time and date_format(#{date},'%Y-%m-%d') &lt;=
            tem.validity_end_time
        </if>
        <!--        <if test="templateType != null">-->
        <!--            and template_type = #{templateType}-->
        <!--        </if>-->
        order by begin_time
    </select>

    <select id="queryElecRateTemplateSubByStationId" resultType="com.ho.business.entity.EarningsCalculateSub">
        select distinct sub.type,sub.price from elec_template_sub sub
        right join elec_template tem on tem.id = sub.pid
        where station_id = #{stationId} and tem.is_enable = 1
        <if test="date == null">
            and date_format(date_sub(now(),interval 1 day),'%Y-%m-%d') >= tem.validity_start_time and
            date_format(date_sub(now(),interval 1 day),'%Y-%m-%d') &lt;= tem.validity_end_time
        </if>
        <if test="date != null">
            and date_format(#{date},'%Y-%m-%d') >= tem.validity_start_time and date_format(#{date},'%Y-%m-%d') &lt;=
            tem.validity_end_time
        </if>
        order by sub.type
    </select>

    <select id="getSrcIdByStationId" resultType="java.lang.Integer">
        select src_id
        from device
        where device_type = #{deviceType}
          and station_id = #{stationId} limit 1
    </select>

    <select id="getColCount" resultType="java.lang.Double">
        select ifnull(offset, 0) as data
        from col_count
        where station_id = #{stationId}
          and col = #{col}
          and count_flag = 2
    </select>

    <select id="queryData" resultType="com.ho.business.entity.MonFreezeTotal">
        select total_charge, total_discharge, month
        from mon_freeze_total
        where station_id = #{stationId}
          and month = #{month}
    </select>

    <insert id="batchInsertData" parameterType="com.ho.business.entity.MonFreezeTotal">
        insert into mon_freeze_total (station_id,total_charge,total_discharge,month)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.stationId},#{item.totalCharge},#{item.totalDischarge},#{item.month})
        </foreach>
    </insert>


    <select id="selectSumIncomeByStationId" resultType="java.math.BigDecimal">
        select
        sum(digital)
        from earnings_calculate
        where type = #{type}
        <if test="stationId != null">
            and station_id = #{stationId}
        </if>
        and day BETWEEN #{beginTime}
        AND #{endTime}
    </select>

    <select id="queryRegionDataByDistrict" resultType="com.ho.business.vo.RegionData">
        select p.name      province_name,
               p.region_id province_region_id,
               c.name      city_name,
               c.region_id city_region_id,
               d.name      district_name,
               d.region_id district_region_id
        from dict_province p
                 left join dict_city c on p.id = c.p_id
                 left join dict_district d on c.id = d.p_id
        where
            (p.name = #{district} and c.name = #{city}) or c.name = #{city} or d.name = #{province}
            limit 1
    </select>
    <select id="getGroupProfit" resultType="com.ho.business.vo.resp.profit.GroupProfitType">
        select
        sum(digital) as digital,type
        from earnings_calculate
        <where>
            digital > 0
            <if test="groupId != null">
                and group_id = #{groupId}
            </if>
            <if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
                and day BETWEEN #{beginTime}
                AND #{endTime}
            </if>
        </where>
        group by type
    </select>
    <select id="getStationProfit" resultType="com.ho.business.vo.resp.profit.StationProfitType">
        select
        station_id,type,sum(digital) as digital
        from earnings_calculate
        <where>
            digital > 0
            <if test="groupId != null">
                and group_id = #{groupId}
            </if>
            <if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
                and day BETWEEN #{beginTime}
                AND #{endTime}
            </if>
        </where>
        group by station_id,type
    </select>
    <select id="getDayProfit" resultType="com.ho.business.vo.resp.profit.DayProfitType">
        select
        day,type,sum(digital) as digital
        from earnings_calculate
        <where>
            digital > 0
            <if test="groupId != null">
                and group_id = #{groupId}
            </if>
            <if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
                and day BETWEEN #{beginTime}
                AND #{endTime}
            </if>
        </where>
        group by day,type
    </select>
    <select id="getFirstTime" resultType="java.lang.String">
        SELECT min(day) day
        FROM earnings_calculate;
    </select>
    <select id="getProfit" resultType="com.ho.business.vo.resp.profit.DayProfitType">
        select
        station_id,day,type,sum(digital) as digital
        from earnings_calculate
        <where>
            digital > 0
            <if test="groupId != null">
                and group_id = #{groupId}
            </if>
            <if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
                and day BETWEEN #{beginTime}
                AND #{endTime}
            </if>
        </where>
        group by station_id, day,type

    </select>

    <select id="getEarningsByParam" resultType="com.ho.business.vo.resp.profit.DayProfitType">
        select
        station_id,day,type,sum(digital) as digital
        from earnings_calculate
        <where>
            digital > 0
            <if test="groupId != null">
                and group_id = #{groupId}
            </if>
            <if test="beginTime != null and beginTime != '' and endTime != null and endTime != ''">
                and day BETWEEN #{beginTime}
                AND #{endTime}
            </if>
            <if test="stationIds != null and stationIds.size() != 0">
                and station_id in
                <foreach collection="stationIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        group by station_id, day,type
    </select>

    <delete id="deleteByStationId">
        delete
        from earnings_calculate
        <where>
            and day = #{day}
            and station_id = #{stationId}
            <if test="typeList !=null and typeList.size() !=0">
                and type in
                <foreach collection="typeList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </delete>

</mapper>
