<?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.windenginegroup.WindEngineGroupMapper">

    <select id="selectWindEngineGroupByNames" resultType="com.energy.manage.service.domain.vo.windenginegroup.WindEngineGroupPageVo">
        SELECT
        wf.field_code as fieldCode,
        wf.field_name as fieldName,
        weg.engine_code AS engineCode,
        weg.engine_name as engineName,
        weg.rated_capacity as ratedCapacity,
        weg.elevation_height as elevationHeight,
        weg.hub_height as hubHeight,
        weg.longitude,
        weg.latitude,
        weg.sightcing,
        weg.state,
        wem.machine_type_code as machineTypeCode,
        wem.manufacturer_name as manufacturerName,
        weg.create_time as createTime
        from
        wind_field wf
        inner join
        wind_engine_group weg on wf.field_code = weg.field_code
        left JOIN
        wind_engine_mill wem on weg.mill_type_code = wem.mill_type_code and wem.del_state = 0
        where 1=1 and weg.del_state = 0
        <if test="item.engineName!=null">
            and
            weg.engine_name like CONCAT("%", #{item.engineName} ,"%")
        </if>
        <if test="item.fieldName!=null">
            AND
            wf.field_name = #{item.fieldName}
        </if>
        <if test="item.fieldCode!=null">
            AND
            wf.field_code = #{item.fieldCode}
        </if>
        <if test="item.beginTime != null and item.beginTime != ''">
            and weg.create_time  <![CDATA[>= ]]>  #{item.beginTime}
        </if>
        <if test="item.endTime != null and item.endTime != ''">
            and weg.create_time <![CDATA[<= ]]> #{item.endTime}
        </if>
        order by weg.id
    </select>


    <select id="selectWindEngineGroupListByFieldCode" resultType="com.energy.manage.service.domain.vo.windenginegroup.WindEngineGroupPageVo">
        SELECT
        wf.field_code as fieldCode,
        wf.field_name as fieldName,
        weg.engine_code AS engineCode,
        weg.engine_name as engineName,
        weg.rated_capacity as ratedCapacity,
        weg.elevation_height as elevationHeight,
        weg.hub_height as hubHeight,
        weg.longitude,
        weg.latitude,
        weg.sightcing,
        weg.state,
        wem.machine_type_code as machineTypeCode,
        wem.manufacturer_name as manufacturerName,
        weg.create_time as createTime
        from
        wind_field wf
        inner join
        wind_engine_group weg on wf.field_code = weg.field_code
        left JOIN
        wind_engine_mill wem on weg.mill_type_code = wem.mill_type_code and wem.del_state = 0
        where 1=1 and weg.del_state = 0
        <if test="fieldCode!=null">
            and  wf.field_code = #{fieldCode}
        </if>
        order by weg.id
    </select>


    <select id="selectWindEngineGroupRatedListByFieldCode" resultType="com.energy.manage.service.domain.vo.windenginegroup.WindEngineGroupPageVo">
        SELECT
        wf.field_code as fieldCode,
        wf.field_name as fieldName,
        weg.engine_code AS engineCode,
        weg.engine_name as engineName,
        weg.rated_capacity as ratedCapacity,
        weg.elevation_height as elevationHeight,
        weg.hub_height as hubHeight,
        weg.longitude,
        weg.latitude,
        weg.sightcing,
        weg.state,
        wem.machine_type_code as machineTypeCode,
        wem.manufacturer_name as manufacturerName,
        wem.rated_wind_speed as ratedWindSpeed,
        wem.rated_cut_in_windspeed as ratedCutInWindspeed,
        wem.rated_cut_out_windspeed as ratedCutOutWindspeed,
        weg.create_time as createTime
        from
        wind_field wf
        inner join
        wind_engine_group weg on wf.field_code = weg.field_code
        left JOIN
        wind_engine_mill wem on weg.mill_type_code = wem.mill_type_code and wem.del_state = 0
        where 1=1 and weg.del_state = 0
        <if test="fieldCode!=null">
            and  wf.field_code = #{fieldCode}
        </if>
        order by weg.id
    </select>



    <select id="selectWindEngineGroupByRoleId" parameterType="java.lang.Integer" resultType="com.energy.manage.service.domain.vo.windenginegroup.WindEngineGroupVo">
        select
            engine.field_code as fieldCode,
            engine.field_name as fieldName,
            engine.engine_code AS engineCode,
            engine.engine_name as engineName
        from wind_engine_group engine
        left join sys_organization_auth auth
        on engine.field_code = auth.code_number
        where auth.role_id = #{roleId}
    </select>

    <select id="selectWindEngineGroupByBatchCode" parameterType="java.lang.String" resultType="com.energy.manage.service.domain.vo.windenginegroup.WindEngineGroupVo">
        select
            engine.engine_code as engineCode,
            engine.engine_name as engineName
        from wind_engine_group engine
        left join analysis_result result
        on engine.field_code = result.field_code
        where result.batch_code = #{batchCode}
    </select>

    <select id="selectMaxRatedCapacity" parameterType="java.lang.String" resultType="java.math.BigDecimal">
        select max(rated_capacity)
        from wind_engine_group
        where 1 = 1
        <if test="engineCodes != null and engineCodes.size() > 0">
            and engine_code in
            <foreach collection="engineCodes" item="engineCode" open="(" separator="," close=")">
                #{engineCode}
            </foreach>
        </if>
        <if test="fieldCode != null and fieldCode != ''">
            and field_code = #{fieldCode}
        </if>
    </select>



    <update id="updateWindEngineGroupList" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            UPDATE
            wind_engine_group
            <set>
            <if test="item.engineName!=null">
                engine_name = #{item.engineName},
            </if>
            <if test="item.ratedCapacity!=null">
                rated_capacity = #{item.ratedCapacity},
            </if>
            <if test="item.elevationHeight!=null">
                elevation_height = #{item.elevationHeight},
            </if>
            <if test="item.hubHeight!=null">
                hub_height = #{item.hubHeight},
            </if>
            <if test="item.longitude!=null">
                longitude = #{item.longitude},
            </if>
            <if test="item.latitude!=null">
                latitude = #{item.latitude},
            </if>
            update_time = now()
            </set>
            where
            engine_code = #{item.engineCode}
        </foreach>
    </update>


</mapper>
