<?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.alarm.modules.plan.dao.PlanAssociatedDao">
  <!-- Result Map -->
  <resultMap id="BaseResultMap" type="com.devplatform.alarm.modules.plan.bean.PlanAssociated">
    <result column="id" property="id"/>
    <result column="et_id" property="etId"/>
    <result column="et_name" property="etName"/>
    <result column="sub_code" property="subCode"/>
    <result column="sub_system" property="subSystem"/>
    <result column="resource_num" property="resourceNum"/>
    <result column="sys_sign" property="sysSign"/>
    <result column="status" property="status"/>
    <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="station_id" property="stationId"/>
    <result column="byx1" property="byx1"/>
    <result column="byx2" property="byx2"/>
    <result column="byx3" property="byx3"/>
  </resultMap>

  <!-- plan_associated table all fields -->
  <sql id="Base_Column_List">
		id,et_id,et_name,sub_code,sub_system,resource_num,sys_sign,status,deleted,create_user_id,create_time,update_user_id,update_time,station_id,byx1,byx2,byx3
	</sql>

  <!-- 公共查询条件 -->
  <sql id="Example_Where_Clause">
    where deleted=0
    <if test="id!=null and id!=''">and id = #{id}</if>
    <if test="etId!=null and etId!=''">and et_id = #{etId}</if>
    <if test="etName!=null and etName!=''">and et_name = #{etName}</if>
    <if test="subCode!=null and subCode!=''">and sub_code = #{subCode}</if>
    <if test="subSystem!=null and subSystem!=''">and sub_system = #{subSystem}</if>
    <if test="resourceNum!=null ">and resource_num = #{resourceNum}</if>
    <if test="sysSign!=null and sysSign!=''">and sys_sign = #{sysSign}</if>
    <if test="status!=null ">and status = #{status}</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="stationId!=null and stationId!=''">and station_id = #{stationId}</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>
  </sql>

  <!--查询总数-->
  <select id="queryPageByCount" parameterType="Object" resultType="java.lang.Integer">
    select count(1) from plan_associated pa
    <include refid="queryPageByListClause"></include>
  </select>

  <select id="queryPageByList" parameterType="Object" resultMap="BaseResultMap">
    select pa.*,
    le.event_name AS eventName,
    dic.`name` AS resourceTypeName,
    dic2.NAME AS subSystemName
    FROM
    plan_associated pa
    LEFT JOIN link_event le ON le.id = pa.et_id
    LEFT JOIN sys_dictionary dic ON dic.id = pa.byx1
    LEFT JOIN sys_dictionary dic2 ON dic2.value=pa.sub_code AND dic2.parent_id=#{subSystemDicId} and dic2.station_id = pa.station_id
    <include refid="queryPageByListClause"></include>
    <if test="pager.orderCondition != null and pager.orderCondition != ''">
      ${pager.orderCondition}
    </if>
    <if test="pager.mysqlQueryCondition != null and pager.mysqlQueryCondition != ''">
      ${pager.mysqlQueryCondition}
    </if>
  </select>

  <sql id="queryPageByListClause">
    where pa.deleted=0
    <if test="id!=null and id!=''">and pa.id = #{id}</if>
    <if test="etId!=null and etId!=''">and pa.et_id = #{etId}</if>
    <if test="etName!=null and etName!=''">and pa.et_name = #{etName}</if>
    <if test="subCode!=null and subCode!=''">and pa.sub_code = #{subCode}</if>
    <if test="subSystem!=null and subSystem!=''">and pa.sub_system = #{subSystem}</if>
    <if test="resourceNum!=null ">and pa.resource_num = #{resourceNum}</if>
    <if test="sysSign!=null and sysSign!=''">and pa.sys_sign = #{sysSign}</if>
    <if test="status!=null ">and pa.status = #{status}</if>
    <if test="deleted!=null ">and pa.deleted = #{deleted}</if>
    <if test="createUserId!=null and createUserId!=''">and pa.create_user_id = #{createUserId}</if>
    <if test="createTime!=null ">and pa.create_time = #{createTime}</if>
    <if test="updateUserId!=null and updateUserId!=''">and pa.update_user_id = #{updateUserId}</if>
    <if test="updateTime!=null ">and pa.update_time = #{updateTime}</if>
    <if test="stationId!=null and stationId!=''">and pa.station_id = #{stationId}</if>
    <if test="byx1!=null and byx1!=''">and pa.byx1 = #{byx1}</if>
    <if test="byx2!=null and byx2!=''">and pa.byx2 = #{byx2}</if>
    <if test="byx3!=null and byx3!=''">and pa.byx3 = #{byx3}</if>
    <if test="planName!=null and planName!=''">
        and pa.id in
        (select DISTINCT pap.associated_id from plan_associated_plan pap
        left join plan_register pr on pr.id = pap.plan_id
        left join plan_associated pa on pa.id = pap.associated_id
        where pa.deleted = 0 and INSTR(pr.name, #{planName}))
    </if>
  </sql>

  <update id="deleteByIds">
    UPDATE plan_associated
    SET deleted =1
    WHERE id IN (
    <foreach collection="list" item="id" separator=",">
      #{id}
    </foreach>
    ) AND deleted = 0
  </update>

  <select id="queryPlanId" parameterType="object" resultMap="BaseResultMap">
    SELECT
    pa.*,
    par.resource_id as resourceId,
    pap.plan_id as planId
    FROM
    plan_associated pa
    LEFT JOIN plan_associated_resource par ON pa.id = par.associated_id
    LEFT JOIN plan_associated_plan pap on pa.id = pap.associated_id
    where pa.et_id = #{etId}
    and par.resource_id = #{resId}
    <if test="stationId!=null and stationId!=''">and pa.station_id = #{stationId}</if>
    and pa.deleted = 0
    <!--        SELECT-->
    <!--            pa.*,-->
    <!--            par.resource_id as resourceId,-->
    <!--            pap.plan_id as planId-->
    <!--        FROM-->
    <!--            plan_associated pa-->
    <!--            LEFT JOIN plan_associated_resource par ON pa.id = par.associated_id-->
    <!--            LEFT JOIN plan_associated_plan pap on pa.id = pap.associated_id-->
    <!--            LEFT JOIN plan_event_type pet on pa.et_id = pet.id-->
    <!--            where pet.byx1 = #{etId}-->
    <!--            and par.resource_id = #{resId}-->
    <!--            <if test="stationId!=null and stationId!=''">and pa.station_id = #{stationId}</if>-->
    <!--            and pa.deleted = 0-->
  </select>

  <select id="queryListByPlanId" parameterType="java.lang.String" resultMap="BaseResultMap">
        SELECT
            pa.*
        FROM
            plan_associated pa
            LEFT JOIN plan_associated_plan pap on pa.id = pap.associated_id
            where pap.plan_id = #{planId} and pa.deleted = 0
    </select>

  <select id="queryListByRtPlanId" parameterType="java.lang.String" resultMap="BaseResultMap">
        SELECT
            pa.*
        FROM
            plan_associated pa
            LEFT JOIN plan_associated_plan pap on pa.id = pap.associated_id
            LEFT JOIN plan_register pr on pr.id = pap.plan_id
            where pr.byx2 = #{planId} and pa.deleted = 0
    </select>


</mapper>