package com.xdap.self_development.controller;


import com.definesys.mpaas.common.http.Response;
import com.xdap.self_development.controller.request.DataEntryEngineModelPageRequest;
import com.xdap.self_development.domain.dto.DataEntryEngineModelDto;
import com.xdap.self_development.domain.dto.PageResultDTO;
import com.xdap.self_development.service.DataEntryEngineModelService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

/**
 * 发动机型号数据控制器
 * 提供发动机型号数据相关的功能，包括Excel导入导出、添加、删除、分页查询等
 *
 * @version 1.0
 * @since 2025
 */
@RestController
@RequestMapping("/custom/dataEntryEngineModel")
public class DataEntryEngineModelController {


    @Resource
    private DataEntryEngineModelService entryEngineModelService;

    /**
     * 同步新模板参数
     *
     * @param modelId 机型ID
     */
    @GetMapping("/synchronizeParams")
    public Response synchronizeParams(
            @NotBlank(message = "机型不能为空") String modelId,
            @NotBlank(message = "用户不能为空") String userId
    ) {
        Integer version = entryEngineModelService.synchronizeParams(modelId, userId);
        return Response.ok().setMessage("同步成功").data(version);
    }

    /**
     * Excel导入
     *
     * @param file   文件
     * @param userId 用户ID
     * @param ycOrOt 用途标识
     * @return 操作结果响应
     */
    @PostMapping("/excelImport")
    public Response excelImport(@RequestParam(value = "file") MultipartFile file
    ,String userId,String ycOrOt) {
        if (file == null || file.isEmpty()) {
            return Response.error("请选择要导入的文件");
        }
        // 文件类型校验
        String filename = file.getOriginalFilename();
        if (filename == null || (!filename.endsWith(".xlsx") && !filename.endsWith(".xls"))) {
            return Response.error("只支持.xlsx或.xls格式的Excel文件");
        }
        try {
            entryEngineModelService.excelImport(file,userId,ycOrOt);
        } catch (Exception e) {
            return Response.error("导入失败"+e.getMessage());
        }
        return Response.ok().data("导入成功");
    }
    /**
     * 导出Excel模板
     *
     * @param response HTTP响应
     * @return 操作结果响应
     */
    @GetMapping("/excelExport")
    public Response excelExport(HttpServletResponse response) {
        try {
            entryEngineModelService.excelExport(response);
        } catch (Exception e) {
            return Response.error("导出失败" + e.getMessage());
        }
        return Response.ok().data("导出成功");
    }

    /**
     * 添加-单条写入
     *
     * @param pageRequest 页面请求对象
     * @return 操作结果响应
     */
    @PostMapping("/insert")
    public Response insert(@RequestBody DataEntryEngineModelPageRequest pageRequest) {
        entryEngineModelService.insert(pageRequest,pageRequest.getLoginUserId());
        return Response.ok();
    }

    /**
     * 删除机型
     *
     * @param modelId 机型ID
     * @return 操作结果响应
     */
    @GetMapping("/delete")
    public Response delete(
            @RequestParam String modelId
    ) {
        entryEngineModelService.delete(modelId);
        return Response.ok().setMessage("删除成功");
    }

    /**
     * 分页查询
     *
     * @param pageRequest 页面请求对象
     * @return 分页查询结果
     */
    @PostMapping("/selectByPage")
    public Response selectByConditionPage(@RequestBody DataEntryEngineModelPageRequest pageRequest) {
        PageResultDTO<DataEntryEngineModelDto> tPageResultDTO = entryEngineModelService.selectByConditionPage(pageRequest);
        return Response.ok().data(tPageResultDTO);
    }

    /**
     * 多版本机型分页查询
     *
     * @return 分页查询结果
     */
    @GetMapping("/selectVersionByPage")
    public Response selectVersionByConditionPage(
            @NotBlank(message = "用户不能为空") @RequestParam String userId
    ) {
        PageResultDTO<DataEntryEngineModelDto> tPageResultDTO =
                entryEngineModelService.selectVersionByConditionPage(userId);
        return Response.ok().data(tPageResultDTO);
    }
}

