package com.cool.bang.modules.approval.controller;

import com.cool.bang.common.base.BaseController;
import com.cool.bang.common.constant.ControllerConstant;
import com.cool.bang.common.web.request.PageDomain;
import com.cool.bang.common.web.response.Result;
import com.cool.bang.common.web.response.module.ResultTable;
import com.cool.bang.modules.approval.domain.po.ApprovalNode;
import com.cool.bang.modules.approval.service.IApprovalNodeService;
import com.cool.bang.modules.system.service.IUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 审批节点 Controller
 *
 * @author PeiZhi Guo
 * @version 1.0.0
 * 2022-08-15 15:48
 */
@RestController
@RequestMapping(ControllerConstant.APPROVAL + "node/v1")
public class ApprovalNodeController extends BaseController {

    @Autowired
    private IApprovalNodeService approvalNodeService;
    @Autowired
    private IUserService userService;


    @GetMapping
    //@PreAuthorize("hasPermission('','approval:node:list')")
    public ResultTable list(PageDomain pageDomain, ApprovalNode param) {
        PageInfo<ApprovalNode> approvalNodePage = approvalNodeService.page(pageDomain, param);
        return pageTable(approvalNodePage.getList(), approvalNodePage.getTotal());
    }

    @GetMapping("{id}")
    public Result get(@PathVariable Long id) {
        return success(approvalNodeService.queryById(id));
    }

    @GetMapping("dlyh")
    public Result dlyh() {
        return success(getCurrentUser().getId());
    }

    @GetMapping("flowList/{id}")
    public Result getFlowList(@PathVariable Long id) {
        ApprovalNode param = new ApprovalNode();
        param.setFlowId(id);
        return success(approvalNodeService.list(param));
    }

    @PostMapping("nodeList")
    public Result getNodeList(Long flowId,String number) {
        ApprovalNode param = new ApprovalNode();
        param.setNumber(Integer.valueOf(number));
        param.setFlowId(flowId);
        return success(approvalNodeService.list(param));
    }

    @PostMapping
    public Result add(@RequestBody ApprovalNode approvalNode) {
        return decide(approvalNodeService.save(approvalNode));
    }

    @PostMapping("edit")
    public Result edit(@RequestBody ApprovalNode approvalNode) {
        approvalNode.setUserName(userService.queryById(approvalNode.getUserId()).getRealName());
        return decide(approvalNodeService.update(approvalNode));
    }
    @PostMapping("nodeEdit")
    public Result nodeEdit(@RequestBody ApprovalNode approvalNode) {
        if (approvalNode.getNodeType() != 3 ){
        approvalNode.setUserName(userService.queryById(approvalNode.getUserId()).getRealName());}
        ApprovalNode app = approvalNodeService.queryById(approvalNode.getId());
        boolean res = true;
        if (app == null){
            res = approvalNodeService.save(approvalNode);
        }else{
            res = approvalNodeService.update(approvalNode);
        }
        return decide(res);
    }
    @PostMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        return decide(approvalNodeService.removeById(id));
    }

}
