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.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.receipt.BpmOAReceiptCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.receipt.BpmOAReceiptPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.receipt.BpmOAReceiptRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAContractDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAReceiptDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAContractService;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAReceiptService;
import cn.iocoder.yudao.module.hrm.api.crmcustomer.CrmCustomerApi;
import cn.iocoder.yudao.module.hrm.api.crmcustomer.dto.CrmCustomerDTO;
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/receipt")
@Validated
public class BpmOAReceiptController {

    @Resource
    private BpmOAReceiptService receiptService;

    @Resource
    private CrmCustomerApi customerApi;

    @Resource
    private BpmOAContractService contractService;

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

        return success(receiptService.createReceipt(getLoginUserId(), createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得回款申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOAReceiptRespVO> getReceipt(@RequestParam("id") Long id) {

        BpmOAReceiptDO receipt = receiptService.getReceipt(id);
        BpmOAReceiptRespVO respVO = BeanUtils.toBean(receipt, BpmOAReceiptRespVO.class);
        if (respVO != null) {

            // 获取客户信息
            CrmCustomerDTO crmCustomerDTO = customerApi.getCustomer(receipt.getCustomerId()).getCheckedData();
            // 获取合同信息
            BpmOAContractDO contractDO = contractService.getContract(receipt.getContractId());

            // 设置客户名称
            respVO.setCustomerName(crmCustomerDTO != null ? crmCustomerDTO.getName() : null);
            // 设置合同名称
            respVO.setContractName(contractDO != null ? contractDO.getContractName() : null);
        }

        return success(respVO);
    }

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

        BpmOAReceiptDO receipt = receiptService.getByProcessInstanceId(processInstanceId);
        BpmOAReceiptRespVO respVO = BeanUtils.toBean(receipt, BpmOAReceiptRespVO.class);
        if (respVO != null) {

            // 获取客户信息
            CrmCustomerDTO crmCustomerDTO = customerApi.getCustomer(receipt.getCustomerId()).getCheckedData();
            // 获取合同信息
            BpmOAContractDO contractDO = contractService.getContract(receipt.getContractId());

            // 设置客户名称
            respVO.setCustomerName(crmCustomerDTO != null ? crmCustomerDTO.getName() : null);
            // 设置合同名称
            respVO.setContractName(contractDO != null ? contractDO.getContractName() : null);
        }

        return success(respVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得回款分页列表")
    public CommonResult<PageResult<BpmOAReceiptRespVO>> getPage(@Valid BpmOAReceiptPageReqVO pageReqVO) {

        return success(receiptService.getReceiptPage(pageReqVO));
    }
}
