package com.persagy.server.services.dict

import cn.sagacloud.server.datacenter.utils.IdUtils
import com.persagy.server.datacenter.models.entities.dict.DictSpaceProject
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.requests.SCreateRequest
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.models.responses.SCreateResponse

/**
 * 项目级空间类型
 *
 * @author 张维新
 */
object DictSpaceProjectService  : SObjectService<DictSpaceProject>(SMybatisDao(DictSpaceProject::class.java)) {

    fun zoneTypeCreate(request: SCreateRequest<DictSpaceProject>): SCreateResponse<DictSpaceProject> {
        try {
            /** 项目id */
            val projectId = SPageContext.getHeader("ProjectId")
            if (request.content!=null&&request.content.size>0){

                for (content in request.content){
                 if (content.parentId.isNullOrEmpty()){
                     val sCreateResponse = SCreateResponse<DictSpaceProject>()
                     sCreateResponse.result = SResponseType.failure
                     sCreateResponse.message = "父节点不能为空"
                     return sCreateResponse
                 }
                    if (!content.parentId.isNullOrEmpty()&&content.parentId!="OtherZone"){
                        val sCreateResponse = SCreateResponse<DictSpaceProject>()
                        sCreateResponse.result = SResponseType.failure
                        sCreateResponse.message = "只能为其它分区添加子分区"
                        return sCreateResponse
                    }
                    if (content.name.isNullOrEmpty()){
                        val sCreateResponse = SCreateResponse<DictSpaceProject>()
                        sCreateResponse.result = SResponseType.failure
                        sCreateResponse.message = "分区名不能为空"
                        return sCreateResponse
                    }
                    if (!content.name.isNullOrEmpty()){
                        val entity = select(SFilter.eq("name", content.name!!)).entity()
                        if (entity!=null){
                            val sCreateResponse = SCreateResponse<DictSpaceProject>()
                            sCreateResponse.result = SResponseType.failure
                            sCreateResponse.message = "分区名不能与已有分区名重复"
                            return sCreateResponse
                        }
                    }
                    if (!content.id.isNullOrEmpty()){
                        val entity = select(SFilter.eq("id", content.id!!)).entity()
                        if (entity!=null){
                            val sCreateResponse = SCreateResponse<DictSpaceProject>()
                            sCreateResponse.result = SResponseType.failure
                            sCreateResponse.message = "id  不能重复"
                            return sCreateResponse
                        }
                    }

                }
                for (content in request.content){
                    if (content.id.isNullOrEmpty()){
                        content.id = IdUtils.uuidCreate()+"Zone"
                    }
                    content.projectId = projectId
                    insert(content)
                }

                val sCreateResponse = SCreateResponse<DictSpaceProject>()
                sCreateResponse.result = SResponseType.success
                return sCreateResponse
            }else{
                val sCreateResponse = SCreateResponse<DictSpaceProject>()
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "创建内容不能为空"
                return sCreateResponse
            }
        } catch (e: Exception) {
            e.printStackTrace()
            return SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Fun


    fun zoneTypeDelete(idList: ArrayList<DictSpaceProject>): SBaseResponse {
        try {
            if (idList!=null&&idList.size>0){
                for (dictSpace in idList){
                    if (dictSpace.id.isNullOrEmpty()){
                        val sCreateResponse = SBaseResponse()
                        sCreateResponse.result = SResponseType.failure
                        sCreateResponse.message = "id不能为空"
                        return sCreateResponse
                    }

                    if (!dictSpace.id.isNullOrEmpty() ){
                        if (dictSpace.id == "AirConditioningZone"||dictSpace.id == "CleanZone"||dictSpace.id == "DomesticWaterSupplyZone"
                                ||dictSpace.id == "FireZone"||dictSpace.id == "FunctionZone"||dictSpace.id == "GeneralZone"
                                ||dictSpace.id == "HeatingZone"||dictSpace.id == "LightingZone"||dictSpace.id =="NetworkZone"
                                ||dictSpace.id == "PowerSupplyZone"||dictSpace.id == "SecurityZone"||dictSpace.id == "TenantZone"||dictSpace.id == "OtherZone"){
                            val sBaseResponse = SBaseResponse(SResponseType.failure)
                            sBaseResponse.message = "不能删除字典定义分区"
                            return sBaseResponse
                        }
                    }
                }
                for (dictSpace in idList){
                    delete(SFilter.eq("id",dictSpace.id!!))
                }
                val sBaseResponse = SBaseResponse(SResponseType.success)
                return sBaseResponse
            }else{
                val sBaseResponse = SBaseResponse(SResponseType.failure)
                sBaseResponse.message = "内容不可以为空"
                return sBaseResponse
            }
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message.toString())
        }
    } //Fun

} // Class DictSpaceProjectService