package com.persagy.server.services.rel

import com.persagy.server.datacenter.models.entities.rel.RBdForSyList
import com.persagy.server.datacenter.models.entities.rel.RSyForBd
import com.persagy.server.datacenter.models.entities.rel.RSyForBdList
import com.persagy.server.services.base.RService
import com.persagy.base.extensions.toJson
import com.persagy.mybatis.SMybatisDao
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 RSyForBdService: RService<RSyForBd>(SMybatisDao(RSyForBd::class.java)){

    /**
     * 创建系统服务于建筑关系
     *
     * @param   rSyForBd    系统服务于建筑关系对象
     */
    fun create(rSyForBd: RSyForBd): SCreateResponse<RSyForBd> {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            rSyForBd.projectId = projectId
            val isInsert = insert(rSyForBd)
            if (isInsert){
                SCreateResponse(SResponseType.success)
            }else{
                SCreateResponse(SResponseType.failure)
            }
        } catch (e: Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } //Function create()

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

    /**
     * 创建系统服务于建筑关系
     *
     * @param    rSyForBdList     系统和建筑关系的对象列表
     */
    fun createSy(rSyForBdList: RSyForBdList): SCreateResponse<RSyForBd> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            delete("sysID = '${rSyForBdList.sysId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            for (buildingId in rSyForBdList.buildingIdList!!){
                val rSyForBd = RSyForBd()
                rSyForBd.sysId = rSyForBdList.sysId
                rSyForBd.buildingId = buildingId
                rSyForBd.projectId = projectId
                val replace = replace(rSyForBd)
                if (replace){
                    successList.add(rSyForBd)
                }else{
                    failureList.add(rSyForBd)
                }
            }
            if (successList.size>0){
                val sCreateResponse = SCreateResponse<RSyForBd>(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    rBdForSyList    系统和建筑关系的对象列表
     */
    fun createsBd(rBdForSyList: RBdForSyList): SCreateResponse<RSyForBd> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            delete("buildingID = '${rBdForSyList.buildingId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            for (sysId in rBdForSyList.sysIdList!!){
                val rSyForBd = RSyForBd()
                rSyForBd.buildingId = rBdForSyList.buildingId
                rSyForBd.sysId = sysId
                rSyForBd.projectId = projectId
                val replace = replace(rSyForBd)
                if (replace){
                    successList.add(rSyForBd)
                }else{
                    failureList.add(rSyForBd)
                }
            }
            if (successList.size>0){
                if (failureList.size>0){
                    val sCreateResponse = SCreateResponse<RSyForBd>(SResponseType.success)
                    sCreateResponse.message ="失败对象："+failureList.toJson()
                    sCreateResponse
                }else{
                    val sCreateResponse = SCreateResponse<RSyForBd>(SResponseType.success)
                    sCreateResponse
                }
            }else{
                SCreateResponse(SResponseType.failure)
            }
        } catch (e:Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createsBd()

} // Object RSyForBdService