package com.bonus.tcp.smartIdentify.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bonus.common.core.constant.BusinessConstants;
import com.bonus.common.core.utils.DateTimeHelper;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.entity.bracelet.vo.DeviceVo;
import com.bonus.common.entity.bracelet.vo.WarnInfoVo;
import com.bonus.common.entity.tcp.DeviceUseVo;
import com.bonus.common.entity.tcp.TcpDataVo;
import com.bonus.tcp.smartIdentify.mapper.TcpDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class TcpDataService {

    @Autowired
    private TcpDataMapper mapper;

    public static  int data_type=41;
    public static  int warn_type=51;
    public static  int warn_content_type=1;
    public static  int warn_content_type2=2;
    public static  int warn_content_type3=3;
    /**
     * 电量编码
     */
    public static  String dl_code="dl_code";
    /**
     * 距离编码
     */
    public static  String jl_code="jy_code";
    @Async
    public  void updateTcpChannelId(String channelId,String devCode,int type){
        try{
            TcpDataVo vo=new TcpDataVo();
            vo.setDevCode(devCode);
            vo.setChannelId(channelId);
            vo.setDevType(BusinessConstants.JD);
            mapper.updateTcpChannelId(vo);;
        }catch (Exception e){
            log.error(e.toString(),e);
        }

    }

    @Async
    public  void downLoadStatus(String channelId){
        try{
            mapper.downLoadStatus(channelId);;
        }catch (Exception e){
            log.error(e.toString(),e);
        }

    }

    /**
     * 更新设备 的点了及告警距离
     * @param devCode
     * @param dl
     * @param jl
     */
    @Async
    public  void updateDeviceInfo(String devCode,String dl,String jl){
        try{
          String finalDl=dl.split("\\|")[0];
            TcpDataVo vo=new TcpDataVo();
            vo.setDevCode(devCode);
            String time=DateTimeHelper.getNowTime();
            vo.setDevType(BusinessConstants.JD);
            List<TcpDataVo> detailList=mapper.getDevDetails(vo);
            if(StringUtils.isNotEmpty(detailList)){
                detailList.forEach(data->{
                    data.setDataTime(time);
                        if(dl_code.equals(data.getDataCode())){
                            //电量
                            data.setValue(finalDl);
                        }else if(jl_code.equals(data.getDataCode())) {
                            //距离
                            data.setValue(jl);
                        }
                         mapper.updateDataVal(data);
                });

            }

        }catch (Exception e){
            log.error(e.toString(),e);
        }

    }
    @Async
    public void insertWarnInfo(String devCode, int type, int report,String msg) {
        try{
            TcpDataVo  tcpDataVo=mapper.getDevInfoByCode(devCode,BusinessConstants.JD);
            if(tcpDataVo!=null){
                tcpDataVo.setPeopleType("0");
                tcpDataVo.setWarnType("3");
                if(report==warn_content_type){
                    tcpDataVo.setWarnContent("电量过低");
                }else if(report==warn_content_type2){
                    tcpDataVo.setWarnContent("雷达报警");
                }else if(report==warn_content_type3){
                    tcpDataVo.setWarnContent("近电告警");
                }
                tcpDataVo.setWarnTime(DateTimeHelper.getNowTime());
                tcpDataVo.setWarnType("3");
                tcpDataVo.setDevType("1");
                tcpDataVo.setDataSource(msg);
                mapper.insertWarnInfo(tcpDataVo);
            }


        }catch (Exception e){
            log.error(e.toString(),e);
        }
    }



    public String getDevCodeByChannel(String channel) {
        try{
         return    mapper.getDevCodeByChannel(channel);
        }catch (Exception e){
            log.error(e.toString(),e);
        }
        return "";
    }

    /**
     * 告警数据入库
     * @param jsonString
     */
    public void insertDevWarnInfo(Map<String,Object> jsonString) {
        try{

        }catch (Exception e){
            log.error(e.toString(),e);
        }
    }
}
