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.CmcGaussNegative;
import com.ruoyi.web.calculate.service.ICmcGaussNegativeService;


@RestController
@RequestMapping("/calculate/gaussNegative")
public class CmcGaussNegativeController 
{
    
    @Autowired
    private ICmcGaussNegativeService cmcGaussNegativeService;

     /**
     * 导入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 cmcGaussNegativeService.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<CmcGaussNegative> dataList)
    {
        if (dataList == null || dataList.isEmpty()) {
            throw new RuntimeException("没有需要导出的数据");
        }
        
        List<CmcGaussNegative> exportList = new ArrayList<>();
        for (CmcGaussNegative item : dataList) {
            CmcGaussNegative exportItem = new CmcGaussNegative();
            exportItem.setPointNumber(item.getPointNumber());
            exportItem.setCoordinateSystem(item.getCoordinateSystem());
            exportItem.setLongitude(item.getLongitude());
            exportItem.setLongitudePosition(item.getLongitudePosition());
            exportItem.setLatitude(item.getLatitude());
            exportItem.setLatitudePosition(item.getLatitudePosition());
            exportItem.setBand(item.getBand());
            exportItem.setBandwidth(item.getBandwidth());
            exportItem.setProjectionHeight(item.getProjectionHeight());
            exportItem.setGaussX(item.getGaussX());
            exportItem.setGaussY(item.getGaussY());
            exportList.add(exportItem);
        }
        
        ExcelUtil<CmcGaussNegative> util = new ExcelUtil<CmcGaussNegative>(CmcGaussNegative.class);
        util.exportExcel(response, exportList, "高斯反算结果");
    }

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