package com.cf.imes.module.executor.controller.admin.rawgoods;

import com.cf.imes.module.executor.controller.admin.rawgoods.vo.RawGoodsImportRespVO;
import com.cf.imes.module.executor.controller.admin.rawgoods.vo.RawGoodsPageReqVO;
import com.cf.imes.module.executor.controller.admin.rawgoods.vo.RawGoodsRespVO;
import com.cf.imes.module.executor.controller.admin.rawgoods.vo.RawGoodsSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;

import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import static com.cf.imes.framework.common.pojo.CommonResult.success;

import com.cf.imes.framework.excel.core.util.ExcelUtils;

import com.cf.imes.module.executor.dal.dataobject.rawgoods.RawGoodsDO;
import com.cf.imes.module.executor.service.rawgoods.RawGoodsService;

@Tag(name = "管理后台 - 生产单设计商品")
@RestController
@RequestMapping("/executor/raw-goods")
@Validated
public class RawGoodsController {

    @Resource
    private RawGoodsService rawGoodsService;

    @PostMapping("/create")
    @Operation(summary = "创建生产单原设计商品")
    @PreAuthorize("@ss.hasPermission('executor:raw-goods:create')")
    public CommonResult<Long> createRawGoods(@Valid @RequestBody RawGoodsSaveReqVO createReqVO) {
        return success(rawGoodsService.createRawGoods(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产单原设计商品")
    @PreAuthorize("@ss.hasPermission('executor:raw-goods:update')")
    public CommonResult<Boolean> updateRawGoods(@Valid @RequestBody RawGoodsSaveReqVO updateReqVO) {
        rawGoodsService.updateRawGoods(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产单原设计商品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('executor:raw-goods:delete')")
    public CommonResult<Boolean> deleteRawGoods(@RequestParam("id") Long id) {
        rawGoodsService.deleteRawGoods(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产单原设计商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('executor:raw-goods:query')")
    public CommonResult<RawGoodsRespVO> getRawGoods(@RequestParam("id") Long id) {
        RawGoodsDO rawGoods = rawGoodsService.getRawGoods(id);
        return success(BeanUtils.toBean(rawGoods, RawGoodsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产单原设计商品分页")
    @PreAuthorize("@ss.hasPermission('executor:raw-goods:query')")
    public CommonResult<PageResult<RawGoodsRespVO>> getRawGoodsPage(@Valid RawGoodsPageReqVO pageReqVO) {
        PageResult<RawGoodsDO> pageResult = rawGoodsService.getRawGoodsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RawGoodsRespVO.class));
    }

    @GetMapping("/getByOrder")
    @Operation(summary = "获得生产单原设计商品")
    @Parameter(name = "orderId", description = "生产单号", required = true, example = "1776888013545013248")
    @PreAuthorize("@ss.hasPermission('executor:raw-goods:query')")
    public CommonResult<List<RawGoodsRespVO>> getRawGoodsByOrder(@RequestParam("orderId") Long orderId) {
        List<RawGoodsDO> rawGoods = rawGoodsService.getRawGoodsByOrder(orderId);
        return success(BeanUtils.toBean(rawGoods, RawGoodsRespVO.class));
    }
}