package org.springblade.openinterface.carbarrier.listener;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.springblade.community.car.entity.Car;
import org.springblade.community.car.service.ICarService;
import org.springblade.core.log.logger.BladeLogger;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.openinterface.carbarrier.entity.SubscribeResponseResult;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

/**
 * Created By lidexi in 2021/1/5
 * 车道道闸订阅接口
 **/
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("openinterface/carbarrier/listen")
public class CarBarrierSubscribeController {

	private BladeLogger logger;
	private ICarEnterRecordService carEnterRecordService;
	private ICarOutRecordService carOutRecordService;

	private ICarService carService;

	/**
	 * 车道道闸入场记录订阅接口
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/enterRecordReport")
	public SubscribeResponseResult enterRecordReport(@RequestBody Map<String, Object> map) throws ParseException {
		log.error("enterRecordReport --> " + JSONUtil.toJsonStr(map));
		logger.info("enterRecordReport", JSONUtil.toJsonStr(map));

		CarEnterRecord carEnterRecord = new CarEnterRecord();
		Map data = (Map) map.get("data");
		/**
		 * key	String	是		停车场key
		 * --carNo	String	是		车牌号
		 * --orderNo	String	是		停车订单号
		 * --enterTime	String	是		入场时间
		 * --carType	String	是		车辆类型编码
		 * --gateName	String	是		入口车道名称
		 * --operatorName	String	是		入口操作员名称
		 * --reserveOrderNo	String	是		预约车位订单号
		 * --imgUrl
		 */
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		df.setTimeZone(TimeZone.getTimeZone("Etc/GMT-8"));
		if (data.get("key") != null) {
			carEnterRecord.setParkKey(data.get("key").toString());
		}

		if (data.get("carNo") != null) {
			carEnterRecord.setCarNo(data.get("carNo").toString());
			Car carWrapper = new Car();
			carWrapper.setNumber(carEnterRecord.getCarNo());
			Car car = carService.getOne(Condition.getQueryWrapper(carWrapper));
			if (car != null){
				carEnterRecord.setEnterpriseId(car.getEnterpriseId());
				carEnterRecord.setAgencyId(car.getAgencyId());
				carEnterRecord.setResidentialId(car.getResidentialId());
				carEnterRecord.setOrgPosition(car.getOrgPosition());
			}

		}

		if (data.get("orderNo") != null) {
			carEnterRecord.setOrderNo(data.get("orderNo").toString());
		}

		if (data.get("enterTime") != null) {
			carEnterRecord.setEnterTime(df.parse(data.get("enterTime").toString()));
		}

		if (data.get("carType") != null) {
			carEnterRecord.setCarTypeNo(data.get("carType").toString());
		}

		if (data.get("gateName") != null) {
			carEnterRecord.setEnterGateName(data.get("gateName").toString());
		}

		if (data.get("operatorName") != null) {
			carEnterRecord.setEnterOperatorName(data.get("operatorName").toString());
		}

		if(data.get("reserveOrderNo") != null){
			carEnterRecord.setReserveOrderNo(data.get("reserveOrderNo").toString());
		}

		carEnterRecordService.save(carEnterRecord);
		SubscribeResponseResult subscribeResponseResult = new SubscribeResponseResult();
		subscribeResponseResult.setCode("1");
		subscribeResponseResult.setMsg("ok");
		subscribeResponseResult.setRand(map.get("rand").toString());
		subscribeResponseResult.setSign(map.get("sign").toString());
		return subscribeResponseResult;
	}

	/**
	 * 车道道闸出场记录订阅接口
	 * @param map
	 * @return
	 */
	@PostMapping("/outRecordReport")
	public SubscribeResponseResult outRecordReport(@RequestBody Map<String, Object> map) throws ParseException {
		log.error("outRecordReport --> " + JSONUtil.toJsonStr(map));
		logger.info("outRecordReport", JSONUtil.toJsonStr(map));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		df.setTimeZone(TimeZone.getTimeZone("Etc/GMT-8"));
		/**
		 * --key	String	是		停车场key
		 * --carNo	String	是		车牌号
		 * --orderNo	String	是		停车订单号
		 * --outTime	String	是		出场时间
		 * --carType	String	是		车辆类型编码
		 * --gateName	String	是		出口车道名称
		 * --operatorName	String	是		出口操作员名称
		 * --totalAmount	String	是		订单总金额
		 * --couponKey	String	是		优惠券GUID
		 * --imgUrl	String	是		出口车辆图片
		 * --couponMoney	String	是		优惠券金额
		 * --walletPayMoney	String	是		钱包付款金额
		 * --freeReason	String	否		免费原因
		 */
		Map data = (Map) map.get("data");
		CarOutRecord carOutRecord = new CarOutRecord();
		if (data.get("key") != null) {
			carOutRecord.setParkKey(data.get("key").toString());
		}

		if (data.get("carNo") != null) {
			carOutRecord.setCarNo(data.get("carNo").toString());
			Car carWrapper = new Car();
			carWrapper.setNumber(carOutRecord.getCarNo());
			Car car = carService.getOne(Condition.getQueryWrapper(carWrapper));
			if (car != null){
				carOutRecord.setEnterpriseId(car.getEnterpriseId());
				carOutRecord.setAgencyId(car.getAgencyId());
				carOutRecord.setResidentialId(car.getResidentialId());
				carOutRecord.setOrgPosition(car.getOrgPosition());
			}
		}

		if (data.get("orderNo") != null) {
			carOutRecord.setOrderNo(data.get("orderNo").toString());
			//根据订单号查询入场记录
			List<CarEnterRecord> carEnterRecords = carEnterRecordService.getBaseMapper().selectList(new QueryWrapper<>(new CarEnterRecord()).lambda().eq(CarEnterRecord::getOrderNo, data.get("orderNo").toString()));
			if (carEnterRecords.size() > 0) {
				carOutRecord.setEnterGateName(carEnterRecords.get(0).getEnterGateName());
				carOutRecord.setEnterTime(carEnterRecords.get(0).getEnterTime());
			}
		}
		if (data.get("outTime") != null) {
			carOutRecord.setOutTime(df.parse(data.get("outTime").toString()));
		}

		if (data.get("carType") != null) {
			carOutRecord.setCarTypeNo(data.get("carType").toString());
		}

		if (data.get("gateName") != null) {
			carOutRecord.setOutGateName(data.get("gateName").toString());
		}

		if (data.get("operatorName") != null) {
			carOutRecord.setOutOperatorName(data.get("operatorName").toString());
		}

		if (data.get("totalAmount") != null) {
			carOutRecord.setTotalAmount(data.get("totalAmount").toString());
		}

		if (data.get("freeReason") != null) {
			carOutRecord.setFreeReason(data.get("freeReason").toString());
		}
		carOutRecordService.save(carOutRecord);
		SubscribeResponseResult subscribeResponseResult = new SubscribeResponseResult();
		subscribeResponseResult.setCode("1");
		subscribeResponseResult.setMsg("ok");
		subscribeResponseResult.setRand(map.get("rand").toString());
		subscribeResponseResult.setSign(map.get("sign").toString());
		return subscribeResponseResult;
	}

	/**
	 * 车道道闸入场图片上传订阅接口
	 * @param map
	 * @return
	 */
	@PostMapping("/enterRecordPicReport")
	public SubscribeResponseResult enterRecordPicReport(@RequestBody Map<String, Object> map) {
		log.error("enterRecordPicReport --> " + JSONUtil.toJsonStr(map));
		logger.info("enterRecordPicReport", JSONUtil.toJsonStr(map));

		Map data = (Map)map.get("data");
		LambdaQueryWrapper<CarEnterRecord> wrapper = new QueryWrapper<>(new CarEnterRecord()).lambda();
		if (data.get("key") != null) {
			wrapper.eq(CarEnterRecord::getParkKey,data.get("key").toString());
		}

		if (data.get("carNo") != null) {
			wrapper.eq(CarEnterRecord::getCarNo,data.get("carNo").toString());
		}

		if (data.get("orderNo") != null) {
			wrapper.eq(CarEnterRecord::getOrderNo,data.get("orderNo").toString());
		}
		CarEnterRecord carEnterRecord = carEnterRecordService.getOne(wrapper);
		if(carEnterRecord != null){
			if(data.get("imgUrl") != null){
				carEnterRecord.setEnterImgPath(data.get("imgUrl").toString().replaceAll("amp;",""));
				carEnterRecordService.updateById(carEnterRecord);
			}
		}

		SubscribeResponseResult subscribeResponseResult = new SubscribeResponseResult();
		subscribeResponseResult.setCode("1");
		subscribeResponseResult.setMsg("ok");
		subscribeResponseResult.setRand(map.get("rand").toString());
		subscribeResponseResult.setSign(map.get("sign").toString());
		return subscribeResponseResult;
	}

	/**
	 * 车道道闸出场图片上传订阅接口
	 * @param map
	 * @return
	 */
	@PostMapping("/outRecordPicReport")
	public SubscribeResponseResult outRecordPicReport(@RequestBody Map<String, Object> map) {
		log.error("outRecordPicReport --> " + JSONUtil.toJsonStr(map));
		logger.info("outRecordPicReport", JSONUtil.toJsonStr(map));

		Map data = (Map)map.get("data");
		LambdaQueryWrapper<CarOutRecord> wrapper = new QueryWrapper<>(new CarOutRecord()).lambda();
		if (data.get("key") != null) {
			wrapper.eq(CarOutRecord::getParkKey,data.get("key").toString());
		}

		if (data.get("carNo") != null) {
			wrapper.eq(CarOutRecord::getCarNo,data.get("carNo").toString());
		}

		if (data.get("orderNo") != null) {
			wrapper.eq(CarOutRecord::getOrderNo,data.get("orderNo").toString());
		}
		CarOutRecord carOutRecord = carOutRecordService.getOne(wrapper);
		if(carOutRecord != null){
			if(data.get("imgUrl") != null){
				carOutRecord.setOutImgPath(data.get("imgUrl").toString().replaceAll("amp;",""));
				carOutRecordService.updateById(carOutRecord);
			}
		}
		SubscribeResponseResult subscribeResponseResult = new SubscribeResponseResult();
		subscribeResponseResult.setCode("1");
		subscribeResponseResult.setMsg("ok");
		subscribeResponseResult.setRand(map.get("rand").toString());
		subscribeResponseResult.setSign(map.get("sign").toString());
		return subscribeResponseResult;
	}
}
