package com.zksy.environmentHJ212.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksy.environmentHJ212.domain.po.PsMonitorSz;
import com.zksy.environmentHJ212.mapper.PsMonitorSzMapper;
import com.zksy.environmentHJ212.service.PsMonitorSzService;
import com.zksy.environmentHJ212.utils.DateTimeUtil;
import com.zksy.environmentHJ212.utils.MonitorDatasynch;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 表A.0.26 水质检测数据，属性表，关联监测点 服务实现类
 * </p>
 *
 * @author Javen
 * @since 2021-06-08
 */
@Slf4j
@Service
public class PsMonitorSzServiceImpl extends ServiceImpl<PsMonitorSzMapper, PsMonitorSz> implements PsMonitorSzService {
    @Override
    public List<PsMonitorSz> pushMonitorData(String oldDataStr, String[] dataArr, String ipPort) {
            ArrayList<String> arrayList = new ArrayList<>();
            List<PsMonitorSz> psMonitorSzList = new ArrayList<>();
            for (String param : dataArr) {
                System.out.println("param = " + param);
                String String = "&&";
                boolean contains = param.contains(String);
                if (contains) {
                    param = param.replace(String, ",");

                }
                if (param.contains(",")) {
                    arrayList.add(param);
                }

            }
            ArrayList<String> objects = new ArrayList<>();
            for (String entity : arrayList) {
                String substring = null;
                if (entity.contains("-")) {
                    substring = entity.substring(0, entity.indexOf("-"));
                }
                if (StringUtils.isBlank(substring)) {
                    continue;
                }
                if (entity.startsWith(substring)) {
                    objects.add(substring);
                    String[] split = entity.split(",");

                    PsMonitorSz psMonitorSz = new PsMonitorSz();
                    psMonitorSz.setRemark(oldDataStr);
                    for (String parameters : split) {

                        String[] split1 = parameters.split("=");
                        psMonitorSz.setCode(substring);
                        if (parameters.startsWith(substring + "-Rtd=")) {
                            psMonitorSz.setRealTime(new BigDecimal(split1[1]));
                        }
                        if (parameters.startsWith(substring + "-Flag=")) {
                            psMonitorSz.setDataTag(split1[1]);
                        }
                        psMonitorSz.setRecordTime(LocalDateTime.now());
                        psMonitorSz.setId(UUID.randomUUID().toString().replace("-", ""));
                        for (String comParams : dataArr) {
                            if (comParams.contains("&&") && !comParams.contains(",")) {
                                String replace = comParams.replace("&&", ",");
                                String[] split2 = replace.split(",");
                                if (split2.length>1) {
                                    String dateString = split2[1];
                                    if (dateString.startsWith("DataTime=")) {
                                        psMonitorSz.setSampleTime(DateTimeUtil.parseDateTime(dateString.split("=")[1]));
                                    }
                                }
                            }
                            String[] comsplit1 = comParams.split("=");
                            String paraname = comsplit1[0];
                            String value = comsplit1[1];
                            if (paraname.equals("MN")) {
                                psMonitorSz.setMn(value);
                            }
                            if (paraname.equals("Flag")) {
                                psMonitorSz.setFlag(value);
                            }
                            if (paraname.equals("CN")) {
                                psMonitorSz.setCn(value);
                            }
                            if (paraname.equals("ST")) {
                                psMonitorSz.setSt(value);
                            }
                        }
                    }
                    psMonitorSz.setIpport(ipPort);
                    psMonitorSzList.add(psMonitorSz);
                }
            }
            return psMonitorSzList;
    }
}
