package com.persagy.server.services.rel

import com.persagy.server.datacenter.models.entities.rel.RFlForSyList
import com.persagy.server.datacenter.models.entities.rel.RSyForFl
import com.persagy.server.datacenter.models.entities.rel.RSyForFlList
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 RSyForFlService: RService<RSyForFl>(SMybatisDao(RSyForFl::class.java)){

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

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

    /**
     * 创建系统服务于楼层关系
     *
     * @param    rSyForFlList    系统和楼层关系的对象列表
     */
    fun createSy(rSyForFlList: RSyForFlList): SCreateResponse<RSyForFl> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            delete("sysID = '${rSyForFlList.sysId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            for (floorId in rSyForFlList.floorIdList!!){
                val rSyForFl = RSyForFl()
                rSyForFl.sysId = rSyForFlList.sysId
                rSyForFl.floorId = floorId
                rSyForFl.projectId = projectId
                val replace = replace(rSyForFl)
                if (replace){
                    successList.add(rSyForFl)
                }else{
                    failureList.add(rSyForFl)
                }
            }
            if(successList.size > 0){
                val sCreateResponse = SCreateResponse<RSyForFl>(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   rSyForFlList    系统和楼层对应关系列表
     */
    fun createsFl(rSyForFlList: RFlForSyList): SCreateResponse<RSyForFl> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            delete("floorID = '${rSyForFlList.floorId}'")
            val projectId = SPageContext.getHeader("ProjectId")
            for (sysId in rSyForFlList.sysIdList!!){
                val rSyForFl = RSyForFl()
                rSyForFl.floorId = rSyForFlList.floorId
                rSyForFl.sysId = sysId
                rSyForFl.projectId = projectId
                val replace = replace(rSyForFl)
                if (replace){
                    successList.add(rSyForFl)
                }else{
                    failureList.add(rSyForFl)
                }
            }
            if (successList.size > 0 ){
                if (failureList.size > 0){
                    val sCreateResponse = SCreateResponse<RSyForFl>(SResponseType.success)
                    sCreateResponse.message = "失败对象" + failureList.toJson()
                    sCreateResponse
                }else{
                    val sCreateResponse = SCreateResponse<RSyForFl>(SResponseType.failure)
                    sCreateResponse
                }
            }else{
                SCreateResponse(SResponseType.failure)
            }
        }catch (e:Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure, e.message.toString())
        }
    } // Function createsFl()

} // Object RSyForFlService