<?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.dcs.hnyz.mapper.DcsGroupTaskLineMapper">

    <resultMap type="DcsGroupTaskLine" id="DcsGroupTaskLineResult">
        <result property="lineId"    column="line_id"    />
        <result property="groupId"    column="group_id"    />
        <result property="actionId"    column="action_id"    />
        <result property="actionName"    column="action_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="actionNum"    column="action_num"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <resultMap id="GroupTaskLineActionConfigResult" type="DcsGroupTaskLine" extends="DcsGroupTaskLineResult">
<!--        <collection property="actionConfigList" notNullColumn="action_id" javaType="java.util.List" resultMap="ActionConfigResult" />-->
        <association property="actionConfig" notNullColumn="action_id" resultMap="ActionConfigResult"/>
    </resultMap>

    <resultMap type="ActionConfig" id="ActionConfigResult">
        <result property="actionId"    column="sub_action_id"    />
        <result property="equipmentId"    column="sub_equipment_id"    />
        <result property="equipmentName"    column="sub_equipment_name"    />
        <result property="actionType"    column="sub_action_type"    />
        <result property="value"    column="sub_value"    />
        <result property="unit"    column="sub_unit"    />
        <result property="triggerType"    column="sub_trigger_type"    />
        <result property="triggerCondition"    column="sub_trigger_condition"    />
        <result property="triggerDelay"    column="sub_trigger_delay"    />
        <result property="status"    column="sub_status"    />
        <result property="createBy"    column="sub_create_by"    />
        <result property="createTime"    column="sub_create_time"    />
        <result property="updateBy"    column="sub_update_by"    />
        <result property="updateTime"    column="sub_update_time"    />
        <result property="remark"    column="sub_remark"    />
    </resultMap>

    <sql id="selectDcsGroupTaskLineVo">
        select gl.line_id, gl.group_id, gl.action_id, gl.action_name, gl.parent_id, gl.action_num, gl.status, gl.del_flag, gl.create_by, gl.update_by, gl.create_time, gl.update_time, gl.remark
        ,ac.equipment_id as sub_equipment_id,(select equipment_name from equipment where equipment_id = ac.equipment_id ) sub_equipment_name,ac.action_type as sub_action_type,ac.value as sub_value,ac.unit as sub_unit,ac.trigger_type as sub_trigger_type
        ,ac.trigger_condition as sub_trigger_condition,ac.trigger_delay as sub_trigger_delay,ac.status as sub_status,ac.create_by as sub_create_by,ac.create_time as sub_create_time
        ,ac.update_by as sub_update_by,ac.update_time as sub_update_time,ac.remark as sub_remark
        from dcs_group_task_line gl
        left join action_config ac on ac.action_id = gl.action_id
    </sql>

    <select id="selectDcsGroupTaskLineList" parameterType="DcsGroupTaskLine" resultMap="GroupTaskLineActionConfigResult">
        <include refid="selectDcsGroupTaskLineVo"/>
        <where>
            <if test="groupId != null "> and gl.group_id = #{groupId}</if>
            <if test="actionId != null "> and gl.action_id = #{actionId}</if>
            <if test="actionName != null  and actionName != ''"> and gl.action_name like concat('%', #{actionName}, '%')</if>
            <if test="parentId != null "> and gl.parent_id = #{parentId}</if>
            <if test="actionNum != null "> and gl.action_num = #{actionNum}</if>
            <if test="status != null"> and gl.status = #{status}</if>
        </where>
        order by gl.action_num
    </select>

    <select id="selectDcsGroupTaskLineByLineId" parameterType="Long" resultMap="DcsGroupTaskLineResult">
        <include refid="selectDcsGroupTaskLineVo"/>
        where gl.line_id = #{lineId}
    </select>

    <insert id="insertDcsGroupTaskLine" parameterType="DcsGroupTaskLine" useGeneratedKeys="true" keyProperty="lineId">
        insert into dcs_group_task_line
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="groupId != null">group_id,</if>
            <if test="actionId != null">action_id,</if>
            <if test="actionName != null">action_name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="actionNum != null">action_num,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="groupId != null">#{groupId},</if>
            <if test="actionId != null">#{actionId},</if>
            <if test="actionName != null">#{actionName},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="actionNum != null">#{actionNum},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateDcsGroupTaskLine" parameterType="DcsGroupTaskLine">
        update dcs_group_task_line
        <trim prefix="SET" suffixOverrides=",">
            <if test="groupId != null">group_id = #{groupId},</if>
            <if test="actionId != null">action_id = #{actionId},</if>
            <if test="actionName != null">action_name = #{actionName},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="actionNum != null">action_num = #{actionNum},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where line_id = #{lineId}
    </update>

    <delete id="deleteDcsGroupTaskLineByLineId" parameterType="Long">
        delete from dcs_group_task_line where line_id = #{lineId}
    </delete>

    <delete id="deleteDcsGroupTaskLineByLineIds" parameterType="String">
        delete from dcs_group_task_line where line_id in
        <foreach item="lineId" collection="array" open="(" separator="," close=")">
            #{lineId}
        </foreach>
    </delete>
</mapper>
