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

    <insert id="addPlanningPolicy">
        insert into planning_policy
        (
        <if test="stationId != null">
            station_id,
        </if>
        <if test="policyTypeId != null">
            policy_type_id,
        </if>
        <if test="policyName != null and  policyName != ''">
            policy_name,
        </if>
        <if test="customPolicyName != null and customPolicyName != ''">
            custom_policy_name,
        </if>
        <if test="customPolicyStartDate != null">
            custom_policy_start_date,
        </if>
        <if test="customPolicyEndDate != null">
            custom_policy_end_date,
        </if>
        <if test="temId != null">
            tem_id,
        </if>
        <if test="policyPriorityId != null">
            policy_priority_id,
        </if>
        <if test="effectiveType != null">
            effective_type,
        </if>
        <if test="effectiveStartDate != null">
            effective_start_date,
        </if>
        <if test="effectiveEndDate != null">
            effective_end_date,
        </if>
        <if test="status != null">
            status,
        </if>
        <if test="context != null and context != ''">
            context,
        </if>
        create_time
        )
        values
        (
        <if test="stationId != null">
            #{stationId},
        </if>
        <if test="policyTypeId != null">
            #{policyTypeId},
        </if>
        <if test="policyName != null and policyName != ''">
            #{policyName},
        </if>
        <if test="customPolicyName != null and customPolicyName != ''">
            #{customPolicyName},
        </if>
        <if test="customPolicyStartDate != null">
            #{customPolicyStartDate,jdbcType=DATE},
        </if>
        <if test="customPolicyEndDate != null">
            #{customPolicyEndDate,jdbcType=DATE},
        </if>
        <if test="temId != null">
            #{temId},
        </if>
        <if test="policyPriorityId != null">
            #{policyPriorityId},
        </if>
        <if test="effectiveType != null">
            #{effectiveType},
        </if>
        <if test="effectiveStartDate != null">
            #{effectiveStartDate,jdbcType=DATE},
        </if>
        <if test="effectiveEndDate != null">
            #{effectiveEndDate,jdbcType=DATE},
        </if>
        <if test="status != null">
            #{status},
        </if>
        <if test="context != null and context != ''">
            #{context},
        </if>
        now()
        )
    </insert>

    <resultMap id="baseResult" type="com.ho.business.entity.PlanningPolicy">
        <id property="policyId" column="policy_id" />
        <result property="stationId" column="station_id"/>
        <result property="policyTypeId" column="policy_type_id"/>
        <result property="policyTypeName" column="policy_type_name"/>
        <result property="policyName" column="policy_name"/>
        <result property="customPolicyName" column="custom_policy_name"/>
        <result property="customPolicyStartDate" column="custom_policy_start_date"/>
        <result property="customPolicyEndDate" column="custom_policy_end_date"/>
        <result property="temId" column="tem_id"/>
        <result property="planningTemplateName" column="planning_template_name"/>
        <result property="policyPriorityId" column="policy_priority_id"/>
        <result property="policyPriorityName" column="policy_priority_name"/>
        <result property="effectiveType" column="effective_type"/>
        <result property="effectiveStartDate" column="effective_start_date"/>
        <result property="effectiveEndDate" column="effective_end_date"/>
        <result property="status" column="status"/>
        <result property="context" column="context"/>
        <result property="createTime" column="create_time"/>
        <result property="modifyTime" column="modify_time"/>
    </resultMap>

    <select id="getPlanningPolicys" resultMap="baseResult">
        select policy.policy_id,policy.station_id,policy.policy_type_id,type.policy_type_name,policy.policy_name,policy.custom_policy_name,policy.custom_policy_start_date,policy.custom_policy_end_date,
               policy.tem_id,tem.template_name planning_template_name,policy.policy_priority_id,priority.priority_name policy_priority_name,policy.effective_type,
               policy.effective_start_date,policy.effective_end_date,policy.status,policy.context
        from planning_policy policy
                 left join planning_policy_priority priority on priority.id = policy.policy_priority_id
                 left join planning_policy_type type on type.id = policy.policy_type_id
                 left join planning_curve_template tem on tem.id = policy.tem_id
        <where>
            <if test="customPolicyName != null and customPolicyName != ''">
                and policy.custom_policy_name like concat('%',#{customPolicyName},'%')
            </if>
            <if test="policyName != null and policyName != ''">
                and policy.policy_name like concat('%',#{policyName},'%')
            </if>
            <if test="policyTypeId != null">
                and policy.policy_type_id = #{policyTypeId}
            </if>
            <if test="status != null">
                and policy.status = #{status}
            </if>
        </where>
        order by policy.policy_priority_id
    </select>

    <resultMap id="curveResult" type="com.ho.business.entity.PlanningPolicy">
        <id property="policyId" column="policy_id" />
        <result property="stationId" column="station_id"/>
        <result property="policyTypeId" column="policy_type_id"/>
        <result property="policyTypeName" column="policy_type_name"/>
        <result property="policyName" column="policy_name"/>
        <result property="customPolicyName" column="custom_policy_name"/>
        <result property="customPolicyStartDate" column="custom_policy_start_date"/>
        <result property="customPolicyEndDate" column="custom_policy_end_date"/>
        <result property="temId" column="tem_id"/>
        <result property="planningTemplateName" column="planning_template_name"/>
        <result property="policyPriorityId" column="policy_priority_id"/>
        <result property="policyPriorityName" column="policy_priority_name"/>
        <result property="effectiveType" column="effective_type"/>
        <result property="effectiveStartDate" column="effective_start_date"/>
        <result property="effectiveEndDate" column="effective_end_date"/>
        <result property="status" column="status"/>
        <result property="context" column="context"/>
        <result property="createTime" column="create_time"/>
        <result property="modifyTime" column="modify_time"/>
        <collection property="curves" 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="getPlanningPolicyDetail4Modify" resultMap="curveResult">
        select policy.policy_id,policy.station_id,policy.policy_type_id,policy.policy_name,policy.custom_policy_name,policy.custom_policy_start_date,policy.custom_policy_end_date,
               policy.tem_id,policy.policy_priority_id,policy.effective_type,policy.effective_start_date,policy.effective_end_date,policy.context,policy.status,
               cur.start_time start_time, cur.end_time end_time, cur.p p, cur.q q, cur.soc soc
        from (
                 select policy_id,station_id,policy_type_id,policy_name,custom_policy_name,custom_policy_start_date,custom_policy_end_date,
                    tem_id,policy_priority_id,effective_type,effective_start_date,effective_end_date,context,create_time,modify_time,status
                 from planning_policy
                 <where>
                     <if test="policyId != null">
                         and policy_id = #{policyId}
                     </if>
                 </where>
             ) policy left join planning_curve cur on cur.planning_template_id = policy.tem_id
        order by policy.policy_priority_id,policy.modify_time desc, cur.start_time
    </select>

    <update id="modifyPlanningPolicy">
        update planning_policy set
        <if test="policyTypeId != null">
            policy_type_id = #{policyTypeId},
        </if>
        <if test="policyName != null and policyName != ''">
            policy_name = #{policyName},
        </if>
        <if test="customPolicyName != null and customPolicyName != ''">
            custom_policy_name = #{customPolicyName},
        </if>
        <if test="customPolicyStartDate != null">
            custom_policy_start_date = #{customPolicyStartDate,jdbcType=DATE},
        </if>
        <if test=" customPolicyEndDate != null">
            custom_policy_end_date = #{customPolicyEndDate,jdbcType=DATE},
        </if>
        <if test=" temId != null ">
            tem_id = #{temId},
        </if>
        <if test=" policyPriorityId != null ">
            policy_priority_id = #{policyPriorityId},
        </if>
        <if test=" effectiveType != null ">
            effective_type = #{effectiveType},
        </if>
        <if test=" effectiveStartDate != null">
            effective_start_date = #{effectiveStartDate,jdbcType=DATE},
        </if>
        <if test=" effectiveEndDate != null">
            effective_end_date = #{effectiveEndDate,jdbcType=DATE},
        </if>
        <if test="status != null">
            status = #{status},
        </if>
        <if test=" context != null and context != ''">
            context = #{context},
        </if>
        modify_time = now()
        where policy_id = #{policyId}
    </update>

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

    <resultMap id="dorpDownResult" type="com.ho.business.entity.DropDownVo">
        <id property="id" column="id" />
        <result property="name" column="name"/>
    </resultMap>

    <select id="queryPolicyTypes" resultMap="dorpDownResult">
        select id,policy_type_name name from planning_policy_type order by id
    </select>

    <select id="queryPolicyPriority" resultMap="dorpDownResult">
        select id,priority_name name from planning_policy_priority order by id
    </select>

    <insert id="batchPlanningPolicy">
        insert into planning_policy
        (
        station_id,
        policy_type_id,
        policy_name,
        custom_policy_name,
        custom_policy_start_date,
        custom_policy_end_date,
        tem_id,
        policy_priority_id,
        effective_type,
        effective_start_date,
        effective_end_date,
        status,
        context,
        create_time
        )
        values
        (
        <foreach collection="policys" item="policy" index="index">
            #{policy.stationId},
            #{policy.policyTypeId},
            #{policy.policyName},
            #{policy.customPolicyName},
            #{policy.customPolicyStartDate,jdbcType=DATE},
            #{policy.customPolicyEndDate,jdbcType=DATE},
            #{policy.temId},
            #{policy.policyPriorityId},
            #{policy.effectiveType},
            #{policy.effectiveStartDate,jdbcType=DATE},
            #{policy.effectiveEndDate,jdbcType=DATE},
            #{policy.status},
            #{policy.context},
            now()
        </foreach>
        )
    </insert>

    <select id="getPlanningCurveTemplates" resultType="com.ho.business.entity.PlanningCurveTemplate">
        select id temId,template_name from planning_curve_template where station_id = #{stationId}
    </select>

    <select id="checkName" resultType="java.lang.Integer">
        <if test="type == 1">
            select count(1) from planning_policy
            where policy_name = #{name}
            <if test="policyId != null">
                and policy_id &lt;> #{policyId}
            </if>
        </if>
        <if test="type == 2">
            select count(1) from planning_policy
            where custom_policy_name = #{name}
            <if test="policyId != null">
                and policy_id &lt;> #{policyId}
            </if>
        </if>
    </select>

</mapper>
