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.constant.enums.OperationType;
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.core.annotation.Log;
import com.cool.bang.modules.approval.domain.po.ApprovalFlow;
import com.cool.bang.modules.approval.service.IApprovalFlowService;
import com.cool.bang.modules.approval.service.IApprovalNodeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 审批流程
 *
 * @author PeiZhi Guo
 * 2022-8-1 16:42
 */
@RestController
@RequestMapping(ControllerConstant.APPROVAL + "flow/v1")
public class ApprovalFlowController extends BaseController {

    @Autowired
    private IApprovalFlowService approvalFlowService;
    @Autowired
    private IApprovalNodeService approvalNodeService;


    @GetMapping
    @PreAuthorize("hasPermission('','approval:flow:main')")
    public ResultTable main(PageDomain page, ApprovalFlow approvalFlow) {
        approvalFlow.setDelFlag(1);
        PageInfo<ApprovalFlow> approvalFlowPage = approvalFlowService.page(page, approvalFlow);
        return pageTable(approvalFlowPage.getList(), approvalFlowPage.getTotal());
    }

    @GetMapping("list")
    public Result list(ApprovalFlow approvalFlow) {
        approvalFlow.setDelFlag(1);
        List<ApprovalFlow> approvalFlowPage = approvalFlowService.list(approvalFlow);
        return success(approvalFlowPage);
    }

    @GetMapping("{id}")
    @PreAuthorize("hasPermission('','approval:flow:main')")
    public Result get(@PathVariable Long id) {
        return success(approvalFlowService.queryById(id));
    }

    @GetMapping("/querType")
    public Result querType(Integer flowType) {
        return success(approvalFlowService.queryByFlowType(flowType));
    }

    @PostMapping
    @PreAuthorize("hasPermission('','approval:flow:add')")
    public Result add(@RequestBody ApprovalFlow approvalFlow) {
        approvalFlow.setCreateBy(getCurrentUser().getId());
        approvalFlow.setCreateName(getCurrentUser().getRealName());
        return decide(approvalFlowService.save(approvalFlow));
    }

    @PostMapping("switch")
    @Log(operationType = OperationType.OTHER, title = "禁用|启用")
    public Result updateState(Integer state, Long id) {
        if (approvalFlowService.updateState(state, id)) {
            return success("操作成功");
        }
        return failure("操作失败，请检查是否已启用其他版本！");
    }

    @PostMapping("edit")
    @PreAuthorize("hasPermission('','approval:flow:edit')")
    public Result edit(@RequestBody ApprovalFlow approvalFlow) {
        if (null == approvalFlow.getId()) {
            return failure("id is not null");
        }
        return decide(approvalFlowService.update(approvalFlow));
    }

    @PostMapping("flowSwitch")
    @PreAuthorize("hasPermission('','approval:flow:edit')")
    public Result flowSwitch(@RequestBody ApprovalFlow approvalFlow) {
        if (null == approvalFlow.getId()) {
            return failure("id is not null");
        }
        return decide(approvalFlowService.update(approvalFlow));
    }
}
