package com.persagy.server.services.rel

import com.persagy.server.services.base.RService
import com.persagy.server.datacenter.models.entities.objects.Floor
import com.persagy.server.datacenter.models.entities.rel.RFlInSyList
import com.persagy.server.datacenter.models.entities.rel.RSyInFl
import com.persagy.server.datacenter.models.entities.rel.RSyInFlList
import com.persagy.base.extensions.toJson
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.responses.SBaseResponse
import com.persagy.service.models.responses.SCreateResponse

/**
 * 系统所在楼层关系服务
 *
 * @author 付国宝
 */
object RSyInFlService: RService<RSyInFl>(SMybatisDao(RSyInFl::class.java)) {

    /**
     * 创建系统所在楼层关系
     *
     * @param    rSyInFl     系统所在楼层关系对象
     */
    fun create(rSyInFl: RSyInFl): SCreateResponse<RSyInFl> {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            rSyInFl.projectId = projectId
            val floorService = SObjectService(SMybatisDao(Floor::class.java))
            val floorObj = floorService.select(SFilter.eq("id", rSyInFl.floorId!!)).entity()
            if(floorObj != null){
                rSyInFl.buildingId = floorObj.buildId
            }
            val isInsert = insert(rSyInFl)
            if (isInsert){
                SCreateResponse(SResponseType.success)
            }else{
                SCreateResponse(SResponseType.failure)
            }
        } catch (e: Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function create()

    /**
     * 根据对象删除
     *
     * @param    rSyInFl    关系对象实例
     * @return
     */
    fun deletes(rSyInFl: RSyInFl): SBaseResponse {
        return try {
            val isDelete = delete(rSyInFl)
            /** 删除成功 */
            if (isDelete) {
                val sBaseResponse = SBaseResponse(SResponseType.success)
                sBaseResponse.message = rSyInFl.toJson()
                sBaseResponse
            } else {
                SBaseResponse(SResponseType.failure)
            }
        }catch (e: Exception){
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function deletes()

    /**
     * 创建系统所在楼层关系
     *
     * @param    rSyInFlList    系统和楼层关系的对象列表
     */
    fun createSy(rSyInFlList: RSyInFlList): SCreateResponse<RSyInFl>{
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            delete("sysID = '${rSyInFlList.sysId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            val floorService = SObjectService(SMybatisDao(Floor::class.java))
            if (!rSyInFlList.floorIdList.isNullOrEmpty()){
                for (floorId in rSyInFlList.floorIdList!!){
                    val rSyInFl = RSyInFl()
                    rSyInFl.sysId = rSyInFlList.sysId
                    rSyInFl.floorId = floorId
                    rSyInFl.projectId = projectId

                    val floorObj = floorService.select(SFilter.eq("id", rSyInFl.floorId!!)).entity()
                    if(floorObj != null){
                        rSyInFl.buildingId = floorObj.buildId
                    }

                    val replace = replace(rSyInFl)
                    if (replace){
                        successList.add(rSyInFl)
                    }else{
                        failureList.add(rSyInFl)
                    }
                }
                if (successList.size>0){
                    val sCreateResponse = SCreateResponse<RSyInFl>(SResponseType.success)
                    if (failureList.size>0){
                        sCreateResponse.message ="失败对象："+failureList.toJson()
                    }
                    sCreateResponse
                }else{
                    SCreateResponse(SResponseType.failure)
                }
            }else{
                val sCreateResponse = SCreateResponse<RSyInFl>(SResponseType.success)
                sCreateResponse
            }
        } catch (e: Exception) {
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createSy()


    /**
     * 创建系统所在楼层关系
     *
     * @param    rSyInFlList    系统和楼层关系的对象列表
     */
    fun createSyFl(rSyInFlList: RSyInFlList): SCreateResponse<RSyInFl>{
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
//            delete("sysID = '${rSyInFlList.sysId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            val floorService = SObjectService(SMybatisDao(Floor::class.java))
            for (floorId in rSyInFlList.floorIdList!!){
                val rSyInFl = RSyInFl()
                rSyInFl.sysId = rSyInFlList.sysId
                rSyInFl.floorId = floorId
                rSyInFl.projectId = projectId

                val floorObj = floorService.select(SFilter.eq("id", rSyInFl.floorId!!)).entity()
                if(floorObj != null){
                    rSyInFl.buildingId = floorObj.buildId
                }

                val replace = replace(rSyInFl)
                if (replace){
                    successList.add(rSyInFl)
                }else{
                    failureList.add(rSyInFl)
                }
            }
            if (successList.size>0){
                val sCreateResponse = SCreateResponse<RSyInFl>(SResponseType.success)
                if (failureList.size>0){
                    sCreateResponse.message ="失败对象："+failureList.toJson()
                }
                sCreateResponse
            }else{
                SCreateResponse(SResponseType.failure)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createSy()


    /**
     * 创建所在楼层的系统关系
     *
     * @param    rFlInSyList     系统和楼层关系的对象列表
     */
    fun createsFl(rFlInSyList: RFlInSyList): SCreateResponse<RSyInFl>{
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            delete("floorID = '${rFlInSyList.floorId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            val floorService = SObjectService(SMybatisDao(Floor::class.java))
            for (sysId in rFlInSyList.sysIdList!!){
                val rSyInBd = RSyInFl()
                rSyInBd.floorId = rFlInSyList.floorId
                rSyInBd.sysId = sysId
                rSyInBd.projectId = projectId

                val floorObj = floorService.select(SFilter.eq("id", rFlInSyList.floorId!!)).entity()
                if(floorObj != null){
                    rSyInBd.buildingId = floorObj.buildId
                }

                val replace = replace(rSyInBd)
                if (replace){
                    successList.add(rSyInBd)
                }else{
                    failureList.add(rSyInBd)
                }
            }
            if (successList.size>0){
                if (failureList.size>0){
                    val sCreateResponse = SCreateResponse<RSyInFl>(SResponseType.success)
                    sCreateResponse.message ="失败对象："+failureList.toJson()
                    sCreateResponse
                }else{
                    val sCreateResponse = SCreateResponse<RSyInFl>(SResponseType.success)
                    sCreateResponse
                }
            }else{
                SCreateResponse(SResponseType.failure)
            }
        } catch (e:Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createsFl()

} // Object RSyInFlService