<!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.windfieldbatch.WindFieldBatchMapper">
    <resultMap id="resultMap" type="com.energy.manage.common.po.windfieldbatch.WindFieldBatchPo">
        <id property="id" column="id" jdbcType="INTEGER"/>
        <result property="batchCode" column="batch_code" jdbcType="VARCHAR"/>
        <result property="batchName" column="batch_name" jdbcType="VARCHAR"/>
        <result property="fieldCode" column="field_code" jdbcType="VARCHAR"/>
        <result property="fieldName" column="field_name" jdbcType="VARCHAR"/>
        <result property="batchState" column="batch_state" jdbcType="INTEGER"/>
        <result property="isDelete" column="is_delete" jdbcType="INTEGER"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <insert id="addWindFieldBatch" parameterType="com.energy.manage.service.domain.dto.windfieldbatch.WindFieldBatchDto">
        insert into wind_field_batch
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="batchCode != null">
                batch_code,
            </if>
            <if test="batchName != null">
                batch_name,
            </if>
            <if test="fieldCode != null">
                field_code,
            </if>
            <if test="fieldName != null">
                field_name,
            </if>
            <if test="createBy != null">
                create_by,
            </if>
            <if test="updateBy != null">
                update_by
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="batchCode != null">
                #{batchCode,jdbcType=VARCHAR},
            </if>
            <if test="batchName != null">
                #{batchName,jdbcType=VARCHAR},
            </if>
            <if test="fieldCode != null">
                #{fieldCode,jdbcType=VARCHAR},
            </if>
            <if test="fieldName != null">
                #{fieldName,jdbcType=VARCHAR},
            </if>
            <if test="createBy != null">
                #{createBy,jdbcType=VARCHAR},
            </if>
            <if test="updateBy != null">
                #{updateBy,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>
    <update id="updateWindFieldBatch" parameterType="com.energy.manage.service.domain.dto.windfieldbatch.WindFieldBatchDto">
        update wind_field_batch
        <set>
            <if test="batchName != null">
                batch_name = #{batchName,jdbcType=VARCHAR},
            </if>
            <if test="fieldCode != null">
                field_code = #{fieldCode,jdbcType=VARCHAR},
            </if>
            <if test="fieldName != null">
                field_name = #{fieldName,jdbcType=VARCHAR},
            </if>
            <if test="batchState != null">
                batch_state = #{batchState,jdbcType=VARCHAR},
            </if>
            <if test="isDelete != null">
                is_delete = #{isDelete,jdbcType=INTEGER},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy,jdbcType=VARCHAR}
            </if>
        </set>
        where batch_code = #{batchCode,jdbcType=VARCHAR}
    </update>

    <select id="selectWindFieldBatch" parameterType="com.energy.manage.service.domain.dto.windfieldbatch.WindFileBatchPageDto" resultType="com.energy.manage.service.domain.vo.windfieldbatch.WindFieldBatchVo">
        select batch.batch_code,
               batch.batch_name,
               field.field_name,
               batch.field_code,
               batch.batch_state,
               batch.is_delete,
               batch.create_time
        from wind_field_batch batch
        left join wind_field field
        on batch.field_code = field.field_code
        left join sys_organization_auth auth
        on batch.field_code = auth.code_number
        where batch.is_delete = 0
        and auth.role_id = #{roleId}
        <if test="batchName != null">
            and batch.batch_name like concat('%',#{batchName},'%')
        </if>
        <if test="batchCode != null">
            and batch.batch_code = #{batchCode};
        </if>
        <if test="fieldName != null and fieldName != ''">
            and batch.field_name like concat('%',#{fieldName},'%')
        </if>
        <if test="batchState != null">
            and batch.batch_state = #{batchState}
        </if>
        <if test="startTime != null">
            and batch.create_time &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            and batch.create_time &lt;= #{endTime}
        </if>
        order by batch.update_time desc
    </select>

    <select id="selectByBatchCode" parameterType="java.lang.String" resultType="com.energy.manage.service.domain.vo.windfieldbatch.WindFieldBatchVo">
        select batch.batch_code,
        batch.batch_name,
        batch.field_name,
        batch.field_code,
        batch.batch_state,
        batch.is_delete,
        batch.create_time
        from wind_field_batch batch
        where batch_code = #{batchCode}
    </select>

    <select id="selectByBatchName" parameterType="java.lang.String" resultType="com.energy.manage.service.domain.vo.windfieldbatch.WindFieldBatchVo">
        select batch.batch_code,
               batch.batch_name,
               batch.field_name,
               batch.field_code,
               batch.batch_state,
               batch.is_delete,
               batch.create_time
        from wind_field_batch batch
        where batch_name = #{batchName}
    </select>

</mapper>
