<?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.huatek.busi.mapper.ArresterAssessmentResultMapper">
  <resultMap id="BaseResultMap" type="com.huatek.busi.vo.ArresterAssessmentResultVo">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="site_id" jdbcType="VARCHAR" property="siteId" />
    <result column="main_tree_id" jdbcType="VARCHAR" property="mainTreeId" />
    <result column="assess_time" jdbcType="TIMESTAMP" property="assessTime" />
    <result column="safety_factor" jdbcType="VARCHAR" property="safetyFactor" />
    <result column="health_level" jdbcType="VARCHAR" property="healthLevel" />
    <result column="operation_time" jdbcType="VARCHAR" property="operationTime" />
    <result column="future_failure_rate" jdbcType="VARCHAR" property="futureFailureRate" />
    <result column="type" jdbcType="VARCHAR" property="type" />
  </resultMap>
  <sql id="Base_Column_List">
    id, site_id, main_tree_id, assess_time, safety_factor, health_level, operation_time,
      future_failure_rate, type
  </sql>

    <sql id="Base_Column_List2">
    id AS id,
	site_id AS siteId,
	main_equipment_id AS mainEquipmentId,
	weight_level AS weightLevel,
	`name`,
	pname,
	description,
	value,
	ab_value_flag AS abValueFlag,
	a_value AS aValue,
	b_value AS bValue,
	warning_value AS warningValue,
	model,
	is_attend AS isAttend,
	create_time AS createTime,
	create_user AS createUser,
	sort_num AS sortNum,
	calculated_value AS calculatedValue,
	scope,
	number
  </sql>

  <select id="findArresterAssessmentByMainTreeId" parameterType="java.lang.String" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from arrester_assessment_result t
    <where>
    <if test="siteId != null">
      and t.site_id = #{siteId}
    </if>
    <if test="mainTreeId != null">
      and t.main_tree_id = #{mainTreeId}
    </if>
    <if test="startTime != null">
      and t.assess_time <![CDATA[>=]]> #{startTime}
    </if>
    <if test="endTime != null">
      and t.assess_time <![CDATA[<=]]> #{endTime}
    </if>
    <if test="type != null and type != '' ">
      and t.type = #{type}
    </if>
  </where>
    ORDER BY t.assess_time DESC
  </select>
  <select id="findCalculateStep" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiGisCaculateStepVo">
    SELECT
        step_json AS steps,
        number,
        site_id AS siteId,
        main_equipment_id AS mainEquipmentId
    FROM
        x_calculate_step
    WHERE
        p_id = #{pid}
  </select>
  <select id="findBusiBlqParam" parameterType="java.lang.String" resultType="com.huatek.busi.vo.BusiParamTransformerVoluntarilyVo">
    SELECT
      <include refid="Base_Column_List2" />
    FROM
        busi_param_arrester
    WHERE
        site_id = #{siteId}
        AND main_equipment_id = #{mainTreeId}
        AND number = #{number}
  </select>
  <select id="findEqType" parameterType="java.lang.String" resultType="java.lang.String">
    SELECT
      equipment_type
    FROM
      busi_site_tree
    WHERE
      site_id = #{siteId}
      AND id = #{mainTreeId}
  </select>
  <select id="findParentId" resultType="com.huatek.busi.entity.BusiSiteTree">
    SELECT
        id AS parentId
    FROM
        busi_site_tree
    WHERE
        `status` = 1
    AND parent_id = (
        SELECT
            id
        FROM
            busi_site_tree
        WHERE
        equipment_type = 2
        AND `status` = 1)
  </select>
  <select id="findMainTreeId" parameterType="java.lang.String" resultType="com.huatek.busi.entity.BusiSiteTree">
    SELECT
        id,
        name
    FROM
        busi_site_tree
    WHERE
        `status` = 1
        AND parent_id = #{parentId}
  </select>
    <select id="findAllLoadRate" parameterType="java.lang.String" resultType="com.huatek.busi.entity.TransformerLoadRate">
    SELECT
        A.load_factor AS loadRate,
        A.assess_time AS loadTime,
        B.name AS name
    FROM
        arrester_assessment_result A
        LEFT JOIN busi_site_tree B ON A.main_tree_id = B.id
    WHERE
        A.main_tree_id = #{mainEquipmentId}
        <if test="date != null">
            AND A.assess_time <![CDATA[>=]]> #{date}
        </if>
        <if test="date != null">
            AND A.assess_time <![CDATA[<=]]> DATE_ADD(#{date}, INTERVAL 1 DAY)
        </if>
        ORDER BY
        A.assess_time DESC
        LIMIT 1
  </select>
</mapper>
