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

import com.github.microservice.models.device.DeviceIdsParam;
import com.github.microservice.models.device.DeviceInfoSyncParam;
import com.github.microservice.models.device.DeviceSyncListParam;
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.OnLineState;
import com.zhongshu.card.client.utils.DateUtils;
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.DeviceBind;
import com.zhongshu.card.server.core.domain.devices.DeviceInfo;
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 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
    private DeviceInfoService deviceInfoService;

    @Autowired
    private OrganizationDao organizationDao;

    @Autowired
    private OrganizationRelationDao organizationRelationDao;

    @Autowired
    private OperationLogsService operationLogsService;

    @Autowired
    private 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 deleteDevice(DeviceIdsParam param) {
        List<String> deviceIds = param.getDeviceIds();
        if (ObjectUtils.isNotEmpty(deviceIds)) {
            deviceInfoDao.deleteByDeviceIdIn(deviceIds);
        }
        return ResultContent.buildSuccess();
    }

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

        HashMap<String, Object> map = new HashMap<>();
        map.put("lastOnlineTime", param.getLastOnlineTime());
        map.put("lastOfflineTime", param.getLastOfflineTime());
        map.put("onLineState", onLineState);
        if (param.getState() != null) {
            map.put("state", param.getState());
        }
        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", onLineState);
        commonService.updateData(where, bindMap, DevicePermiss.class.getSimpleName());

        log.info("同步在线状态syncDeviceOnLineState: {} {} {}", param.getDeviceId(),
                DateUtils.paresTime(System.currentTimeMillis(), "HH:mm:ss SSS"),
                onLineState);
        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));
        map.put("lastOfflineTime", param.getLastOfflineTime());
        commonService.updateData(where, map, DeviceInfo.class.getSimpleName());
        return ResultContent.buildSuccess();
    }

}
