kongdeqiang
2022-09-19 a9862e81851bbe037edc6bb1c7f562c1e55c0d7f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
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);
    }
}