SharedParkingJob.java 6.96 KB
package com.zteits.park.job;

import org.apache.commons.lang3.StringUtils;
import com.clouds.common.cache.sys.SysCodeValueCacheUtil;
import com.clouds.common.constants.CodeValKindEnum;
import com.clouds.common.constants.CodeValueEnum;
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.DataStatusEnum;
import com.zteits.clouds.api.apibase.constants.OrderStateEnum;
import com.zteits.clouds.api.apibase.constants.PsIsOccupyEnum;
import com.zteits.clouds.api.apibase.exception.AppException;
import com.zteits.park.dao.ParkingSharedDao;
import com.zteits.park.domain.TdPParkingShare;
import com.zteits.park.domain.TdPParkingShareExample;
import com.zteits.park.service.sharedparking.oper.SharedParkingJobOperService;
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.util.Date;
import java.util.List;
/**
 * 共享车位预约,进场,出场相关定时
 * .<br/>
 *
 * Copyright: Copyright (c) 2017  zteits
 *
 * @ClassName: SharedParkingJob
 * @Description:
 * @version: v1.0.0
 * @author: zhaowg
 * @date: 2018年4月17日 上午10:09:48
 * Modification History:
 * Date             Author          Version            Description
 *---------------------------------------------------------*
 * 2018年4月17日      zhaowg           v1.0.0               创建
 */
@Component
public class SharedParkingJob{
	private static final Logger logger = LoggerFactory.getLogger(SharedParkingJob.class);
	@Autowired
	private ParkingSharedDao parkingSharedDao;
	@Autowired
	private SharedParkingJobOperService sharedParkingJobOperService;

	@XxlJob("sharedParkingJob")
	public ReturnT<String> execute(String... params) throws Exception {
		String param = XxlJobHelper.getJobParam();
		if (StringUtils.isNotEmpty(param) && params == null) {
			params = param.split(",");
		}

		log("预订-15分钟内未支付担保费,取消订单");
		this.reservationOrderCancelJob();
		log("进场-超过最迟入场时间未入场");
		this.inParkingOverTimeJob();
		log("出场-有出场费用且15分钟内没有支付的,则视为欠费");
		this.outParkingNoPayedJob();
		return ReturnT.SUCCESS;
	}
	/**
	 * 出场-待支付,且距离出场时间超过15分钟没有支付时,修改订单为欠费
	 *
	 * 2018年4月17日 zhaowg
	 */
	private void outParkingNoPayedJob() {
		try{
			sharedParkingJobOperService.outParkingNoPayedJob();
		}catch (Exception e) {
			if(e instanceof AppException){
				AppException ex = (AppException)e;
				log("修改出场未支付订单失败:"+ex.getErrMsg());
			}else{
				log("修改出场未支付订单失败:"+e.getMessage());
			}
			e.printStackTrace();
		}
	}
	/**
	 * 超过最迟进场时间仍未进场的,修改订单为完成,有担保费的不退,没有担保费的,计费,修改订单为欠费
	 *
	 * 2018年4月17日 zhaowg
	 */
	private void inParkingOverTimeJob() {
		log("查询超过最迟进场时间仍未进场的共享车位订单");
		String allowLastestInParkDuration = SysCodeValueCacheUtil.queryCodeNameByCodeValue(CodeValKindEnum.SHARED_PARKING_COMMON_SET, CodeValueEnum.ALLOW_LASTEST_INPARK_DURATION,(30*60)+"");
		Integer allowLastestInParkDurationInt = Integer.valueOf(allowLastestInParkDuration);
		log("距离预约入场时间超过:"+allowLastestInParkDurationInt+"秒仍未入场的关闭订单");
		//当前时间往前推指定的分钟
		Date allowLastestInParkDate = new Date(new Date().getTime()-allowLastestInParkDurationInt*1000);
		TdPParkingShareExample example = new TdPParkingShareExample();
		example.createCriteria()
		.andPsOrderStateEqualTo(OrderStateEnum.PAYED.value())//已经支付完担保费
		.andPsPreInTimeLessThan(allowLastestInParkDate)//超过最迟入场时间
		.andPsIsOccupyEqualTo(PsIsOccupyEnum.RENT_NO_CAR.getCode())//未进场
		.andDataStatusEqualTo(DataStatusEnum.DATA_STATUS_VALID.value());//有效数据
		List<TdPParkingShare> parkingShares = parkingSharedDao.selectByExample(example );
		if(CollectionUtils.isEmpty(parkingShares)){
			log("未查询到超过最迟入场时间的订单");
			return;
		}
		log("一共查询到超过最迟入场时间订单条数:"+parkingShares.size());
		for (TdPParkingShare tdPParkingShare : parkingShares) {
			log("开始修改超过最迟入场时间的订单:"+tdPParkingShare.getPsOrderId());
			try{
				sharedParkingJobOperService.inParkingOverTimeJob(tdPParkingShare);
			}catch (Exception e) {
				if(e instanceof AppException){
					AppException ex = (AppException)e;
					log("修改超过最迟入场时间的订单:"+tdPParkingShare.getPsOrderId()+"失败:"+ex.getErrMsg());
				}else{
					log("修改超过最迟入场时间的订单:"+tdPParkingShare.getPsOrderId()+"失败:"+e.getMessage());
				}
				e.printStackTrace();
			}
		}
	}
	/**
	 * 取消预订订单,配合共享车位预订功能,当有担保费,且距离下单时间超过15分钟没有支付时,后台自动取消该订单
	 *
	 * 2018年4月17日 zhaowg
	 */
	private void reservationOrderCancelJob(){
		log("查询允许取消的共享车位订单");
		String allowCancelDuration = SysCodeValueCacheUtil.queryCodeNameByCodeValue(CodeValKindEnum.SHARED_PARKING_COMMON_SET, CodeValueEnum.ALLOW_CANCEL_DURATION,(30*60)+"");
		Long allowCancelTimeInt = Long.valueOf(allowCancelDuration);
		log("距离下单时间未超过:"+allowCancelTimeInt+"秒可以取消订单");
		//当前时间往前推指定的分钟
		Date allowCancelOrderDate = new Date(new Date().getTime()-allowCancelTimeInt*1000);
		TdPParkingShareExample example = new TdPParkingShareExample();
		example.createCriteria()
		.andPsPreActFeeGreaterThan(0L)//担保费大于0
		.andPsOrderStateEqualTo(OrderStateEnum.WAIT_PAY.value())//订单状态为待支付
		.andPsPreCreateTimeLessThan(allowCancelOrderDate)//距离下单时间已经超过规定时间
		.andPsIsOccupyEqualTo(PsIsOccupyEnum.RENT_NO_CAR.getCode())//预订-未进场
		.andDataStatusEqualTo(DataStatusEnum.DATA_STATUS_VALID.value());//有效数据
		List<TdPParkingShare> parkingShares = parkingSharedDao.selectByExample(example );
		if(CollectionUtils.isEmpty(parkingShares)){
			log("未查询到允许取消的订单");
			return;
		}
		log("一共查询到允许取消订单条数:"+parkingShares.size());
		for (TdPParkingShare tdPParkingShare : parkingShares) {
			log("开始取消订单:"+tdPParkingShare.getPsOrderId());
			try{
				sharedParkingJobOperService.reservationOrderCancelJob(tdPParkingShare);
			}catch (Exception e) {
				if(e instanceof AppException){
					AppException ex = (AppException)e;
					log("取消订单:"+tdPParkingShare.getPsOrderId()+"失败:"+ex.getErrMsg());
				}else{
					log("取消订单:"+tdPParkingShare.getPsOrderId()+"失败:"+e.getMessage());
				}
				e.printStackTrace();
			}
		}
	}

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