package cn.iocoder.yudao.module.mes.controller.admin.stockindetail;

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.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mes.controller.admin.stockindetail.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.stockindetail.StockInDetailDO;
import cn.iocoder.yudao.module.mes.service.stockindetail.StockInDetailService;

@Tag(name = "管理后台 - 生产入库分配明细")
@RestController
@RequestMapping("/mes/stock-in-detail")
@Validated
public class StockInDetailController {

    @Resource
    private StockInDetailService stockInDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建生产入库分配明细")
    @PreAuthorize("@ss.hasPermission('mes:stock-in-detail:create')")
    public CommonResult<Long> createStockInDetail(@Valid @RequestBody StockInDetailSaveReqVO createReqVO) {
        return success(stockInDetailService.createStockInDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产入库分配明细")
    @PreAuthorize("@ss.hasPermission('mes:stock-in-detail:update')")
    public CommonResult<Boolean> updateStockInDetail(@Valid @RequestBody StockInDetailSaveReqVO updateReqVO) {
        stockInDetailService.updateStockInDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产入库分配明细")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:stock-in-detail:delete')")
    public CommonResult<Boolean> deleteStockInDetail(@RequestParam("id") Long id) {
        stockInDetailService.deleteStockInDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产入库分配明细")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:stock-in-detail:query')")
    public CommonResult<StockInDetailRespVO> getStockInDetail(@RequestParam("id") Long id) {
        StockInDetailDO stockInDetail = stockInDetailService.getStockInDetail(id);
        return success(BeanUtils.toBean(stockInDetail, StockInDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产入库分配明细分页")
    @PreAuthorize("@ss.hasPermission('mes:stock-in-detail:query')")
    public CommonResult<PageResult<StockInDetailRespVO>> getStockInDetailPage(@Valid StockInDetailPageReqVO pageReqVO) {
        PageResult<StockInDetailDO> pageResult = stockInDetailService.getStockInDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StockInDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产入库分配明细 Excel")
    @PreAuthorize("@ss.hasPermission('mes:stock-in-detail:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStockInDetailExcel(@Valid StockInDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StockInDetailDO> list = stockInDetailService.getStockInDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产入库分配明细.xls", "数据", StockInDetailRespVO.class,
                        BeanUtils.toBean(list, StockInDetailRespVO.class));
    }

    @PutMapping("/updateStockInStatus")
    @Operation(summary = "更新生产入库单的状态")
    @PreAuthorize("@ss.hasPermission('erp:stock-in:update-status')")
    public CommonResult<Boolean> updateStockInStatus(@RequestParam("id") Long id,
                                                     @RequestParam("status") Integer status) {
        stockInDetailService.updateStockInStatus(id, status);
        return success(true);
    }
}