package com.ruoyi.cms.service.impl;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.cms.domain.SubwayLine;
import com.ruoyi.cms.mapper.SubwayLineMapper;
import com.ruoyi.cms.service.ISubwayLineService;
import com.ruoyi.cms.util.Station;
import com.ruoyi.cms.util.SubwayData;
import com.ruoyi.cms.util.SubwayLineJson;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.ruoyi.cms.mapper.SubwayStationMapper;
import com.ruoyi.cms.domain.SubwayStation;
import com.ruoyi.cms.service.ISubwayStationService;

import static com.ruoyi.cms.service.impl.SubwayLineServiceImpl.removeSubwayCache;


/**
 * 地铁站点Service业务层处理
 *
 * @author Lishundong
 * @date 2024-11-12
 */
@Service
public class SubwayStationServiceImpl extends ServiceImpl<SubwayStationMapper,SubwayStation> implements ISubwayStationService
{
    @Autowired
    private SubwayStationMapper subwayStationMapper;
    @Autowired
    private ISubwayLineService lineService;
    @Autowired
    private SubwayLineMapper subwayLineMapper;

    /**
     * 查询地铁站点
     *
     * @param stationId 地铁站点主键
     * @return 地铁站点
     */
    @Override
    public SubwayStation selectSubwayStationByStationId(Long stationId)
    {
        return subwayStationMapper.selectById(stationId);
    }

    /**
     * 查询地铁站点列表
     *
     * @param subwayStation 地铁站点
     * @return 地铁站点
     */
    @Override
    public List<SubwayStation> selectSubwayStationList(SubwayStation subwayStation)
    {
        return subwayStationMapper.selectSubwayStationList(subwayStation);
    }

    /**
     * 新增地铁站点
     *
     * @param subwayStation 地铁站点
     * @return 结果
     */
    @Override
    public int insertSubwayStation(SubwayStation subwayStation)
    {
        Long count = subwayStationMapper.selectCount(Wrappers.<SubwayStation>lambdaQuery().eq(SubwayStation::getStationName, subwayStation.getStationName()));
        if(count>0){
            throw new ServiceException("已存在相同站点名称");
        }
        int insert = subwayStationMapper.insert(subwayStation);
        if(insert>0){
            removeSubwayCache();
        }
        return insert;
    }

    /**
     * 修改地铁站点
     *
     * @param subwayStation 地铁站点
     * @return 结果
     */
    @Override
    public int updateSubwayStation(SubwayStation subwayStation)
    {
        Long count = subwayStationMapper.selectCount(Wrappers.<SubwayStation>lambdaQuery().eq(SubwayStation::getStationName, subwayStation.getStationName()));
        if(count>0){
            throw new ServiceException("已存在相同站点名称");
        }

        int i = subwayStationMapper.updateById(subwayStation);
        if(i>0){
            removeSubwayCache();
        }
        return i;
    }

    /**
     * 批量删除地铁站点
     *
     * @param stationIds 需要删除的地铁站点主键
     * @return 结果
     */
    @Override
    public int deleteSubwayStationByStationIds(Long[] stationIds)
    {
        return subwayStationMapper.deleteBatchIds(Arrays.asList(stationIds));
    }

    @Override
    public void importStation() {
        //https://map.amap.com/subway/index.html 获取json
        String jsonFilePath = "D:\\QD\\file.json"; // JSON 文件路径
        List<SubwayStation> subwayStations = parseJsonToSubwayStations(jsonFilePath);
        subwayStations.forEach(subwayStationMapper::insert);
    }
    /**
     * 解析 JSON 文件并转换为 SubwayStation 列表
     */
    public  List<SubwayStation> parseJsonToSubwayStations(String jsonFilePath) {
        List<SubwayLine> subwayLines = lineService.selectSubwayLineList(new SubwayLine());
        List<SubwayStation> subwayStations = new ArrayList<>();
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        try {
            // 读取 JSON 文件
            SubwayData subwayData = objectMapper.readValue(new File(jsonFilePath), SubwayData.class);

            // 遍历线路和站点
            for (SubwayLineJson line : subwayData.getLines()) {
                String lineName = line.getLineName();
                int orderFlag = 1;
                //获取line Id
                Long lineId = null;
                for(SubwayLine subwayLine:subwayLines){
                    if(subwayLine.getLineName().equals(lineName)){
                        lineId = subwayLine.getLineId();
                    }
                }
                for (Station station : line.getStations()) {
                    // 解析经纬度
                    String[] latLng = station.getLocation().split(",");
                    BigDecimal latitude = new BigDecimal(latLng[1]); // 纬度
                    BigDecimal longitude = new BigDecimal(latLng[0]); // 经度

                    // 创建 SubwayStation 对象
                    SubwayStation subwayStation = new SubwayStation();
                    subwayStation.setStationName(station.getStationName());
                    subwayStation.setLineName(lineName);
                    subwayStation.setLatitude(latitude);
                    subwayStation.setLongitude(longitude);
                    subwayStation.setStationOrder(orderFlag);
                    subwayStation.setLineId(lineId);
                    orderFlag = orderFlag+1;
                    // 添加到结果列表
                    subwayStations.add(subwayStation);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return subwayStations;
    }
}
