<?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.devplatform.equipment.modules.link.dao.LinkLinkageRuleDao">
  <!-- Result Map -->
  <resultMap id="BaseResultMap" type="com.devplatform.equipment.modules.link.bean.LinkLinkageRule">
    <result column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="resource_id" property="resourceId"/>
    <result column="sj_id" property="sjId"/>
    <result column="dz_id" property="dzId"/>
    <result column="execution_mode" property="executionMode"/>
    <result column="status" property="status"/>
    <result column="sys_sign" property="sysSign"/>
    <result column="deleted" property="deleted"/>
    <result column="create_user_id" property="createUserId"/>
    <result column="create_time" property="createTime"/>
    <result column="update_user_id" property="updateUserId"/>
    <result column="update_time" property="updateTime"/>
    <result column="byx1" property="byx1"/>
    <result column="byx2" property="byx2"/>
    <result column="byx3" property="byx3"/>
    <result column="sjName" property="sjName"/>
    <result column="resourceName" property="resourceName"/>
    <result column="stationName" property="stationName"/>
    <result column="deployLocation" property="deployLocation"/>
    <result column="station_id" property="stationId"/>
  </resultMap>

  <!-- link_linkage_rule table all fields -->
  <sql id="Base_Column_List">
        id,name,resource_id,sj_id,dz_id,execution_mode,status,sys_sign,deleted,create_user_id,create_time,update_user_id,update_time,byx1,byx2,byx3,sjName,station_id
    </sql>

  <!-- 公共查询条件 -->
  <sql id="Example_Where_Clause">
    where deleted=0
    <if test="id!=null and id!=''">and id = #{id}</if>
    <if test="name!=null and name!=''">and name = #{name}</if>
    <if test="resourceId!=null and resourceId!=''">and resource_id = #{resourceId}</if>
    <if test="sjId!=null and sjId!=''">and sj_id = #{sjId}</if>
    <if test="dzId!=null and dzId!=''">and dz_id = #{dzId}</if>
    <if test="executionMode!=null ">and execution_mode = #{executionMode}</if>
    <if test="status!=null ">and status = #{status}</if>
    <if test="sysSign!=null and sysSign!=''">and sys_sign = #{sysSign}</if>
    <if test="deleted!=null ">and deleted = #{deleted}</if>
    <if test="createUserId!=null and createUserId!=''">and create_user_id = #{createUserId}</if>
    <if test="createTime!=null ">and create_time = #{createTime}</if>
    <if test="updateUserId!=null and updateUserId!=''">and update_user_id = #{updateUserId}</if>
    <if test="updateTime!=null ">and update_time = #{updateTime}</if>
    <if test="byx1!=null and byx1!=''">and byx1 = #{byx1}</if>
    <if test="byx2!=null and byx2!=''">and byx2 = #{byx2}</if>
    <if test="byx3!=null and byx3!=''">and byx3 = #{byx3}</if>
    <if test="stationId!=null and stationId!=''">and station_id = #{stationId}</if>
  </sql>

  <select id="getListByPage" resultType="java.util.Map">
    select * from
    (SELECT llr.*, le.event_name AS event_name, sr.name AS resource_name, su.name AS user_name
    FROM link_linkage_rule llr
    LEFT JOIN link_event le ON le.id = llr.sj_id
    LEFT JOIN li_resource sr ON sr.id = llr.resource_id and sr.station_id = llr.station_id
    LEFT JOIN sys_user su ON su.user_id = llr.create_user_id and su.station_id = llr.station_id
    WHERE llr.deleted = 0
    <if test="params.name!=null and params.name!=''">
      and (INSTR(llr.name, #{params.name}) or INSTR(sr.name, #{params.name}))
    </if>
    <if test="params.stationId!=null and params.stationId!=''">
      and llr.station_id = #{params.stationId}
    </if>
    <if test="params.eventType!=null and params.eventType!=''">
      and INSTR(le.event_name, #{params.eventType})
    </if>) aa
    <if test="params.sort != null and params.sort != '' and params.order != null and params.order != ''">
      ORDER BY ${params.sort} ${params.order}
    </if>
  </select>

  <select id="queryById" resultMap="BaseResultMap">
    SELECT llr.*,
    le.event_name AS sjName,
    lr.name AS resourceName,
    lr.station_name AS stationName,
    lr.deploy_location AS deployLocation
    FROM link_linkage_rule llr
    LEFT JOIN link_event le ON le.id = llr.sj_id
    LEFT JOIN li_resource lr ON lr.id = llr.resource_id and lr.station_id = llr.station_id
    WHERE llr.id = #{id}
    <if test="stationId!=null and stationId!=''">and llr.station_id = #{stationId}</if>
  </select>

  <select id="getLinkRule" resultMap="linkRuleList">
    SELECT llr.*,
    lr.name AS resourceName,
    lr.station_name AS stationName,
    lr.deploy_location AS deployLocation,
    lr.type,
    lr.sub_code AS subCode,
    lr.station_id AS stationId,
    lr.line_name AS lineName,
    CONCAT(lr.line_name, '-', lr.station_name) AS lineStationName,
    sd.name AS typeName,
    le.event_name AS eventName
    FROM link_linkage_rule llr
    LEFT JOIN link_event le ON le.id = llr.sj_id AND le.deleted = 0
    LEFT JOIN li_resource lr ON lr.id = llr.resource_id AND lr.deleted = 0 and lr.station_id = llr.station_id
    LEFT JOIN sys_dictionary sd ON sd.id = lr.type AND sd.deleted = 0 and sd.station_id = llr.station_id
    WHERE llr.resource_id = #{resourceId} and llr.deleted = 0
    <if test="stationId!=null and stationId!=''">and llr.station_id = #{stationId}</if>
  </select>

  <resultMap id="linkRuleList" type="java.util.HashMap">
    <result column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="resource_id" property="resourceId"/>
    <result column="sj_id" property="sjId"/>
    <result column="dz_id" property="dzId"/>
    <result column="execution_mode" property="executionMode"/>
    <result column="status" property="status"/>
    <result column="sys_sign" property="sysSign"/>
    <result column="deleted" property="deleted"/>
    <result column="create_user_id" property="createUserId"/>
    <result column="create_time" property="createTime"/>
    <result column="update_user_id" property="updateUserId"/>
    <result column="update_time" property="updateTime"/>
    <result column="byx1" property="byx1"/>
    <result column="byx2" property="byx2"/>
    <result column="byx3" property="byx3"/>
    <result column="eventName" property="eventName"/>
    <result column="resourceName" property="resourceName"/>
    <result column="stationName" property="stationName"/>
    <result column="deployLocation" property="deployLocation"/>
    <result column="type" property="type"/>
    <result column="subCode" property="subCode"/>
    <result column="stationId" property="stationId"/>
    <result column="lineName" property="lineName"/>
    <result column="lineStationName" property="lineStationName"/>
    <result column="typeName" property="typeName"/>
    <collection column="{id=id,stationId=stationId}" property="actions" select="getAction"/>
  </resultMap>

  <select id="getAction" resultMap="linkRuleAction">
    SELECT lra.id,
    lra.rule_id,
    lra.action_id,
    lra.sys_sign,
    lra.sort_value,
    lra.resource_id,
    la.params_name1,
    lra.params_value1,
    la.params_name2,
    lra.params_value2,
    la.action_info,
    lr.name AS resourceName,
    lr.station_name,
    lr.deploy_location,
    lr.address_code,
    lr.port,
    lr.username,
    lr.password,
    la.byx1 AS unit1,
    la.byx2 AS unit2,
    lr.type,
    lr.sub_code AS subCode,
    lr.station_id AS stationId,
    lr.line_name AS lineName,
    CONCAT(lr.line_name, '-', lr.station_name) AS lineStationName,
    sd.name AS typeName,
    lr.brand,
    lr.byx1 as channel
    FROM link_rule_action lra
    LEFT JOIN link_action la ON la.id = lra.action_id and la.station_id = lra.station_id
    LEFT JOIN li_resource lr ON lr.id = lra.resource_id and lr.station_id = lra.station_id
    LEFT JOIN sys_dictionary sd ON sd.id = lr.type and sd.station_id = lra.station_id
    WHERE lra.rule_id = #{id}
    <if test="stationId!=null and stationId!=''">and lra.station_id = #{stationId}</if>
    ORDER BY lra.sort_value
  </select>

  <resultMap id="linkRuleAction" type="java.util.HashMap">
    <result column="id" property="id"/>
    <result column="rule_id" property="ruleId"/>
    <result column="action_id" property="actionId"/>
    <result column="params_name1" property="paramsName1"/>
    <result column="params_name2" property="paramsName2"/>
    <result column="params_value1" property="paramsValue1"/>
    <result column="params_value2" property="paramsValue2"/>
    <result column="sys_sign" property="sysSign"/>
    <result column="sort_value" property="sortValue"/>
    <result column="resource_id" property="resourceId"/>
    <result column="resourceName" property="resourceName"/>
    <result column="deploy_location" property="deployLocation"/>
    <result column="station_name" property="stationName"/>
    <result column="action_info" property="actionInfo"/>
    <result column="address_code" property="addressCode"/>
    <result column="port" property="port"/>
    <result column="username" property="username"/>
    <result column="password" property="password"/>
    <result column="unit1" property="unit1"/>
    <result column="unit2" property="unit2"/>
    <result column="type" property="type"/>
    <result column="subCode" property="subCode"/>
    <result column="stationId" property="stationId"/>
    <result column="lineName" property="lineName"/>
    <result column="lineStationName" property="lineStationName"/>
    <result column="typeName" property="typeName"/>
    <result column="brand" property="brand"/>
    <result column="channel" property="channel"/>
  </resultMap>
</mapper>


