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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import org.springblade.common.Constants;
import org.springblade.community.accessrecord.entity.AccessRecord;
import org.springblade.community.accessrecord.service.IAccessRecordService;
import org.springblade.community.residential.entity.Residential;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.core.tool.api.R;

import org.springblade.estate.feedback.entity.Feedback;
import org.springblade.estate.feedback.service.IFeedbackService;
import org.springblade.homecare.alarmlist.entity.Alarm;
import org.springblade.homecare.alarmlist.service.IAlarmService;
import org.springblade.jobs.reportjob.entity.ReportJob;
import org.springblade.jobs.reportjob.service.IReportJobService;
import org.springblade.openinterface.dianxin.DataIntegration.service.IBussinessDataPushService;
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.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.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author lidexi
 * @date 2021/7/16 13:42
 * 有传小区id则按小区推送基础数据，若没有则推送所有小区的数据
 */
@RestController
@RequestMapping("/data/integration")
@AllArgsConstructor
public class BussinessDataPushController {

	private final IBussinessDataPushService bussinessDataPushService;
	private final IReportJobService reportJobService;
	private final IAccessRecordService accessRecordService;
	private final ICarEnterRecordService carEnterRecordService;
	private final ICarOutRecordService carOutRecordService;
	private final IAlarmService alarmService;
	private final IResidentialService residentialService;
	private final IFeedbackService feedbackService;

	/**
	 * 物业报修推送数据融通
	 * @param residential
	 * @return
	 */
	@PostMapping("/estateRepairPush")
	public R estateRepairPush(@RequestBody Residential residential){
		List<ReportJob> reportJobs = this.reportJobService.getBaseMapper().selectList(new QueryWrapper<>(new ReportJob()).lambda()
			.eq(residential.getId() != null,ReportJob::getResidentialId, residential.getId()));
		reportJobs.forEach(reportJob -> {
			this.bussinessDataPushService.pushEstateRepair(reportJob);
		});
		return R.success("success");
	}

	/**
	 * 投诉建议推送数据融通
	 * @param residential
	 * @return
	 */
	@PostMapping("/advicePush")
	public R advicePush(@RequestBody Residential residential){
		List<Feedback> feedbacks = feedbackService.getBaseMapper().selectList(new QueryWrapper<>(new Feedback()).lambda()
			.eq(residential.getId()!=null,Feedback::getResidentialId,residential.getId()));
		feedbacks.forEach(feedback -> {
			this.bussinessDataPushService.pushAdvice(feedback);
		});
		return R.success("success");
	}

	/**
	 * 人员进出记录推送数据融通
	 * @param residential
	 * @return
	 */
	@PostMapping("/userAccessRecordPush")
	public R userAccessRecordPush(@RequestBody Residential residential){
		List<AccessRecord> accessRecords = this.accessRecordService.getBaseMapper().selectList(new QueryWrapper<>(new AccessRecord()).lambda()
			.eq(residential.getId() != null,AccessRecord::getResidentialId, residential.getId()));
		accessRecords.forEach(accessRecord -> {
			this.bussinessDataPushService.pushUserRecord(accessRecord);
		});
		return R.success("success");
	}

	/**
	 * 车辆进出推送数据融通
	 * @param residential
	 * @return
	 */
	@PostMapping("/carRecordPush")
	public R carRecordPush(@RequestBody Residential residential){
		List<CarEnterRecord> carEnterRecords = this.carEnterRecordService.getBaseMapper().selectList(new QueryWrapper<>(new CarEnterRecord()).lambda()
			.eq(residential.getId() != null,CarEnterRecord::getResidentialId, residential.getId()).isNotNull(CarEnterRecord::getResidentialId));

		List<CarOutRecord> carOutRecords = this.carOutRecordService.getBaseMapper().selectList(new QueryWrapper<>(new CarOutRecord()).lambda()
			.eq(residential.getId() != null , CarOutRecord::getResidentialId,residential.getId()).isNotNull(CarOutRecord::getResidentialId));
		this.bussinessDataPushService.pushCarRecord(carEnterRecords,carOutRecords);
		return R.success("success");
	}

	/**
	 * 人脸抓拍推送数据融通
	 * @param residential
	 * @return
	 */
	@PostMapping("/userFaceCatchPush")
	public R userFaceCatchPush(@RequestBody Residential residential){
		List<AccessRecord> accessRecords = this.accessRecordService.getBaseMapper().selectList(new QueryWrapper<>(new AccessRecord()).lambda()
			.eq(residential.getId() != null,AccessRecord::getResidentialId, residential.getId()).isNotNull(AccessRecord::getResidentialId));
		accessRecords.forEach(accessRecord -> {
			this.bussinessDataPushService.pushFaceCatch(accessRecord);
		});
		return R.success("success");
	}

	/**
	 * 烟感气感告警记录推送数据融通
	 * @param residential
	 * @return
	 */
	@PostMapping("/alarmPush")
	public R alarmPush(@RequestBody Residential residential){
		if(residential.getId() != null){
			residential = this.residentialService.getById(residential.getId());
		}

		List<Alarm> alarms = this.alarmService.getBaseMapper().selectList(new QueryWrapper<>(new Alarm()).lambda()
			.like(residential.getId() != null,Alarm::getOrgPosition, residential.getOrgPosition()));
		//筛选烟感气感告警
		List<Alarm> smokeAlarms = alarms.stream().filter(alarm ->
			Objects.equals(alarm.getDeviceType() ,Constants.IotDeviceType.SMOKE.getType()) ||
				Objects.equals(alarm.getDeviceType() , Constants.IotDeviceType.SMOKE_MODEL.getType())).collect(Collectors.toList());

		smokeAlarms.parallelStream().forEach(alarm -> {
			this.bussinessDataPushService.pushSmokeAlarm(alarm);
		});
		List<Alarm> gasAlarms = alarms.stream().filter(alarm ->
			Objects.equals(alarm.getDeviceType() , Constants.IotDeviceType.GAS.getType()) ||
				Objects.equals(alarm.getDeviceType() , Constants.IotDeviceType.GAS_MODEL.getType())).collect(Collectors.toList());

		gasAlarms.parallelStream().forEach(alarm -> {
			this.bussinessDataPushService.pushGasAlarm(alarm);
		});
		return R.success("success");
	}


}
