package com.persagy.server.labsl.services

import cn.sagacloud.server.datacenter.utils.IdUtils
import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.labsl.models.entities.Elements
import com.persagy.server.labsl.models.entities.Graph
import com.persagy.server.labsl.models.entities.pub.*
import com.persagy.server.labsl.models.response.SCommonResponse
import com.persagy.server.labsl.services.pub.GraphPubService
import com.persagy.service.SObjectService
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
import org.slf4j.LoggerFactory
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

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

    /** 日志 */
    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))
    /** 图例节点与对象的关系 */
    private val attachObjectIdsPubService = SObjectService(SMybatisDao(AttachObjectIdsPub::class.java))

    /**
     * 创建之前回调
     */
    override fun onCreateBefore(entity: Graph): Boolean {
        /** 项目id */
        val projectId = SPageContext.getHeader("projectId")
        // id 赋值
        if (entity.id.isNullOrEmpty()){
            entity.id = IdUtils.uuidCreate()
        }
        if (entity.graphId.isNullOrEmpty()){
            entity.graphId = IdUtils.uuidCreate()
        }
        if (entity.version.isNullOrEmpty())
            entity.version = "2.0.1"

        if (entity.projectId.isNullOrEmpty()){
            entity.projectId = projectId
        }
        return super.onCreateBefore(entity)
    } // Fun onCreateBefore()

    /**
     * 创建图
     * @param   graph   图对象
     */
    fun save(graph: Graph): SCreateResponse<Graph> {
        logger.debug("保存图形：${graph.toJson()}")
        val sCreateResponse = SCreateResponse<Graph>()
        /** 项目id */
        val projectId = SPageContext.getHeader("projectId")
        //开启事务
        openTransaction()
        // 未发布
        if (graph.id.isNullOrEmpty()) {
            // id赋值
            graph.id = UUID.randomUUID().toString().replace("-", "")
        }
        if (graph.graphId.isNullOrEmpty()){
            graph.graphId = UUID.randomUUID().toString().replace("-", "")
        }
        if (graph.projectId.isNullOrEmpty()){
            graph.projectId = projectId
        }

        /** 清除以前的版本 */
        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
        }
        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) {
                    nodes.graphId = graph.graphId
                    nodes.id = graph.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) {
                            anchor.graphId = graph.graphId
                            anchor.id = graph.id
                            anchor.nodeId = nodes.id
                            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 attachObjectIdsList = nodes.attachObjectIds
                    if (!attachObjectIdsList.isNullOrEmpty()){
                        for (attachObjectIds in attachObjectIdsList){
                            attachObjectIds.graphId = graph.graphId
                            attachObjectIds.id = graph.id
                            AttachObjectIdsServices.insert(attachObjectIds)
                        }
                    }
                }
            }
            val markersList = graph.elements!!.markers
            if (!markersList.isNullOrEmpty()) {
                for (markers in markersList) {
                    markers.graphId = graph.graphId
                    markers.id = graph.id
                    markers.state = 1
                    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) {
                    relations.graphId = graph.graphId
                    relations.state = 1
                    relations.relationId = graph.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
    } // Fun save（）

    /**
     *  回收站和草稿箱在一个表中，导致同一个图会有多个在回收站中需要配备多主键
     * 发布图形
     */
    fun pubDrawings(graphPub: GraphPub): SCreateResponse<GraphPub> {
        val sCreateResponse = SCreateResponse<GraphPub>()
        /** 项目id */
        val projectId = SPageContext.getHeader("projectId")
        val graph1 = Graph()
        graph1.projectId = projectId
        graph1.id = graphPub.id
        graph1.graphId = graphPub.graphId
        /** 查询草稿箱 */
        val squeryBuilder = GraphPubService.select(
            SFilter.eq("projectId", 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(".")
            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("projectId", projectId!!),
                SFilter.eq("graphId", graph.graphId!!),
                SFilter.eq("id", graphPub.id!!),
                SFilter.eq("state", 1)
            )
            val nodePubList = nodebuilder.table("labsl.node_pub").exec()
            if (nodePubList.size>0){
                for (nodePub in nodePubList){
                    nodePub.id = graph.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("projectId", projectId!!),
                SFilter.eq("graphId", graph.graphId!!),
                SFilter.eq("id", graphPub.id!!),
                SFilter.eq("state", 1)
            )
            val markersPubList = markersbuilder.table("labsl.marker_pub").exec()
            if (markersPubList.size>0){
                for (markersPub in markersPubList){
                    markersPub.id = graph.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("projectId", projectId!!),
                SFilter.eq("graphId", graph.graphId!!),
                SFilter.eq("id", graphPub.id!!),
                SFilter.eq("state", 1)
            )
            val relationList = relationBuilder.table("labsl.relation_pub").exec()
            if (relationList.size>0){
                for (relation in relationList){
                    relation.id = graph.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("projectId", projectId!!),
                SFilter.eq("graphId", graph.graphId!!),
                SFilter.eq("id", graphPub.id!!),
                SFilter.eq("state", 1)
            )
            val anchorList = anchorBuilder.table("labsl.anchor_pub").exec()
            if (anchorList.size>0){
                for (anchor in anchorList){
                    anchor.id = graph.id
                    anchor.graphId = graph.graphId
                    anchor.state = 0
                    val insert1 = anchorPubService.insert(anchor)
                    if (!insert1){
                        // 回滚
                        backTransaction()
                        sCreateResponse.result = SResponseType.failure
                        return  sCreateResponse
                    }
                }
            }

            val attachObjectIdsBuilder = attachObjectIdsPubService.select(
//                SFilter.eq("projectId", projectId!!),
                SFilter.eq("graphId", graph.graphId!!),
                SFilter.eq("id", graphPub.id!!),
                SFilter.eq("state", 1)
            )
            val attachObjectIdsList = attachObjectIdsBuilder.table("labsl.attach_object_ids_pub").exec()
            if (attachObjectIdsList.size>0){
                for (attachObjectIds in attachObjectIdsList){
                    attachObjectIds.id = graph.id
                    attachObjectIds.graphId = graph.graphId
                    attachObjectIds.state = 0
                    val insert1 = attachObjectIdsPubService.insert(attachObjectIds)
                    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
    } // Fun pubDrawings()

    /**
     * 清除草稿箱以前数据
     *
     * @param   graph   对象
     */
    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!!))
                AttachObjectIdsServices.delete(
                    SFilter.eq("id", graph.id!!),
                    SFilter.eq("graphId", graph.graphId!!))
            }
        }
    } // Fun delOldData（）

    /**
     * 草稿箱 - 清除草稿箱正在使用的草稿
     */
    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!!))
                AttachObjectIdsServices.delete(
                    SFilter.eq("state", 1),
                    SFilter.eq("graphId", graph.graphId!!))
            }
        }
    } // Fun delOldData（）

    /**
     * 清除已发布的老数据
     *
     * @param   graph   对象
     */
    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!!))
            }
        }
    } // Fun delOldDataPub()

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

    } // Fun openTransaction()

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

    } // Fun backTransaction()

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

    } // Fun commitTransaction()


    /**
     * 草稿图、移至回收站 或移出回收站
     */
    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
    } // Fun recycle()

    /**
     * 已发布图形移至回收站
     */
    fun recyclePub(idList: ArrayList<Graph>): SBaseResponse{
        val sBaseResponse = SBaseResponse()
        /** 项目id */
        val projectId = SPageContext.getHeader("projectId")

        if (!idList.isNullOrEmpty()){
            for (graph in idList){
                val graphBuilder = GraphService.select(
                    SFilter.eq("projectId", 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
                    val insert = GraphService.insert(graphPub)
                    if (!insert){

                    }
                    val nodeBuilder = NodeService.select(
                        SFilter.eq("id", graph.id!!),
                        SFilter.eq("graphId", graph.graphId!!)
                    )
                    val nodeList = nodeBuilder.table("labsl.node_pub").exec()
                    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()
                        if (anchorList.size>0){
                            for (anchor in anchorList){
                                anchor.id = graphPub.id
                                anchor.state = 4
                                AnchorService.insert(anchor)
                            }
                        }
                        val attachObjectIdsBuilder = AttachObjectIdsServices.select(
                            SFilter.eq("id", graph.id!!),
                            SFilter.eq("graphId", graph.graphId!!)
                        )
                        val attachObjectIdsList = attachObjectIdsBuilder.table("labsl.attach_object_ids_pub").exec()
                        if (attachObjectIdsList.size>0){
                            for (attachObjectIds in attachObjectIdsList){
                                attachObjectIds.id = graphPub.id
                                attachObjectIds.state = 4
                                AttachObjectIdsServices.insert(attachObjectIds)
                            }
                        }
                    }

                    val markersBuilder = MarkersService.select(
                        SFilter.eq("id", graph.id!!),
                        SFilter.eq("graphId", graph.graphId!!)
                    )
                    val markersList = markersBuilder.table("labsl.marker_pub").exec()
                    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()
                    if (relationList.size>0){
                        for (relation in relationList ){
                            relation.id = graphPub.id
                            relation.state = 4
                            RelationService.insert(relation)
                        }
                    }
                    delOldDataPub(graphPub)
                }

            }
            sBaseResponse.result = SResponseType.success
        }else{
            sBaseResponse.result = SResponseType.failure
        }
        return sBaseResponse
    } // Fun recyclePub()

    /**
     * 草稿箱图形数据读取所有节点
     */
    fun readDrafts(graphs: Graph): SCommonResponse<Graph> {
        val sQueryResponse = SCommonResponse<Graph>()
        /** 项目id */
        val projectId = SPageContext.getHeader("projectId")
        val graph = select(
            SFilter.eq("projectId", 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()
            if (nodeList.size>0){
                for (node in nodeList){
                    try {
                        val attachObjectIdsList = AttachObjectIdsServices.select(
                            SFilter.eq("id", graph.id!!),
                            SFilter.eq("graphId", graph.graphId!!),
                            SFilter.eq("nodeId", node.nodeId!!)
                        ).exec()
                        if (attachObjectIdsList.size>0){
                            node.attachObjectIds = attachObjectIdsList
                        }
                        val anchorList = AnchorService.select(
                            SFilter.eq("id", graph.id!!),
                            SFilter.eq("graphId", graph.graphId!!),
                            SFilter.eq("nodeId", node.nodeId!!)
                        ).exec()
                        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()
            if (relationList.size>0){
                elements.relations = relationList
            }
            if (markersList.size>0){
                elements.markers = markersList
            }
            graph.elements = elements
            sQueryResponse.result = SResponseType.failure
            sQueryResponse.content = graph
        }else{
            sQueryResponse.result = SResponseType.failure
        }
        return sQueryResponse
    } // Fun readDrafts()

    /**
     * 已发布图形数据读取所有节点
     */
    fun readPub(graphPub: GraphPub): SCommonResponse<GraphPub>{
        val sQueryResponse = SCommonResponse<GraphPub>()
        /** 项目id */
        val projectId = SPageContext.getHeader("projectId")
        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 attachObjectIdsList = attachObjectIdsPubService.select(
                            SFilter.eq("id", graph.id!!),
                            SFilter.eq("graphId", graph.graphId!!),
                            SFilter.eq("nodeId", node.nodeId!!)
                        ).exec()
                        if (attachObjectIdsList.size>0){
                            node.attachObjectIds = attachObjectIdsList
                        }
                        val anchorList = anchorPubService.select(
                            SFilter.eq("id", graph.id!!),
                            SFilter.eq("graphId", graph.graphId!!),
                            SFilter.eq("nodeId", node.nodeId!!)
                        ).exec()
                        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()
            if (relationList.size>0){
                elements.relations = relationList
            }
            if (markersList.size>0){
                elements.markers = markersList
            }
            graph.elements = elements
            sQueryResponse.result = SResponseType.failure
            sQueryResponse.content = graph
        }else{
            sQueryResponse.result = SResponseType.failure
        }
        return sQueryResponse
    } // Fun readPub()

    /**
     * 回收站-恢复
     */
    fun recovery(graphs: Graph): SBaseResponse{

        /** 项目id */
        val projectId = SPageContext.getHeader("projectId")
        graphs.projectId = projectId
        val graph = select(
            SFilter.eq("projectId", 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)
    } // Fun recovery()


    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)
    }

} // Object GraphService