package com.zhongshu.card.server.core.service.devices;

import com.github.microservice.models.device.DeviceInfoSyncParam;
import com.github.microservice.models.device.DeviceSyncListParam;
import com.github.microservice.models.device.GateWaySyncParam;
import com.github.microservice.models.hxz.DevicePingInfoParam;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.service.school.DeviceInfoService;
import com.zhongshu.card.client.type.DataState;
import com.zhongshu.card.client.type.OnLineState;
import com.zhongshu.card.server.core.dao.devices.DeviceInfoDao;
import com.zhongshu.card.server.core.dao.org.OrganizationDao;
import com.zhongshu.card.server.core.dao.org.OrganizationRelationDao;
import com.zhongshu.card.server.core.domain.devices.DeviceInfo;
import com.zhongshu.card.server.core.domain.devices.DeviceBind;
import com.zhongshu.card.server.core.domain.devices.permiss.DevicePermiss;
import com.zhongshu.card.server.core.service.base.CommonService;
import com.zhongshu.card.server.core.service.user.OperationLogsService;
import com.zhongshu.card.server.core.util.CommonUtil;
import com.zhongshu.card.client.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author TRX
 * @date 2024/6/27
 */
@Slf4j
@Service
public class DeviceSyncFromIotService {

    @Autowired
    DeviceInfoService deviceInfoService;

    @Autowired
    OrganizationDao organizationDao;

    @Autowired
    OrganizationRelationDao organizationRelationDao;

    @Autowired
    OperationLogsService operationLogsService;

    @Autowired
    GateWayInfoServiceImpl gateWayService;

    @Autowired
    CommonService commonService;

    @Autowired
    private DeviceInfoDao deviceInfoDao;

    /**
     * 物联网同步设备
     *
     * @param param
     * @return
     */
    public ResultContent syncFromIotDevices(DeviceSyncListParam param) {
        log.info("syncFromIotDevices");
        List<DeviceInfoSyncParam> list = param.getList();
        if (ObjectUtils.isNotEmpty(list)) {
            for (DeviceInfoSyncParam deviceInfoSyncParam : list) {
                ResultContent content = deviceInfoService.syncDeviceInfo(deviceInfoSyncParam);
                if (content.isSuccess()) {
                    log.info("同步设备成功：{}", deviceInfoSyncParam.getDeviceName());
                } else {
                    log.warn("同步设备失败：{}", content.getMsg());
                }
            }
        }
        return ResultContent.buildSuccess();
    }

    /**
     * 在线状态
     *
     * @param param
     * @return
     */
    public ResultContent syncDeviceOnLineState(DevicePingInfoParam param) {
        HashMap<String, Object> where = new HashMap<>();
        where.put("deviceId", param.getDeviceId());

        HashMap<String, Object> map = new HashMap<>();
        map.put("lastOnlineTime", param.getLastOnlineTime());
        map.put("onLineState", CommonUtil.getEnumByName(OnLineState.class, param.getOnLineState()));
        DataState state = CommonUtil.getEnumByName(DataState.class, param.getState());
        if (state == null) {
            DeviceInfo deviceInfo = deviceInfoDao.findTopByDeviceId(param.getDeviceId());
            if (ObjectUtils.isNotEmpty(deviceInfo)) {
                state = deviceInfo.getState();
            }
        }
        map.put("state", state);
        map.put("lastOnlineTimeStr", DateUtils.paresTime(param.getLastOnlineTime(), DateUtils.FORMAT_LONG));
        commonService.updateData(where, map, DeviceInfo.class.getSimpleName());
        commonService.updateData(where, map, DeviceBind.class.getSimpleName());

        HashMap<String, Object> bindMap = new HashMap<>();
        bindMap.put("onLineState", CommonUtil.getEnumByName(OnLineState.class, param.getOnLineState()));
        commonService.updateData(where, bindMap, DevicePermiss.class.getSimpleName());

        log.info("同步在线状态syncDeviceOnLineState: {}", param.getDeviceId());
        return ResultContent.buildSuccess();
    }

    public ResultContent syncDeviceOnLineTime(DevicePingInfoParam param) {
        HashMap<String, Object> where = new HashMap<>();
        where.put("deviceId", param.getDeviceId());

        HashMap<String, Object> map = new HashMap<>();
        map.put("lastOnlineTime", param.getLastOnlineTime());
        map.put("lastOnlineTimeStr", DateUtils.paresTime(param.getLastOnlineTime(), DateUtils.FORMAT_LONG));
        commonService.updateData(where, map, DeviceInfo.class.getSimpleName());
        return ResultContent.buildSuccess();
    }

    /**
     * 同步网关列表
     *
     * @param param
     * @return
     */
    public ResultContent syncGateWays(GateWaySyncParam param) {
        List<GateWaySyncParam.GateWaySyncInfo> list = param.getList();
        log.info("syncGateWays: {}", list.size());
        if (ObjectUtils.isNotEmpty(list)) {
            for (GateWaySyncParam.GateWaySyncInfo syncInfo : list) {
                ResultContent content = gateWayService.syncGateWayInfo(syncInfo);
                if (content.isSuccess()) {
                    log.info("同步网关成功：{}", syncInfo.getGateWayName());
                } else {
                    log.warn("同步网关失败：{}", content.getMsg());
                }
            }
        }
        return ResultContent.buildSuccess();
    }

}
