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.GetMapping;
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.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.calculate.domain.CmcDMSToDegree;
import com.ruoyi.web.calculate.service.ICmcDMSToDegreeService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 度分秒转度 Controller
 * 
 * @author ruoyi
 * @date 2026-06-12
 */
@RestController
@RequestMapping("/calculate/DMSToDegree")
public class CmcDMSToDegreeController extends BaseController
{
    @Autowired
    private ICmcDMSToDegreeService cmcDMSToDegreeService;

    /**
     * 导入 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 cmcDMSToDegreeService.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<CmcDMSToDegree> dataList)
    {
        if (dataList == null || dataList.isEmpty()) {
            throw new RuntimeException("没有需要导出的数据");
        }
        
        List<CmcDMSToDegree> exportList = new ArrayList<>();
        for (CmcDMSToDegree item : dataList) {
            CmcDMSToDegree exportItem = new CmcDMSToDegree();   
            exportItem.setPointNumber(item.getPointNumber());
            exportItem.setLongitudeDMS(item.getLongitudeDMS());
            exportItem.setLatitudeDMS(item.getLatitudeDMS());
            exportItem.setLongitude(item.getLongitude());
            exportItem.setLatitude(item.getLatitude());
            exportList.add(exportItem);
        }
        
        ExcelUtil<CmcDMSToDegree> util = new ExcelUtil<CmcDMSToDegree>(CmcDMSToDegree.class);
        util.exportExcel(response, exportList, "度分秒转度结果");
    }

    /**
     * 执行度分秒转度（仅计算，不保存）
     */
    @PostMapping("/calculate")
    public AjaxResult calculate(@RequestBody List<CmcDMSToDegree> dataList)     
    {
        List<CmcDMSToDegree> result = cmcDMSToDegreeService.calculateDMSToDegree(dataList);
        return success(result);
    }
}
