/*
 * 文 件 名:  WeatherServiceimpl
 * 版    权:  华设设计集团股份有限公司
 * 描    述:  <描述>
 * 修 改 人:  lvwenbin
 * 修改时间:  2025/5/8
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.huashe.common.data.service.impl;

import com.huashe.common.data.mapper.AdmWeatherForecastMapper;
import com.huashe.common.data.mapper.AdmWeatherRtMapper;
import com.huashe.common.data.mapper.DimGaWeatherMapper;
import com.huashe.common.data.model.GaWeatherType;
import com.huashe.common.data.service.WeatherService;
import com.huashe.common.domain.model.WeatherForecast;
import com.huashe.common.domain.model.WeatherRt;
import com.huashe.common.domain.model.WeatherType;
import com.huashe.common.utils.DateUtils;
import com.huashe.common.utils.ListUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 天气数据服务实现
 * <功能详细描述>
 *
 * @author lvwenbin
 * @version [版本号, 2025/5/8]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service
public class WeatherServiceImpl implements WeatherService {
    @Resource
    private DimGaWeatherMapper weatherTypeMapper;

    @Resource
    private AdmWeatherRtMapper weatherRtMapper;

    @Resource
    private AdmWeatherForecastMapper weatherForecastMapper;

    @Cacheable(value = "weatherRt", key = "#adcode")
    @Override
    public WeatherRt getWeatherRt(String adcode) {
        String dateStr = DateUtils.dateToString(new Date(), DateUtils.YYYY_MM_DD);
        return weatherRtMapper.getByAdcode(adcode, dateStr);
    }

    @Override
    public List<WeatherRt> getWeatherHis(String adcode, String startTime, String endTime) {
        return weatherRtMapper.getByTime(adcode, startTime, endTime);
    }

    @Cacheable(value = "weatherForecast", key = "#adcode")
    @Override
    public List<WeatherForecast> getWeatherForecast(String adcode) {
        return weatherForecastMapper.getByAdcode(adcode);
    }

    @Override
    public List<WeatherType> getWeatherType() {
        List<GaWeatherType> weatherTypes = weatherTypeMapper.getAll();
        List<WeatherType> retList = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(weatherTypes)) {
            List<WeatherType> tmp = ListUtils.convert(weatherTypes, gaWeatherType -> {
                WeatherType weatherType = new WeatherType();
                weatherType.setCode(gaWeatherType.getKey());
                weatherType.setName(gaWeatherType.getValue());
                weatherType.setDesc(gaWeatherType.getDesc());
                return weatherType;
            });

            List<WeatherType> c1List = ListUtils.filterList(tmp,
                weatherType -> StringUtils.endsWith(weatherType.getCode(), "000"));
            List<WeatherType> c2List = ListUtils.filterList(tmp,
                weatherType -> !StringUtils.endsWith(weatherType.getCode(), "000") && StringUtils.endsWith(
                    weatherType.getCode(), "00"));
            List<WeatherType> c3List = ListUtils.filterList(tmp,
                weatherType -> !StringUtils.endsWith(weatherType.getCode(), "000") && !StringUtils.endsWith(
                    weatherType.getCode(), "00"));

            for (WeatherType c1 : c1List) {
                c1.setChildren(buildC2List(c1.getCode(), c2List, c3List));
                retList.add(c1);
            }
        }

        return retList;
    }

    private List<WeatherType> buildC2List(String code, List<WeatherType> c2List, List<WeatherType> c3List) {
        String parentPx = code.replaceAll("0+$", "");
        List<WeatherType> retList = new ArrayList<>();

        for (WeatherType c2 : c2List) {
            if (StringUtils.startsWith(c2.getCode(), parentPx)) {
                c2.setChildren(buildC3List(c2.getCode(), c3List));
                retList.add(c2);
            }
        }
        return retList;
    }

    private List<WeatherType> buildC3List(String code, List<WeatherType> c3List) {
        String parentPx = code.replaceAll("0+$", "");
        List<WeatherType> retList = new ArrayList<>();

        for (WeatherType c3 : c3List) {
            if (StringUtils.startsWith(c3.getCode(), parentPx)) {
                retList.add(c3);
            }
        }
        return retList;
    }
}