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.assetClaim.BpmOAAssetClaimCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.assetClaim.BpmOAAssetClaimRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAAssetClaimDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAAssetClaimItemDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAAssetClaimItemService;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAAssetClaimService;
import cn.iocoder.yudao.module.system.api.assets.AssetsTypeApi;
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.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - OA 资金申领")
@RestController
@RequestMapping("/bpm/oa/assetClaim")
@Validated
public class BpmOAAssetClaimController {

    @Resource
    private BpmOAAssetClaimService assetClaimService;

    @Resource
    private BpmOAAssetClaimItemService itemService;

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

        return success(assetClaimService.createAssetClaim(getLoginUserId(), createReqVO));
    }

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

        BpmOAAssetClaimDO assetClaimDO = assetClaimService.getAssetClaim(id);

        // 获取明细数据
        List<BpmOAAssetClaimItemDO> itemDOS = itemService.getAssetClaimItem(id);

        return success(BeanUtils.toBean(assetClaimDO, BpmOAAssetClaimRespVO.class)
                .setReceive(itemDOS));
    }

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

        BpmOAAssetClaimDO assetClaimDO = assetClaimService.getByProcessInstanceId(processInstanceId);

        // 获取明细数据
        List<BpmOAAssetClaimItemDO> itemDOS = itemService.getAssetClaimItem(assetClaimDO.getId());

        return success(BeanUtils.toBean(assetClaimDO, BpmOAAssetClaimRespVO.class)
                .setReceive(itemDOS));
    }
}
