package cn.iocoder.yudao.module.wms.controller.admin.productionline;

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.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wms.controller.admin.productionline.vo.*;
import cn.iocoder.yudao.module.wms.dal.dataobject.productionline.ProductionLineDO;
import cn.iocoder.yudao.module.wms.service.productionline.ProductionLineService;

@Tag(name = "管理后台 - 产线管理")
@RestController
@RequestMapping("/wms/production-line")
@Validated
public class ProductionLineController {

    @Resource
    private ProductionLineService productionLineService;

    @PostMapping("/create")
    @Operation(summary = "创建产线管理")
    @PreAuthorize("@ss.hasPermission('wms:production-line:create')")
    public CommonResult<Long> createProductionLine(@Valid @RequestBody ProductionLineSaveReqVO createReqVO) {
        return success(productionLineService.createProductionLine(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产线管理")
    @PreAuthorize("@ss.hasPermission('wms:production-line:update')")
    public CommonResult<Boolean> updateProductionLine(@Valid @RequestBody ProductionLineSaveReqVO updateReqVO) {
        productionLineService.updateProductionLine(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产线管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:production-line:delete')")
    public CommonResult<Boolean> deleteProductionLine(@RequestParam("id") Long id) {
        productionLineService.deleteProductionLine(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产线管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:production-line:query')")
    public CommonResult<ProductionLineRespVO> getProductionLine(@RequestParam("id") Long id) {
        ProductionLineDO productionLine = productionLineService.getProductionLine(id);
        return success(BeanUtils.toBean(productionLine, ProductionLineRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产线管理分页")
    @PreAuthorize("@ss.hasPermission('wms:production-line:query')")
    public CommonResult<PageResult<ProductionLineRespVO>> getProductionLinePage(@Valid ProductionLinePageReqVO pageReqVO) {
        PageResult<ProductionLineDO> pageResult = productionLineService.getProductionLinePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductionLineRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产线管理 Excel")
    @PreAuthorize("@ss.hasPermission('wms:production-line:export')")
    @OperateLog(type = EXPORT)
    public void exportProductionLineExcel(@Valid ProductionLinePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductionLineDO> list = productionLineService.getProductionLinePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产线管理.xls", "数据", ProductionLineRespVO.class,
                        BeanUtils.toBean(list, ProductionLineRespVO.class));
    }

}