package org.springblade.device.controller;



import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.common.constant.DeviceConstant;
import org.springblade.common.constant.DictCodeConstant;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.device.entity.IotDevice;
import org.springblade.device.service.IIotDeviceService;
import org.springblade.device.vo.SmartOperationStatisticsVO;
import org.springblade.system.entity.DictBiz;
import org.springblade.system.feign.IDictBizClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author cy-computer
 */
@RestController
@AllArgsConstructor
@RequestMapping("/devicestatistics")
@Api(value = "", tags = "接口")
public class DeviceStatisticsController extends BladeController {

	private final IIotDeviceService iotDeviceService;
	private final IDictBizClient dictBizClient;

	/**
	 * 统计web端智能运营中心页面的设备管理数据
	 */
	@GetMapping("/smartOperationStatistics")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "统计web端智能运营中心页面的设备管理数据", notes = "传入energyStatistics")
	public R bizData(@RequestParam Integer size) {
		//获取所有设备类型
		List<DictBiz> dictList = dictBizClient.getList(DictCodeConstant.DICT_CODE).getData();
		Assert.isTrue(CollUtil.isNotEmpty(dictList),DictCodeConstant.DICT_LIST_IS_EMPTY);
		List<SmartOperationStatisticsVO> voList = new ArrayList<>();
		for (int i = 0; i < dictList.size(); i++) {
			if (i>= size) {
				break;
			}
			DictBiz dictBiz = dictList.get(i);
			String key= dictBiz.getDictValue();
			String value= dictBiz.getDictKey();
			//筛选符合条件的设备
			List<IotDevice> list = this.getListDeviceType(value);
			//获取该设备数量
			long count = list.stream().count();
			//获取该设备在线数量
			long onlineCount = list.stream().filter(item -> DeviceConstant.DeviceNetStatus.ONLINE.getValue().equals(item.getDeviceNetStatus())).count();
			//获取该设备离线数量
			long offlineCount = list.stream().filter(item -> DeviceConstant.DeviceNetStatus.OFFLINE.getValue().equals(item.getDeviceNetStatus())).count();
			//获取该设备告警的数量
			long warningCount = list.stream().filter(item -> DeviceConstant.DeviceStatus.ALARM.getValue().equals(item.getDeviceStatus())).count();

			SmartOperationStatisticsVO vo = SmartOperationStatisticsVO.builder()
				.enumKey(key)
				.enumValue(value)
				.count(count)
				.onlineCount(onlineCount)
				.offlineCount(offlineCount)
				.warningCount(warningCount)
				.build();
			voList.add(vo);
		}

		return R.data(voList);
	}

	private List<IotDevice> getListDeviceType(String deviceType){
		List<IotDevice> deviceList = iotDeviceService.list(Condition.getQueryWrapper(new IotDevice()).lambda()
			.eq(IotDevice::getDeviceType, deviceType).select(IotDevice::getId, IotDevice::getDeviceNetStatus));
		List<IotDevice> list = Optional.ofNullable(deviceList).orElse(new ArrayList<>());
		return list;
	}

}
