package cn.iocoder.yudao.module.smartfactory.controller.admin.factorypackingline;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorypackingline.vo.FactoryPackingLinePageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorypackingline.vo.FactoryPackingLineRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorypackingline.vo.FactoryPackingLineSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factorypackingline.FactoryPackingLineDO;
import cn.iocoder.yudao.module.smartfactory.service.factorypackingline.FactoryPackingLineService;
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.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 打包线配置")
@RestController
@RequestMapping("/smartfactory/factory-packing-line")
@Validated
public class FactoryPackingLineController {

    @Resource
    private FactoryPackingLineService factoryPackingLineService;

    @PostMapping("/create")
    @Operation(summary = "创建打包线配置")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-packing-line:create')")
    public CommonResult<Long> createFactoryPackingLine(@Valid @RequestBody FactoryPackingLineSaveReqVO createReqVO) {
        return success(factoryPackingLineService.createFactoryPackingLine(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新打包线配置")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-packing-line:update')")
    public CommonResult<Boolean> updateFactoryPackingLine(@Valid @RequestBody FactoryPackingLineSaveReqVO updateReqVO) {
        factoryPackingLineService.updateFactoryPackingLine(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除打包线配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-packing-line:delete')")
    public CommonResult<Boolean> deleteFactoryPackingLine(@RequestParam("id") Long id) {
        factoryPackingLineService.deleteFactoryPackingLine(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得打包线配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-packing-line:query')")
    public CommonResult<FactoryPackingLineRespVO> getFactoryPackingLine(@RequestParam("id") Long id) {
        FactoryPackingLineDO factoryPackingLine = factoryPackingLineService.getFactoryPackingLine(id);
        return success(BeanUtils.toBean(factoryPackingLine, FactoryPackingLineRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得打包线配置分页")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-packing-line:query')")
    public CommonResult<PageResult<FactoryPackingLineRespVO>> getFactoryPackingLinePage(@Valid FactoryPackingLinePageReqVO pageReqVO) {
        PageResult<FactoryPackingLineRespVO> pageResult = factoryPackingLineService.getFactoryPackingLinePage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出打包线配置 Excel")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-packing-line:export')")
    @OperateLog(type = EXPORT)
    public void exportFactoryPackingLineExcel(@Valid FactoryPackingLinePageReqVO pageReqVO,
                                              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FactoryPackingLineRespVO> list = factoryPackingLineService.getFactoryPackingLinePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "打包线配置.xls", "数据", FactoryPackingLineRespVO.class, list);
    }

}
