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

    <resultMap id="BaseResultMap" type="com.ho.business.entity.ElecMeterValue">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="groupId" column="group_id" jdbcType="INTEGER"/>
        <result property="pid" column="pid" jdbcType="INTEGER"/>
        <result property="stationId" column="station_id" jdbcType="INTEGER"/>
        <result property="srcId" column="src_id" jdbcType="INTEGER"/>
        <result property="deptId" column="dept_id" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="day" column="day" jdbcType="VARCHAR"/>
        <result property="type" column="type" jdbcType="VARCHAR"/>
        <result property="digital" column="digital" jdbcType="VARCHAR"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
    </resultMap>

    <sql id="Base_Column_List">
        id
        ,group_id,pid,
        station_id,src_id,dept_id,
        create_time,day,type,
        digital,status
    </sql>

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

    <select id="selectSumByType" resultType="java.math.BigDecimal">
        SELECT sum(digital) as sum
        from elec_meter_value
        WHERE station_id = #{stationId}
        and type = #{type}
        <if test="srcId!=null">
            and src_id = #{srcId}
        </if>
        <if test="beginTime != null">
            and day &gt;= #{beginTime}
        </if>
        <if test="endTime != null">
            and day &lt;= #{endTime}
        </if>
        and status !=0
    </select>

    <select id="selectSumValue" resultType="java.math.BigDecimal">
        SELECT sum(digital) as sum
        from elec_meter_value
        WHERE station_id = #{stationId}
        and type = #{type}
        <if test="srcId!=null">
            and src_id = #{srcId}
        </if>
        and status !=0
    </select>

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

    <select id="selectList" resultType="com.ho.business.entity.ElecMeterValue">
        SELECT
        <include refid="Base_Column_List"/>
        from elec_meter_value
        <where>
            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>
            and day BETWEEN #{beginTime}
            AND #{endTime}
        </where>
    </select>

    <select id="selectAllList" resultType="com.ho.business.entity.ElecMeterValue">
        SELECT
        <include refid="Base_Column_List"/>
        from elec_meter_value
        <where>
            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>
            <if test="deviceTypeList!=null and deviceTypeList.size() !=0">
                and src_id in
                <foreach collection="deviceTypeList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            and day BETWEEN #{beginTime}
            AND #{endTime}
        </where>
    </select>

    <select id="selectListByStationIds" resultType="com.ho.business.entity.ElecMeterValue">
        SELECT
        <include refid="Base_Column_List"/>
        from elec_meter_value
        <where>
            <if test="stationIds!=null and stationIds.size() !=0">
                and station_id in
                <foreach collection="stationIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            and type = #{type}
            <if test="beginTime != null">
                and day &gt;= #{beginTime}
            </if>
            <if test="endTime != null">
                and day &lt;= #{endTime}
            </if>
            and status !=0
        </where>
    </select>

    <select id="selectByStationIdsAndTime" resultType="com.ho.business.entity.ElecMeterValue">
        SELECT sum(digital) as digital,type,station_id
        from elec_meter_value
        <where>
            <if test="stationIds!=null and stationIds.size() !=0">
                and station_id in
                <foreach collection="stationIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="beginTime != null">
                and day &gt;= #{beginTime}
            </if>
            <if test="endTime != null">
                and day &lt;= #{endTime}
            </if>
            <if test="srcId!=null">
                and src_id = #{srcId}
            </if>
            and status !=0
            group by type,station_id
        </where>
    </select>

    <select id="selectGroupByDay" resultType="com.ho.business.entity.ElecMeterValue">
        SELECT sum(digital) as digital,type,station_id,day
        from elec_meter_value
        <where>
            <if test="stationIds!=null and stationIds.size() !=0">
                and station_id in
                <foreach collection="stationIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="beginTime != null">
                and day &gt;= #{beginTime}
            </if>
            <if test="endTime != null">
                and day &lt;= #{endTime}
            </if>
            <if test="srcId != null">
                and src_id = #{srcId}
            </if>
            <if test="srcIds!=null and srcIds.size() !=0 ">
                and src_id in
                <foreach collection="srcIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            and status !=0
            group by type,station_id,day
        </where>
    </select>
    <select id="getElec" resultType="com.ho.business.vo.resp.profit.DayProfitType">
        select
        station_id,day,type,sum(digital) as digital
        from elec_meter_value
        <where>
            digital > 0
            <if test="type != null">
                and type = #{type}
            </if>
            <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="deleteByPrimaryKey" parameterType="java.lang.Long">
        delete
        from elec_meter_value
        where id = #{id,jdbcType=INTEGER}
    </delete>

    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.ElecMeterValue"
            useGeneratedKeys="true">
        insert into elec_meter_value
        ( id, group_id, pid
        , station_id, src_id, dept_id
        , create_time, day, type
        , digital, status)
        values ( #{id,jdbcType=INTEGER}, #{groupId,jdbcType=INTEGER}, #{pid,jdbcType=INTEGER}
               , #{stationId,jdbcType=INTEGER}, #{srcId,jdbcType=INTEGER}, #{deptId,jdbcType=INTEGER}
               , #{createTime,jdbcType=TIMESTAMP}, #{day,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}
               , #{digital,jdbcType=VARCHAR}, #{status})
    </insert>
    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.ElecMeterValue"
            useGeneratedKeys="true">
        insert into elec_meter_value
        <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="status != null">status,</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="status != null">#{status},</if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.ho.business.entity.ElecMeterValue">
        update elec_meter_value
        <set>
            <if test="groupId != null">
                group_id = #{groupId,jdbcType=INTEGER},
            </if>
            <if test="pid != null">
                pid = #{pid,jdbcType=INTEGER},
            </if>
            <if test="stationId != null">
                station_id = #{stationId,jdbcType=INTEGER},
            </if>
            <if test="srcId != null">
                src_id = #{srcId,jdbcType=INTEGER},
            </if>
            <if test="deptId != null">
                dept_id = #{deptId,jdbcType=INTEGER},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="day != null">
                day = #{day,jdbcType=VARCHAR},
            </if>
            <if test="type != null">
                type = #{type,jdbcType=VARCHAR},
            </if>
            <if test="digital != null">
                digital = #{digital,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                status = #{status},
            </if>

        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.ho.business.entity.ElecMeterValue">
        update elec_meter_value
        set group_id    = #{groupId,jdbcType=INTEGER},
            pid         = #{pid,jdbcType=INTEGER},
            station_id  = #{stationId,jdbcType=INTEGER},
            src_id      = #{srcId,jdbcType=INTEGER},
            dept_id     = #{deptId,jdbcType=INTEGER},
            create_time = #{createTime,jdbcType=TIMESTAMP},
            day         = #{day,jdbcType=VARCHAR},
            type        = #{type,jdbcType=VARCHAR},
            digital     = #{digital,jdbcType=VARCHAR}
        where id = #{id,jdbcType=INTEGER}
    </update>


    <delete id="deleteByDay">
        delete
        from elec_meter_value
        <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 elec_meter_value
        <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 elec_meter_value
        (group_id, pid, station_id, src_id, dept_id, create_time, day, type, digital,status)
        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.status}
            )
        </foreach>


    </insert>
</mapper>
