package org.springblade.statistics.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.joda.time.LocalDate;
import org.springblade.community.accessrecord.entity.AccessRecord;
import org.springblade.community.accessrecord.service.IAccessRecordService;
import org.springblade.community.agency.entity.Agency;
import org.springblade.community.agency.service.IAgencyService;
import org.springblade.community.building.entity.Building;
import org.springblade.community.building.service.IBuildingService;
import org.springblade.community.car.entity.Car;
import org.springblade.community.car.service.ICarService;
import org.springblade.community.carrecord.entity.CarRecord;
import org.springblade.community.carrecord.service.ICarRecordService;
import org.springblade.community.enterprise.entity.Enterprise;
import org.springblade.community.enterprise.service.IEnterpriseService;
import org.springblade.community.houseuser.entity.Houseuser;
import org.springblade.community.houseuser.service.IHouseuserService;
import org.springblade.community.parkinglot.entity.ParkingLot;
import org.springblade.community.parkinglot.entity.ParkingSpace;
import org.springblade.community.parkinglot.service.IParkingLotService;
import org.springblade.community.parkinglot.service.IParkingSpaceService;
import org.springblade.community.residential.entity.Residential;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.community.room.entity.Room;
import org.springblade.community.room.service.IRoomService;
import org.springblade.community.staff.entity.EnterpriseStaff;
import org.springblade.community.staff.service.IEnterpriseStaffService;
import org.springblade.community.tdcarrecord.entity.TdCarRecord;
import org.springblade.community.tdcarrecord.service.ITdCarRecordService;
import org.springblade.community.unit.entity.Unit;
import org.springblade.community.unit.service.IUnitService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.logger.BladeLogger;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.jobs.reportjob.service.IReportJobService;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.service.IDictBizService;
import org.springblade.smartapplication.capture.service.ICaptureRecordsService;
import org.springblade.smartapplication.carenterrecord.entity.CarEnterRecord;
import org.springblade.smartapplication.carenterrecord.service.ICarEnterRecordService;
import org.springblade.smartapplication.caroutrecord.entity.CarOutRecord;
import org.springblade.smartapplication.caroutrecord.service.ICarOutRecordService;
import org.springblade.smartapplication.facedevice.entity.FaceDevice;
import org.springblade.smartapplication.facedevice.service.IFaceDeviceService;
import org.springblade.smartapplication.residentialdevicetype.entity.ResidentialDeviceType;
import org.springblade.smartapplication.residentialdevicetype.service.IResidentialDeviceTypeService;
import org.springblade.smartapplication.smartmonitor.entity.SmartVideoMonitor;
import org.springblade.smartapplication.smartmonitor.entity.SmartVideoMonitorRecord;
import org.springblade.smartapplication.smartmonitor.service.ISmartVideoMonitorRecordService;
import org.springblade.smartapplication.smartmonitor.service.ISmartVideoMonitorService;
import org.springblade.smartapplication.smartmonitor.service.ISmartVideoMonitorStatService;
import org.springblade.smartapplication.smartsmoke.entity.SmartDevice;
import org.springblade.smartapplication.smartsmoke.service.ISmartDeviceService;
import org.springblade.smartapplication.smartsmoke.wrapper.SmartDeviceWrapper;
import org.springblade.smartapplication.temperaturerecord.entity.TemperatureRecord;
import org.springblade.smartapplication.temperaturerecord.service.ITemperatureRecordService;
import org.springblade.smartapplication.vcdevice.entity.VideoCloudDevice;
import org.springblade.smartapplication.vcdevice.service.IVideoCloudDeviceService;
import org.springblade.statistics.constant.RedisConstant;
import org.springblade.statistics.entity.*;
import org.springblade.statistics.vo.*;
import org.springblade.system.deviceicon.service.IDeviceTypeIconService;
import org.springblade.system.deviceicon.vo.DeviceTypeIconVO;
import org.springblade.system.orgposition.entity.OrgPosition;
import org.springblade.system.orgposition.service.IOrgPositionService;
import org.springblade.system.tenanttype.entity.TenantType;
import org.springblade.system.tenanttype.service.ITenantTypeService;
import org.springblade.utils.QueryWrapperUtil;
import org.springblade.utils.SerializableUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 社区统计接口
 */
@RestController
@AllArgsConstructor
@RequestMapping("/statistics")
@Api(value = "大屏统计", tags = "大屏统计")
public class StatisticsController extends BladeController {

	private final IAgencyService agencyService;
	private final IResidentialService residentialService;
	private final IUnitService unitService;
	private final IRoomService roomService;
	private final IParkingLotService parkingLotService;
	private final IParkingSpaceService parkingSpaceService;
	private final ISmartDeviceService smartDeviceService;
	private final IDictBizService dictBizService;
	private final IHouseuserService houseuserService;
	private final IBuildingService buildingService;
	private final IOrgPositionService orgPositionService;
	private final ICaptureRecordsService captureRecordsService;
	private final IVideoCloudDeviceService videoCloudDeviceService;
	private final IFaceDeviceService faceDeviceService;
	private final IReportJobService reportJobService;
	private final ICarEnterRecordService carEnterRecordService;
	private final ICarOutRecordService carOutRecordService;
	private final ITdCarRecordService tdCarRecordService;
	private final ITenantTypeService tenantTypeService;
	private final ICarRecordService carRecordService;
	private final IEnterpriseService enterpriseService;
	private final IEnterpriseStaffService enterpriseStaffService;
	private final RedisTemplate<String,String> redisTemplate;
	private final IDeviceTypeIconService deviceTypeIconService;
	private final ICarService carService;
	private final ITemperatureRecordService temperatureRecordService;
	private final IAccessRecordService accessRecordService;
	private final IResidentialDeviceTypeService residentialDeviceTypeService;
	private final ISmartVideoMonitorRecordService smartVideoMonitorRecordService;
	private final ISmartVideoMonitorService smartVideoMonitorService;
	/**
	 * 社区全景页面统计
	 */
	@GetMapping("/community")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "社区全景页面统计")
	@Transactional
	@ApiIgnore
	public CommunityStatistics community(BladeUser bladeUser){
		CommunityStatistics communityStatistics = new CommunityStatistics();
		//社区
		LambdaQueryWrapper<Agency> agencyLambdaQueryWrapper = new QueryWrapperUtil<Agency>().getBasicQueryWrapper(Agency::getOrgPosition,new Agency(),bladeUser,orgPositionService);
		//社区列表
		communityStatistics.setAgencies(this.agencyService.list(agencyLambdaQueryWrapper));
		//社区统计
		communityStatistics.setAgencyCount(this.agencyService.count(agencyLambdaQueryWrapper));
		//小区
		LambdaQueryWrapper<Residential> residentialLambdaQueryWrapper = new QueryWrapperUtil<Residential>().getBasicQueryWrapper(Residential::getOrgPosition,new Residential(),bladeUser,orgPositionService);
		communityStatistics.setResidentialCount(this.residentialService.count(residentialLambdaQueryWrapper));
		//楼栋
		LambdaQueryWrapper<Building> buildingLambdaQueryWrapper = new QueryWrapperUtil<Building>().getBasicQueryWrapper(Building::getOrgPosition,new Building(),bladeUser,orgPositionService);
		communityStatistics.setBuildingCount(this.buildingService.count(buildingLambdaQueryWrapper));
		//单元
		LambdaQueryWrapper<Unit> unitLambdaQueryWrapper = new QueryWrapperUtil<Unit>().getBasicQueryWrapper(Unit::getOrgPosition,new Unit(),bladeUser,orgPositionService);
		communityStatistics.setUnitCount(this.unitService.count(unitLambdaQueryWrapper));
		//房间
		LambdaQueryWrapper<Room> roomLambdaQueryWrapper = new QueryWrapperUtil<Room>().getBasicQueryWrapper(Room::getOrgPosition,new Room(),bladeUser,orgPositionService);
		communityStatistics.setRoomCount(this.roomService.count(roomLambdaQueryWrapper));
		//车位
		communityStatistics.setParkingSpaceCount(this.parkingSpaceService.count());
		//设备
		communityStatistics.setDeviceCount(this.smartDeviceService.count());
		//人口
		communityStatistics.setPersonCount(this.houseuserService.count());
		//常住人口
		Integer permanentPeople = this.houseuserService.count(Condition.getQueryWrapper(new Houseuser()).lambda().eq(Houseuser::getPersonType,1));
		communityStatistics.setPermanentPeopleCount(permanentPeople);
		//流动人口
		Integer floatPeople = this.houseuserService.count(Condition.getQueryWrapper(new Houseuser()).lambda().eq(Houseuser::getPersonType,2));
		communityStatistics.setFloatPeopleCount(floatPeople);
		return communityStatistics;
	}

	/**
	 * 社区态势页面统计
	 */
	@GetMapping("/agency")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "社区态势统计")
	@Transactional
	@ApiIgnore
	public AgencyStatistics agency(Agency agency, BladeUser bladeUser){
//		String redisKey ="screen:"+ RedisConstant.AGENCY+":"+agency.getId();

		Agency agencyById = this.agencyService.getById(agency.getId());
		Assert.notNull(agencyById,"该社区不存在");
		AgencyStatistics agencyStatistics = new AgencyStatistics();
		List<Residential> residentials = this.residentialService.list(Condition.getQueryWrapper(new Residential()).lambda().eq(Residential::getAgencyId, agencyById.getId()));
		agencyStatistics.setResidentials(residentials);

		//楼栋和车位
		int buildingCount  = 0;
		int parkSpaceCount = 0;
		for(Residential residential : residentials){
			int count1 = this.buildingService.count(Condition.getQueryWrapper(new Building()).lambda().eq(Building::getResidentialId, residential.getId()));
			buildingCount = buildingCount + count1;
			int count2 = this.parkingSpaceService.count(Condition.getQueryWrapper(new ParkingSpace()).lambda().eq(ParkingSpace::getResidentialId, residential.getId()));
			parkSpaceCount = parkSpaceCount + count2;
		}

		// 楼宇数
		agencyStatistics.setBuildingCount(StringUtils.isEmpty(buildingCount + "") ? 0 : buildingCount);

		// 停车位数
		agencyStatistics.setParkingSpaceCount(StringUtils.isEmpty(parkSpaceCount + "") ? 0 : parkSpaceCount);

		//社区户数
		LambdaQueryWrapper<Room> roomLambdaQueryWrapper = new QueryWrapperUtil<Room>().getBasicQueryWrapper(Room::getOrgPosition,new Room(),bladeUser,orgPositionService);
		roomLambdaQueryWrapper.eq(Room::getAgencyId,agencyById.getId());
		Integer roomCount = this.roomService.count(roomLambdaQueryWrapper);
		agencyStatistics.setRoomCount(roomCount);

		//社区人口
		Integer personCount = this.houseuserService.count(Condition.getQueryWrapper(new Houseuser()).lambda().eq(Houseuser::getAgencyId,agencyById.getId()));
		agencyStatistics.setPersonCount(personCount);

		//业主人员
		Integer ownerCount = this.houseuserService.count(Condition.getQueryWrapper(
			new Houseuser()).lambda().eq(Houseuser::getAgencyId,agencyById.getId()).eq(Houseuser::getType,1));
		agencyStatistics.setOwnerCount(ownerCount);

		//租客人员
		Integer tenantCount = this.houseuserService.count(Condition.getQueryWrapper(
			new Houseuser()).lambda().eq(Houseuser::getAgencyId,agencyById.getId()).eq(Houseuser::getType,3));
		agencyStatistics.setTenantCount(StringUtils.isEmpty(tenantCount + "") ? 0 : tenantCount);

		//重点人员
		Integer keyPersonCount = this.houseuserService.count(Condition.getQueryWrapper(
			new Houseuser()).lambda().eq(Houseuser::getAgencyId,agencyById.getId()).eq(Houseuser::getPersonType,3));
		agencyStatistics.setKeyPersonCount(StringUtils.isEmpty(keyPersonCount + "") ? 0 : keyPersonCount);

//		设备状态1 正常 2 告警 3失联 4 停用(弃用) 5待布防

		//正常设备
		Integer safetyCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceStatus,1).like(SmartDevice::getOrgPosition, agencyById.getOrgPosition()));
		agencyStatistics.setSafetyDeviceCount(StringUtils.isEmpty(safetyCount + "") ? 0 : safetyCount);

		//警告设备
		Integer warningDeviceCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceStatus,2).like(SmartDevice::getOrgPosition, agencyById.getOrgPosition()));
		agencyStatistics.setWarningDeviceCount(warningDeviceCount);

		//失联设备
		Integer missingDeviceCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceStatus,3).like(SmartDevice::getOrgPosition, agencyById.getOrgPosition()));
		agencyStatistics.setWarningDeviceCount(missingDeviceCount);

		//停用设备
		Integer stopUseDeviceCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceStatus,4).like(SmartDevice::getOrgPosition, agencyById.getOrgPosition()));
		agencyStatistics.setWarningDeviceCount(stopUseDeviceCount);

		//待布防设备
		Integer tobeDefendDeviceCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDeviceStatus,5).like(SmartDevice::getOrgPosition, agencyById.getOrgPosition()));
		agencyStatistics.setWarningDeviceCount(tobeDefendDeviceCount);

		//企业数
		Integer enterpriseCount = this.enterpriseService.count(Condition.getQueryWrapper(new Enterprise()).lambda().eq(Enterprise::getTenantId, bladeUser.getTenantId()));
		agencyStatistics.setEnterpriseCount(StringUtils.isEmpty(enterpriseCount + "") ? 0 : enterpriseCount);

		//企业列表
		List<Enterprise> enterpriseList = this.enterpriseService.list(Condition.getQueryWrapper(new Enterprise()).lambda().eq(Enterprise::getTenantId, bladeUser.getTenantId()));
		agencyStatistics.setEnterpriseList(enterpriseList);

		//员工数
		Integer staffCount = this.enterpriseStaffService.count(Condition.getQueryWrapper(new EnterpriseStaff()).lambda().eq(EnterpriseStaff::getTenantId, bladeUser.getTenantId()));
		agencyStatistics.setStaffCount(staffCount);

		return agencyStatistics;
	}

	/**
	 * 社区态势页面统计
	 */
	@GetMapping("/residential/index")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "小区态势统计")
	@Transactional
	@ApiIgnore
	public ResidentialStatistics residential(Residential residential){
		ResidentialStatistics residentialStatistics = new ResidentialStatistics();
		Residential residential1 = residentialService.getById(residential.getId());
		String tenantId =null;
		if(residential1 != null){
			tenantId =residential1.getTenantId();
		}

		if(null != tenantId){
			TenantType tenantType = tenantTypeService.getBaseMapper().selectOne(new QueryWrapper<>(new TenantType()).lambda().eq(TenantType::getTenantId,tenantId));
			if(null != tenantType){
				if(tenantType.getTenantType() == 1){
					Integer enterpriseCount = enterpriseService.getBaseMapper().selectCount(new QueryWrapper<>(new Enterprise()).lambda().eq(Enterprise::getResidentialId, residential.getId()));
					residentialStatistics.setEnterpriseCount(enterpriseCount);
					Integer enterpriseStaffCount = enterpriseStaffService.getBaseMapper().selectCount(new QueryWrapper<>(new EnterpriseStaff()).lambda().eq(EnterpriseStaff::getResidentialId, residential.getId()));
					residentialStatistics.setEnterpriseStaffCount(enterpriseStaffCount);
					Integer devicesCount = smartDeviceService.getBaseMapper().selectCount(new QueryWrapper<>(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId,residential.getId()));
					residentialStatistics.setDevicesCount(devicesCount);
					Integer carCount = carService.getBaseMapper().selectCount(new QueryWrapper<>(new Car()).lambda().eq(Car::getResidentialId,residential.getId()));
					residentialStatistics.setCarCount(carCount);
				}
			}
		}
		Residential residentialById = this.residentialService.getById(residential.getId());
		Assert.notNull(residentialById,"该小区不存在");

		//小区户数
		Integer roomCount = this.roomService.count(Condition.getQueryWrapper(
			new Room()).lambda().eq(Room::getResidentialId,residentialById.getId()));
		residentialStatistics.setRoomCount(roomCount);
		//楼宇总数
		Integer buildingCount = this.buildingService.count(Condition.getQueryWrapper(
			new Building()).lambda().eq(Building::getResidentialId,residentialById.getId()));
		residentialStatistics.setBuildingCount(buildingCount);
		//小区人口
		Integer personCount = this.houseuserService.count(Condition.getQueryWrapper(
			new Houseuser()).lambda().eq(Houseuser::getResidentialId,residentialById.getId()));
		residentialStatistics.setPersonCount(personCount);
		//小区车位
		Integer parkingSpaceCount = this.parkingSpaceService.count(Condition.getQueryWrapper(new ParkingSpace()).lambda().eq(ParkingSpace::getResidentialId,residentialById.getId()));
		residentialStatistics.setParkingLotCount(parkingSpaceCount);
		//业主人员
		Integer ownerCount = this.houseuserService.count(Condition.getQueryWrapper(
			new Houseuser()).lambda().eq(Houseuser::getResidentialId,residentialById.getId()).eq(Houseuser::getType,1));
		residentialStatistics.setOwnerCount(ownerCount);
		//租客人员
		Integer tenantCount = this.houseuserService.count(Condition.getQueryWrapper(
			new Houseuser()).lambda().eq(Houseuser::getResidentialId,residentialById.getId()).eq(Houseuser::getType,3));
		residentialStatistics.setTenantCount(tenantCount);
		return residentialStatistics;
	}

	/**
	 * 社区态势人员管理
	 */
	@GetMapping("/residential/person")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "社区态势人员管理")
	@Transactional
	@ApiIgnore
	public ResidentialStatistics personsByResidential(Residential residential){

		return null;
	}

	/**
	 * 社区态势车辆管理
	 */
	@GetMapping("/residential/car")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "社区态势车辆管理")
	@Transactional
	@ApiIgnore
	public CarStatistics carByResidential(Residential residential){
		Residential residentialById = this.residentialService.getById(residential.getId());
		Assert.notNull(residentialById,"该小区不存在");
		CarStatistics carStatistics = new CarStatistics();
		//车位总数
		Integer parkingLotCount = this.parkingLotService.count(Condition.getQueryWrapper(
			new ParkingLot()).lambda().eq(ParkingLot::getResidentialId,residentialById.getId()));
		carStatistics.setParkingLotCount(parkingLotCount);
		//已使用车位
		Integer userParkingCount = this.parkingLotService.count(Condition.getQueryWrapper(
			new ParkingLot()).lambda().eq(ParkingLot::getResidentialId,residentialById.getId()).isNotNull(ParkingLot::getSpaceTotal));
		//空闲车位
		Integer freeParkingCount = this.parkingLotService.count(Condition.getQueryWrapper(
			new ParkingLot()).lambda().eq(ParkingLot::getResidentialId,residentialById.getId()).isNull(ParkingLot::getSpaceTotal));
		return carStatistics;
	}

	/**
	 * 社区态势房屋管理
	 */
	@GetMapping("/residential/room")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "社区态势房屋管理")
	@Transactional
	@ApiIgnore
	public CarStatistics roomByResidential(Residential residential){

		return null;
	}

	/**
	 * 社区态势设备管理
	 */
	@GetMapping("/residential/device")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "社区态势设备管理")
	@Transactional
	@ApiIgnore
	public DeviceStatistics deviceByResidential(Residential residential,Long deviceType){
		Residential residentialById = this.residentialService.getById(residential.getId());
		Assert.notNull(residentialById,"该小区不存在");
		DeviceStatistics deviceStatistics = new DeviceStatistics();

//		List<DictBiz> deviceTypes = this.dictBizService.getBaseMapper().selectList(new QueryWrapper<>(new DictBiz()).lambda().eq(DictBiz::getCode,"device_type").ne(DictBiz::getParentId,0L));

		List<ResidentialDeviceType> residentialDeviceTypes = this.residentialDeviceTypeService.getBaseMapper().selectList(new QueryWrapper<>(new ResidentialDeviceType()).lambda().eq(ResidentialDeviceType::getResidentialId, residentialById.getId()));
		residentialDeviceTypes.forEach(dictBiz -> {
			//获取设备类型的图标
			LambdaQueryWrapper<SmartDevice> smartDeviceLambdaQueryWrapper = Condition.getQueryWrapper(new SmartDevice()).lambda();
			SingleDeviceStatistics singleDeviceStatistics = new SingleDeviceStatistics();
			//设备类型名称
			singleDeviceStatistics.setDeviceTypeName(dictBiz.getDeviceTypeName());
			//设备类型
			singleDeviceStatistics.setDeviceType(dictBiz.getDeviceType());
			//过滤经纬度为空的数据
			smartDeviceLambdaQueryWrapper.like(SmartDevice::getLatitudeLongitude,".%,%.");
			//过滤小区的id
			smartDeviceLambdaQueryWrapper.eq(SmartDevice::getResidentialId,residentialById.getId());
			//过滤设备类型
			smartDeviceLambdaQueryWrapper.eq(SmartDevice::getDeviceType,dictBiz.getDeviceType());
			//设备正常数
			Integer normalCount = this.smartDeviceService.count(smartDeviceLambdaQueryWrapper.eq(SmartDevice::getDeviceStatus,1));
			singleDeviceStatistics.setNormalCount(normalCount);
			//设备异常数
			Integer alertCount = this.smartDeviceService.count(smartDeviceLambdaQueryWrapper.ne(SmartDevice::getDeviceStatus,1));
			singleDeviceStatistics.setAlertCount(alertCount);
			//存入返回对象
			deviceStatistics.getSingleDeviceStatisticsList().add(singleDeviceStatistics);
		});
		this.getSmartDeviceList(deviceStatistics,residentialById.getId(),deviceType);
		//在线设备
		Integer onlineCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda()
			.eq(SmartDevice::getResidentialId,residentialById.getId())
			.like(SmartDevice::getLatitudeLongitude,".%,%.")
			.eq(SmartDevice::getDefenseStatus,1));
		deviceStatistics.setOnlineCount(onlineCount);
		//离线设备
		Integer offlineCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda()
			.eq(SmartDevice::getResidentialId,residentialById.getId())
			.like(SmartDevice::getLatitudeLongitude,".%,%.")
			.eq(SmartDevice::getDefenseStatus,2));
		deviceStatistics.setOfflineCount(offlineCount);
		//告警设备
		Integer alertCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda()
			.eq(SmartDevice::getResidentialId,residentialById.getId())
			.like(SmartDevice::getLatitudeLongitude,".%,%.")
			.eq(SmartDevice::getDeviceStatus,2)
		);
		deviceStatistics.setAlertCount(alertCount);

		/*List<SmartDevice> smokeDevices = this.smartDeviceService.list(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 1));
		deviceStatistics.setSmokeDevices(smokeDevices);
		Integer smokeCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 1));
		deviceStatistics.setSmokeCount(smokeCount);

		List<SmartDevice> gasDevices = this.smartDeviceService.list(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 2));
		deviceStatistics.setGasDevices(gasDevices);
		Integer gasCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 2));
		deviceStatistics.setGasCount(gasCount);

		List<SmartDevice> coverDevices = this.smartDeviceService.list(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 3));
		deviceStatistics.setCoverDevices(coverDevices);
		Integer coverCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 3));
		deviceStatistics.setCoverCount(coverCount);

		List<SmartDevice> alarmDevices = this.smartDeviceService.list(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 4));
		deviceStatistics.setAlarmDevices(alarmDevices);
		Integer alarmDeviceCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 4));
		deviceStatistics.setAlarmDeviceCount(alarmDeviceCount);

		List<SmartDevice> lampDevices = this.smartDeviceService.list(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 5));
		deviceStatistics.setDevices(lampDevices);
		Integer lampCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 5));
		deviceStatistics.setLampCount(lampCount);

		List<SmartDevice> trashbinDevices = this.smartDeviceService.list(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 6));
		deviceStatistics.setTrashbinDevices(trashbinDevices);
		Integer trashbinCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 6));
		deviceStatistics.setTrashbinCount(trashbinCount);

		List<SmartDevice> parkDevices = this.smartDeviceService.list(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 7));
		deviceStatistics.setParkDevices(parkDevices);
		Integer parkCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 7));
		deviceStatistics.setParkCount(parkCount);

		List<SmartDevice> gateDevices = this.smartDeviceService.list(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 8));
		deviceStatistics.setGateDevices(gateDevices);
		Integer gateCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 8));
		deviceStatistics.setGateCount(gateCount);

		List<SmartDevice> xfDevices = this.smartDeviceService.list(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 9));
		deviceStatistics.setXfDevices(xfDevices);
		Integer xfCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getResidentialId, residential.getId()).eq(SmartDevice::getDeviceType, 9));
		deviceStatistics.setXfCount(xfCount);

		Integer captureCount = this.captureRecordsService.count(Condition.getQueryWrapper(new CaptureRecords()).lambda().eq(CaptureRecords::getResidentialId,residential.getId()));
		deviceStatistics.setCaptureCount(captureCount);

		Integer videoCount = this.videoCloudDeviceService.count();
		deviceStatistics.setVideoCount(videoCount);

		Integer onlineCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDefenseStatus,1));
		deviceStatistics.setOnlineCount(onlineCount);
		Integer offlineCount = this.smartDeviceService.count(Condition.getQueryWrapper(new SmartDevice()).lambda().eq(SmartDevice::getDefenseStatus,2));
		deviceStatistics.setOfflineCount(offlineCount);*/
		return deviceStatistics;
	}

	public void getSmartDeviceList(DeviceStatistics deviceStatistics, Long residentialId, Long deviceType){
		LambdaQueryWrapper<SmartDevice> smartDeviceLambdaQueryWrapper = Condition.getQueryWrapper(new SmartDevice()).lambda();

		//过滤小区的id
		smartDeviceLambdaQueryWrapper.eq(SmartDevice::getResidentialId,residentialId);
		if (deviceType == null){
			Integer smartDeviceCount = this.smartDeviceService.count(smartDeviceLambdaQueryWrapper);
			deviceStatistics.setSmartDeviceCount(smartDeviceCount);
			//过滤经纬度为空的数据
			smartDeviceLambdaQueryWrapper.like(SmartDevice::getLatitudeLongitude,".%,%.");
			List<SmartDevice> smartDeviceList = this.smartDeviceService.list(smartDeviceLambdaQueryWrapper);
			deviceStatistics.setSmartDeviceList(SmartDeviceWrapper.build(dictBizService,deviceTypeIconService,enterpriseService).listVO(smartDeviceList));
		}else {
			Integer smartDeviceCount = this.smartDeviceService.count(smartDeviceLambdaQueryWrapper.eq(SmartDevice::getDeviceType,deviceType));
			deviceStatistics.setSmartDeviceCount(smartDeviceCount);
			//过滤经纬度为空的数据
			smartDeviceLambdaQueryWrapper.like(SmartDevice::getLatitudeLongitude,".%,%.");
			List<SmartDevice> smartDeviceList = this.smartDeviceService.list(smartDeviceLambdaQueryWrapper.eq(SmartDevice::getDeviceType,deviceType));
			deviceStatistics.setSmartDeviceList(SmartDeviceWrapper.build(dictBizService,deviceTypeIconService,enterpriseService).listVO(smartDeviceList));
		}
	};

	/**
	 * 大屏物业统计
	 */
	@GetMapping("/screen/estate/static")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "大屏物业统计")
	@Transactional
	public R<EstateStaticVO> getScreenEstateStatic(String codeOrgPosition,BladeUser bladeUser) throws Exception {

		//判断是否有缓存
		String redisKey ="screen:"+ bladeUser.getUserId()+":"+ RedisConstant.ESTATE_STATIC+":"+codeOrgPosition;
		String value = redisTemplate.opsForValue().get(redisKey);
		if(value != null){
			EstateStaticVO estateStaticVO1 = (EstateStaticVO) SerializableUtil.deserializeToObject(value);
			return R.data(estateStaticVO1);
		}

		EstateStaticVO estateStaticVO = new EstateStaticVO();

		//*************************物业统计**************************//
		//社区总数
		LambdaQueryWrapper<Agency> agencyLambdaQueryWrapper = new  QueryWrapperUtil<Agency>().getBasicQueryWrapper(Agency::getOrgPosition,new Agency(),bladeUser,orgPositionService);
		agencyLambdaQueryWrapper.like(Agency::getOrgPosition,codeOrgPosition);
		List<Agency> agencies = agencyService.getBaseMapper().selectList(agencyLambdaQueryWrapper);
		int agencyCount =agencies.size();

		//小区列表
		LambdaQueryWrapper<Residential> residentialWrapper = new QueryWrapperUtil<Residential>().getBasicQueryWrapper(Residential::getOrgPosition, new Residential(), bladeUser, orgPositionService);
		residentialWrapper.like(Residential::getOrgPosition,codeOrgPosition);
		List<Residential> residentialList = residentialService.getBaseMapper().selectList(residentialWrapper);
		//小区总数
		int residentialCount =residentialList.size();
		//楼栋总数
		int buildingCount = 0;
		//单元总数
		int unitCount =0;
		//楼层总数
		int floorCount =0;
		//房间总数
		int roomCount = 0;

		for(int i = 0;i<residentialList.size();i++){
			Residential residential1 = residentialList.get(i);
			buildingCount = buildingCount + residential1.getBuildingNumber();
			unitCount = unitCount + residential1.getUnitNumber();
			floorCount = floorCount + residential1.getFloorNumber();
			roomCount = roomCount + residential1.getRoomNumber();
		}

		//住户总数

		LambdaQueryWrapper<Houseuser> houseuserLambdaQueryWrapper = new QueryWrapperUtil<Houseuser>().getBasicQueryWrapper(Houseuser::getOrgPosition,new Houseuser(),bladeUser,orgPositionService);
		houseuserLambdaQueryWrapper.like(Houseuser::getOrgPosition,codeOrgPosition);
		int userCount = houseuserService.count(houseuserLambdaQueryWrapper);

		//智能设备总数

		LambdaQueryWrapper<SmartDevice> smartDeviceLambdaQueryWrapper = new QueryWrapperUtil<SmartDevice>().getBasicQueryWrapper(SmartDevice::getOrgPosition,new SmartDevice(),bladeUser,orgPositionService);
		smartDeviceLambdaQueryWrapper.like(SmartDevice::getOrgPosition,codeOrgPosition);
		int deviceCount = smartDeviceService.count(smartDeviceLambdaQueryWrapper);



		estateStaticVO.setAgencyCount(agencyCount);
		estateStaticVO.setResidentialCount(residentialCount);
		estateStaticVO.setBuildingCount(buildingCount);
		estateStaticVO.setUnitCount(unitCount);
		estateStaticVO.setFloorCount(floorCount);
		estateStaticVO.setRoomCount(roomCount);
		estateStaticVO.setUserCount(userCount);
		estateStaticVO.setDeviceCount(deviceCount);
		estateStaticVO.setAgencyList(agencies);

		try {
			redisTemplate.opsForValue().set(redisKey,SerializableUtil.serializeToString(estateStaticVO));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return R.data(estateStaticVO);
	}


	/**
	 * 大屏智能设备分类统计
	 */
	@GetMapping("/screen/smartDevice/static")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "大屏智能设备分类统计")
	@Transactional
	public R<SmartDeviceStaticVO> smartDeviceStaticVOR(@RequestParam(value = "deviceType",required = false) String deviceType,@RequestParam("codeOrgPosition") String codeOrgPosition, BladeUser bladeUser) throws Exception {

		String redisKey = "screen:"+bladeUser.getUserId()+":"+RedisConstant.SMARTDEVICE_STATIC+":"+codeOrgPosition+":"+deviceType;
		String value = redisTemplate.opsForValue().get(redisKey);
		if(value != null){
			SmartDeviceStaticVO smartDeviceStaticVO1 =(SmartDeviceStaticVO) SerializableUtil.deserializeToObject(value);
			return R.data(smartDeviceStaticVO1);
		}


		SmartDeviceStaticVO smartDeviceStaticVO = new SmartDeviceStaticVO();
		if(deviceType == null ){
			List<SmartDevice> smartDevices = new ArrayList<>();
			List<DictBiz> deviceTypeList = dictBizService.getBaseMapper().selectList(new QueryWrapper<>(new DictBiz()).lambda().eq(DictBiz::getCode, "device_type").notIn(DictBiz::getDictKey,-1));

			for (int i=0;i<deviceTypeList.size();i++){
				DictBiz dictBiz = deviceTypeList.get(i);
				SingleTypeSmartDeviceVO singleTypeSmartDeviceVO = new SingleTypeSmartDeviceVO();
				singleTypeSmartDeviceVO.setDeviceType(dictBiz.getDictKey());
				singleTypeSmartDeviceVO.setDeviceTypeName(dictBiz.getDictValue());
				LambdaQueryWrapper<SmartDevice> smartDeviceLambdaQueryWrapper = new QueryWrapperUtil<SmartDevice>().getBasicQueryWrapper(SmartDevice::getOrgPosition, new SmartDevice(), bladeUser, orgPositionService);
				smartDeviceLambdaQueryWrapper.like(SmartDevice::getOrgPosition,codeOrgPosition).eq(SmartDevice::getDeviceType,dictBiz.getDictKey());
				smartDeviceLambdaQueryWrapper.like(SmartDevice::getLatitudeLongitude,".%,%.");
				List<SmartDevice> smartDevices1 = smartDeviceService.getBaseMapper().selectList(smartDeviceLambdaQueryWrapper);
				int count = smartDevices1.size();
				singleTypeSmartDeviceVO.setDeviceCount(count);
				smartDeviceStaticVO.getSingleTypeSmartDeviceVOList().add(singleTypeSmartDeviceVO);
				smartDevices.addAll(smartDevices1);

			}
			smartDeviceStaticVO.setSmartDeviceList(smartDevices);
		}else {
			List<DictBiz> deviceTypeList = dictBizService.getBaseMapper().selectList(new QueryWrapper<>(new DictBiz()).lambda().eq(DictBiz::getCode, "device_type").notIn(DictBiz::getDictKey, -1));

			for (int i = 0; i < deviceTypeList.size(); i++) {
				DictBiz dictBiz = deviceTypeList.get(i);

				SingleTypeSmartDeviceVO singleTypeSmartDeviceVO = new SingleTypeSmartDeviceVO();
				singleTypeSmartDeviceVO.setDeviceType(dictBiz.getDictKey());
				singleTypeSmartDeviceVO.setDeviceTypeName(dictBiz.getDictValue());
				LambdaQueryWrapper<SmartDevice> smartDeviceLambdaQueryWrapper = new QueryWrapperUtil<SmartDevice>().getBasicQueryWrapper(SmartDevice::getOrgPosition, new SmartDevice(), bladeUser, orgPositionService);
				smartDeviceLambdaQueryWrapper.like(SmartDevice::getOrgPosition, codeOrgPosition).eq(SmartDevice::getDeviceType, dictBiz.getDictKey());
				smartDeviceLambdaQueryWrapper.like(SmartDevice::getLatitudeLongitude,".%,%.");
				int count = 0;
				if (dictBiz.getDictKey().equals(deviceType)) {
					List<SmartDevice> smartDeviceList = smartDeviceService.getBaseMapper().selectList(smartDeviceLambdaQueryWrapper);
					smartDeviceStaticVO.setSmartDeviceList(smartDeviceList);
					count = smartDeviceList.size();
				} else {
					count = smartDeviceService.count(smartDeviceLambdaQueryWrapper);
				}

				singleTypeSmartDeviceVO.setDeviceCount(count);
				if(count!=0){
					smartDeviceStaticVO.getSingleTypeSmartDeviceVOList().add(singleTypeSmartDeviceVO);
				}

			}
		}

		redisTemplate.opsForValue().set(redisKey,SerializableUtil.serializeToString(smartDeviceStaticVO));
		return R.data(smartDeviceStaticVO);
	}

	/**
	 * 大屏地磁、视频云设备、人脸设备统计
	 */
	@GetMapping("/screen/MultiStatic/static")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "大屏地磁、视频云设备、人脸设备统计")
	@Transactional
	public R<MultiStaticVO> multiStaticVOR(String codeOrgPosition,BladeUser bladeUser) throws Exception {

		String redisKey = "screen:"+bladeUser.getUserId()+":"+RedisConstant.MULTISTATIC+":"+codeOrgPosition;
		String value = redisTemplate.opsForValue().get(redisKey);
		if(value != null){
			MultiStaticVO multiStaticVO1 = (MultiStaticVO)SerializableUtil.deserializeToObject(value);
			return R.data(multiStaticVO1);
		}


		MultiStaticVO multiStaticVO = new MultiStaticVO();

		//********************视频云设备统计**************************//
		LambdaQueryWrapper<VideoCloudDevice> videoCloudDeviceLambdaQueryWrapper = new QueryWrapperUtil<VideoCloudDevice>().getBasicQueryWrapper(VideoCloudDevice::getOrgPosition,new VideoCloudDevice(),bladeUser,orgPositionService);
		videoCloudDeviceLambdaQueryWrapper.like(VideoCloudDevice::getOrgPosition,codeOrgPosition);
		//视频云设备总数
		int vcDeviceCount = videoCloudDeviceService.count(videoCloudDeviceLambdaQueryWrapper);
		//在线视频云设备总数
		int offlineVcDeviceCount = videoCloudDeviceService.count(videoCloudDeviceLambdaQueryWrapper.eq(VideoCloudDevice::getIsOnline,0));
		int onlineVcDeviceCount = vcDeviceCount - offlineVcDeviceCount;

		//赋值
		VcDeviceVO vcDeviceVO = new VcDeviceVO();
		vcDeviceVO.setOfflineCount(offlineVcDeviceCount);
		vcDeviceVO.setVcDeviceCount(vcDeviceCount);
		vcDeviceVO.setOnLineCount(onlineVcDeviceCount);

		multiStaticVO.setVcDeviceVO(vcDeviceVO);

		//***************************人脸设备****************************//

		//小区总数
		LambdaQueryWrapper<Residential> residentialWrapper = new QueryWrapperUtil<Residential>().getBasicQueryWrapper(Residential::getOrgPosition, new Residential(), bladeUser, orgPositionService);
		residentialWrapper.like(Residential::getOrgPosition,codeOrgPosition);
		int residentialCount =residentialService.count(residentialWrapper);
		//住户总数

		LambdaQueryWrapper<Houseuser> houseuserLambdaQueryWrapper = new QueryWrapperUtil<Houseuser>().getBasicQueryWrapper(Houseuser::getOrgPosition,new Houseuser(),bladeUser,orgPositionService);
		houseuserLambdaQueryWrapper.like(Houseuser::getOrgPosition,codeOrgPosition);
		int userCount = houseuserService.count(houseuserLambdaQueryWrapper);

		LambdaQueryWrapper<FaceDevice> faceDeviceLambdaQueryWrapper = new QueryWrapperUtil<FaceDevice>().getBasicQueryWrapper(FaceDevice::getOrgPosition,new FaceDevice(),bladeUser,orgPositionService);
		faceDeviceLambdaQueryWrapper.like(FaceDevice::getOrgPosition,codeOrgPosition);
		//人脸总数
		int faceCount = userCount;
		//人脸设备总数
		int faceDeviceCount = faceDeviceService.count(faceDeviceLambdaQueryWrapper);
		QueryWrapper queryWrapper = new QueryWrapper();
		LambdaQueryWrapper<FaceDevice> lambdaQueryWrapper = queryWrapper.select("DISTINCT residential_id").lambda();
		String deptId = bladeUser.getDeptId();
		String[] deptIds = deptId.split(",");
		for (int i = 0; i < deptIds.length; i++) {
			OrgPosition orgPosition = orgPositionService.getBaseMapper().selectOne(new QueryWrapper<>(new OrgPosition()).lambda().eq(OrgPosition::getOrgId, deptIds[i]));
			if (i == 0) {
				lambdaQueryWrapper.like(FaceDevice::getOrgPosition,orgPosition.getOrgPosition());
			}else{
				lambdaQueryWrapper.or().like(FaceDevice::getOrgPosition,orgPosition.getOrgPosition());
			}
		}
		//已安装人脸设备小区数
		int hasInstallResidentialCount = faceDeviceService.count(lambdaQueryWrapper.like(FaceDevice::getOrgPosition,codeOrgPosition));
		//未安装人脸设备小区数
		int unInstallResidentialCount = residentialCount - hasInstallResidentialCount;

		//赋值
		FaceDeviceStaticVO faceDeviceStaticVO = new FaceDeviceStaticVO();
		faceDeviceStaticVO.setFaceCount(faceCount);
		faceDeviceStaticVO.setFaceDeviceCount(faceDeviceCount);
		faceDeviceStaticVO.setHasInstallResidentialCount(hasInstallResidentialCount);
		faceDeviceStaticVO.setUnInstallResidentialCount(unInstallResidentialCount);

		multiStaticVO.setFaceDeviceStaticVO(faceDeviceStaticVO);

		//**********************地磁统计***********************//

		//地磁总数100007
		LambdaQueryWrapper<SmartDevice> smartDeviceLambdaQueryWrapper = new QueryWrapperUtil<SmartDevice>().getBasicQueryWrapper(SmartDevice::getOrgPosition, new SmartDevice(), bladeUser, orgPositionService);
		smartDeviceLambdaQueryWrapper.eq(SmartDevice::getDeviceType,"100007");
		int parkingDeviceCount  = smartDeviceService.count(smartDeviceLambdaQueryWrapper);

		//停车抓拍数
		//进车抓拍
		int carRecordCount = 0;



		//车辆总数
		int carCount = 0;


		//车位数
		int parkingCount = 0;

		//停车场数
		int parkingLotCount =0;


		ParkingStaticVO parkingStaticVO = new ParkingStaticVO();

		parkingStaticVO.setParkingDeviceCount(parkingDeviceCount);
		parkingStaticVO.setCarCount(carCount);
		parkingStaticVO.setCarRecordCount(carRecordCount);
		parkingStaticVO.setParkingCount(parkingCount);
		parkingStaticVO.setParkingLotCount(parkingLotCount);

		multiStaticVO.setParkingStaticVO(parkingStaticVO);


		//*************告警**********************//
		int totalAlarmCount = 42;
		int deviceAlarmCount = 16 ;

		int reportAlarmCount = 14;

		int peopleAlarmCount = 12;

		AlarmOrderVO alarmOrderVO = new AlarmOrderVO();
		alarmOrderVO.setDeviceAlarmCount(deviceAlarmCount);
		alarmOrderVO.setPeopleAlarmCount(peopleAlarmCount);
		alarmOrderVO.setReportAlarmCount(reportAlarmCount);
		alarmOrderVO.setTotalAlarmCount(totalAlarmCount);

		multiStaticVO.setAlarmOrderVO(alarmOrderVO);

//		redisTemplate.opsForValue().set(redisKey,SerializableUtil.serializeToString(multiStaticVO));

		redisTemplate.opsForValue().set(redisKey,SerializableUtil.serializeToString(multiStaticVO));
		return R.data(multiStaticVO);
	}

	/**
	 * 	园区当日动态数据
	 * */
	@GetMapping("/screen/park/todayData")
	@ApiOperation("园区当日动态数据")
	public R<ParkTodayDataVO> todayData(@RequestParam("tenantId") String tenantId,
										@RequestParam(value = "agencyId",required = false) String agencyId,
										@RequestParam(value = "enterpriseId",required = false) String enterpriseId,
										@RequestParam(value = "residentialId",required = false) String residentialId){

		Integer carInCount = 0;
		Integer carOutCount = 0;
		Integer personInCount = 0;
		Integer personOutCount = 0;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		ParkingLot parkingLot = parkingLotService.getByAgencyId(Long.valueOf(agencyId));
		if(parkingLot != null){
			carInCount = tdCarRecordService.getBaseMapper().selectCount(new QueryWrapper<>(new TdCarRecord()).lambda().eq(TdCarRecord::getInMac, parkingLot.getParkKey()).like(TdCarRecord::getCreateTime, sdf.format(new Date())));
			carOutCount = tdCarRecordService.getBaseMapper().selectCount(new QueryWrapper<>(new TdCarRecord()).lambda().eq(TdCarRecord::getOutMac, parkingLot.getParkKey()).like(TdCarRecord::getCreateTime, sdf.format(new Date())));
		}
		List<SmartVideoMonitor> smartVideoMonitorList = this.smartVideoMonitorService.getByAgencyId(Long.valueOf(agencyId));
		if(smartVideoMonitorList != null && smartVideoMonitorList.size() > 0){
			List<String> monitorIdLis = smartVideoMonitorList.stream().map(SmartVideoMonitor::getSerialNum).collect(Collectors.toList());
			personInCount = smartVideoMonitorRecordService.getBaseMapper().selectCount(new QueryWrapper<>(new SmartVideoMonitorRecord()).lambda().in(SmartVideoMonitorRecord::getSerialNum, monitorIdLis).ge(SmartVideoMonitorRecord::getEnterCnt, 1).ge(SmartVideoMonitorRecord::getCreateTime, sdf.format(new Date())));
			personOutCount = smartVideoMonitorRecordService.getBaseMapper().selectCount(new QueryWrapper<>(new SmartVideoMonitorRecord()).lambda().in(SmartVideoMonitorRecord::getSerialNum, monitorIdLis).ge(SmartVideoMonitorRecord::getLeaveCnt, 1).ge(SmartVideoMonitorRecord::getCreateTime, sdf.format(new Date())));
		}else{
			personInCount = 0;
			personOutCount = 0;
		}
		ParkTodayDataVO result = new ParkTodayDataVO.ParkTodayDataVOBuilder()
			.withPeopleInNum(personInCount)
			.withPeopleOutNum(personOutCount)
			.withCarInNum(carInCount)
			.withCarOutNum(carOutCount)
			.build();
		return R.data(result);
	}

	/**
	 * 	园区当日体温统计
	 * */
	@GetMapping("/screen/park/todayTemperature")
	@ApiOperation("园区当日体温统计")
	public R<ParkTodayTemperatureVO> todayTemperature(){
		return R.data(
			new ParkTodayTemperatureVO.ParkTodayTemperatureVOBuilder()
				.withExceptionCount(temperatureRecordService.getBaseMapper().selectCount(new QueryWrapper<TemperatureRecord>().lambda()
					.eq(TemperatureRecord::getTenantId,"804981")
					.eq(TemperatureRecord::getIsDeleted,0)
					.eq(TemperatureRecord::getIsException,1)
					.ge(TemperatureRecord::getCreateTime, DateUtil.beginOfDay(new Date()))
				))
				.withOutsideCount(temperatureRecordService.getBaseMapper().selectCount(new QueryWrapper<TemperatureRecord>().lambda()
					.eq(TemperatureRecord::getTenantId,"804981")
					.eq(TemperatureRecord::getIsDeleted,0)
					.isNull(TemperatureRecord::getPersonName)
					.ge(TemperatureRecord::getCreateTime, DateUtil.beginOfDay(new Date()))

				))
				.withInsideCount(temperatureRecordService.getBaseMapper().selectCount(new QueryWrapper<TemperatureRecord>().lambda()
					.eq(TemperatureRecord::getTenantId,"804981")
					.eq(TemperatureRecord::getIsDeleted,0)
					.isNotNull(TemperatureRecord::getPersonName)
					.ge(TemperatureRecord::getCreateTime, DateUtil.beginOfDay(new Date()))

				))
				.build()
		);
	}

	/**
	 * 	园区车辆出入趋势
	 * */
	@GetMapping("/screen/park/carTendency")
	@ApiOperation("园区车辆出入趋势")
	public R<CarTendencyVO> carTendency(@RequestParam("tenantId") String tenantId,
										@RequestParam(value = "agencyId",required = false) String agencyId,
										@RequestParam(value = "enterpriseId",required = false) String enterpriseId,
										@RequestParam(value = "residentialId",required = false) String residentialId){
		/*获取近5日日期列表*/
		Date today = new Date();
		List<String> dateList = new ArrayList<>();
		dateList.add(DateUtil.formatDate(today));
		dateList.add(DateUtil.formatDate(DateUtil.offsetDay(today,-1)));
		dateList.add(DateUtil.formatDate(DateUtil.offsetDay(today,-2)));
		dateList.add(DateUtil.formatDate(DateUtil.offsetDay(today,-3)));
		dateList.add(DateUtil.formatDate(DateUtil.offsetDay(today,-4)));

		Residential residential= residentialService.getById(residentialId);
		agencyId = agencyId == null ? residential.getAgencyId().toString() : agencyId;

		/*获取近5日车辆出入记录*/
		//内部进场车辆
		List<Integer> enterCarOfInside = new ArrayList<>();
		//外部进场车辆
		List<Integer> enterCarOfOutside = new ArrayList<>();
		//内部出场车辆
		List<Integer> departCarOfInside = new ArrayList<>();
		//外部出场车辆
		List<Integer> departCarOfOutside = new ArrayList<>();
		//人员出入记录
		List<Integer> peopleNumList = new ArrayList<>();

		/*并行流获取各日期数据*/
		String finalAgencyId = agencyId;
		dateList.stream().forEach(item -> {
			enterCarOfInside.add(carEnterRecordService.getBaseMapper().selectCount(new QueryWrapper<>(new CarEnterRecord()).lambda()
				.eq(finalAgencyId !=null&&!"".equals(finalAgencyId),CarEnterRecord::getAgencyId, finalAgencyId)
				.eq(enterpriseId!=null&&!"".equals(enterpriseId),CarEnterRecord::getEnterpriseId,enterpriseId)
				.eq(tenantId!=null&&!"".equals(tenantId),CarEnterRecord::getTenantId,tenantId)
				.eq(residentialId!=null&&!"".equals(residentialId),CarEnterRecord::getResidentialId,residentialId)
				.eq(CarEnterRecord::getIsDeleted,0)
				.notIn(CarEnterRecord::getCarTypeNo,Arrays.asList("3648","3649","3650","3651"))
				.like(CarEnterRecord::getEnterTime, item)
			));
			enterCarOfOutside.add(carEnterRecordService.getBaseMapper().selectCount(new QueryWrapper<>(new CarEnterRecord()).lambda()
				.eq(finalAgencyId !=null&&!"".equals(finalAgencyId),CarEnterRecord::getAgencyId, finalAgencyId)
				.eq(enterpriseId!=null&&!"".equals(enterpriseId),CarEnterRecord::getEnterpriseId,enterpriseId)
				.eq(tenantId!=null&&!"".equals(tenantId),CarEnterRecord::getTenantId,tenantId)
				.eq(residentialId!=null&&!"".equals(residentialId),CarEnterRecord::getResidentialId,residentialId)
				.eq(CarEnterRecord::getIsDeleted,0)
				.in(CarEnterRecord::getCarTypeNo,Arrays.asList("3648","3649","3650","3651"))
				.like(CarEnterRecord::getEnterTime, item)
			));
			departCarOfInside.add(carOutRecordService.getBaseMapper().selectCount(new QueryWrapper<>(new CarOutRecord()).lambda()
				.eq(finalAgencyId !=null&&!"".equals(finalAgencyId),CarOutRecord::getAgencyId, finalAgencyId)
				.eq(enterpriseId!=null&&!"".equals(enterpriseId),CarOutRecord::getEnterpriseId,enterpriseId)
				.eq(tenantId!=null&&!"".equals(tenantId),CarOutRecord::getTenantId,tenantId)
				.eq(residentialId!=null&&!"".equals(residentialId),CarOutRecord::getResidentialId,residentialId)
				.eq(CarOutRecord::getIsDeleted,0)
				.notIn(CarOutRecord::getCarTypeNo,Arrays.asList("3648","3649","3650","3651"))
				.like(CarOutRecord::getOutTime, item)
			));
			departCarOfOutside.add(carOutRecordService.getBaseMapper().selectCount(new QueryWrapper<>(new CarOutRecord()).lambda()
				.eq(finalAgencyId !=null&&!"".equals(finalAgencyId),CarOutRecord::getAgencyId, finalAgencyId)
				.eq(enterpriseId!=null&&!"".equals(enterpriseId),CarOutRecord::getEnterpriseId,enterpriseId)
				.eq(tenantId!=null&&!"".equals(tenantId),CarOutRecord::getTenantId,tenantId)
				.eq(residentialId!=null&&!"".equals(residentialId),CarOutRecord::getResidentialId,residentialId)
				.eq(CarOutRecord::getIsDeleted,0)
				.in(CarOutRecord::getCarTypeNo,Arrays.asList("3648","3649","3650","3651"))
				.like(CarOutRecord::getOutTime, item)
			));
			peopleNumList.add( accessRecordService.getBaseMapper().selectCount( new QueryWrapper<>(new AccessRecord()).lambda()
				.eq(finalAgencyId !=null&&!"".equals(finalAgencyId),AccessRecord::getAgencyId, finalAgencyId)
				.eq(enterpriseId!=null&&!"".equals(enterpriseId),AccessRecord::getEnterpriseId,enterpriseId)
				.eq(tenantId!=null&&!"".equals(tenantId),AccessRecord::getTenantId,tenantId)
				.eq(residentialId!=null&&!"".equals(residentialId),AccessRecord::getResidentialId,residentialId)
				.eq(AccessRecord::getIsDeleted,0)
				.like(AccessRecord::getOpenTime, item)
			));
		});

		return R.data(
			new CarTendencyVO.CarTendencyVOBuilder()
				.withDateList(dateList)
				.withDepartCarOfInside(departCarOfInside)
				.withDepartCarOfOutside(departCarOfOutside)
				.withEnterCarOfInside(enterCarOfInside)
				.withEnterCarOfOutside(enterCarOfOutside)
				.withPeopleNUm(peopleNumList)
			.build()
		);
	}

	/**
	 * 获取某个账号下设备的告警率以及离线率
	 * @param staticType 1-告警率 2-离线率
	 * @param bladeUser
	 * @return
	 */
	@GetMapping("/getDeviceStatusStatic")
	public R<DeviceStatusStaticVO> getDeviceStatusStatic(Integer staticType,BladeUser bladeUser){
		Assert.notNull(staticType,"统计类型不能为空");
		String redisKey = "iot:device:status:static:"+staticType+":"+bladeUser.getUserId();
		String value = redisTemplate.opsForValue().get(redisKey);
		if(value != null){
			DeviceStatusStaticVO deviceStatusStaticVO = null;
			try {
				deviceStatusStaticVO = (DeviceStatusStaticVO) SerializableUtil.deserializeToObject(value);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return R.data(deviceStatusStaticVO);
		}

		//获取近半年月份集合
		List<String> halfYearMonth = org.springblade.common.util.DateUtil.getHalfYearMonth(true);
		DeviceStatusStaticVO deviceStatusStaticVO = new DeviceStatusStaticVO();
		deviceStatusStaticVO.setDateList(halfYearMonth);

		//staticType 1-告警率 2-离线率
		if(staticType == 1){

		}else if(staticType == 2){

		}

		try {
			redisTemplate.opsForValue().set(redisKey,SerializableUtil.serializeToString(deviceStatusStaticVO));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.data(deviceStatusStaticVO);
	}

}
