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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.reimbursement.BpmOAReimbursementCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.reimbursement.BpmOAReimbursementRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAReimbursementDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAReimbursementService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.project.ProjectApi;
import cn.iocoder.yudao.module.system.api.project.dto.ProjectDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jodd.util.StringUtil;
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/reimbursement")
@Validated
public class BpmOAReimbursementController {

    @Resource
    private BpmOAReimbursementService service;

    @Resource
    private ProjectApi projectApi;

    @Resource
    private DeptApi deptApi;

    @PostMapping("/create")
//    @PreAuthorize("@ss.hasPermission('bpm:oa-reimbursement:create')")
    @Operation(summary = "创建请求申请")
    public CommonResult<Long> createReimbursement(@Valid @RequestBody BpmOAReimbursementCreateReqVO createReqVO) {
        return success(service.createReimbursement(getLoginUserId(), createReqVO));
    }

    @GetMapping("/get")
//    @PreAuthorize("@ss.hasPermission('bpm:oa-reimbursement:query')")
    @Operation(summary = "获得报销申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOAReimbursementRespVO> getReimbursement(@RequestParam("id") Long id) {

        BpmOAReimbursementDO reimbursement = service.getReimbursement(id);
        BpmOAReimbursementRespVO respVO = service.convert(reimbursement);
        if (respVO != null) {
            if (StringUtil.isNotEmpty(respVO.getProjectNo())) {
                // 设置项目名称
                ProjectDTO projectDTO = projectApi.getProject(respVO.getProjectNo()).getCheckedData();
                respVO.setProjectName(projectDTO.getName());
            }
            if (respVO.getPaymentCompany() != null) {
                // 设置付款公司名称
                respVO.setPaymentCompanyName(deptApi.getDept(respVO.getPaymentCompany()).getCheckedData().getName());
            }
        }
        return success(respVO);
    }

    @GetMapping("/getByImprest")
    @Operation(summary = "获得指定备用金得报销申请")
    @Parameter(name = "imprestId", description = "备用金编号", required = true, example = "1024")
    public CommonResult<BpmOAReimbursementRespVO> getReimbursementByImprest(@RequestParam("imprestId") Long imprestId) {

        BpmOAReimbursementDO reimbursement = service.getReimbursementByImprest(imprestId);
        BpmOAReimbursementRespVO respVO = service.convert(reimbursement);
        if (respVO != null && StringUtil.isNotEmpty(respVO.getProjectNo())) {
            // 设置项目名称
            ProjectDTO projectDTO = projectApi.getProject(respVO.getProjectNo()).getCheckedData();
            respVO.setProjectName(projectDTO.getName());
        }
        return success(respVO);
    }

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

        BpmOAReimbursementDO reimbursement = service.getByProcessInstanceId(processInstanceId);
        BpmOAReimbursementRespVO respVO = service.convert(reimbursement);
        if (respVO != null) {
            if (StringUtil.isNotEmpty(respVO.getProjectNo())) {
                // 设置项目名称
                ProjectDTO projectDTO = projectApi.getProject(respVO.getProjectNo()).getCheckedData();
                respVO.setProjectName(projectDTO.getName());
            }
            if (respVO.getPaymentCompany() != null) {
                // 设置付款公司名称
                respVO.setPaymentCompanyName(deptApi.getDept(respVO.getPaymentCompany()).getCheckedData().getName());
            }
        }
        return success(respVO);
    }
}
