package com.persagy.server.services.models


import com.persagy.base.extensions.toJson
import com.persagy.server.datacenter.datacenter.dao.mappers.SchemeMapper
import com.persagy.server.datacenter.models.entities.assistant.ModelKey
import com.persagy.server.datacenter.models.requests.ModelKeyRequest
import com.persagy.service.SPageContext
import com.persagy.service.utils.SSpringContextUtil
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

/**
 * 模型服务
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/10/16 15:44
 */
@Service
object ModelService {

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


    /** 自定义sql */
    private val schemeMapper by lazy {
        SSpringContextUtil.getBean(SchemeMapper::class.java) as SchemeMapper
    }

    /**
     * @param modelKeyRequest   条件
     * @return 返回结果
     */
    fun modelKey(modelKeyRequest: ModelKeyRequest): ModelKey {
        val projectId = SPageContext.getHeader("projectId")
        val modelKey = ModelKey()
        try {
            if (modelKeyRequest.floorId.isNullOrEmpty()) {
                modelKey.result = "failure"
                modelKey.message = "楼层 id 不能为空"
                return modelKey
            }

            if (projectId.isNullOrEmpty() && modelKeyRequest.projectId.isNullOrEmpty()) {
                modelKey.result = "failure"
                modelKey.message = "项目 id 不能为空"
                return modelKey
            }
            if (modelKeyRequest.projectId.isNullOrEmpty()){
                modelKeyRequest.projectId = projectId
            }
            val model = schemeMapper.modelKey(modelKeyRequest.projectId!!, modelKeyRequest.floorId!!)
            logger.debug("对象=${model?.toJson()}")
            if (model!=null && !model.bimIdPre.isNullOrEmpty()){
                val split = model.bimIdPre!!.split(":")
                modelKey.bimIdPre = split[0]
                modelKey.key = model.key
                modelKey.result = "success"
                return modelKey
            }else {
                modelKey.result = "failure"
                modelKey.message = "楼层没有相关数据"
                return modelKey
            }

        } catch (e: Exception) {
            e.printStackTrace()
            modelKey.result = "failure"
            modelKey.message = e.message!!
            return modelKey
        }
    }
}