package com.fastbee.iot.tdengine.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.iot.domain.Device;
import com.fastbee.iot.domain.DeviceLog;
import com.fastbee.iot.mapper.TableMapper;
import com.fastbee.iot.model.DeviceStatistic;
import com.fastbee.iot.model.HistoryModel;
import com.fastbee.iot.model.ThingsModelLogCountVO;
import com.fastbee.iot.model.param.DataCenterParam;
import com.fastbee.iot.tdengine.service.ILogService;
import com.fastbee.iot.mapper.DeviceLogMapper;
import com.fastbee.iot.model.MonitorModel;
import com.fastbee.iot.tdengine.service.model.TdLogDto;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 类名: MySqlLogServiceImpl
 * 描述: MySQL存储日志实现类
 * 时间: 2022/5/22,0022 13:37
 * 开发人: admin
 */
public class MySqlLogServiceImpl implements ILogService {


    private DeviceLogMapper deviceLogMapper;
    private String tableName = "iot_device_log_";

    @Resource
    private TableMapper tableMapper;

    public MySqlLogServiceImpl(DeviceLogMapper _deviceLogMapper){
        this.deviceLogMapper=_deviceLogMapper;
    }

    /**
     * 分库存入
     * @param deviceLogEntityList
     */
    @Override
    public int saveDeviceLogList(List<DeviceLog> deviceLogEntityList){
        if(deviceLogEntityList.size() > 0){
            //DeviceLog deviceLogEntity = deviceLogEntityList.get(0);
            //Date createTime = deviceLogEntity.getCreateTime();
            //String ym = DateUtils.parseDateToStr(DateUtils.YYYYMM,createTime);
            //String ymTableName = tableName + ym;
            //if(null == tableMapper.getTableByName(ymTableName)){
            //    deviceLogMapper.createTable(ymTableName,1L);
            //}
            //List<String> stringList = deviceLogEntityList.stream()
            //        .map(a->a.getSerialNumber())
            //        .collect(Collectors.toList());
            //deviceMapper.batchChangeOnline(stringList);
            //return deviceLogMapper.saveBatch(deviceLogEntityList,ymTableName);
        }
        return 0;
    }

    /***
     * 新增设备日志
     * @return
     */
    @Override
    public int saveDeviceLog(DeviceLog deviceLog) {
        return deviceLogMapper.insertDeviceLog(deviceLog);
    }

    @Override
    public int saveBatch(TdLogDto dto){
        return deviceLogMapper.saveBatch(dto.getList());
    }

    /***
     * 根据设备ID删除设备日志
     * @return
     */
    @Override
    public int deleteDeviceLogByDeviceNumber(String deviceNumber) {
        return deviceLogMapper.deleteDeviceLogByDeviceNumber(deviceNumber);
    }

    /***
     * 设备属性、功能、事件和监测数据总数
     * @return
     */
    @Override
    public DeviceStatistic selectCategoryLogCount(Device device){
        return deviceLogMapper.selectCategoryLogCount(device);
    }

    /***
     * 日志列表
     * @return
     */
    @Override
    public List<DeviceLog> selectDeviceLogList(DeviceLog deviceLog) {
        return deviceLogMapper.selectDeviceLogList(deviceLog);
    }

    /***
     * 监测数据列表
     * @return
     */
    @Override
    public List<MonitorModel> selectMonitorList(DeviceLog deviceLog) {
        return deviceLogMapper.selectMonitorList(deviceLog);
    }

    /**查询历史数据  is_Montor=1 或 is_history=1*/
    @Override
    public Map<String, List<HistoryModel>> selectHistoryList(DeviceLog deviceLog){
        List<HistoryModel> historyList = deviceLogMapper.selectHistoryList(deviceLog);
        return historyList.stream().collect(Collectors.groupingBy(HistoryModel::getIdentity));
    }

    @Override
    public List<HistoryModel> listHistory(DeviceLog deviceLog) {
        return deviceLogMapper.listHistory(deviceLog);
    }

    @Override
    public List<HistoryModel> listhistoryGroupByCreateTime(DeviceLog deviceLog) {
        return deviceLogMapper.listhistoryGroupByCreateTime(deviceLog);
    }

    /**
     * 场景统计变量历史值
     * @param deviceLog 实体类
     * @return java.lang.String
     */
    @Override
    public List<String> selectStatsValue(DeviceLog deviceLog) {
        return deviceLogMapper.selectStatsValue(deviceLog);
    }

    @Override
    public List<ThingsModelLogCountVO> countThingsModelInvoke(DataCenterParam dataCenterParam) {
        return deviceLogMapper.countThingsModelInvoke(dataCenterParam);
    }

    @Override
    public DeviceLog selectLastReport(DeviceLog deviceLog) {
        return deviceLogMapper.selectLastReport(deviceLog);
    }

    @Override
    public List<DeviceLog> selectDataList(HashMap<String, Object> kvHashMap) {
        DeviceLog deviceLog = new DeviceLog();
        deviceLog.setBeginTime(kvHashMap.get("beginTime").toString());
        deviceLog.setEndTime(kvHashMap.get("endTime").toString());
        deviceLog.setSerialNumber((kvHashMap.get("serialNumber").toString()));
//        if (kvHashMap.get("endTime") != null) {
//            DateTime endTime = DateUtil.parse(kvHashMap.get("endTime").toString());
//            String yyyyMM = endTime.toString("yyyyMM");
//            String yyyyMM2 = DateUtil.offsetMonth(endTime, -1).toString("yyyyMM");
//            if (tableMapper.getTableByName(tableName + yyyyMM) == null) {
//                deviceLogMapper.createTableByDate(tableName + yyyyMM);
//            }
//            if (tableMapper.getTableByName(tableName + yyyyMM2) == null) {
//                deviceLogMapper.createTableByDate(tableName + yyyyMM2);
//            }
//            kvHashMap.put("tableName", tableName + yyyyMM);
//            kvHashMap.put("tableName2", tableName + yyyyMM2);
//        }
//        if (kvHashMap.get("tableName") == null) {
//            Date date = new Date();
//            String ym = DateUtils.parseDateToStr(DateUtils.YYYYMM, date);
//            String ym2 = DateUtils.parseDateToStr(DateUtils.YYYYMM, DateUtil.offsetMonth(new Date(), -1));
//            if (tableMapper.getTableByName(tableName + ym) == null) {
//                deviceLogMapper.createTableByDate(tableName + ym);
//            }
//            if (tableMapper.getTableByName(tableName + ym2) == null) {
//                deviceLogMapper.createTableByDate(tableName + ym2);
//            }
//            kvHashMap.put("tableName", tableName + ym);
//            kvHashMap.put("tableName2", tableName + ym2);
//        }
        return deviceLogMapper.selectDeviceLogList(deviceLog);
    }
}
