package com.ho.td.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.entity.PointCurveSrcCol;
import com.ho.common.tools.util.DateNewUtil;
import com.ho.datacollect.api.constant.DataCollectConstant;
import com.ho.datacollect.api.vo.req.add.TdDeviceAddReq;
import com.ho.td.api.entity.query.ResultData;
import com.ho.td.api.entity.query.TdBaseTimeQuery;
import com.ho.td.api.entity.query.UpdateTdReq;
import com.ho.td.mapper.DeviceComplexMapper;
import com.ho.td.mapper.DeviceSimpleMapper;
import com.ho.td.service.Device001Service;
import com.ho.td.util.TableUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class Device001ServiceImpl implements Device001Service {

    @Autowired
    DeviceComplexMapper deviceComplexMapper;

    @Autowired
    DeviceSimpleMapper deviceSimpleMapper;

    @Override
    public int modifyHistoryData(TdBaseTimeQuery query) {
        Date start = DateUtil.parse(query.getBeginTime(), CommonConstant.DATE);
        Date end = DateUtil.parse(query.getEndTime(), CommonConstant.DATE);
        //按照15分钟自动补充值
        List<DateTime> times = DateUtil.rangeToList(DateNewUtil.roundToNearestQuarter(start, CommonConstant.FIFTEEN, CommonConstant.ONE),
                DateNewUtil.roundToNearestQuarter(end, CommonConstant.FIFTEEN, CommonConstant.ZERO), DateField.MINUTE, CommonConstant.FIFTEEN);
        //查询历史值
        Map<String, BigDecimal> colValMap = query.getColValMap();
        for (PointCurveSrcCol pointCurveSrcCol : query.getDeviceIdList()) {
            query.setSrcId(pointCurveSrcCol.getSrcId());
            List<String> cols = pointCurveSrcCol.getCols();
            String key = query.getStationId()+"_"+pointCurveSrcCol.getSrcId();
            String modelName = query.getMap().get(pointCurveSrcCol.getSrcId());
            for (String col : cols) {
                query.setCol(col);
                String tableName = TableUtil.getTableName(query);
                UpdateTdReq updateTdReq = new UpdateTdReq();
                updateTdReq.setStart(start.getTime());
                updateTdReq.setEnd(end.getTime());
                updateTdReq.setTableName(modelName+"_"+tableName);
                updateTdReq.setCol(col);
                List<ResultData> list = deviceComplexMapper.getData(updateTdReq);
                if(!list.isEmpty()){
                    for (ResultData resultData : list) {
                        if (resultData.getTs() != null) {
                            resultData.setTableName(updateTdReq.getTableName());
                            resultData.setColName(updateTdReq.getCol());
                            String time = DateUtil.format(new Date(resultData.getTs()), CommonConstant.DATE_YMD);
                            BigDecimal val = colValMap.get(time + "_" + key + "_" + col);
                            if(val==null){
                                continue;
                            }
                            resultData.setCol(val);
                            deviceComplexMapper.modifyData(resultData);
                        }
                    }
                }else{
                    //按照15分钟自动补充值
                    for (DateTime d:times) {
                        ResultData resultData = new ResultData();
                        resultData.setTs(d.getTime());
                        resultData.setTableName(updateTdReq.getTableName());
                        resultData.setColName(updateTdReq.getCol());
                        String time = DateUtil.format(d, CommonConstant.DATE_YMD);
                        BigDecimal val = colValMap.get(time + "_" + key + "_" + col);
                        if(val==null){
                            continue;
                        }
                        resultData.setCol(val);
                        deviceComplexMapper.modifyData(resultData);
                    }
                }
            }
        }
        return 0;
    }

    @Override
    @Async
    public void insertDeviceSimple(TdDeviceAddReq device) {
        deviceSimpleMapper.insertDeviceSimple(device);
    }

    @Override
    @Async
    public void insertDeviceComplex(List<Integer> complexTableList, TdDeviceAddReq device) {
        String table = device.getTableName();
        log.info("insertDeviceComplex=====,{}", table);
        for (Integer complex : complexTableList) {
            switch (complex) {
                case 0:
                    log.info("insertDeviceComplex=====,{}", complex);
                    deviceComplexMapper.insertDeviceComplex(device);
                    break;
                case 1:
                    device.setTableName(table + DataCollectConstant.TABLE_LINK + CommonConstant.ONE);
                    log.info("insertDeviceComplex1=====,{}", complex);
                    deviceComplexMapper.insertDeviceComplex1(device);
                    break;
                case 2:
                    device.setTableName(table + DataCollectConstant.TABLE_LINK + CommonConstant.TWO);
                    log.info("insertDeviceComplex2=====,{}", complex);
                    deviceComplexMapper.insertDeviceComplex2(device);
                    break;
                case 3:
                    device.setTableName(table + DataCollectConstant.TABLE_LINK + CommonConstant.THREE);
                    log.info("insertDeviceComplex3=====,{}", complex);
                    deviceComplexMapper.insertDeviceComplex3(device);
                    break;
                case 4:
                    device.setTableName(table + DataCollectConstant.TABLE_LINK + CommonConstant.FOUR);
                    log.info("insertDeviceComplex4=====,{}", complex);
                    deviceComplexMapper.insertDeviceComplex4(device);
                    break;
                case 5:
                    device.setTableName(table + DataCollectConstant.TABLE_LINK + CommonConstant.FIVE);
                    log.info("insertDeviceComplex5=====,{}", complex);
                    deviceComplexMapper.insertDeviceComplex5(device);
                    break;
                case 6:
                    device.setTableName(table + DataCollectConstant.TABLE_LINK + CommonConstant.SIX);
                    log.info("insertDeviceComplex6=====,{}", complex);
                    deviceComplexMapper.insertDeviceComplex6(device);
                    break;
                case 7:
                    device.setTableName(table + DataCollectConstant.TABLE_LINK + CommonConstant.SEVEN);
                    log.info("insertDeviceComplex7=====,{}", complex);
                    deviceComplexMapper.insertDeviceComplex7(device);
                    break;
                default:
                    log.info("insertDeviceComplex not match=====,{}", complex);
            }
        }
    }

    @Override
    public void insertDynamicSimple(TdDeviceAddReq device) {
        deviceSimpleMapper.insertDeviceSimpleNew(device);
    }

}
