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

    <resultMap id="BaseResultMap" type="com.ho.business.entity.ElecTemplateSub">
            <id property="id" column="id" jdbcType="INTEGER"/>
            <result property="pid" column="pid" jdbcType="INTEGER"/>
            <result property="beginTime" column="begin_time" jdbcType="VARCHAR"/>
            <result property="endTime" column="end_time" jdbcType="VARCHAR"/>
            <result property="type" column="type" jdbcType="VARCHAR"/>
            <result property="price" column="price" jdbcType="DECIMAL"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,pid,begin_time,
        end_time,type,price
    </sql>

    <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
        from elec_template_sub
        where  id = #{id,jdbcType=INTEGER}
    </select>

    <select id="selectByPid" resultType="com.ho.business.entity.ElecTemplateSub">
        select
        <include refid="Base_Column_List" />
        from elec_template_sub
        where  pid = #{pid}
    </select>

    <select id="selectByPids" resultType="com.ho.business.entity.ElecTemplateSub">
        select
        <include refid="Base_Column_List" />
        from elec_template_sub
        <where>
        <if test="pIds != null and pIds.size() !=0">
            and pid in
            <foreach collection="pIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        </where>
    </select>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
        delete from elec_template_sub
        where  id = #{id,jdbcType=INTEGER} 
    </delete>
    <delete id="deletedByPid">
        delete from elec_template_sub
        where  pid = #{pid}
    </delete>
    <delete id="deletedByPids">
        delete
        from elec_template_sub
        <where>
            <if test="pIds != null and pIds.size() !=0">
                and pid in
                <foreach collection="pIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </delete>
    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.ElecTemplateSub" useGeneratedKeys="true">
        insert into elec_template_sub
        ( id,pid,begin_time
        ,end_time,type,price
        )
        values (#{id,jdbcType=INTEGER},#{pid,jdbcType=INTEGER},#{beginTime,jdbcType=VARCHAR}
        ,#{endTime,jdbcType=VARCHAR},#{type,jdbcType=VARCHAR},#{price,jdbcType=DECIMAL}
        )
    </insert>
    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.ElecTemplateSub" useGeneratedKeys="true">
        insert into elec_template_sub
        <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="id != null">id,</if>
                <if test="pid != null">pid,</if>
                <if test="beginTime != null">begin_time,</if>
                <if test="endTime != null">end_time,</if>
                <if test="type != null">type,</if>
                <if test="price != null">price,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="id != null">#{id,jdbcType=INTEGER},</if>
                <if test="pid != null">#{pid,jdbcType=INTEGER},</if>
                <if test="beginTime != null">#{beginTime,jdbcType=VARCHAR},</if>
                <if test="endTime != null">#{endTime,jdbcType=VARCHAR},</if>
                <if test="type != null">#{type,jdbcType=VARCHAR},</if>
                <if test="price != null">#{price,jdbcType=DECIMAL},</if>
        </trim>
    </insert>
    <insert id="insertList">
        insert into elec_template_sub
        (pid,begin_time,end_time,type,price)
        values
        <foreach separator="," collection="list" item="item">
            (#{item.pid},#{item.beginTime},#{item.endTime},#{item.type},#{item.price})
        </foreach>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.ho.business.entity.ElecTemplateSub">
        update elec_template_sub
        <set>
                <if test="pid != null">
                    pid = #{pid,jdbcType=INTEGER},
                </if>
                <if test="beginTime != null">
                    begin_time = #{beginTime,jdbcType=VARCHAR},
                </if>
                <if test="endTime != null">
                    end_time = #{endTime,jdbcType=VARCHAR},
                </if>
                <if test="type != null">
                    type = #{type,jdbcType=VARCHAR},
                </if>
                <if test="price != null">
                    price = #{price,jdbcType=DECIMAL},
                </if>
        </set>
        where   id = #{id,jdbcType=INTEGER} 
    </update>
    <update id="updateByPrimaryKey" parameterType="com.ho.business.entity.ElecTemplateSub">
        update elec_template_sub
        set 
            pid =  #{pid,jdbcType=INTEGER},
            begin_time =  #{beginTime,jdbcType=VARCHAR},
            end_time =  #{endTime,jdbcType=VARCHAR},
            type =  #{type,jdbcType=VARCHAR},
            price =  #{price,jdbcType=DECIMAL}
        where   id = #{id,jdbcType=INTEGER} 
    </update>


    <select id="selectByStationId" resultType="com.ho.business.entity.ElecTemplateSub">
        select sub.id,sub.begin_time,sub.end_time,sub.price,sub.type
        from elec_template tem
                 left join elec_template_sub sub on sub.pid = tem.id
        where tem.station_id = #{stationId} and tem.is_enable = 1
        order by sub.begin_time
    </select>

    <insert id="insertRateMeterValue" parameterType="com.ho.business.entity.ElecRateMeterValue">
        insert into elec_rate_meter_value ( station_id,create_time,day,type,digital,rate_type ) values
        <foreach collection="values" item="item" index="index" separator=",">
            ( #{item.stationId},sysdate(),#{item.day},#{item.type},#{item.digital},#{item.rateType} )
        </foreach>
    </insert>

    <select id="getPcsIncomeData" resultType="HashMap">
        <if test="req.type == 'day' or req.type == 'month'">
            SELECT a.selected_date day,ifnull(b.data,'') data FROM
            (
            SELECT adddate('1970-01-01',t4.i * 10000 + t3.i * 1000 + t2.i * 100 + t1.i * 10 + t0.i) selected_date
            FROM
            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
            UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t0,
            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
            UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t1,
            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
            UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t2,
            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
            UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t3,
            ( SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4
            UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 ) t4
            ) a
            left join (
            select a.day,(b.data-a.data) data from (
            (select day,sum(digital) data from elec_rate_meter_value value where station_id = #{req.stationId} and type = 1 and day between DATE_FORMAT(#{beginTime},'%Y-%m-%d') and DATE_FORMAT(#{endTime},'%Y-%m-%d') group by day) a,
            (select day,sum(digital) data from elec_rate_meter_value value where station_id = #{req.stationId} and type = 2 and day between DATE_FORMAT(#{beginTime},'%Y-%m-%d') and DATE_FORMAT(#{endTime},'%Y-%m-%d') group by day) b
            ) where a.day=b.day
            ) b on b.day = a.selected_date
            WHERE a.selected_date BETWEEN #{beginTime} AND #{endTime}
            order by a.selected_date
        </if>
        <if test="req.type == 'year'">
            select a.yearAndMonth day,ifnull(sum(b.data),'') data from (
            select date_format(`date`, '%Y-%m') yearAndMonth from (select adddate('1970-01-01', interval t2.i * 100 + t1.i * 10 + t0.i month) `date`
            from (select 0 i union select 1 union
            select 2 union select 3 union
            select 4 union select 5 union
            select 6 union select 7 union
            select 8 union select 9) t0,
            (select 0 i union select 1 union
            select 2 union select 3 union
            select 4 union select 5 union
            select 6 union select 7 union
            select 8 union select 9) t1,
            (select 0 i union select 1 union
            select 2 union select 3 union
            select 4 union select 5 union
            select 6 union select 7 union
            select 8 union select 9) t2) a
            where date between date_format(#{beginTime},'%Y-%m-%d') and #{endTime}
            ) a
            left join (
            select date_format(a.day,'%Y-%m') date,(b.data-a.data) data from (
            (select day,sum(digital) data from elec_rate_meter_value value where station_id = #{req.stationId} and type = 1 and day between DATE_FORMAT(#{beginTime},'%Y-%m-%d') and DATE_FORMAT(#{endTime},'%Y-%m-%d') group by day) a,
            (select day,sum(digital) data from elec_rate_meter_value value where station_id = #{req.stationId} and type = 2 and day between DATE_FORMAT(#{beginTime},'%Y-%m-%d') and DATE_FORMAT(#{endTime},'%Y-%m-%d') group by day) b
            ) where a.day=b.day
            ) b on b.date = a.yearAndMonth
            group by a.yearAndMonth
            order by a.yearAndMonth
        </if>
    </select>

    <delete id="deleteRateMeterValue">
        delete from elec_rate_meter_value where day between DATE_FORMAT(#{beginTime},'%Y-%m-%d') and DATE_FORMAT(#{endTime},'%Y-%m-%d')
        and type = #{type}
    </delete>

    <select id="queryRealTimePcsIncomeData" resultType="com.ho.business.entity.ElecRateMeterValue">
        select id,type,digital,rate_type from elec_rate_meter_value
        where station_id = #{stationId} and day = DATE_FORMAT(#{dateTime},'%Y-%m-%d')
        order by id
    </select>

</mapper>
