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.supplier.BpmOASupplierPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.supplier.BpmOASupplierProductRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.supplier.BpmOASupplierRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.supplier.BpmOASupplierSaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOASupplierDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOASupplierProductDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOASupplierService;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOaSupplierProductService;
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.security.access.prepost.PreAuthorize;
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 = "管理后台 - OA供应商审核")
@RestController
@RequestMapping("/bpm/oa-supplier")
@Validated
public class BpmOaSupplierController {

    @Resource
    private BpmOASupplierService oaSupplierService;
    @Resource
    private BpmOaSupplierProductService oaSupplierProductService;


    @PostMapping("/create")
    @Operation(summary = "创建OA供应商审核")
    public CommonResult<Long> createOaSupplier(@Valid @RequestBody BpmOASupplierSaveReqVO createReqVO) {
        return success(oaSupplierService.createOaSupplier(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新OA供应商审核")
    public CommonResult<Boolean> updateOaSupplier(@Valid @RequestBody BpmOASupplierSaveReqVO updateReqVO) {
        oaSupplierService.updateOaSupplier(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除OA供应商审核")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteOaSupplier(@RequestParam("id") Long id) {
        oaSupplierService.deleteOaSupplier(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得OA供应商审核")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOASupplierRespVO> getOaSupplier(@RequestParam("id") Long id) {
        BpmOASupplierDO oaSupplier = oaSupplierService.getOaSupplier(id);
        BpmOASupplierRespVO vo = BeanUtils.toBean(oaSupplier, BpmOASupplierRespVO.class);
        List<BpmOASupplierProductDO> items = oaSupplierProductService.getByOaSupplierId(id);
        List<BpmOASupplierProductRespVO> itemVos = BeanUtils.toBean(items, BpmOASupplierProductRespVO.class);
        vo.setItems(itemVos);
        return success(vo);
    }

    @GetMapping("/page")
    @Operation(summary = "获得OA供应商审核分页")
    public CommonResult<PageResult<BpmOASupplierRespVO>> getOaSupplierPage(@Valid BpmOASupplierPageReqVO pageReqVO) {
        PageResult<BpmOASupplierDO> pageResult = oaSupplierService.getOaSupplierPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BpmOASupplierRespVO.class));
    }

}
