<?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.system.SysOrganizationAuthMapper">
    <resultMap id="resultMap" type="com.energy.manage.common.po.system.SysOrganizationAuthPO">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="roleId" column="role_id" jdbcType="VARCHAR"/>
        <result property="codeNumber" column="code_number" jdbcType="VARCHAR"/>
        <result property="codeType" column="code_type" jdbcType="VARCHAR"/>
        <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"/>
    </resultMap>
    <sql id="Base_Sql">
        id,role_id,code_number,code_type,create_by,create_time,update_by,update_time
    </sql>
    <select id="selectByRoleId" parameterType="java.lang.Integer" resultType="com.energy.manage.service.domain.vo.system.SysOrganizationAuthVo">
        select
            auth.code_number,
            auth.code_type,
            auth.create_by,
            auth.create_time,
            field.field_name
        from sys_organization_auth auth
        left join wind_field field
        on auth.code_number = field.field_code
        where auth.role_id = #{roleId}
    </select>

    <select id="selectSysOrganizationAuthTreeByRoleId" resultType="com.energy.manage.service.domain.vo.cockpit.CompanyFieldTreeRoleVo">
        select
        soa.role_id as roleId,
        soa.code_number as codeNumber,
        soa.parent_code as parentCode,
        soa.code_type as codeType,
        wc.company_name as companyName,
        wf.field_name as fieldName,
        wf.longitude as longitude,
        wf.latitude as latitude,
        wf.density,
        wf.rated_capacity_number as ratedCapacityNumber,
        wf.elevation_height as elevationHeight,
        wfbr.batch_code as batchCode,
        wfbr.analysis_state as analysisState,
        wfbr.create_time as wfbrCreateTime
        from
        (select a.role_id,
        wr.code_number,
        wr.parent_code,
        wr.type as code_type
        from sys_organization_auth a
        inner join wind_relation wr on a.code_number = wr.code_number where 1=1 and a.role_id =#{roleId}) soa
        left join
        wind_company wc on soa.code_number = wc.company_code
        left join
        wind_field wf on soa.code_number = wf.field_code
        left join
        (select
             id,
             batch_code,
             field_code,
             create_time,
             analysis_state
         from
             analysis_result
         where id in
         (select max(id) from analysis_result group by field_code)) wfbr on wf.field_code = wfbr.field_code
        where 1=1
        </select>

    <select id="selectInfoByRoleId" resultType="com.energy.manage.service.domain.vo.system.SysOrganizationAuthVo">
        select
               auth.code_number,
               auth.code_type,
               auth.role_id,
               wc.company_name,
               wf.field_name,
               wf.company_code
        from sys_organization_auth auth
        left join wind_company wc
        on auth.code_number = wc.company_code
        left join wind_field wf
        on auth.code_number = wf.field_code
        where auth.role_id = #{roleId}
    </select>

    <select id="selectSysOrganizationAuthListByRoleId" resultType="com.energy.manage.service.domain.vo.cockpit.CompanyFieldTreeRoleVo">
        select
        soa.role_id as roleId,
        soa.code_number as codeNumber,
        soa.parent_code as parentCode,
        soa.code_type as codeType,
        wc.company_name as companyName,
        wf.field_name as fieldName,
        wf.longitude as longitude,
        wf.latitude as latitude,
        wf.density,
        wf.rated_capacity_number as ratedCapacityNumber,
        wf.elevation_height as elevationHeight,
        wfbr.batch_code as batchCode,
        wfbr.analysis_state as analysisState
        from
        (select a.role_id,
        wr.code_number,
        wr.parent_code,
        wr.type as code_type
        from sys_organization_auth a
        inner join wind_relation wr on a.code_number = wr.code_number where 1=1 and a.role_id =#{roleId} and wr.type = 'field') soa
        left join
        wind_company wc on soa.code_number = wc.company_code
        left join
        wind_field wf on soa.code_number = wf.field_code
        left join
        (select
            id,
            batch_code,
            field_code,
            create_time,
            analysis_state
         from
            analysis_result
         where create_time in
         (select max(create_time) from analysis_result group by field_code)) wfbr on wf.field_code = wfbr.field_code
        where 1=1
        </select>

        <select id="selectFieldAndGrooupNumberByRoleId" resultType="com.energy.manage.service.domain.vo.cockpit.CompanyFieldGroupNumberVo">
            select
            soa.code_number as fieldCode,
            weg.groupCount as engineGroupNumber,
            wfba.analysis_state as analysisState
            from
            sys_organization_auth soa
            inner join
            (select
            field_code,
            count(1) as groupCount
            from
            wind_engine_group where del_state = 0 group by field_code) weg on soa.code_number = weg.field_code
            inner join
            (select
                id,
                batch_code,
                field_code,
                create_time,
                analysis_state
            from
                analysis_result
            where create_time in
            (select
            max(create_time)
            from analysis_result
            group by field_code)) wfba on soa.code_number = wfba.field_code
            where 1=1
            and soa.code_type = 'field'
            and soa.role_id = #{roleId}
        </select>


        <!-- 首页待办任务 -->
        <select id="selectFieldAnalysisResultByroleId" resultType="com.energy.manage.service.domain.vo.homepage.WaitTaskVo">
            select
            sum(uncorrelatedCount) as uncorrelatedCount,
            sum(errCount) as errCount
            from
            (select
            soa.role_id as roleId,
            soa.code_number as codeNumber,
            soa.parent_code as parentCode,
            soa.code_type as codeType,
            wf.field_name as fieldName,
            uar.uncorrelatedCount as uncorrelatedCount,
            uer.errCount as errCount
            from
            (select a.role_id,
            wr.code_number,
            wr.parent_code,
            wr.type as code_type
            from sys_organization_auth a
            inner join wind_relation wr on a.code_number = wr.code_number where 1=1 and a.role_id = #{roleId}  and wr.type = 'field') soa
            left join
            wind_field wf on soa.code_number = wf.field_code
            left join
            (select field_code as fieldCode,count(1) as uncorrelatedCount from analysis_result where analysis_state = -1  group by field_code) uar
            on soa.code_number = uar.fieldCode
            left join
            (select field_code as fieldCode,count(1) as errCount from analysis_result where 	err_state = 1  group by field_code) uer
            on soa.code_number = uer.fieldCode) wait_task
        </select>



        <!-- 分析未关联任务列表 -->
        <select id="selectWindRelationByAnalysisState" resultType="com.energy.manage.common.po.analysis.AnalysisResultPo">
            select * from analysis_result where field_code in (select
            wr.code_number
            from sys_organization_auth a
            inner join wind_relation wr on a.code_number = wr.code_number where 1=1 and a.role_id =  #{roleId} and wr.type = 'field')
            and analysis_state = -1
        </select>

        <!-- 分析异常数列表 -->
        <select id="selectWindRelationByErrState" resultType="com.energy.manage.common.po.analysis.AnalysisResultPo">
            select * from analysis_result where field_code in (select
            wr.code_number
            from sys_organization_auth a
            inner join wind_relation wr on a.code_number = wr.code_number where 1=1 and a.role_id =  #{roleId} and wr.type = 'field')
        and err_state = 1
        </select>

</mapper>
