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

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
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.module.mes.controller.admin.record.vo.RecordTemplatePageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.record.vo.RecordTemplateRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.record.vo.RecordTemplateSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.record.*;
import cn.iocoder.yudao.module.mes.service.record.RecordTemplateService;
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.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 记录表")
@RestController
@RequestMapping("/mes/record-template")
@Validated
public class RecordTemplateController {

    @Resource
    private RecordTemplateService recordTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建记录表")
    @PreAuthorize("@ss.hasPermission('mes:record-template:create')")
    public CommonResult<Long> createRecordTemplate(@Valid @RequestBody RecordTemplateSaveReqVO createReqVO) {
        return success(recordTemplateService.createRecordTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新记录表")
    @PreAuthorize("@ss.hasPermission('mes:record-template:update')")
    public CommonResult<Boolean> updateRecordTemplate(@Valid @RequestBody RecordTemplateSaveReqVO updateReqVO) {
        recordTemplateService.updateRecordTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除记录表")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:record-template:delete')")
    public CommonResult<Boolean> deleteRecordTemplate(@RequestParam("id") Long id) {
        recordTemplateService.deleteRecordTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得记录表")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<RecordTemplateRespVO> getRecordTemplate(@RequestParam("id") Long id) {
        RecordTemplateDO recordTemplate = recordTemplateService.getRecordTemplate(id);
        return success(BeanUtils.toBean(recordTemplate, RecordTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得记录表分页")
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<PageResult<RecordTemplateRespVO>> getRecordTemplatePage(@Valid RecordTemplatePageReqVO pageReqVO) {
        PageResult<RecordTemplateDO> pageResult = recordTemplateService.getRecordTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecordTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出记录表 Excel")
    @PreAuthorize("@ss.hasPermission('mes:record-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecordTemplateExcel(@Valid RecordTemplatePageReqVO pageReqVO,
                                          HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecordTemplateDO> list = recordTemplateService.getRecordTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "记录表.xls", "数据", RecordTemplateRespVO.class,
                BeanUtils.toBean(list, RecordTemplateRespVO.class));
    }
// ==================== 子表（碎浆记录） ====================

    @GetMapping("/record-suijiang/page")
    @Operation(summary = "获得碎浆记录分页")
    @Parameter(name = "recordId", description = "组织id")
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<PageResult<RecordSuijiangDO>> getRecordSuijiangPage(PageParam pageReqVO,
                                                                            @RequestParam("recordId") Long recordId) {
        return success(recordTemplateService.getRecordSuijiangPage(pageReqVO, recordId));
    }

    @PostMapping("/record-suijiang/create")
    @Operation(summary = "创建碎浆记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:create')")
    public CommonResult<Long> createRecordSuijiang(@Valid @RequestBody RecordSuijiangDO recordSuijiang) {
        return success(recordTemplateService.createRecordSuijiang(recordSuijiang));
    }

    @PutMapping("/record-suijiang/update")
    @Operation(summary = "更新碎浆记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:update')")
    public CommonResult<Boolean> updateRecordSuijiang(@Valid @RequestBody RecordSuijiangDO recordSuijiang) {
        recordTemplateService.updateRecordSuijiang(recordSuijiang);
        return success(true);
    }

    @DeleteMapping("/record-suijiang/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除碎浆记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:delete')")
    public CommonResult<Boolean> deleteRecordSuijiang(@RequestParam("id") Long id) {
        recordTemplateService.deleteRecordSuijiang(id);
        return success(true);
    }

    @GetMapping("/record-suijiang/get")
    @Operation(summary = "获得碎浆记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<RecordSuijiangDO> getRecordSuijiang(@RequestParam("id") Long id) {
        return success(recordTemplateService.getRecordSuijiang(id));
    }

    // ==================== 子表（成型记录） ====================

    @GetMapping("/record-chengxing/page")
    @Operation(summary = "获得成型记录分页")
    @Parameter(name = "recordId", description = "记录id")
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<PageResult<RecordChengxingDO>> getRecordChengxingPage(PageParam pageReqVO,
                                                                              @RequestParam("recordId") Long recordId) {
        return success(recordTemplateService.getRecordChengxingPage(pageReqVO, recordId));
    }

    @PostMapping("/record-chengxing/create")
    @Operation(summary = "创建成型记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:create')")
    public CommonResult<Long> createRecordChengxing(@Valid @RequestBody RecordChengxingDO recordChengxing) {
        return success(recordTemplateService.createRecordChengxing(recordChengxing));
    }

    @PutMapping("/record-chengxing/update")
    @Operation(summary = "更新成型记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:update')")
    public CommonResult<Boolean> updateRecordChengxing(@Valid @RequestBody RecordChengxingDO recordChengxing) {
        recordTemplateService.updateRecordChengxing(recordChengxing);
        return success(true);
    }

    @DeleteMapping("/record-chengxing/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除成型记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:delete')")
    public CommonResult<Boolean> deleteRecordChengxing(@RequestParam("id") Long id) {
        recordTemplateService.deleteRecordChengxing(id);
        return success(true);
    }

    @GetMapping("/record-chengxing/get")
    @Operation(summary = "获得成型记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<RecordChengxingDO> getRecordChengxing(@RequestParam("id") Long id) {
        return success(recordTemplateService.getRecordChengxing(id));
    }

    // ==================== 子表（热压记录） ====================

    @GetMapping("/record-reya/page")
    @Operation(summary = "获得热压记录分页")
    @Parameter(name = "recordId", description = "记录id")
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<PageResult<RecordReyaDO>> getRecordReyaPage(PageParam pageReqVO,
                                                                    @RequestParam("recordId") Long recordId) {
        return success(recordTemplateService.getRecordReyaPage(pageReqVO, recordId));
    }

    @PostMapping("/record-reya/create")
    @Operation(summary = "创建热压记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:create')")
    public CommonResult<Long> createRecordReya(@Valid @RequestBody RecordReyaDO recordReya) {
        return success(recordTemplateService.createRecordReya(recordReya));
    }

    @PutMapping("/record-reya/update")
    @Operation(summary = "更新热压记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:update')")
    public CommonResult<Boolean> updateRecordReya(@Valid @RequestBody RecordReyaDO recordReya) {
        recordTemplateService.updateRecordReya(recordReya);
        return success(true);
    }

    @DeleteMapping("/record-reya/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除热压记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:delete')")
    public CommonResult<Boolean> deleteRecordReya(@RequestParam("id") Long id) {
        recordTemplateService.deleteRecordReya(id);
        return success(true);
    }

    @GetMapping("/record-reya/get")
    @Operation(summary = "获得热压记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<RecordReyaDO> getRecordReya(@RequestParam("id") Long id) {
        return success(recordTemplateService.getRecordReya(id));
    }

    // ==================== 子表（切边记录） ====================

    @GetMapping("/record-qiebian/page")
    @Operation(summary = "获得切边记录分页")
    @Parameter(name = "recordId", description = "记录id")
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<PageResult<RecordQiebianDO>> getRecordQiebianPage(PageParam pageReqVO,
                                                                          @RequestParam("recordId") Long recordId) {
        return success(recordTemplateService.getRecordQiebianPage(pageReqVO, recordId));
    }

    @PostMapping("/record-qiebian/create")
    @Operation(summary = "创建切边记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:create')")
    public CommonResult<Long> createRecordQiebian(@Valid @RequestBody RecordQiebianDO recordQiebian) {
        return success(recordTemplateService.createRecordQiebian(recordQiebian));
    }

    @PutMapping("/record-qiebian/update")
    @Operation(summary = "更新切边记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:update')")
    public CommonResult<Boolean> updateRecordQiebian(@Valid @RequestBody RecordQiebianDO recordQiebian) {
        recordTemplateService.updateRecordQiebian(recordQiebian);
        return success(true);
    }

    @DeleteMapping("/record-qiebian/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除切边记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:delete')")
    public CommonResult<Boolean> deleteRecordQiebian(@RequestParam("id") Long id) {
        recordTemplateService.deleteRecordQiebian(id);
        return success(true);
    }

    @GetMapping("/record-qiebian/get")
    @Operation(summary = "获得切边记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<RecordQiebianDO> getRecordQiebian(@RequestParam("id") Long id) {
        return success(recordTemplateService.getRecordQiebian(id));
    }
// ==================== 子表（打包记录） ====================

    @GetMapping("/record-dabao/page")
    @Operation(summary = "获得打包记录分页")
    @Parameter(name = "recordId", description = "记录id")
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<PageResult<RecordDabaoDO>> getRecordDabaoPage(PageParam pageReqVO,
                                                                      @RequestParam("recordId") Long recordId) {
        return success(recordTemplateService.getRecordDabaoPage(pageReqVO, recordId));
    }

    @PostMapping("/record-dabao/create")
    @Operation(summary = "创建打包记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:create')")
    public CommonResult<Long> createRecordDabao(@Valid @RequestBody RecordDabaoDO recordDabao) {
        return success(recordTemplateService.createRecordDabao(recordDabao));
    }

    @PutMapping("/record-dabao/update")
    @Operation(summary = "更新打包记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:update')")
    public CommonResult<Boolean> updateRecordDabao(@Valid @RequestBody RecordDabaoDO recordDabao) {
        recordTemplateService.updateRecordDabao(recordDabao);
        return success(true);
    }

    @DeleteMapping("/record-dabao/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除打包记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:delete')")
    public CommonResult<Boolean> deleteRecordDabao(@RequestParam("id") Long id) {
        recordTemplateService.deleteRecordDabao(id);
        return success(true);
    }

    @GetMapping("/record-dabao/get")
    @Operation(summary = "获得打包记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<RecordDabaoDO> getRecordDabao(@RequestParam("id") Long id) {
        return success(recordTemplateService.getRecordDabao(id));
    }

    // ==================== 子表（打浆记录） ====================

    @GetMapping("/record-dajiang/page")
    @Operation(summary = "获得打浆记录分页")
    @Parameter(name = "recordId", description = "记录id")
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<PageResult<RecordDajiangDO>> getRecordDajiangPage(PageParam pageReqVO,
                                                                          @RequestParam("recordId") Long recordId) {
        return success(recordTemplateService.getRecordDajiangPage(pageReqVO, recordId));
    }

    @PostMapping("/record-dajiang/create")
    @Operation(summary = "创建打浆记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:create')")
    public CommonResult<Long> createRecordDajiang(@Valid @RequestBody RecordDajiangDO recordDajiang) {
        return success(recordTemplateService.createRecordDajiang(recordDajiang));
    }

    @PutMapping("/record-dajiang/update")
    @Operation(summary = "更新打浆记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:update')")
    public CommonResult<Boolean> updateRecordDajiang(@Valid @RequestBody RecordDajiangDO recordDajiang) {
        recordTemplateService.updateRecordDajiang(recordDajiang);
        return success(true);
    }

    @DeleteMapping("/record-dajiang/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除打浆记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:delete')")
    public CommonResult<Boolean> deleteRecordDajiang(@RequestParam("id") Long id) {
        recordTemplateService.deleteRecordDajiang(id);
        return success(true);
    }

    @GetMapping("/record-dajiang/get")
    @Operation(summary = "获得打浆记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<RecordDajiangDO> getRecordDajiang(@RequestParam("id") Long id) {
        return success(recordTemplateService.getRecordDajiang(id));
    }
    // ==================== 子表（干燥记录） ====================

    @GetMapping("/record-ganzao/page")
    @Operation(summary = "获得干燥记录分页")
    @Parameter(name = "recordId", description = "记录id")
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<PageResult<RecordGanzaoDO>> getRecordGanzaoPage(PageParam pageReqVO,
                                                                        @RequestParam("recordId") Long recordId) {
        return success(recordTemplateService.getRecordGanzaoPage(pageReqVO, recordId));
    }

    @PostMapping("/record-ganzao/create")
    @Operation(summary = "创建干燥记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:create')")
    public CommonResult<Long> createRecordGanzao(@Valid @RequestBody RecordGanzaoDO recordGanzao) {
        return success(recordTemplateService.createRecordGanzao(recordGanzao));
    }

    @PutMapping("/record-ganzao/update")
    @Operation(summary = "更新干燥记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:update')")
    public CommonResult<Boolean> updateRecordGanzao(@Valid @RequestBody RecordGanzaoDO recordGanzao) {
        recordTemplateService.updateRecordGanzao(recordGanzao);
        return success(true);
    }

    @DeleteMapping("/record-ganzao/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除干燥记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:delete')")
    public CommonResult<Boolean> deleteRecordGanzao(@RequestParam("id") Long id) {
        recordTemplateService.deleteRecordGanzao(id);
        return success(true);
    }

    @GetMapping("/record-ganzao/get")
    @Operation(summary = "获得干燥记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<RecordGanzaoDO> getRecordGanzao(@RequestParam("id") Long id) {
        return success(recordTemplateService.getRecordGanzao(id));
    }

    // ==================== 子表（加湿记录） ====================

    @GetMapping("/record-jiashi/page")
    @Operation(summary = "获得加湿记录分页")
    @Parameter(name = "recordId", description = "记录id")
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<PageResult<RecordJiashiDO>> getRecordJiashiPage(PageParam pageReqVO,
                                                                        @RequestParam("recordId") Long recordId) {
        return success(recordTemplateService.getRecordJiashiPage(pageReqVO, recordId));
    }

    @PostMapping("/record-jiashi/create")
    @Operation(summary = "创建加湿记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:create')")
    public CommonResult<Long> createRecordJiashi(@Valid @RequestBody RecordJiashiDO recordJiashi) {
        return success(recordTemplateService.createRecordJiashi(recordJiashi));
    }

    @PutMapping("/record-jiashi/update")
    @Operation(summary = "更新加湿记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:update')")
    public CommonResult<Boolean> updateRecordJiashi(@Valid @RequestBody RecordJiashiDO recordJiashi) {
        recordTemplateService.updateRecordJiashi(recordJiashi);
        return success(true);
    }

    @DeleteMapping("/record-jiashi/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除加湿记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:delete')")
    public CommonResult<Boolean> deleteRecordJiashi(@RequestParam("id") Long id) {
        recordTemplateService.deleteRecordJiashi(id);
        return success(true);
    }

    @GetMapping("/record-jiashi/get")
    @Operation(summary = "获得加湿记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<RecordJiashiDO> getRecordJiashi(@RequestParam("id") Long id) {
        return success(recordTemplateService.getRecordJiashi(id));
    }

    // ==================== 子表（检验记录） ====================

    @GetMapping("/record-pinjian/page")
    @Operation(summary = "获得检验记录分页")
    @Parameter(name = "recordId", description = "记录id")
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<PageResult<RecordPinjianDO>> getRecordPinjianPage(PageParam pageReqVO,
                                                                          @RequestParam("recordId") Long recordId) {
        return success(recordTemplateService.getRecordPinjianPage(pageReqVO, recordId));
    }

    @PostMapping("/record-pinjian/create")
    @Operation(summary = "创建检验记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:create')")
    public CommonResult<Long> createRecordPinjian(@Valid @RequestBody RecordPinjianDO recordPinjian) {
        return success(recordTemplateService.createRecordPinjian(recordPinjian));
    }

    @PutMapping("/record-pinjian/update")
    @Operation(summary = "更新检验记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:update')")
    public CommonResult<Boolean> updateRecordPinjian(@Valid @RequestBody RecordPinjianDO recordPinjian) {
        recordTemplateService.updateRecordPinjian(recordPinjian);
        return success(true);
    }

    @DeleteMapping("/record-pinjian/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除检验记录")
    @PreAuthorize("@ss.hasPermission('mes:record-template:delete')")
    public CommonResult<Boolean> deleteRecordPinjian(@RequestParam("id") Long id) {
        recordTemplateService.deleteRecordPinjian(id);
        return success(true);
    }

    @GetMapping("/record-pinjian/get")
    @Operation(summary = "获得检验记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:record-template:query')")
    public CommonResult<RecordPinjianDO> getRecordPinjian(@RequestParam("id") Long id) {
        return success(recordTemplateService.getRecordPinjian(id));
    }
}