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

    <select id="getIssueDevices" resultType="HashMap">
        select
        src_id as value,
        <choose>
            <when test="lang != null and lang=='en_US' ">
                device_name_en as label
            </when>
            <otherwise>
                device_name as label
            </otherwise>
        </choose>
        from device where (device_type like 'agc%' or device_type like 'planCurve%' ) and station_id = #{stationId}
    </select>

    <select id="getTemplate" resultType="HashMap">
        select id as value,
        <choose>
            <when test="lang != null and lang=='en_US' ">
                template_name_en as label
            </when>
            <otherwise>
                template_name as label
            </otherwise>
        </choose>
        from planning_curve_template
        where  ( station_id = #{stationId} or station_id is null ) and status = 1
    </select>

    <select id="getIssueComms" resultType="com.ho.business.entity.PlanningIssueComm">
        select model.col model_col,type.col type_col,type.max_value,type.min_value,model.col_name
        from model_device_col device
                 join device_type_col type on type.id = device.device_col_id
                 join model_type_col model on model.id = device.model_col_id
        where model.col like 'plan%' and device.device_type=#{deviceType}
    </select>

    <select id="getDeviceById" resultType="com.ho.business.entity.Device">
        select src_id,device_name,device_type,serial_no from device where src_id = #{srcId} and station_id = #{stationId}
    </select>

    <select id="getCurveDetail" resultType="com.ho.business.entity.PlanningCurve">
        select id,start_time,end_time,p,q,soc from planning_curve where planning_template_id = #{temId}
    </select>

    <insert id="insertIssueStatus">
        insert into planning_curve_device ( station_id,plan_tem_id,device_id,issue,operation,operation_date )
        values( #{stationId},#{planTemId},#{deviceId},0,#{operation},sysdate() )
    </insert>

    <select id="queryIssueStatus" resultType="com.ho.business.entity.PlanningIssueDevice">
        select id,station_id,plan_tem_id,device_id,issue,operation ,operation_date
        from planning_curve_device
        <where>
            <if test="stationId != null">
                station_id = #{stationId}
            </if>
            <if test="deviceId != null">
                and device_id = #{deviceId}
            </if>
            <if test="planTemId != null">
                and plan_tem_id = #{planTemId}
            </if>
        </where>
    </select>

    <delete id="deleteIssueStatus">
        delete from planning_curve_device where station_id = #{stationId}
    </delete>


    <resultMap id="baseResult" type="com.ho.business.entity.PlanningIssueCommDetail">
        <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="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"/>
        <result property="deviceId" column="device_id"/>
        <result property="issue" column="issue"/>
        <result property="operation" column="operation"/>
        <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="queryCurrentIssue" 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, cur.p p, cur.q q, cur.soc soc,cur.planning_template_id
        from (
        select id,template_no,template_name,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="stationId != null and stationId != ''">
                and station_id = #{stationId}
            </if>
            <if test="planTemId != null" >
                and id = #{planTemId}
            </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>

    <select id="chechTemplateExists" resultType="java.lang.Integer">
        select count(1) from planning_curve_template where id = #{temId}
    </select>

    <insert id="planCurveOperationRecord" useGeneratedKeys="true" keyColumn="id" keyProperty="id" parameterType="com.ho.business.vo.resp.planningCurve.PlanningIssueVo">
        insert into planning_curve_operation_record
        (user_id,user_name,operate_time,operate_content,operate_content_en,operate_result,operate_result_en,effective_time,status,previous_planning_curve,latest_planning_curve,station_id)
        values (
                   #{condition.userId},#{condition.userName},#{condition.operateTime},#{condition.operateContent},#{condition.operateContentEn},
                   #{condition.operateResult},#{condition.operateResultEn},
                   #{condition.effectiveTime},#{condition.status},#{condition.previousPlanningCurve},#{condition.latestPlanningCurve},#{condition.stationId}
               )
    </insert>

    <insert id="addPlanningCurveHistory" parameterType="com.ho.business.entity.PlanningCurveHistory">
        insert into planning_curve_history ( temp_no,operate_no,start_time,end_time,p,soc_upper,soc_lower,effective_time) values
        <foreach collection="condition" item="item" index="index" separator=",">
            (#{item.tempNo},#{item.operateNo},#{item.startTime},#{item.endTime},#{item.p},#{item.socUpper},#{item.socLower},#{item.effectiveTime})
        </foreach>
    </insert>

    <select id="getPlanCurveOperationList" resultType="com.ho.business.entity.PlanCurveOperationRecordReq">
        select user_id,user_name,operate_time,
        <choose>
            <when test="lang != null and lang=='en_US' ">
                operate_content_en operate_content,
            </when>
            <otherwise>
                operate_content,
            </otherwise>
        </choose>
        <choose>
            <when test="lang != null and lang=='en_US' ">
                operate_result_en operate_result,
            </when>
            <otherwise>
                operate_result,
            </otherwise>
        </choose>
        effective_time,status,previous_planning_curve,latest_planning_curve,station_id from
        planning_curve_operation_record where station_id = #{stationId} order by operate_time desc
    </select>

    <select id="getPlanningCurveHistory" resultType="com.ho.business.entity.PlanningCurveHistory">
        select temp_no,operate_no,start_time,end_time,p,soc_upper,soc_lower,effective_time from planning_curve_history
            where operate_no = #{planningCurveId}  order by start_time

    </select>
</mapper>
