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.imprest.BpmOAImprestCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.imprest.BpmOAImprestRespVO;
import cn.iocoder.yudao.module.bpm.convert.oa.BpmOAImprestConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAImprestDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAImprestService;
import cn.iocoder.yudao.module.system.api.bank.BankApi;
import cn.iocoder.yudao.module.system.api.bank.dto.BankRespDTO;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
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/imprest")
@Validated
public class BpmOAImprestController {

    @Resource
    private BpmOAImprestService imprestService;

    @Resource
    private BankApi bankApi;

    @Resource
    private DeptApi deptApi;

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

        return success(imprestService.createImprest(getLoginUserId(), createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得备用金申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOAImprestRespVO> getImprest(@RequestParam("id") Long id) {

        BpmOAImprestDO imprest = imprestService.getImprest(id);
        BpmOAImprestRespVO respVO = BpmOAImprestConvert.INSTANCE.convert(imprest);
        if (respVO != null) {
            if (respVO.getBankId() != null) {
                // 获取收款账号信息
                BankRespDTO bankRespDTO = bankApi.getBank(respVO.getBankId()).getCheckedData();
                if (bankRespDTO != null) {
                    respVO.setBankName(bankRespDTO.getBankName());
                    respVO.setNickname(bankRespDTO.getNickname());
                    respVO.setBankNo(bankRespDTO.getBankNo());
                }
            }

            // 获取公司信息
            DeptRespDTO company = deptApi.getDept(respVO.getCompanyId()).getCheckedData();
            respVO.setCompanyName(company.getName());
        }

        return success(respVO);
    }

    @GetMapping("/getOne")
    @Operation(summary = "获得备用金表单")
    @Parameter(name = "userId", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOAImprestRespVO> getImprestByUserId(@RequestParam("userId") Long userId) {

        //根据user 查询审批通过并且未报销得表单。
        BpmOAImprestDO imprest = imprestService.getImprestByUserId(userId);
        if (imprest == null) {
            return success(null);
        }

        BpmOAImprestRespVO respVO = BpmOAImprestConvert.INSTANCE.convert(imprest);
        if (respVO.getStatus().equals(BpmOAImprestDO.IN_PROGRESS)) {
            respVO.setNote("您有一笔备用金报销正在处理中！");
        }

        return success(respVO);
    }

    @GetMapping("/getProcessInstanceId")
    @Operation(summary = "获得备用金表单")
    @Parameter(name = "processInstanceId", description = "流程实例编号", required = true, example = "1024")
    public CommonResult<BpmOAImprestRespVO> getByProcessInstanceId(@RequestParam("processInstanceId") String processInstanceId) {

        BpmOAImprestDO imprest = imprestService.getByProcessInstanceId(processInstanceId);
        BpmOAImprestRespVO respVO = BpmOAImprestConvert.INSTANCE.convert(imprest);
        if (respVO != null) {
            // 获取收款账号信息
            BankRespDTO bankRespDTO = bankApi.getBank(imprest.getBankId()).getCheckedData();
            if (bankRespDTO != null) {
                respVO.setBankName(bankRespDTO.getBankName());
                respVO.setNickname(bankRespDTO.getNickname());
                respVO.setBankNo(bankRespDTO.getBankNo());
            }

            // 获取公司信息
            DeptRespDTO company = deptApi.getDept(respVO.getCompanyId()).getCheckedData();
            respVO.setCompanyName(company.getName());
        }

        return success(respVO);
    }
}