package com.fs.watch.service.impl;

import com.fs.common.core.page.TableDataInfo;
import com.fs.common.utils.StringUtils;
import com.fs.watch.domain.IotSim;
import com.fs.watch.domain.WatchDeviceInfoClic;
import com.fs.watch.domain.vo.WatchDeviceInfoAndIotVo;
import com.fs.watch.domain.vo.WatchDeviceInfoVo;
import com.fs.watch.mapper.WatchDeviceInfoClicMapper;
import com.fs.watch.param.DeviceIdAndIotInfoQueryParam;
import com.fs.watch.param.WatchDeviceInfoClicParam;
import com.fs.watch.service.WatchDeviceInfoClicService;
import com.fs.watch.service.WatchDeviceInfoService;
import com.fs.watch.utils.IotUtils;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 设备原始回传信息记录(WatchDeviceInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-12-17 15:11:06
 */
@Service("watchDeviceInfoService")
@Slf4j
public class WatchDeviceInfoClicServiceImpl implements WatchDeviceInfoClicService {
    @Autowired
    private WatchDeviceInfoClicMapper watchDeviceInfoClicMapper;

    @Autowired
    private WatchDeviceInfoService deviceInfoService;


    /**
     * 新增数据
     *
     * @param watchDeviceInfo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insert(WatchDeviceInfoClic watchDeviceInfo) {
        try {
            //查询该设备记录是否已存在
            WatchDeviceInfoClic deviceInfoClic = watchDeviceInfoClicMapper.getByDeviceId(watchDeviceInfo.getDeviceId());
            if (deviceInfoClic == null) {
                //不存在即新增
                watchDeviceInfoClicMapper.insert(watchDeviceInfo);
            } else {
                //存在即更新
                watchDeviceInfoClicMapper.updateByDeviceId(watchDeviceInfo);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;
        }
        return true;
    }

    @Override
    public TableDataInfo getDeviceIdAndIotInfo(DeviceIdAndIotInfoQueryParam param) {
        TableDataInfo res = new TableDataInfo();
        long total = 0;
        List<WatchDeviceInfoAndIotVo> vos = new CopyOnWriteArrayList<>();
        //按设备编号
        String iccid = param.getIccid();
        String deviceId = param.getDeviceId();
        if (StringUtils.isNotBlank(deviceId)) {
            getListByDeviceIdAndIccid(param, iccid, vos);
            int startIndex = (param.getPageNum() - 1) * param.getPageSize();
            int endIndex = startIndex + param.getPageSize() - 1;
            int index = vos.size() - 1;
            vos = vos.subList(startIndex, Math.min(index, endIndex) + 1);
            total = vos.size();
        } else {
            //按iccid查询
            if (StringUtils.isNotBlank(iccid)) {
                //单个查询
                IotSim iotSim = IotUtils.querySIMInfo(iccid);
                if (iotSim != null) {
                    WatchDeviceInfoAndIotVo watchDeviceInfoAndIotVo = getWatchDeviceInfoAndIotVo(iotSim);
                    vos.add(watchDeviceInfoAndIotVo);
                    total = 1L;
                }
            } else {
                //查询所有物联网卡
                TableDataInfo tableDataInfo = IotUtils.querySIMTable(param.getPageNum(), param.getPageSize());
                if (tableDataInfo != null) {
                    List<IotSim> iotSims = (List<IotSim>) tableDataInfo.getRows();
                    //查询对应卡信息
                    if (iotSims != null && !iotSims.isEmpty()) {
                        for (IotSim temp : iotSims) {
                            WatchDeviceInfoAndIotVo vo = getWatchDeviceInfoAndIotVo(temp);
                            vos.add(vo);
                        }
                    }
                    total = tableDataInfo.getTotal();

                }
            }
        }
        res.setRows(vos);
        res.setTotal(total);
        res.setCode(200);
        res.setMsg("success");
        return res;
    }

    /**
     * 根据设备id和iccis查询
     *
     * @param param
     * @param iccid
     * @param vos
     */
    private void getListByDeviceIdAndIccid(DeviceIdAndIotInfoQueryParam param, String iccid, List<WatchDeviceInfoAndIotVo> vos) {
        WatchDeviceInfoClicParam watchDeviceInfoParam = new WatchDeviceInfoClicParam();
        watchDeviceInfoParam.setDeviceId(param.getDeviceId());
        List<WatchDeviceInfoClic> watchDeviceInfos = selectWatchDeviceInfoClicList(watchDeviceInfoParam);
        if (watchDeviceInfos != null && !watchDeviceInfos.isEmpty()) {
            for (WatchDeviceInfoClic info : watchDeviceInfos) {
                String sim1Iccid = info.getSim1Iccid();
                String deviceId = info.getDeviceId();
                if (StringUtils.isNotBlank(sim1Iccid)) {
                    IotSim iotSim = IotUtils.querySIMInfo(sim1Iccid);
                    if (iotSim != null) {
                        if (StringUtils.isNotBlank(iccid)) {
                            if (!iotSim.getIccid().contains(iccid)) {
                                continue;
                            }
                        }
                        WatchDeviceInfoAndIotVo vo = new WatchDeviceInfoAndIotVo();
                        BeanUtils.copyProperties(iotSim, vo);
                        vo.setDeviceId(deviceId);
                        //查询公司名称
                        WatchDeviceInfoVo deviceInfoVo = deviceInfoService.getByNumber(deviceId);
                        vo.setCompanyName(deviceInfoVo != null ? deviceInfoVo.getCompanyName() : null);
                        vos.add(getWatchDeviceInfoAndIotVo(iotSim));
                    }
                }
            }
        }
    }

    private @NotNull WatchDeviceInfoAndIotVo getWatchDeviceInfoAndIotVo(IotSim temp) {
        WatchDeviceInfoAndIotVo vo = new WatchDeviceInfoAndIotVo();
        BeanUtils.copyProperties(temp, vo);
        //查询设备信息
        String iccid = temp.getIccid();
        WatchDeviceInfoClic watchDeviceInfo = watchDeviceInfoClicMapper.selectOneByIcc(iccid);
        if (watchDeviceInfo != null) {
            vo.setDeviceId(watchDeviceInfo.getDeviceId());
            //查询公司名称
            WatchDeviceInfoVo deviceInfoVo = deviceInfoService.getByNumber(watchDeviceInfo.getDeviceId());
            vo.setCompanyName(deviceInfoVo != null ? deviceInfoVo.getCompanyName() : null);
        }
        return vo;
    }

    public List<WatchDeviceInfoClic> selectWatchDeviceInfoClicList(WatchDeviceInfoClicParam param) {
        return watchDeviceInfoClicMapper.selectDeviceInfoList(param);
    }

}
