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.supplierprocurementplan.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOASupplierProcurementPlanDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOASupplierProcurementPlanItemDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOASupplierProcurementPlanItemService;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOASupplierProcurementPlanService;
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;

@Tag(name = "管理后台 - 供应商采购计划")
@RestController
@RequestMapping("/bpm/oa-supplier-procurement-plan")
@Validated
public class BpmOASupplierProcurementPlanController {

    @Resource
    private BpmOASupplierProcurementPlanService oaSupplierProcurementPlanService;
    @Resource
    private BpmOASupplierProcurementPlanItemService oaSupplierProcurementPlanItemService;

    @PostMapping("/create")
    @Operation(summary = "创建供应商采购计划")
    public CommonResult<Long> createOaSupplierProcurementPlan(@Valid @RequestBody BpmOASupplierProcurementPlanSaveReqVO createReqVO) {
        return success(oaSupplierProcurementPlanService.createOaSupplierProcurementPlan(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新供应商采购计划")
    public CommonResult<Boolean> updateOaSupplierProcurementPlan(@Valid @RequestBody BpmOASupplierProcurementPlanSaveReqVO updateReqVO) {
        oaSupplierProcurementPlanService.updateOaSupplierProcurementPlan(updateReqVO);
        return success(true);
    }

    @PutMapping("/uploadDeliveryReceipt")
    @Operation(summary = "上传到货凭证")
    public CommonResult<Boolean> uploadDeliveryReceipt(@Valid @RequestBody TheArrivalFileItemsVO vo) {
        oaSupplierProcurementPlanService.uploadDeliveryReceipt(vo);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商采购计划")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteOaSupplierProcurementPlan(@RequestParam("id") Long id) {
        oaSupplierProcurementPlanService.deleteOaSupplierProcurementPlan(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得供应商采购计划")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOASupplierProcurementPlanRespVO> getOaSupplierProcurementPlan(@RequestParam("id") Long id) {
        BpmOASupplierProcurementPlanDO oaSupplierProcurementPlan = oaSupplierProcurementPlanService.getOaSupplierProcurementPlan(id);
        BpmOASupplierProcurementPlanRespVO vo = BeanUtils.toBean(oaSupplierProcurementPlan, BpmOASupplierProcurementPlanRespVO.class);
        List<BpmOASupplierProcurementPlanItemDO> items = oaSupplierProcurementPlanItemService.getByProcurementPlanId(id);
        List<BpmOASupplierProcurementPlanItemRespVO> itemVos = BeanUtils.toBean(items, BpmOASupplierProcurementPlanItemRespVO.class);
        vo.setItems(itemVos);
        return success(vo);
    }

    @GetMapping("/page")
    @Operation(summary = "获得供应商采购计划分页")
    public CommonResult<PageResult<BpmOASupplierProcurementPlanRespVO>> getOaSupplierProcurementPlanPage(@Valid BpmOASupplierProcurementPlanPageReqVO pageReqVO) {
        PageResult<BpmOASupplierProcurementPlanDO> pageResult = oaSupplierProcurementPlanService.getOaSupplierProcurementPlanPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BpmOASupplierProcurementPlanRespVO.class));
    }

    @GetMapping("/getPlanItem")
    @Operation(summary = "获得供应商采购计划商品列表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<BpmOASupplierProcurementPlanItemRespVO>> getPlanItem(@RequestParam("id") Long id) {
        List<BpmOASupplierProcurementPlanItemDO> items = oaSupplierProcurementPlanItemService.getByProcurementPlanId(id);
        List<BpmOASupplierProcurementPlanItemRespVO> itemVos = BeanUtils.toBean(items, BpmOASupplierProcurementPlanItemRespVO.class);
        return success(itemVos);
    }

    @GetMapping("/getListBySupplierId")
    @Operation(summary = "根据供应商id获取采购计划列表")
    public CommonResult<List<BpmOASupplierProcurementPlanDO>> getListBySupplierId(@ModelAttribute BpmOASupplierProcurementPlanReqVO vo) {
        List<BpmOASupplierProcurementPlanDO> vos = oaSupplierProcurementPlanService.getListBySupplierId(vo);
        return success(vos);
    }

}
