package com.persagy.labsl.service.impl

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.collection.CollUtil
import cn.hutool.core.util.ObjectUtil
import cn.hutool.core.util.StrUtil
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.persagy.labsl.Opts
import com.persagy.labsl.domain.*
import com.persagy.labsl.mapper.GraphMapper
import com.persagy.labsl.service.*
import com.persagy.labsl.utils.IdUtils
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 lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.*
import kotlin.collections.ArrayList

/**
 *
 */
@Service
@Slf4j
open class GraphServiceImpl : ServiceImpl<GraphMapper?, GraphEntity?>(), GraphService{

    @Autowired
    lateinit var nodeService: NodeService
    @Autowired
    lateinit var markerService: MarkerService
    @Autowired
    lateinit var relationService: RelationService
    @Autowired
    lateinit var anchorService: AnchorService
    @Autowired
    lateinit var graphPubService: GraphPubService
    @Autowired
    lateinit var nodePubService: NodePubService
    @Autowired
    lateinit var markerPubService: MarkerPubService
    @Autowired
    lateinit var relationPubService: RelationPubService
    @Autowired
    lateinit var anchorPubService: AnchorPubService
    @Autowired
    lateinit var attachObjectIdsService: AttachObjectIdsService
    /**
     * 创建草稿图
     * @author : lijie
     * Update By 2022/6/7 14:41
     */
    @Transactional
    override fun draftsCreateLogic(request: SCreateRequest<GraphEntity>): SCreateResponse<GraphEntity> {
        val sCreateResponse = SCreateResponse<GraphEntity>(SResponseType.success)
        if (request.content.isNullOrEmpty()){
            sCreateResponse.message = "不可以为空"
            return sCreateResponse
        }
        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 = list(KtQueryWrapper(GraphEntity())
                    .eq(StrUtil.isNotBlank(Opts.projectId),GraphEntity::projectId, Opts.projectId!!)
                    .eq(StrUtil.isNotBlank(content.categoryId),GraphEntity::categoryId, content.categoryId!!)
                    .eq(StrUtil.isNotBlank(content.name),GraphEntity::name, content.name!!))
            if (CollUtil.isNotEmpty(entity)) {
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "同一分类下图名称不可以重复"
                return sCreateResponse
            }
            val entity1 = graphPubService.list(KtQueryWrapper(GraphPubEntity())
                    .eq(StrUtil.isNotBlank(Opts.projectId),GraphPubEntity::projectId, Opts.projectId!!)
                    .eq(StrUtil.isNotBlank(content.categoryId),GraphPubEntity::categoryId, content.categoryId!!)
                    .eq(StrUtil.isNotBlank(content.name),GraphPubEntity::name, content.name!!))
            if (CollUtil.isNotEmpty(entity1)) {
                sCreateResponse.result = SResponseType.failure
                sCreateResponse.message = "同一分类下已发布图名称已存在"
                return sCreateResponse
            }
            save(content)
        }
        sCreateResponse.entityList = request.content
        return sCreateResponse
    }
    /**
     * 保存草稿图
     * @param graph   图对象
     * @return 图形对象列表
     */
    @Transactional
    override fun saveLogic(graph: GraphEntity): SCreateResponse<GraphEntity> {
        val sCreateResponse = SCreateResponse<GraphEntity>()
            /** 未发布 */
            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!!

            /** 清除以前的版本 */
            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
            /** 插入图形数据 */
            save(graph)
            /** 列表不能为空 */
            saveElements(graph)
            /** 返回标志 */
            sCreateResponse.result = SResponseType.success
            /** 返回对象列表 */
            sCreateResponse.entityList = arrayListOf(graph)
            /** 返回对象 */
            return sCreateResponse
    }
    /**
     * 发布草稿
     * @author : lijie
     * Update By 2022/6/7 14:49
     */
    @Transactional
    override fun pubDrawingsLogic(graphPub: GraphPubEntity): SCreateResponse<GraphPubEntity> {
        /** 返回对象 */
        val sCreateResponse = SCreateResponse<GraphPubEntity>()
        /** 图对象 */
        val graph1 = GraphEntity()
        /** 项目 id */
        graph1.projectId = Opts.projectId!!
        /** 图编码 */
        graph1.id = graphPub.id
        /** 图 id */
        graph1.graphId = graphPub.graphId
        /** 设置表明，查询 */
        val queryWrapper: KtQueryWrapper<GraphEntity> = KtQueryWrapper(GraphEntity())
        queryWrapper.eq(StrUtil.isNotBlank(Opts.projectId), GraphEntity::projectId, Opts.projectId)
                .eq(StrUtil.isNotBlank(graphPub.id), GraphEntity::id, graphPub.id)
                .eq(StrUtil.isNotBlank(graphPub.graphId), GraphEntity::graphId, graphPub.graphId)
                .eq(GraphEntity::state, 1)
        val graphEntities = list(queryWrapper)
        if (CollUtil.isEmpty(graphEntities)){
            /** 返回标志 */
            sCreateResponse.result = SResponseType.failure
            sCreateResponse.message = "草稿箱中没有待发布图形"
            /** 返回对象 */
            return sCreateResponse
        }
        val graphEntity = graphEntities.get(0)
        val graph = BeanUtil.copyProperties(graphEntity, GraphPubEntity::class.java)
        /** 清除已发布的老数据 */
        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
        graphPubService.save(graph)
        /** 设置表明，查询 */
        val nodeList = nodeService.list(KtQueryWrapper(NodeEntity())
                .eq(StrUtil.isNotBlank(graphPub.id),NodeEntity::id,graphPub.id!!)
                .eq(StrUtil.isNotBlank(graphPub.graphId),NodeEntity::graphId,graph.graphId!!)
                .eq(NodeEntity::state,1))
        /** 列表长度大于 0 */
        if (CollUtil.isNotEmpty(nodeList)){
            log.debug("对象长度 = ${nodeList.size}   ******************************************************")
            val nodePubList = ArrayList<NodePubEntity>()
            /** 遍历列表 */
            for (node in nodeList){
                val nodePub = BeanUtil.copyProperties(node, NodePubEntity::class.java)
                /** 图编码 */
                nodePub.id = graph.id
                /** 图 id */
                nodePub.graphId = graph.graphId
                /** 发布标志 */
                nodePub.state = 0
                nodePubList.add(nodePub)
            }
            /** 插入数据 */
            nodePubService.saveBatch(CollUtil.newArrayList(nodePubList))
        }
        /** 设置表明，查询 */
        val markersList = markerService.list(KtQueryWrapper(MarkerEntity())
                .eq(StrUtil.isNotBlank(graphPub.id),MarkerEntity::id,graphPub.id!!)
                .eq(StrUtil.isNotBlank(graphPub.graphId),MarkerEntity::graphId,graph.graphId!!)
                .eq(MarkerEntity::state,1))
        /** 列表大于 0 */
        if (CollUtil.isNotEmpty(markersList)){
            val markerPubList = ArrayList<MarkerPubEntity>()
            /** 遍历列表 */
            for (markers in markersList){
                val markersPub = BeanUtil.copyProperties(markers, MarkerPubEntity::class.java)
                /** 图编码 */
                markersPub.id = graph.id
                /** 图 id */
                markersPub.graphId = graph.graphId
                /** 发布标志 */
                markersPub.state = 0
                markerPubList.add(markersPub)
            }
            markerPubService.saveBatch(CollUtil.newArrayList(markerPubList))
        }
        /** 设置表明，查询 */
        val relationList = relationService.list(KtQueryWrapper(RelationEntity())
                .eq(StrUtil.isNotBlank(graphPub.id),RelationEntity::id,graphPub.id!!)
                .eq(StrUtil.isNotBlank(graphPub.graphId),RelationEntity::graphId,graph.graphId!!)
                .eq(RelationEntity::state,1))
        /** 列表长度大于 0 */
        if (CollUtil.isNotEmpty(relationList)){
            val relationPubList = ArrayList<RelationPubEntity>()
            /** 遍历列表 */
            for (relation in relationList){
                val relationPub = BeanUtil.copyProperties(relation, RelationPubEntity::class.java)
                /** 图编码  */
                relationPub.id = graph.id
                /** 图 id */
                relationPub.graphId = graph.graphId
                /** 已发布标志 */
                relationPub.state = 0
                relationPubList.add(relationPub)
            }
            relationPubService.saveBatch(CollUtil.newArrayList(relationPubList))
        }

        /** 设置表明，查询 */
        val anchorList = anchorService.list(KtQueryWrapper(AnchorEntity())
                .eq(StrUtil.isNotBlank(graphPub.id),AnchorEntity::id,graphPub.id!!)
                .eq(StrUtil.isNotBlank(graphPub.graphId),AnchorEntity::graphId,graph.graphId!!)
                .eq(AnchorEntity::state,1))
        /** 列表长度大于 0 */
        if (CollUtil.isNotEmpty(anchorList)){
            val anchorPubList = ArrayList<AnchorPubEntity>()
            /** 遍历列表 */
            for (anchor in anchorList){
                val anchorPub = BeanUtil.copyProperties(anchor, AnchorPubEntity::class.java)
                /** 图编码 */
                anchorPub.id = graph.id
                /** 图 id */
                anchorPub.graphId = graph.graphId
                /** 已发布图标志 */
                anchorPub.state = 0
                anchorPubList.add(anchorPub)
            }
            anchorPubService.saveBatch(CollUtil.newArrayList(anchorPubList))
        }
        /** 清除草稿箱图 */
        delOldData(graph1)
        /** 对象列表 */
        sCreateResponse.entityList = arrayListOf(graph)
        /** 返回标志 */
        sCreateResponse.result = SResponseType.success
        /** 返回对象 */
        return sCreateResponse
    }
    /**
     * 图、移至或移出回收站
     * @author : lijie
     * Update By 2022/6/7 15:39
     */
    @Transactional
    override fun recycleLogic(idList: ArrayList<GraphEntity>): SBaseResponse {
        /** 返回对象 */
        val sBaseResponse = SBaseResponse()
        if (idList.isNullOrEmpty()){
            /** 返回标记 */
            sBaseResponse.result = SResponseType.failure
            return sBaseResponse
        }
        /** 遍历列表 */
        for (graph in idList) {
            /** 删除时间 */
            graph.delTime = Date()
            /** 回收站标记 */
            graph.state = 4
        }
        updateBatchById(CollUtil.newArrayList(idList))
        /** 返回标记 */
        sBaseResponse.result = SResponseType.success
        return sBaseResponse
    }
    /**
     * 已发布图形移至回收站
     * @author : lijie
     * Update By 2022/6/7 15:48
     */
    @Transactional
    override fun recyclePubLogic(idList: java.util.ArrayList<GraphEntity>): SBaseResponse {
        /** 返回对象 */
        val sBaseResponse = SBaseResponse()
        if (idList.isNullOrEmpty()){
            /** 返回标志 */
            sBaseResponse.result = SResponseType.failure
            return sBaseResponse
        }
        /** 遍历列表 */
        for (graph in idList){
            /** 项目 id */
            graph.projectId = Opts.projectId
            /** 设置表明，查询 */
            val graphPub = graphPubService.getOne(KtQueryWrapper(GraphPubEntity())
                    .eq(StrUtil.isNotBlank(Opts.projectId),GraphPubEntity::projectId,Opts.projectId!!)
                    .eq(StrUtil.isNotBlank(graph.id),GraphPubEntity::id,graph.id!!)
                    .eq(StrUtil.isNotBlank(graph.graphId),GraphPubEntity::graphId,graph.graphId!!))
            if (ObjectUtil.isNull(graphPub)){
                continue
            }
            val graphEntity = BeanUtil.copyProperties(graphPub, GraphEntity::class.java)
            /** 图编码 */
            graphEntity.id = IdUtils.uuidCreate()
            /** 回收站标志 */
            graphEntity.state = 4
            graphEntity.delTime = Date()
            save(graphEntity)
            /** 设置表明，查询 */
            val nodePubList = nodePubService.list(KtQueryWrapper(NodePubEntity())
                    .eq(StrUtil.isNotBlank(graph.id),NodePubEntity::id,graph.id!!)
                    .eq(StrUtil.isNotBlank(graph.graphId),NodePubEntity::graphId,graph.graphId!!))
            /** 里边数量大于 0  */
            if (CollUtil.isNotEmpty(nodePubList)){
                val nodeList = ArrayList<NodeEntity>()
                /** 遍历列表 */
                for (nodePub in nodePubList){
                    val node = BeanUtil.copyProperties(nodePub, NodeEntity::class.java)
                    /** 图编码 */
                    node.id = graphEntity.id
                    /** 回收站标志 */
                    node.state = 4
                    nodeList.add(node)
                }
                nodeService.saveBatch(CollUtil.newArrayList(nodeList))
                /** 设置表明，查询 */
                val anchorPubList = anchorPubService.list(KtQueryWrapper(AnchorPubEntity())
                        .eq(StrUtil.isNotBlank(graph.id),AnchorPubEntity::id,graph.id!!)
                        .eq(StrUtil.isNotBlank(graph.graphId),AnchorPubEntity::graphId,graph.graphId!!))
                /** 列表大于 0  */
                if (CollUtil.isNotEmpty(anchorPubList)){
                    val anchorList = ArrayList<AnchorEntity>()
                    /** 遍历列表 */
                    for (anchorPub in anchorPubList){
                        val anchor = BeanUtil.copyProperties(anchorPub, AnchorEntity::class.java)
                        /** 图编码 */
                        anchor.id = graphEntity.id
                        /** 回收站标志 */
                        anchor.state = 4
                        anchorList.add(anchor)
                    }
                    anchorService.saveBatch(CollUtil.newArrayList(anchorList))
                }
            }
            /** 设置表明，查询 */
            val markerPubList = markerPubService.list(KtQueryWrapper(MarkerPubEntity())
                    .eq(StrUtil.isNotBlank(graph.id),MarkerPubEntity::id,graph.id!!)
                    .eq(StrUtil.isNotBlank(graph.graphId),MarkerPubEntity::graphId,graph.graphId!!))
            /** 列表大于 0  */
            if (CollUtil.isNotEmpty(markerPubList)){
                val markerList = ArrayList<MarkerEntity>()
                /** 遍历列表 */
                for (markerPub in markerPubList){
                    val marker = BeanUtil.copyProperties(markerPub, MarkerEntity::class.java)
                    /** 图编码 */
                    marker.id = graphEntity.id
                    /** 回收站标志 */
                    marker.state = 4
                    markerList.add(marker)
                }
                markerService.saveBatch(CollUtil.newArrayList(markerList))
            }
            /** 设置表明，查询 */
            val relationPubList = relationPubService.list(KtQueryWrapper(RelationPubEntity())
                    .eq(StrUtil.isNotBlank(graph.id),RelationPubEntity::id,graph.id!!)
                    .eq(StrUtil.isNotBlank(graph.graphId),RelationPubEntity::graphId,graph.graphId!!))
            /** 列表大于 0  */
            if (CollUtil.isNotEmpty(relationPubList)){
                val relationList = ArrayList<RelationEntity>()
                /** 遍历列表 */
                for (relationPub in relationPubList){
                    val relation = BeanUtil.copyProperties(relationPub, RelationEntity::class.java)
                    /** 图编码 */
                    relation.id = graphEntity.id
                    /** 回收站标志 */
                    relation.state = 4
                    relationList.add(relation)
                }
                relationService.saveBatch(CollUtil.newArrayList(relationList))
            }
            /** 删除已发布的图形数据 */
            delOldDataPub(graph)
        }
        /** 返回标志 */
        sBaseResponse.result = SResponseType.success
        return sBaseResponse
    }
    /**
     * 更新草稿
     * @author : lijie
     * Update By 2022/6/7 16:28
     */
    @Transactional
    override fun draftsUpdateLogic(request: SUpdateRequest<GraphEntity>): SBaseResponse {
        val sBaseResponse = SBaseResponse(SResponseType.success)
        if (request.content.isNullOrEmpty()){
            return sBaseResponse
        }
        for (content in request.content!!) {
            val entity = list(KtQueryWrapper(GraphEntity())
                    .eq(StrUtil.isNotBlank(Opts.projectId),GraphEntity::projectId, Opts.projectId!!)
                    .eq(StrUtil.isNotBlank(content.categoryId),GraphEntity::categoryId, content.categoryId!!)
                    .eq(StrUtil.isNotBlank(content.name),GraphEntity::name, content.name!!)
                    .ne(StrUtil.isNotBlank(content.graphId),GraphEntity::graphId, content.graphId))
            if (CollUtil.isNotEmpty(entity)) {
                sBaseResponse.result = SResponseType.failure
                sBaseResponse.message = "图名称已经存在"
                return sBaseResponse
            }
            val entity1 = graphPubService.list(KtQueryWrapper(GraphPubEntity())
                    .eq(StrUtil.isNotBlank(Opts.projectId),GraphPubEntity::projectId, Opts.projectId!!)
                    .eq(StrUtil.isNotBlank(content.categoryId),GraphPubEntity::categoryId, content.categoryId!!)
                    .eq(StrUtil.isNotBlank(content.name),GraphPubEntity::name, content.name!!)
                    .ne(StrUtil.isNotBlank(content.graphId),GraphPubEntity::graphId, content.graphId))
            if (CollUtil.isNotEmpty(entity1)) {
                sBaseResponse.result = SResponseType.failure
                sBaseResponse.message = "同一分类下已发布图名称已存在"
                return sBaseResponse
            }
        }
        updateBatchById(CollUtil.newArrayList(request.content))
        return sBaseResponse
    }
    /**
     * 更新已发布图形
     * @author : lijie
     * Update By 2022/6/7 17:57
     */
    @Transactional
    override fun pubUpdateLogic(request: SUpdateRequest<GraphPubEntity>): SBaseResponse {
        val sBaseResponse = SBaseResponse(SResponseType.success)
        if (request.content.isNullOrEmpty()){
            return sBaseResponse
        }
        for (content in request.content!!) {
            val entity = list(KtQueryWrapper(GraphEntity())
                    .eq(StrUtil.isNotBlank(Opts.projectId),GraphEntity::projectId, Opts.projectId!!)
                    .eq(StrUtil.isNotBlank(content.categoryId),GraphEntity::categoryId, content.categoryId!!)
                    .eq(StrUtil.isNotBlank(content.name),GraphEntity::name, content.name!!)
                    .ne(StrUtil.isNotBlank(content.graphId),GraphEntity::graphId, content.graphId))
            if (CollUtil.isNotEmpty(entity)) {
                sBaseResponse.result = SResponseType.failure
                sBaseResponse.message = "图名称已经存在"
                return sBaseResponse
            }
            val entity1 = graphPubService.list(KtQueryWrapper(GraphPubEntity())
                    .eq(StrUtil.isNotBlank(Opts.projectId),GraphPubEntity::projectId, Opts.projectId!!)
                    .eq(StrUtil.isNotBlank(content.categoryId),GraphPubEntity::categoryId, content.categoryId!!)
                    .eq(StrUtil.isNotBlank(content.name),GraphPubEntity::name, content.name!!)
                    .ne(StrUtil.isNotBlank(content.graphId),GraphPubEntity::graphId, content.graphId))
            if (CollUtil.isNotEmpty(entity1)) {
                sBaseResponse.result = SResponseType.failure
                sBaseResponse.message = "同一分类下已发布图名称已存在"
                return sBaseResponse
            }
        }
        graphPubService.updateBatchById(CollUtil.newArrayList(request.content))
        return sBaseResponse
    }
    /**
     * 恢复图形
     * @author : lijie
     * Update By 2022/6/7 18:07
     */
    @Transactional
    override fun recoveryLogic(graphs: GraphEntity): SBaseResponse {
        /** 项目 id */
        graphs.projectId = Opts.projectId
        /** 查询图 */
        val graph = getOne(KtQueryWrapper(GraphEntity())
                .eq(StrUtil.isNotBlank(Opts.projectId),GraphEntity::projectId,Opts.projectId!!)
                .eq(StrUtil.isNotBlank(graphs.id),GraphEntity::id,graphs.id!!)
                .eq(StrUtil.isNotBlank(graphs.graphId),GraphEntity::graphId,graphs.graphId!!))
        if (ObjectUtil.isNull(graph)){
            return SBaseResponse(SResponseType.success)
        }
        /** 图对象不为空 */
        /** 查询 */
        val markersList = markerService.list(KtQueryWrapper(MarkerEntity())
                .eq(StrUtil.isNotBlank(graphs.id!!),MarkerEntity::id,graphs.id!!)
                .eq(StrUtil.isNotBlank(graphs.graphId!!),MarkerEntity::graphId,graphs.graphId))
        /** 查询 */
        val nodeList = nodeService.list(KtQueryWrapper(NodeEntity())
                .eq(StrUtil.isNotBlank(graphs.id!!),NodeEntity::id,graphs.id!!)
                .eq(StrUtil.isNotBlank(graphs.graphId!!),NodeEntity::graphId,graphs.graphId))
        /** 查询 */
        val anchorList = anchorService.list(KtQueryWrapper(AnchorEntity())
                .eq(StrUtil.isNotBlank(graphs.id!!),AnchorEntity::id,graphs.id!!)
                .eq(StrUtil.isNotBlank(graphs.graphId!!),AnchorEntity::graphId,graphs.graphId))
        /** 查询 */
        val attachObjectIdsList = attachObjectIdsService.list(KtQueryWrapper(AttachObjectIdsEntity())
                .eq(StrUtil.isNotBlank(graphs.id!!),AttachObjectIdsEntity::id,graphs.id!!)
                .eq(StrUtil.isNotBlank(graphs.graphId!!),AttachObjectIdsEntity::graphId,graphs.graphId))
        /** 查询 */
        val relationList = relationService.list(KtQueryWrapper(RelationEntity())
                .eq(StrUtil.isNotBlank(graphs.id!!),RelationEntity::id,graphs.id!!)
                .eq(StrUtil.isNotBlank(graphs.graphId!!),RelationEntity::graphId,graphs.graphId))
        /** 清除草稿箱数据 */
        delOldDataTwo(graphs)
        /** 草稿箱标志 */
        graph?.state = 1
        /** 更新 */
        updateById(graph)
        /** 列表不为空 */
        if (!markersList.isNullOrEmpty()){
            /** 遍历列表 */
            for (markers in markersList){
                /** 草稿箱标志 */
                markers?.state = 1
            }
            markerService.updateBatchById(markersList)
        }

        /** 列表不为空 */
        if (!nodeList.isNullOrEmpty()){
            /** 遍历列表 */
            for (node in nodeList){
                /** 回收站标志 */
                node?.state = 1
            }
            nodeService.updateBatchById(nodeList)
        }

        /** 列表不为空 */
        if (!anchorList.isNullOrEmpty()){
            /** 遍历列表 */
            for (anchor in anchorList){
                /** 草稿箱标志 */
                anchor?.state = 1
            }
            anchorService.updateBatchById(anchorList)
        }

        /** 列表不为空 */
        if (!attachObjectIdsList.isNullOrEmpty()){
            /** 遍历列表 */
            for (attachObjectIds in attachObjectIdsList){
                /** 草稿箱标志 */
                attachObjectIds?.state = 1
            }
            attachObjectIdsService.updateBatchById(attachObjectIdsList)
        }

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

    /**
     * 清除已发布的老数据
     *
     * @param   graph   图对象
     */
    private fun delOldDataPub(graph: GraphEntity) {
        val graphWrapper= KtQueryWrapper(GraphPubEntity())
        val nodeWrapper=KtQueryWrapper(NodePubEntity())
        val markerWrapper=KtQueryWrapper(MarkerPubEntity())
        val relationWrapper=KtQueryWrapper(RelationPubEntity())
        val anchorWrapper=KtQueryWrapper(AnchorPubEntity())
        graphWrapper.eq(StrUtil.isNotBlank(graph.projectId),GraphPubEntity::projectId,graph.projectId!!)
        graphWrapper.eq(StrUtil.isNotBlank(graph.id),GraphPubEntity::id,graph.id!!)
        graphWrapper.eq(StrUtil.isNotBlank(graph.graphId),GraphPubEntity::graphId,graph.graphId)
        graphPubService.remove(graphWrapper)
        nodeWrapper.eq(StrUtil.isNotBlank(graph.id),NodePubEntity::id,graph.id!!)
        nodeWrapper.eq(StrUtil.isNotBlank(graph.graphId),NodePubEntity::graphId,graph.graphId)
        nodePubService.remove(nodeWrapper)
        markerWrapper.eq(StrUtil.isNotBlank(graph.id),MarkerPubEntity::id,graph.id!!)
        markerWrapper.eq(StrUtil.isNotBlank(graph.graphId),MarkerPubEntity::graphId,graph.graphId)
        markerPubService.remove(markerWrapper)
        relationWrapper.eq(StrUtil.isNotBlank(graph.id),RelationPubEntity::id,graph.id!!)
        relationWrapper.eq(StrUtil.isNotBlank(graph.graphId),RelationPubEntity::graphId,graph.graphId)
        relationPubService.remove(relationWrapper)
        anchorWrapper.eq(StrUtil.isNotBlank(graph.id),AnchorPubEntity::id,graph.id!!)
        anchorWrapper.eq(StrUtil.isNotBlank(graph.graphId),AnchorPubEntity::graphId,graph.graphId)
        anchorPubService.remove(anchorWrapper)
    }

    /**
     * 保存子数据
     * @author : lijie
     * Update By 2022/6/7 15:10
     */
    private fun saveElements(graph: GraphEntity) {
        if (null==graph.elements){
            return
        }
        val nodesList = graph.elements!!.nodes
        /** 列表不能为空 */
        if (!nodesList.isNullOrEmpty()) {
            val addNodesList = ArrayList<NodeEntity>()
            val addAnchorsList = ArrayList<AnchorEntity>()
            /** 遍历列表 */
            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("-", "")
                }
                addNodesList.add(nodes)
                val anchorList = nodes.anchorList
                /** 列表不能为空 */
                if (anchorList.isNullOrEmpty()) {
                    continue
                }
                /** 遍历列表 */
                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("-", "")
                    }
                    addAnchorsList.add(anchor)
                }
            }
            if (CollUtil.isNotEmpty(addNodesList)){
                nodeService.saveBatch(CollUtil.newArrayList(addNodesList))
            }
            if (CollUtil.isNotEmpty(addAnchorsList)){
                anchorService.saveBatch(CollUtil.newArrayList(addAnchorsList))
            }
        }

        val markersList = graph.elements!!.markers
        /** 列表不为空 */
        if (!markersList.isNullOrEmpty()) {
            val addMarkerList = ArrayList<MarkerEntity>()
            /** 遍历列表 */
            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("-", "")
                }
                addMarkerList.add(markers)
            }
            if (CollUtil.isNotEmpty(addMarkerList)){
                markerService.saveBatch(CollUtil.newArrayList(addMarkerList))
            }
        }

        val relationsList = graph.elements!!.relations
        /** 列表不为空 */
        if (!relationsList.isNullOrEmpty()) {
            val addRelationList = ArrayList<RelationEntity>()
            /** 遍历列表 */
            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("-", "")
                }
                addRelationList.add(relations)
            }
            if (CollUtil.isNotEmpty(addRelationList)){
                relationService.saveBatch(CollUtil.newArrayList(addRelationList))
            }
        }
    }

    /**
     * 清除草稿箱以前数据
     *
     * @param  graph   图对象
     */
    private fun delOldData(graph: GraphEntity) {
        val graphWrapper= KtQueryWrapper(GraphEntity())
        val nodeWrapper=KtQueryWrapper(NodeEntity())
        val markerWrapper=KtQueryWrapper(MarkerEntity())
        val relationWrapper=KtQueryWrapper(RelationEntity())
        val anchorWrapper=KtQueryWrapper(AnchorEntity())
        graphWrapper.eq(StrUtil.isNotBlank(graph.projectId),GraphEntity::projectId,graph.projectId!!)
        graphWrapper.eq(StrUtil.isNotBlank(graph.id),GraphEntity::id,graph.id!!)
        graphWrapper.eq(StrUtil.isNotBlank(graph.graphId),GraphEntity::graphId,graph.graphId)
        remove(graphWrapper)
        nodeWrapper.eq(StrUtil.isNotBlank(graph.id),NodeEntity::id,graph.id!!)
        nodeWrapper.eq(StrUtil.isNotBlank(graph.graphId),NodeEntity::graphId,graph.graphId)
        nodeService.remove(nodeWrapper)
        markerWrapper.eq(StrUtil.isNotBlank(graph.id),MarkerEntity::id,graph.id!!)
        markerWrapper.eq(StrUtil.isNotBlank(graph.graphId),MarkerEntity::graphId,graph.graphId)
        markerService.remove(markerWrapper)
        relationWrapper.eq(StrUtil.isNotBlank(graph.id),RelationEntity::id,graph.id!!)
        relationWrapper.eq(StrUtil.isNotBlank(graph.graphId),RelationEntity::graphId,graph.graphId)
        relationService.remove(relationWrapper)
        anchorWrapper.eq(StrUtil.isNotBlank(graph.id),AnchorEntity::id,graph.id!!)
        anchorWrapper.eq(StrUtil.isNotBlank(graph.graphId),AnchorEntity::graphId,graph.graphId)
        anchorService.remove(anchorWrapper)
    }
    /**
     * 清除草稿箱以前数据
     *
     * @param  graph   图对象
     */
    private fun delOldDataTwo(graph: GraphEntity) {
        val graphWrapper= KtQueryWrapper(GraphEntity())
        val nodeWrapper=KtQueryWrapper(NodeEntity())
        val markerWrapper=KtQueryWrapper(MarkerEntity())
        val relationWrapper=KtQueryWrapper(RelationEntity())
        val anchorWrapper=KtQueryWrapper(AnchorEntity())
        graphWrapper.eq(StrUtil.isNotBlank(graph.projectId),GraphEntity::projectId,graph.projectId!!)
        graphWrapper.eq(StrUtil.isNotBlank(graph.id),GraphEntity::id,graph.id!!)
        graphWrapper.eq(StrUtil.isNotBlank(graph.graphId),GraphEntity::graphId,graph.graphId)
        graphWrapper.eq(GraphEntity::state,1)
        remove(graphWrapper)
        nodeWrapper.eq(StrUtil.isNotBlank(graph.id),NodeEntity::id,graph.id!!)
        nodeWrapper.eq(StrUtil.isNotBlank(graph.graphId),NodeEntity::graphId,graph.graphId)
        nodeWrapper.eq(NodeEntity::state,1)
        nodeService.remove(nodeWrapper)
        markerWrapper.eq(StrUtil.isNotBlank(graph.id),MarkerEntity::id,graph.id!!)
        markerWrapper.eq(StrUtil.isNotBlank(graph.graphId),MarkerEntity::graphId,graph.graphId)
        markerWrapper.eq(MarkerEntity::state,1)
        markerService.remove(markerWrapper)
        relationWrapper.eq(StrUtil.isNotBlank(graph.id),RelationEntity::id,graph.id!!)
        relationWrapper.eq(StrUtil.isNotBlank(graph.graphId),RelationEntity::graphId,graph.graphId)
        relationWrapper.eq(RelationEntity::state,1)
        relationService.remove(relationWrapper)
        anchorWrapper.eq(StrUtil.isNotBlank(graph.id),AnchorEntity::id,graph.id!!)
        anchorWrapper.eq(StrUtil.isNotBlank(graph.graphId),AnchorEntity::graphId,graph.graphId)
        anchorWrapper.eq(AnchorEntity::state,1)
        anchorService.remove(anchorWrapper)
    }

}