<?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.windexceptioncount.WindExceptionCountMapper">
    <resultMap id="resultMap" type="com.energy.manage.common.po.windexceptioncount.WindExceptionCountPo">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="batch_code" property="batchCode" jdbcType="VARCHAR"/>
        <result column="engine_code" property="engineCode" jdbcType="VARCHAR"/>
        <result column="analysis_type_code" property="analysisTypeCode" jdbcType="VARCHAR"/>
        <result column="err_count" property="errCount" jdbcType="INTEGER"/>
        <result column="handle_err_count" property="handleErrCount" jdbcType="INTEGER"/>
        <result column="create_by" property="createBy" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_by" property="updateBy" jdbcType="INTEGER"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
        <result column="err_desc" property="errDesc" jdbcType="VARCHAR"/>
    </resultMap>

    <select id="selectByCondition" parameterType="com.energy.manage.service.domain.dto.windexceptioncount.WindExceptionCountDto" resultType="com.energy.manage.service.domain.vo.windexceptioncount.WindExceptionCountVo">
        select
            count.id,
            count.batch_code,
            count.engine_code,
            engine.engine_name,
            type.type_name as analysis_type_name,
            count.analysis_type_code,
            count.err_count,
            count.handle_err_count,
            count.create_by,
            count.create_time,
            count.update_by,
            count.update_time,
            count.err_desc
        from
            wind_exception_count count
        left join analysis_type type
        on count.analysis_type_code = type.type_code
        left join wind_engine_group engine
        on engine.engine_code = count.engine_code
        where
            count.is_delete = 0
        and count.batch_code = #{batchCode}
        <if test="engineCode != null and engineCode != ''">
            and count.engine_code = #{engineCode}
        </if>
        <if test="analysisTypeCode != null and analysisTypeCode != ''">
            and count.analysis_type_code = #{analysisTypeCode}
        </if>
        order by count.update_time desc
    </select>

    <select id="selectByfieldCodes" resultType="com.energy.manage.service.domain.vo.windexceptioncount.WindExceptionCountVo">
        select
            count.*,
            engine.engine_name,
            result.field_code,
            wf.field_name
        from
            wind_exception_count count
        left join analysis_result result
        on result.batch_code = count.batch_code
        left join wind_engine_group engine
        on engine.engine_code = count.engine_code
        left join wind_field wf
        on result.field_code = wf.field_code
        where count.is_delete = 0
        and result.batch_code in (
                select distinct
                    batch_code
                from
                    analysis_result
                where
                    field_code in
                    <foreach collection="fieldCodes" open="(" separator="," close=")" item="fieldCode">
                        #{fieldCode,jdbcType=VARCHAR}
                    </foreach>
                )
        <if test="startTime != null and endTime != null">
            and analysis_start_time between #{startTime,jdbcType=VARCHAR} and #{endTime,jdbcType=VARCHAR}
        </if>
    </select>

    <insert id="insertPoSelective" parameterType="com.energy.manage.service.domain.dto.windexceptioncount.WindExceptionCountDto">
        insert into wind_exception_count
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="batchCode != null">
                batch_code,
            </if>
            <if test="engineCode != null">
                engine_code,
            </if>
            <if test="analysisTypeCode != null">
                analysis_type_code,
            </if>
            <if test="errCount != null">
                err_count,
            </if>
            <if test="handleErrCount!= null">
                handle_err_count,
            </if>
            <if test="createBy != null">
                create_by,
            </if>
            <if test="updateBy != null">
                update_by,
            </if>
            <if test="errDesc != null and errDesc != ''">
                err_desc
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="batchCode != null">
                #{batchCode,jdbcType=VARCHAR},
            </if>
            <if test="engineCode != null">
                #{engineCode,jdbcType=VARCHAR},
            </if>
            <if test="analysisTypeCode != null">
                #{analysisTypeCode,jdbcType=VARCHAR},
            </if>
            <if test="errCount != null">
                #{errCount,jdbcType=INTEGER},
            </if>
            <if test="handleErrCount != null">
                #{handleErrCount,jdbcType=INTEGER},
            </if>
            <if test="createBy != null">
                #{createBy,jdbcType=INTEGER},
            </if>
            <if test="updateBy != null">
                #{updateBy,jdbcType=INTEGER},
            </if>
            <if test="errDesc != null and errDesc != ''">
                #{errDesc,jdbcType=VARCHAR}
            </if>
        </trim>
    </insert>

    <update id="updatePoSelecttive" parameterType="com.energy.manage.service.domain.dto.windexceptioncount.WindExceptionCountDto">
        update wind_exception_count
        <set>
            <if test="errCount != null">
                err_count = #{errCount,jdbcType=INTEGER},
            </if>
            <if test="handleErrCount != null">
                handle_err_count = #{handleErrCount,jdbcType=INTEGER},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete,jdbcType=INTEGER},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy,jdbcType=INTEGER},
            </if>
            <if test="errDesc != null and errDesc != ''">
                err_desc = #{errDesc,jdbcType=VARCHAR}
            </if>
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>


    <select id="selectExceptionCountBybatchCode" resultType="com.energy.manage.service.domain.vo.analysis.AnalysisErrorCodeVo">
        select
        batch_code,
        engine_code
        from
        wind_exception_count
        where
        is_delete = 0
        and
        batch_code = #{batchCode}
        and
        err_count > handle_err_count
        group by engine_code
   </select>

    <select id="selectByBatchAndEngine" parameterType="java.lang.String" resultType="com.energy.manage.service.domain.vo.windexceptioncount.WindExceptionDescVo">
        select count.batch_code,
               count.engine_code,
               type.type_name as analysis_type_name,
               count.err_desc
        from wind_exception_count count
        left join analysis_type type
        on count.analysis_type_code = type.type_code
        where count.is_delete = 0
        and count.err_count > count.handle_err_count
        and  count.batch_code = #{batchCode}
        and  count.engine_code = #{engineCode}

    </select>

</mapper>
