package com.persagy.meiku.services

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

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

    /**
     * 创建之前操作
     *
     * @param entity    图例对象
     * @return 是否执行
     */
    override fun onCreateBefore(entity: MeiKu): Boolean {
        /** id 为空，赋值  */
        if (entity.id.isNullOrEmpty()) {
            entity.id = UUID.randomUUID().toString().replace("-", "")
        }
        return super.onCreateBefore(entity)
    }

    /**
     * 保存
     *
     * @param meiKu     图例对象
     * @return 返回结果
     */
    fun save(meiKu: MeiKu): SCreateResponse<MeiKu> {
        /** 返回对象 */
        val sCreateResponse = SCreateResponse<MeiKu>()
        return try {
            /** id 为空赋值 */
            if (meiKu.id.isNullOrEmpty()) {
                meiKu.id = UUID.randomUUID().toString().replace("-", "")
            }
            /** 插入数据 */
            val insert = insert(meiKu)
            /** 锚点列表不为空 */
            if (!meiKu.anchorList.isNullOrEmpty()) {
                /** 遍历列表 */
                for (anchor in meiKu.anchorList!!) {
                    /** 图例 id */
                    anchor.meiKuId = meiKu.id
                    /** 锚点 id 为空赋值 */
                    if (anchor.id.isNullOrEmpty()) {
                        anchor.id = UUID.randomUUID().toString().replace("-", "")
                    }
                    /** 插入锚点数据 */
                    val insert1 = MeiKuAnchorService.insert(anchor)
                }
            }

            /** 状态 */
            if (!meiKu.state .isNullOrEmpty()) {
                val stateList = meiKu.state
                /** 遍历状态 */
                for (state in stateList!!){
                    /** 图例 id */
                    state!!.meiKuId = meiKu.id
                    /** 状态 id 为空赋值 */
                    if (state!!.id.isNullOrEmpty()) {
                        state!!.id = UUID.randomUUID().toString().replace("-", "")
                    }

                    /** 插入数据 */
                    MeiKuStateService.insert(state)
                }
            }
            /** 对象列表 */
            sCreateResponse.entityList = arrayListOf(meiKu)
            /** 标志 */
            sCreateResponse.result = SResponseType.success
            /** 对象 */
            sCreateResponse
        } catch (e: Exception) {
            e.printStackTrace()
            sCreateResponse.result = SResponseType.failure
            sCreateResponse
        }
    }

    /**
     * 单个对象修改，包括下面节点
     *
     * @param meiKu     图例对象
     * @return 返回结果
     */
    fun revise(meiKu: MeiKu): SBaseResponse {
        /** 返回对象 */
        val sBaseResponse = SBaseResponse()
        return try {
            /** 图例id不为空 */
            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!!) {
                        /** 图例 id */
                        anchor.meiKuId = meiKu.id
                        /** 锚点 id 为空赋值 */
                        if (anchor.id.isNullOrEmpty()) {
                            anchor.id = UUID.randomUUID().toString().replace("-", "")
                        }
                        /** 插入 */
                        val insert1 = MeiKuAnchorService.insert(anchor)
                    }
                }
                /** 标志不为空 */
                if (!meiKu.state.isNullOrEmpty()) {
                    val stateList = meiKu.state
                    /** 遍历列表 */
                    for (state in stateList!!){

                        state!!.meiKuId = meiKu.id
                        /** 标志 id 为空赋值 */
                        if (state!!.id.isNullOrEmpty()) {
                            state!!.id = UUID.randomUUID().toString().replace("-", "")
                        }
                        /** 插入数据 */
                        val insert1 = MeiKuStateService.insert(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
        }
    }

    /**
     * 清除节点
     */
    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)
    }

    /**
     * 读取图下面所有节点
     *
     * @param meiKu     图例对象
     * @return 返回结果
     */
    fun read(meiKu: MeiKu): SCommonResponse<MeiKu> {
        /** 返回对象 */
        val sQueryResponse = SCommonResponse<MeiKu>()
        return try {
            /** 图例 id 不可以为空 */
            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!!)).exec()
                    if (!meiKuState.isNullOrEmpty()){
                        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
        }
    }

}