<?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.liresource.dao.LiResourceDao">
  <!-- Result Map -->
  <resultMap id="BaseResultMap" type="com.devplatform.equipment.modules.liresource.bean.LiResource">
    <result column="id" property="id"/>
    <result column="name" property="name"/>
    <result column="code" property="code"/>
    <result column="type" property="type"/>
    <result column="line_id" property="lineId"/>
    <result column="line_name" property="lineName"/>
    <result column="station_id" property="stationId"/>
    <result column="station_name" property="stationName"/>
    <result column="tier_id" property="tierId"/>
    <result column="sub_system" property="subSystem"/>
    <result column="sub_code" property="subCode"/>
    <result column="address_code" property="addressCode"/>
    <result column="port" property="port"/>
    <result column="ajj_port" property="ajjPort"/>
    <result column="username" property="username"/>
    <result column="password" property="password"/>
    <result column="xpoint" property="xpoint"/>
    <result column="ypoint" property="ypoint"/>
    <result column="width" property="width"/>
    <result column="rotate" property="rotate"/>
    <result column="deploy_location" property="deployLocation"/>
    <result column="operator_id" property="operatorId"/>
    <result column="operator_name" property="operatorName"/>
    <result column="attached_info" property="attachedInfo"/>
    <result column="remark" property="remark"/>
    <result column="icon" property="icon"/>
    <result column="parent_name" property="parentName"/>
    <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="byx1" property="byx1"/>
    <result column="byx2" property="byx2"/>
    <result column="byx3" property="byx3"/>
    <result column="brand" property="brand"/>
    <result column="xlongit" property="xlongit"/>
    <result column="ylat" property="ylat"/>
    <result column="image_src" property="imageSrc"/>
  </resultMap>

  <!-- li_resource table all fields -->
  <sql id="Base_Column_List">
		id,name,code,type,line_id,line_name,station_id,station_name,tier_id,sub_system,sub_code,address_code,port,ajj_port,username,password,xpoint,ypoint,width,rotate,deploy_location,operator_id,operator_name,attached_info,remark,icon,parent_name,sys_sign,status,deleted,create_user_id,create_time,update_user_id,update_time,byx1,byx2,byx3,brand,xlongit,ylat,image_src
	</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="code!=null and code!=''">and code = #{code}</if>
    <if test="type!=null and type!=''">and type = #{type}</if>
    <if test="lineId!=null and lineId!=''">and line_id = #{lineId}</if>
    <if test="lineName!=null and lineName!=''">and line_name = #{lineName}</if>
    <if test="stationId!=null and stationId!=''">and station_id = #{stationId}</if>
    <if test="stationName!=null and stationName!=''">and station_name = #{stationName}</if>
    <if test="tierId!=null and tierId!=''">and tier_id = #{tierId}</if>
    <if test="subSystem!=null and subSystem!=''">and sub_system = #{subSystem}</if>
    <if test="subCode!=null and subCode!=''">and sub_code = #{subCode}</if>
    <if test="addressCode!=null and addressCode!=''">and address_code = #{addressCode}</if>
    <if test="port!=null and port!=''">and port = #{port}</if>
    <if test="ajjPort!=null and ajjPort!=''">and ajj_port = #{ajjPort}</if>
    <if test="username!=null and username!=''">and username = #{username}</if>
    <if test="password!=null and password!=''">and password = #{password}</if>
    <if test="xpoint!=null and xpoint!=''">and xpoint = #{xpoint}</if>
    <if test="ypoint!=null and ypoint!=''">and ypoint = #{ypoint}</if>
    <if test="deployLocation!=null and deployLocation!=''">and deploy_location = #{deployLocation}</if>
    <if test="operatorId!=null and operatorId!=''">and operator_id = #{operatorId}</if>
    <if test="operatorName!=null and operatorName!=''">and operator_name = #{operatorName}</if>
    <if test="attachedInfo!=null and attachedInfo!=''">and attached_info = #{attachedInfo}</if>
    <if test="remark!=null and remark!=''">and remark = #{remark}</if>
    <if test="icon!=null and icon!=''">and icon = #{icon}</if>
    <if test="parentName!=null and parentName!=''">and parent_name = #{parentName}</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="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="brand!=null and brand!=''">and brand = #{brand}</if>
  </sql>

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

  <select id="queryPageByList" parameterType="Object" resultMap="BaseResultMap">
    select * from li_resource
    <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 deleted=0
    <if test="id!=null and id!=''">and id = #{id}</if>
    <if test="name!=null and name!=''">and name = #{name}</if>
    <if test="code!=null and code!=''">and code = #{code}</if>
    <if test="type!=null and type!=''">and type = #{type}</if>
    <if test="lineId!=null and lineId!=''">and line_id = #{lineId}</if>
    <if test="lineName!=null and lineName!=''">and line_name = #{lineName}</if>
    <if test="stationId!=null and stationId!=''">and station_id = #{stationId}</if>
    <if test="stationName!=null and stationName!=''">and station_name = #{stationName}</if>
    <if test="tierId!=null and tierId!=''">and tier_id = #{tierId}</if>
    <if test="subSystem!=null and subSystem!=''">and sub_system = #{subSystem}</if>
    <if test="subCode!=null and subCode!=''">and sub_code = #{subCode}</if>
    <if test="addressCode!=null and addressCode!=''">and address_code = #{addressCode}</if>
    <if test="port!=null and port!=''">and port = #{port}</if>
    <if test="ajjPort!=null and ajjPort!=''">and ajj_port = #{ajjPort}</if>
    <if test="username!=null and username!=''">and username = #{username}</if>
    <if test="password!=null and password!=''">and password = #{password}</if>
    <if test="xpoint!=null and xpoint!=''">and xpoint = #{xpoint}</if>
    <if test="ypoint!=null and ypoint!=''">and ypoint = #{ypoint}</if>
    <if test="deployLocation!=null and deployLocation!=''">and deploy_location = #{deployLocation}</if>
    <if test="operatorId!=null and operatorId!=''">and operator_id = #{operatorId}</if>
    <if test="operatorName!=null and operatorName!=''">and operator_name = #{operatorName}</if>
    <if test="attachedInfo!=null and attachedInfo!=''">and attached_info = #{attachedInfo}</if>
    <if test="remark!=null and remark!=''">and remark = #{remark}</if>
    <if test="icon!=null and icon!=''">and icon = #{icon}</if>
    <if test="parentName!=null and parentName!=''">and parent_name = #{parentName}</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="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="brand!=null and brand!=''">and brand = #{brand}</if>
  </sql>

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

  <select id="getTreeList" parameterType="Object" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"></include>
    from li_resource
    <include refid="queryPageByListClause"></include>
    and sub_code in('01','02','04')
    and deleted = 0
    order by create_time desc
  </select>

  <select id="getBeanById" parameterType="Object" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"></include>
    ,(select name from li_station_map lm where lm.id=lr.tier_id and lm.deleted=0) as tierName
    ,(select name from sys_dictionary sd where sd.id=lr.type and sd.deleted=0 and sd.station_id = lr.station_id) as typeName
    from li_resource lr
    where id=#{id} and deleted=0
  </select>

  <select id="getByIds" parameterType="list" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"></include>
    FROM li_resource lr
    WHERE deleted = 0 and id IN (
    <foreach collection="list" item="id" separator=",">
      #{id}
    </foreach>
    ) AND deleted = 0
  </select>

  <select id="getNormalByIds" parameterType="list" resultMap="BaseResultMap">
    SELECT
    <include refid="Base_Column_List"></include>
    FROM li_resource lr
    WHERE deleted = 0 AND id IN (
    <foreach collection="list" item="id" separator=",">
      #{id}
    </foreach>
    )
  </select>

  <select id="getTreeListByModel" parameterType="com.devplatform.equipment.modules.liresource.model.LiResourceModel"
    resultType="com.devplatform.equipment.modules.liresource.bean.LiResource">
    select
    <include refid="Base_Column_List"></include>
    from li_resource
    WHERE deleted = 0
    <if test="stationId!=null and stationId!=''">and station_id = #{stationId}</if>
    <if test="tierId!=null and tierId!=''">and tier_id = #{tierId}</if>
    <if test="type!=null and type!=''">and type = #{type}</if>
    <if test="subCode!=null and subCode!=''">and sub_code = #{subCode}</if>
    <if test="name!=null and name!=''">and INSTR(name, #{name})</if>
    order by create_time desc
  </select>

  <select id="getAllResourceNameList" parameterType="com.devplatform.equipment.modules.liresource.model.LiResourceModel"
    resultType="java.lang.String">
        SELECT name FROM li_resource
        where deleted = 0 AND station_id = #{stationId} AND sub_code = #{subCode}
    </select>

  <select id="queryByEnshrine" resultType="com.devplatform.equipment.modules.liresource.bean.LiResource">
    select lr.* from li_resource lr
    left join li_enshrine le on lr.id = le.resource_id
    where lr.deleted = 0 and le.deleted = 0 and le.byx1 is null
    <if test="subCode!=null and subCode!=''">
      and lr.sub_code = #{subCode}
    </if>
    <if test="stationId!=null and stationId!=''">
      and lr.station_id = #{stationId}
    </if>
    <if test="type!=null and type!=''">
      and lr.type = #{type}
    </if>
    <if test="name!=null and name!=''">
      and INSTR(lr.name, #{name})
    </if>
    <if test="createUserId!=null and createUserId!=''">
      and le.create_user_id = #{createUserId}
    </if>
    order by lr.create_time desc
  </select>

  <select id="queryByDefenceId" resultType="com.devplatform.equipment.modules.liresource.bean.LiResource">
        -- 		select lr.id,lr.name,lr.code,sd.name as type,lr.line_id,lr.line_name,lr.station_id,lr.station_name,
-- 			lr.tier_id,lr.sub_system,lr.sub_code,lr.address_code,lr.port,lr.username,lr.password,lr.xpoint,
-- 			lr.ypoint,lr.deploy_location,lr.operator_id,lr.operator_name,lr.attached_info,lr.remark,lr.icon,
-- 			lr.parent_name,lr.sys_sign,lr.status,lr.deleted,lr.create_user_id,lr.create_time,
-- 			lr.update_user_id,lr.update_time,lr.byx1,lr.byx2,lr.byx3
        SELECT lr.*
        FROM invade_defence_resource idr
                 LEFT JOIN li_resource lr ON idr.resource_id = lr.id
-- 		left join sys_dictionary sd on lr.type = sd.id
        WHERE idr.defence_id = #{id} and lr.deleted = 0
        ORDER BY lr.create_time DESC
    </select>
  <select id="queryByParent" resultType="com.devplatform.equipment.modules.liresource.bean.LiResource">
    select
    <include refid="Base_Column_List"></include>
    from li_resource
    where parent_name = #{id} and deleted=0
  </select>
  <select id="getAllEq" resultType="com.devplatform.equipment.modules.liresource.bean.LiResource">
        SELECT *
        FROM li_resource lr
                 RIGHT JOIN li_station_map lsm ON lsm.id = lr.tier_id AND lsm.deleted = 0
        WHERE lr.station_id = #{stationId}
          AND sub_code = #{subCode}
          AND lr.deleted = 0
    </select>

  <!--根据资源id获取对应图层排序值-->
  <select id="getOrderNumByResId" parameterType="java.lang.String" resultType="java.lang.String">
        SELECT
            map.order_num
        FROM
            li_resource li
            LEFT JOIN li_station_map map ON li.tier_id = map.id
        WHERE
            li.id = #{id}  and li.deleted = 0 and map.deleted = 0
    </select>
  <select id="queryById" resultType="com.devplatform.equipment.modules.liresource.bean.LiResource">
        SELECT *
        FROM li_resource
        WHERE id = #{id} and deleted = 0
    </select>
</mapper>


