package com.persagy.server.labsl.controller

import com.persagy.database.SFilter
import com.persagy.server.labsl.models.entities.Graph
import com.persagy.server.labsl.models.entities.pub.GraphPub
import com.persagy.server.labsl.models.response.SCommonResponse
import com.persagy.server.labsl.services.GraphService
import com.persagy.server.labsl.services.pub.GraphPubService
import com.persagy.service.SPageContext
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.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * 图形
 *
 * @author wx
 */
@Api(tags = ["001、图实例"])
@RestController
@RequestMapping("/graph")
class GraphController {

    /**
     * 创建图
     *
     * @param   request     待图对象列表
     * @return  创建完成图对象列表
     */
    @ApiOperation(value="草稿箱-创建图", notes="")
    @PostMapping("/drafts/create")
    fun create(@RequestBody request: SCreateRequest<Graph>): SCreateResponse<Graph> {
        return GraphService.createList(request)
    } // Function create()

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

    /**
     * 发布图形
     */
    @ApiOperation(value="草稿箱-发布图形", notes=" Id 和 GraphId 必须填写")
    @PostMapping("/drafts/publish")
    fun pubDrawings(@RequestBody graph: GraphPub): SCreateResponse<GraphPub>{
        return GraphService.pubDrawings(graph)
    } // Function pubDrawings()


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

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

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

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

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

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

    /**
     * 已发布图形查询
     */
    @ApiOperation(value = "已发布-查询图形信息", notes = "Id 和 GraphId 必填")
    @PostMapping(value = ["/pub/query"])
    fun querPub(@RequestBody request: SQueryRequest): SQueryResponse<GraphPub> {
        val projectId = SPageContext.getHeader("projectId")
        return GraphPubService.pageQuery(request, arrayListOf(SFilter.eq("projectId",projectId!!)))
    } // Function querPub()

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

    /**
     * 回收站- 恢复图形
     */
    @ApiOperation(value = "回收站- 恢复图形", notes = "Id 和 GraphId 必填 ")
    @PostMapping(value = ["recycle/recovery"])
    fun recovery(@RequestBody graph: Graph): SBaseResponse{
    return GraphService.recovery(graph)
    } // Fun recovery()


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

} // Class GraphController