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

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.moldrecorditem.vo.*;
import cn.iocoder.yudao.module.mes.dal.dataobject.moldrecorditem.MoldRecordItemDO;
import cn.iocoder.yudao.module.mes.service.moldrecorditem.MoldRecordItemService;

@Tag(name = "管理后台 - 维保方案")
@RestController
@RequestMapping("/mes/mold-record-item")
@Validated
public class MoldRecordItemController {

    @Resource
    private MoldRecordItemService moldRecordItemService;

    @PostMapping("/create")
    @Operation(summary = "创建维保方案")
    @PreAuthorize("@ss.hasPermission('mes:mold-record-item:create')")
    public CommonResult<Long> createMoldRecordItem(@Valid @RequestBody MoldRecordItemSaveReqVO createReqVO) {
        return success(moldRecordItemService.createMoldRecordItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新维保方案")
    @PreAuthorize("@ss.hasPermission('mes:mold-record-item:update')")
    public CommonResult<Boolean> updateMoldRecordItem(@Valid @RequestBody MoldRecordItemSaveReqVO updateReqVO) {
        moldRecordItemService.updateMoldRecordItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除维保方案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:mold-record-item:delete')")
    public CommonResult<Boolean> deleteMoldRecordItem(@RequestParam("id") Long id) {
        moldRecordItemService.deleteMoldRecordItem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得维保方案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:mold-record-item:query')")
    public CommonResult<MoldRecordItemRespVO> getMoldRecordItem(@RequestParam("id") Long id) {
        MoldRecordItemDO moldRecordItem = moldRecordItemService.getMoldRecordItem(id);
        return success(BeanUtils.toBean(moldRecordItem, MoldRecordItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得维保方案分页")
    @PreAuthorize("@ss.hasPermission('mes:mold-record-item:query')")
    public CommonResult<PageResult<MoldRecordItemRespVO>> getMoldRecordItemPage(@Valid MoldRecordItemPageReqVO pageReqVO) {
        PageResult<MoldRecordItemDO> pageResult = moldRecordItemService.getMoldRecordItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MoldRecordItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出维保方案 Excel")
    @PreAuthorize("@ss.hasPermission('mes:mold-record-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMoldRecordItemExcel(@Valid MoldRecordItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MoldRecordItemDO> list = moldRecordItemService.getMoldRecordItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "维保方案.xls", "数据", MoldRecordItemRespVO.class,
                        BeanUtils.toBean(list, MoldRecordItemRespVO.class));
    }

}