package cn.iocoder.yudao.module.bpm.controller.admin.oa;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.expenses.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAExpensesDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAExpensesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * OA 生产开支 Controller
 *
 * @author 符溶馨
 */
@Tag(name = "管理后台 - OA 生产开支")
@RestController
@RequestMapping("/bpm/oa/expenses")
@Validated
public class BpmOAExpensesController {

    @Resource
    private BpmOAExpensesService expensesService;

    @PostMapping("/create")
    @Operation(summary = "创建请求申请")
    public CommonResult<Long> createExpenses(@Valid @RequestBody BpmOAExpensesCreateReqVO createReqVO) {

        return success(expensesService.createExpenses(getLoginUserId(), createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产开支申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOAExpensesRespVO> getExpenses(@RequestParam("id") Long id) {

        BpmOAExpensesDO expensesDO = expensesService.getExpenses(id);
        BpmOAExpensesRespVO respVO = expensesService.convertExpenses(expensesDO);

        return success(respVO);
    }

    @GetMapping("/getByProcessInstanceId")
    @Operation(summary = "获得现金支出申请")
    @Parameter(name = "processInstanceId", description = "流程实例编号", required = true, example = "1024")
    public CommonResult<BpmOAExpensesRespVO> getExpensesByProcessInstanceId(@RequestParam("processInstanceId") String processInstanceId) {

        BpmOAExpensesDO expensesDO = expensesService.getByProcessInstanceId(processInstanceId);
        BpmOAExpensesRespVO respVO = expensesService.convertExpenses(expensesDO);

        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产开支申请分页")
    @DataPermission(enable = false)
    public CommonResult<PageResult<BpmOAExpensesPageRespVO>> getExpensesPage(BpmOAExpensesPageReqVO pageReqVO) {

        PageResult<BpmOAExpensesPageRespVO> respVO = expensesService.getExpensesPage(pageReqVO);
        return success(respVO);
    }

    @GetMapping("/total")
    @Operation(summary = "获得开支金额统计")
    @DataPermission(enable = false)
    public CommonResult<BpmOAExpensesTotal> getExpensesTotal(BpmOAExpensesPageReqVO pageReqVO) {

        return success(expensesService.getExpensesTotal(pageReqVO));
    }

    @GetMapping("/payment")
    @Operation(summary = "提前付款")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Boolean> getPayment(Long id) {

        expensesService.getPayment(id);
        return success(true);
    }

    @GetMapping("/refused")
    @Operation(summary = "拒绝付款")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @Parameter(name = "processInstanceId", description = "流程实例编号", required = true, example = "1024")
    public CommonResult<Boolean> refused(@RequestParam("id") Long id,
                                         @RequestParam("processInstanceId") String processInstanceId) {

        expensesService.refused(id, processInstanceId);
        return success(true);
    }

    @GetMapping("/oneClickPayment")
    @Operation(summary = "一键支付")
    @Parameter(name = "month", description = "月份", required = true, example = "2025-04")
    @DataPermission(enable = false)
    public CommonResult<Boolean> oneClickPayment(String month) {

        expensesService.oneClickPayment(month);
        return success(true);
    }
}