package com.dayou.controller;

import com.dayou.annotation.OperLog;
import com.dayou.dto.InstanceNodeDTO;
import com.dayou.vo.NightingaleEchartsVO;
import com.dayou.vo.WorkNodeInstanceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.service.workflow.IWorkFlowNodeInstanceService;
import com.dayou.entity.WorkFlowNodeInstance;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 工作流节点实例
 *
 * @author wucl
 * @since 2023-10-17
 */
@RestController
@RequestMapping("workFlowNodeInstance")
@Slf4j
public class WorkFlowNodeInstanceController extends BaseController {
    @Autowired
    private IWorkFlowNodeInstanceService workFlowNodeInstanceService;

    /**
    * 工作流节点实例列表
    */
    @GetMapping("")
    public RestResponse<Page<WorkFlowNodeInstance>> page(WorkFlowNodeInstance workFlowNodeInstance, Page page){
        Page<WorkFlowNodeInstance> pages=workFlowNodeInstanceService.selectPage(page,workFlowNodeInstance);
        return RestResponse.data(pages);
    }

    /**
     * 工作流节点实例详情
     */
    @GetMapping("/{id}")
    public RestResponse<WorkFlowNodeInstance> detail(@PathVariable Long id){
        WorkFlowNodeInstance xWorkFlowNodeInstance =workFlowNodeInstanceService.detail(id);
        return RestResponse.data(xWorkFlowNodeInstance);
     }

    /**
     * 工作流节点实例新增
     */
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody WorkFlowNodeInstance workFlowNodeInstance) {
        Boolean ret = workFlowNodeInstanceService.add(workFlowNodeInstance);
        return RestResponse.data(ret);
    }

    /**
     * 工作流节点实例更新
     */
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody WorkFlowNodeInstance workFlowNodeInstance) {
        Boolean ret = workFlowNodeInstanceService.update(workFlowNodeInstance);
        return RestResponse.data(ret);
    }

    /**
     * 工作流节点实例删除
     */
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = workFlowNodeInstanceService.delete(id);
        return RestResponse.data(ret);
    }

    /**
     * 实例节点流程图
     * @param instance
     * @return
     */
    @PostMapping("/queue")
    public RestResponse<List<WorkNodeInstanceVO>> instanceQueue(@RequestBody @Valid InstanceNodeDTO instance){
        List<WorkNodeInstanceVO> queue = workFlowNodeInstanceService.filterWaitingNodeBeforePendingNode(instance);
        return RestResponse.data(queue);
    }

    /**
     * 更通用一些的流程图
     * @param instance
     * @return
     */
    @PostMapping("/array")
    public RestResponse<List<WorkNodeInstanceVO>> instanceArray(@RequestBody @Valid InstanceNodeDTO instance){
        List<WorkNodeInstanceVO> queue = workFlowNodeInstanceService.instanceArray(instance);
        return RestResponse.data(queue);
    }
    /**
     * 获取当前实例节点
     * @param instance
     * @return
     */
    @PostMapping("/current")
    public RestResponse<WorkNodeInstanceVO> instanceCurrent(@RequestBody @Valid InstanceNodeDTO instance){
        WorkNodeInstanceVO current = workFlowNodeInstanceService.instanceCurrent(instance);
        return RestResponse.data(current);
    }
    /**
     * 不同节点分组统计
     * @param businessType
     * @return
     */
    @GetMapping("/countNode/{businessType}")
    public RestResponse<List<NightingaleEchartsVO>> instanceNodeCountByCode(@PathVariable("businessType") String businessType){
        List<NightingaleEchartsVO> list = workFlowNodeInstanceService.instanceNodeCountByCode(businessType);
        return RestResponse.data(list);
    }

    /**
     * 实例节点激活
     * @param id
     * @return
     */
    @OperLog(title = "实例节点激活")
    @GetMapping("/activate/{id}")
    public RestResponse<Boolean> activateInstanceNode(@PathVariable("id") Long id){
        Boolean ret = workFlowNodeInstanceService.activateInstanceNode(id);
        return RestResponse.data(ret);
    }

    /**
     * 检查当前是否可退回节点
     * @param productionType
     * @param productionNo
     * @return
     */
    @GetMapping("/checkIfReverse/{productionType}/{productionNo}")
    public RestResponse<Boolean> checkIfReverse(@PathVariable("productionType") String productionType, @PathVariable("productionNo") String productionNo){
        Boolean ret = workFlowNodeInstanceService.checkIfReverse(productionType,productionNo);
        return RestResponse.data(ret);
    }


}

