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


    <!-- 根据电站id查询费率模板下拉列表数据 -->
    <select id="getRateTemplateList" resultType="com.ho.business.entity.RateTemplate">
        select id,
               template_name
        from income_rate_template
        where station_id = #{stationId}
          and open = 1
    </select>

    <!-- 根据模板id查询费率下拉列表数据 -->
    <select id="getRateList" resultType="com.ho.business.entity.IncomeRateMapping">
        select rate_id,
               rate_name,
               start_time,
               end_time
        from income_rate_mapping
        where template_id = #{templateId}
    </select>

    <!-- 根据模板id和费率id查询费率详情 -->
    <select id="getRateDetails" resultType="com.ho.business.entity.IncomeRateMapping">
        select price,
               start_time,
               end_time,
               discount
        from income_rate_mapping
        where rate_id = #{rateId}
          and template_id = #{templateId}
    </select>


    <!-- 根据电站id查询企业用电费率 -->
    <select id="selectIncomeRates" resultType="com.ho.business.entity.IncomeRate">
        select q.id,
               q.start_time,
               q.end_time,
               q.rate_name,
               q.q,
               q.price,
               q.discount,
               q.template_id,
               q.rate_id,
               q.data_date,
               1             type,
               q.template_id old_template_id,
               q.rate_id     old_rate_id
        from income_rate q
                 left join income_rate_template tem on q.template_id = tem.id
        where tem.station_id = #{stationId}
          and date_format(q.data_date, '%Y-%m') = #{dataDate}
    </select>

    <!-- 查询上网用电数据 -->
    <select id="selectQRate" resultType="com.ho.business.entity.IncomeQRate">
        select q.id,
               q.surf_q,
               q.price,
               q.template_id,
               q.rate_id,
               q.data_date,
               q.discount,
               q.start_time,
               q.end_time,
               q.template_id old_template_id,
               q.rate_id     old_rate_id
        from income_q_rate q
                 left join income_rate_template tem on q.template_id = tem.id
        where tem.station_id = #{stationId}
          and date_format(q.data_date, '%Y-%m') = #{dataDate}
    </select>

    <!-- 删除(可批量) -->
    <delete id="deleteIncomeRates" parameterType="com.ho.business.entity.IncomeRate">
        delete
        from income_rate
        where station_id = #{stationId}
          and date_format(data_date, '%Y-%m') = #{dataDate}
    </delete>

    <!-- 批量插入企业用电费率 -->
    <insert id="batchIncomeRates" parameterType="com.ho.business.entity.IncomeRate">
        insert into income_rate
        (
        data_date,
        rate_id,
        template_id,
        q,
        start_time,
        end_time,
        rate_name,
        price,
        discount,
        station_id
        )
        values
        <foreach item="data" collection="incomeRates" separator=",">
            (
            concat(#{dataDate},'-01'),
            <if test="data.rateId != null and data.rateId != 0">
                #{data.rateId},
            </if>
            <if test="data.rateId == null or data.rateId == 0">
                (
                select rate_id from income_rate_mapping where rate_name = #{data.rateName} and template_id
                =#{data.templateId}
                ),
            </if>
            #{data.templateId},
            #{data.q},
            #{data.startTime},
            #{data.endTime},
            #{data.rateName},
            #{data.price},
            #{data.discount},
            #{data.stationId}
            )
        </foreach>
    </insert>

    <insert id="insertSurfQRate" parameterType="com.ho.business.entity.IncomeQRate">
        insert into income_q_rate (data_date, surf_q, rate_id, template_id, start_time, end_time, rate_name, price,
                                   discount)
        values (date_format(concat(#{surfQRate.dataDate}, '-01'), '%Y-%m-%d'), #{surfQRate.surfQ}, #{surfQRate.rateId},
                #{surfQRate.templateId}, #{surfQRate.startTime}, #{surfQRate.endTime}, #{surfQRate.rateName},
                #{surfQRate.price}, #{surfQRate.discount})
    </insert>

    <!-- 修改上网用电数据 -->
    <update id="modifySurfRate" parameterType="com.ho.business.entity.IncomeQRate">
        update income_q_rate
        set surf_q      = #{surfQ},
            template_id = #{templateId},
            rate_id     = #{rateId},
            rate_name   = #{rateName},
            start_time  = #{startTime},
            end_time    = #{endTime},
            price       = #{price},
            discount    = #{discount}
        where id = #{id}
    </update>

    <!-- 确认按钮 type = 2 -->
    <update id="confirmTwo" parameterType="com.ho.business.entity.IncomeTotal">
        update income_total
        set flag = 1
        where station_id = #{stationId}
          and date_format(date, '%Y-%m') = #{dataDate}
        and type = 2
    </update>

    <!-- 确认按钮 type = 1 -->
    <update id="confirmOne">
        update income_total
        set flag = 1
        where station_id = #{stationId}
          and date_format(data_date, '%Y-%m') = #{dataDate}
          and type = 1
    </update>

    <!-- 查询费率号(下拉框数据) -->
    <select id="selectRates" resultType="com.ho.business.entity.IncomeRateMapping">
        select rate_id,
               rate_name,
               station_id
        from income_rate_mapping
    </select>

    <!-- 查询累计收益 -->
    <select id="selectTotal" resultType="com.ho.business.entity.IncomeTotal">
        select
        ele_total_fee,
        income_fee,
        total_fee,
        flag,
        date,
        station_id
        from income_total
        where station_id = #{incomeTotal.stationId} and date_format(data_date, '%Y-%m') = #{incomeTotal.dataDate}
          and type = 1 limit 1
    </select>

    <!-- 查询累计收益 -->
    <select id="selectTotalReal" resultType="com.ho.business.entity.IncomeTotal">
        select
        sum(ele_total_fee) ele_total_fee,
        sum(income_fee) income_fee,
        sum(total_fee) total_fee,
        flag,
        station_id
        from income_total
        where type = 2
        <if test="incomeTotal.stationId != null">
            and station_id = #{incomeTotal.stationId}
        </if>
        <if test="incomeTotal.startTime != null and incomeTotal.endTime != null ">
            and  date BETWEEN #{incomeTotal.startTime} and #{incomeTotal.endTime}
        </if>
        group by station_id,flag
    </select>

    <!-- 根据模板id和当前系统时间查询费率 -->
    <select id="selectRate" resultType="com.ho.business.entity.IncomeRateMapping">
        select rate_id, price, discount
        from income_rate_mapping
        where template_id = #{templateId}
          and start_time &lt;= current_time
          and end_time >= current_time
    </select>

    <!-- 在累计收益表中插入一条数据 -->
    <insert id="insertTotalFee" parameterType="com.ho.business.entity.IncomeTotal">
        insert into income_total (ele_total_fee, income_fee, total_fee, flag, date, station_id, data_date, group_id,
                                  type, template_id, rate_id)
        values (#{incomeTotal.eleTotalFee}, #{incomeTotal.incomeFee}, #{incomeTotal.totalFee}, 0,
                current_date, #{incomeTotal.stationId}, str_to_date(concat(#{incomeTotal.dataDate}, '-01'), '%Y-%m-%d'),
                #{incomeTotal.groupId}, 1, #{incomeTotal.templateId}, #{incomeTotal.rateId})
    </insert>

    <!-- 修改企业用电收益 -->
    <update id="modifyEleTotalFee" parameterType="com.ho.business.entity.IncomeTotal">
        update income_total set
        <if test="eleTotalFee != 0">
            ele_total_fee = #{eleTotalFee},
        </if>
        <if test="incomeFee != 0">
            income_fee = #{incomeFee},
        </if>
        <if test="templateId != 0 and templateId != null">
            template_id = #{templateId},
        </if>
        <if test="surfTemplateId != 0 and surfTemplateId != null">
            surf_template_id = #{surfTemplateId},
        </if>
        rate_id = #{rateId},
        total_fee = (#{eleTotalFee} + #{incomeFee})
        where date_format(data_date, '%Y-%m') = #{dataDate} and station_id = #{stationId} and flag = 0 and type = 1
    </update>

    <select id="getIncomeFee" resultType="java.lang.String">
        select ifnull(income_fee, 0) income_fee
        from income_total
        where date_format(data_date, '%Y-%m') = #{dataDate}
          and station_id = #{stationId}
          and type = 1
    </select>

    <select id="getEleTotalFee" resultType="java.lang.String">
        select ifnull(ele_total_fee, 0) ele_total_fee
        from income_total
        where date_format(data_date, '%Y-%m') = #{dataDate}
          and station_id = #{stationId}
          and type = 1
    </select>

    <!-- 根据date(时间)和templateId(模板id)查询是否已经有一条数据 -->
    <select id="selectTotalFee" resultType="com.ho.business.entity.IncomeTotal">
        select sum(ele_total_fee) ele_total_fee, sum(income_fee) income_fee, sum(total_fee) total_fee
        from income_total
        where date_format(data_date, '%Y-%m') = #{dataDate}
          and station_id = #{stationId}
          and type = 1
        group by template_id, date
    </select>

    <select id="selectIncomeDetails" resultType="com.ho.business.entity.IncomeRate">
        select rate.start_time,
               rate.end_time,
               rate.rate_name,
               q.q,
               rate.price,
               concat(rate.discount * 100, '%') discount,
               tem.id                           template_id,
               rate.rate_id,
               q.data_date
        from income_rate q
                 left join income_rate_template tem on q.template_id = tem.id
                 left join income_rate_mapping rate on rate.rate_id = q.rate_id and q.template_id = rate.template_id
        where tem.station_id = #{stationId}
          and date_format(q.data_date, '%Y-%m') = #{dataDate}
          and q.template_id = #{templateId}
          and q.rate_id = #{rate}
    </select>

    <!-- excel模板导入 -->
    <insert id="excelInput" parameterType="com.ho.business.entity.IncomeRate">
        insert into income_rate
        (
        data_date,
        rate_id,
        template_id,
        q,
        start_time,
        end_time,
        rate_name,
        price,
        discount,
        station_id
        )
        values
        (
        concat(#{dataDate},'-01'),
        <if test="incomeRate.rateId != null and incomeRate.rateId != 0">
            #{incomeRate.rateId},
        </if>
        <if test="incomeRate.rateId == null or incomeRate.rateId == 0">
            (
            select rate_id from income_rate_mapping where rate_name = #{incomeRate.rateName} and template_id
            =#{templateId}
            ),
        </if>
        #{templateId},
        #{incomeRate.q},
        #{incomeRate.startTime},
        #{incomeRate.endTime},
        #{incomeRate.rateName},
        #{incomeRate.price},
        #{incomeRate.discount},
        #{incomeRate.stationId}
        )
    </insert>

    <select id="getTemplateByName" resultType="com.ho.business.entity.RateTemplate">
        select id
        from income_rate_template
        where station_id = #{stationId}
          and template_name = #{templateName}
    </select>

    <select id="getRateByName" resultType="com.ho.business.entity.IncomeRateMapping">
        select rate_id,
               rate_name,
               price,
               discount,
               start_time,
               end_time
        from income_rate_mapping
        where template_id = #{templateId}
          and rate_name = #{rateName}
    </select>

    <select id="ischeck" resultType="java.lang.Integer">
        select count(1)
        from income_rate q
                 left join income_rate_template tem on q.template_id = tem.id
                 left join income_rate_mapping rate on rate.rate_id = q.rate_id and q.template_id = rate.template_id
        where tem.station_id = #{stationId}
          and date_format(q.data_date, '%Y-%m') = #{dataDate}
          and q.template_id = #{templateId}
          and rate.rate_id = #{rateId}
          and q.id &lt;> #{id}
    </select>

    <update id="modifyIncomeRate">
        update income_rate
        set q          = #{incomeRate.q},
            rate_id    = #{incomeRate.rateId},
            template_id=#{incomeRate.templateId},
            rate_name  = #{incomeRate.rateName},
            start_time = #{incomeRate.startTime},
            end_time   = #{incomeRate.endTime},
            price      = #{incomeRate.price},
            discount   = #{incomeRate.discount}
        where id = #{incomeRate.id}
    </update>


    <select id="queryRateTotal" resultType="java.lang.String">
        select sum(q.q * rate.price * rate.discount) data
        from income_rate q
                 left join income_rate_template tem on tem.id = q.template_id
                 left join income_rate_mapping rate on rate.rate_id = q.rate_id and rate.template_id = q.template_id
        where q.template_id = #{templateId}
          and date_format(q.data_date, '%Y-%m') = #{dataDate}
          and q.rate_id &lt;> #{rateId}
    </select>


    <select id="queryPriceBystationId" resultType="com.ho.business.entity.IncomeQRate">
        select tem.id templateId,
               rate.rate_id,
               rate.price,
               rate.discount,
               rate.rate_name,
               rate.start_time,
               rate.end_time,
               tem.station_id,
               rate.discount
        from income_rate_mapping rate
                 left join income_rate_template tem on tem.id = rate.template_id
        where tem.template_type = 1
          and rate.start_time &lt;= #{time}
          and rate.end_time > #{time}
          and open = 1
          and tem.station_id = #{stationId}
            limit 1
    </select>

    <insert id="insertRealData">
        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 (#{templateId}, #{rateName}, #{rateId}, #{price}, #{ele}, #{eleTotalFee}, #{totalFee}, 0, current_date,
                #{srcId}, #{stationId}, #{dataDate}, #{groupId}, 2, #{discount}, #{startTime}, #{endTime})
    </insert>

    <select id="selectRealRates" resultType="com.ho.business.entity.IncomeRate">
        select rate_name,price,sum(ele) q,discount,start_time,end_time from income_total
        where type = 2
        <if test="stationId != null">
            and station_id = #{stationId}
        </if>
        <if test="startTime != null and endTime != null ">
            and  date BETWEEN #{startTime} and #{endTime}
        </if>
        group by template_id,rate_name,price,discount,start_time,end_time
    </select>

    <!-- 判断企业用电是否选择了别的模板 -->
    <select id="checkTemplate" resultType="java.lang.Integer">
        select count(1) from income_rate q
        left join income_rate_template tem on q.template_id = tem.id
        left join income_rate_mapping rate on rate.rate_id = q.rate_id and q.template_id = rate.template_id
        where tem.station_id = #{stationId} and date_format(q.data_date, '%Y-%m') = #{dataDate} and q.template_id &lt;>
        #{templateId}
        <if test="id != 0 and id != null">
            and q.id &lt;> #{id}
        </if>
    </select>

    <select id="checkTemplateOpen" resultType="java.lang.Integer">
        select count(1)
        from income_rate_template
        where id = #{templateId} and open = '1'
    </select>



</mapper>