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 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("/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));
    }

}