<?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.analysis.AnalysisResultMapper">
    <resultMap id="BaseResultMap" type="com.energy.manage.common.po.analysis.AnalysisResultPo">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="batch_code" property="batchCode" jdbcType="VARCHAR"/>
        <result column="analysis_state" property="analysisState" jdbcType="INTEGER"/>
        <result column="err_state" property="errState" jdbcType="INTEGER"/>
        <result column="err_code" property="errCode" jdbcType="VARCHAR"/>
        <result column="err_info" property="errInfo" jdbcType="VARCHAR"/>
        <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="analysis_progress" property="analysisProgress" jdbcType="DECIMAL"/>
        <result column="on_off_call" property="onOffCall" jdbcType="INTEGER"/>

        <result column="field_code" property="fieldCode" jdbcType="VARCHAR"/>
        <result column="analysis_name" property="analysisName" jdbcType="VARCHAR"/>
        <result column="data_start_time" property="dataStartTime" jdbcType="DATE"/>
        <result column="data_end_time" property="dataEndTime" jdbcType="DATE"/>
        <result column="sketch" property="sketch" jdbcType="VARCHAR"/>
        <result column="data_type_code" property="dataTypeCode" jdbcType="VARCHAR"/>
        <result column="analysis_start_time" property="analysisStartTime" jdbcType="TIMESTAMP"/>
        <result column="analysis_finish_time" property="analysisFinishTime" jdbcType="TIMESTAMP"/>
        <result column="analysis_flag" property="analysisFlag" jdbcType="INTEGER"/>

    </resultMap>

    <select id="selectByCondition" parameterType="com.energy.manage.service.domain.dto.analysis.AnalysisResultDto" resultType="com.energy.manage.service.domain.vo.analysis.AnalysisResultVo">
        select
            result.*,
            field.field_name
        from analysis_result result
        left join sys_organization_auth auth
        on result.field_code = auth.code_number
        left join wind_field field
        on result.field_code = field.field_code
        where auth.role_id = #{roleId,jdbcType=INTEGER}
        <if test="fieldName != null and fieldName != ''">
            and field.field_name like concat('%', #{fieldName,jdbcType=VARCHAR}, '%')
        </if>
        <if test="analysisName != null and analysisName != ''">
            and result.analysis_name like concat('%', #{analysisName,jdbcType=VARCHAR}, '%')
        </if>
        <if test="analysisState != null">
            and result.analysis_state = #{analysisState}
        </if>
        <if test="errState != null">
            and result.err_state = #{errState}
        </if>
        order by result.update_time desc
    </select>

    <select id="selectByFieldCode" parameterType="java.lang.String" resultType="com.energy.manage.service.domain.vo.analysis.AnalysisResultVo">
        select
        field.field_name,
        result.field_code,
        result.batch_code,
        result.analysis_state,
        result.err_state,
        result.err_info,
        result.create_time,
        result.analysis_progress,
        result.on_off_call,
        result.analysis_name,
        result.data_start_time,
        result.data_end_time,
        result.sketch,
        result.data_type_code,
        result.analysis_start_time,
        result.analysis_finish_time,
        type.transfer_type_name as data_type_name
        from analysis_result result
        left join wind_field field
        on result.field_code = field.field_code
        left join data_transfer_type type
        on result.data_type_code = type.transfer_type
        where result.field_code = #{fieldCode}
        order by result.update_time desc
    </select>

    <select id="selectAlasisingCount" resultType="int">
        select count(*)
        from analysis_result
        where analysis_state in (10,20)
    </select>

    <select id="selectShortInfoByFieldCode" parameterType="java.lang.String" resultType="com.energy.manage.service.domain.vo.analysis.AnalysisInfoVo">
        select batch_code, analysis_name from analysis_result where field_code = #{fieldCode}
    </select>

    <select id="selectByBatchCode" parameterType="java.lang.String" resultMap="BaseResultMap">
        select *
        from analysis_result
        where batch_code = #{batchCode,jdbcType=VARCHAR}
    </select>

    <select id="selectLastBatchResultByFieldCode"  resultType="com.energy.manage.service.domain.vo.analysis.AnalysisResultVo">
        select *
        from  analysis_result
        where id in (
            select max( id )
            from analysis_result
            group by field_code
            having field_code in
            <foreach collection="fieldCodes" item="item" separator="," open="(" close=")">
                #{item,jdbcType=VARCHAR}
            </foreach>
        )
        <if test="startTime != null and endTime != null">
            and analysis_start_time between #{startTime,jdbcType=VARCHAR} and #{endTime,jdbcType=VARCHAR}
        </if>
    </select>

    <select id="selectAnalysisingCount" resultType="int">
        select count(*) from analysis_result where analysis_state = 10 or analysis_state = 20
    </select>

    <select id="selectAnalysisProcessingCount" resultType="int">
        select count(*) from analysis_result where analysis_state = 20
    </select>


    <select id="selectFieldCode" parameterType="com.energy.manage.service.domain.dto.analysis.AnalysisResultDto" resultType="com.energy.manage.common.po.windfield.WindFieldPO">
        select
        result.field_code,
        field.field_name
        from analysis_result result
        left join wind_field field
        on result.field_code = field.field_code
        left join sys_organization_auth auth
        on result.field_code = auth.code_number
        where auth.role_id = #{roleId}
        <if test="analysisName != null and analysisName != ''">
            and result.analysis_name like concat('%',#{analysisName},'%')
        </if>
        <if test="batchCode != null">
            and result.batch_code = #{batchCode};
        </if>
        <if test="fieldName != null and fieldName != ''">
            and field.field_name like concat('%',#{fieldName},'%')
        </if>
        group by result.field_code, field.field_name
        order by result.update_time desc
    </select>

    <update id="updateOnOffCall">
        update analysis_result
        set on_off_call = #{onOffCall,jdbcType=INTEGER}
        where batch_code = #{batchCode,jdbcType=VARCHAR}
    </update>

    <update id="updateAnalysisState" parameterType="java.lang.String">
        update analysis_result
        set analysis_state = 10,
        err_state = 0,
        err_code = '',
        err_info = '',
        analysis_progress = 0.00,
        data_start_time = null,
        data_end_time = null,
        data_type_code = '',
        analysis_start_time = null,
        analysis_finish_time = null
        where (analysis_state = -1 or analysis_state = 30)
        and batch_code = #{batchCode,jdbcType=VARCHAR}
    </update>

    <update id="updateByBatchCode" parameterType="com.energy.manage.service.domain.dto.analysis.AnalysisDto">
        update analysis_result
        set
            err_state = 0,
            err_code = '',
            err_info = '',
            data_start_time = #{beginTime,jdbcType=INTEGER},
            data_end_time = #{endTime,jdbcType=VARCHAR},
            data_type_code = #{scada,jdbcType=VARCHAR},
            analysis_start_time = now(),
            update_time = now(),
            update_by = #{updateBy,jdbcType=INTEGER},
            <if test="autoOrManual != null and autoOrManual == 'manual'">
                analysis_flag = 1
            </if>
            <if test="autoOrManual != null and autoOrManual == 'automatic'">
                analysis_flag = 0
            </if>
        where batch_code = #{dataBatchNum,jdbcType=VARCHAR}
    </update>
</mapper>
