<?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.energy.manage.service.mappers.autoanalysisconfigdetail.AutoAnalysisConfigDetailMapper">
  <resultMap id="BaseResultMap" type="com.energy.manage.common.po.autoanalysisconfigdetail.AutoAnalysisConfigDetailPo">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="field_code" jdbcType="VARCHAR" property="fieldCode" />
    <result column="interval_days" jdbcType="INTEGER" property="intervalDays" />
    <result column="start_time" jdbcType="TIMESTAMP" property="startTime" />
    <result column="startup_state" jdbcType="INTEGER" property="startupState" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="config_unique_id" jdbcType="VARCHAR" property="configUniqueId" />
  </resultMap>
  <sql id="Base_Column_List">
     field_code, interval_days, start_time, startup_state, config_unique_id,create_time, update_time
  </sql>

  <select id="selectByCondition" parameterType="com.energy.manage.service.domain.dto.analysis.AutoAnalysisConfigDetailDto"  resultType="com.energy.manage.service.domain.vo.analysis.AutoAnalysisConfigDetailVo">
    select
    detail.*,
    field.field_name
    from auto_analysis_config_detail detail
    left join wind_field field
    on detail.field_code = field.field_code
    where 1=1
      <if test="fieldCode != null and fieldCode != ''">
        and detail.field_code = #{fieldCode}
      </if>
      <if test="fieldName != null and fieldName != ''">
        and field.field_name like concat ('%', #{fieldName}, '%')
      </if>
      <if test="startupState != null">
        and detail.startup_state = #{startupState}
      </if>
      order by detail.update_time desc
  </select>

  <select id="selectByUniqueId" parameterType="java.lang.String" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from auto_analysis_config_detail
    where config_unique_id = #{configUniqueId}
  </select>
  <select id="selectExistConfig" parameterType="com.energy.manage.service.domain.dto.analysis.AutoAnalysisConfigDetailDto" resultType="int">
    select count(*)
    from auto_analysis_config_detail
    where field_code = #{fieldCode}
    and interval_days = #{intervalDays}
    and start_time = #{startTime}

  </select>

  <insert id="insertConfig" parameterType="com.energy.manage.service.domain.dto.analysis.AutoAnalysisConfigDetailDto">
    insert into auto_analysis_config_detail
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="fieldCode != null and fieldCode != ''">
        field_code,
      </if>
      <if test="intervalDays != null">
        interval_days,
      </if>
      <if test="startTime != null">
        start_time,
      </if>
      <if test="startupState != null">
        startup_state,
      </if>
      <if test="configUniqueId != null">
        config_unique_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="fieldCode != null and fieldCode != ''">
        #{fieldCode,jdbcType=VARCHAR},
      </if>
      <if test="intervalDays != null">
        #{intervalDays,jdbcType=INTEGER},
      </if>
      <if test="startTime != null">
        #{startTime,jdbcType=TIMESTAMP},
      </if>
      <if test="startupState != null">
        #{startupState,jdbcType=BIT},
      </if>
      <if test="configUniqueId != null">
        #{configUniqueId,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>

  <update id="updateConfigByCondition" parameterType="com.energy.manage.service.domain.dto.analysis.AutoAnalysisConfigDetailDto">
    update auto_analysis_config_detail
    <set>
      <if test="intervalDays != null">
        interval_days = #{intervalDays,jdbcType=INTEGER},
      </if>
      <if test="startTime != null">
        start_time = #{startTime,jdbcType=TIMESTAMP},
      </if>
      <if test="startupState != null">
        startup_state = #{startupState,jdbcType=BIT},
      </if>
    </set>
    where config_unique_id = #{configUniqueId,jdbcType=VARCHAR}
  </update>
</mapper>
