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.CmcDMSToDegree;
import com.ruoyi.web.calculate.domain.CmcDegreeToRadian;
import com.ruoyi.web.calculate.service.ICmcDegreeToRadianService;

/**
 * 角度度转弧度控制器
 * 
 * @author ruoyi
 * @date 2026-06-17
 */
@RestController
@RequestMapping("/calculate/degreeToRadian")
public class CmcDegreeToRadianController {

    @Autowired
    private ICmcDegreeToRadianService cmcDegreeToRadianService;

    /**
     * 导入Excel数据
     */
    @PostMapping("/import")
    public AjaxResult importExcel(@RequestParam("file") MultipartFile file) {
        return cmcDegreeToRadianService.importExcelData(file);
    }

    /**
     * 导出角度度转弧度列表
     */
    @Log(title = "角度度转弧度", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody List<CmcDegreeToRadian> dataList)
    {
        if (dataList == null || dataList.isEmpty()) {
            throw new RuntimeException("没有需要导出的数据");
        }
        
        List<CmcDegreeToRadian> exportList = new ArrayList<>();
        for (CmcDegreeToRadian item : dataList) {
            CmcDegreeToRadian exportItem = new CmcDegreeToRadian();   
            exportItem.setPointNumber(item.getPointNumber());
            exportItem.setLongitude(item.getLongitude());
            exportItem.setLatitude(item.getLatitude());
            exportItem.setLongitudeRadian(item.getLongitudeRadian());
            exportItem.setLatitudeRadian(item.getLatitudeRadian());
            exportList.add(exportItem);
        }
        
        ExcelUtil<CmcDegreeToRadian> util = new ExcelUtil<CmcDegreeToRadian>(CmcDegreeToRadian.class);
        util.exportExcel(response, exportList, "角度度转弧度结果");
    }

    /**
     * 执行角度度转弧度计算
     */
    @PostMapping("/calculate")
    public AjaxResult calculateDegreeToRadian(@RequestBody List<CmcDegreeToRadian> dataList) {
        try {
            List<CmcDegreeToRadian> results = cmcDegreeToRadianService.calculateDegreeToRadian(dataList);
            return AjaxResult.success("计算完成", results);
        } catch (Exception e) {
            return AjaxResult.error("计算失败：" + e.getMessage());
        }
    }
}
