package com.ho.filecenter.service.impl;

import com.ho.business.entity.Station;
import com.ho.filecenter.service.WeatherStationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fancl
 * @desc:
 * @date 2023/5/16
 */
@Service
@Slf4j
public class WeatherStationServiceImpl implements WeatherStationService {

    //可以先取市前面的数据
    @Override
    public List<Station> dealStation(List<Station> stations) {
        List<Station> stationList = new ArrayList<>();
        Map<String, Station> stationNameMap = new HashMap<>();
        for (Station station : stations) {
            //如果包含省和市的,截取第一个市后面到下一个市或区或县
            String origAddress = station.getAddress();
            if(origAddress.contains("省")&&origAddress.contains("市") ){
                int idxShi = station.getAddress().indexOf("市");
                //找下一个市,区,县
                String suffixAddress = origAddress.substring(idxShi+1);
                //判断是否还有市, 区,县
                int idxSubCity = suffixAddress.indexOf("市");
                //没有市, 继续查区
                if(idxSubCity==-1){
                    idxSubCity = suffixAddress.indexOf("区");
                }
                //没有区, 继续查县
                if(idxSubCity==-1){
                    idxSubCity = suffixAddress.indexOf("县");
                }
                //要显示位置的信息,赋值
                String addressShow = suffixAddress.substring(0,idxSubCity+1);
                station.setName(addressShow);
                System.out.println();
            }
            int idxShi = station.getAddress().indexOf("市");
            if (idxShi != -1) {
                String stationNameShort = station.getAddress().substring(0, idxShi+1);
                log.info("截取后的站名:" + stationNameShort);
                stationNameMap.put(stationNameShort, station);
            } else {
                //没找打就继续下一轮
                continue;
            }
        }

        for(Map.Entry<String, Station> entry:stationNameMap.entrySet()){
            Station station = entry.getValue();
            //把处理后的站名放在了address里,把要显示的地区名放在了stationName里

            station.setAddress(entry.getKey());
            stationList.add(station);
        }
        return stationList;
    }
}
