package com.ruoyi.web.calculate.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.calculate.domain.CmcGaussBandChange;
import com.ruoyi.web.calculate.service.ICmcGaussBandChangeService;

@RestController
@RequestMapping("/calculate/gaussBandChange")
public class CmcGaussBandChangeController {

    @Autowired
    private ICmcGaussBandChangeService cmcGaussBandChangeService;

    /**
     * 导入Excel数据
     */
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file) {
        try {
            // 检查文件是否为空
            if (file.isEmpty()) {
                return AjaxResult.error("请选择要导入的文件！");
            }

            // 检查文件格式
            String fileName = file.getOriginalFilename();
            if (fileName == null || (!fileName.endsWith(".xls") && !fileName.endsWith(".xlsx"))) {
                return AjaxResult.error("请上传Excel文件（.xls或.xlsx格式）！");
            }

            // 调用Service层解析Excel并返回数据
            return cmcGaussBandChangeService.importExcelData(file);

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("导入失败：" + e.getMessage());
        }
    }

    /**
     * 导出高斯反算列表
     */
    @Log(title = "高斯换带", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody List<CmcGaussBandChange> dataList) {
        if (dataList == null || dataList.isEmpty()) {
            throw new RuntimeException("没有需要导出的数据");
        }

        List<CmcGaussBandChange> exportList = new ArrayList<>();
        for (CmcGaussBandChange item : dataList) {
            CmcGaussBandChange exportItem = new CmcGaussBandChange();
            exportItem.setPointNumber(item.getPointNumber());
            exportItem.setCoordinateSystem(item.getCoordinateSystem());
            exportItem.setGaussX(item.getGaussX());
            exportItem.setGaussY(item.getGaussY());
            exportItem.setBand(item.getBand());
            exportItem.setBandwidth(item.getBandwidth());
            exportItem.setNewBand(item.getNewBand());
            exportItem.setNewBandwidth(item.getNewBandwidth());
            exportItem.setLongitudePosition(item.getLongitudePosition());
            exportItem.setLatitudePosition(item.getLatitudePosition());
            exportItem.setNewGaussX(item.getNewGaussX());
            exportItem.setNewGaussY(item.getNewGaussY());
            exportItem.setProjectionHeight(item.getProjectionHeight());
            exportList.add(exportItem);
        }

        ExcelUtil<CmcGaussBandChange> util = new ExcelUtil<CmcGaussBandChange>(CmcGaussBandChange.class);
        util.exportExcel(response, exportList, "高斯换带结果");
    }

    /**
     * 执行高斯换带（仅计算，不保存）
     */
    @PostMapping("/calculate")
    public AjaxResult calculate(@RequestBody List<CmcGaussBandChange> dataList) {
        List<CmcGaussBandChange> result = cmcGaussBandChangeService.calculateGaussBandChange(dataList);
        return AjaxResult.success(result);
    }

}
