package com.persagy.labsl.controller

import com.persagy.database.SFilter
import com.persagy.labsl.Opts
import com.persagy.labsl.models.entities.tpt.Graph
import com.persagy.labsl.models.entities.tpt.pub.GraphPub
import com.persagy.labsl.models.response.SCommonResponse
import com.persagy.labsl.services.GraphService
import com.persagy.labsl.services.pub.GraphPubService
import com.persagy.service.models.requests.SCreateRequest
import com.persagy.service.models.requests.SQueryRequest
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 io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.web.bind.annotation.*

/**
 * 图形
 *
 * @author wx  <zhangweixin@sagacloud.com>
 */
@Tag(name = "001、系统图")
@RestController
@RequestMapping("/graph")
class GraphController {

    /**
     * 创建图
     *
     * @param request     待图对象列表
     * @return 创建完成图对象列表
     */
   @Operation(summary ="草稿箱-创建图", description = "id和graphId为必填，图中会包含多个节点，详细节点结构见示例")
    @PostMapping("/drafts/create")
    fun create(@RequestBody request: SCreateRequest<Graph>): SCreateResponse<Graph> {
        return GraphService.draftsCreate(request)
    } // Function create()

    /**
     * 提交图形
     *
     * @param  graph   待图对象
     * @return  图对象列表
     */
   @Operation(summary = "草稿箱-保存图形", description = "")
    @PostMapping("/drafts/save")
    fun save(@RequestBody graph: Graph): SCreateResponse<Graph> {
        return GraphService.save(graph)
    } // Function save()

    /**
     * 发布图形
     *
     * @param graph     待发布的图对象
     * @return 发布结果
     */
   @Operation(summary="草稿箱-发布图形", description =" Id 和 GraphId 必须填写")
    @PostMapping("/drafts/publish")
    fun pubDrawings(@RequestBody graph: GraphPub): SCreateResponse<GraphPub>{
        return GraphService.pubDrawings(graph)
    } // Function pubDrawings()


    /**
     * 图、移至或移出回收站
     *
     * @param idList    图例列表
     * @return 移出结果
     */
   @Operation(summary = "草稿箱-移至回收站", description = " Id 和 GraphId 必填")
    @PostMapping(value = ["/drafts/recycle"])
    fun recycle(@RequestBody idList: ArrayList<Graph>): SBaseResponse {
        return GraphService.recycle(idList)
    } // Function recycle()

    /**
     * 已发布图形移至回收站
     *
     * @param idList    图列表
     * @return 移出结果
     */
   @Operation(summary = "已发布-图形移至回收站", description = "Id 和 GraphId 必填")
    @PostMapping(value = ["/pub/recycle"])
    fun recyclePub(@RequestBody idList: ArrayList<Graph>): SBaseResponse{
        return GraphService.recyclePub(idList)
    } // Function recyclePub()

    /**
     * 更新图形信息
     *
     * @param request   更新的内容对象
     * @return 更新的结果
     */
   @Operation(summary = "草稿箱-更新图形信息", description = "Id 和 GraphId 必填")
    @PostMapping(value = ["/drafts/update"])
    fun update(@RequestBody request: SUpdateRequest<Graph>): SBaseResponse {
        return GraphService.draftsUpdate(request)
    } // Function update()

    /**
     * 查询图形信息
     *
     * @param  request  查询条件
     * @return 查询结果
     */
   @Operation(summary = "草稿箱-查询图形信息", description = "state 1(草稿箱)，state 4(回收站) ，Id 和 GraphId 必填")
    @PostMapping(value = ["/drafts/query"])
    fun query(@RequestBody request: SQueryRequest): SQueryResponse<Graph> {
        return GraphService.pageQuery(request, arrayListOf(SFilter.eq("projectId",Opts.projectId!!)))
    } // Function query()

    /**
     * 草稿箱 - 读取图形所有节点
     *
     * @param graph  图例对象
     * @return 查询结果
     */
   @Operation(summary = "草稿箱-查询图形以及所有节点信息", description = "Id 和 GraphId 必填")
    @PostMapping(value = ["/drafts/read"])
    fun readDrafts(@RequestBody graph: Graph): SCommonResponse<Graph> {
        return GraphService.readDrafts(graph)
    } // Fun readDrafts()

    /**
     * 已发布 - 读取图形所有节点
     *
     * @param graph  图例对象】
     * @return 查询结果
     */
   @Operation(summary = "已发布-查询图形以及所有节点信息", description = "Id 和 GraphId 必填")
    @PostMapping(value = ["/pub/read"])
    fun readPub(@RequestBody graph: GraphPub): SCommonResponse<GraphPub> {
        return GraphService.readPub(graph)
    } // Fun readPub()

    /**
     * 已发布图形查询
     *
     * @param request  查询条件
     * @return 查询结果
     */
   @Operation(summary = "已发布-查询图形信息", description = "Id 和 GraphId 必填")
    @PostMapping(value = ["/pub/query"])
    fun querPub(@RequestBody request: SQueryRequest): SQueryResponse<GraphPub> {
        return GraphPubService.pageQuery(request, arrayListOf(SFilter.eq("projectId",Opts.projectId!!)))
    } // Function querPub()

    /**
     * 更新图形信息
     *
     * @param request  更新的内容对象
     * @return 更新的结果
     */
   @Operation(summary = "已发布-更新图形信息", description = "")
    @PostMapping(value = ["/pub/update"])
    fun updatePub(@RequestBody request: SUpdateRequest<GraphPub>): SBaseResponse {
        return GraphService.pubUpdate(request)
    } // Function update()

    /**
     * 回收站- 恢复图形
     *
     * @param graph  图对象
     * @return 恢复结果
     */
   @Operation(summary = "回收站- 恢复图形", description = "Id 和 GraphId 必填 ")
    @PostMapping(value = ["recycle/recovery"])
    fun recovery(@RequestBody graph: Graph): SBaseResponse{
    return GraphService.recovery(graph)
    } // Fun recovery()

    /**
     * 根据id删除图形
     *
     * @param idList      图对象数组
     * @return 删除的结果信息
     */
   @Operation(summary = "回收站-根据id删除图形信息，删除草稿箱图形", description = "Id 和 GraphId 必填 ")
    @PostMapping(value = ["recycle/delete"])
    fun delete(@RequestBody idList: ArrayList<Graph>): SBaseResponse {
        return GraphService.deleteByKeysList(idList)
    } // Function delete()


//    /**
//     * 根据标签查询已发布图形
//     */
//    @Operation(summary = "已发布-根据标签查询已发布图形", description = "")
//    @PostMapping(value = ["/pub/label"])
//    fun labelPub(@RequestParam label: String): SQueryResponse<GraphPub>{
//        return GraphService.containLabelPub(label)
//    }

} // Class GraphController