<?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.windfield.WindFieldMapper">

    <update id="updateWindFieldEngineNumberByFieldCode">
        update wind_field
        set engine_number = engine_number - 1
        where field_code=#{fieldCode}
    </update>

    <!-- 风场列表 -->
    <select id="selectWindFieldPageByfield" resultType="com.energy.manage.service.domain.vo.windfield.WindFieldPageVo">
        SELECT
        wf.id as id,
        wf.company_code as companyCode,
        wc.company_name as windCompany,
        wf.field_code as fieldCode,
        wf.field_name as fieldName,
        wf.density as density,
        wf.state as state,
        wf.province_name as provinceName,
        wf.city_name as cityName,
        wf.elevation_height as elevationHeight,
        wf.create_time as createTime,
        wf.longitude as longitude,
        wf.latitude as latitude,
        woeg.wegNumber,
        woatr.atrNumber,
        fc.contractNumber
        FROM wind_field wf
        left join (SELECT weg.field_code,count(1) as wegNumber FROM wind_engine_group weg where weg.del_state = 0 group
        by field_code) woeg on wf.field_code =woeg.field_code
        left join (SELECT atr.field_code,count(1) as atrNumber FROM anemometer_tower_relation atr group by
        atr.field_code) woatr on wf.field_code = woatr.field_code
        left join (select field_code,count(1) as contractNumber from wind_field_contract where del_state = 0 group by
        field_code) fc on wf.field_code = fc.field_code
        left join wind_company wc on wf.company_code = wc.company_code
        where 1=1 and wf.del_state = 0
        <if test="item.fieldName!=null">
            and
            wf.field_name like CONCAT("%", #{item.fieldName} ,"%")
        </if>
        <if test="item.companyName!=null">
            and
            wc.company_name like CONCAT("%", #{item.companyName} ,"%")
        </if>
        <if test="item.provinceId!=null">
            and
            wf.province_id = #{item.provinceId}
        </if>
        <if test="item.cityId!=null">
            and
            wf.city_id = #{item.cityId}
        </if>
        <if test="item.beginTime != null and item.beginTime != ''">
            and wf.create_time  <![CDATA[>= ]]>  #{item.beginTime}
        </if>
        <if test="item.endTime != null and item.endTime != ''">
            and wf.create_time <![CDATA[<= ]]> #{item.endTime}
        </if>
        <if test="item.fieldName!=null or item.beginTime != null or item.endTime!=null or item.companyName != null or item.provinceId != null  ">
            and
            wf.field_code in (select code_number from sys_organization_auth where role_id =#{item.roleId} and code_type
            = 'field')
        </if>
        <if test="item.fieldName == null and item.beginTime == null and item.beginTime == null and item.companyName == null and item.provinceId == null ">
            and
            wf.create_by = #{item.userId}
            or
            wf.field_code in (select code_number from sys_organization_auth where role_id =#{item.roleId} and code_type
            = 'field')
        </if>
        order by id desc
    </select>


    <select id="selectWindFieldNamesByUserId"
            resultType="com.energy.manage.service.domain.vo.windfield.WindFieldNameVo">
         SELECT
        wf.field_code as fieldCode,
        wf.field_name as fieldName
        FROM wind_field wf
        where 1=1
        and wf.del_state = 0
        and wf.state = 1
        and wf.create_by = #{userId}
         or wf.field_code in (select code_number from sys_organization_auth where role_id = #{roleId} and code_type = 'field')
    </select>

    <select id="selectByCodeNumber" parameterType="java.lang.String"
            resultType="com.energy.manage.service.domain.vo.windfield.WindFieldVo">
        select
            company.company_name,
            field.company_code as companyCode,
            field.field_code as fieldCode,
            field.field_name as fieldName,
            field.density as density,
            field.state as state,
            field.province_name as provinceName,
            field.city_name as cityName,
            field.elevation_height as elevationHeight,
            field.latitude,
            field.longitude,
            field.rated_capacity_number,
            field.create_time as createTime
        from wind_field field
        left join wind_company company
        on field.company_code = company.company_code
        where field.field_code = #{codeNumber}
    </select>

    <!-- 查询每个风机容量总数 -->
    <select id="selectRatedCapacityByState" resultType="com.energy.manage.service.domain.vo.windenginegroup.WindFieldRatedCapacityVo">
         select field_code as fieldCode,sum(rated_capacity) as ratedCapacityNumber
         from wind_engine_group
          where del_state = 0 and state = 1 group by field_code
     </select>

    <!-- 批量更新语句 -->
    <update id="batchUpdateRatedCapacity" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" separator=";">
            UPDATE wind_field
            <set>
                rated_capacity_number = #{item.ratedCapacityNumber}
            </set>
            WHERE field_code = #{item.fieldCode}
        </foreach>
    </update>

</mapper>
