package com.usky.iot.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.iot.RemoteIotTaskService;
import com.usky.iot.domain.DmpDeviceInfo;
import com.usky.iot.service.BaseAlarmService;
import com.usky.iot.service.CrmCustomInfoService;
import com.usky.iot.service.DmpDeviceInfoService;
import com.usky.iot.service.job.DmpDataOverviewJob;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ServiceIotTaskApi implements RemoteIotTaskService {

    @Autowired
    private DmpDataOverviewJob dmpDataOverviewJob;

    @Autowired
    private BaseAlarmService baseAlarmService;

    @Autowired
    private CrmCustomInfoService crmCustomInfoService;

    @Autowired
    private DmpDeviceInfoService dmpDeviceInfoService;

    @Override
    public void dataOverviewJobData () {
        dmpDataOverviewJob.execute();
    }

    @Override
    public void deviceStatus() {
        dmpDeviceInfoService.deviceStatus();
    }

    @Override
    public void deviceOffLineAlarm() {
        baseAlarmService.deviceOffLineAlarm();
    }

    @Override
    public void customInfoStatus() {
        crmCustomInfoService.updateStatus();
    }

    @Override
    public void addDeviceInfo(String productCode, String deviceUuid, String deviceId, String deviceName, String installAddress, Integer serviceStatus){
        DmpDeviceInfo dmpDeviceInfo = new DmpDeviceInfo();
        dmpDeviceInfo.setProductCode(productCode);
        dmpDeviceInfo.setDeviceUuid(deviceUuid);
        dmpDeviceInfo.setDeviceId(deviceId);
        dmpDeviceInfo.setDeviceName(deviceName);
        dmpDeviceInfo.setInstallAddress(installAddress);
        dmpDeviceInfo.setServiceStatus(serviceStatus);

        dmpDeviceInfoService.add(dmpDeviceInfo);
    }

    @Override
    public void deleteDeviceInfo(String deviceUuid){
        LambdaQueryWrapper<DmpDeviceInfo> deviceQueryWrapper = Wrappers.lambdaQuery();
        deviceQueryWrapper.eq(DmpDeviceInfo::getDeviceUuid,deviceUuid);
        DmpDeviceInfo dmpDeviceInfo = dmpDeviceInfoService.getOne(deviceQueryWrapper);

        dmpDeviceInfoService.remove(dmpDeviceInfo.getId());
    }
}
