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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.finance.BpmOAFinanceCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.finance.BpmOAFinanceRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAFinanceDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAFinanceService;
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 java.time.format.DateTimeFormatter;

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/finance")
@Validated
public class BpmOAFinanceController {

    @Resource
    private BpmOAFinanceService financeService;

    @PostMapping("/create")
    @Operation(summary = "创建活动经费申请")
    public CommonResult<Long> createFinance(@Valid @RequestBody BpmOAFinanceCreateReqVO createReqVO) {

        return success(financeService.createFinance(getLoginUserId(), createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得活动经费申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOAFinanceRespVO> getFinance(@RequestParam("id") Long id) {

        BpmOAFinanceDO finance = financeService.getFinance(id);
        BpmOAFinanceRespVO respVO = BeanUtils.toBean(finance, BpmOAFinanceRespVO.class);
        respVO.setStartTime(finance.getStartTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        respVO.setEndTime(finance.getEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        return success(respVO);
    }

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

        BpmOAFinanceDO finance = financeService.getByProcessInstanceId(processInstanceId);

        BpmOAFinanceRespVO respVO = BeanUtils.toBean(finance, BpmOAFinanceRespVO.class);
        respVO.setStartTime(finance.getStartTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        respVO.setEndTime(finance.getEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

        return success(respVO);
    }
}
