/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .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.syn.services.dict

import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.dictnew.DefClass
import com.persagy.server.datacenter.models.entities.dictnew.DefFuncId
import com.persagy.server.datacenter.models.entities.dictnew.DefFuncIdScheme
import com.persagy.server.datacenter.models.entities.dictnew.Group
import com.persagy.server.syn.utils.ParamTools
import com.persagy.service.SBaseService
import com.persagy.service.SObjectService
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SCreateResponse

/**
 * 设置方案服务
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/10/15 17:10
 */
object DictSchemeService : SObjectService<DefFuncIdScheme>(SMybatisDao(DefFuncIdScheme::class.java)) {
    /** 集团服务 */
    private val groupService = SBaseService(SMybatisDao(Group::class.java))
    /** 详细信息点 */
    private val defFuncIdService = SBaseService(SMybatisDao(DefFuncId::class.java))
    /** 类型服务 */
    private val defClassService = SObjectService(SMybatisDao(DefClass::class.java))

    /**
     * 预支方案1
     */
    fun setSchemeOne(): SCreateResponse<DefFuncIdScheme> {
        return try {
            /** 读取集团 */
            val groupList = groupService.selectAll().exec()
            for (group in groupList) {
                val defClassList =
                    defClassService.select(SFilter.eq("type", "group"), SFilter.eq("groupCode", group.code!!)).exec()
                for (defClass in defClassList) {
                    val defFuncIdBuilder = select(
                        SFilter.eq("groupCode", group.code!!),
                        SFilter.eq("classCode", defClass.code!!)
                    )
                    val defFuncIdList = defFuncIdBuilder.table("dict.def_funcid").exec()

                    for (defFuncId in defFuncIdList){

                        if (defFuncId.firstName.isNullOrEmpty() || defFuncId.firstName == "基本信息" || defFuncId.firstName == "技术参数" ||
                            defFuncId.firstName == "运行参数" || defFuncId.firstName == "报警消息") {
                            defFuncId.visible = true
                        } else if (defFuncId.secondName == "设备厂家" || defFuncId.secondName == "供应购买" || defFuncId.secondName == "现场安装"
                            || defFuncId.secondName == "运行维保" || defFuncId.secondName == "照片文档") {
                            defFuncId.visible = true
                        } else if (defFuncId.priority == "S") {
                            defFuncId.visible = true
                        } else {
                            defFuncId.visible = false
                        }
                        val dicConfigureEditable = ParamTools.dicConfigureEditable()
                        val containsKey = dicConfigureEditable.containsKey(defFuncId.code!!)
                        if (containsKey){
                            defFuncId.editable = false
                            defFuncId.unDisabled = false
                            defFuncId.visible = false
                        }

                        val dicConfigure = ParamTools.dicConfigure()
                        if (defFuncId.code!=null){
                            val containsKey = dicConfigure.containsKey(defFuncId.code!!)
                            if (containsKey){
                                defFuncId.editable = true
                                defFuncId.unDisabled = false
                                defFuncId.visible = true
                            }
                        }

                        defFuncId.schemeId = 1
                        replace(defFuncId)
                    }
                }
            }
            SCreateResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message!!)
        }

    } // Fun setSchemeOne

    /**
     * 万达方案
     */
    fun wanDaScheme(): SCreateResponse<DefFuncIdScheme>{
        val defDictSchemeList = select(SFilter.eq("schemeId", 1)).exec()
        for (defDictScheme in defDictSchemeList){
            defDictScheme.schemeId = 2

            if (defDictScheme.classCode == "ACCCCC"){
                if (defDictScheme.name == "设备本地编码"||defDictScheme.name == "设备本地名称"||defDictScheme.name == "品牌"
                    ||defDictScheme.name == "额定功率"||defDictScheme.name == "额定制冷量"||defDictScheme.name == "累计耗电量"
                    ||defDictScheme.name == "冷凝器入口水温"||defDictScheme.name == "冷凝器出口水温"||defDictScheme.name == "耗电功率"
                    ||defDictScheme.name == "蒸发器入口水温"||defDictScheme.name == "蒸发器出口水温"||defDictScheme.name == "运行状态"
                    ||defDictScheme.name == "A相电流"||defDictScheme.name == "B相电流"||defDictScheme.name == "C相电流"
                    ||defDictScheme.name == "电流负载率"||defDictScheme.name == "冷冻水出口温度设定值"||defDictScheme.name == "冷冻水出水温度设定值"){
                    defDictScheme.unDisabled = true
                    defDictScheme.editable = true
                    defDictScheme.visible = true
                }else{
                    defDictScheme.visible = false
                }
            }

            if (defDictScheme.classCode == "ACCCSC"){
                if (defDictScheme.name == "设备本地编码"||defDictScheme.name == "设备本地名称"||defDictScheme.name == "品牌"
                    ||defDictScheme.name == "额定功率"||defDictScheme.name == "额定制冷量"||defDictScheme.name == "累计耗电量"
                    ||defDictScheme.name == "冷凝器入口水温"||defDictScheme.name == "冷凝器出口水温"||defDictScheme.name == "耗电功率"
                    ||defDictScheme.name == "蒸发器入口水温"||defDictScheme.name == "蒸发器出口水温"||defDictScheme.name == "运行状态"
                    ||defDictScheme.name == "A相电流"||defDictScheme.name == "B相电流"||defDictScheme.name == "C相电流"
                    ||defDictScheme.name == "电流负载率"||defDictScheme.name == "冷冻水出口温度设定值"||defDictScheme.name == "冷冻水出水温度设定值"){
                    defDictScheme.unDisabled = true
                    defDictScheme.editable = true
                    defDictScheme.visible = true
                }else{
                    defDictScheme.visible = false
                }
            }

            if (defDictScheme.classCode == "ACCCCP"){
                if (defDictScheme.name == "设备本地编码"||defDictScheme.name == "设备本地名称"||defDictScheme.name == "品牌"
                    ||defDictScheme.name == "额定功率"||defDictScheme.name == "额定流量"||defDictScheme.name == "是否有变频功能"
                    ||defDictScheme.name == "额定扬程"||defDictScheme.name == "累计耗电量"||defDictScheme.name == "耗电功率"
                    ||defDictScheme.name == "运行频率"){
                    defDictScheme.unDisabled = true
                    defDictScheme.editable = true
                    defDictScheme.visible = true
                }else{
                    defDictScheme.visible = false
                }
            }

            if (defDictScheme.classCode == "ACCCOP"){
                if (defDictScheme.name == "设备本地编码" || defDictScheme.name == "设备本地名称" || defDictScheme.name == "品牌"
                    || defDictScheme.name == "额定功率" || defDictScheme.name == "额定流量" || defDictScheme.name == "是否有变频功能"
                    || defDictScheme.name == "额定扬程" || defDictScheme.name == "累计耗电量" || defDictScheme.name == "耗电功率"
                    || defDictScheme.name == "运行频率") {
                    defDictScheme.unDisabled = true
                    defDictScheme.editable = true
                    defDictScheme.visible = true
                } else {
                    defDictScheme.visible = false
                }
            }

            if (defDictScheme.classCode == "ACCCOT"){
                if (defDictScheme.name == "设备本地编码" || defDictScheme.name == "设备本地名称" || defDictScheme.name == "品牌"
                    || defDictScheme.name == "风机类型" || defDictScheme.name == "单台风机额定功率" || defDictScheme.name == "累计耗电量"
                    || defDictScheme.name == "耗电功率" || defDictScheme.name == "风机运行频率") {
                    defDictScheme.unDisabled = true
                    defDictScheme.editable = true
                    defDictScheme.visible = true
                } else {
                    defDictScheme.visible = false
                }
            }

            if (defDictScheme.classCode == "ACCC"){
                if (defDictScheme.name == "系统本地编码" || defDictScheme.name == "系统本地名称" || defDictScheme.name == "冷冻水供水干管温度"
                    || defDictScheme.name == "冷冻水总流量" || defDictScheme.name == "冷却水供水干管温度" || defDictScheme.name == "冷却水回水干管温度"
                    || defDictScheme.name == "冷却水总流量" || defDictScheme.name == "冷冻水回水干管温度"|| defDictScheme.name == "逐时供冷量"
                    || defDictScheme.name == "累计供冷量") {
                    defDictScheme.unDisabled = true
                    defDictScheme.editable = true
                    defDictScheme.visible = true
                } else {
                    defDictScheme.visible = false
                }
            }

            if (defDictScheme.classCode == "AirConditioningZone"){
                if (defDictScheme.name == "空间本地编码" || defDictScheme.name == "空间本地名称" || defDictScheme.name == "空间功能区类型"
                    || defDictScheme.name == "空气相对湿度" || defDictScheme.name == "空气（干球）温度" ) {
                    defDictScheme.unDisabled = true
                    defDictScheme.editable = true
                    defDictScheme.visible = true
                } else {
                    defDictScheme.visible = false
                }
            }
            replace(defDictScheme)
        }
       return SCreateResponse<DefFuncIdScheme>( SResponseType.success)
    } // Fun wanDaScheme()





} // Object DictSchemeService