package com.usky.iot.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.core.util.DateUtils;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.iot.domain.DmpDataOverview;
import com.usky.iot.mapper.DmpDataOverviewMapper;
import com.usky.iot.service.DmpDataOverviewService;
import com.usky.iot.service.vo.DmpDataOverviewVO;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 数据概览
 *
 * @author ya
 * @since 2022-10-14
 */
@Service
public class DmpDataOverviewServiceImpl extends AbstractCrudService<DmpDataOverviewMapper, DmpDataOverview> implements DmpDataOverviewService {


    @Override
    public List<DmpDataOverview> list(Date startTime, Date endTime, Integer tenantId, Integer type) {
        LambdaQueryWrapper<DmpDataOverview> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.between(null != startTime && null != endTime, DmpDataOverview::getCreatedTime, startTime, endTime)
                .eq(null != tenantId, DmpDataOverview::getTenantId, tenantId)
                .eq(null != type, DmpDataOverview::getCycleType, type);
        List<DmpDataOverview> list = this.list(queryWrapper);
        Optional.ofNullable(type)
                .ifPresent(t ->{
                    if (1 == type && null != endTime){
                        prefectMonth(list, startTime, endTime, "yyyy-MM-dd", date -> DateUtils.addDays(date, 1));
                    }else if (2 == type && null != endTime){
                        prefectMonth(list, startTime, endTime, "yyyy-MM", date -> DateUtils.addMonths(date, 1));
                    }
                });
        return list.stream().
                sorted(Comparator.comparing(DmpDataOverview::getCreatedTime)).
                collect(Collectors.toList());
    }


    /**
     * 完善数据
     * @param list
     * @param startTime
     * @param endTime
     * @param format
     * @param function
     */
    public void prefectMonth(List<DmpDataOverview> list, Date startTime, Date endTime, String format, Function<Date, Date> function) {
        do {
            String dates = DateUtils.format(startTime, format);
            Date finalStartTime = startTime;
            list.stream().filter(dv -> DateUtils.format(dv.getCreatedTime(), format).equals(dates))
                    .findAny()
                    .orElseGet(() -> {
                        DmpDataOverview dmpDataOverview = new DmpDataOverview();
                        dmpDataOverview.setCreatedTime(finalStartTime);
                        DmpDataOverviewVO dmpDataOverviewVO = new DmpDataOverviewVO();
                        dmpDataOverviewVO.setDeviceCount(0);
                        dmpDataOverviewVO.setHyCount(0);
                        dmpDataOverviewVO.setJhCount(0);
                        dmpDataOverviewVO.setZcCount(0);
                        dmpDataOverviewVO.setDzcCount(0);
                        dmpDataOverviewVO.setDlxCount(0);
                        dmpDataOverviewVO.setJhRadioCount(0.0D);
                        dmpDataOverviewVO.setHyRadioCount(0.0D);
                        dmpDataOverview.setDeviceNumber(JSON.toJSONString(dmpDataOverviewVO));
                        list.add(dmpDataOverview);
                        return dmpDataOverview;
                    });
            startTime = function.apply(startTime);
        } while ((startTime.getTime() - endTime.getTime()) <= 0);
    }
}
