/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.smartapplication.smartsmoke.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.community.agency.entity.Agency;
import org.springblade.community.agency.service.IAgencyService;
import org.springblade.community.enterprise.entity.Enterprise;
import org.springblade.community.enterprise.service.IEnterpriseService;
import org.springblade.community.parkingveh.service.IParkingVehService;
import org.springblade.community.residential.entity.Residential;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.community.staff.entity.EnterpriseStaff;
import org.springblade.community.staff.wrapper.EnterpriseStaffWrapper;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.homecare.alarmlist.entity.Alarm;
import org.springblade.homecare.alarmlist.service.IAlarmService;
import org.springblade.homecare.alarmlist.vo.AlarmVO;
import org.springblade.homecare.alarmlist.wrapper.AlarmWrapper;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IDictBizService;
import org.springblade.smartapplication.smartgate.entity.SmartGate;
import org.springblade.smartapplication.smartsmoke.dto.SmartDeviceDTO;
import org.springblade.smartapplication.smartsmoke.dto.SmartDeviceFireFlightVO;
import org.springblade.smartapplication.smartsmoke.dto.WxLoginDTO;
import org.springblade.smartapplication.smartsmoke.entity.SmartDevice;
import org.springblade.smartapplication.smartsmoke.service.ISmartDeviceService;
import org.springblade.smartapplication.smartsmoke.vo.FireStaticVO;
import org.springblade.smartapplication.smartsmoke.vo.GateStaticVO;
import org.springblade.smartapplication.smartsmoke.vo.SmartDeviceVO;
import org.springblade.smartapplication.smartsmoke.wrapper.SmartDeviceWrapper;
import org.springblade.statistics.entity.DeviceStatistics;
import org.springblade.statistics.entity.SingleDeviceStatistics;
import org.springblade.system.deviceicon.service.IDeviceTypeIconService;
import org.springblade.system.orgposition.entity.OrgPosition;
import org.springblade.system.orgposition.service.IOrgPositionService;
import org.springblade.system.street.service.IStreetService;
import org.springblade.third.butt.iotdevice.entity.IotAuthentication;
import org.springblade.third.butt.iotdevice.entity.PepApiResultBean;
import org.springblade.third.butt.iotdevice.service.IAepDeviceManageService;
import org.springblade.third.butt.iotdevice.service.IperceptionIotDeviceService;
import org.springblade.third.iotaepproduct.entity.ThirdIotAepProduct;
import org.springblade.third.iotaepproduct.service.IThirdIotAepProductService;
import org.springblade.utils.QueryWrapperUtil;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 智能设备 控制器
 *
 * @author BladeX
 * @since 2020-10-17
 */
@RestController
@AllArgsConstructor
@RequestMapping("smartapplication/smartdevice")
@Api(value = "智能设备", tags = "智能设备接口")
public class SmartDeviceController extends BladeController {

	private ISmartDeviceService smartDeviceService;
	private IResidentialService residentialService;
	private IAgencyService agencyService;
	private IStreetService streetService;
	private IOrgPositionService orgPositionService;
	private IDeptService deptService;
	private IEnterpriseService enterpriseService;
	private IAlarmService alarmService;
	private IDictBizService dictBizService;
	private IDeviceTypeIconService deviceTypeIconService;
	private IThirdIotAepProductService thirdIotAepProductService;
	private IAepDeviceManageService aepDeviceManageService;
	private IperceptionIotDeviceService perceptionIotDeviceService;

	/**
	 * 获取所有设备
	 */
	@GetMapping("/getAll")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "获取所有设备", notes = "获取所有设备")
	public R<SmartDeviceFireFlightVO> getAll(SmartDeviceDTO smartDeviceDTO, BladeUser bladeUser) {
		SmartDeviceFireFlightVO smartDeviceFireFlightVO = new SmartDeviceFireFlightVO();
		SmartDevice smartDevice = Objects.requireNonNull(BeanUtil.copy(smartDeviceDTO,SmartDeviceDTO.class));
		smartDevice.setResidentialId(null);
		String deptId = bladeUser.getDeptId();
		String[] deptIds = deptId.split(",");
		LambdaQueryWrapper<Residential> residentialLambdaQueryWrapper = new QueryWrapper<>(new Residential()).lambda();


		LambdaQueryWrapper<SmartDevice> smartDeviceLambdaQueryWrapperwrapper = Condition.getQueryWrapper(smartDevice).lambda();
		smartDeviceLambdaQueryWrapperwrapper.like(SmartDevice::getLatitudeLongitude,".%,%.");

		for(int i=0;i<deptIds.length;i++){
			OrgPosition orgPosition = orgPositionService.getOne(new QueryWrapper<>(new OrgPosition()).lambda().eq(OrgPosition::getOrgId,deptIds[i]));
			if(i ==0){
				smartDeviceLambdaQueryWrapperwrapper.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				residentialLambdaQueryWrapper.like(Residential::getOrgPosition,orgPosition.getOrgPosition());
			}else{
				smartDeviceLambdaQueryWrapperwrapper.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				residentialLambdaQueryWrapper.or().like(Residential::getOrgPosition,orgPosition.getOrgPosition());
			}
		}

		//获取小区列表
		List<Residential> residentials = residentialService.getBaseMapper().selectList(residentialLambdaQueryWrapper);
		smartDeviceFireFlightVO.setResidentialList(residentials);
		if(smartDeviceDTO.getResidentialId() ==null){
			for(int i =0;i<residentials.size();i++){
				if(i ==0){
					smartDeviceLambdaQueryWrapperwrapper.eq(SmartDevice::getResidentialId,residentials.get(i).getId());
				}else{
					smartDeviceLambdaQueryWrapperwrapper.or().eq(SmartDevice::getResidentialId,residentials.get(i).getId());
				}
			}
		}else{
			smartDeviceLambdaQueryWrapperwrapper.eq(SmartDevice::getResidentialId,smartDeviceDTO.getResidentialId());
		}




		Integer[] deviceTypes = smartDeviceDTO.getDeviceTypes();
		if(deviceTypes!= null){
			for(int i=0;i<deviceTypes.length;i++){
				if(i==0){
					smartDeviceLambdaQueryWrapperwrapper.eq(SmartDevice::getDeviceType,deviceTypes[i]);
				}else{
					smartDeviceLambdaQueryWrapperwrapper.or().eq(SmartDevice::getDeviceType,deviceTypes[i]);
				}

			}
		}

//		wrapper.isNotNull(SmartDevice::getLatitudeLongitude);


		smartDeviceLambdaQueryWrapperwrapper.isNotNull(SmartDevice::getLatitudeLongitude);
		List<SmartDevice> lists = smartDeviceService.getBaseMapper().selectList(smartDeviceLambdaQueryWrapperwrapper);
		smartDeviceFireFlightVO.setSmartDeviceList(SmartDeviceWrapper.build(dictBizService,deviceTypeIconService,enterpriseService).listVO(lists));
		return R.data(smartDeviceFireFlightVO);
	}

	/**
	 * 获取当前园区所有企业
	 */
	@GetMapping("/getEnterpriseList")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "获取当前园区所有企业", notes = "传入BladeUser")
	public R getEnterpriseList(BladeUser bladeUser) {
		List<Enterprise> enterpriseList = enterpriseService.getBaseMapper().selectList(new QueryWrapper<Enterprise>().eq("tenant_id",bladeUser.getTenantId()).ne("is_deleted",1));
		if (enterpriseList == null){
			return null;
		}
		return R.data(enterpriseList);
	}


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入smartDevice")
	public R<SmartDevice> detail(SmartDevice smartDevice) {
		SmartDevice detail = smartDeviceService.getOne(Condition.getQueryWrapper(smartDevice));
		return R.data(detail);
	}

	/**
	 * 分页 智能设备
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "分页", notes = "传入smartDevice")
	public R<IPage<SmartDevice>> list(@RequestParam Map<String,Object> smartDevice, Query query, BladeUser bladeUser, WxLoginDTO wxLoginDTO) {
		Integer deviceType =null;
		if( smartDevice.get("deviceType")!=null){
			deviceType = Integer.valueOf(smartDevice.get("deviceType").toString());
			smartDevice.remove("deviceType");
		}
		if (wxLoginDTO.getCreditCode() == null) {
			if (bladeUser == null){
				return R.data(null);
			}
			String deptId = bladeUser.getDeptId();
			String[] deptIds = deptId.split(",");
			Dept dept = deptService.getById(deptIds[0]);
			if (dept.getDeptCategory() == 1) {
				OrgPosition orgPosition = orgPositionService.getBaseMapper().selectOne(new QueryWrapper<>(new OrgPosition()).lambda().eq(OrgPosition::getOrgId, dept.getId()));
				if (orgPosition == null) {
					return null;
				}
				String[] enterpriseIds = orgPosition.getEnterpriseIds().split(",");
				QueryWrapper<SmartDevice> wrapper = Condition.getQueryWrapper(smartDevice, SmartDevice.class);
				if (deviceType != null) {
					wrapper.eq("device_type", deviceType);
				}
				wrapper.like("enterprise_ids", enterpriseIds[0]);

				IPage<SmartDevice> page = smartDeviceService.page(Condition.getPage(query), wrapper.eq("tenant_id",bladeUser.getTenantId()));
				return R.data(page);
			} else {
				LambdaQueryWrapper<SmartDevice> wrapper = new QueryWrapperUtil<SmartDevice>().getBasicQueryWrapperByMap(SmartDevice::getOrgPosition, smartDevice, new SmartDevice(), bladeUser, orgPositionService);
				if (deviceType != null) {
					wrapper.eq(SmartDevice::getDeviceType, deviceType);
				}
				IPage<SmartDevice> pages = smartDeviceService.page(Condition.getPage(query), wrapper.eq(SmartDevice::getTenantId, bladeUser.getTenantId()));
				return R.data(pages);
			}
		}else {
			Enterprise enterprise = enterpriseService.getOne(new QueryWrapper<>(new Enterprise()).eq("credit_code",wxLoginDTO.getCreditCode()).ne("is_deleted",1));
			QueryWrapper<SmartDevice> wrapper = new QueryWrapper<SmartDevice>();
			if (wxLoginDTO.getPrefix()==null && wxLoginDTO.getDeviceType()==null && wxLoginDTO.getDeviceStatus()==null){
				IPage<SmartDevice> pages = smartDeviceService.page(Condition.getPage(query), wrapper.like("enterprise_ids", enterprise.getId()).ne("is_deleted",1));
				return R.data(pages);
			}
			if (wxLoginDTO.getDeviceType()!=null){
				if (wxLoginDTO.getDeviceStatus() ==null){
					IPage<SmartDevice> pages = smartDeviceService.page(Condition.getPage(query), wrapper.like("enterprise_ids", enterprise.getId()).eq("device_type",wxLoginDTO.getDeviceType()).ne("is_deleted",1));
					return R.data(pages);
				}else {
					IPage<SmartDevice> pages = smartDeviceService.page(Condition.getPage(query), wrapper.like("enterprise_ids", enterprise.getId()).eq("device_type",wxLoginDTO.getDeviceType()).eq("device_status",wxLoginDTO.getDeviceStatus()).ne("is_deleted",1));
					return R.data(pages);
				}
			} else if (wxLoginDTO.getPrefix() != null){
				if (wxLoginDTO.getDeviceStatus() == null){
					IPage<SmartDevice> pages = smartDeviceService.page(Condition.getPage(query), wrapper.like("enterprise_ids", enterprise.getId()).eq("prefix",wxLoginDTO.getPrefix()).ne("is_deleted",1));
					return R.data(pages);
				}else {
					IPage<SmartDevice> pages = smartDeviceService.page(Condition.getPage(query), wrapper.like("enterprise_ids", enterprise.getId()).eq("prefix",wxLoginDTO.getPrefix()).eq("device_status",wxLoginDTO.getDeviceStatus()).ne("is_deleted",1));
					return R.data(pages);
				}
			}else {
				IPage<SmartDevice> pages = smartDeviceService.page(Condition.getPage(query), wrapper.like("enterprise_ids", enterprise.getId()).eq("device_status",wxLoginDTO.getDeviceStatus()).ne("is_deleted",1));
				return R.data(pages);
			}
		}
	}

	/**
	 *设备告警记录(小程序)
	 */
	@GetMapping("/getAlarmRecord")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "设备告警记录",notes = "传入alarm")
	public R<IPage<AlarmVO>> getAlarmRecord(Query query, Alarm alarm){
		String recordTime = null;
		if(alarm.getRecordTime() != null){
			recordTime = alarm.getRecordTime();
			alarm.setRecordTime(null);
		}
		IPage<Alarm> pages = null;
		if(recordTime != null){
			 pages = alarmService.page(Condition.getPage(query), Condition.getQueryWrapper(alarm).lambda().like(Alarm::getRecordTime,recordTime).orderByDesc(Alarm::getCreateTime));
		}else {
			pages = alarmService.page(Condition.getPage(query),Condition.getQueryWrapper(alarm).orderByDesc("create_time"));
		}

		return R.data(AlarmWrapper.build().pageVO(pages));
	}

	/*************************小程序**************************/
	@GetMapping("/getDeviceList")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "园区设备", notes = "传入agency")
	public R<IPage<SmartDeviceVO>> AgencyLogin(SmartDevice smartDevice, BladeUser bladeUser, Query query){
		IPage<SmartDevice> pages = smartDeviceService.page(Condition.getPage(query), Condition.getQueryWrapper(smartDevice));
		return R.data(SmartDeviceWrapper.build(dictBizService,deviceTypeIconService,enterpriseService).pageVO(pages));
	}


	/**
	 * 自定义分页 智能设备
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入smartDevice")
	public R<IPage<SmartDevice>> page(SmartDeviceVO smartDevice, Query query) {
		QueryWrapper<SmartDevice> wrapper = Condition.getQueryWrapper(smartDevice);

		if (smartDevice.getAgencyId() != null){
			Agency agency = agencyService.getById(smartDevice.getAgencyId());
			smartDevice.setAgencyId(null);
			wrapper.like("org_position", agency.getOrgPosition());
		}

//		IPage<SmartDeviceVO> pages = smartDeviceService.selectSmartDevicePage(Condition.getPage(query), smartDevice);
		IPage<SmartDevice> pages = smartDeviceService.page(Condition.getPage(query), wrapper);
		return R.data(pages);
	}

	/**
	 * 设备分类统计
	 */
	@GetMapping("/getSmartDeviceStaticVo")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "设备分类统计")
	public R<SmartDeviceVO> getSmartDeviceStaticVo(SmartDevice smartDevice,BladeUser bladeUser){
		LambdaQueryWrapper<SmartDevice> initWrapper1 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper2 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper3 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper4 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper5 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper6 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper7 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper8 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper9 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper10 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper11 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper12 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper13 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper14 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper15 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper16 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper17 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper18 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper19 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper20 = Condition.getQueryWrapper(smartDevice).lambda();

		LambdaQueryWrapper<SmartDevice> initWrapper21 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper22 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper23 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper24 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper25 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper26 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper27 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper28 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper29 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper30 = Condition.getQueryWrapper(smartDevice).lambda();

		LambdaQueryWrapper<SmartDevice> initWrapper31 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper32 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper33 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper34 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper35 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper36 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper37 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper38 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper39 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper40 = Condition.getQueryWrapper(smartDevice).lambda();

		LambdaQueryWrapper<SmartDevice> initWrapper41 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper42 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper43 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper44 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper45 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper46 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper47 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper48 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper49 = Condition.getQueryWrapper(smartDevice).lambda();

		LambdaQueryWrapper<SmartDevice> initWrapper50 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper51 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper52 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper53 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper54 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper55 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper56 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper57 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper58 = Condition.getQueryWrapper(smartDevice).lambda();
		LambdaQueryWrapper<SmartDevice> initWrapper59 = Condition.getQueryWrapper(smartDevice).lambda();

		String deptId = bladeUser.getDeptId();
		String[] deptIds = deptId.split(",");

		for(int i=0;i<deptIds.length;i++){
			OrgPosition orgPosition = orgPositionService.getOne(new QueryWrapper<>(new OrgPosition()).lambda().eq(OrgPosition::getOrgId,deptIds[i]));
			if(i ==0){
				initWrapper1.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper2.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper3.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper4.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper5.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper6.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper7.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper8.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper9.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper10.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());

				initWrapper11.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper12.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper13.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper14.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper15.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper16.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper17.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper18.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper19.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper20.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());

				initWrapper21.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper22.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper23.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper24.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper25.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper26.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper27.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper28.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper29.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper30.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());

				initWrapper31.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper32.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper33.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper34.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper35.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper36.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper37.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper38.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper39.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper40.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());

				initWrapper41.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper42.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper43.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper44.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper45.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper46.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper47.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper48.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper49.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());

				initWrapper50.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper51.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper52.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper53.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper54.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper55.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper56.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper57.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper58.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper59.like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
			}else{
				initWrapper1.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper2.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper3.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper4.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper5.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper6.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper7.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper8.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper9.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper10.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());

				initWrapper11.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper12.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper13.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper14.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper15.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper16.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper17.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper18.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper19.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper20.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());

				initWrapper21.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper22.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper23.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper24.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper25.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper26.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper27.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper28.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper29.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper30.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());

				initWrapper31.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper32.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper33.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper34.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper35.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper36.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper37.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper38.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper39.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper40.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper41.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper42.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper43.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper44.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper45.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper46.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper47.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper48.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper49.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());

				initWrapper50.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper51.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper52.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper53.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper54.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper55.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper56.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper57.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper58.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
				initWrapper59.or().like(SmartDevice::getOrgPosition,orgPosition.getOrgPosition());
			}
		}

		SmartDeviceVO smartDeviceVO = new SmartDeviceVO();

		if (smartDevice != null){
			//传入设备类型的统计
			if (smartDevice.getDeviceType() != null){
//				Integer deviceCount = this.smartDeviceService.count(Condition.getQueryWrapper(smartDevice));
//				Integer normalCount = this.smartDeviceService.count(Condition.getQueryWrapper(smartDevice).lambda().eq(SmartDevice::getDeviceStatus,1));
//				Integer outlineCount = this.smartDeviceService.count(Condition.getQueryWrapper(smartDevice).lambda().eq(SmartDevice::getDeviceStatus, 3));
//				Integer alarmCount = this.smartDeviceService.count(Condition.getQueryWrapper(smartDevice).lambda().eq(SmartDevice::getDeviceStatus, 2));
//				Integer pauseCount = this.smartDeviceService.count(Condition.getQueryWrapper(smartDevice).lambda().eq(SmartDevice::getDeviceStatus, 4));
				Integer deviceCount = this.smartDeviceService.count(initWrapper1);
				Integer normalCount = this.smartDeviceService.count(initWrapper1.eq(SmartDevice::getDeviceStatus,1));
				Integer outlineCount = this.smartDeviceService.count(initWrapper2.eq(SmartDevice::getDeviceStatus, 3));
				Integer alarmCount = this.smartDeviceService.count(initWrapper3.eq(SmartDevice::getDeviceStatus, 2));
				Integer pauseCount = this.smartDeviceService.count(initWrapper4.eq(SmartDevice::getDeviceStatus, 4));
				smartDeviceVO.setDeviceCount(deviceCount);
				smartDeviceVO.setNormalCount(normalCount);
				smartDeviceVO.setOutlineCount(outlineCount);
				smartDeviceVO.setAlarmCount(alarmCount);
				smartDeviceVO.setPauseCount(pauseCount);
			}else {
				//设备列表统计
				//设备总数
				Integer deviceCount = this.smartDeviceService.count(initWrapper1);
				smartDeviceVO.setDeviceCount(deviceCount);
				//正常数
//				Integer normalCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceStatus,1));
				Integer normalCount = this.smartDeviceService.count(initWrapper1.eq(SmartDevice::getDeviceStatus,1));
				smartDeviceVO.setNormalCount(normalCount);
				//失联数
//				Integer outlineCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceStatus, 3));
				Integer outlineCount = this.smartDeviceService.count(initWrapper2.eq(SmartDevice::getDeviceStatus, 3));
				smartDeviceVO.setOutlineCount(outlineCount);
				//告警数
//				Integer alarmCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceStatus, 2));
				Integer alarmCount = this.smartDeviceService.count(initWrapper3.eq(SmartDevice::getDeviceStatus, 2));
				smartDeviceVO.setAlarmCount(alarmCount);
				//停用数
//				Integer pauseCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceStatus, 4));
				Integer pauseCount = this.smartDeviceService.count(initWrapper4.eq(SmartDevice::getDeviceStatus, 4));
				smartDeviceVO.setPauseCount(pauseCount);

				//烟感总数
//				Integer smokeCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 1));
//				Integer smokeCount = this.smartDeviceService.count(initWrapper5.eq(SmartDevice::getDeviceType, 1));
				Integer smokeCount = this.smartDeviceService.count(initWrapper5.eq(SmartDevice::getDeviceType, "100004"));
				smartDeviceVO.setSmokeCount(smokeCount);
				//正常数
//				Integer smokeNormal = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 1).eq(SmartDevice::getDeviceStatus,1));
				Integer smokeNormal = this.smartDeviceService.count(initWrapper6.eq(SmartDevice::getDeviceType, "100004").eq(SmartDevice::getDeviceStatus,1));
				smartDeviceVO.setSmokeNormal(smokeNormal);
				//失联数
//				Integer smokeOutline = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 1).eq(SmartDevice::getDeviceStatus, 3));
				Integer smokeOutline = this.smartDeviceService.count(initWrapper7.eq(SmartDevice::getDeviceType, "100004").eq(SmartDevice::getDeviceStatus, 3));
				smartDeviceVO.setSmokeOutline(smokeOutline);
				//告警数
//				Integer smokeAlarm = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 1).eq(SmartDevice::getDeviceStatus, 2));
				Integer smokeAlarm = this.smartDeviceService.count(initWrapper8.eq(SmartDevice::getDeviceType, "100004").eq(SmartDevice::getDeviceStatus, 2));
				smartDeviceVO.setSmokeAlarm(smokeAlarm);
				//停用数
//				Integer smokePause = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 1).eq(SmartDevice::getDeviceStatus, 4));
				Integer smokePause = this.smartDeviceService.count(initWrapper9.eq(SmartDevice::getDeviceType, "100004").eq(SmartDevice::getDeviceStatus, 4));
				smartDeviceVO.setSmokePause(smokePause);

				//烟感物模型总数
//				Integer smokeModelCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 1));
//				Integer smokeModelCount = this.smartDeviceService.count(initWrapper5.eq(SmartDevice::getDeviceType, 1));
				Integer smokeModelCount = this.smartDeviceService.count(initWrapper50.eq(SmartDevice::getDeviceType, "10000002"));
				smartDeviceVO.setSmokeModelCount(smokeModelCount);
				//正常数
//				Integer smokeNormal = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 1).eq(SmartDevice::getDeviceStatus,1));
				Integer smokeModelNormal = this.smartDeviceService.count(initWrapper51.eq(SmartDevice::getDeviceType, "10000002").eq(SmartDevice::getDeviceStatus,1));
				smartDeviceVO.setSmokeModelNormal(smokeModelNormal);
				//失联数
//				Integer smokeOutline = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 1).eq(SmartDevice::getDeviceStatus, 3));
				Integer smokeModelOutline = this.smartDeviceService.count(initWrapper52.eq(SmartDevice::getDeviceType, "10000002").eq(SmartDevice::getDeviceStatus, 3));
				smartDeviceVO.setSmokeModelOutline(smokeModelOutline);
				//告警数
//				Integer smokeAlarm = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 1).eq(SmartDevice::getDeviceStatus, 2));
				Integer smokeModelAlarm = this.smartDeviceService.count(initWrapper53.eq(SmartDevice::getDeviceType, "10000002").eq(SmartDevice::getDeviceStatus, 2));
				smartDeviceVO.setSmokeModelAlarm(smokeModelAlarm);
				//停用数
//				Integer smokePause = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 1).eq(SmartDevice::getDeviceStatus, 4));
				Integer smokeModelPause = this.smartDeviceService.count(initWrapper54.eq(SmartDevice::getDeviceType, "10000002").eq(SmartDevice::getDeviceStatus, 4));
				smartDeviceVO.setSmokeModelPause(smokeModelPause);

				//燃气监控总数
//				Integer gasCount = smartDeviceService.getBaseMapper().selectCount(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 2));
				//燃气监控
//				Integer gasCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 2));
				Integer gasCount = this.smartDeviceService.count(initWrapper10.eq(SmartDevice::getDeviceType, "100020"));
				smartDeviceVO.setGasCount(gasCount);
				//正常数
//				Integer gasNormal = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 2).eq(SmartDevice::getDeviceStatus,1));
				Integer gasNormal = this.smartDeviceService.count(initWrapper11.eq(SmartDevice::getDeviceType, "100020").eq(SmartDevice::getDeviceStatus,1));
				smartDeviceVO.setGasNormal(gasNormal);
				//失联数
//				Integer gasOutline = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 2).eq(SmartDevice::getDeviceStatus, 3));
				Integer gasOutline = this.smartDeviceService.count(initWrapper12.eq(SmartDevice::getDeviceType, "100020").eq(SmartDevice::getDeviceStatus, 3));
				smartDeviceVO.setGasOutline(gasOutline);
				//告警数
//				Integer gasAlarm = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 2).eq(SmartDevice::getDeviceStatus, 2));
				Integer gasAlarm = this.smartDeviceService.count(initWrapper13.eq(SmartDevice::getDeviceType, "100020").eq(SmartDevice::getDeviceStatus, 2));
				smartDeviceVO.setGasAlarm(gasAlarm);
				//停用数
//				Integer gasPause = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 2).eq(SmartDevice::getDeviceStatus, 4));
				Integer gasPause = this.smartDeviceService.count(initWrapper14.eq(SmartDevice::getDeviceType, "100020").eq(SmartDevice::getDeviceStatus, 4));
				smartDeviceVO.setGasPause(gasPause);

				//燃气监控物模型总数
//				Integer gasCount = smartDeviceService.getBaseMapper().selectCount(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 2));
				//燃气监控
//				Integer gasCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 2));
				Integer gasModelCount = this.smartDeviceService.count(initWrapper55.eq(SmartDevice::getDeviceType, "10000017"));
				smartDeviceVO.setGasModelCount(gasModelCount);
				//正常数
//				Integer gasNormal = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 2).eq(SmartDevice::getDeviceStatus,1));
				Integer gasModelNormal = this.smartDeviceService.count(initWrapper56.eq(SmartDevice::getDeviceType, "10000017").eq(SmartDevice::getDeviceStatus,1));
				smartDeviceVO.setGasModelNormal(gasModelNormal);
				//失联数
//				Integer gasOutline = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 2).eq(SmartDevice::getDeviceStatus, 3));
				Integer gasModelOutline = this.smartDeviceService.count(initWrapper57.eq(SmartDevice::getDeviceType, "10000017").eq(SmartDevice::getDeviceStatus, 3));
				smartDeviceVO.setGasModelOutline(gasModelOutline);
				//告警数
//				Integer gasAlarm = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 2).eq(SmartDevice::getDeviceStatus, 2));
				Integer gasModelAlarm = this.smartDeviceService.count(initWrapper58.eq(SmartDevice::getDeviceType, "10000017").eq(SmartDevice::getDeviceStatus, 2));
				smartDeviceVO.setGasModelAlarm(gasModelAlarm);
				//停用数
//				Integer gasPause = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 2).eq(SmartDevice::getDeviceStatus, 4));
				Integer gasModelPause = this.smartDeviceService.count(initWrapper59.eq(SmartDevice::getDeviceType, "10000017").eq(SmartDevice::getDeviceStatus, 4));
				smartDeviceVO.setGasModelPause(gasModelPause);

				//井盖监控总数
//		Integer coverCount = smartDeviceService.getBaseMapper().selectCount(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 3));
//				Integer coverCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 3));
				Integer coverCount = this.smartDeviceService.count(initWrapper15.eq(SmartDevice::getDeviceType, 3));
				smartDeviceVO.setCoverCount(coverCount);
				//正常数
//				Integer coverNormal = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 3).eq(SmartDevice::getDeviceStatus,1));
				Integer coverNormal = this.smartDeviceService.count(initWrapper16.eq(SmartDevice::getDeviceType, 3).eq(SmartDevice::getDeviceStatus,1));
				smartDeviceVO.setCoverNormal(coverNormal);
				//失联数
//				Integer coverOutline = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 3).eq(SmartDevice::getDeviceStatus, 3));
				Integer coverOutline = this.smartDeviceService.count(initWrapper17.eq(SmartDevice::getDeviceType, 3).eq(SmartDevice::getDeviceStatus, 3));
				smartDeviceVO.setCoverOutline(coverOutline);
				//告警数
//				Integer coverAlarm = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 3).eq(SmartDevice::getDeviceStatus, 2));
				Integer coverAlarm = this.smartDeviceService.count(initWrapper18.eq(SmartDevice::getDeviceType, 3).eq(SmartDevice::getDeviceStatus, 2));
				smartDeviceVO.setCoverAlarm(coverAlarm);
				//停用数
//				Integer coverPause = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 3).eq(SmartDevice::getDeviceStatus, 4));
				Integer coverPause = this.smartDeviceService.count(initWrapper19.eq(SmartDevice::getDeviceType, 3).eq(SmartDevice::getDeviceStatus, 4));
				smartDeviceVO.setCoverPause(coverPause);

				//一键告警总数
//		Integer alarmDeviceCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(smartDevice).lambda().eq(SmartDevice::getDeviceType, 4));
//				Integer alarmDeviceCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 4));
				Integer alarmDeviceCount = this.smartDeviceService.count(initWrapper20.eq(SmartDevice::getDeviceType, 4));
				smartDeviceVO.setAlarmDeviceCount(alarmDeviceCount);
				//正常数
//				Integer alarmDeviceNormal = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 4).eq(SmartDevice::getDeviceStatus,1));
				Integer alarmDeviceNormal = this.smartDeviceService.count(initWrapper21.eq(SmartDevice::getDeviceType, 4).eq(SmartDevice::getDeviceStatus,1));
				smartDeviceVO.setAlarmDeviceNormal(alarmDeviceNormal);
				//失联数
//				Integer alarmDeviceOutline = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 4).eq(SmartDevice::getDeviceStatus, 3));
				Integer alarmDeviceOutline = this.smartDeviceService.count(initWrapper22.eq(SmartDevice::getDeviceType, 4).eq(SmartDevice::getDeviceStatus, 3));
				smartDeviceVO.setAlarmDeviceOutline(alarmDeviceOutline);
				//告警数
//				Integer alarmDeviceAlarm = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 4).eq(SmartDevice::getDeviceStatus, 2));
				Integer alarmDeviceAlarm = this.smartDeviceService.count(initWrapper23.eq(SmartDevice::getDeviceType, 4).eq(SmartDevice::getDeviceStatus, 2));
				smartDeviceVO.setAlarmDeviceAlarm(alarmDeviceAlarm);
				//停用数
//				Integer alarmDevicePause = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceType, 4).eq(SmartDevice::getDeviceStatus, 4));
				Integer alarmDevicePause = this.smartDeviceService.count(initWrapper24.eq(SmartDevice::getDeviceType, 4).eq(SmartDevice::getDeviceStatus, 4));
				smartDeviceVO.setAlarmDevicePause(alarmDevicePause);

				//智慧路灯总数
//		Integer lampCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(smartDevice).lambda().eq(SmartDevice::getDeviceType, 5));
				Integer lampCount = this.smartDeviceService.count(initWrapper25.eq(SmartDevice::getDeviceType, 5));
				smartDeviceVO.setLampCount(lampCount);
				//正常数
				Integer lampNormal = this.smartDeviceService.count(initWrapper26.eq(SmartDevice::getDeviceType, 5).eq(SmartDevice::getDeviceStatus,1));
				smartDeviceVO.setLampNormal(lampNormal);
				//失联数
				Integer lampOutline = this.smartDeviceService.count(initWrapper27.eq(SmartDevice::getDeviceType, 5).eq(SmartDevice::getDeviceStatus, 3));
				smartDeviceVO.setLampOutline(lampOutline);
				//告警数
				Integer lampAlarm = this.smartDeviceService.count(initWrapper28.eq(SmartDevice::getDeviceType, 5).eq(SmartDevice::getDeviceStatus, 2));
				smartDeviceVO.setLampAlarm(lampAlarm);
				//停用数
				Integer lampPause = this.smartDeviceService.count(initWrapper29.eq(SmartDevice::getDeviceType, 5).eq(SmartDevice::getDeviceStatus, 4));
				smartDeviceVO.setLampPause(lampPause);

				//智慧消防
//		Integer xfCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(smartDevice).lambda().eq(SmartDevice::getDeviceType, 6));
				Integer xfCount = this.smartDeviceService.count(initWrapper30.eq(SmartDevice::getDeviceType, 960417));
				smartDeviceVO.setXfCount(xfCount);
				//正常数
				Integer xfNormal = this.smartDeviceService.count(initWrapper31.eq(SmartDevice::getDeviceType, 960417).eq(SmartDevice::getDeviceStatus,1));
				smartDeviceVO.setXfNormal(xfNormal);
				//失联数
				Integer xfOutline = this.smartDeviceService.count(initWrapper32.eq(SmartDevice::getDeviceType, 960417).eq(SmartDevice::getDeviceStatus, 3));
				smartDeviceVO.setXfOutline(xfOutline);
				//告警数
				Integer xfAlarm = this.smartDeviceService.count(initWrapper33.eq(SmartDevice::getDeviceType, 960417).eq(SmartDevice::getDeviceStatus, 2));
				smartDeviceVO.setXfAlarm(xfAlarm);
				//停用数
				Integer xfPause = this.smartDeviceService.count(initWrapper34.eq(SmartDevice::getDeviceType, 960417).eq(SmartDevice::getDeviceStatus, 4));
				smartDeviceVO.setXfPause(xfPause);

				//垃圾桶
//		Integer trashBinCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(smartDevice).lambda().eq(SmartDevice::getDeviceType, 7));
				Integer trashBinCount = this.smartDeviceService.count(initWrapper35.eq(SmartDevice::getDeviceType, 7));
				smartDeviceVO.setTrashbinCount(trashBinCount);
				//正常数
				Integer trashBinNormal = this.smartDeviceService.count(initWrapper36.eq(SmartDevice::getDeviceType, 7).eq(SmartDevice::getDeviceStatus,1));
				smartDeviceVO.setTrashbinNormal(trashBinNormal);
				//失联数
				Integer trashBinOutline = this.smartDeviceService.count(initWrapper37.eq(SmartDevice::getDeviceType, 7).eq(SmartDevice::getDeviceStatus, 3));
				smartDeviceVO.setTrashbinOutline(trashBinOutline);
				//告警数
				Integer trashBinAlarm = this.smartDeviceService.count(initWrapper38.eq(SmartDevice::getDeviceType, 7).eq(SmartDevice::getDeviceStatus, 2));
				smartDeviceVO.setTrashbinAlarm(trashBinAlarm);
				//停用数
				Integer trashBinPause = this.smartDeviceService.count(initWrapper39.eq(SmartDevice::getDeviceType, 7).eq(SmartDevice::getDeviceStatus, 4));
				smartDeviceVO.setTrashbinPause(trashBinPause);

				//停车监控
//		Integer parkCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(smartDevice).lambda().eq(SmartDevice::getDeviceType, 8));
				Integer parkCount = this.smartDeviceService.count(initWrapper40.eq(SmartDevice::getDeviceType, 8));
				smartDeviceVO.setParkCount(parkCount);
				//正常数
				Integer parkNormal = this.smartDeviceService.count(initWrapper41.eq(SmartDevice::getDeviceType, 8).eq(SmartDevice::getDeviceStatus,1));
				smartDeviceVO.setParkNormal(parkNormal);
				//失联数
				Integer parkOutline = this.smartDeviceService.count(initWrapper42.eq(SmartDevice::getDeviceType, 8).eq(SmartDevice::getDeviceStatus, 3));
				smartDeviceVO.setParkOutline(parkOutline);
				//告警数
				Integer parkAlarm = this.smartDeviceService.count(initWrapper43.eq(SmartDevice::getDeviceType, 8).eq(SmartDevice::getDeviceStatus, 2));
				smartDeviceVO.setParkAlarm(parkAlarm);
				//停用数
				Integer parkPause = this.smartDeviceService.count(initWrapper44.eq(SmartDevice::getDeviceType, 8).eq(SmartDevice::getDeviceStatus, 4));
				smartDeviceVO.setParkPause(parkPause);

				//门磁
//		Integer gateCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(smartDevice).lambda().eq(SmartDevice::getDeviceType, 9));
				Integer gateCount = this.smartDeviceService.count(initWrapper45.eq(SmartDevice::getDeviceType, 9));
				smartDeviceVO.setGateCount(gateCount);
				//正常数
				Integer gateNormal = this.smartDeviceService.count(initWrapper46.eq(SmartDevice::getDeviceType, 9).eq(SmartDevice::getDeviceStatus,1));
				smartDeviceVO.setGateNormal(gateNormal);
				//失联数
				Integer gateOutline = this.smartDeviceService.count(initWrapper47.eq(SmartDevice::getDeviceType, 9).eq(SmartDevice::getDeviceStatus, 3));
				smartDeviceVO.setGateOutline(gateOutline);
				//告警数
				Integer gateAlarm = this.smartDeviceService.count(initWrapper48.eq(SmartDevice::getDeviceType, 9).eq(SmartDevice::getDeviceStatus, 2));
				smartDeviceVO.setGateAlarm(gateAlarm);
				//停用数
				Integer gatePause = this.smartDeviceService.count(initWrapper49.eq(SmartDevice::getDeviceType, 9).eq(SmartDevice::getDeviceStatus, 4));
				smartDeviceVO.setGatePause(gatePause);
			}
		}

		/*if (smartDevice.getDeviceType() != null) {
			normalCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(new SmartDevice()).lambda().eq(SmartDevice::getDeviceStatus, 1).eq(SmartDevice::getDeviceType, smartDevice.getDeviceType()));
			outlineCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(new SmartDevice()).lambda().eq(SmartDevice::getDeviceStatus, 3).eq(SmartDevice::getDeviceType, smartDevice.getDeviceType()));
			alarmCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(new SmartDevice()).lambda().eq(SmartDevice::getDeviceStatus, 2).eq(SmartDevice::getDeviceType, smartDevice.getDeviceType()));
			pauseCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(new SmartDevice()).lambda().eq(SmartDevice::getDeviceStatus, 4).eq(SmartDevice::getDeviceType, smartDevice.getDeviceType()));
		} else {
		}*/
//		normalCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(smartDevice).lambda().eq(SmartDevice::getDeviceStatus, 1));
//		outlineCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(smartDevice).lambda().eq(SmartDevice::getDeviceStatus, 3));
//		alarmCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(smartDevice).lambda().eq(SmartDevice::getDeviceStatus, 2));
//		pauseCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(smartDevice).lambda().eq(SmartDevice::getDeviceStatus, 4));


/*		smartDeviceVO.setPauseCount(pauseCount);
		smartDeviceVO.setAlarmCount(alarmCount);
		smartDeviceVO.setOutlineCount(outlineCount);
		smartDeviceVO.setNormalCount(normalCount);

		smartDeviceVO.setCoverCount(coverCount);
		smartDeviceVO.setAlarmDeviceCount(alarmDeviceCount);
		smartDeviceVO.setGasCount(gasCount);
		smartDeviceVO.setGateCount(gateCount);
		smartDeviceVO.setSmokeCount(smokeCount);
		smartDeviceVO.setLampCount(lampCount);
		smartDeviceVO.setXfCount(xfCount);
		smartDeviceVO.setTrashbinCount(trashBinCount);
		smartDeviceVO.setParkCount(parkCount);*/
		return R.data(smartDeviceVO);
	};

	/**
	 * 设备列表统计
	 */
	@GetMapping("/deviceListCount")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "设备列表统计")
	public R<List<SingleDeviceStatistics>> deviceListCount(SmartDevice smartDevice,BladeUser bladeUser){
		List<SingleDeviceStatistics> singleDeviceStatisticsList = new ArrayList<>();
		List<DictBiz> deviceTypes = this.dictBizService.getList("device_type");
		for (int i =0;i<deviceTypes.size();i++) {
			DictBiz item =deviceTypes.get(i);
			SingleDeviceStatistics singleDeviceStatistics = new SingleDeviceStatistics();
			//设备类型
			singleDeviceStatistics.setDeviceType(item.getDictKey());
			singleDeviceStatistics.setDeviceTypeName(item.getDictValue());
			//总数
			LambdaQueryWrapper<SmartDevice> deviceLambdaQueryWrapper = this.getWrapper(smartDevice, bladeUser, item.getDictKey(), null);
			int deviceCount = this.smartDeviceService.count(deviceLambdaQueryWrapper);
			singleDeviceStatistics.setDeviceCount(deviceCount);
			//正常
			LambdaQueryWrapper<SmartDevice> normalCountWrapper = this.getWrapper(smartDevice, bladeUser, item.getDictKey(), 1);
			int normalCount = this.smartDeviceService.count(normalCountWrapper);
			singleDeviceStatistics.setNormalCount(normalCount);
			//告警
			LambdaQueryWrapper<SmartDevice> alarmCountWrapper = this.getWrapper(smartDevice, bladeUser, item.getDictKey(), 2);
			int alarmCount = this.smartDeviceService.count(alarmCountWrapper);
			singleDeviceStatistics.setAlertCount(alarmCount);
			//失联
			LambdaQueryWrapper<SmartDevice> outlineCountWrapper = this.getWrapper(smartDevice, bladeUser, item.getDictKey(), 3);
			int outlineCount = this.smartDeviceService.count(outlineCountWrapper);
			singleDeviceStatistics.setOutlineCount(outlineCount);
			//停用
			LambdaQueryWrapper<SmartDevice> pauseCountWrapper = this.getWrapper(smartDevice, bladeUser, item.getDictKey(), 4);
			int pauseCount = this.smartDeviceService.count(pauseCountWrapper);
			singleDeviceStatistics.setPauseCount(pauseCount);
			//待布防
			LambdaQueryWrapper<SmartDevice> armedCountWrapper = this.getWrapper(smartDevice, bladeUser, item.getDictKey(), 5);
			int armedCount = this.smartDeviceService.count(armedCountWrapper);
			singleDeviceStatistics.setArmedCount(armedCount);
			singleDeviceStatisticsList.add(singleDeviceStatistics);
		}
		return R.data(singleDeviceStatisticsList);
	}

	public LambdaQueryWrapper<SmartDevice> getWrapper(SmartDevice smartDevice, BladeUser bladeUser,String deviceType, Integer deviceStatus){
		LambdaQueryWrapper<SmartDevice> smartDeviceLambdaQueryWrapper = new QueryWrapperUtil<SmartDevice>().getBasicQueryWrapper(
			SmartDevice::getOrgPosition,smartDevice,bladeUser,orgPositionService);
		//设备类型过滤
		smartDeviceLambdaQueryWrapper.eq(SmartDevice::getDeviceType,deviceType);
		//设备状态过滤
		smartDeviceLambdaQueryWrapper.eq(deviceStatus != null, SmartDevice::getDeviceStatus, deviceStatus);
		return smartDeviceLambdaQueryWrapper;
	}

	/**
	 * 新增 智能设备
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入smartDevice")
	public R save(@Valid @RequestBody SmartDevice smartDevice) {

		return R.status(smartDeviceService.save(smartDevice));
	}

	/**
	 * 修改 智能设备
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入smartDevice")
	public R update(@Valid @RequestBody SmartDevice smartDevice) {
		return R.status(smartDeviceService.updateById(smartDevice));
	}

	/**
	 * 新增或修改 智能设备
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入smartDevice")
	public R submit(@Valid @RequestBody SmartDevice smartDevice) {

		// 新增的时候，还没有id
//		if (smartDevice.getId() == null){
//			smartDeviceService.saveOrUpdate(smartDevice);
//		}
//
//		if (smartDevice.getResidentialId() != null) {
//			Residential residential = residentialService.getById(smartDevice.getResidentialId());
//			if (residential != null) {
//				smartDevice.setOrgPosition(residential.getOrgPosition() + "/" + smartDevice.getId());
//			}else{
//				System.out.println("该设备还未绑定小区/园区分区！！");
//			}
//		}

//		return R.status(smartDeviceService.saveOrUpdate(smartDevice));
		Long residentialId = smartDevice.getResidentialId();
		Residential residential = residentialService.getById(residentialId);
		Assert.notNull(residential, "找不到对应的小区");
		smartDevice.setOrgPosition(residential.getOrgPosition());
		smartDevice.setTenantId(residential.getTenantId());

		//产品
		if(StringUtil.isNotBlank(smartDevice.getProductId())){
			ThirdIotAepProduct product = thirdIotAepProductService.getOne(new QueryWrapper<>(new ThirdIotAepProduct()
			).lambda().eq(ThirdIotAepProduct::getProductId,smartDevice.getProductId()));
			smartDevice.setProtocolType(Integer.valueOf(product.getProductProtocol()));
			smartDevice.setMasterKey(product.getApiKey());
			smartDevice.setDeviceType(product.getDeviceType().toString());
			smartDevice.setPrefix(product.getPrefix());
			smartDevice.setNbModuleModel(product.getTupDeviceModel());
			smartDevice.setManufacturersNo(product.getManufacturersNo());
			smartDevice.setDeviceSpecld(product.getDeviceSpecld());
		}
		if (smartDevice.getId() != null) {//更新
			boolean result = smartDeviceService.updateById(smartDevice);
			return R.status(result);
		} else {//新增设备
			boolean save = false;
			switch (smartDevice.getPlatform()) {
				case "scepctwing":
					smartDevice.setDeviceId(smartDevice.getImei());
					PepApiResultBean scepctwingResult = (PepApiResultBean) perceptionIotDeviceService.registerDevice(smartDevice).getData();
					if (Integer.valueOf(scepctwingResult.getCode()) == 0 && ("成功").equals(scepctwingResult.getMsg())) {

					} else {
						return R.fail(scepctwingResult.getMsg());
					}
					break;
				case "aep":
					Map aepResult = (Map) aepDeviceManageService.registerDevice(smartDevice).getData();
					if (Integer.valueOf(aepResult.get("code").toString()) == 0 && ("ok").equals(aepResult.get("msg").toString())) {
						Map result = (Map) aepResult.get("result");
						smartDevice.setDeviceId(result.get("deviceId").toString());
//						smartDevice.setDeviceStatus(Integer.valueOf(result.get("deviceStatus").toString()));
					} else {
						return R.fail(aepResult.get("msg").toString());
					}
					break;
			}

			save = smartDeviceService.save(smartDevice);
			return R.status(save);
		}
	}


	/**
	 * 删除 智能设备
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(smartDeviceService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 自定义分页 智能设备
	 */
	@GetMapping("/getGateStatic")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "门磁统计", notes = "")
	public R<GateStaticVO> getGateStatic() {
		GateStaticVO gateStaticVO = new GateStaticVO();

		List<String> dateList = getDates();
		gateStaticVO.setDateList(dateList);

		return R.data(gateStaticVO);
	}

	private List<String> getDates() {
		List<String> dateList = new ArrayList<>();

		for(int i=-30;i<=-1;i++){
			dateList.add(LocalDate.now().plusDays(i).toString());
		}

		return dateList;
	}

	/**
	 * 设置设备的orgPosition
	 *
	 * @return
	 */
	@PostMapping("/setOrgPosition")
	public R setOrgPosition(){
		List<SmartDevice> smartDevices = smartDeviceService.getBaseMapper().selectList(new QueryWrapper<>(new SmartDevice()));
		for(int i=0;i<smartDevices.size();i++){
			SmartDevice smartDevice = smartDevices.get(i);
			if(smartDevice.getResidentialId()!=null){
				Residential residential = residentialService.getById(smartDevice.getResidentialId());
				smartDevice.setOrgPosition(residential.getOrgPosition()+"/"+smartDevice.getId());
				smartDeviceService.updateById(smartDevice);
			}else{
				List<Residential> residentials = residentialService.getBaseMapper().selectList(new QueryWrapper<>(new Residential()));
				for(int j =0 ;j<residentials.size();j++){
					if(smartDevice.getDeviceName().contains(residentials.get(j).getName())){
						smartDevice.setResidentialId(residentials.get(j).getId());
						smartDevice.setOrgPosition(residentials.get(j).getOrgPosition()+"/"+smartDevice.getId());
						smartDeviceService.updateById(smartDevice);
						break;
					}
				}
			}

		}

		return  new R();

	}

	/**
	 * 设备绑定小区信息
	 *
	 * @return
	 */
	@PostMapping("/setResidentialByDeviceName")
	public R setResidentialByDeviceName(){
		List<Residential> residentialList = residentialService.getBaseMapper().selectList(new QueryWrapper<>(new Residential()));
		residentialList.parallelStream().forEach(residential -> {
			List<SmartDevice> smartDeviceList = smartDeviceService.getBaseMapper().selectList(new QueryWrapper<>(new SmartDevice()));
			smartDeviceList.parallelStream().forEach(smartDevice -> {
				if(smartDevice.getDeviceName().contains(residential.getName())&&smartDevice.getResidentialId()==null){
					smartDevice.setResidentialId(residential.getId());
					smartDevice.setOrgPosition(residential.getOrgPosition()+"/"+smartDevice.getId());
					smartDeviceService.updateById(smartDevice);
				}
			});
		});

		return  new R();

	}


	@PostMapping("/addData")
	public R addData(){
		for(int i=1;i<=30;i++){
			SmartDevice smartDevice = new SmartDevice();
			smartDevice.setDefenseStatus(1);
			Long l = 61878041367136L+i;
			smartDevice.setImei(l.toString());
			Random random = new Random();
			smartDevice.setBatteryVoltage("98");
			smartDevice.setDeviceStatus(1);
			smartDevice.setDeviceName("德锐斯园区智能烟感"+i);
			smartDevice.setSignalIntensity("14");
			smartDevice.setDeviceType("100004");
			smartDeviceService.save(smartDevice);
		}
		return new R();
	}

	@PostMapping("/batchBindResidential")
	public R batchBindResidential(){
		List<Residential> residentialList = residentialService.list();
		SmartDevice device = new SmartDevice();
		device.setDeviceUsage(10);
		List<SmartDevice> homeDeviceList = smartDeviceService.list(Condition.getQueryWrapper(device));
		for (int j = 0; j < homeDeviceList.size(); j++) {
			SmartDevice itemDevice = homeDeviceList.get(j);
			String deviceInstallAddress = itemDevice.getDeviceInstallAddress();
			for (int i = 0; i < residentialList.size(); i++) {
				Residential residential = residentialList.get(i);
				if (deviceInstallAddress.contains(residential.getName())) {
					itemDevice.setResidentialId(residential.getId());
					itemDevice.setOrgPosition(residential.getOrgPosition() + "/" + itemDevice.getId());
					smartDeviceService.updateById(itemDevice);
					break;
				}
			}
		}

		return R.status(true);
	}


	@ApiOperation(value = "园区小程序获取消防设备统计")
	@GetMapping("/getFireStatic")
	public R<FireStaticVO> getFireStatic(BladeUser bladeUser){
		FireStaticVO fireStaticVO = new FireStaticVO();

		//烟感设备总数
		List<SmartDevice> smokeDevices = smartDeviceService.getBaseMapper().selectList(new QueryWrapper<>(new SmartDevice()).lambda()
			.eq(SmartDevice::getDeviceType, 100004).or()
			.eq(SmartDevice::getDeviceType, 10000002)
			.eq(SmartDevice::getTenantId, bladeUser.getTenantId()));

		fireStaticVO.setSmokeAmount(smokeDevices.size());
		Long smokeNormal = smokeDevices.stream().filter(smartDevice -> 1==smartDevice.getDeviceStatus()).count();
		fireStaticVO.setSmokeNormal(Math.toIntExact(smokeNormal));
		fireStaticVO.setSmokeException(fireStaticVO.getSmokeAmount()-fireStaticVO.getSmokeNormal());

		//消防栓设备总数
		List<SmartDevice> hydrantDevices = smartDeviceService.getBaseMapper().selectList(new QueryWrapper<>(new SmartDevice()).lambda()
			.eq(SmartDevice::getDeviceType, 960417)
			.eq(SmartDevice::getTenantId, bladeUser.getTenantId()));

		fireStaticVO.setHydrantAmount(hydrantDevices.size());
		Long hydrantNormal = hydrantDevices.stream().filter(hydrantDevice -> 1==hydrantDevice.getDeviceStatus()).count();
		fireStaticVO.setHydrantNormal(Math.toIntExact(hydrantNormal));
		fireStaticVO.setHydrantException(fireStaticVO.getHydrantAmount()-fireStaticVO.getHydrantNormal());

		//燃气设备总数
		List<SmartDevice> gasDevices = smartDeviceService.getBaseMapper().selectList(new QueryWrapper<>(new SmartDevice()).lambda()
			.eq(SmartDevice::getDeviceType, 100020).or()
			.eq(SmartDevice::getDeviceType,10000017)
			.eq(SmartDevice::getTenantId, bladeUser.getTenantId()));

		fireStaticVO.setGasAmount(gasDevices.size());
		Long gasNormal = gasDevices.stream().filter(gas -> 1==gas.getDeviceStatus()).count();
		fireStaticVO.setGasNormal(Math.toIntExact(gasNormal));
		fireStaticVO.setGasException(fireStaticVO.getGasAmount()-fireStaticVO.getGasNormal());


		return R.data(fireStaticVO);
	}

}
