ParkingLotHeatMapHourJob.java 12.5 KB
package com.zteits.park.job;

import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.clouds.common.cache.park.ParkingLotGeoCacheUtil;
import com.clouds.common.utils.ListPageHelper;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zteits.clouds.api.apibase.constants.LonLatTypeEnum;
import com.zteits.park.dao.InParkLogDao;
import com.zteits.park.dao.ParkingLotDao;
import com.zteits.park.dao.ParkinglotHeatmapHourDao;
import com.zteits.park.domain.InparkLogCountDO;
import com.zteits.park.domain.ParkingLotAndGeoDO;
import com.zteits.park.domain.ParkinglotHeatmapHour;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Map.Entry;

/**
 * 企业云平台-->停车热力分布图-->按照小时.<br/>
 *
 * Copyright: Copyright (c) 2017  zteits
 *
 * @ClassName: ParkingLotHeatMapHourJob.java
 * @Description:
 * @version: v1.0.0
 * @author: wangfs
 * @date: 2017年7月10日   下午5:24:36
 * Modification History:
 * Date             Author          Version            Description
 *---------------------------------------------------------*
 * 2017年7月10日      wangfs           v1.0.0               创建
 */
@Component
public class ParkingLotHeatMapHourJob{
	private static final Logger logger = LoggerFactory.getLogger(ParkingLotHeatMapHourJob.class);
	@Autowired
	private ParkingLotDao parkingLotDao;

	@Autowired
	private InParkLogDao inParkLogDao;

	@Autowired
	private ParkinglotHeatmapHourDao parkinglotHeatmapHourDao;

	/**停车场热力分布查询范围 200米.*/
	private static final String  DISTANCE = "200";


	/**
	 * 企业云平台-->停车热力分布图-->按照小时.<br/>
	 * 描述:1.每间隔2小时跑一次(0,2,4,6,8...24点跑job)即:0 0,2,4,6,8,10,12,14,16,18,20,22 * * * ? *。
	 *      2.统计2小时中200米内各个停车场进场车辆的和。
	 *
	 */
	@XxlJob("ParkingLotHeatMapHourJob")
	public ReturnT<String> execute(String... params) throws Exception {
		String param = XxlJobHelper.getJobParam();
		if (StringUtils.isNotEmpty(param) && params == null) {
			params = param.split(",");
		}

		ReturnT<List<ParkinglotHeatmapHour>> result = new ReturnT<List<ParkinglotHeatmapHour>>();
		SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.log("--begin---企业云平台-->停车热力分布图-->按照小时..."+formart.format(new Date()));
		if(params == null || params.length == 0){
			return new ReturnT<>(ReturnT.FAIL_CODE, "请求参数不能为空,格式:5,91,1 参数说明:第一个参数='省份Id'"
					+ " ,第二个参数='地市ID', 第三个参数='停车场经纬度'" );
		}

		this.log("获取到的入参="+params.toString());

		ParkingLotAndGeoDO parkingLotAndGeoDO = new ParkingLotAndGeoDO();
		try{
			parkingLotAndGeoDO.setPlProvinceId(Long.valueOf(params[0]));
			parkingLotAndGeoDO.setPlCityId(Long.valueOf(params[1]));
			parkingLotAndGeoDO.setGeoType(Integer.valueOf(params[2]));
			/**停车热力分布图-->按小时-查询停车场基本信息及停车数量..*/
			result=  this.queryParkingLotHeatMapHour(parkingLotAndGeoDO);
			/**通过给定的停车场编号和给定的范围,查询附近的停车场编号,将查询到的范围停车场 进场车辆.*/
			this.queryParkLotNosByRadius(result);
			System.out.println(JSONObject.toJSON(result.getContent()));
			if(result != null && !CollectionUtils.isEmpty(result.getContent())){
				SimpleDateFormat formartDate = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
				Date heatmapTime = formartDate.parse(formartDate.format(new Date()));
				this.log("begin删除数据,入参="+formartDate.format(new Date()));
				Integer deleteCount = parkinglotHeatmapHourDao.deleteParkinglotHeatmapHourByheatmapTime(heatmapTime,parkingLotAndGeoDO.getPlCityId());
				this.log("end 删除数据,结果="+deleteCount+" 条数据。");
				this.log("begin保存数据");
				Integer insertCount = parkinglotHeatmapHourDao.insertParkinglotHeatmapHourList(result.getContent());
				this.log("end保存结果="+insertCount+" 条数据。");
			}
			this.log("--end---企业云平台-->停车热力分布图-->按照小时..."+formart.format(new Date()));
		}catch (Exception e) {
			if (e instanceof InterruptedException) {
    	        throw e;
    	    }
    	    logger.warn("{}", e);
		}
		return ReturnT.SUCCESS;
	}

	/**
	 * 通过给定的停车场编号和给定的范围,查询附近的停车场编号,将查询到的范围停车场 进场车辆sum.<br/>

	 * @return
	 * 2017年7月10日 zhaowg
	 */
	private void queryParkLotNosByRadius(ReturnT<List<ParkinglotHeatmapHour>> result){
		this.log("begin查询200米范围内的停车场编码...");
		Map<String,List<String>> map = new HashMap<String,List<String>>();
		List<String> ParkLotNosList = new ArrayList<String>();
        if(result != null && !CollectionUtils.isEmpty(result.getContent())){
        	List<ParkinglotHeatmapHour> list = result.getContent();
			for(ParkinglotHeatmapHour parkinglotHeatmapHour :list){
				if(parkinglotHeatmapHour != null){
					ParkLotNosList = ParkingLotGeoCacheUtil.queryParkLotNosByRadius(LonLatTypeEnum.LON_LAT_TYPE_1,Double.valueOf(parkinglotHeatmapHour.getLon().toString()),Double.valueOf(parkinglotHeatmapHour.getLat().toString()),Double.valueOf(DISTANCE));
					if(!CollectionUtils.isEmpty(ParkLotNosList)){
						map.put(parkinglotHeatmapHour.getPlNo(), ParkLotNosList);
					}
				}
			}
			this.log("end查询200米范围内的停车场编码...,结果="+(map != null && map.size()>0 ? map.size() :0));
			//计算范围
			if(map != null && map.size() >0){
				Set<Entry<String, List<String>>> set = map.entrySet();
				for(Iterator<Entry<String, List<String>>>  iterator = set.iterator();iterator.hasNext();){
					Entry<String, List<String>> entry = iterator.next();
					List<ParkinglotHeatmapHour> ll = result.getContent();
					for(ParkinglotHeatmapHour parkinglotHeatmapHour :ll){
						if(parkinglotHeatmapHour != null && entry.getKey().equals(parkinglotHeatmapHour.getPlNo())){
							this.log("停车场编码=["+entry.getKey()+"],对应的200米范围内的停车场编码="+JSONObject.toJSONString(entry.getValue()));
							parkinglotHeatmapHour.setCurrentParkCount(this.sumInParkCount(entry.getValue(), ll));
						}
					}

				}

			}
		}


	}

	/**
	 * 计算对应范围内多个停车场停车数量求和.<br/>
	 * @param list
	 * @param ll
	 * @return
	 */
	private Integer sumInParkCount(List<String> list,List<ParkinglotHeatmapHour> ll){
		this.log("begin计算200米范围内,各个停车场进场车辆和..");
		int sum = 0;
		if(!CollectionUtils.isEmpty(list) && !CollectionUtils.isEmpty(ll)){
			for(String plNo : list){
				for(ParkinglotHeatmapHour parkinglotHeatmapHour :ll){
					if(plNo.equals(parkinglotHeatmapHour.getPlNo())){
						this.log(plNo+"=对应入场车辆数量="+parkinglotHeatmapHour.getCurrentParkCount());
						sum += parkinglotHeatmapHour.getCurrentParkCount();
					}
				}
			}
		}
		this.log("end计算200米范围内,各个停车场进场车辆和..,结果sum="+sum);
		return sum;
	}



	/**
	 * 停车热力分布图-->按小时-查询停车场基本信息及停车数量.<br/>
	 * @param parkingLotAndGeoDO
	 * @return
	 */
	private ReturnT<List<ParkinglotHeatmapHour>> queryParkingLotHeatMapHour(ParkingLotAndGeoDO parkingLotAndGeoDO ){
		SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
		List<String> plNos = new ArrayList<String>();
		String endTime = formart.format(new Date());
		List<InparkLogCountDO>  inparkList = new ArrayList<InparkLogCountDO>();
		List<ParkinglotHeatmapHour> list = new ArrayList<ParkinglotHeatmapHour>();
		try{
			this.log("begin--查询停车场基本信息list..,入参="+JSONObject.toJSONString(parkingLotAndGeoDO));
			/**查询停车场及停车场经纬度信息.*/
			parkingLotAndGeoDO.setLonLatType(LonLatTypeEnum.LON_LAT_TYPE_1.value());
			parkingLotAndGeoDO.setGeoType(1);
			 list = parkingLotDao.queryParkingLotAndGeoInfo(parkingLotAndGeoDO);
			this.log("end--查询基本信息list..,结果list.size()="+(list != null && list.size() >0 ? list.size() : 0));
			if(!CollectionUtils.isEmpty(list)){
				for(ParkinglotHeatmapHour parklot:list){
					if(parklot != null){
						plNos.add(parklot.getPlNo());
					}
				}

				Date beginTime = headDate(formart.parse(endTime),-2);
                /**查询进场车辆数.*/
				  inparkList = this.queryParklotInCarCount(beginTime, formart.parse(endTime), plNos);

			}

			/**合并list.*/
			list =  mergeList(formart.parse(endTime),inparkList,list);


		}catch (Exception e) {
			e.printStackTrace();
			return new ReturnT<List<ParkinglotHeatmapHour>>(ReturnT.FAIL_CODE,"查询信息异常!");
		}


		return new ReturnT<List<ParkinglotHeatmapHour>>(list);
	}

	/**
	 * 合并list<br/>
	 * @param endTime
	 * @param inparkList
	 * @param list
	 * @return
	 * @throws ParseException
	 */
	private List<ParkinglotHeatmapHour>  mergeList(Date endTime,List<InparkLogCountDO>  inparkList,List<ParkinglotHeatmapHour> list) throws ParseException{
		List<ParkinglotHeatmapHour> rsultList = new ArrayList<ParkinglotHeatmapHour>();
		SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
		Calendar col = Calendar.getInstance();
		col.setTime(formart.parse(formart.format(endTime)));
		int heatmapTime = col.get(Calendar.HOUR_OF_DAY);
		this.log("begin合并进场车辆数及停车场及停车场经纬度信息信息");
		this.log("转换后的小时="+heatmapTime);

		if(!CollectionUtils.isEmpty(list) && !CollectionUtils.isEmpty(inparkList)){
				for(ParkinglotHeatmapHour parkinglotHeatmapHour:list){
					parkinglotHeatmapHour.setHeatmapTime(endTime);
					parkinglotHeatmapHour.setHeatmapHour(heatmapTime+"");
					parkinglotHeatmapHour.setCreateDate(new Date());
					parkinglotHeatmapHour.setRanges(Integer.valueOf(DISTANCE));
					parkinglotHeatmapHour.setDataStatus(1);
					parkinglotHeatmapHour.setCurrentParkCount(1);
					for(InparkLogCountDO inparkLogCountDO:inparkList){
						if(parkinglotHeatmapHour.getPlNo().equals(inparkLogCountDO.getPlNo())){
							System.out.println("存在在停车辆,车场编码="+parkinglotHeatmapHour.getPlNo()+"="+inparkLogCountDO.getCurrentParkCount());
							parkinglotHeatmapHour.setCurrentParkCount(inparkLogCountDO.getCurrentParkCount());
						}
					}

				}
				rsultList.addAll(list);
			}


		this.log("end合并进场车辆数及停车场及停车场经纬度信息信息,结果="+JSONObject.toJSON(rsultList));
		return rsultList;
	}

	/**
	 * 查询进场车辆数.<br/>
	 * @param beginTime
	 * @param endTime
	 * @param plNos
	 * @return
	 */
	private List<InparkLogCountDO> queryParklotInCarCount(Date beginTime, Date endTime,List<String> plNos){
		SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
		this.log("---begin查询进场车辆数,入参={beginTime="+formart.format(beginTime)+",endTime="+formart.format(endTime)+",plNos="+plNos+"}");
		List<InparkLogCountDO>  inparkList = new ArrayList<InparkLogCountDO>();
		if(!CollectionUtils.isEmpty(plNos)){
			ListPageHelper<String> pageInfo =  new ListPageHelper<String>(plNos,500);
			for(int i=1;i<=pageInfo.getTotalPages();i++){
				System.out.println(JSONObject.toJSON(pageInfo.getPagedList(i)));
				List<InparkLogCountDO>  list = inParkLogDao.queryParklotInCarCount(beginTime, endTime, pageInfo.getPagedList(i));
				if(!CollectionUtils.isEmpty(list)){
					inparkList.addAll(list);
				}
			}
		}
		this.log("---end查询进场车辆数,结果 inparkList.size="+((inparkList !=null && inparkList.size() >0) ? inparkList.size() :0 ));

		return inparkList;
	}




	/**
	 * 计算前2小时的时间.<br/>
	 * @param date
	 * @param hours
	 * @return
	 * @throws ParseException
	 */
	private static Date headDate(Date date ,int hours) throws ParseException {
	    Calendar cal = Calendar.getInstance();
	    cal.setTime(date);
	    cal.add(Calendar.HOUR_OF_DAY, hours);
	    return cal.getTime();
	}

	private void log(String msg){
		XxlJobHelper.log(msg);
		logger.info(msg);
	}

	public static void main(String[] args) throws ParseException {
		SimpleDateFormat formart = new SimpleDateFormat("yyyy-MM-dd HH:00:00");
		System.out.println(formart.format(new Date()));
		Calendar col = Calendar.getInstance();
		col.setTime(formart.parse(formart.format(new Date())));
		System.out.println(col.get(Calendar.HOUR_OF_DAY));
		System.out.println(formart.format(new Date()).substring(10, 13));
	}


}