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


    <sql id="Base_Column_List">
        id
        ,pid,group_id,dept_id,
        `name`,name_en,address,address_en,longitude,
        latitude,capacity,rate_power,
        status,type,create_time,
        contact,contact_details,grid_time,
        update_time,deleted,is_enable,cabin_num,district,ad_code,is_daily_count,topology_type,cupboard_type,plan_version,inverter_flag,icc_id,province,city,
        electricity_type,customer_type,voltage_level,batch_number,nation
    </sql>

    <select id="selectByName" resultType="com.ho.business.entity.Station">
        select
        <include refid="Base_Column_List"/>
        from station
        where name = #{name,jdbcType=VARCHAR}
        and deleted = 1
    </select>

    <select id="getByName" resultType="com.ho.business.entity.Station">
        select
        <include refid="Base_Column_List"/>
        from station
        where name LIKE concat('%',#{name},'%')
    </select>

    <select id="selectByDimName" resultType="com.ho.business.entity.Station">
        select
        <include refid="Base_Column_List"/>
        from station
        where name LIKE concat('%',#{name},'%')
        and group_id = #{groupId}
        and deleted = 1
    </select>


    <select id="selectByPrimaryKey" resultType="com.ho.business.entity.Station">
        select
        <include refid="Base_Column_List"/>
        from station
        where id = #{id,jdbcType=INTEGER}
    </select>

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

    <select id="selectByNameAndId" resultType="com.ho.business.entity.Station">
        select
        <include refid="Base_Column_List"/>
        from station
        where id != #{id,jdbcType=INTEGER}
        and name = #{name,jdbcType=VARCHAR}
        and dept_id = #{deptId}
        and deleted = 1
    </select>

    <select id="selectAll" resultType="com.ho.business.entity.Station">
        select
        <include refid="Base_Column_List"/>
        from station
        where deleted = 1
    </select>

    <select id="selectByInverterFlag" resultType="com.ho.business.entity.Station">
        select
        <include refid="Base_Column_List"/>
        from station
        where inverter_flag = #{inverterFlag}
        and deleted = 1
    </select>



    <select id="selectByCondition" resultType="com.ho.business.entity.Station">
        select
        <include refid="Base_Column_List"/>
        from station
        where deleted = 1
        <if test="record.name != null and record.name != ''">
            and name LIKE concat('%',#{record.name},'%')
        </if>
        <if test="record.startTime != null">
            and grid_time &gt;= #{record.startTime}
        </if>
        <if test="record.endTime != null">
            and grid_time &lt;= #{record.endTime}
        </if>
        <if test="depts != null and depts.size != 0">
            and dept_id in
            <foreach collection="depts" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
    </select>
    <select id="selectByDeptId" resultType="com.ho.business.entity.Station">
        select
        <include refid="Base_Column_List"/>
        FROM station
        where dept_id= #{deptId}
        and deleted = 1
    </select>

    <select id="selectByGroupIdNotExclude" resultType="com.ho.business.entity.Station">
        select <include refid="Base_Column_List"/>
        FROM station
        <where>
            <if test="record.name != null and record.name != ''">
                and `name` LIKE concat('%',#{record.name},'%')
            </if>
            <if test="record.batchNumber != null and record.batchNumber != ''">
                and batch_number LIKE concat('%',#{record.batchNumber},'%')
            </if>
            <if test="record.startTime != null">
                and grid_time &gt;= #{record.startTime}
            </if>
            <if test="record.endTime != null">
                and grid_time &lt;= #{record.endTime}
            </if>
            <if test="record.groupId != null">
                and group_id = #{record.groupId}
            </if>
            <if test="record.ids != null and record.ids.size() != 0">
                and id in
                <foreach collection="record.ids" open="(" close=")" separator="," item="item">
                    #{item}
                </foreach>
            </if>
            and deleted = 1
        </where>
    </select>

    <select id="selectStatistics" resultType="com.ho.business.entity.Station">
        select
        <include refid="Base_Column_List"/>
        FROM station
        where
        deleted = #{deleted}
        and is_daily_count = #{dailyCount}
    </select>


    <delete id="deleteByPrimaryKey">
        delete
        from station
        where id = #{id,jdbcType=INTEGER}
    </delete>
    <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.Station"
            useGeneratedKeys="true">
        insert into station
        ( id, pid, group_id, dept_id
        , name, address, longitude
        , latitude, capacity, rate_power
        , contact, contact_details, grid_time
        , status, type, create_time
        , update_time, deleted, is_enable,batch_number)
        values ( #{record.id,jdbcType=INTEGER}, #{record.pId,jdbcType=INTEGER}, #{record.groupId,jdbcType=INTEGER}
               , #{record.deptId,jdbcType=INTEGER}
               , #{record.name,jdbcType=VARCHAR}, #{record.address,jdbcType=VARCHAR}
               , #{record.longitude,jdbcType=DECIMAL}
               , #{record.latitude,jdbcType=DECIMAL}, #{record.capacity,jdbcType=DECIMAL}
               , #{record.ratePower,jdbcType=DECIMAL}
               , #{record.contact,jdbcType=VARCHAR}, #{record.contactDetails,jdbcType=VARCHAR}
               , #{record.gridTime,jdbcType=TIMESTAMP}
               , #{record.status,jdbcType=VARCHAR}, #{record.type,jdbcType=VARCHAR}
               , #{record.createTime,jdbcType=TIMESTAMP}
               , #{record.updateTime,jdbcType=TIMESTAMP}, #{record.deleted,jdbcType=TINYINT}
               , #{record.isEnable}
               , #{record.cabinNum}
               ,#{record.batchNumber})
    </insert>
    <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.ho.business.entity.Station"
            useGeneratedKeys="true">
        insert into station
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="record.id != null">id,</if>
            <if test="record.pId != null">pid,</if>
            <if test="record.groupId != null">group_id,</if>
            <if test="record.deptId != null">dept_id,</if>
            <if test="record.name != null">name,</if>
            <if test="record.address != null">address,</if>
            <if test="record.longitude != null">longitude,</if>
            <if test="record.latitude != null">latitude,</if>
            <if test="record.capacity != null">capacity,</if>
            <if test="record.ratePower != null">rate_power,</if>
            <if test="record.contact != null">contact,</if>
            <if test="record.contactDetails != null">contact_details,</if>
            <if test="record.gridTime != null">grid_time,</if>
            <if test="record.status != null">status,</if>
            <if test="record.type != null">type,</if>
            <if test="record.createTime != null">create_time,</if>
            <if test="record.updateTime != null">update_time,</if>
            <if test="record.deleted != null">deleted,</if>
            <if test="record.isEnable != null">is_enable,</if>
            <if test="record.cabinNum != null">cabin_num,</if>
            <if test="record.district != null">district,</if>
            <if test="record.adCode != null">ad_code,</if>
            <if test="record.isDailyCount != null">is_daily_count,</if>
            <if test="record.topologyType != null">topology_type,</if>
            <if test="record.cupboardType != null">cupboard_type,</if>
            <if test="record.planVersion != null">plan_version,</if>
            <if test="record.inverterFlag != null">inverter_flag,</if>
            <if test="record.iccId != null">icc_id,</if>
            <if test="record.province != null">province,</if>
            <if test="record.city != null">city,</if>
            <if test="record.electricityType != null">electricity_type,</if>
            <if test="record.customerType != null">customer_type,</if>
            <if test="record.voltageLevel != null">voltage_level,</if>
            <if test="record.batchNumber != null">batch_number,</if>
            <if test="record.nation != null">nation,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="record.id != null">#{record.id,jdbcType=INTEGER},</if>
            <if test="record.pId != null">#{record.pId,jdbcType=INTEGER},</if>
            <if test="record.groupId != null">#{record.groupId,jdbcType=INTEGER},</if>
            <if test="record.deptId != null">#{record.deptId,jdbcType=INTEGER},</if>
            <if test="record.name != null">#{record.name,jdbcType=VARCHAR},</if>
            <if test="record.address != null">#{record.address,jdbcType=VARCHAR},</if>
            <if test="record.longitude != null">#{record.longitude,jdbcType=DECIMAL},</if>
            <if test="record.latitude != null">#{record.latitude,jdbcType=DECIMAL},</if>
            <if test="record.capacity != null">#{record.capacity,jdbcType=DECIMAL},</if>
            <if test="record.ratePower != null">#{record.ratePower,jdbcType=DECIMAL},</if>
            <if test="record.contact != null">#{record.contact,jdbcType=VARCHAR},</if>
            <if test="record.contactDetails != null">#{record.contactDetails,jdbcType=VARCHAR},</if>
            <if test="record.gridTime != null">#{record.gridTime,jdbcType=TIMESTAMP},</if>
            <if test="record.status != null">#{record.status,jdbcType=VARCHAR},</if>
            <if test="record.type != null">#{record.type,jdbcType=VARCHAR},</if>
            <if test="record.createTime != null">#{record.createTime,jdbcType=TIMESTAMP},</if>
            <if test="record.updateTime != null">#{record.updateTime,jdbcType=TIMESTAMP},</if>
            <if test="record.deleted != null">#{record.deleted,jdbcType=TINYINT},</if>
            <if test="record.isEnable != null">#{record.isEnable},</if>
            <if test="record.cabinNum != null">#{record.cabinNum},</if>
            <if test="record.district != null">#{record.district},</if>
            <if test="record.adCode != null">#{record.adCode},</if>
            <if test="record.isDailyCount != null">#{record.isDailyCount},</if>
            <if test="record.topologyType != null">#{record.topologyType},</if>
            <if test="record.cupboardType != null">#{record.cupboardType},</if>
            <if test="record.planVersion != null">#{record.planVersion},</if>
            <if test="record.inverterFlag != null">#{record.inverterFlag},</if>
            <if test="record.iccId != null">#{record.iccId},</if>
            <if test="record.province != null">#{record.province},</if>
            <if test="record.city != null">#{record.city},</if>
            <if test="record.electricityType != null">#{record.electricityType},</if>
            <if test="record.customerType != null">#{record.customerType},</if>
            <if test="record.voltageLevel != null">#{record.voltageLevel},</if>
            <if test="record.batchNumber != null">#{record.batchNumber},</if>
            <if test="record.nation != null">#{record.nation},</if>
        </trim>
    </insert>

    <update id="updateByPrimaryKeySelective" parameterType="com.ho.business.entity.Station">
        update station
        <set>
            <if test="record.groupId != null">
                group_id = #{record.groupId,jdbcType=INTEGER},
            </if>
            <if test="record.deptId != null">
                dept_id = #{record.deptId,jdbcType=INTEGER},
            </if>
            <if test="record.name != null">
                name = #{record.name,jdbcType=VARCHAR},
            </if>
            <if test="record.address != null">
                address = #{record.address,jdbcType=VARCHAR},
            </if>
            <if test="record.longitude != null">
                longitude = #{record.longitude,jdbcType=DECIMAL},
            </if>
            <if test="record.latitude != null">
                latitude = #{record.latitude,jdbcType=DECIMAL},
            </if>
            <if test="record.capacity != null">
                capacity = #{record.capacity,jdbcType=DECIMAL},
            </if>
            <if test="record.ratePower != null">
                rate_power = #{record.ratePower,jdbcType=DECIMAL},
            </if>
            <if test="record.contact != null">
                contact = #{record.contact,jdbcType=VARCHAR},
            </if>
            <if test="record.contactDetails != null">
                contact_details = #{record.contactDetails,jdbcType=VARCHAR},
            </if>
            <if test="record.gridTime != null">
                grid_time = #{record.gridTime,jdbcType=TIMESTAMP},
            </if>
            <if test="record.status != null">
                status = #{record.status,jdbcType=VARCHAR},
            </if>
            <if test="record.type != null">
                type = #{record.type,jdbcType=VARCHAR},
            </if>
            <if test="record.createTime != null">
                create_time = #{record.createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="record.updateTime != null">
                update_time = #{record.updateTime,jdbcType=TIMESTAMP},
            </if>
            <if test="record.deleted != null">
                deleted = #{record.deleted,jdbcType=TINYINT},
            </if>
            <if test="record.isEnable != null">
                is_enable = #{record.isEnable,jdbcType=TINYINT},
            </if>
            <if test="record.cabinNum != null">
                cabin_num = #{record.cabinNum},
            </if>
            <if test="record.district != null">
                district = #{record.district},
            </if>
            <if test="record.adCode != null">
                ad_code = #{record.adCode},
            </if>
            <if test="record.isDailyCount != null">
                is_daily_count = #{record.isDailyCount},
            </if>
            <if test="record.topologyType != null">
                topology_type = #{record.topologyType},
            </if>
            <if test="record.cupboardType != null">
                cupboard_type = #{record.cupboardType},
            </if>
            <if test="record.planVersion != null">
                plan_version = #{record.planVersion},
            </if>
            <if test="record.inverterFlag != null">
                inverter_flag = #{record.inverterFlag},
            </if>
            <if test="record.iccId != null">
                icc_id = #{record.iccId},
            </if>
            <if test="record.iccId == null">
                icc_id = null,
            </if>
            <if test="record.province != null">
                province = #{record.province},
            </if>
            <if test="record.city != null">
                city = #{record.city},
            </if>
            <if test="record.electricityType != null">
                electricity_type = #{record.electricityType},
            </if>
            <if test="record.customerType != null">
                customer_type = #{record.customerType},
            </if>
            <if test="record.voltageLevel != null">
                voltage_level = #{record.voltageLevel},
            </if>
            <if test="record.batchNumber != null">
                batch_number = #{record.batchNumber},
            </if>
            <if test="record.nation != null">
                nation = #{record.nation},
            </if>
        </set>
        where id = #{record.id,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.ho.business.entity.Station">
        update station
        set group_id        = #{groupId,jdbcType=INTEGER},
            dept_id         = #{deptId,jdbcType=INTEGER},
            name            = #{name,jdbcType=VARCHAR},
            address         = #{address,jdbcType=VARCHAR},
            longitude       = #{longitude,jdbcType=DECIMAL},
            latitude        = #{latitude,jdbcType=DECIMAL},
            capacity        = #{capacity,jdbcType=DECIMAL},
            rate_power      = #{ratePower,jdbcType=DECIMAL},
            contact         = #{contact,jdbcType=VARCHAR},
            contact_details = #{contactDetails,jdbcType=VARCHAR},
            grid_time       = #{gridTime,jdbcType=VARCHAR},
            status          = #{status,jdbcType=VARCHAR},
            type            = #{type,jdbcType=VARCHAR},
            create_time     = #{createTime,jdbcType=TIMESTAMP},
            update_time     = #{updateTime,jdbcType=TIMESTAMP},
            deleted         = #{deleted,jdbcType=TINYINT},
            is_enable       = #{isEnable,jdbcType=TINYINT},
            cabin_num       = #{cabinNum},
            batch_number       = #{batchNumber},
            where id = #{id,jdbcType=INTEGER}
    </update>

    <select id="selectByParam" resultType="com.ho.business.vo.resp.StationInfoResp">
        select s.id,s.pid,s.group_id,s.dept_id,s.name,s.address,s.capacity,s.rate_power,s.status,s.type,s.grid_time,
               s.cupboard_type,s.plan_version,d.device_name,d.producer,d.producer_type from
                (
                    <if test="record.list != null and record.list.size != 0">
                        <foreach collection="record.list" open="(" close=")" separator=" union all " item="item">
                            select * from device
                            <where>
                                <if test="item.deviceType != null">
                                    and device_type like concat('%',#{item.deviceType},'%')
                                </if>
                                <if test="item.producer != null">
                                    and producer like concat('%',#{item.producer},'%')
                                </if>
                                <if test="item.producerType != null">
                                    and producer_type like concat('%',#{item.producerType},'%')
                                </if>
                                and `virtual` = 0 and category > 2 and device_type is not null
                            </where>
                        </foreach>
                    </if>
                    <if test="record.list == null">
                        select * from device where `virtual` = 0 and category > 2 and device_type is not null
                    </if>
                ) d,
                ( select * from station
                    <where>
                        <if test="record.name != null">
                            and name like concat('%',#{record.name},'%')
                        </if>
                        <if test="record.cupboardType != null">
                            and cupboard_type = #{record.cupboardType}
                        </if>
                        <if test="record.planVersion != null">
                            and plan_version like concat('%',#{record.planVersion},'%')
                        </if>
                    </where>
                ) s
        where d.station_id = s.id order by s.id asc
    </select>

    <select id="getTermDictionary" parameterType="java.lang.String" resultType="java.util.Map">
        select * from term_dictionary
        <where>
            <if test="language!= null and language !=''">
                and language  = #{language}
            </if>
        </where>
    </select>

    <update id="updateStationList" keyColumn="id" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
        <foreach collection="list" item="item" index="index" separator=";">
            update station set name = #{item.name},name_en = #{item.nameEn},address = #{item.address},address_en = #{item.addressEn} where id = #{item.id}
        </foreach>
    </update>

</mapper>
