/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .F88X
 *        X8888Y
 *      .}888888N;
 *        i888888N;        .:!              .I$WI:
 *          R888888I      .'N88~            i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
 *          .R888888I    .;N8888~          .X8'  "8I.!,/8"  !%NY8`"8I8~~8>,88I
 *            +888888N;  .8888888Y                                  "&&8Y.}8,
 *            ./888888N;  .R888888Y        .'}~    .>}'.`+>  i}!    "i'  +/'  .'i~  !11,.:">,  .~]!  .i}i
 *              ~888888%:  .I888888l      .]88~`1/iY88Ii+1'.R$8$8]"888888888>  Y8$  W8E  X8E  W8888'188Il}Y88$*
 *              18888888    E8888881    .]W%8$`R8X'&8%++N8i,8N%N8+l8%`  .}8N:.R$RE%N88N%N$K$R  188,FE$8%~Y88I
 *            .E888888I  .i8888888'      .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
 *            8888888I  .,N888888~        ~88i"8W,!N8*.I88.}888%F,i$88"F88"  888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
 *          i888888N'      I888Y          ]88;/EX*IFKFK88X  K8R  .l8W  88Y  ~88}'88E&%8W.X8N``]88!.$8K  .:W8I
 *        .i888888N;        I8Y          .&8$  .X88!  i881.:%888>I88  ;88]  +88+.';;;;:.Y88X  18N.,88l  .+88/
 *      .:R888888I
 *      .&888888I                                          Copyright (c) 2009-2020.  博锐尚格科技股份有限公司
 *        ~8888'
 *        .!88~                                                                     All rights reserved.
 *
 * *********************************************************************************************************************
 */

package com.persagy.server.services.assistant

import com.persagy.server.datacenter.models.response.EqpeDefferentReqponse
import com.persagy.server.datacenter.models.response.StatisticsCountResponse
import com.persagy.server.mappers.StatisticsCountMapper
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SCountResponse
import com.persagy.service.models.responses.SQueryResponse
// import org.checkerframework.checker.units.qual.A
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashMap


/**
 * 统计
 *
 * @author  邓婷婷
 */
@Service
open class StatisticsService{

    // 日志
    private val logger = LoggerFactory.getLogger(StatisticsService::class.java)


    /** 实例化 mapper 对象 */
    @Autowired
    lateinit var mapper: StatisticsCountMapper

    /**
     * 不能同步到物理世界的关系数据
     */
    fun unSynchronizeRelation(): SCountResponse {
        val projectId = SPageContext.getHeader("projectId").toString()
        val count1 = mapper.sh2bdCount(projectId)
        val count2 = mapper.sh2flCount(projectId)
        val count3 = mapper.sy2bdCount(projectId)
        val count4 = mapper.sy2flCount(projectId)
        val resultCount = count1 + count2 + count3 + count4
        return SCountResponse(resultCount)
    }

    /**
     * 岗位和关联的资产信息点值不一致
     */
    fun eq2peCount(): SCountResponse {
        val projectId = SPageContext.getHeader("projectId").toString()
        val count = mapper.eq2peCount(projectId)
        return SCountResponse(count)
    }

    /**
     * 岗位和关联的资产信息点值不一致数据
     */
    fun eq2peDifferent(): SQueryResponse<ArrayList<EqpeDefferentReqponse>>{
        return try {
            val projectId =  SPageContext.getHeader("projectId").toString()

            var list=ArrayList<EqpeDefferentReqponse>()
            //岗位和关联的资产数据
            var lis=mapper.eq2peDifferent(projectId)
            for (li in lis) {
                var eqpeDefferentReqponse=EqpeDefferentReqponse()
                var property= TreeMap<String,Any>()
                var equipment=TreeMap<String,Any>()
                if (li["name"]!=li["_name"]){
                    if (li["name"]==null){
                        property["资产名称"]=" "
                    }else{
                        property["资产名称"]= li["name"] as Any
                    }
                    if (li["_name"]==null){
                        equipment["岗位名称"]=" "
                    }else{
                        equipment["岗位名称"]= li["_name"] as Any
                    }

                }
                if (li["local_id"]!=li["_local_id"]){
                    if (li["local_id"]==null){
                        property["资产本地编码"]=" "
                    }else{
                        property["资产本地编码"] = li["local_id"] as Any
                    }
                    if (li["_local_id"]==null){
                        equipment["岗位本地编码"]=" "
                    }else{
                        equipment["岗位本地编码"] = li["_local_id"] as Any
                    }
                }
                if (li["local_name"]!=li["_local_name"]){
                    if (li["local_name"]==null){
                        property["资产本地名称"]=" "
                    }else{
                        property["资产本地名称"] = li["local_name"] as Any
                    }
                    if (li["_local_name"]==null){
                        equipment["岗位本地名称"]=" "
                    }else{
                        equipment["岗位本地名称"] = li["_local_name"] as Any
                    }
                }
                if (li["qr_code"]!=li["_qr_code"]){
                    if (li["qr_code"]==null){
                        property["资产二维码图片"]=" "
                    }else{
                        property["资产二维码图片"] = li["qr_code"] as Any
                    }
                    if (li["_qr_code"]==null){
                        equipment["岗位二维码图片"]=" "
                    }else{
                        equipment["岗位二维码图片"] = li["_qr_code"] as Any
                    }
                }
                if (li["cad_id"]!=li["_cad_id"]){
                    if (li["cad_id"]==null){
                        property["设计图纸中编码"]=" "
                    }else{
                        property["设计图纸中编码"] = li["cad_id"] as Any
                    }
                    if (li["_cad_id"]==null){
                        equipment["设计图纸中编码"]=" "
                    }else{
                        equipment["设计图纸中编码"] = li["_cad_id"] as Any
                    }
                }
                if (li["bim_location"]!=li["_bim_location"]){
                    if (li["bim_location"]==null){
                        property["BIM模型中坐标"]=" "
                    }else{
                        property["BIM模型中坐标"] = li["bim_location"] as Any
                    }
                    if (li["_bim_location"]==null){
                        equipment["BIM模型中坐标"]=" "
                    }else{
                        equipment["BIM模型中坐标"] = li["_bim_location"] as Any
                    }
                }
                if (li["bim_id"]!=li["_bim_id"]){
                    if (li["bim_id"]==null){
                        property["BIM模型中编码"]=" "
                    }else{
                        property["BIM模型中编码"] = li["bim_id"] as Any
                    }
                    if (li["_bim_id"]==null){
                        equipment["BIM模型中编码"]=" "
                    }else{
                        equipment["BIM模型中编码"] = li["_bim_id"] as Any
                    }
                }
                if (li["ledger_param"]!=li["_ledger_param"]){
                    if (li["ledger_param"]==null){
                        property["台账参数"]=" "
                    }else{
                        property["台账参数"] = li["ledger_param"] as Any
                    }
                    if (li["_ledger_param"]==null){
                        equipment["台账参数"]=" "
                    }else{
                        equipment["台账参数"] = li["_ledger_param"] as Any
                    }
                }
                if (li["comm_param"]!=li["_comm_param"]){
                    if (li["comm_param"]==null){
                        property["通讯参数"]=" "
                    }else{
                        property["通讯参数"] = li["comm_param"] as Any
                    }
                    if (li["_comm_param"]==null){
                        equipment["通讯参数"]=" "
                    }else {
                        equipment["通讯参数"] = li["_comm_param"] as Any
                    }
                }
                if (li["tech_param"]!=li["_tech_param"]){
                    if (li["tech_param"]==null){
                        property["技术参数"]=" "
                    }else{
                        property["技术参数"] = li["tech_param"] as Any
                    }
                    if (li["_tech_param"]==null){
                        equipment["技术参数"]=" "
                    }else{
                        equipment["技术参数"] = li["_tech_param"] as Any
                    }
                }
                if (li["run_param"]!=li["_run_param"]){
                    if (li["run_param"]==null){
                        property["运行参数"]=" "
                    }else{
                        property["运行参数"] = li["run_param"] as Any
                    }
                    if (li["_run_param"]==null){
                        equipment["运行参数"]=" "
                    }else{
                        equipment["运行参数"] = li["_run_param"] as Any
                    }
                }
                if (li["setting_param"]!=li["_setting_param"]){
                    if (li["setting_param"]==null){
                        property["设定参数"]=" "
                    }else{
                        property["设定参数"] = li["setting_param"] as Any
                    }
                    if (li["_setting_param"]==null){
                        equipment["设定参数"]=" "
                    }else{
                        equipment["设定参数"] = li["_setting_param"] as Any
                    }
                }
                if (li["alram"]!=li["_alram"]){
                    if (li["alram"]==null){
                        property["报警信息"]=" "
                    }else{
                        property["报警信息"] = li["alram"] as Any
                    }
                    if (li["_alram"]==null){
                        equipment["报警信息"]=" "
                    }else{
                        equipment["报警信息"] = li["_alram"] as Any
                    }
                }
                if (li["custom_param"]!=li["_custom_param"]){
                    if (li["custom_param"]==null){
                        property["自定义参数"]=" "
                    }else{
                        property["自定义参数"] = li["custom_param"] as Any
                    }
                    if (li["_custom_param"]==null){
                        equipment["自定义参数"]=" "
                    }else{
                        equipment["自定义参数"] = li["_custom_param"] as Any
                    }
                }
                if (li["dp_manufacturer_id"]!=li["_dp_manufacturer_id"]){
                    if (li["dp_manufacturer_id"]==null){
                        property["品牌型号id"]=" "
                    }else{
                        property["品牌型号id"] = li["dp_manufacturer_id"] as Any
                    }
                    if (li["_dp_manufacturer_id"]==null){
                        equipment["品牌型号id"]=" "
                    }else{
                        equipment["品牌型号id"] = li["_dp_manufacturer_id"] as Any
                    }
                }
                if (li["dp_supplier_id"]!=li["_dp_supplier_id"]){
                    if (li["dp_supplier_id"]==null){
                        property["供应商信息id"]=" "
                    }else{
                        property["供应商信息id"] = li["dp_supplier_id"] as Any
                    }
                    if (li["_dp_supplier_id"]==null){
                        equipment["供应商信息id"]=" "
                    }else{
                        equipment["供应商信息id"] = li["_dp_supplier_id"] as Any
                    }
                }
                if (li["dp_maintainer_id"]!=li["_dp_maintainer_id"]){
                    if (li["dp_maintainer_id"]==null){
                        property["维修商信息id"]=" "
                    }else{
                        property["维修商信息id"] = li["dp_maintainer_id"] as Any
                    }
                    if (li["_dp_maintainer_id"]==null){
                        equipment["维修商信息id"]=" "
                    }else{
                        equipment["维修商信息id"] = li["_dp_maintainer_id"] as Any
                    }
                }
                if (li["dp_insurer_id"]!=li["_dp_insurer_id"]){
                    if (li["dp_insurer_id"]==null){
                        property["保险公司信息id"]=" "
                    }else{
                        property["保险公司信息id"] = li["dp_insurer_id"] as Any
                    }
                    if (li["_dp_insurer_id"]==null){
                        equipment["保险公司信息id"]=" "
                    }else{
                        equipment["保险公司信息id"] = li["_dp_insurer_id"] as Any
                    }
                }
                if (li["temp_sensor"]!=li["_temp_sensor"]){
                    if (li["temp_sensor"]==null){
                        property["温度传感器"]=" "
                    }else{
                        property["温度传感器"] = li["temp_sensor"] as Any
                    }
                    if (li["_temp_sensor"]==null){
                        equipment["温度传感器"]=" "
                    }else{
                        equipment["温度传感器"] = li["_temp_sensor"] as Any
                    }
                }
                if (li["tech_param_general"]!=li["_tech_param_general"]){
                    if (li["tech_param_general"]==null){
                        property["技术参数-通用"]=" "
                    }else{
                        property["技术参数-通用"] = li["tech_param_general"] as Any
                    }
                    if (li["_tech_param_general"]==null){
                        equipment["技术参数-通用"]=" "
                    }else{
                        equipment["技术参数-通用"] = li["_tech_param_general"] as Any
                    }
                }
                if (li["run_param_general"]!=li["_run_param_general"]){
                    if (li["run_param_general"]==null){
                        property["运行参数-通用"]=" "
                    }else{
                        property["运行参数-通用"] = li["run_param_general"] as Any
                    }
                    if (li["_run_param_general"]==null){
                        equipment["运行参数-通用"]=" "
                    }else{
                        equipment["运行参数-通用"] = li["_run_param_general"] as Any
                    }
                }
                if (li["speed_sensor"]!=li["_speed_sensor"]){
                    if (li["speed_sensor"]==null){
                        property["速度传感器"]=" "
                    }else{
                        property["速度传感器"] = li["speed_sensor"] as Any
                    }
                    if (li["_speed_sensor"]==null){
                        equipment["速度传感器"]=" "
                    }else{
                        equipment["速度传感器"] = li["_speed_sensor"] as Any
                    }
                }
                if (li["sound_sensor"]!=li["_sound_sensor"]){
                    if (li["sound_sensor"]==null){
                        property["声音传感器"]=" "
                    }else{
                        property["声音传感器"] = li["sound_sensor"] as Any
                    }
                    if (li["_sound_sensor"]==null){
                        equipment["声音传感器"]=" "
                    }else{
                        equipment["声音传感器"] = li["_sound_sensor"] as Any
                    }
                }
                if (li["humidity_sensor"]!=li["_humidity_sensor"]){
                    if (li["humidity_sensor"]==null){
                        property["湿度传感器"]=" "
                    }else{
                        property["湿度传感器"] = li["humidity_sensor"] as Any
                    }
                    if (li["_humidity_sensor"]==null){
                        equipment["湿度传感器"]=" "
                    }else{
                        equipment["湿度传感器"] = li["_humidity_sensor"] as Any
                    }
                }
                if (li["press_transd"]!=li["_press_transd"]){
                    if (li["press_transd"]==null){
                        property["压力传感器"]=" "
                    }else{
                        property["压力传感器"] = li["press_transd"] as Any
                    }
                    if (li["_press_transd"]==null){
                        equipment["压力传感器"]=" "
                    }else{
                        equipment["压力传感器"] = li["_press_transd"] as Any
                    }
                }
                if (li["heat_sensor"]!=li["_heat_sensor"]){
                    if (li["heat_sensor"]==null){
                        property["热量传感器"]=" "
                    }else{
                        property["热量传感器"] = li["heat_sensor"] as Any
                    }
                    if (li["_heat_sensor"]==null){
                        equipment["热量传感器"]=" "
                    }else{
                        equipment["热量传感器"] = li["_heat_sensor"] as Any
                    }
                }
                if (li["flow_sensor"]!=li["_flow_sensor"]){
                    if (li["flow_sensor"]==null){
                        property["流量传感器"]=" "
                    }else{
                        property["流量传感器"] = li["flow_sensor"] as Any
                    }
                    if (li["_flow_sensor"]==null){
                        equipment["流量传感器"]=" "
                    }else{
                        equipment["流量传感器"] = li["_flow_sensor"] as Any
                    }
                }
                if (li["electricity_sensor"]!=li["_electricity_sensor"]){
                    if (li["electricity_sensor"]==null){
                        property["电度传感器"]=" "
                    }else{
                        property["电度传感器"] = li["electricity_sensor"] as Any
                    }
                    if (li["_electricity_sensor"]==null){
                        equipment["电度传感器"]=" "
                    }else{
                        equipment["电度传感器"] = li["_electricity_sensor"] as Any
                    }
                }
                if (li["displ_sensor"]!=li["_displ_sensor"]){
                    if (li["displ_sensor"]==null){
                        property["位移传感器"]=" "
                    }else{
                        property["位移传感器"] = li["displ_sensor"] as Any
                    }
                    if (li["_displ_sensor"]==null){
                        equipment["位移传感器"]=" "
                    }else{
                        equipment["位移传感器"] = li["_displ_sensor"] as Any
                    }
                }
                if (li["concent_sensor"]!=li["_concent_sensor"]){
                    if (li["concent_sensor"]==null){
                        property["浓度传感器"]=" "
                    }else{
                        property["浓度传感器"] = li["concent_sensor"] as Any
                    }
                    if (li["_concent_sensor"]==null){
                        equipment["浓度传感器"]=" "
                    }else{
                        equipment["浓度传感器"] = li["_concent_sensor"] as Any
                    }
                }
                if (li["image_sensor"]!=li["_image_sensor"]){
                    if (li["image_sensor"]==null){
                        property["图像传感器"]=" "
                    }else{
                        property["图像传感器"] = li["image_sensor"] as Any
                    }
                    if (li["_image_sensor"]==null){
                        equipment["图像传感器"]=" "
                    }else{
                        equipment["图像传感器"] = li["_image_sensor"] as Any
                    }
                }
                if (li["feedback_param"]!=li["_feedback_param"]){
                    if (li["feedback_param"]==null){
                        property["反馈参数"]=" "
                    }else{
                        property["反馈参数"] = li["feedback_param"] as Any
                    }
                    if (li["_feedback_param"]==null){
                        equipment["反馈参数"]=" "
                    }else{
                        equipment["反馈参数"] = li["_feedback_param"] as Any
                    }
                }
                if (li["man_flow_sensor"]!=li["_man_flow_sensor"]){
                    if (li["man_flow_sensor"]==null){
                        property["人流量传感器"]=" "
                    }else{
                        property["人流量传感器"] = li["man_flow_sensor"] as Any
                    }
                    if (li["_man_flow_sensor"]==null){
                        equipment["人流量传感器"]=" "
                    }else{
                        equipment["人流量传感器"] = li["_man_flow_sensor"] as Any
                    }
                }
                if (li["dp_specification_id"]!=li["_dp_specification_id"]){
                    if (li["dp_specification_id"]==null){
                        property["设备型号id"]=" "
                    }else{
                        property["设备型号id"] = li["dp_specification_id"] as Any
                    }
                    if (li["_dp_specification_id"]==null){
                        equipment["设备型号id"]=" "
                    }else{
                        equipment["设备型号id"] = li["_dp_specification_id"] as Any
                    }
                }
                if (li["dp_brand_id"]!=li["_dp_brand_id"]){
                    if (li["dp_brand_id"]==null){
                        property["品牌id"]=" "
                    }else{
                        property["品牌id"] = li["dp_brand_id"] as Any
                    }
                    if (li["_dp_brand_id"]==null){
                        equipment["品牌id"]=" "
                    }else{
                        equipment["品牌id"] = li["_dp_brand_id"] as Any
                    }
                }
                if (li["equip_serial"]!=li["_equip_serial"]) {
                    if (li["equip_serial"]==null){
                        property["设备序号"]=" "
                    }else{
                        property["设备序号"] = li["equip_serial"] as Any
                    }
                    if (li["_equip_serial"]==null){
                        equipment["设备序号"]=" "
                    }else{
                        equipment["设备序号"] = li["_equip_serial"] as Any
                    }
                }
                eqpeDefferentReqponse.property=property
                eqpeDefferentReqponse.equipment=equipment
                list.add(eqpeDefferentReqponse)

            }
            val sQueryResponse = SQueryResponse<ArrayList<EqpeDefferentReqponse>>(SResponseType.success)
            sQueryResponse.content = listOf(list)
            sQueryResponse
        } catch ( e: Exception){
            e.printStackTrace()
            SQueryResponse(SResponseType.failure)
        }
    }

    /**
     * 设备-未关联资产的设备
     */
    fun uneq2pe(): SCountResponse {
        val projectId = SPageContext.getHeader("projectId").toString()
        val count = mapper.uneq2peCount(projectId)
        return SCountResponse(count)
    }

    /**
     * 业务空间-各分区占比
     */
    fun spObjectTypeCount(): ArrayList<StatisticsCountResponse> {
        val projectId = SPageContext.getHeader("projectId").toString()
        val list = mapper.spObjectTypeCount(projectId)
        val map = HashMap<String, Long>()
        for (obj in list) {
            map.put(obj.name!!, obj.count!!)
        }
        val array = arrayListOf("HeatingZone","CleanZone","DomesticWaterSupplyZone","NetworkZone","TenantZone","AirConditioningZone","FunctionZone","FireZone","SecurityZone","GeneralZone","PowerSupplyZone","LightingZone")

        for(type in array){
            if(!map.containsKey(type)){
                val obj = StatisticsCountResponse()
                obj.name = type
                obj.count = 0
                list.add(obj)
            }
        }
        return list
    }

    /**
     * 业务空间-默认分区功能类型占比
     */
    fun generalSpFuncTypeCount(): ArrayList<StatisticsCountResponse> {
        val projectId = SPageContext.getHeader("projectId").toString()
        return mapper.generalSpFuncTypeCount(projectId)
    }

    /**
     * 租户-关联业务空间完成度
     */
    fun tenant2SpCount(): SCountResponse {
        val projectId = SPageContext.getHeader("projectId").toString()
        val count = mapper.tenant2SpCount(projectId)
        return SCountResponse(count)
    }

    /**
     * 竖井-关联业务空间完成度
     */
    fun shaft2SpCount(): SCountResponse {
        val projectId = SPageContext.getHeader("projectId").toString()
        val count = mapper.shaft2SpCount(projectId)
        return SCountResponse(count)
    }

    /**
     * 竖井-按功能类型占比
     */
    fun shaftFuncTypeCount(): ArrayList<StatisticsCountResponse> {
        val projectId = SPageContext.getHeader("projectId").toString()
        return mapper.shaftFuncTypeCount(projectId)
    }
} // Class StatisticsService