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

    <resultMap id="baseResult" type="com.ho.business.entity.PlanningCurveTemplate">
        <id property="temId" column="tem_id" />
        <result property="templateNo" column="template_no"/>
        <result property="templateName" column="template_name"/>
        <result property="stationId" column="station_id"/>
        <result property="income" column="income"/>
        <result property="groupId" column="group_id"/>
        <!-- <result property="elecTemplateId" column="elec_template_id"/> -->
        <result property="status" column="status"/>
        <result property="updateTime" column="update_time"/>
        <result property="temP" column="tem_p"/>
        <result property="capacity" column="capacity"/>
        <result property="socUpper" column="soc_upper"/>
        <result property="socLower" column="soc_lower"/>
        <collection property="planningCurves" ofType="com.ho.business.entity.PlanningCurve">
            <id property="id" column="id"/>
            <result property="planningTemplateId" column="planning_template_id"/>
            <result property="startTime" column="start_time"/>
            <result property="endTime" column="end_time"/>
            <result property="p" column="p"/>
            <result property="q" column="q"/>
            <result property="soc" column="soc"/>
        </collection>
    </resultMap>

    <!-- 查询模板列表接口 -->
    <select id="getPlanningCurveTemplates" resultType="java.util.HashMap">
        select tem.id tem_id, tem.template_no template_no, tem.template_name template_name, tem.station_id, tem.income, tem.group_id,
        tem.elec_template_id elec_template_id, tem.status status, date_format(tem.update_time,'%Y-%m-%d %H:%i:%s') update_time, tem.p tem_p, tem.capacity, tem.soc_upper,
        tem.soc_lower, cur.id id, cur.start_time start_time, cur.end_time end_time, cur.p p, cur.q q, cur.soc soc,cur.planning_template_id
        from (
        select id,template_no,
        <choose>
            <when test="lang != null and lang=='en_US' ">
                template_name_en template_name,
            </when>
            <otherwise>
                template_name,
            </otherwise>
        </choose>
        elec_template_id,station_id,group_id,income,status,p,capacity,soc_upper,soc_lower,create_time,update_time
        from planning_curve_template
        <where>
            <if test="temId != null and temId != ''">
                id = #{temId}
            </if>
            <if test="templateName != null and templateName != ''">
                and (template_name like concat('%',#{templateName},'%') or template_name_en like concat('%',#{templateName},'%'))
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="stationId != null and stationId != ''">
                and station_id = #{stationId} or station_id is null
            </if>
            <if test="groupId != null and groupId != ''">
                and group_id = #{groupId} or group_id is null
            </if>
        </where>
        <if test="pageNum != null and pageSize != null" >
            limit ${pageNum},${pageSize}
        </if>
        )tem left join planning_curve cur on cur.planning_template_id = tem.id
        order by tem.create_time,tem.update_time desc, cur.start_time
    </select>

    <select id="getSeq" resultType="java.lang.Integer">
        select ifnull(max(id),0)+1 from planning_curve_template
    </select>

    <!-- 新增模板 -->
    <insert id="addPlanningCurveTemplate" parameterType="com.ho.business.entity.PlanningCurveTemplate">
        insert into planning_curve_template ( id,template_no,template_name,template_name_en,<!-- elec_template_id, -->
        <if test="stationId != null and stationId != ''">
            station_id,
        </if>
        <if test="groupId != null and groupId != ''">
            group_id,
        </if>
        income,status,p,capacity,soc_upper,soc_lower,create_time )
        values( #{temId},#{templateNo},#{templateName},#{templateNameEn},<!-- #{elecTemplateId}, -->
        <if test="stationId != null and stationId != ''">
            #{stationId},
        </if>
        <if test="groupId != null and groupId != ''">
            #{groupId},
        </if>
        #{income},#{status},#{temP},#{capacity},#{socUpper},#{socLower},sysdate() )
    </insert>

    <!-- 新增曲线 -->
    <insert id="addPlanningCurve" parameterType="com.ho.business.entity.PlanningCurve">
        insert into planning_curve ( planning_template_id,start_time,end_time,p) values
        <foreach collection="curves" item="item" index="index" separator=",">
            ( #{planningTemplateId},#{item.startTime},#{item.endTime},#{item.p})
        </foreach>
    </insert>

    <!-- 编辑计划曲线模板 -->
    <update id="modifyPlanningCurveTemplate" parameterType="com.ho.business.entity.PlanningCurveTemplate">
        update planning_curve_template set
            <if test="templateNo != null and templateNo != ''">
                template_no = #{templateNo},
            </if>
            <if test="templateName != null and templateName != ''">
                template_name = #{templateName},
            </if>
            <if test="templateNameEn != null and templateNameEn != ''">
                template_name_en = #{templateNameEn},
            </if>
            <!--
             <if test="elecTemplateId != null and elecTemplateId != ''">
                elec_template_id = #{elecTemplateId},
            </if>
             -->
            <if test="stationId != null and stationId != ''">
                station_id = #{stationId},
            </if>
            <if test="groupId != null and groupId != ''">
                group_id = #{groupId},
            </if>
            <if test="income != null and income != ''">
                income = #{income},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="temP != null and temP != ''">
                p = #{temP},
            </if>
            <if test="capacity != null and capacity != ''">
                capacity = #{capacity},
            </if>
            <if test="socUpper != null and socUpper != ''">
                soc_upper = #{socUpper},
            </if>
            <if test="socLower != null and socLower != ''">
                soc_lower = #{socLower},
            </if>
            update_time = sysdate()
            where id = #{temId}
    </update>

    <!-- 删除计划曲线数据 -->
    <delete id="deleteCurves">
        delete from planning_curve where planning_template_id = #{id}
    </delete>

    <delete id="deletePlanningCurveTemplates">
        delete from planning_curve_template
        where id in
            <foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
                    #{item}
            </foreach>
    </delete>

    <delete id="batchDeleteCurves">
        delete from planning_curve
        where planning_template_id in
            <foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
    </delete>

    <select id="checkNo" resultType="java.lang.Integer">
        select count(1) from planning_curve_template where template_no = #{templateNo} and id &lt;> #{temId} and station_id = #{stationId}
    </select>

    <select id="checkName" resultType="java.lang.Integer">
        select count(1) from planning_curve_template where template_name = #{templateName} and id &lt;> #{temId} and station_id = #{stationId}
    </select>

    <!-- 查询模板列表接口 -->
    <select id="getPlanningCurveTemplate" resultMap="baseResult">
        select tem.id tem_id, tem.template_no template_no, tem.template_name template_name, tem.station_id, tem.income, tem.group_id,
        tem.elec_template_id elec_template_id, tem.status status, tem.update_time update_time, tem.p tem_p, tem.capacity, tem.soc_upper,
        tem.soc_lower, cur.id id, cur.start_time start_time, cur.end_time end_time, ifnull(cur.p,0) p, ifnull(cur.q,0) q, cur.soc soc,cur.planning_template_id
        from (
        select id,template_no,
        <choose>
            <when test="lang != null and lang=='en_US' ">
                template_name_en template_name,
            </when>
            <otherwise>
                template_name,
            </otherwise>
        </choose>
        elec_template_id,station_id,group_id,income,status,p,capacity,soc_upper,soc_lower,create_time,update_time
        from planning_curve_template
        <where>
            <if test="temId != null and temId != ''">
                id = #{temId}
            </if>
            <if test="templateName != null and templateName != ''">
                and template_name like concat('%',#{templateName},'%')
            </if>
            <if test="status != null and status != ''">
                and status = #{status}
            </if>
            <if test="stationId != null and stationId != ''">
                and station_id = #{stationId} or station_id is null
            </if>
            <if test="groupId != null and groupId != ''">
                and group_id = #{groupId} or group_id is null
            </if>
        </where>
        ) tem left join planning_curve cur on cur.planning_template_id = tem.id
        order by tem.create_time,tem.update_time desc, cur.start_time
    </select>

</mapper>
