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

import com.cf.imes.module.executor.dal.dataobject.remainplaten.RemainPlateDO;
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 com.cf.imes.framework.common.pojo.PageParam;
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.framework.operatelog.core.annotations.OperateLog;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.cf.imes.module.executor.controller.admin.remainplate.vo.*;
import com.cf.imes.module.executor.service.remainplate.RemainPlateService;

@Tag(name = "管理后台 - 生产单余料板表 order_remain_plate_{N}")
@RestController
@RequestMapping("/executor/remain-plate")
@Validated
public class RemainPlateController {

    @Resource
    private RemainPlateService remainPlateService;

    @PostMapping("/create")
    @Operation(summary = "创建生产单余料板表}")
    @PreAuthorize("@ss.hasPermission('executor:remain-plate:create')")
    public CommonResult<Long> createRemainPlate(@Valid @RequestBody RemainPlateSaveReqVO createReqVO) {
        return success(remainPlateService.createRemainPlate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产单余料板表")
    @PreAuthorize("@ss.hasPermission('executor:remain-plate:update')")
    public CommonResult<Boolean> updateRemainPlate(@Valid @RequestBody RemainPlateSaveReqVO updateReqVO) {
        remainPlateService.updateRemainPlate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产单余料板表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('executor:remain-plate:delete')")
    public CommonResult<Boolean> deleteRemainPlate(@RequestParam("id") Long id) {
        remainPlateService.deleteRemainPlate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产单余料板表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('executor:remain-plate:query')")
    public CommonResult<RemainPlateRespVO> getRemainPlate(@RequestParam("id") Long id) {
        RemainPlateDO remainPlate = remainPlateService.getRemainPlate(id);
        return success(BeanUtils.toBean(remainPlate, RemainPlateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产单余料板表 order_remain_plate分页")
    @PreAuthorize("@ss.hasPermission('executor:remain-plate:query')")
    public CommonResult<PageResult<RemainPlateRespVO>> getRemainPlatePage(@Valid RemainPlatePageReqVO pageReqVO) {
        PageResult<RemainPlateDO> pageResult = remainPlateService.getRemainPlatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RemainPlateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产单余料板表 Excel")
    @PreAuthorize("@ss.hasPermission('executor:remain-plate:export')")
    @OperateLog(type = EXPORT)
    public void exportRemainPlateExcel(@Valid RemainPlatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RemainPlateDO> list = remainPlateService.getRemainPlatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产单余料板表.xls", "数据", RemainPlateRespVO.class,
                        BeanUtils.toBean(list, RemainPlateRespVO.class));
    }

}