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

    <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"/>
        <result property="modelType" column="model_type"/>
        <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="getSeq" resultType="java.lang.Integer">
        select ifnull(max(id),0)+1 from planning_curve_template
    </select>

    <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} or station_id is null )
    </select>

    <insert id="addPlanningCurveTemplate" parameterType="com.ho.business.entity.PlanningCurveTemplate" keyColumn="id" keyProperty="temId" useGeneratedKeys="true">
        insert into planning_curve_template (
                                             id,template_no,template_name,
        <if test="stationId != null">
            station_id,
        </if>
        <if test="groupId != null">
            group_id,
        </if>
        <if test="income != null" > income, </if>
        <if test="status != null"> `status`, </if>
        <if test="temP != null"> p, </if>
        <if test="capacity != null"> capacity, </if>
        <if test="socUpper != null"> soc_upper, </if>
        <if test="socLower != null"> soc_lower, </if>
        create_time
        )
        values(
               #{temId},
               #{templateNo},
               #{templateName},
        <if test="stationId != null">
            #{stationId},
        </if>
        <if test="groupId != null">
            #{groupId},
        </if>
        <if test="income != null">
            #{income},
        </if>
        <if test="status != null">#{status},</if>
        <if test="temP != null">#{temP},</if>
        <if test="capacity != null">#{capacity},</if>
        <if test="socUpper != null">#{socUpper},</if>
        <if test="socLower != null">#{socLower},</if>
               sysdate() )
    </insert>

    <!-- 新增曲线 -->
    <insert id="addPlanningCurve" parameterType="com.ho.business.entity.PlanningCurve">
        insert into planning_curve ( planning_template_id,start_time,end_time,p,q) values
        <foreach collection="curves" item="item" index="index" separator=",">
            ( #{planningTemplateId},#{item.startTime},#{item.endTime},#{item.p},#{item.q})
        </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="elecTemplateId != null and elecTemplateId != ''">
            elec_template_id = #{elecTemplateId},
        </if>
         -->
            <if test="stationId != null ">
                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="deleteTemplateById">
        delete from planning_curve_template where id = #{temId}
    </delete>

    <delete id="deleteCurves">
        delete from planning_curve where planning_template_id = #{temId}
    </delete>

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

    <insert id="addPolicyConfig" parameterType="com.ho.business.entity.PolicyConfig">
        insert into peak_policy_config (id,station_id,plan_id,tem_id,`month`)
        values(#{id},#{stationId},#{planId},#{temId},#{month})
    </insert>

    <insert id="addPolicyConfigData" parameterType="com.ho.business.vo.req.PolicyConfigData">
        insert into peak_policy_config_data (`day`,tem_id,plan_id)
        values
        <foreach item="item" collection="datas" index="index" separator=",">
            (#{item.day},#{item.temId},#{item.planId})
        </foreach>

    </insert>

    <select id="queryPolicyConifg" parameterType="com.ho.business.entity.PolicyConfig" resultType="com.ho.business.vo.req.PolicyConfigData">
        select ppcd.id,ppcd.day,ppcd.plan_id,ppcd.tem_id
        from peak_policy_config ppc
        left join peak_policy_config_data ppcd on ppc.plan_id = ppcd.plan_id
        where month = #{month} and station_id = #{stationId}
        order by ppcd.`day`
    </select>

    <select id="queryPolicyConifgMonth" resultType="com.ho.business.entity.PolicyConfig">
        select id,station_id,plan_id,tem_id,`month`,case when tem_id is null then concat(plan_id,'_',1) else concat(tem_id,'_',0) end as `key` from peak_policy_config
        where station_id = #{stationId}
    </select>

    <!-- 查询模板列表接口 -->
    <select id="getPlanningCurveTemplates" 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, 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,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="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 = #{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>

    <resultMap id="tableQuery" type="com.ho.business.entity.PlanningCurveTableTemplate">
        <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"/>
        <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"/>
    </resultMap>

    <select id="getPlanningCurveTableTemplates" resultMap="tableQuery">
        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,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="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 = #{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="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,
        case when tem.station_id is null then 0 else 1 end as model_type
        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="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>
        limit 1
        ) 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="getTemplateIds" resultType="java.lang.Integer">
        select id from planning_curve_template where station_id = #{stationId} or station_id is null
    </select>

    <select id="queryTemplateCurves" resultType="com.ho.business.entity.PlanningCurve">
        select c.p,c.q,c.soc,c.planning_template_id,c.start_time,c.id,t.template_name from planning_curve c
        join planning_curve_template t on t.id = c.planning_template_id
        where c.planning_template_id = #{temId}
    </select>

    <delete id="deletePolicyConfig" parameterType="com.ho.business.entity.PolicyConfig">
        delete c,d from peak_policy_config c
               join peak_policy_config_data d on c.plan_id = d.plan_id
               where station_id = #{stationId} and c.month = #{month}
    </delete>

    <select id="getIssueDevice" resultType="com.ho.business.vo.resp.planningCurve.PlanningIssueVo">
        SELECT d.src_id,d.station_id,d.device_type,sn.sn from device d
        join station_sn sn on sn.station_id = d.station_id
        where device_type like '%plan%' order by station_id
    </select>
<!--
    <select id="queryPolicyConfigByParam" resultType="com.ho.business.vo.PolicyConfigVo">
        select d.tem_id from peak_policy_config c
        join peak_policy_config_data d on c.plan_id = d.plan_id
        where c.station_id = #{stationId} and c.month = #{month}
        and d.tem_id is not null
        limit 1
    </select>
    -->

    <!-- 此查询方法排除了后台生成的方案 -->
    <select id="checkUse" resultType="java.lang.Integer">
        select count(1) from peak_policy_config_data d
                                 join peak_policy_config_plan p on d.plan_id = p.id
        where d.tem_id = #{temId} and p.type = 0
    </select>

    <select id="checkUse2" resultType="java.lang.Integer">
        select count(1) from peak_policy_config c
                                 join peak_policy_config_plan p on c.plan_id = p.id
        where c.tem_id = #{temId}
    </select>

    <select id="queryIssueDevice" resultType="java.lang.Integer">
        select src_id
        from device where (device_type like 'agc%' or device_type like 'planCurve%' ) and station_id = #{stationId}
        limit 1
    </select>
    
    <select id="getSnByStationId" resultType="java.lang.String">
        select sn from station_sn where station_id = #{stationId}
    </select>

    <select id="queryPeakPolicyConfigData" resultType="com.ho.business.vo.req.PolicyConfigData">
        select d.day,c.month,d.tem_id from peak_policy_config c
        join peak_policy_config_data d on c.plan_id = d.plan_id
        where c.station_id = #{stationId}
        order by month,day
    </select>

    <resultMap id="peakResult" type="com.ho.business.entity.PolicyConfigPlan">
        <id property="id" column="id" />
        <result property="planName" column="plan_name"/>
        <result property="createTime" column="create_time"/>
        <result property="stationId" column="station_id"/>
        <result property="month" column="month"/>
        <result property="type" column="type"/>
        <collection property="childs" ofType="com.ho.business.vo.req.PolicyConfigData">
            <id property="id" column="data_id"/>
            <result property="day" column="day"/>
            <result property="month" column="month"/>
            <result property="temId" column="tem_id"/>
            <result property="planId" column="plan_id"/>
            <result property="templateName" column="template_name"/>
        </collection>
    </resultMap>

    <select id="queryPlanList" resultMap="peakResult">
        select plan.id,plan.plan_name,plan.create_time,plan.station_id,plan.month,plan.type,
               d.id as data_id,d.day,d.tem_id,d.plan_id,
               t.template_name from peak_policy_config_plan plan
        join peak_policy_config_data d on d.plan_id = plan.id
        join planning_curve_template t on t.id = d.tem_id
        where plan.station_id = #{stationId} and plan.type = 0
        <if test="month != null" >
            and month = #{month}
        </if>
        order by plan.month
    </select>

    <select id="queryPeakPolicyList" resultType="com.ho.business.entity.PeakShavingBean">
        select t.id,t.template_name name,0 type,concat(t.id,'_',0) as `key` from planning_curve_template t
        where ( t.station_id = #{stationId} or t.station_id is null ) and t.status = 1
        order by t.template_name
    </select>
    
    <delete id="deletePlan" parameterType="com.ho.business.entity.PeakShavingBean">
        delete c,d from peak_policy_config_plan c
                            join peak_policy_config_data d on c.id = d.plan_id
        where c.id = #{id}
    </delete>

    <select id="checkPlanUse" resultType="java.lang.Integer">
        select count(1) from peak_policy_config where plan_id = #{id}
    </select>

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

    <insert id="addPlan" parameterType="com.ho.business.entity.PolicyConfigPlan">
        insert into peak_policy_config_plan( id,plan_name,station_id,`month`,create_time,type)
        values ( #{id},#{planName},#{stationId},#{month},now(),#{type} )
    </insert>

    <select id="queryPolicyConifgDetail" resultType="com.ho.business.vo.req.PolicyConfigData">
        select ppcd.day,pct.template_name from peak_policy_config_plan ppcp
                                                   join peak_policy_config_data ppcd on ppcp.id = ppcd.plan_id
                                                   join planning_curve_template pct on pct.id = ppcd.tem_id
        where ppcp.id = #{planId}
        order by ppcd.day
    </select>

    <delete id="deletePolicyConfigByMonth">
        delete from peak_policy_config where month = #{month} and station_id = #{stationId}
    </delete>

    <select id="queryHolidayList" resultType="com.ho.business.vo.Holiday">
        select id,date,type,name,week,DATE_FORMAT(date,'%Y') year,DATE_FORMAT(date,'%d') day from dict_holiday where DATE_FORMAT(date,'%m') = #{month} order by date
    </select>

    <select id="queryTemplateStatus" resultType="java.lang.Integer">
        select `status` from planning_curve_template where id = #{temId}
    </select>

    <select id="checkPlanName" resultType="java.lang.Integer">
        select count(1) from peak_policy_config_plan where station_id = #{stationId} and plan_name = #{planName} and type = 0 and month = #{month}
    </select>

    <select id="getPCT" resultType="com.ho.business.entity.PlanningCurveTemplate">
        select tem.id tem_id, tem.template_no template_no, tem.template_name template_name,
            tem.station_id, tem.income, tem.group_id, tem.status status, tem.p tem_p, tem.capacity, tem.soc_upper,tem.soc_lower
        from planning_curve_template tem
        where template_no = #{templateNo} and template_name = #{templateName} and station_id = #{stationId} limit 1
    </select>

    <insert id="insertPlanningCurve" parameterType="com.ho.business.entity.PlanningCurve">
        insert into planning_curve ( planning_template_id,start_time,end_time,p,q) values
        ( #{planningTemplateId},#{startTime},#{endTime},#{p},#{q})
    </insert>

    <delete id="deletePlanningCurve" >
        delete from planning_curve
            where planning_template_id = #{templateId} and start_time = #{startTime}
    </delete>

</mapper>
