<?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.xzzn.ems.mapper.EmsSiteSettingMapper">

    <resultMap type="EmsSiteSetting" id="EmsSiteSettingResult">
        <result property="id"    column="id"    />
        <result property="siteName"    column="site_name"    />
        <result property="siteShortName"    column="site_short_name"    />
        <result property="siteAddress"    column="site_address"    />
        <result property="runningTime"    column="running_time"    />
        <result property="latitude"    column="latitude"    />
        <result property="longitude"    column="longitude"    />
        <result property="installCapacity"    column="install_capacity"    />
        <result property="installPower"    column="install_power"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="siteId"    column="site_id"    />
    </resultMap>

    <sql id="selectEmsSiteSettingVo">
        select id, site_name, site_short_name, site_address, running_time, latitude, longitude, install_capacity, install_power, remark, create_by, update_by, create_time, update_time, site_id from ems_site_setting
    </sql>

    <select id="selectEmsSiteSettingList" parameterType="EmsSiteSetting" resultMap="EmsSiteSettingResult">
        <include refid="selectEmsSiteSettingVo"/>
        <where>
            <if test="siteName != null  and siteName != ''"> and site_name like concat('%', #{siteName}, '%')</if>
            <if test="siteShortName != null  and siteShortName != ''"> and site_short_name like concat('%', #{siteShortName}, '%')</if>
            <if test="siteAddress != null  and siteAddress != ''"> and site_address = #{siteAddress}</if>
            <if test="runningTime != null "> and running_time = #{runningTime}</if>
            <if test="latitude != null "> and latitude = #{latitude}</if>
            <if test="longitude != null "> and longitude = #{longitude}</if>
            <if test="installCapacity != null "> and install_capacity = #{installCapacity}</if>
            <if test="installPower != null "> and install_power = #{installPower}</if>
            <if test="siteId != null  and siteId != ''"> and site_id = #{siteId}</if>
        </where>
    </select>

    <select id="selectEmsSiteSettingById" parameterType="Long" resultMap="EmsSiteSettingResult">
        <include refid="selectEmsSiteSettingVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsSiteSetting" parameterType="EmsSiteSetting" useGeneratedKeys="true" keyProperty="id">
        insert into ems_site_setting
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="siteName != null and siteName != ''">site_name,</if>
            <if test="siteShortName != null">site_short_name,</if>
            <if test="siteAddress != null">site_address,</if>
            <if test="runningTime != null">running_time,</if>
            <if test="latitude != null">latitude,</if>
            <if test="longitude != null">longitude,</if>
            <if test="installCapacity != null">install_capacity,</if>
            <if test="installPower != null">install_power,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="siteId != null and siteId != ''">site_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="siteName != null and siteName != ''">#{siteName},</if>
            <if test="siteShortName != null">#{siteShortName},</if>
            <if test="siteAddress != null">#{siteAddress},</if>
            <if test="runningTime != null">#{runningTime},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="installCapacity != null">#{installCapacity},</if>
            <if test="installPower != null">#{installPower},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="siteId != null and siteId != ''">#{siteId},</if>
        </trim>
    </insert>

    <update id="updateEmsSiteSetting" parameterType="EmsSiteSetting">
        update ems_site_setting
        <trim prefix="SET" suffixOverrides=",">
            <if test="siteName != null and siteName != ''">site_name = #{siteName},</if>
            <if test="siteShortName != null">site_short_name = #{siteShortName},</if>
            <if test="siteAddress != null">site_address = #{siteAddress},</if>
            <if test="runningTime != null">running_time = #{runningTime},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="installCapacity != null">install_capacity = #{installCapacity},</if>
            <if test="installPower != null">install_power = #{installPower},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="siteId != null and siteId != ''">site_id = #{siteId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsSiteSettingById" parameterType="Long">
        delete from ems_site_setting where id = #{id}
    </delete>

    <delete id="deleteEmsSiteSettingByIds" parameterType="String">
        delete from ems_site_setting where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="getSiteTotalInfo" resultType="com.xzzn.ems.domain.vo.SiteTotalInfoVo">
        select count(DISTINCT s.site_id) as siteNum,
               SUM(s.install_capacity) as installCapacity,
               SUM(s.install_power) as installPower
        from ems_site_setting s
    </select>

    <select id="selectEmsSiteSettingBySiteId" parameterType="String" resultMap="EmsSiteSettingResult">
        <include refid="selectEmsSiteSettingVo"/>
        where site_id = #{siteId}
    </select>

    <select id="getSiteInfoList" resultMap="EmsSiteSettingResult">
        <include refid="selectEmsSiteSettingVo"/>
        WHERE  1=1
        <if test="siteName != null and siteName != ''">
            AND site_name LIKE CONCAT('%', #{siteName}, '%')
        </if>
        <if test="startTime != null and startTime != null and endTime != null and endTime != ''">
            AND running_time &gt;= STR_TO_DATE( #{startTime}, '%Y-%m-%d')
            AND running_time &lt; DATE_ADD(STR_TO_DATE( #{endTime}, '%Y-%m-%d'), INTERVAL 1 DAY)
        </if>
    </select>

    <select id="getAllSiteDeviceList" parameterType="String" resultType="com.xzzn.ems.domain.vo.SiteDeviceListVo">
        select  ed.site_id as siteId,
                ed.device_id as deviceId,ed.device_name as deviceName,
                ed.device_type as deviceType,ed.device_status as deviceStatus,ed.work_status as workStatus,
                ed.device_category as deviceCategory,
                ed.picture_url as pictureUrl,
                ed.id,
                ed.parent_id as parentId
        from ems_devices_setting ed
        where 1=1
        <if test="siteId != null and siteId != ''">
            and ed.site_id = #{siteId}
        </if>
        <if test="deviceCategory != null and deviceCategory != ''">
            and ed.device_category = #{deviceCategory}
        </if>
        order by ed.device_id
    </select>

    <select id="getAllSiteDeviceListNoDisp" parameterType="String" resultType="com.xzzn.ems.domain.vo.SiteDeviceListVo">
        select  ed.site_id as siteId,
        ed.device_id as deviceId,ed.device_name as deviceName,
        ed.device_type as deviceType,ed.device_status as deviceStatus,ed.work_status as workStatus,
        ed.device_category as deviceCategory,
        ed.picture_url as pictureUrl,
        ed.id,
        ed.parent_id as parentId
        from ems_devices_setting ed
        where 1 = 1
        <if test="siteId != null and siteId != ''">
            and ed.site_id = #{siteId}
        </if>
        and (ed.display_flg is null or ed.display_flg != '1')
        <if test="deviceCategory != null and deviceCategory != ''">
            and ed.device_category = #{deviceCategory}
        </if>
        order by ed.device_id
    </select>
    <select id="getAllSiteId"  resultType="String">
        select distinct site_id from ems_site_setting
    </select>
</mapper>
