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

    <resultMap id="baseResult" type="com.ho.business.entity.PlanningCurveTactics">
        <id property="id" column="id" />
        <result property="temId" column="tem_id" />
        <result property="startDate" column="start_date"/>
        <result property="endDate" column="end_date"/>
        <result property="type" column="type"/>
        <result property="createTime" column="create_time"/>
        <result property="creator" column="creator"/>
        <result property="updateTime" column="update_time"/>
        <result property="modifier" column="modifier"/>
        <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="getTacticsList" resultType="com.ho.business.entity.PlanningCurveTactics">
        select ct.id,ct.tem_id,ct.start_date,ct.end_date,ct.type,ct.create_time,ct.creator,
               t.template_name as tem_name
        from planning_curve_tactics ct
        left join planning_curve_template t on ct.tem_id = t.id
        where station_id = #{stationId}
        <if test="temName != null and temName != ''" >
            and t.template_name like concat('%',#{temName},'%')
        </if>
        <if test="type != null">
            and ct.type = #{type}
        </if>
        <if test="startDate != null and endDate != null">
            and (
            ct.start_date between #{startDate} and #{endDate}
                or
            ct.end_date between #{startDate} and #{endDate}
            )
        </if>
        <if test="pageNum != null and pageSize != null" >
            limit ${pageNum},${pageSize}
        </if>
    </select>

    <select id="getTacticsDetails" resultType="com.ho.business.entity.PlanningCurveTactics">
        select ct.tem_id,ct.start_date,ct.end_date,ct.type,ct.create_time,ct.creator
        from planning_curve_tactics ct
        where ct.id = #{id}
    </select>

    <select id="getPlanningCurves" resultType="com.ho.business.entity.PlanningCurve">
        select 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 planning_curve cur
        where cur.planning_template_id = #{temId}
    </select>

    <insert id="add" parameterType="com.ho.business.entity.PlanningCurveTactics">
        INSERT INTO `business_db`.`planning_curve_tactics`
            ( `tem_id`, `start_date`, `end_date`, `type`, `create_time`, `creator`)
        VALUES ( #{temId}, #{startDate}, #{endDate}, #{type}, now(), #{creator})
    </insert>

    <update id="modify" parameterType="com.ho.business.entity.PlanningCurveTactics">
        UPDATE `planning_curve_tactics`
        SET
            `tem_id` = #{temId},
            `start_date` = date_format(#{startDate},'%Y-%m-%d'),
            `end_date` = date_format(#{endDate},'%Y-%m-%d'),
            `type` = #{type},
            `update_time` = now(),
            `modifier` = #{modifier}
        WHERE `id` = #{id}
    </update>

    <delete id="delete" parameterType="java.lang.Integer">
        delete from planning_curve_tactics where `id` = #{id}
    </delete>

    <select id="getPlanningCurveTemplates" resultType="com.ho.business.entity.PlanningCurveTactics">
        select t.id as value,t.template_name title  from planning_curve_template t
        where t.station_id = #{stationId}
    </select>

    <select id="getTacticsByStationId" resultType="com.ho.business.entity.PlanningCurveTactics">
        select ct.tem_id,ct.start_date,ct.end_date,ct.type,ct.create_time,ct.creator,
               t.template_name as tem_name
        from planning_curve_tactics ct
        left join planning_curve_template t on ct.tem_id = t.id
        where station_id = #{stationId}
    </select>

</mapper>
