package com.persagy.labsl.services.planar

import com.persagy.database.SFilter
import com.persagy.labsl.Opts
import com.persagy.labsl.models.entities.pbs.PlanarGraphPub
import com.persagy.labsl.models.entities.pbs.QbsFolder
import com.persagy.labsl.utils.IdUtils
import com.persagy.mybatis.SMybatisDao
import com.persagy.service.SObjectService
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.requests.SCreateRequest
import com.persagy.service.models.requests.SUpdateRequest
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.models.responses.SCreateResponse

/**
 * 文件夹服务
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/12/1 15:37
 */
object QbsFolderService : SObjectService<QbsFolder>(SMybatisDao(QbsFolder::class.java)) {

    /** 已发布 */
    val planarGraphPubService = SObjectService(SMybatisDao(PlanarGraphPub::class.java))

    /**
     * 创建文件夹
     *
     * @param request   对象列表
     * @return 对象列表
     */
    fun creates(request: SCreateRequest<QbsFolder>): SCreateResponse<QbsFolder> {
        val sCreateResponse = SCreateResponse<QbsFolder>()
        try {
            if (!request.content.isNullOrEmpty()) {
                    for (content in request.content){
                        val execList = select(SFilter.eq("projectId", Opts.projectId!!),SFilter.eq("name", content.name!!)).exec()
                        if (execList.size>0){
                            sCreateResponse.result = SResponseType.failure
                            sCreateResponse.message ="文件夹名称已存在"
                            return sCreateResponse
                        }
                        content.projectId = Opts.projectId
                        content.id = IdUtils.uuidCreate()
                        insert(content)

                    }
                sCreateResponse.entityList = request.content
                sCreateResponse.result = SResponseType.success
                } else {
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message ="创建内容不可以为空"
                }
        } catch (e: Exception) {
            e.printStackTrace()
            sCreateResponse.result = SResponseType.failure
        }
        return sCreateResponse
    }

    /**
     * 删除
     */
    fun deletes(idList: ArrayList<QbsFolder>): SBaseResponse {
        val sBaseResponse = SBaseResponse()
        if (idList.size>0) {
            try {
                for (qbsFolder in idList) {
                    val planarGraphList = PlanarGraphService.select(
                        SFilter.eq("folderId", qbsFolder.id!!),
                        SFilter.eq("projectId", Opts.projectId!!),
                        SFilter.eq("state", "Draft")
                    ).exec()
                    val planarGraphPubList = planarGraphPubService.select(
                        SFilter.eq("folderId", qbsFolder.id!!),
                        SFilter.eq("projectId", Opts.projectId!!),
                        SFilter.eq("state", "Publish")
                    ).exec()
                    if (planarGraphList.size>0||planarGraphPubList.size>0) {
                        sBaseResponse.message = "文件夹有内容，禁止删除"
                        sBaseResponse.result = SResponseType.failure
                        return sBaseResponse
                    }
                }

                for (qbsFolder in idList) {
                    val delete = delete(qbsFolder)
                    if (delete) {
                        PlanarGraphService.delete(
                            SFilter.eq("id", qbsFolder.id!!),
                            SFilter.eq("projectId", Opts.projectId!!),
                            SFilter.eq("state", "Recyle")
                        )
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            sBaseResponse.result =SResponseType.success
        } else {
            sBaseResponse.result = SResponseType.failure
        }
        return sBaseResponse
    }

    /**
     * 修改文件夹信息
     *
     * @param request   对象列表
     * @return 返回修改标记对象
     */
    fun updates(request: SUpdateRequest<QbsFolder>): SBaseResponse {
        val sBaseResponse = SBaseResponse()
        if (!request.content.isNullOrEmpty()) {
           for (content in request.content!!) {
               val execList = select(SFilter.eq("projectId", Opts.projectId!!),SFilter.not(SFilter.eq("id",content.id!!)),SFilter.eq("name", content.name!!)).exec()
               if (execList.size>0) {
                   sBaseResponse.result = SResponseType.failure
                   sBaseResponse.message = "文件夹名已存在"
                   return sBaseResponse
               }
               if (!content.projectId.isNullOrEmpty()) {
                   content.projectId = Opts.projectId
               }
               update(content)
           }
            sBaseResponse.result = SResponseType.success
        } else {
            sBaseResponse.result = SResponseType.failure
        }
        return  sBaseResponse
    }


}