package com.boying.service;
|
|
import com.boying.common.BaseDao;
|
import com.boying.common.BaseService;
|
import com.boying.common.util.DateUtil;
|
import com.boying.common.util.StringUtil;
|
import com.boying.entity.CostRule;
|
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.annotation.Resource;
|
import javax.persistence.criteria.CriteriaBuilder;
|
import javax.persistence.criteria.CriteriaQuery;
|
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Root;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Date;
|
import java.util.List;
|
|
@Service
|
@Transactional(readOnly = true)
|
public class CostRuleService extends BaseService {
|
|
@Override
|
@Resource(name="costRuleDao")
|
protected void setBaseDao(BaseDao baseDao) {
|
this.baseDao = baseDao;
|
}
|
|
public double getMoney(Long parkId, Date startTime, Date endTime,int isJun) throws ParseException {
|
Specification<CostRule> specification = new Specification<CostRule>() {
|
@Override
|
public Predicate toPredicate(Root<CostRule> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
|
List<Predicate> list = new ArrayList<Predicate>();
|
list.add(cb.equal(root.get("parkId").as(Long.class), parkId));
|
Predicate[] arr = new Predicate[list.size()];
|
cq.where(list.toArray(arr));
|
return null;
|
}
|
};
|
|
List<CostRule> list = this.baseDao.findAll(specification);
|
if(list.size()==0){
|
return 0;
|
}else{
|
CostRule costRule = list.get(0);
|
|
//判断是否是军牌免费
|
if(costRule.getArmyCar()==isJun){
|
return 0;
|
}
|
|
//如果在免费时间内,不收费
|
long l = (endTime.getTime() - startTime.getTime()) / 1000 / 60;
|
if(l<=costRule.getFreeTime()){
|
return 0;
|
}
|
|
return jiSuan(costRule,startTime,endTime,0);
|
}
|
}
|
|
public double jiSuan(CostRule costRule,Date startTime, Date endTime,double sum) throws ParseException {
|
boolean flag = true;
|
while(flag){
|
//时间段1
|
String cst1 = costRule.getChargeStartTime1();
|
String cet1 = costRule.getChargeEndTime1();
|
|
//时间段2
|
String cst2 = costRule.getChargeStartTime2();
|
String cet2 = costRule.getChargeEndTime2();
|
|
//时间段3
|
String cst3 = costRule.getChargeStartTime3();
|
String cet3 = costRule.getChargeEndTime3();
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
String format = sdf.format(startTime);
|
|
if(!StringUtil.isNullOrEmpty(cst1)&&!StringUtil.isNullOrEmpty(cet1)&&costRule.getCost1()!=null){
|
String s1 = cst1.substring(0,2);
|
String e1 = cet1.substring(0,2);
|
|
//如果是不夸天的
|
if(Integer.valueOf(s1) < Integer.valueOf(e1)){
|
Date d1 = DateUtil.stringToDate(format+" "+cst1,null);
|
//如果停车开始日期在设定的开始日期之后,则按照逻辑顺序按天依次计算。
|
if(startTime.getTime()>=d1.getTime()){
|
Date d2 = DateUtil.stringToDate(format+" "+cet1,null);
|
if(endTime.getTime()<=d2.getTime()){
|
//如果在时间段1之间
|
return money(endTime.getTime() - startTime.getTime(),costRule.getCost1(),costRule.getMaxCost1())+sum;
|
}else{
|
//如果超过了时间段1的结束时间,判断是否在第二时间段
|
double money = money(d2.getTime() - startTime.getTime(), costRule.getCost1(), costRule.getMaxCost1());
|
|
if(!StringUtil.isNullOrEmpty(cst2)&&!StringUtil.isNullOrEmpty(cet2)&&costRule.getCost2()!=null){
|
String s2 = cst2.substring(0,2);
|
String e2 = cet2.substring(0,2);
|
//判断第二个时段是否夸天
|
if(Integer.valueOf(s2) > Integer.valueOf(e2)){
|
//如果第二个时间段夸天了
|
Date d21 = DateUtil.stringToDate(format+" "+cst2,null);
|
Date d22 = DateUtil.stringToDate(DateUtil.getYearMoneyDay3(format)+" "+cet2,null);
|
if(endTime.getTime()<=d22.getTime()){
|
//如果在第二时间段内
|
double money1 = money(endTime.getTime() - d21.getTime(), costRule.getCost2(), costRule.getMaxCost2());
|
return money1+money+sum;
|
}else{
|
//如果超过了第二时间段的结束时间,判断是否在第三时间段
|
double money2 = money(d22.getTime() - d21.getTime(), costRule.getCost2(), costRule.getMaxCost2());
|
|
if(!StringUtil.isNullOrEmpty(cst3)&&!StringUtil.isNullOrEmpty(cet3)&&costRule.getCost3()!=null){
|
String s3 = cst3.substring(0,2);
|
String e3 = cet3.substring(0,2);
|
//必须是不夸天的
|
Date d31 = DateUtil.stringToDate(DateUtil.getYearMoneyDay3(format)+" "+cst3,null);
|
Date d32 = DateUtil.stringToDate(DateUtil.getYearMoneyDay3(format)+" "+cet3,null);
|
if(endTime.getTime()<=d32.getTime()){
|
//如果在第三时间段内
|
double money1 = money(endTime.getTime() - d31.getTime(), costRule.getCost3(), costRule.getMaxCost3());
|
return money1+money2+sum;
|
}else{
|
//如果不在,日期+1循环
|
double money3 = money(d32.getTime() - d31.getTime(), costRule.getCost3(), costRule.getMaxCost3());
|
sum+=money+money2+money3;
|
//jiSuan(costRule,DateUtil.getYearMoneyDay4(format,cst1),endTime,sum);
|
startTime = DateUtil.getYearMoneyDay4(format,cst1);
|
}
|
}else{
|
sum+=money+money2;
|
startTime = DateUtil.getYearMoneyDay4(format,cst1);
|
}
|
}
|
}else{
|
//如果没夸天
|
Date d21 = DateUtil.stringToDate(format+" "+cst2,null);
|
Date d22 = DateUtil.stringToDate(format+" "+cet2,null);
|
if(endTime.getTime()<=d22.getTime()){
|
//如果在第二时间段内
|
double money1 = money(endTime.getTime() - d21.getTime(), costRule.getCost2(), costRule.getMaxCost2());
|
return money1+money+sum;
|
}else{
|
//如果超过了第二时间段的结束时间,判断是否在第三时间段
|
double money2 = money(d22.getTime() - d21.getTime(), costRule.getCost2(), costRule.getMaxCost2());
|
|
if(!StringUtil.isNullOrEmpty(cst3)&&!StringUtil.isNullOrEmpty(cet3)&&costRule.getCost3()!=null){
|
String s3 = cst3.substring(0,2);
|
String e3 = cet3.substring(0,2);
|
//如果是不夸天的
|
if(Integer.valueOf(s3) < Integer.valueOf(e3)){
|
Date d31 = DateUtil.stringToDate(format+" "+cst3,null);
|
Date d32 = DateUtil.stringToDate(format+" "+cet3,null);
|
if(endTime.getTime()<=d32.getTime()){
|
//如果在第三时间段内
|
double money1 = money(endTime.getTime() - d31.getTime(), costRule.getCost3(), costRule.getMaxCost3());
|
return money+money1+money2+sum;
|
}else{
|
//如果还是超出第三时间段的结束时间,那么日期+1继续执行一遍这个动作
|
double money3 = money(d32.getTime() - d31.getTime(), costRule.getCost3(), costRule.getMaxCost3());
|
sum+=money+money2+money3;
|
startTime = DateUtil.getYearMoneyDay4(format,cst1);
|
}
|
}else{
|
//如果夸天了
|
Date d31 = DateUtil.stringToDate(format+" "+cst3,null);
|
Date d32 = DateUtil.stringToDate(DateUtil.getYearMoneyDay3(format)+" "+cet3,null);
|
if(endTime.getTime()<=d32.getTime()){
|
//如果在第三时间段内
|
double money1 = money(endTime.getTime() - d31.getTime(), costRule.getCost3(), costRule.getMaxCost3());
|
return money+money1+money2+sum;
|
}else{
|
//如果还是超出第三时间段的结束时间,那么日期+1继续执行一遍这个动作
|
double money3 = money(d32.getTime() - d31.getTime(), costRule.getCost3(), costRule.getMaxCost3());
|
sum+=money+money2+money3;
|
startTime = DateUtil.getYearMoneyDay4(format,cst1);
|
}
|
}
|
}else{
|
sum+=money+money2;
|
startTime = DateUtil.getYearMoneyDay4(format,cst1);
|
}
|
}
|
}
|
}else{
|
sum+=money;
|
startTime = DateUtil.getYearMoneyDay4(format,cst1);
|
}
|
}
|
}else{
|
//如果停车开始日期在设定的开始日期之后,则按照逻辑顺序按天依次计算。
|
if(!StringUtil.isNullOrEmpty(cst2)&&!StringUtil.isNullOrEmpty(cet2)&&costRule.getCost2()!=null) {
|
String s2 = cst2.substring(0,2);
|
String e2 = cet2.substring(0,2);
|
//判断第二个时段是否夸天
|
if(Integer.valueOf(s2) > Integer.valueOf(e2)){
|
//如果第二个时间段夸天了
|
Date d21 = DateUtil.stringToDate(format+" 00:00:00",null);
|
Date d22 = DateUtil.stringToDate(format+" "+cet2,null);
|
if(endTime.getTime()<=d22.getTime()){
|
//如果在第二时间段内
|
double money1 = money(endTime.getTime() - startTime.getTime(), costRule.getCost2(), costRule.getMaxCost2());
|
return money1+sum;
|
}else{
|
//如果超过了第二时间段的结束时间,判断是否在第三时间段
|
double money2 = money(d22.getTime() - startTime.getTime(), costRule.getCost2(), costRule.getMaxCost2());
|
|
if(!StringUtil.isNullOrEmpty(cst3)&&!StringUtil.isNullOrEmpty(cet3)&&costRule.getCost3()!=null){
|
String s3 = cst3.substring(0,2);
|
String e3 = cet3.substring(0,2);
|
if(Integer.valueOf(s3) > Integer.valueOf(e3)){
|
return 0;
|
}
|
//必须是不夸天的
|
Date d31 = DateUtil.stringToDate(format+" "+cst3,null);
|
Date d32 = DateUtil.stringToDate(format+" "+cet3,null);
|
if(endTime.getTime()<=d32.getTime()){
|
//如果在第三时间段内
|
double money1 = money(endTime.getTime() - d31.getTime(), costRule.getCost3(), costRule.getMaxCost3());
|
return money1+money2+sum;
|
}else{
|
//如果不在,日期+1循环
|
double money3 = money(d32.getTime() - d31.getTime(), costRule.getCost3(), costRule.getMaxCost3());
|
sum+=money2+money3;
|
startTime = DateUtil.stringToDate(format+" "+cst1,null);
|
}
|
}else{
|
sum+=money2;
|
startTime = DateUtil.stringToDate(format+" "+cst1,null);
|
}
|
}
|
}else{
|
if(!StringUtil.isNullOrEmpty(cst3)&&!StringUtil.isNullOrEmpty(cet3)&&costRule.getCost3()!=null){
|
String s3 = cst3.substring(0,2);
|
String e3 = cet3.substring(0,2);
|
//如果是不夸天的
|
if(Integer.valueOf(s3) < Integer.valueOf(e3)){
|
startTime = DateUtil.stringToDate(format+" "+cst1,null);
|
}else{
|
//如果夸天了
|
Date d31 = DateUtil.stringToDate(format+" 00:00:00",null);
|
Date d32 = DateUtil.stringToDate(format+" "+cet3,null);
|
if(endTime.getTime()<=d32.getTime()){
|
//如果在第三时间段内
|
double money1 = money(endTime.getTime() - d31.getTime(), costRule.getCost3(), costRule.getMaxCost3());
|
return money1+sum;
|
}else{
|
//如果还是超出第三时间段的结束时间,那么日期+1继续执行一遍这个动作
|
double money3 = money(d32.getTime() - d31.getTime(), costRule.getCost3(), costRule.getMaxCost3());
|
sum+=money3;
|
startTime = DateUtil.stringToDate(format+" "+cst1,null);
|
}
|
}
|
}else{
|
startTime = DateUtil.stringToDate(format+" "+cst1,null);
|
}
|
}
|
}else{
|
startTime = DateUtil.stringToDate(format+" "+cst1,null);
|
}
|
}
|
}
|
}
|
}
|
|
return sum;
|
}
|
|
//l:分钟 cost:每小时费用 maxCost:封顶费
|
public double money(long l,Double cost,Integer maxCost){
|
l=l/1000/60;
|
int time = 0;
|
// if(((l*1.0)/60)>(l/60)){//判断停车时间是否要加1
|
// time = Long.valueOf(l / 60 + 1).intValue();
|
// }else{
|
// time = Long.valueOf(l / 60).intValue();
|
// }
|
|
if(((l*1.0)/30)>(l/30)){//判断停车时间是否要加1
|
time = Long.valueOf(l / 30 + 1).intValue();
|
}else{
|
time = Long.valueOf(l / 30).intValue();
|
}
|
if(maxCost!=null){
|
if(time*cost>maxCost){//大于封顶价格
|
return maxCost;
|
}
|
}
|
return time*cost;
|
}
|
|
public static void main(String[] args) {
|
System.out.println((120*1.0)/60>2);
|
}
|
}
|