package com.persagy.labsl.services

import com.persagy.labsl.utils.IdUtils
import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.labsl.Opts
import com.persagy.labsl.models.entities.tpt.Elements
import com.persagy.labsl.models.entities.tpt.Graph
import com.persagy.labsl.models.entities.tpt.pub.*
import com.persagy.labsl.models.response.SCommonResponse
import com.persagy.labsl.services.pub.GraphPubService
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
import com.persagy.service.models.responses.SQueryResponse
import com.persagy.service.utils.SSpringContextUtil
import org.slf4j.LoggerFactory
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

/**
 * 图服务
 *
 * @author wx  <zhangweixin@sagacloud.com>
 */
object GraphService : SObjectService<Graph>(SMybatisDao(
    Graph::class.java)) {

    /** 自定义sql */
    private val customMapper by lazy {
        SSpringContextUtil.getBean(CustomService::class.java) as CustomService
    }

    /** 日志 */
    private val logger = LoggerFactory.getLogger(GraphService::class.java)
    /** 发布的图例节点服务 */
    private val nodePubService = SObjectService(SMybatisDao(NodePub::class.java))
    /** 注释、说明 */
    private val markersPubService = SObjectService(SMybatisDao(MarkersPub::class.java))
    /** 管线对象 */
    private val relationPubService = SObjectService(SMybatisDao(RelationPub::class.java))
    /** 锚点 */
    private val anchorPubService = SObjectService(SMybatisDao(AnchorPub::class.java))

    /**
     * 创建之前回调
     *
     * @param entity    图例对象
     */
    override fun onCreateBefore(entity: Graph): Boolean {
        /** id 赋值 */
        if (entity.id.isNullOrEmpty()){
            entity.id = IdUtils.uuidCreate()
        }

        /** 图 id */
        if (entity.graphId.isNullOrEmpty()){
            entity.graphId = IdUtils.uuidCreate()
        }

        /** 版本 */
        if (entity.version.isNullOrEmpty())
            entity.version = "2.0.1"

        /** 项目 id */
        if (entity.projectId.isNullOrEmpty()){
            entity.projectId = Opts.projectId
        }
        return super.onCreateBefore(entity)
    } // Fun onCreateBefore()

    /**
     * 创建
     */
    fun  draftsCreate(request: SCreateRequest<Graph>): SCreateResponse<Graph>{
        val sCreateResponse = SCreateResponse<Graph>(SResponseType.success)
        try {
            if (!request.content.isNullOrEmpty()) {
                for (content in request.content) {
                    /** id 赋值 */
                    if (content.id.isNullOrEmpty()){
                        content.id = IdUtils.uuidCreate()
                    }

                    /** 图 id */
                    if (content.graphId.isNullOrEmpty()){
                        content.graphId = IdUtils.uuidCreate()
                    }

                    /** 版本 */
                    if (content.version.isNullOrEmpty())
                        content.version = "2.0.1"

                    /** 项目 id */
                    if (content.projectId.isNullOrEmpty()){
                        content.projectId = Opts.projectId
                    }
                    val entity = select(
                        SFilter.eq("projectId", Opts.projectId!!), SFilter.eq("categoryId", content.categoryId!!),
                        SFilter.eq("name", content.name!!)
                    ).entity()
                    if (entity!=null) {
                        sCreateResponse.result = SResponseType.failure
                        sCreateResponse.message = "同一分类下图名称不可以重复"
                        return sCreateResponse
                    }else {
                        val entity1 = GraphPubService.select(
                            SFilter.eq("projectId", Opts.projectId!!), SFilter.eq("categoryId", content.categoryId!!),
                            SFilter.eq("name", content.name!!)
                        ).entity()
                        if (entity1!=null) {
                            sCreateResponse.result = SResponseType.failure
                            sCreateResponse.message = "同一分类下已发布图名称已存在"
                            return sCreateResponse
                        }else {
                            insert(content)
                        }
                    }
                }
                sCreateResponse.entityList = request.content
            }else {
                sCreateResponse.message = "不可以为空"
            }
            return sCreateResponse
        } catch (e: Exception) {
            e.printStackTrace()
            return SCreateResponse(SResponseType.failure)
        }
    }

    fun draftsUpdate(request: SUpdateRequest<Graph>): SBaseResponse {
        val sBaseResponse = SBaseResponse(SResponseType.success)
        try {
            if (!request.content.isNullOrEmpty()) {
                for (content in request.content!!) {

                    val entity = select(
                        SFilter.eq("projectId", Opts.projectId!!), SFilter.eq("categoryId", content.categoryId!!),
                        SFilter.eq("name", content.name!!), SFilter.not(SFilter.eq("graphId",content.graphId!!))
                    ).entity()
                    if (entity!=null) {
                        sBaseResponse.result = SResponseType.failure
                        sBaseResponse.message = "图名称已经存在"
                        return sBaseResponse
                    }
                    val entity1 = GraphPubService.select(
                        SFilter.eq("projectId", Opts.projectId!!), SFilter.eq("categoryId", content.categoryId!!),
                        SFilter.eq("name", content.name!!),SFilter.not(SFilter.eq("graphId",content.graphId!!))
                    ).entity()
                    if (entity1!=null) {
                        sBaseResponse.result = SResponseType.failure
                        sBaseResponse.message = "同一分类下已发布图名称已存在"
                        return sBaseResponse
                    }else {
                       update(content)
                    }
                }
            }
            return sBaseResponse
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message.toString())
        }
    }

    /**
     * 已发布修改
     */
    fun pubUpdate(request: SUpdateRequest<GraphPub>): SBaseResponse {
        val sBaseResponse = SBaseResponse(SResponseType.success)
        if (!request.content.isNullOrEmpty()) {
            for (content in request.content!!) {

                val entity = select(
                    SFilter.eq("projectId", Opts.projectId!!), SFilter.eq("categoryId", content.categoryId!!),
                    SFilter.eq("name", content.name!!), SFilter.not(SFilter.eq("graphId",content.graphId!!))
                ).entity()
                if (entity!=null) {
                    sBaseResponse.result = SResponseType.failure
                    sBaseResponse.message = "图名称已经存在"
                    return sBaseResponse
                }
                val entity1 = GraphPubService.select(
                    SFilter.eq("projectId", Opts.projectId!!), SFilter.eq("categoryId", content.categoryId!!),
                    SFilter.eq("name", content.name!!),SFilter.not(SFilter.eq("graphId",content.graphId!!))
                ).entity()
                if (entity1!=null) {
                    sBaseResponse.result = SResponseType.failure
                    sBaseResponse.message = "同一分类下已发布图名称已存在"
                    return sBaseResponse
                }else {
                    GraphPubService.update(content)
                }

            }
        }
        return sBaseResponse
    }







    /**
     * 创建图
     *
     * @param graph   图对象
     * @return 图形对象列表
     */
    fun save(graph: Graph): SCreateResponse<Graph> {
        val sCreateResponse = SCreateResponse<Graph>()
        var readDrafts: SCommonResponse<Graph>? = null
        try {
            /** 开启事务 */
            openTransaction()
            /** 未发布 */
            if (graph.id.isNullOrEmpty()) {
                /** id赋值 */
                graph.id = UUID.randomUUID().toString().replace("-", "")
            }

            /** 图 id 为空赋值*/
            if (graph.graphId.isNullOrEmpty()){
                graph.graphId = UUID.randomUUID().toString().replace("-", "")
            }

            /** 项目 id 赋值 */
            graph.projectId = Opts.projectId!!

            readDrafts = readDrafts(graph)


            /** 清除以前的版本 */
            delOldData(graph)
            /** 版本号 */
            if (graph.version.isNullOrEmpty()) {
                graph.version = "2.0.1"
            } else {
                val listVersion = graph.version!!.split(".")
                val num = listVersion[2].toInt() + 1
                graph.version = listVersion[0] + "." + listVersion[1] + "." + num
            }
            /** 图标记设置为 1 草稿箱 */
            graph.state = 1
            /** 插入图形数据 */
            val insertGraph = insert(graph)
            /** 插入是否成功 */
            if (!insertGraph) {
                /** 事务回滚 */
                backTransaction()
                /** 返回标记 */
                sCreateResponse.result = SResponseType.failure
                /** 返回对象 */
                return  sCreateResponse
            }

            /** 列表不能为空 */
            if (graph.elements != null) {
                val nodesList = graph.elements!!.nodes
                /** 列表不能为空 */
                if (!nodesList.isNullOrEmpty()) {
                    /** 遍历列表 */
                    for (nodes in nodesList) {
                        /**  图 id 赋值给子节点*/
                        nodes.graphId = graph.graphId
                        /** 图形 id */
                        nodes.id = graph.id
                        nodes.state = 1
                        /** 子节点 id */
                        if (nodes.nodeId.isNullOrEmpty()) {
                            /** id 赋值 */
                            nodes.nodeId = UUID.randomUUID().toString().replace("-", "")
                        }

                        /** 插入数据 */
                        val insertNode = NodeService.insert(nodes)
                        /** 插入是否成功 */
                        if (!insertNode) {
                            /** 事务回滚 */
                            backTransaction()
                            /** 返回标记 */
                            sCreateResponse.result = SResponseType.failure
                            /** 返回对象 */
                            return  sCreateResponse
                        }

                        val anchorList = nodes.anchorList
                        /** 列表不能为空 */
                        if (!anchorList.isNullOrEmpty()) {
                            /** 遍历列表 */
                            for (anchor in anchorList) {
                                /** 图 id 赋值 */
                                anchor.graphId = graph.graphId
                                /** 图编码 */
                                anchor.id = graph.id
                                /** 节点 id 赋值 */
                                anchor.nodeId = nodes.nodeId
                                anchor.state = 1
                                if (anchor.anchorId.isNullOrEmpty()) {
                                    /** id赋值 */
                                    anchor.anchorId = UUID.randomUUID().toString().replace("-", "")
                                }

                                /** 插入数据 */
                                val insertAnchor = AnchorService.insert(anchor)
                                /** 插入是否成功 */
                                if (!insertAnchor) {
                                    /** 事务回滚 */
                                    backTransaction()
                                    /** 返回标记 */
                                    sCreateResponse.result = SResponseType.failure
                                    /** 返回对象 */
                                    return  sCreateResponse
                                }
                            }
                        }
                    }
                }

                val markersList = graph.elements!!.markers
                /** 列表不为空 */
                if (!markersList.isNullOrEmpty()) {
                    /** 遍历列表 */
                    for (markers in markersList) {
                        /** 图 id */
                        markers.graphId = graph.graphId
                        /** 标识 id */
                        markers.id = graph.id
                        /** 草稿箱标志 */
                        markers.state = 1
                        /** id 为空赋值 */
                        if (markers.markerId.isNullOrEmpty()) {
                            /** id赋值 */
                            markers.markerId = UUID.randomUUID().toString().replace("-", "")
                        }

                        /** 插入数据 */
                        val insertMarkers = MarkersService.insert(markers)
                        /** 数据是否插入成功 */
                        if (!insertMarkers) {
                            /** 事务回滚 */
                            backTransaction()
                            /** 返回标志 */
                            sCreateResponse.result = SResponseType.failure
                            /** 返回对象 */
                            return  sCreateResponse
                        }
                    }
                }

                val relationsList = graph.elements!!.relations
                /** 列表不为空 */
                if (!relationsList.isNullOrEmpty()) {
                    /** 遍历列表 */
                    for (relations in relationsList) {
                        /** 图 id */
                        relations.graphId = graph.graphId
                        /** 草稿箱标志 */
                        relations.state = 1
                        /** 图编码 */
                        relations.id = graph.id
                        /** 管线 id 为空 赋值 */
                        if (relations.relationId.isNullOrEmpty()) {
                            /** id 赋值 */
                            relations.relationId = UUID.randomUUID().toString().replace("-", "")
                        }

                        /** 插入数据 */
                        val insertRelation = RelationService.insert(relations)
                        /** 插入是否成功 */
                        if (!insertRelation) {
                            /** 事务回滚 */
                            backTransaction()
                            /** 返回标志 */
                            sCreateResponse.result = SResponseType.failure
                            /** 返回对象 */
                            return  sCreateResponse
                        }
                    }
                }
            }

            /** 提交事务 */
            commitTransaction()
            /** 返回标志 */
            sCreateResponse.result = SResponseType.success
            /** 返回对象列表 */
            sCreateResponse.entityList = arrayListOf(graph)
            /** 返回对象 */
            return sCreateResponse
        } catch (e: Exception) {
            e.printStackTrace()
            if (readDrafts != null){
                val content = readDrafts?.content
                if (content != null){
                    save(content)
                }
            }
            return SCreateResponse(SResponseType.failure,e.message.toString())
        }
    }

    /**
     * 发布图形 回收站和草稿箱在一个表中，导致同一个图会有多个在回收站中需要配备多主键
     *
     * @param graphPub  待发布的图对象
     * @return 已发布的图对象列表
     */
    fun pubDrawings(graphPub: GraphPub): SCreateResponse<GraphPub> {
        return try {
            /** 返回对象 */
            val sCreateResponse = SCreateResponse<GraphPub>()
            /** 图对象 */
            val graph1 = Graph()
            /** 项目 id */
            graph1.projectId = Opts.projectId!!
            /** 图编码 */
            graph1.id = graphPub.id
            /** 图 id */
            graph1.graphId = graphPub.graphId
            /** 查询草稿箱 */
            val squeryBuilder = GraphPubService.select(
                SFilter.eq("projectId", Opts.projectId!!),
                SFilter.eq("id", graphPub.id!!),
                SFilter.eq("graphId", graphPub.graphId!!),
                SFilter.eq("state", 1)
            )
            /** 设置表明，查询 */
            val graph = squeryBuilder.table("labsl.graph").entity()
            /** 对象不为空 */
            if (graph!=null){
                /** 开启事务 */
                openTransaction()
                /** 清除已发布的老数据 */
                delOldDataPub(graph1)
                /** 版本号 */
                val listVersion = graph.version!!.split(".")
                /** 版本号加 1 */
                val num = listVersion[1].toInt() + 1
                /** 版本号赋值 */
                graph.version = listVersion[0] + "." + num + "." + 0
                /** 已发布标志 */
                graph.state = 0
                /** 已发布 */
                graph.isAvailable = true
    //            graph.isPub = true
                /** 插入数据 */
                val insert = GraphPubService.insert(graph)
                /** 插入数据是否成功 */
                if (!insert) {
                    /** 回滚 */
                    backTransaction()
                    /** 返回标志 */
                    sCreateResponse.result = SResponseType.failure
                    /** 返回对象 */
                    return  sCreateResponse
                }

                /** 子节点 */
                val nodebuilder = nodePubService.select(
                    SFilter.eq("graphId", graph.graphId!!),
                    SFilter.eq("id", graphPub.id!!),
                    SFilter.eq("state", 1)
                )
                /** 设置表明，查询 */
                val nodePubList = nodebuilder.table("labsl.node").exec()
                /** 列表长度大于 0 */
                if (nodePubList.size>0){
                    logger.debug("对象长度 = ${nodePubList.size}   ******************************************************")
                    /** 遍历列表 */
                    for (nodePub in nodePubList){
                        /** 图编码 */
                        nodePub.id = graph.id
                        /** 图 id */
                        nodePub.graphId = graph.graphId
                        /** 发布标志 */
                        nodePub.state = 0
                        /** 插入数据 */
                        val insert1 = nodePubService.insert(nodePub)
                        /** 插入是否成功 */
                        if (!insert1){
                            /** 回滚 */
                            backTransaction()
                            /** 返回标志 */
                            sCreateResponse.result = SResponseType.failure
                            /** 返回对象 */
                            return  sCreateResponse
                        }
                    }
                }

                /** 查询 */
                val markersbuilder = markersPubService.select(
                    SFilter.eq("graphId", graph.graphId!!),
                    SFilter.eq("id", graphPub.id!!),
                    SFilter.eq("state", 1)
                )
                /** 设置表明，查询 */
                val markersPubList = markersbuilder.table("labsl.marker").exec()
                /** 列表大于 0 */
                if (markersPubList.size>0){
                    /** 遍历列表 */
                    for (markersPub in markersPubList){
                        /** 图编码 */
                        markersPub.id = graph.id
                        /** 图 id */
                        markersPub.graphId = graph.graphId
                        /** 发布标志 */
                        markersPub.state = 0
                        /** 插图数据 */
                        val insert1 = markersPubService.insert(markersPub)
                        /** 插入是否成功 */
                        if (!insert1){
                            /** 回滚 */
                            backTransaction()
                            /** 返回标志 */
                            sCreateResponse.result = SResponseType.failure
                            /** 返回对象 */
                            return  sCreateResponse
                        }
                    }
                }

                /** 查询 */
                val relationBuilder = relationPubService.select(
                    SFilter.eq("graphId", graph.graphId!!),
                    SFilter.eq("id", graphPub.id!!),
                    SFilter.eq("state", 1)
                )
                /** 设置表明，查询 */
                val relationList = relationBuilder.table("labsl.relation").exec()
                /** 列表长度大于 0 */
                if (relationList.size>0){
                    /** 遍历列表 */
                    for (relation in relationList){
                        /** 图编码  */
                        relation.id = graph.id
                        /** 图 id */
                        relation.graphId = graph.graphId
                        /** 已发布标志 */
                        relation.state = 0
                        /** 插入数据 */
                        val insert1 = relationPubService.insert(relation)
                        /** 插入是否成功 */
                        if (!insert1){
                            /** 回滚 */
                            backTransaction()
                            /** 返回标志 */
                            sCreateResponse.result = SResponseType.failure
                            /** 返回对象 */
                            return  sCreateResponse
                        }
                    }
                }

                /** 查询 */
                val anchorBuilder = anchorPubService.select(
                    SFilter.eq("graphId", graph.graphId!!),
                    SFilter.eq("id", graphPub.id!!),
                    SFilter.eq("state", 1)
                )
                /** 设置表明，查询 */
                val anchorList = anchorBuilder.table("labsl.anchor").exec()
                /** 列表长度大于 0 */
                if (anchorList.size>0){
                    /** 遍历列表 */
                    for (anchor in anchorList){
                        /** 图编码 */
                        anchor.id = graph.id
                        /** 图 id */
                        anchor.graphId = graph.graphId
                        /** 已发布图标志 */
                        anchor.state = 0
                        /** 插入数据 */
                        val insert1 = anchorPubService.insert(anchor)
                        /** 插入是否成功 */
                        if (!insert1){
                            /** 回滚 */
                            backTransaction()
                            /** 返回标志 */
                            sCreateResponse.result = SResponseType.failure
                            /** 返回对象 */
                            return  sCreateResponse
                        }
                    }
                }

                /** 清除草稿箱图 */
                delOldData(graph1)
                /** 提交事务 */
                commitTransaction()
                /** 对象列表 */
                sCreateResponse.entityList = arrayListOf(graph)
                /** 返回标志 */
                sCreateResponse.result = SResponseType.success
            }else{
                /** 返回标志 */
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "草稿箱中没有待发布图形"
            }

            /** 返回对象 */
            return sCreateResponse
        } catch (e: Exception) {
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 清除草稿箱以前数据
     *
     * @param  graph   图对象
     */
    private fun delOldData(graph: Graph) {
        logger.debug("保存图形：${graph.toJson()}")
        /** 清除以前的版本 */
        val entity = select(
            SFilter.eq("projectId", graph.projectId!!),
            SFilter.eq("id", graph.id!!),
            SFilter.eq("graphId", graph.graphId!!)
        ).entity()

        if (entity != null) {
            /** 清除之前的草稿记录 */
            val delete = delete(
                SFilter.eq("projectId", graph.projectId!!),
                SFilter.eq("id", graph.id!!),
                SFilter.eq("graphId", graph.graphId!!)
            )
            if (delete){
                /** 清除图下子集 */
                NodeService.delete(
                    SFilter.eq("id", graph.id!!),
                    SFilter.eq("graphId", graph.graphId!!))
                MarkersService.delete(
                    SFilter.eq("id", graph.id!!),
                    SFilter.eq("graphId", graph.graphId!!))
                RelationService.delete(
                    SFilter.eq("id", graph.id!!),
                    SFilter.eq("graphId", graph.graphId!!))
                AnchorService.delete(
                    SFilter.eq("id", graph.id!!),
                    SFilter.eq("graphId", graph.graphId!!))
            }
        }
    }

    /**
     * 草稿箱 - 清除草稿箱正在使用的草稿
     *
     * @param graph  图对象
     */
    private fun delOldDataTwo(graph: Graph) {
        /** 清除以前的版本 */
        val entity = select(
            SFilter.eq("projectId", graph.projectId!!),
            SFilter.eq("state", 1),
            SFilter.eq("graphId", graph.graphId!!)
        ).entity()

        /** 对象不为空 */
        if (entity != null) {
            /** 清除之前的草稿记录 */
            val delete = delete(
                SFilter.eq("projectId", graph.projectId!!),
                SFilter.eq("state", 1),
                SFilter.eq("graphId", graph.graphId!!)
            )

            /** 删除成功执行下面操作 */
            if (delete){
                /** 清除图下子集 */
                NodeService.delete(
                    SFilter.eq("state", 1),
                    SFilter.eq("graphId", graph.graphId!!))
                MarkersService.delete(
                    SFilter.eq("state", 1),
                    SFilter.eq("graphId", graph.graphId!!))
                RelationService.delete(
                    SFilter.eq("state", 1),
                    SFilter.eq("graphId", graph.graphId!!))
                AnchorService.delete(
                    SFilter.eq("state", 1),
                    SFilter.eq("graphId", graph.graphId!!))
            }
        }
    }

    /**
     * 清除已发布的老数据
     *
     * @param   graph   图对象
     */
    private fun delOldDataPub(graph: Graph) {
        /** 清除以前的版本 */
        val entity = GraphPubService.select(
            SFilter.eq("projectId", graph.projectId!!),
            SFilter.eq("id", graph.id!!),
            SFilter.eq("graphId", graph.graphId!!)
        ).entity()

        /** 对象不为空 */
        if (entity != null) {
            /** 清除之前的草稿记录 */
            val delete = GraphPubService.delete(
                SFilter.eq("projectId", graph.projectId!!),
                SFilter.eq("id", graph.id!!),
                SFilter.eq("graphId", graph.graphId!!)
            )

            /** 删除成功执行线面操作 */
            if (delete){
                /** 清除图下子集 */
                nodePubService.delete(
                    SFilter.eq("id", graph.id!!),
                    SFilter.eq("graphId", graph.graphId!!))
                markersPubService.delete(
                    SFilter.eq("id", graph.id!!),
                    SFilter.eq("graphId", graph.graphId!!))
                relationPubService.delete(
                    SFilter.eq("id", graph.id!!),
                    SFilter.eq("graphId", graph.graphId!!))
                anchorPubService.delete(
                    SFilter.eq("id", graph.id!!),
                    SFilter.eq("graphId", graph.graphId!!))
            }
        }
    }

    /**
     * 开启事务
     */
    private fun openTransaction() {

    }

    /**
     * 回滚事务
     */
    private fun backTransaction() {

    }

    /**
     * 提交事务
     */
    private fun commitTransaction() {

    }


    /**
     * 草稿图、移至回收站 或移出回收站
     *
     * @param idList    图对象列表
     * @return 操作结果
     */
    fun recycle(idList: ArrayList<Graph>): SBaseResponse {
        /** 返回对象 */
        val sBaseResponse = SBaseResponse()
        /** 列表不为空 */
        if (!idList.isNullOrEmpty()) {
            /** 遍历列表 */
            for (graph in idList) {
                /** 删除时间 */
                graph.delTime = time()
                /** 回收站标记 */
                graph.state = 4
                /** 更新字典列表*/
                val list = ArrayList<String>()
                list.add("delTime")
                list.add("state")
                /** 更新 */
                update(graph, list)
            }

            /** 返回标记 */
            sBaseResponse.result = SResponseType.success
        } else {
            /** 返回标记 */
            sBaseResponse.result = SResponseType.failure
        }
        return sBaseResponse
    }

    /**
     * 已发布图形移至回收站
     *
     * @param idList    图对象列表
     * @return  操作结果
     */
    fun recyclePub(idList: ArrayList<Graph>): SBaseResponse{
        /** 返回对象 */
        val sBaseResponse = SBaseResponse()
        /** 开启事务 */
        openTransaction()
        /** 列表不为空 */
        if (!idList.isNullOrEmpty()){
            /** 遍历列表 */
            for (graph in idList){
                /** 项目 id */
                graph.projectId = Opts.projectId
                /** 查询 */
                val graphBuilder = GraphService.select(
                    SFilter.eq("projectId", Opts.projectId!!),
                    SFilter.eq("id", graph.id!!),
                    SFilter.eq("graphId", graph.graphId!!)
                )
                /** 设置表明，查询 */
                val graphPub = graphBuilder.table("labsl.graph_pub").entity()
                /** 对象不为空 */
                if (graphPub!=null){
                    /** 图编码 */
                    graphPub.id = IdUtils.uuidCreate()
                    /** 回收站标志 */
                    graphPub.state = 4
                    graphPub.delTime = time()
                    /** 插入数据 */
                    val insert = GraphService.insert(graphPub)
                    /** 插入是否成功 */
                    if (!insert){
                        /** 回滚处理 */
                        backTransaction()
                        /** 返回标志 */
                        sBaseResponse.result = SResponseType.failure
                        /** 返回对象 */
                        return sBaseResponse
                    }

                    /** 查询 */
                    val nodeBuilder = NodeService.select(
                        SFilter.eq("id", graph.id!!),
                        SFilter.eq("graphId", graph.graphId!!)
                    )

                    /** 设置表明，查询 */
                    val nodeList = nodeBuilder.table("labsl.node_pub").exec()
                    /** 里边数量大于 0  */
                    if (nodeList.size>0){
                        /** 遍历列表 */
                        for (node in nodeList){
                            /** 图编码 */
                            node.id = graphPub.id
                            /** 回收站标志 */
                            node.state = 4
                            /** 插入数据 */
                            NodeService.insert(node)
                        }

                        /** 查询 */
                        val anchorBuilder = AnchorService.select(
                            SFilter.eq("id", graph.id!!),
                            SFilter.eq("graphId", graph.graphId!!)
                        )
                        /** 设置表明，查询 */
                        val anchorList = anchorBuilder.table("labsl.anchor_pub").exec()
                        /** 列表大于 0  */
                        if (anchorList.size>0){
                            /** 遍历列表 */
                            for (anchor in anchorList){
                                /** 图编码 */
                                anchor.id = graphPub.id
                                /** 回收站标志 */
                                anchor.state = 4
                                /** 插入数据 */
                                AnchorService.insert(anchor)
                            }
                        }
                    }

                    /** 查询 */
                    val markersBuilder = MarkersService.select(
                        SFilter.eq("id", graph.id!!),
                        SFilter.eq("graphId", graph.graphId!!)
                    )
                    /** 设置表明，查询 */
                    val markersList = markersBuilder.table("labsl.marker_pub").exec()
                    /** 列表大于 0  */
                    if (markersList.size>0){
                        /** 遍历列表 */
                        for (markers in markersList){
                            /** 图编码 */
                            markers.id = graphPub.id
                            /** 回收站标志 */
                            markers.state = 4
                            /** 插入数据 */
                            MarkersService.insert(markers)
                        }
                    }

                    /** 查询 */
                    val relationBuilder = RelationService.select(
                        SFilter.eq("id", graph.id!!),
                        SFilter.eq("graphId", graph.graphId!!)
                    )
                    /** 设置表明，查询 */
                    val relationList = relationBuilder.table("labsl.relation_pub").exec()
                    /** 列表大于 0  */
                    if (relationList.size>0){
                        /** 遍历列表 */
                        for (relation in relationList ){
                            /** 图编码 */
                            relation.id = graphPub.id
                            /** 回收站标志 */
                            relation.state = 4
                            /** 插入数据 */
                            RelationService.insert(relation)
                        }
                    }

                    /** 删除已发布的图形数据 */
                    delOldDataPub(graph)
                }
            }

            /** 返回标志 */
            sBaseResponse.result = SResponseType.success
        }else{
            /** 返回标志 */
            sBaseResponse.result = SResponseType.failure
        }
        return sBaseResponse
    }

    /**
     * 草稿箱图形数据读取所有节点
     *
     * @param graphs    图对象
     * @return 查询结果
     */
    fun readDrafts(graphs: Graph): SCommonResponse<Graph> {
        /** 返回对象 */
        val sQueryResponse = SCommonResponse<Graph>()
        /** 查询 */
        val graph = select(
            SFilter.eq("projectId", Opts.projectId!!),
            SFilter.eq("id", graphs.id!!),
            SFilter.eq("graphId", graphs.graphId!!)
        ).entity()
        /** 对象不为空，继续操作 */
        if (graph!=null){
            /** 查询节点 */
            val nodeList = NodeService.select(
                SFilter.eq("id", graph.id!!),
                SFilter.eq("graphId", graph.graphId!!)
            ).exec()
            /** 子列表数组 */
            val elements = Elements()
            /** 列表大于 0  */
            if (nodeList.size>0){
                /** 遍历列表 */
                for (node in nodeList){
                    try {
                        /** 查询 */
                        val anchorList = AnchorService.select(
                            SFilter.eq("id", graph.id!!),
                            SFilter.eq("graphId", graph.graphId!!),
                            SFilter.eq("nodeId", node.nodeId!!)
                        ).exec()
                        /** 列表大于 0  */
                        if (anchorList.size>0){
                            node.anchorList = anchorList
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
                /** 赋值 */
                elements.nodes = nodeList
            }

            /** 查询 */
            val markersList = MarkersService.select(
                SFilter.eq("id", graph.id!!),
                SFilter.eq("graphId", graph.graphId!!)
            ).exec()

            /** 查询管线 */
            val relationList = RelationService.select(
                SFilter.eq("id", graph.id!!),
                SFilter.eq("graphId", graph.graphId!!)
            ).exec()
            /** 列表大于 0 */
            if (relationList.size>0){
                elements.relations = relationList
            }

            /** 列表大于 0 */
            if (markersList.size>0){
                elements.markers = markersList
            }
            /** 赋值 */
            graph.elements = elements
            /** 标志*/
            sQueryResponse.result = SResponseType.success
            /** 对象 */
            sQueryResponse.content = graph
        }else{
            sQueryResponse.message = "图不存在"
            sQueryResponse.result = SResponseType.failure
        }
        /** 返回对象 */
        return sQueryResponse
    }

    /**
     * 已发布图形数据读取所有节点
     *
     * @param graphPub  图对象
     * @return 查询结果
     */
    fun readPub(graphPub: GraphPub): SCommonResponse<GraphPub>{
        /** 返回对象 */
        val sQueryResponse = SCommonResponse<GraphPub>()
        var projectId: String
        if (!Opts.projectId.isNullOrEmpty()){
            projectId = Opts.projectId!!
        }else if (!graphPub.projectId.isNullOrEmpty()) {
            projectId = graphPub.projectId!!
        }else {
            sQueryResponse.message = "项目id不可以为空"
            sQueryResponse.result = SResponseType.failure
            return  sQueryResponse
        }
        /** 查询 */
        val graph = GraphPubService.select(
            SFilter.eq("projectId", projectId),
            SFilter.eq("id", graphPub.id!!),
            SFilter.eq("graphId", graphPub.graphId!!)
        ).entity()
        /** 对象不为空 */
        if (graph!=null){
            /** 查询 */
            val nodeList = nodePubService.select(
                SFilter.eq("id", graph.id!!),
                SFilter.eq("graphId", graph.graphId!!)
            ).exec()
            /** 列表对象 */
            val elements = ElementsPub()
            /** 列表不为空 */
            if (nodeList.size>0){
                /** 遍历列表 */
                for (node in nodeList){
                    try {
                        /** 查询 */
                        val anchorList = anchorPubService.select(
                            SFilter.eq("id", graph.id!!),
                            SFilter.eq("graphId", graph.graphId!!),
                            SFilter.eq("nodeId", node.nodeId!!)
                        ).exec()
                        /** 列表大于 0 */
                        if (anchorList.size>0){
                            node.anchorList = anchorList
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }

                /** 赋值 */
                elements.nodes = nodeList
            }
            /**查询 */
            val markersList = markersPubService.select(
                SFilter.eq("id", graph.id!!),
                SFilter.eq("graphId", graph.graphId!!)
            ).exec()
            /** 查询 */
            val relationList = relationPubService.select(
                SFilter.eq("id", graph.id!!),
                SFilter.eq("graphId", graph.graphId!!)
            ).exec()
            /** 管线列表数量大于 0  */
            if (relationList.size>0){
                elements.relations = relationList
            }

            /** 列表数量大于 0 */
            if (markersList.size>0){
                elements.markers = markersList
            }
            /** 列表赋值 */
            graph.elements = elements
            /** 返回标志 */
            sQueryResponse.result = SResponseType.success
            /** 对象 */
            sQueryResponse.content = graph
        }else{
            /** 返回标志 */
            sQueryResponse.result = SResponseType.failure
        }

        /** 返回结果 */
        return sQueryResponse
    }

    /**
     * 回收站-恢复
     *
     * @param graphs    图对象
     * @return 处理结果
     */
    fun recovery(graphs: Graph): SBaseResponse{
        /** 项目 id */
        graphs.projectId = Opts.projectId
        /** 查询图 */
        val graph = select(
            SFilter.eq("projectId", Opts.projectId!!),
            SFilter.eq("id", graphs.id!!),
            SFilter.eq("graphId", graphs.graphId!!)
        ).entity()
        /** 图对象不为空 */
        if (graph!=null){
            /** 查询 */
            val markersList = MarkersService.select(
                SFilter.eq("id", graphs.id!!),
                SFilter.eq("graphId", graphs.graphId!!)
            ).exec()
            /** 查询 */
            val nodeList = NodeService.select(
                SFilter.eq("id", graphs.id!!),
                SFilter.eq("graphId", graphs.graphId!!)
            ).exec()
            /** 查询 */
            val anchorList = AnchorService.select(
                SFilter.eq("id", graphs.id!!),
                SFilter.eq("graphId", graphs.graphId!!)
            ).exec()
            /** 查询 */
            val attachObjectIdsList = AttachObjectIdsServices.select(
                SFilter.eq("id", graphs.id!!),
                SFilter.eq("graphId", graphs.graphId!!)
            ).exec()
            /** 查询 */
            val relationList = RelationService.select(
                SFilter.eq("id", graphs.id!!),
                SFilter.eq("graphId", graphs.graphId!!)
            ).exec()
            /** 清除草稿箱数据 */
            delOldDataTwo(graphs)
            /** 列表 */
            val listState = ArrayList<String>()
            listState.add("State")
            /** 草稿箱标志 */
            graph.state = 1
            /** 更新 */
            update(graph,listState)
            /** 列表不为空 */
            if (!markersList.isNullOrEmpty()){
                /** 遍历列表 */
                for (markers in markersList){
                    /** 草稿箱标志 */
                    markers.state = 1
                    /** 更新数据 */
                    MarkersService.update(markers,listState)
                }
            }

            /** 列表不为空 */
            if (!nodeList.isNullOrEmpty()){
                /** 遍历列表 */
                for (node in nodeList){
                    /** 回收站标志 */
                    node.state = 1
                    /** 更新 */
                    NodeService.update(node,listState)
                }
            }

            /** 列表不为空 */
            if (!anchorList.isNullOrEmpty()){
                /** 遍历列表 */
                for (anchor in anchorList){
                    /** 草稿箱标志 */
                    anchor.state = 1
                    /** 更新 */
                    AnchorService.update(anchor,listState)
                }
            }

            /** 列表不为空 */
            if (!attachObjectIdsList.isNullOrEmpty()){
                /** 遍历列表 */
                for (attachObjectIds in attachObjectIdsList){
                    /** 草稿箱标志 */
                    attachObjectIds.state = 1
                    /** 更新 */
                    AttachObjectIdsServices.update(attachObjectIds,listState)
                }
            }

            /** 列表不为空 */
            if (!relationList.isNullOrEmpty()){
                /** 遍历列表 */
                for (relation in relationList){
                    /** 回收站标志 */
                    relation.state = 1
                    /** 更新 */
                    RelationService.update(relation,listState)
                }
            }
        }
        /** 返回操作状态 */
        return SBaseResponse(SResponseType.success)
    }

    /**
     * 查询未发布的
     *
     * @param label    标签
     */
    fun containLabelPub(label: String): SQueryResponse<GraphPub> {
         val sQueryResponse = SQueryResponse<GraphPub>(SResponseType.success)
        val containLabelPub = customMapper.containLabelPub(Opts.projectId!!, label)
        if (!containLabelPub.isNullOrEmpty()) {
            sQueryResponse.content = containLabelPub
            sQueryResponse.total = containLabelPub.size.toLong()
        }else {
            sQueryResponse.content = ArrayList<GraphPub>()
        }
        return sQueryResponse
    }



    /**
     * 当前时间
     *
     * @return 当前时间
     */
    private fun time():Date {
        val df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //设置日期格式
        println(df.format(Date())) // new Date()为获取当前系统时间
        val format = df.format(Date())
        return df.parse(format)
    }

}