package com.persagy.server.meiku.services

import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.meiku.models.entities.MeiKu
import com.persagy.server.meiku.models.response.SCommonResponse
import com.persagy.service.SObjectService
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.models.responses.SCreateResponse
import java.util.*

/**
 *
 * @author  wx
 * @date  2020/9/21 11:21
 */
object MeiKuService : SObjectService<MeiKu>(SMybatisDao(MeiKu::class.java)) {

    override fun onCreateBefore(entity: MeiKu): Boolean {
        if (entity.id.isNullOrEmpty()) {
            entity.id = UUID.randomUUID().toString().replace("-", "")
        }
        return super.onCreateBefore(entity)
    }

    /**
     * 保存
     */
    fun save(meiKu: MeiKu): SCreateResponse<MeiKu> {
        val sCreateResponse = SCreateResponse<MeiKu>()
        return try {
            if (meiKu.id.isNullOrEmpty()) {
                meiKu.id = UUID.randomUUID().toString().replace("-", "")
            }
            val insert = insert(meiKu)
            if (!meiKu.anchorList.isNullOrEmpty()) {
                for (anchor in meiKu.anchorList!!) {
                    anchor.meiTuId = meiKu.id
                    if (anchor.id.isNullOrEmpty()) {
                        anchor.id = UUID.randomUUID().toString().replace("-", "")
                    }
                    val insert1 = MeiKuAnchorService.insert(anchor)
                }
            }
            if (meiKu.state != null) {
                val state = meiKu.state
                state!!.meiTuId = meiKu.id
                if (state!!.id.isNullOrEmpty()) {
                    state!!.id = UUID.randomUUID().toString().replace("-", "")
                }
                MeiKuStateService.insert(meiKu.state!!)
            }
            sCreateResponse.entityList = arrayListOf(meiKu)
            sCreateResponse.result = SResponseType.success
            sCreateResponse
        } catch (e: Exception) {
            e.printStackTrace()
            sCreateResponse.result = SResponseType.failure
            sCreateResponse
        }
    } // Fun save()

    /**
     * 单个对象修改，包括下面节点
     */
    fun revise(meiKu: MeiKu): SBaseResponse {
        val sBaseResponse = SBaseResponse()
        return try {
            if (!meiKu.id.isNullOrEmpty()) {
                delete(SFilter.eq("id", meiKu.id!!))
                MeiKuAnchorService.delete(SFilter.eq("id", meiKu.id!!))
                MeiKuStateService.delete(SFilter.eq("id", meiKu.id!!))
                val insert = insert(meiKu)
                if (!meiKu.anchorList.isNullOrEmpty()){
                    for (anchor in meiKu.anchorList!!) {
                        anchor.meiTuId = meiKu.id
                        if (anchor.id.isNullOrEmpty()) {
                            anchor.id = UUID.randomUUID().toString().replace("-", "")
                        }
                        val insert1 = MeiKuAnchorService.insert(anchor)
                    }
                }
                if (meiKu.state != null) {
                    val state = meiKu.state
                    state!!.meiTuId = meiKu.id
                    if (state!!.id.isNullOrEmpty()) {
                        state!!.id = UUID.randomUUID().toString().replace("-", "")
                    }
                    val insert1 = MeiKuStateService.insert(meiKu.state!!)
                }
                sBaseResponse.result = SResponseType.success
            } else {
                sBaseResponse.result = SResponseType.failure
            }
            sBaseResponse
        } catch (e: Exception) {
            e.printStackTrace()
            sBaseResponse.result = SResponseType.failure
            sBaseResponse.message = e.message.toString()
            sBaseResponse
        }
    } // Fun revise()

    /**
     * 清除节点
     */
    override fun onDeleteSuccess(entityList: ArrayList<MeiKu>) {
        for (entity in entityList){
            MeiKuAnchorService.delete(SFilter.eq("id", entity.id!!))
            MeiKuStateService.delete(SFilter.eq("id", entity.id!!))
        }
        super.onDeleteSuccess(entityList)
    }

    /**
     * 读取图下面所有节点
     */
    fun read(meiKu: MeiKu):SCommonResponse<MeiKu>{
        val sQueryResponse = SCommonResponse<MeiKu>()
        return try {
            if (!meiKu.id.isNullOrEmpty()){
                val entity = select(SFilter.eq("id", meiKu.id!!)).entity()
                if (entity!=null){
                    val meiKuAnchorList = MeiKuAnchorService.select(SFilter.eq("id", meiKu.id!!)).exec()
                    if (meiKuAnchorList.size>0){
                        entity.anchorList = meiKuAnchorList
                    }
                    val meiKuState = MeiKuStateService.select(SFilter.eq("id", meiKu.id!!)).entity()
                    if (meiKuState!=null){
                        entity.state = meiKuState
                    }
                    sQueryResponse.content = entity
                    sQueryResponse.result = SResponseType.success
                }else{
                    sQueryResponse.result = SResponseType.failure
                }
            }else{
                sQueryResponse.result = SResponseType.failure
            }
             sQueryResponse
        } catch (e: Exception) {
            e.printStackTrace()
            sQueryResponse.result = SResponseType.failure
            sQueryResponse.message = e.message.toString()
            sQueryResponse
        }
    } // read()


} // Object MeiKuService