<?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.admin.modules.liresource.dao.LiResourceDao">
  <!-- Result Map -->
  <resultMap id="BaseResultMap" type="com.devplatform.admin.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="region" property="region"/>
    <result column="equipment_model" property="equipmentModel"/>
    <result column="max_power" property="maxPower"/>
    <result column="install_date" property="installDate"/>
    <result column="maintenance_date" property="maintenanceDate"/>
    <result column="station_id" property="stationId"/>
    <result column="station_name" property="stationName"/>
    <result column="tier_id" property="tierId"/>
    <result column="address_code" property="addressCode"/>
    <result column="port" property="port"/>
    <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="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"/>
    <result column="mode_types" property="modeTypes"/>
  </resultMap>

  <!-- li_resource table all fields -->
  <sql id="Base_Column_List">
		id,name,code,type,station_id,station_name,tier_id,address_code,port,
		username,password,xpoint,ypoint,width,rotate,deploy_location,operator_id,operator_name,
		attached_info,remark,icon,status,deleted,create_user_id,create_time,update_user_id,update_time,
		byx1,byx2,byx3,brand,xlongit,ylat,image_src,equipment_model,max_power,install_date,maintenance_date,region,mode_types
	</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 INSTR(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="equipmentModel!=null and equipmentModel!=''">and equipment_model = #{equipmentModel}</if>
    <if test="maxPower!=null and maxPower!=''">and max_power = #{maxPower}</if>
    <if test="installDate!=null ">and install_date = #{installDate}</if>
    <if test="maintenanceDate!=null ">and maintenance_date = #{maintenanceDate}</if>
    <if test="region!=null ">and region = #{region}</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="addressCode!=null and addressCode!=''">and address_code = #{addressCode}</if>
    <if test="port!=null and port!=''">and port = #{port}</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="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>
    <if test="modeTypes!=null and modeTypes!=''">and mode_types = #{modeTypes}</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 li.*,sdb.name as brandName,
    (CASE WHEN li.status = 1 THEN '运行'
    WHEN li.status = 2 THEN '停止'
    WHEN li.status = 3 THEN '离线'
    ELSE '故障' END) as statusName
    from li_resource li
    left join sys_dictionary sdb on sdb.id = li.brand
    <include refid="queryPageByListClause2"></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 INSTR(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="equipmentModel!=null and equipmentModel!=''">and equipment_model = #{equipmentModel}</if>
    <if test="maxPower!=null and maxPower!=''">and max_power = #{maxPower}</if>
    <if test="installDate!=null ">and install_date = #{installDate}</if>
    <if test="maintenanceDate!=null ">and maintenance_date = #{maintenanceDate}</if>
    <if test="region!=null ">and region = #{region}</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="addressCode!=null and addressCode!=''">and address_code = #{addressCode}</if>
    <if test="port!=null and port!=''">and port = #{port}</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="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>
    <if test="modeTypes!=null and modeTypes!=''">and mode_types = #{modeTypes}</if>
    <if test="list!=null and list.size()>0">
      and type in
      <foreach close=")" collection="list" index="index" item="item" open="(" separator=",">
        #{item}
      </foreach>
    </if>
  </sql>

  <sql id="queryPageByListClause2">
    where li.deleted=0
    <if test="id!=null and id!=''">and li.id = #{id}</if>
    <if test="name!=null and name!=''">and INSTR(li.name, #{name})</if>
    <if test="code!=null and code!=''">and li.code = #{code}</if>
    <if test="type!=null and type!=''">and li.type = #{type}</if>
    <if test="equipmentModel!=null and equipmentModel!=''">and li.equipment_model = #{equipmentModel}</if>
    <if test="maxPower!=null and maxPower!=''">and li.max_power = #{maxPower}</if>
    <if test="installDate!=null ">and li.install_date = #{installDate}</if>
    <if test="maintenanceDate!=null ">and li.maintenance_date = #{maintenanceDate}</if>
    <if test="region!=null ">and li.region = #{region}</if>
    <if test="stationId!=null and stationId!=''">and li.station_id = #{stationId}</if>
    <if test="stationName!=null and stationName!=''">and li.station_name = #{stationName}</if>
    <if test="tierId!=null and tierId!=''">and li.tier_id = #{tierId}</if>
    <if test="addressCode!=null and addressCode!=''">and li.address_code = #{addressCode}</if>
    <if test="port!=null and port!=''">and li.port = #{port}</if>
    <if test="username!=null and username!=''">and li.username = #{username}</if>
    <if test="password!=null and password!=''">and li.password = #{password}</if>
    <if test="xpoint!=null and xpoint!=''">and li.xpoint = #{xpoint}</if>
    <if test="ypoint!=null and ypoint!=''">and li.ypoint = #{ypoint}</if>
    <if test="deployLocation!=null and deployLocation!=''">and li.deploy_location = #{deployLocation}</if>
    <if test="operatorId!=null and operatorId!=''">and li.operator_id = #{operatorId}</if>
    <if test="operatorName!=null and operatorName!=''">and li.operator_name = #{operatorName}</if>
    <if test="attachedInfo!=null and attachedInfo!=''">and li.attached_info = #{attachedInfo}</if>
    <if test="remark!=null and remark!=''">and li.remark = #{remark}</if>
    <if test="icon!=null and icon!=''">and li.icon = #{icon}</if>
    <if test="status!=null ">and li.status = #{status}</if>
    <if test="deleted!=null ">and li.deleted = #{deleted}</if>
    <if test="createUserId!=null and createUserId!=''">and li.create_user_id = #{createUserId}</if>
    <if test="createTime!=null ">and li.create_time = #{createTime}</if>
    <if test="updateUserId!=null and updateUserId!=''">and li.update_user_id = #{updateUserId}</if>
    <if test="updateTime!=null ">and li.update_time = #{updateTime}</if>
    <if test="byx1!=null and byx1!=''">and li.byx1 = #{byx1}</if>
    <if test="byx2!=null and byx2!=''">and li.byx2 = #{byx2}</if>
    <if test="byx3!=null and byx3!=''">and li.byx3 = #{byx3}</if>
    <if test="brand!=null and brand!=''">and li.brand = #{brand}</if>
    <if test="modeTypes!=null and modeTypes!=''">and li.mode_types = #{modeTypes}</if>
    <if test="list!=null and list.size()>0">
      and li.type in
      <foreach close=")" collection="list" index="index" item="item" open="(" separator=",">
        #{item}
      </foreach>
    </if>
  </sql>

  <select id="getTreeList" parameterType="Object" resultMap="BaseResultMap">
    SELECT
    li.*,
    lis.`name` AS tierName
    FROM
    li_resource li
    LEFT JOIN li_station_map lis ON lis.id = li.tier_id
    <include refid="queryPageByListClause2"></include>
    ORDER BY li.create_time DESC
  </select>

  <select id="getBeanById" parameterType="Object" resultMap="BaseResultMap">
    select
    lr.*,
    lsm.name as tierName,
    sdt.name as resourceName,
    sdb.name as brandName
    from li_resource lr
    left join sys_dictionary sdt on sdt.id = lr.type
    left join sys_dictionary sdb on sdb.id = lr.brand
    left join li_station_map lsm on lsm.id = lr.tier_id
    where lr.id = #{id}
    <if test="stationId!=null and stationId!=''">and lr.station_id = #{stationId}</if>
  </select>

  <select id="queryCountByTierId" resultType="java.lang.Integer">
		select count(1) from li_resource
		where deleted = 0 and tier_id = #{tierId}
	</select>
  <select id="queryByCode" resultType="com.devplatform.admin.modules.liresource.bean.LiResource">
			select lr.id,lr.name,lr.code,sd.name as 'type',lr.address_code,lr.port,lr.username,lr.password,lr.deploy_location,lr.station_name
			sd2.name as brandName
			from li_resource lr
		left join sys_dictionary sd on lr.`type` = sd.id and lr.station_id = sd.station_id
		left join sys_dictionary sd2 on lr.brand = sd2.id and lr.station_id = sd2.station_id
		where lr.deleted = 0 and sd.deleted = 0 and lr.code = #{code}
		limit 0,1
	</select>
    <select id="queryListByParam" resultType="com.devplatform.admin.modules.liresource.bean.LiResource">
      select lr.id,lr.name,lr.type
      from li_resource lr
      where lr.deleted = 0 and FIND_IN_SET(#{modeTypes},lr.mode_types)
    </select>
</mapper>


