package org.springblade.openinterface.dianxin.DataIntegration.Controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qiniu.linking.model.Device;
import lombok.AllArgsConstructor;
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.elevator.elevator.entity.Elevator;
import org.springblade.community.elevator.elevator.service.IElevatorService;
import org.springblade.community.houseuser.entity.Houseuser;
import org.springblade.community.houseuser.service.IHouseuserService;
import org.springblade.community.parkingveh.entity.ParkingVeh;
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.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.unit.entity.Unit;
import org.springblade.community.unit.service.IUnitService;
import org.springblade.core.tool.api.R;
import org.springblade.openinterface.dianxin.DataIntegration.service.IBaseDataPushService;
import org.springblade.smartapplication.facedevice.entity.FaceDevice;
import org.springblade.smartapplication.facedevice.service.IFaceDeviceService;
import org.springblade.smartapplication.smartsmoke.entity.SmartDevice;
import org.springblade.smartapplication.smartsmoke.service.ISmartDeviceService;
import org.springblade.smartapplication.vcdevice.entity.VideoCloudDevice;
import org.springblade.smartapplication.vcdevice.service.IVideoCloudDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties;
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lidexi
 * @date 2021/7/15 15:02
 * 数据融通-电信
 * 基础数据推送控制层
 * 有传小区id则按小区推送基础数据，若没有则推送所有小区的数据
 */
@RestController
@RequestMapping("/data/integration")
@AllArgsConstructor
public class BaseDataPushController {

	private final IBaseDataPushService baseDataPushService;
	private final IResidentialService residentialService;
	private final IBuildingService buildingService;
	private final IUnitService unitService;
	private final IRoomService roomService;
	private final IHouseuserService houseuserService;
	private final IEnterpriseStaffService enterpriseStaffService;
	private final ICarService carService;
	private final IFaceDeviceService faceDeviceService;
	private final IVideoCloudDeviceService videoCloudDeviceService;
	private final ISmartDeviceService smartDeviceService;
	private final IElevatorService elevatorService;
	private final IParkingVehService parkingVehService;




	/**
	 * 推送小区数据到电信集约平台
	 * @return
	 */
	@PostMapping("/pushResidentail")
	public R pushResidentail(@RequestBody Residential residential){
		List<Residential> residentials = this.residentialService.getBaseMapper().selectList(new QueryWrapper<>(new Residential()).lambda()
			.eq(residential.getId() != null, Residential::getId, residential.getId()));
		residentials.forEach(residentialPush -> {
			this.baseDataPushService.pushResidential(residentialPush);
		});
		return R.data("success");
	}

	/**
	 * 推送区域数据到电信集约平台
	 * @return
	 */
	@PostMapping("/pushArea")
	public R pushArea(@RequestBody Residential residential){
		List<Residential> areas = this.residentialService.getBaseMapper().selectList(new QueryWrapper<>(new Residential()).lambda()
			.eq(residential.getId() != null, Residential::getId, residential.getId()));
		areas.forEach(areaPush -> {
			this.baseDataPushService.pushArea(areaPush);
		});

		return R.data("success");
	}

	/**
	 * 推送楼栋数据到电信集约平台
	 * @return
	 */
	@PostMapping("/pushBuilding")
	public R pushBuilding(@RequestBody Residential residential){
		List<Building> buildings = buildingService.getBaseMapper().selectList(new QueryWrapper<>(new Building()).lambda()
			.eq(residential.getId() != null,Building::getResidentialId, residential.getId()));
		Assert.isFalse(buildings.size() == 0,"没有对应的楼栋数据");
		buildings.forEach(building -> {
			this.baseDataPushService.pushBuilding(building);
		});
		return R.success("success");
	}

	/**
	 * 推送单元数据到电信集约平台
	 * @return
	 */
	@PostMapping("/pushUnit")
	public R pushUnit(@RequestBody Residential residential){
		List<Unit> units = this.unitService.getBaseMapper().selectList(new QueryWrapper<>(new Unit()).lambda()
			.eq(residential.getId() != null,Unit::getResidentialId, residential.getId()));
		Assert.isFalse(units.size() == 0 ,"没有对应的单元数据");
		units.forEach(unit -> {
			this.baseDataPushService.pushUnit(unit);
		});
		return R.success("success");
	}

	/**
	 * 推送房屋数据到电信集约平台
	 * @return
	 */
	@PostMapping("/pushRoom")
	public R pushRoom(@RequestBody Residential residential){
		List<Long> residentialIdList = new ArrayList<>();
		if(residential.getId() == null){
			residentialIdList = this.residentialService.getBaseMapper().selectList(new QueryWrapper<>(new Residential())
				.lambda().isNotNull(Residential::getInhabitantId)).stream().map(Residential::getId).collect(Collectors.toList());
		}
		List<Room> rooms = this.roomService.getBaseMapper().selectList(new QueryWrapper<>(new Room()).lambda()
			.eq(residential.getId() != null,Room::getResidentialId, residential.getId()).isNull(Room::getInhabitantId)
			.in(residentialIdList.size() != 0,Room::getResidentialId,residentialIdList));
		rooms.parallelStream().forEach(room -> {
			this.baseDataPushService.pushRoom(room);
		});
		return R.success("success");
	}

	/**
	 * 推送 居民数据 以及 居民房屋关系 到电信集约平台
	 * @return
	 */
	@PostMapping("/pushHouseuser")
	public R pushHouseuser(@RequestBody Residential residential){
		List<Houseuser> houseusers = this.houseuserService.getBaseMapper().selectList(new QueryWrapper<>(new Houseuser()).lambda()
			.eq(residential.getId() != null,Houseuser::getResidentialId, residential.getId()).isNotNull(Houseuser::getBuildingId)
			.isNotNull(Houseuser::getUnitId).isNotNull(Houseuser::getRoomId));
		houseusers.forEach(houseuser -> {
			//推送给居民数据
			this.baseDataPushService.pushHouseuser(houseuser);
			//推送居民房屋关系
			this.baseDataPushService.pushHouseuserAndRoom(houseuser);
		});
		return R.success("success");
	}

	/**
	 * 推送员工数据到电信集约平台
	 * @param residential
	 * @return
	 */
	@PostMapping("/pushStaff")
	public R pushStaff(@RequestBody Residential residential){
		List<EnterpriseStaff> enterpriseStaffs = this.enterpriseStaffService.getBaseMapper().selectList(new QueryWrapper<>(new EnterpriseStaff()).lambda()
			.eq(residential.getId() != null, EnterpriseStaff::getResidentialId, residential.getId()).isNotNull(EnterpriseStaff::getIdcard));
		enterpriseStaffs.stream().filter(enterpriseStaff -> enterpriseStaff.getIdcard() != null&&!enterpriseStaff.getIdcard().equals("")).collect(Collectors.toList());
		Assert.isFalse(enterpriseStaffs.size() == 0 ,"没有企业员工需要推送");
		enterpriseStaffs.forEach(enterpriseStaff -> {
			this.baseDataPushService.pushStaff(enterpriseStaff);
		});

		return R.success("success");
	}

	/**
	 * 推送机动车数据到电信集约平台
	 * @param residential
	 * @return
	 */
	@PostMapping("/pushCar")
	public R pushCar(@RequestBody Residential residential){
		List<Car> cars = this.carService.getBaseMapper().selectList(new QueryWrapper<>(new Car()).lambda()
			.eq(residential.getId() != null, Car::getResidentialId, residential.getId()));
		cars.forEach(car -> {
			this.baseDataPushService.pushCar(car);
		});
		return R.success("success");
	}

	/**
	 * 推送设备数据以及设备状态到电信集约平台
	 * @param residential
	 * @return
	 */
	@PostMapping("/pushDevice")
	public R pushDevice(@RequestBody Residential residential){

		//人脸门禁一体机
		List<FaceDevice> faceDevices = faceDeviceService.getBaseMapper().selectList(new QueryWrapper<>(new FaceDevice()).lambda()
			.eq(residential.getId() != null,FaceDevice::getResidentialId, residential.getId()));
		//视频监控摄像机
		List<VideoCloudDevice> videoCloudDevices = videoCloudDeviceService.getBaseMapper().selectList(new QueryWrapper<>(new VideoCloudDevice()).lambda()
			.eq(residential.getId() != null,VideoCloudDevice::getResidentialId, residential.getId()));
		//iot设备
		List<SmartDevice> smartDevices = smartDeviceService.getBaseMapper().selectList(new QueryWrapper<>(new SmartDevice()).lambda()
			.eq(residential.getId() != null,SmartDevice::getResidentialId, residential.getId()).isNull(SmartDevice::getInhabitantId));
		//电梯
		List<Elevator> elevators = elevatorService.getBaseMapper().selectList(new QueryWrapper<>(new Elevator()).lambda()
			.eq(residential.getId() != null,Elevator::getResidentialId, residential.getId()));

		//车辆抓拍设备
		List<ParkingVeh> parkingVehs = parkingVehService.getBaseMapper().selectList(new QueryWrapper<>(new ParkingVeh()).lambda()
			.eq(residential.getId() != null,ParkingVeh::getResidentialId,residential.getId()));




		//推送人脸门禁一体机
		faceDevices.forEach(faceDevice -> {
//			this.baseDataPushService.pushFaceDevice(faceDevice); //finish
		});
		//推送视频监控摄像机
		videoCloudDevices.forEach(videoCloudDevice -> {
//			this.baseDataPushService.pushVideoCloudDevice(videoCloudDevice);
		});
		//推送iot设备
		smartDevices.forEach(smartDevice -> {
			this.baseDataPushService.pushIotDevice(smartDevice);
		});
		//推送电梯
		elevators.forEach(elevator -> {
			this.baseDataPushService.pushElevator(elevator);
		});
		//推送车牌识别设备
		parkingVehs.forEach(parkingVeh -> {
			this.baseDataPushService.pushParkingVeh(parkingVeh);
		});



		return R.success("success");
	}






}
