package com.mvc.netty;


import com.mvc.entity.TAiread;
import com.mvc.entity.TPackLog;
import com.mvc.netty.dealconstant.MsgIdentifier;
import com.mvc.netty.dealconstant.PackageData;
import com.mvc.netty.dealconstant.PackageOperateCode;
import com.mvc.service.copy.TAireadService;
import com.mvc.utils.BeanUtil;
import com.mvc.utils.DateUtil;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 补发报
 */
@Component
public class ReissueMessageParser implements MessageParser {
    private  static Logger log = LoggerFactory.getLogger(ReissueMessageParser.class);

    @Autowired
    private TAireadService tAireadService;

    @Override
    public List<TAiread> parse(TPackLog tPackage,String picsize) {
        String temp = tPackage.getMessage();
        String value = null;
        String tmpMsg = null;
        List<TAiread> rawdataList = new ArrayList<>();

        String highStr = null;
        String widthStr = null;
        String sizeStr = null;

        // 解析报文
        List<String> keys = MsgIdentifier.keys();
        while (!StringUtil.isNullOrEmpty(temp)) {
            boolean isFind = false;
            TAiread rawdata = new TAiread();

            // 初始化TRawdata
            rawdata.setAireadId(tPackage.getPackId());
            rawdata.setStationCode(tPackage.getStationCode());
            rawdata.setRptTime(tPackage.getRptTime());
            rawdata.setRevTime(tPackage.getRevTime());
            rawdata.setFunCode(tPackage.getFunCode());

            //协议借用密码第一个字节表达设备类型(20=水表,21=压力表)(现在是非压力表即水表,后续加入其它类型要注意维护) 2022-09-13 guozhao
            String devType= StringUtils.isNotEmpty(tPackage.getRptPwd())&&tPackage.getRptPwd().substring(0,2).equals("21")?"2":"1";
            rawdata.setDeviceType(devType);

            // 观测时间标识符
            tmpMsg = PackageOperateCode.O_F0F0.parse(temp);

            if (!temp.equals(tmpMsg)) {
                temp = tmpMsg;
                // 数据时间
                temp = PackageData.Data_Time.parse(temp);
                value = PackageData.Data_Time.getValue();
                rawdata.setSampleTime(DateUtil.parseTimeStr(value, "yyMMddHHmm"));
            } else {
                rawdata.setSampleTime(tPackage.getDatatime());
            }

            while (!StringUtil.isNullOrEmpty(temp)) {
                boolean blnFind = false;
                for (String key: keys) {
                    if (temp.toUpperCase().startsWith(key)) {
                        MsgIdentifier identifier = MsgIdentifier.getElement(key);
                        if (identifier != null) {
                                try {
                                    temp = identifier.parse(temp);
                                    value = identifier.getValue();
                                    BeanUtil.fillTRawdata(rawdata, value, identifier);
                                    isFind = true;
                                    tPackage.setFlag("1");
                                    rawdataList.add(rawdata);
                                } catch (Exception e) {
                                    tPackage.setFlag("2");
                                    isFind = false;
                                    rawdataList.clear();
                                }
                                break;
                        }
                    }
                }
                if (blnFind == false) {
                    break;
                }
            }
            if (!isFind) {
                break;
            }
        }
        //给补发报回填电压，信号比，信号强度
        TAiread tAiread = tAireadService.findlastTAireadBydatatime(tPackage.getStationCode(),tPackage.getRptTime());
        if(tAiread!=null){
            for(TAiread data:rawdataList){
                data.setVoltage(tAiread.getVoltage());//电压
                data.setRsrp(tAiread.getRsrp());//信号质量
            }
        }
        return rawdataList;
    }
}
