<?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.datatransfer.DataTransferMapper">
    <resultMap id="resultMap" type="com.energy.manage.common.po.datatransfer.DataTransferPo">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="batchCode" column="batch_code" jdbcType="VARCHAR"/>
        <result property="fieldCode" column="field_code" jdbcType="VARCHAR"/>
        <result property="engineCount" column="engine_count" jdbcType="VARCHAR"/>
        <result property="transferType" column="transfer_type" jdbcType="VARCHAR"/>
        <result property="transferAddr" column="transfer_addr" jdbcType="VARCHAR"/>
        <result property="transferState" column="transfer_state" jdbcType="INTEGER"/>
        <result property="timeGranularity" column="time_granularity" jdbcType="INTEGER"/>
        <result property="errInfo" column="err_info" jdbcType="VARCHAR"/>
        <result property="transferFinishTime" column="transfer_finish_time" jdbcType="TIMESTAMP"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="create_by" jdbcType="INTEGER"/>
        <result property="updateBy" column="update_by" jdbcType="INTEGER"/>
        <result property="transferProgress" column="transfer_progress" jdbcType="DECIMAL"/>
        <result property="dataMinTime" column="data_min_time" jdbcType="DATE"/>
        <result property="dataMaxTime" column="data_max_time" jdbcType="DATE"/>
    </resultMap>
    <sql id="BASE_SQL">
        id,
        batch_code,
        field_code,
        transfer_addr,
        engine_count,
        transfer_type,
        transfer_state,
        time_granularity,
        err_info,
        transfer_finish_time,
        create_time,
        update_time,
        create_by,
        update_by
    </sql>

    <select id="selectAllWindFieldBtach"  parameterType ="java.lang.Integer" resultType="java.lang.String">
        select distinct transfer.batch_code
        from data_transfer transfer
        left join sys_organization_auth auth
        on transfer.field_code = auth.code_number
        where auth.role_id = #{roleId,jdbcType=INTEGER}
    </select>

    <select id="queryDataTransferGroup" resultType="com.energy.manage.service.domain.vo.datatransfer.DataTransferBatchShortVo">
        select
            transfer.field_code,
            transfer.batch_code,
            field.field_name,
            batch.batch_name,
            max(transfer.update_time) update_time
        from
            data_transfer  transfer
        left join wind_field_batch batch
        on transfer.batch_code = batch.batch_code
        left join wind_field field
        on transfer.field_code = field.field_code
        left join sys_organization_auth auth
        on transfer.field_code = auth.code_number
        where
            auth.role_id = #{roleId,jdbcType=INTEGER}
            <if test="fieldName != null and fieldName != ''">
                and field.field_name like concat('%',#{fieldName},'%')
            </if>
            <if test="batchName != null and batchName != ''">
                and batch.batch_name like concat('%',#{batchName},'%')
            </if>
        group by
            transfer.field_code,
            transfer.batch_code,
            field.field_name,
            batch.batch_name
        order by update_time desc
    </select>

    <select id="selectDataTransferListByCondition" parameterType="com.energy.manage.service.domain.dto.datatransfer.DataTransferDto" resultType="com.energy.manage.service.domain.vo.datatransfer.DataTransferVo">
        select
            batch.batch_name,
            batch.field_name,
            transfer.batch_code,
            transfer.field_code,
            transfer.engine_count,
            transfer.transfer_type,
            transfer.transfer_state,
            transfer.time_granularity,
            transfer.err_info,
            transfer.transfer_addr,
            transfer.transfer_finish_time,
            transfer.transfer_progress
        from data_transfer transfer
        left join sys_organization_auth auth
        on transfer.field_code = auth.code_number
        left join wind_field_batch batch
        on batch.batch_code = transfer.batch_code
        where auth.role_id = #{roleId,jdbcType=INTEGER}
        <if test="batchName != null and batchName != ''">
            and batch.batch_name like concat('%', #{batchName,jdbcType=VARCHAR}, '%')
        </if>
        <if test="batchCode != null and batchCode != ''">
            and transfer.batch_code = #{batchCode,jdbcType=VARCHAR}
        </if>
        <if test="transferState != null">
            and transfer.transfer_state = #{transferState,jdbcType=INTEGER}
        </if>
        order by transfer.update_time desc
    </select>
    <select id="selectDataTransferListByBatchCode" parameterType="java.lang.String" resultType="com.energy.manage.service.domain.vo.datatransfer.DataTransferVo">
        select
            batch.batch_name,
            field.field_name,
            transfer.batch_code,
            transfer.field_code,
            transfer.engine_count,
            transfer.transfer_type,
            type.transfer_type_name,
            transfer.transfer_state,
            transfer.time_granularity,
            transfer.err_info,
            transfer.transfer_addr,
            transfer.transfer_finish_time,
            transfer.transfer_progress
        from data_transfer transfer
        left join data_transfer_type type
        on transfer.transfer_type = type.transfer_type
        left join wind_field_batch batch
        on batch.batch_code = transfer.batch_code
        left join wind_field field
        on field.field_code = transfer.field_code
        where transfer.batch_code = #{batchCode,jdbcType=VARCHAR}
    </select>

    <select id="selectByBatchCodeAndDataTransferType" parameterType="java.lang.String" resultType="com.energy.manage.common.po.datatransfer.DataTransferPo">
        select
            batch_code,
            field_code,
            engine_count,
            transfer_type,
            transfer_state,
            err_info,
            transfer_addr,
            transfer_finish_time,
            update_by
        from data_transfer
        where batch_code = #{batchCode,jdbcType=VARCHAR}
        and transfer_type = #{transferType,jdbcType=VARCHAR}
    </select>

    <select id="selectCountByTransferState" parameterType="java.lang.Integer" resultType="java.lang.Integer">
        select count(*) from data_transfer where transfer_state = #{transferState,jdbcType=INTEGER}
    </select>

    <insert id="addDataTransferBatch" parameterType="com.energy.manage.common.po.datatransfer.DataTransferPo">
        insert into data_transfer(
            batch_code,
            field_code,
            transfer_type,
            transfer_addr,
            transfer_state,
            time_granularity,
            create_by,
            update_by
        )values
        <foreach collection="poList" item="po" separator="," >
          ( #{po.batchCode,jdbcType=VARCHAR},
            #{po.fieldCode,jdbcType=VARCHAR},
            #{po.transferType,jdbcType=VARCHAR},
            #{po.transferAddr,jdbcType=VARCHAR},
            #{po.transferState,jdbcType=INTEGER},
            #{po.timeGranularity,jdbcType=INTEGER},
            #{po.createBy,jdbcType=INTEGER},
            #{po.updateBy,jdbcType=INTEGER}
            )
        </foreach>
    </insert>

    <update id="updateDataTransfer" parameterType="com.energy.manage.common.po.datatransfer.DataTransferPo">
        update data_transfer
        <set>
            <if test="transferState != null">
                transfer_state = #{transferState,jdbcType=INTEGER},
            </if>
            <if test="transferAddr != null and transferAddr != ''">
                transfer_addr = #{transferAddr,jdbcType=VARCHAR},
            </if>
            <if test="timeGranularity != null">
                time_granularity = #{timeGranularity,jdbcType=INTEGER},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy,jdbcType=INTEGER},
            </if>
            <if test="transferProgress != null">
                transfer_progress = #{transferProgress,jdbcType=DECIMAL}
            </if>
        </set>
        where batch_code = #{batchCode,jdbcType=VARCHAR}
        and transfer_type = #{transferType,jdbcType=VARCHAR}
    </update>

    <select id="selectDataTimeByBatchCodeAndType" parameterType="java.lang.String" resultType="com.energy.manage.service.domain.vo.datatransfer.DataTransferDataTimeVo">
        select
            data_min_time,
            data_max_time
        from data_transfer
        where batch_code = #{batchCode,jdbcType=VARCHAR}
        and transfer_type = #{transferType,jdbcType=VARCHAR}
    </select>
</mapper>
