package com.persagy.server.services.rel

import com.persagy.server.datacenter.models.entities.rel.RShForSyList
import com.persagy.server.datacenter.models.entities.rel.RSyForSh
import com.persagy.server.datacenter.models.entities.rel.RSyForShList
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 RSyForShService: RService<RSyForSh>(SMybatisDao(RSyForSh::class.java)) {
    /**
     * 创建系统服务于竖井关系
     *
     * @param   rSyForSh    系统服务于竖井关系对象
     */
    fun create(rSyForSh: RSyForSh): SCreateResponse<RSyForSh> {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            rSyForSh.projectId = projectId
            val isInsert = insert(rSyForSh)
            if(isInsert){
                SCreateResponse(SResponseType.success)
            }else{
                SCreateResponse(SResponseType.failure)
            }
        } catch (e: Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure, e.message.toString())
        }
    } // Function create()

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

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

} // Object RSyForShService