package org.springblade.openinterface.dianxin.DataIntegration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springblade.community.accessrecord.entity.AccessRecord;
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.core.tool.api.R;
import org.springblade.estate.feedback.entity.Feedback;
import org.springblade.homecare.alarmlist.entity.Alarm;
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.openinterface.dianxin.constants.JiYueConf;
import org.springblade.openinterface.dianxin.util.MyHttpUtil;
import org.springblade.smartapplication.carenterrecord.entity.CarEnterRecord;
import org.springblade.smartapplication.caroutrecord.entity.CarOutRecord;
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.temperaturerecord.entity.TemperatureRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

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

/**
 * @author lidexi
 * @date 2021/7/16 11:46
 */
@Service
public class BussinessDataPushServiceImpl implements IBussinessDataPushService {


	@Autowired
	private IResidentialService residentialService;
	@Autowired
	private IHouseuserService houseuserService;
	@Autowired
	private IFaceDeviceService faceDeviceService;
	@Autowired
	private IParkingVehService parkingVehService;
	@Autowired
	private ISmartDeviceService smartDeviceService;

	@Override
	public R pushEstateRepair(ReportJob reportJob) {
		Map<String, Object> map = this.reportJobToMap(reportJob);
		ResponseEntity result = MyHttpUtil.post(map, JiYueConf.UrlConstant.ESTATE_REPAIR_PUSH);
		return R.data(result);
	}

	@Override
	public R pushAdvice(Feedback feedback) {
		Map<String,Object> map = this.adviceToMap(feedback);
		ResponseEntity result = MyHttpUtil.post(map, JiYueConf.UrlConstant.ADVICES_PUSH);
		return R.data(result);
	}

	@Override
	public R pushUserRecord(AccessRecord accessRecord) {
		Map<String, Object> map = this.accessRecordToMap(accessRecord);
		ResponseEntity result = MyHttpUtil.post(map, JiYueConf.UrlConstant.USER_ACCESS_RECORD_PUSH);
		return R.data(result);
	}

	@Override
	public R pushCarRecord(List<CarEnterRecord> carEnterRecords, List<CarOutRecord> carOutRecords) {
		List<Map<String,Object>> carRecordMapList = this.carRecordToMap(carEnterRecords,carOutRecords);
		carRecordMapList.forEach(carRecordMap->{
			ResponseEntity post = MyHttpUtil.post(carRecordMap, JiYueConf.UrlConstant.CAR_RECODE_PUSH);
			System.out.println(post.toString());
		});
		return R.data("success");
	}

	@Override
	public R pushFaceCatch(AccessRecord accessRecord) {
		Map<String, Object> map = this.faceCatchToMap(accessRecord);
		ResponseEntity result = MyHttpUtil.post(map, JiYueConf.UrlConstant.FACE_CATCH_PUSH);
		return R.data(result);
	}

	@Override
	public R pushSmokeAlarm(Alarm alarm) {
		Map<String, Object> map = this.smokeAlarmToMap(alarm);
		ResponseEntity result = MyHttpUtil.post(map, JiYueConf.UrlConstant.SMOKE_DEVICE_ALARM_PUSH);
		return R.data(result);
	}

	@Override
	public R pushGasAlarm(Alarm alarm) {
		Map<String, Object> map = this.gasAlarmToMap(alarm);
		ResponseEntity result = MyHttpUtil.post(map, JiYueConf.UrlConstant.GAS_DEVICE_ALARM_PUSH);
		return R.data(result);
	}

	/**
	 * 物业报修转数据融通map
	 * @param reportJob
	 * @return
	 */
	private Map<String,Object> reportJobToMap(ReportJob reportJob){
		Map<String,Object> map = new HashMap<>();

		if (reportJob.getResidentialId() != null) {
			Residential residential = residentialService.getById(reportJob.getResidentialId());
			if (residential != null) {
				map.put("repairType", 0);
				map.put("name", reportJob.getReportor());
				map.put("tel", reportJob.getReportPhone());
				map.put("malfunctionLocation", reportJob.getReportPosition());
				map.put("status", reportJob.getJobStatus() == 1 ? 3 : 0);
				map.put("communityId", residential.getInhabitantId());
				map.put("malfunctionType", 4);
				map.put("malfunctionDescription", reportJob.getReportDetail());
				map.put("localId",reportJob.getId().toString());
			}
		}

		return map;
	}

	/**
	 * 投诉建议转数据融通map
	 * @param feedback
	 * @return
	 */
	private Map<String,Object> adviceToMap(Feedback feedback){
		Map<String,Object> map = new HashMap<>();

		map.put("feedbackType",1);//报修类型:0投诉、1建议、2表扬
		map.put("name",feedback.getName());
		map.put("tel",feedback.getPhone());
		map.put("detail",feedback.getContent());
		map.put("status",3);//0待处理、2已撤回、3已完成、4已评价
		map.put("localId",feedback.getId().toString());
		Residential residential = residentialService.getById(feedback.getResidentialId());
		map.put("communityId", residential.getInhabitantId());


		return map;
	}

	/**
	 * 投诉建议转数据融通map
	 * @param carEnterRecords
	 * @param carOutRecords
	 * @return
	 */
	private List<Map<String,Object>> carRecordToMap(List<CarEnterRecord> carEnterRecords,List<CarOutRecord> carOutRecords){
		List<Map<String,Object>> resultList = new ArrayList<>();
		carEnterRecords.forEach(carEnterRecord -> {
			Map<String,Object> map = new HashMap<>();
			map.put("plateNum",carEnterRecord.getCarNo());
			if(carEnterRecord.getResidentialId() != null){
				Residential residential = residentialService.getById(carEnterRecord.getResidentialId());
				if(residential != null){
					map.put("communityId",residential.getInhabitantId());
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					map.put("carInOutTime", sdf.format(carEnterRecord.getCreateTime()));
				}
			}
			map.put("carInOutType",1);
			List<ParkingVeh> parkingVehs = parkingVehService.getBaseMapper().selectList(new QueryWrapper<>(new ParkingVeh()).lambda()
				.eq(ParkingVeh::getResidentialId, carEnterRecord.getResidentialId())
				.eq(ParkingVeh::getVehName, carEnterRecord.getEnterGateName()));
			map.put("deviceId",parkingVehs.size()>0?parkingVehs.get(0).getInhabitantId():"未知");
			map.put("localId",carEnterRecord.getId().toString());
			resultList.add(map);
		});

		carOutRecords.forEach(carOutRecord -> {
			Map<String,Object> map = new HashMap<>();
			map.put("plateNum",carOutRecord.getCarNo());
			if(carOutRecord.getResidentialId() != null){
				Residential residential = residentialService.getById(carOutRecord.getResidentialId());
				if(residential != null){
					map.put("communityId",residential.getInhabitantId());
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					map.put("carInOutTime", sdf.format(carOutRecord.getCreateTime()));
				}
			}
			map.put("carInOutType",0);
			List<ParkingVeh> parkingVehs = parkingVehService.getBaseMapper().selectList(new QueryWrapper<>(new ParkingVeh()).lambda()
				.eq(ParkingVeh::getResidentialId, carOutRecord.getResidentialId())
				.eq(ParkingVeh::getVehName, carOutRecord.getOutGateName()));
			map.put("deviceId",parkingVehs.size()>0?parkingVehs.get(0).getInhabitantId():"未知");
			map.put("localId",carOutRecord.getId().toString());
			resultList.add(map);
		});

		return resultList;
	}

	/**
	 * 人员出入转数据融通map
	 * @param accessRecord
	 * @return
	 */
	private Map<String,Object> accessRecordToMap(AccessRecord accessRecord){
		Map<String,Object> map = new HashMap<>();

		if(accessRecord.getUserId() != null && accessRecord.getDeviceId() != null){
			Houseuser user = houseuserService.getById(accessRecord.getUserId());
			if (user != null) {
				Residential residential = residentialService.getById(user.getResidentialId());
				FaceDevice faceDevice = faceDeviceService.getById(accessRecord.getDeviceId());
				if(user != null && faceDevice != null && residential != null){
					map.put("personCode",user.getInhabitantId());
					map.put("deviceCode",faceDevice.getInhabitantId());
					map.put("inOutFlag",1);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					map.put("createTime", sdf.format(accessRecord.getCreateTime()!=null?accessRecord.getCreateTime():new Date()));
					map.put("passKind","face");
					map.put("communityId",residential.getInhabitantId());
					map.put("localId",accessRecord.getId().toString());
				}

			}

		}
		return map;
	}



	/**
	 * 人脸抓拍转数据融通map
	 * @param accessRecord
	 * @return
	 */
	private Map<String,Object> faceCatchToMap(AccessRecord accessRecord){
		Map<String,Object> map = new HashMap<>();

		if( accessRecord.getDeviceId() != null){
				Residential residential = residentialService.getById(accessRecord.getResidentialId());
				FaceDevice faceDevice = faceDeviceService.getById(accessRecord.getDeviceId());
				if( faceDevice != null && residential != null){
					map.put("infoKind",1);
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					map.put("gmtCreate", sdf.format(accessRecord.getCreateTime()));
					map.put("communityId",residential.getInhabitantId());
					map.put("localId",accessRecord.getId().toString());
					map.put("deviceId",faceDevice.getInhabitantId());

				}
			}


		return map;
	}

	/**
	 * 烟感告警数据转数据融通map
	 * @param alarm
	 * @return
	 */
	private Map<String,Object> smokeAlarmToMap(Alarm alarm){
		Map<String,Object> map = new HashMap<>();
		map.put("deviceName",alarm.getDeviceName());
		List<SmartDevice> smartDevices = smartDeviceService.getBaseMapper().selectList(new QueryWrapper<>(new SmartDevice())
			.lambda().eq(SmartDevice::getImei, alarm.getDeviceId()));
		if(smartDevices.size()>0){
			map.put("deviceId",smartDevices.get(0).getInhabitantId());
			Residential residential = residentialService.getById(smartDevices.get(0).getResidentialId());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			map.put("warnTime",sdf.format(alarm.getCreateTime() != null?alarm.getCreateTime():new Date()));
			if(residential != null)
			map.put("communityId",residential != null ? residential.getInhabitantId():"");
			map.put("event","error:1");
			map.put("localId",alarm.getId().toString());
		}

		return map;
	}

	/**
	 * 气感告警数据转数据融通map
	 * @param alarm
	 * @return
	 */
	private Map<String,Object> gasAlarmToMap(Alarm alarm){
		Map<String,Object> map = new HashMap<>();
		map.put("deviceName",alarm.getDeviceName());
		List<SmartDevice> smartDevices = smartDeviceService.getBaseMapper().selectList(new QueryWrapper<>(new SmartDevice())
			.lambda().eq(SmartDevice::getDeviceId, alarm.getDeviceId()));
		if(smartDevices.size()>0){
			map.put("deviceId",smartDevices.get(0).getInhabitantId());
			Residential residential = residentialService.getById(smartDevices.get(0).getResidentialId());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			map.put("warnTime",sdf.format(alarm.getCreateTime()!=null?alarm.getCreateTime():new Date()));
			if(residential != null)
			map.put("communityId",residential != null ? residential.getInhabitantId():"");
			map.put("event","error:1");
			map.put("localId",alarm.getId().toString());
		}

		return map;
	}


}
