<?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.ztmap.project.zhny.strategy.mapper.TStrategyMapper">
    
    <resultMap type="TStrategy" id="TStrategyResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
    </resultMap>

    <resultMap type="TStrategy" id="getStrategyListResult">
        <result property="id"    column="strategy_id"    />
        <result property="name"    column="strategy_name"    />
        <collection  property="strategyConditionList"    javaType="java.util.List"        resultMap="TStrategyConditionResult" />
    </resultMap>

    <resultMap type="TStrategyCondition" id="TStrategyConditionResult">
        <result property="id"    column="sc_id"    />
        <result property="strategyId"    column="sc_strategy_id"    />
        <result property="conditionParams"    column="condition_params"    />
        <result property="conditionType"    column="condition_type"    />
        <result property="targetCondition"    column="target_condition"    />
        <result property="conditionParams2"    column="condition_params2"    />
        <result property="conditionType2"    column="condition_type2"    />
        <result property="targetCondition2"    column="target_condition2"    />
        <result property="conditionParams3"    column="condition_params3"    />
        <result property="conditionType3"    column="condition_type3"    />
        <result property="targetCondition3"    column="target_condition3"    />

        <!--   条件1     -->
        <result property="qufen"    column="qufen"    />
        <result property="condition_field1"    column="condition_field1"    />
        <result property="condition_field_type1"    column="condition_field_type1"    />
        <result property="condition_field2"    column="condition_field2"    />
        <result property="condition_field_type2"    column="condition_field_type2"    />
        <result property="condition_field3"    column="condition_field3"    />
        <result property="condition_field4"    column="condition_field4"    />
        <!--   条件1     -->
        <result property="qufen2"    column="qufen2"    />
        <result property="condition2_field1"    column="condition2_field1"    />
        <result property="condition2_field_type1"    column="condition2_field_type1"    />
        <result property="condition2_field2"    column="condition2_field2"    />
        <result property="condition2_field_type2"    column="condition2_field_type2"    />
        <result property="condition2_field3"    column="condition2_field3"    />
        <result property="condition2_field4"    column="condition2_field4"    />
        <!--   条件1     -->
        <result property="qufen3"    column="qufen3"    />
        <result property="condition3_field1"    column="condition3_field1"    />
        <result property="condition3_field_type1"    column="condition3_field_type1"    />
        <result property="condition3_field2"    column="condition3_field2"    />
        <result property="condition3_field_type2"    column="condition3_field_type2"    />
        <result property="condition3_field3"    column="condition3_field3"    />
        <result property="condition3_field4"    column="condition3_field4"    />
        <collection  property="strategyTargets"    javaType="java.util.List"        resultMap="TStrategyTargetResult" />
    </resultMap>

    <resultMap type="TStrategyTarget" id="TStrategyTargetResult">
        <result property="id"    column="st_id"    />
        <result property="conditionId"    column="st_condition_id"    />
        <result property="targetType"    column="target_type"    />
        <result property="lightingNum"    column="lighting_num"    />
        <result property="ktwd"    column="ktwd"    />
        <result property="ktfs"    column="ktfs"    />
        <result property="dxzs"    column="dxzs"    />
    </resultMap>

    <sql id="selectTStrategyVo">
        select id, name,  create_by, create_time from t_strategy
    </sql>

    <select id="getStrategyList" parameterType="map" resultMap="getStrategyListResult">
        SELECT
            s.id as strategy_id,
            s.`name` as strategy_name,
            sc.id as sc_id,
            sc.strategy_id as sc_strategy_id,
            sc.condition_params,
            sc.condition_type,
            sc.target_condition,
            sc.condition_params2,
            sc.condition_type2,
            sc.target_condition2,
            sc.condition_params3,
            sc.condition_type3,
            sc.target_condition3,
            st.id as st_id,
            st.condition_id as st_condition_id,
            st.target_type,
            st.lighting_num,
            st.ktwd,
            st.ktfs,
            st.dxzs,
            sc.qufen,
            sc.condition_field1,
            sc.condition_field_type1,
            sc.condition_field2,
            sc.condition_field_type2,
            sc.condition_field3,
            sc.condition_field4,
            sc.qufen2,
            sc.condition2_field1,
            sc.condition2_field_type1,
            sc.condition2_field2,
            sc.condition2_field_type2,
            sc.condition2_field3,
            sc.condition2_field4,
            sc.qufen3,
            sc.condition3_field1,
            sc.condition3_field_type1,
            sc.condition3_field2,
            sc.condition3_field_type2,
            sc.condition3_field3,
            sc.condition3_field4
        FROM
            t_strategy s
                LEFT JOIN t_strategy_condition sc on s.id = sc.strategy_id
                LEFT JOIN t_strategy_target st on sc.id = st.condition_id
        <where>
            <if test="strategyId!=null and strategyId != ''">
                and s.id = #{strategyId}
            </if>
        </where>
    </select>

    <select id="selectTStrategyList" parameterType="TStrategy" resultMap="TStrategyResult">
        <include refid="selectTStrategyVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
        </where>
    </select>
    
    <select id="selectTStrategyById" parameterType="Long" resultMap="TStrategyResult">
        <include refid="selectTStrategyVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTStrategy" parameterType="TStrategy" useGeneratedKeys="true" keyProperty="id">
        insert into t_strategy
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateTStrategy" parameterType="TStrategy">
        update t_strategy
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTStrategyById" parameterType="Long">
        delete from t_strategy where id = #{id}
    </delete>

    <delete id="deleteTStrategyByIds" parameterType="String">
        delete from t_strategy where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>