package com.yida.data.school.epidemic.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.epidemic.EpidemicAccessFlow;
import com.yida.data.school.epidemic.mapper.EpidemicAccessFlowMapper;
import com.yida.data.school.epidemic.service.EpidemicAccessFlowService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenbo
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
@RequiredArgsConstructor
public class EpidemicAccessFlowServiceImpl extends ServiceImpl<EpidemicAccessFlowMapper, EpidemicAccessFlow>
        implements EpidemicAccessFlowService {
    @Override
    public IPage<EpidemicAccessFlow> findAccessFlowList(Page<EpidemicAccessFlow> page, Integer num, Integer type, Long deptId) {
        return baseMapper.findAccessFlowList(page, num, type, deptId);
    }

    @Override
    public List<EpidemicAccessFlow> h5FindAccessFlowList(Integer num, Integer type, Long deptId) {
        if (ObjectUtil.isEmpty(num) || num <= 0) {
            return new ArrayList<>();
        }
        //取数据库最后一个数据
        EpidemicAccessFlow last = baseMapper.findLastOne(deptId, type);
        if (ObjectUtil.isEmpty(last)) {
            return new ArrayList<>();
        }
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
        LocalDateTime now = last.getTime();

        List<EpidemicAccessFlow> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            String dateTime = now.minusHours(i).format(fmt);
            EpidemicAccessFlow accessFlow = baseMapper.h5FindAccessFlowList(deptId, type, dateTime);
            if (ObjectUtil.isEmpty(accessFlow)) {
                EpidemicAccessFlow accessFlow1 = new EpidemicAccessFlow();
                if (ObjectUtil.isEmpty(type)){
                    accessFlow1.setType(null);
                }else {
                    accessFlow1.setType(type.toString());
                }
                accessFlow1.setDeptId(deptId);
                accessFlow1.setNum(0);
                accessFlow1.setTime(LocalDateTime.parse(dateTime, fmt));
                list.add(accessFlow1);
            } else {
                accessFlow.setDeptId(deptId);
                accessFlow.setTime(LocalDateTime.parse(dateTime, fmt));
                if (ObjectUtil.isEmpty(type)){
                    accessFlow.setType(null);
                }else {
                    accessFlow.setType(type.toString());
                }
                list.add(accessFlow);
            }
        }
        return list;
    }
}
