package com.persagy.server.services.rel

import com.persagy.server.datacenter.models.entities.rel.RSpForSyBaseList
import com.persagy.server.datacenter.models.entities.rel.RSy2Sp
import com.persagy.server.datacenter.models.entities.rel.RSyForSpBase
import com.persagy.server.datacenter.models.entities.rel.RSyForSpBaseList
import com.persagy.server.services.objects.ZoneSpaceBaseService
import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.service.SBaseService
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 RSyForSpBaseService: SBaseService<RSyForSpBase>(SMybatisDao(RSyForSpBase::class.java)) {

    var rSy2SpService = SObjectService(SMybatisDao( RSy2Sp::class.java))
    /**
     * 创建系统服务于业务空间关系
     *
     * @param   rSyForSpBase    系统服务于业务空间关系对象
     */
    fun create(rSyForSpBase: RSyForSpBase): SCreateResponse<RSyForSpBase> {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            rSyForSpBase.projectId = projectId
            val rSy2Sp = RSy2Sp()
            rSy2Sp.id1 = rSyForSpBase.sysId
            rSy2Sp.id2 = rSyForSpBase.spaceId
            rSy2Sp.zoneType = rSyForSpBase.objectType
            rSy2Sp.sign = 1
            rSy2Sp.projectId = projectId
            rSy2Sp.type = "sy2sp_for"
            val isInsert = rSy2SpService.insert(rSy2Sp)
            if (isInsert){
                SCreateResponse(SResponseType.success)
            }else{
                SCreateResponse(SResponseType.failure)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function create()

    /**
     * 根据对象删除
     *
     * @param   rSyForSpBase    关系对象实例
     * @return
     */
    fun deletes(rSyForSpBase: RSyForSpBase): SBaseResponse {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            val entity = ZoneSpaceBaseService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("id", rSyForSpBase.spaceId!!)).entity()
            val rSy2Sp = RSy2Sp()
            rSy2Sp.id1 = rSyForSpBase.sysId
            rSy2Sp.id2 = rSyForSpBase.spaceId
            rSy2Sp.projectId = projectId
            rSy2Sp.type = "sy2sp_for"
            rSy2Sp.zoneType = entity!!.objectType
            val isDelete =rSy2SpService.delete(rSy2Sp)
            /** 删除成功 */
            if (isDelete) {
                val sBaseResponse = SBaseResponse(SResponseType.success)
                sBaseResponse.message = rSyForSpBase.toJson()
                sBaseResponse
            } else {
                SBaseResponse(SResponseType.failure)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function deletes()

    /**
     * 创建系统服务于业务空间关系
     *
     * @param   rSyForSpBaseList    系统服务于业务空间关系对象列表
     */
    fun createSy(rSyForSpBaseList: RSyForSpBaseList): SCreateResponse<RSyForSpBase> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            rSy2SpService.delete("id1 = '${rSyForSpBaseList.sysId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            for (spaceId in rSyForSpBaseList.spaceIdList!!){
                val entity = ZoneSpaceBaseService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("id", spaceId!!)).entity()
                val rSy2Sp = RSy2Sp()
                rSy2Sp.id1 = rSyForSpBaseList.sysId
                rSy2Sp.id2 = spaceId
                rSy2Sp.zoneType = entity!!.objectType
                rSy2Sp.sign = 1
                rSy2Sp.projectId = projectId
                rSy2Sp.type = "sy2sp_for"
                val replace = rSy2SpService.replace(rSy2Sp)
                if (replace){
                    successList.add(rSy2Sp)
                }else{
                    failureList.add(rSy2Sp)
                }
            }
            if (successList.size>0){
                val sCreateResponse = SCreateResponse<RSyForSpBase>(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   rSyForSpBaseList    服务于业务空间的系统关系对象列表
     */
    fun createsSp(rSyForSpBaseList: RSpForSyBaseList): SCreateResponse<RSyForSpBase> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            val projectId = SPageContext.getHeader("ProjectId")
            rSy2SpService.delete("id2 = '${rSyForSpBaseList.spaceId}'")
            val entity = ZoneSpaceBaseService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("id", rSyForSpBaseList.spaceId!!)).entity()

            for (sysId in rSyForSpBaseList.sysIdList!!){
                val rSy2Sp = RSy2Sp()
                rSy2Sp.id2 = rSyForSpBaseList.spaceId
                rSy2Sp.id1 = sysId
                rSy2Sp.projectId = projectId
                rSy2Sp.zoneType = entity!!.objectType
                rSy2Sp.sign = 1
                rSy2Sp.type = "sy2sp_for"
                val replace = rSy2SpService.replace(rSy2Sp)
                if (replace){
                    successList.add(rSy2Sp)
                }else{
                    failureList.add(rSy2Sp)
                }
            }
            if (successList.size>0){
                if (failureList.size>0){
                    val sCreateResponse = SCreateResponse<RSyForSpBase>(SResponseType.success)
                    sCreateResponse.message ="失败对象："+failureList.toJson()
                    sCreateResponse
                }else{
                    val sCreateResponse = SCreateResponse<RSyForSpBase>(SResponseType.success)
                    sCreateResponse
                }
            }else{
                SCreateResponse(SResponseType.failure)
            }
        } catch (e:Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createsSp()

} // Object RSyForSpBaseService