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

import cn.iocoder.yudao.module.bpm.controller.admin.oa.expense.vo.OaExpensePageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.expense.vo.OaExpenseRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.expense.vo.OaExpenseSaveReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.expense.vo.OaExpensePageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.expense.vo.OaExpenseRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.expense.vo.OaExpenseSaveReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.BpmTaskApproveReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.task.vo.task.BpmTaskReturnReqVO;
import cn.iocoder.yudao.module.bpm.service.oa.expense.OaExpenseService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.expense.OaExpenseDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.expense.OaExpenseObjDO;
import cn.iocoder.yudao.module.bpm.service.oa.expense.OaExpenseService;

@Tag(name = "管理后台 - 报销流程信息")
@RestController
@RequestMapping("/bpm/oa-expense")
@Validated
public class OaExpenseController {


    @Resource
    private OaExpenseService oaExpenseService;

    @PostMapping("/staging")
    @Operation(summary = "暂存报销审批流程信息")
    @ApiOperationSupport(order = 1)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:staging')")
    public CommonResult<Long> stagingOaExpense(@RequestBody OaExpenseSaveReqVO stagingReqVO) {
        Long oaExpenseId = oaExpenseService.stagingOaExpense(stagingReqVO);
        return success(oaExpenseId, "暂存成功");
    }

    @PostMapping("/commit")
    @Operation(summary = "提交报销审批流程信息")
    @ApiOperationSupport(order = 2)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:commit')")
    public CommonResult<Long> commitOaExpense(@Valid @RequestBody OaExpenseSaveReqVO commitReqVO) {
        Long oaExpenseId = oaExpenseService.commitOaExpense(commitReqVO);
        return success(oaExpenseId, "提交成功");
    }

    @PostMapping("/agree")
    @Operation(summary = "审批同意报销审批流程信息")
    @ApiOperationSupport(order = 3)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:agree')")
    public CommonResult<Long> agreeOaExpense(@Valid @RequestBody BpmTaskApproveReqVO agreeReqVO) {
        Long result = oaExpenseService.agreeOaExpense(agreeReqVO);
        return success(result, "审批成功");
    }

    @PostMapping("/disagree")
    @Operation(summary = "驳回报销审批流程信息")
    @ApiOperationSupport(order = 4)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:disagree')")
    public CommonResult<Long> disagreeOaExpense(@Valid @RequestBody BpmTaskReturnReqVO disagreeReqVO) {
        Long result = oaExpenseService.disagreeOaExpense(disagreeReqVO);
        return success(result, "驳回成功");
    }

    @PostMapping("/revocation")
    @Operation(summary = "撤回报销审批流程信息")
    @ApiOperationSupport(order = 5)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:revocation')")
    public CommonResult<Long> revocationOaExpense(@Valid @RequestBody BpmTaskApproveReqVO revocationReqVO) {
        Long result = oaExpenseService.revocationOaExpense(revocationReqVO);
        return success(result, "撤回成功");
    }

    @PostMapping("/reCommit")
    @Operation(summary = "驳回或撤回后再次提交报销审批流程信息")
    @ApiOperationSupport(order = 6)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:re-commit')")
    public CommonResult<Long> reCommitOaExpense(@Valid @RequestBody OaExpenseSaveReqVO reCommitReqVO) {
        Long result = oaExpenseService.reCommitOaExpense(reCommitReqVO);
        return success(result, "再次提交成功");
    }

    @DeleteMapping("/close")
    @Operation(summary = "驳回或撤回后关闭报销审批流程信息")
    @ApiOperationSupport(order = 7)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-entry:close')")
    public CommonResult<Long> closeOaExpense(@RequestParam("id") Long id) {
        Long result = oaExpenseService.closeOaExpense(id);

        return success(result, "关闭成功");
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报销审批流程信息")
    @ApiOperationSupport(order = 8)
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:delete')")
    public CommonResult<Boolean> deleteOaExpense(@RequestParam("id") Long id) {
        oaExpenseService.deleteOaExpense(id);
        return success(true, "删除成功");
    }

    @GetMapping("/get")
    @Operation(summary = "根据id获得报销审批流程信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1")
    @ApiOperationSupport(order = 9)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:query-id')")
    public CommonResult<OaExpenseRespVO> getOaExpense(@RequestParam("id") Long id) {
        OaExpenseRespVO respVO = oaExpenseService.getOaExpense(id);
        return success(respVO, "查询对象成功");
    }

    @GetMapping("/getByProcInstId")
    @Operation(summary = "根据流程实例id获得报销审批流程信息")
    @Parameter(name = "procInstId", description = "流程实例id", required = true, example = "9528a78d-457e-11ef-853c-4c034fce6445")
    @ApiOperationSupport(order = 10)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:query-proc-inst-id')")
    public CommonResult<OaExpenseRespVO> getOaExpenseByProcInstId(@RequestParam("procInstId") String procInstId) {
        OaExpenseRespVO respVO = oaExpenseService.getOaExpenseByProcInstId(procInstId);
        return success(respVO, "查询对象成功");
    }

    @GetMapping("/page")
    @Operation(summary = "获得报销审批流程信息分页")
    @ApiOperationSupport(order = 11)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:query-page')")
    public CommonResult<PageResult<OaExpenseRespVO>> getOaExpensePage(@Valid @ParameterObject OaExpensePageReqVO pageReqVO) {
        return success(oaExpenseService.getOaExpensePage(pageReqVO), "查询列表成功");
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报销审批流程信息 Excel")
    @ApiOperationSupport(order = 12)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOaExpenseExcel(@ParameterObject OaExpensePageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        PageResult<OaExpenseRespVO> pageResult = oaExpenseService.exportOaExpenseExcel(pageReqVO);
        List<OaExpenseRespVO> list = pageResult.getList();
        // 导出 Excel
        ExcelUtils.write(response, "报销审批流程信息.xls", "数据", OaExpenseRespVO.class,
                BeanUtils.toBean(list, OaExpenseRespVO.class));
    }
    
    // @Resource
    // private OaExpenseService oaExpenseService;
    //
    // @PostMapping("/create")
    // @Operation(summary = "创建报销流程信息")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:create')")
    // public CommonResult<Long> createOaExpense(@Valid @RequestBody OaExpenseSaveReqVO createReqVO) {
    //     return success(oaExpenseService.createOaExpense(createReqVO));
    // }
    //
    // @PutMapping("/update")
    // @Operation(summary = "更新报销流程信息")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:update')")
    // public CommonResult<Boolean> updateOaExpense(@Valid @RequestBody OaExpenseSaveReqVO updateReqVO) {
    //     oaExpenseService.updateOaExpense(updateReqVO);
    //     return success(true);
    // }
    //
    // @DeleteMapping("/delete")
    // @Operation(summary = "删除报销流程信息")
    // @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:delete')")
    // public CommonResult<Boolean> deleteOaExpense(@RequestParam("id") Long id) {
    //     oaExpenseService.deleteOaExpense(id);
    //     return success(true);
    // }
    //
    // @GetMapping("/get")
    // @Operation(summary = "获得报销流程信息")
    // @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:query')")
    // public CommonResult<OaExpenseRespVO> getOaExpense(@RequestParam("id") Long id) {
    //     OaExpenseDO oaExpense = oaExpenseService.getOaExpense(id);
    //     return success(BeanUtils.toBean(oaExpense, OaExpenseRespVO.class));
    // }
    //
    // @GetMapping("/page")
    // @Operation(summary = "获得报销流程信息分页")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:query')")
    // public CommonResult<PageResult<OaExpenseRespVO>> getOaExpensePage(@Valid OaExpensePageReqVO pageReqVO) {
    //     PageResult<OaExpenseDO> pageResult = oaExpenseService.getOaExpensePage(pageReqVO);
    //     return success(BeanUtils.toBean(pageResult, OaExpenseRespVO.class));
    // }
    //
    // @GetMapping("/export-excel")
    // @Operation(summary = "导出报销流程信息 Excel")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:export')")
    // @ApiAccessLog(operateType = EXPORT)
    // public void exportOaExpenseExcel(@Valid OaExpensePageReqVO pageReqVO,
    //           HttpServletResponse response) throws IOException {
    //     pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
    //     List<OaExpenseDO> list = oaExpenseService.getOaExpensePage(pageReqVO).getList();
    //     // 导出 Excel
    //     ExcelUtils.write(response, "报销流程信息.xls", "数据", OaExpenseRespVO.class,
    //                     BeanUtils.toBean(list, OaExpenseRespVO.class));
    // }
    //
    // // ==================== 子表（报销流程信息子） ====================
    //
    // @GetMapping("/oa-expense-obj/list-by-expense-id")
    // @Operation(summary = "获得报销流程信息子列表")
    // @Parameter(name = "expenseId", description = "报销主表主键id")
    // @PreAuthorize("@ss.hasPermission('bpm:oa-expense:query')")
    // public CommonResult<List<OaExpenseObjDO>> getOaExpenseObjListByExpenseId(@RequestParam("expenseId") Long expenseId) {
    //     return success(oaExpenseService.getOaExpenseObjListByExpenseId(expenseId));
    // }

}