package com.nokia.tsl_data.service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nokia.tsl_data.dao.UserCountDao;

@Service
public class UserCountService {

    @Autowired
    private UserCountDao userCountDao;

    /**
     * 读取管理端用户数如果当前月份没有数据就读上一个月
     * 
     * 递归方式，最多递归2层
     * 
     * @param monthId
     * @return
     */
    public Map<String, Double> getMangementUserCountForMonth(String monthId) {
        List<Map<String, Object>> mangementUserCountForMonth = getMangementUserCountForMonth(monthId, 2);
        Map<String, Double> result = new HashMap<>();
        for (Map<String, Object> map : mangementUserCountForMonth) {
            result.put(map.get("city_name").toString(),
                    Double.parseDouble(map.get("management_user_count").toString()));
        }
        return result;
    }

    /**
     * 递归实现getangementUserCountForMonth
     * 
     * @param monthId
     * @param stackLayerCount
     * @return
     */
    private List<Map<String, Object>> getMangementUserCountForMonth(String monthId, int stackLayerCount) {
        List<Map<String, Object>> result = userCountDao.selectMangementUserCountForMonth(monthId);
        if (stackLayerCount != 0) {
            stackLayerCount -= 1;
            if (result == null || result.size() == 0) {
                LocalDate localDate = LocalDate.parse(monthId + "01", DateTimeFormatter.ofPattern("yyyyMMdd"));
                localDate = localDate.plusMonths(-1L);
                String newMonthId = DateTimeFormatter.ofPattern("yyyyMM").format(localDate);
                return getMangementUserCountForMonth(newMonthId, stackLayerCount);
            }
        }
        return result;
    }

    /**
     * 读取客户端用户数如果当前月份没有数据就读上一个月
     * 
     * 递归方式，最多递归2层
     * 
     * @param monthId
     * @return
     */
    public Map<String, Double> getCustomerUserCountForMonth(String monthId) {
        List<Map<String, Object>> mangementUserCountForMonth = getCustomerUserCountForMonth(monthId, 2);
        Map<String, Double> result = new HashMap<>();
        for (Map<String, Object> map : mangementUserCountForMonth) {
            result.put(map.get("city_name").toString(),
                    Double.parseDouble(map.get("customer_user_count").toString()));
        }
        return result;
    }

    /**
     * 递归实现getangementUserCountForMonth
     * 
     * @param monthId
     * @param stackLayerCount
     * @return
     */
    private List<Map<String, Object>> getCustomerUserCountForMonth(String monthId, int stackLayerCount) {
        List<Map<String, Object>> result = userCountDao.selectCustomerUserCountForMonth(monthId);
        if (stackLayerCount != 0) {
            stackLayerCount -= 1;
            if (result == null || result.size() == 0) {
                LocalDate localDate = LocalDate.parse(monthId + "01", DateTimeFormatter.ofPattern("yyyyMMdd"));
                localDate = localDate.plusMonths(-1L);
                String newMonthId = DateTimeFormatter.ofPattern("yyyyMM").format(localDate);
                return getCustomerUserCountForMonth(newMonthId, stackLayerCount);
            }
        }
        return result;
    }
}
