package com.dayou.controller;

import com.dayou.dto.WorkNodeCommit;
import com.dayou.vo.NameCodeVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
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.IWorkFlowService;
import com.dayou.entity.WorkFlow;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.common.PullDownModel;
import com.dayou.utils.ConvertUtil;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.validation.Valid;

/**
 * 工作流
 *
 * @author wucl
 * @since 2023-10-17
 */
@RestController
@RequestMapping("workFlow")
@Slf4j
public class WorkFlowController extends BaseController {
    @Autowired
    private IWorkFlowService workFlowService;


    /**
    * 工作流列表
    */
    @GetMapping("")
    public RestResponse<Page<WorkFlow>> page(WorkFlow workFlow, Page page){
        Page<WorkFlow> pages=workFlowService.selectPage(page,workFlow);
        return RestResponse.data(pages);
    }

    /**
     * 工作流详情
     */
    @GetMapping("/{id}")
    public RestResponse<WorkFlow> detail(@PathVariable Long id){
        WorkFlow xWorkFlow =workFlowService.detail(id);
        return RestResponse.data(xWorkFlow);
     }

    /**
     * 工作流新增
     */
    @PostMapping("")
    public RestResponse<Long> save(@RequestBody WorkFlow workFlow) {
        Long id = workFlowService.add(workFlow);
        return RestResponse.data(id);
    }

    /**
     * 工作流更新
     */
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody WorkFlow workFlow) {
        Boolean ret = workFlowService.update(workFlow);
        return RestResponse.data(ret);
    }

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


    /**
     * 工作流下拉列表
     */
    @GetMapping("/simpleAll")
    public RestResponse<List<PullDownModel>> simpleAll(){
        LambdaQueryWrapper<WorkFlow> lambdaQueryWrapper = new LambdaQueryWrapper<WorkFlow>()
        .select(WorkFlow::getId,WorkFlow::getName)
        .eq(WorkFlow::getDeleted,false);
        List<WorkFlow> list= workFlowService.list(lambdaQueryWrapper);
        List<PullDownModel> ret=ConvertUtil.copyList(list, PullDownModel.class);
        return RestResponse.data(ret);
    }

    /**
     * 节点枚举
     * @return
     */
    @GetMapping("/enum")
    public RestResponse<List<NameCodeVO>> flowsEnum(){
        List<NameCodeVO> enums = workFlowService.flowsEnum();
        return RestResponse.data(enums);
    }

    /**
     * 节点提交
     * @param workNodeCommit
     * @return
     */
    @PostMapping("/commit")
    public RestResponse<Boolean> commit(@RequestBody @Valid WorkNodeCommit workNodeCommit){
        Boolean result = workFlowService.commit(workNodeCommit);
        return RestResponse.data(result);
    }

    /**
     * 批量提交流程
     * @param workNodeCommits 提交信息集合
     * @return Boolean
     */
    @PostMapping("/batchCommit")
    public RestResponse<Boolean> batchCommit(@RequestBody @Valid List<WorkNodeCommit> workNodeCommits){
        return RestResponse.data(workFlowService.batchCommit(workNodeCommits));
    }
}

