package com.sunyard.ssp.modules.monitor.transaction.serviceImpl;

import com.sunyard.ssp.modules.monitor.transaction.entity.StatisticalConstant;
import com.sunyard.ssp.modules.monitor.transaction.entity.StatisticalParam;
import com.sunyard.ssp.modules.monitor.transaction.entity.StatisticalResult;
import com.sunyard.ssp.modules.monitor.transaction.mapper.StatisticalMapper;
import com.sunyard.ssp.modules.monitor.transaction.service.TransactionService;
import com.sunyard.ssp.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class StatisticalServiceImpl implements TransactionService {

    @Autowired
    private StatisticalMapper statisticalMapper;

    @Override
    public Object getTransactionData(StatisticalParam statisticalParam) {
        statisticalParam = dealWithTimeFormat(statisticalParam);
        if (StatisticalConstant.STATISTICAL_METHOD_PIE_CHART.equals(statisticalParam.getStatisticalMethod())) {
            return statisticalMapper.countPieChart(statisticalParam);
        } else if (StatisticalConstant.STATISTICAL_METHOD_LINE_CHART_TIME_INTERVAL.equals(statisticalParam.getStatisticalMethod())) {
            List<StatisticalResult> statisticalResults = statisticalMapper.countLineChartByTimeInterval(statisticalParam);
            return dealReturnTimeFormat(statisticalResults, statisticalParam.getTimeInterval());
        } else if (StatisticalConstant.STATISTICAL_METHOD_LINE_CHART_KEY_GROUP.equals(statisticalParam.getStatisticalMethod())) {
            List<StatisticalResult> statisticalResults = statisticalMapper.countLineChartByKeyGroup(statisticalParam);
            return dealReturnTimeFormat(statisticalResults, statisticalParam.getTimeInterval());
        } else {
            return null;
        }
    }

    /**
     * 处理时间格式
     *
     * @param statisticalParam
     * @return
     */
    private StatisticalParam dealWithTimeFormat(StatisticalParam statisticalParam) {
        statisticalParam.setSqlStatisticalTime(DateUtil.StringToDate(statisticalParam.getInputStatisticalTime()));
        if (StatisticalConstant.TIME_METHOD_MONTH.equals(statisticalParam.getTimeInterval())) {
            statisticalParam.setSqlStatisticalTimeFormat(StatisticalConstant.TIME_MONTH_FORMAT);
            statisticalParam.setTimeIntervalFormat(StatisticalConstant.TIME_MONTH_WEAK_FORMAT);
        } else if (StatisticalConstant.TIME_METHOD_WEAK.equals(statisticalParam.getTimeInterval())) {
            statisticalParam.setSqlStatisticalTimeFormat(StatisticalConstant.TIME_WEAK_FORMAT);
            statisticalParam.setTimeIntervalFormat(StatisticalConstant.TIME_DAY_FORMAT);
        } else {
            statisticalParam.setSqlStatisticalTimeFormat(StatisticalConstant.TIME_DAY_FORMAT);
            statisticalParam.setTimeIntervalFormat(StatisticalConstant.TIME_HOUR_FORMAT);
        }
        return statisticalParam;
    }

    /**
     * 出来返回时间
     *
     * @param statisticalResults
     * @param timeMethod
     * @return
     */
    private List<StatisticalResult> dealReturnTimeFormat(List<StatisticalResult> statisticalResults, String timeMethod) {
        if (StatisticalConstant.TIME_METHOD_MONTH.equals(timeMethod)) {
            statisticalResults.forEach(statisticalResult -> {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(statisticalResult.getStatisticalTime());
                stringBuilder.setCharAt(7, '月');
                stringBuilder.insert(8, " 第");
                stringBuilder.append("周");
                statisticalResult.setStatisticalTime(stringBuilder.toString());
            });
        } else if (StatisticalConstant.TIME_METHOD_DAY.equals(timeMethod)) {
            statisticalResults.forEach(statisticalResult -> {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(statisticalResult.getStatisticalTime());
                stringBuilder.append("时");
                statisticalResult.setStatisticalTime(stringBuilder.toString());
            });
        } else {

        }
        return statisticalResults;
    }
}
