xuhy
2023-06-14 17f18c3d56a70327874aa7dda0adfa2f5e3dafce
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package com.stylefeng.guns.modular.system.service.impl;
 
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.controller.resp.DataStatisticsOrderYearResp;
import com.stylefeng.guns.modular.system.controller.resp.PerformanceTableResp;
import com.stylefeng.guns.modular.system.controller.resp.TOrderResp;
import com.stylefeng.guns.modular.system.controller.resp.TOrderServerResp;
import com.stylefeng.guns.modular.system.dao.TAppUserMapper;
import com.stylefeng.guns.modular.system.dao.TBranchOfficeMapper;
import com.stylefeng.guns.modular.system.dao.TDriverMapper;
import com.stylefeng.guns.modular.system.dao.TOrderMapper;
import com.stylefeng.guns.modular.system.enums.OrderStateEnum;
import com.stylefeng.guns.modular.system.model.TAppUser;
import com.stylefeng.guns.modular.system.model.TBranchOffice;
import com.stylefeng.guns.modular.system.model.TDriver;
import com.stylefeng.guns.modular.system.model.TOrder;
import com.stylefeng.guns.modular.system.service.ITOrderService;
import com.stylefeng.guns.modular.system.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
 
/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2023-02-15
 */
@Service
public class TOrderServiceImpl extends ServiceImpl<TOrderMapper, TOrder> implements ITOrderService {
 
    @Autowired
    private TOrderMapper tOrderMapper;
    @Autowired
    private TAppUserMapper tAppUserMapper;
    @Autowired
    private TDriverMapper tDriverMapper;
    @Autowired
    private TBranchOfficeMapper tBranchOfficeMapper;
 
    @Override
    public List<TOrderResp> getOrderList(String createTime, String code, Integer source, String userName, String userPhone, Integer state, String driverName,Integer isException) {
        String startTime = null;
        String endTime = null;
        // 开始,结束时间
        if(StringUtils.hasLength(createTime)){
            String[] split = createTime.split(" - ");
            startTime = split[0] + " 00:00:00";
            endTime = split[1] + " 23:59:59";
        }
        Integer roleType = Objects.requireNonNull(ShiroKit.getUser()).getRoleType();
        Integer objectId = Objects.requireNonNull(ShiroKit.getUser()).getObjectId();
        return tOrderMapper.getOrderList(startTime,endTime,code,source,userName,userPhone,state,driverName,isException,roleType,objectId);
    }
 
    @Override
    public void orderDetail(Integer orderId, Model model) {
        // 订单信息
        TOrder tOrder = tOrderMapper.selectById(orderId);
        model.addAttribute("createTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tOrder.getCreateTime()));//下单时间
        model.addAttribute("code",tOrder.getCode());//订单编号
        model.addAttribute("source",tOrder.getSource());//订单来源
        model.addAttribute("startAddress",tOrder.getStartAddress());//起点
        model.addAttribute("startLng",tOrder.getStartLng());// 起点经度
        model.addAttribute("startLat",tOrder.getStartLat());// 起点纬度
        model.addAttribute("endAddress",tOrder.getEndAddress());//终点
        model.addAttribute("endLng",tOrder.getEndLng());// 终点经度
        model.addAttribute("endLat",tOrder.getEndLat());// 终点纬度
 
        if(Objects.nonNull(tOrder.getStartTime())){
            model.addAttribute("startTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tOrder.getStartTime()));//乘车时间
        }else {
            model.addAttribute("startTime","");//乘车时间
        }
        if(Objects.nonNull(tOrder.getBoardingTime())){
            model.addAttribute("boardingTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tOrder.getBoardingTime()));//乘车时间
        }else {
            model.addAttribute("boardingTime","");//乘车时间
        }
        if(Objects.nonNull(tOrder.getGetoffTime())){
            model.addAttribute("getoffTime",new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tOrder.getGetoffTime()));//乘车时间
        }else {
            model.addAttribute("getoffTime","");//乘车时间
        }
        model.addAttribute("startPrice",tOrder.getStartPrice());//起步价
        model.addAttribute("overDrivePrice",tOrder.getOverDrivePrice());//里程费
        model.addAttribute("waitTimePrice",tOrder.getWaitTimePrice());//等待费
        model.addAttribute("badWeatherPrice",tOrder.getBadWeatherPrice());//恶劣天气费用
        model.addAttribute("orderMoney",tOrder.getOrderMoney());//实际费用
        model.addAttribute("payMoney",tOrder.getPayMoney());//实际支付费用
        model.addAttribute("discountedPrice",tOrder.getDiscountedPrice());//优惠券抵扣费用
        model.addAttribute("payType",tOrder.getPayType());//支付方式
        model.addAttribute("discountAmount",tOrder.getDiscountAmount());//折扣金额
        model.addAttribute("userId",tOrder.getUserId());//用户id
        model.addAttribute("routeRecord",tOrder.getRouteRecord());//行程录音
 
        // 查询用户
        TAppUser tAppUser = tAppUserMapper.selectById(tOrder.getUserId());
        if(Objects.nonNull(tAppUser)){
            model.addAttribute("userName",tAppUser.getNickname());
            model.addAttribute("userPhone",tAppUser.getPhone());
            model.addAttribute("havDiscount",tAppUser.getHavDiscount());
            if(1 == tAppUser.getHavDiscount()){
                // 计算9折优惠金额
                BigDecimal multiply = tOrder.getOrderMoney().multiply(new BigDecimal("0.1")).setScale(2);
                model.addAttribute("multiply",multiply);
            }else {
                model.addAttribute("multiply",0);
            }
        }else {
            model.addAttribute("userName","");
            model.addAttribute("userPhone","");
            model.addAttribute("havDiscount","");
            model.addAttribute("multiply",0);
        }
 
        // 查询司机
        TDriver tDriver = tDriverMapper.selectById(tOrder.getDriverId());
        if(Objects.nonNull(tDriver)){
            model.addAttribute("driverName",tDriver.getName());
            model.addAttribute("driverPhone",tDriver.getPhone());
            // 查询司机所属分公司
            TBranchOffice tBranchOffice = tBranchOfficeMapper.selectById(tDriver.getBranchOfficeId());
            if(Objects.nonNull(tBranchOffice)){
                model.addAttribute("branchOfficeName",tBranchOffice.getPrincipal());
            }else {
                model.addAttribute("branchOfficeName","");
            }
        }else {
            model.addAttribute("branchOfficeName","");
            model.addAttribute("driverName","");
            model.addAttribute("driverPhone","");
        }
 
    }
 
    @Override
    public void orderExceptionDetail(Integer orderId, Model model) {
        this.orderDetail(orderId,model);
        model.addAttribute("orderId",orderId);
    }
 
    @Override
    public void getDataStatisticsByYear(Integer agentId,String yearDate, Model model,Map<String, Object> map) {
 
        List<DataStatisticsOrderYearResp> data = tOrderMapper.getDataStatisticsByYear(agentId,yearDate);
        List<DataStatisticsOrderYearResp> resps =new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            DataStatisticsOrderYearResp dataStatisticsOrderYearResp = new DataStatisticsOrderYearResp();
            if(i < 10){
                dataStatisticsOrderYearResp.setMonth("0" + i);
            }else {
                dataStatisticsOrderYearResp.setMonth(String.valueOf(i));
            }
            dataStatisticsOrderYearResp.setOrderCount(0);
            resps.add(dataStatisticsOrderYearResp);
        }
        if(!CollectionUtils.isEmpty(data)){
            for (DataStatisticsOrderYearResp datum : data) {
                for (DataStatisticsOrderYearResp resp : resps) {
                    if(resp.getMonth().equals(datum.getMonth())){
                        resp.setOrderCount(datum.getOrderCount());
                        break;
                    }
                }
            }
        }
        model.addAttribute("yearResp",resps);
        map.put("yearResp",resps);
    }
 
    @Override
    public void getDataStatisticsOrderCount(Integer agentId, String monthDate, Model model,Map<String, Object> map) {
        List<PerformanceTableResp> list = tOrderMapper.getDataStatisticsOrderCount(agentId,monthDate);
        model.addAttribute("performanceResp",list);
        map.put("performanceResp",list);
    }
 
    @Override
    public void getDataStatisticsCount(Integer agentId, Model model) {
        Integer pendingOrderCount = tOrderMapper.getDataStatisticsCount(agentId, OrderStateEnum.PENDING_ORDER.getCode(), LocalDate.now());
        Integer finishCount = tOrderMapper.getDataStatisticsCount(agentId, OrderStateEnum.FINISH.getCode(),LocalDate.now());
        Integer cancelCount = tOrderMapper.getDataStatisticsCount(agentId, OrderStateEnum.CANCELED.getCode(),LocalDate.now());
 
        Integer serverCount = tOrderMapper.getDataStatisticsServerCount(agentId, LocalDate.now());
 
        model.addAttribute("pendingOrderCount",pendingOrderCount);
        model.addAttribute("finishCount",finishCount);
        model.addAttribute("cancelCount",cancelCount);
        model.addAttribute("serverCount",serverCount);
    }
 
    @Override
    public List<TOrderServerResp> getDataStatisticsServerList(Integer agentId) {
        return tOrderMapper.getDataStatisticsServerList(agentId,LocalDate.now());
    }
 
    @Override
    public List<TOrder> getDataStatisticsAllList(Integer agentId,Integer type) {
        return tOrderMapper.getDataStatisticsAllList(agentId,type,LocalDate.now());
    }
 
    @Override
    public void getDataStatisticsCountByIds(List<Integer> ids, Model model) {
        Integer pendingOrderCount = tOrderMapper.getDataStatisticsCountByIds(ids, 1, LocalDate.now());
        Integer finishCount = tOrderMapper.getDataStatisticsCountByIds(ids, 2,LocalDate.now());
        Integer cancelCount = tOrderMapper.getDataStatisticsCountByIds(ids, 3,LocalDate.now());
        Integer serverCount = tOrderMapper.getDataStatisticsServerCountByIds(ids, LocalDate.now());
 
        model.addAttribute("pendingOrderCount",pendingOrderCount);
        model.addAttribute("finishCount",finishCount);
        model.addAttribute("cancelCount",cancelCount);
        model.addAttribute("serverCount",serverCount);
    }
 
    @Override
    public List<TOrderServerResp> getDataStatisticsServerListByIds(List<Integer> ids) {
        return tOrderMapper.getDataStatisticsServerListByIds(ids,LocalDate.now());
    }
 
    @Override
    public List<TOrder> getDataStatisticsAllListByIds(List<Integer> ids,Integer type) {
        return tOrderMapper.getDataStatisticsAllListByIds(ids,type,LocalDate.now());
    }
 
    @Override
    public void getDataStatisticsCountGetMap(Integer agentId, HashMap<String, Object> map) {
        Integer pendingOrderCount = tOrderMapper.getDataStatisticsCount(agentId, OrderStateEnum.PENDING_ORDER.getCode(), LocalDate.now());
        Integer finishCount = tOrderMapper.getDataStatisticsCount(agentId, OrderStateEnum.FINISH.getCode(),LocalDate.now());
        Integer cancelCount = tOrderMapper.getDataStatisticsCount(agentId, OrderStateEnum.CANCELED.getCode(),LocalDate.now());
 
        Integer serverCount = tOrderMapper.getDataStatisticsServerCount(agentId, LocalDate.now());
 
        map.put("pendingOrderCount",pendingOrderCount);
        map.put("finishCount",finishCount);
        map.put("cancelCount",cancelCount);
        map.put("serverCount",serverCount);
    }
 
    @Override
    public void getDataStatisticsCountByIdsGetMap(List<Integer> ids, HashMap<String, Object> map) {
        Integer pendingOrderCount = tOrderMapper.getDataStatisticsCountByIds(ids, 1, LocalDate.now());
        Integer finishCount = tOrderMapper.getDataStatisticsCountByIds(ids, 2,LocalDate.now());
        Integer cancelCount = tOrderMapper.getDataStatisticsCountByIds(ids, 3,LocalDate.now());
        Integer serverCount = tOrderMapper.getDataStatisticsServerCountByIds(ids, LocalDate.now());
 
        map.put("pendingOrderCount",pendingOrderCount);
        map.put("finishCount",finishCount);
        map.put("cancelCount",cancelCount);
        map.put("serverCount",serverCount);
    }
 
    @Override
    public void getDataStatisticsByYearByIds(List<Integer> ids, String yearDate, Model model, Map<String, Object> map) {
 
        List<DataStatisticsOrderYearResp> data = tOrderMapper.getDataStatisticsByYearByIds(ids,yearDate);
        List<DataStatisticsOrderYearResp> resps =new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            DataStatisticsOrderYearResp dataStatisticsOrderYearResp = new DataStatisticsOrderYearResp();
            if(i < 10){
                dataStatisticsOrderYearResp.setMonth("0" + i);
            }else {
                dataStatisticsOrderYearResp.setMonth(String.valueOf(i));
            }
            dataStatisticsOrderYearResp.setOrderCount(0);
            resps.add(dataStatisticsOrderYearResp);
        }
        if(!CollectionUtils.isEmpty(data)){
            for (DataStatisticsOrderYearResp datum : data) {
                for (DataStatisticsOrderYearResp resp : resps) {
                    if(resp.getMonth().equals(datum.getMonth())){
                        resp.setOrderCount(datum.getOrderCount());
                        break;
                    }
                }
            }
        }
        model.addAttribute("yearResp",resps);
        map.put("yearResp",resps);
    }
 
    @Override
    public void getDataStatisticsOrderCountByIds(List<Integer> ids, String monthDate, Model model, Map<String, Object> map) {
        List<PerformanceTableResp> list = tOrderMapper.getDataStatisticsOrderCountByIds(ids,monthDate);
        model.addAttribute("performanceResp",list);
        map.put("performanceResp",list);
    }
 
    @Override
    public void getStatisticsOrderByMonth(Integer agentId, String dayDate, Model model, Map<String, Object> map) {
        List<DataStatisticsOrderYearResp> data = tOrderMapper.getStatisticsOrderByMonth(agentId,dayDate);
        List<DataStatisticsOrderYearResp> resps =new ArrayList<>();
        int month = DateUtil.getDaysOfMonth(new Date());
        for (int i = 1; i <= month; i++) {
            DataStatisticsOrderYearResp dataStatisticsOrderYearResp = new DataStatisticsOrderYearResp();
            if(i < 10){
                dataStatisticsOrderYearResp.setMonth("0" + i);
            }else {
                dataStatisticsOrderYearResp.setMonth(String.valueOf(i));
            }
            dataStatisticsOrderYearResp.setOrderCount(0);
            resps.add(dataStatisticsOrderYearResp);
        }
        if(!CollectionUtils.isEmpty(data)){
            for (DataStatisticsOrderYearResp datum : data) {
                for (DataStatisticsOrderYearResp resp : resps) {
                    if(resp.getMonth().equals(datum.getMonth())){
                        resp.setOrderCount(datum.getOrderCount());
                        break;
                    }
                }
            }
        }
        model.addAttribute("monthResp",resps);
        map.put("monthResp",resps);
    }
 
    @Override
    public void getStatisticsOrderByMonthIds(List<Integer> ids, String dayDate, Model model, Map<String, Object> map) {
        List<DataStatisticsOrderYearResp> data = tOrderMapper.getStatisticsOrderByMonthIds(ids,dayDate);
        List<DataStatisticsOrderYearResp> resps =new ArrayList<>();
        int month = DateUtil.getDaysOfMonth(new Date());
        for (int i = 1; i <= month; i++) {
            DataStatisticsOrderYearResp dataStatisticsOrderYearResp = new DataStatisticsOrderYearResp();
            if(i < 10){
                dataStatisticsOrderYearResp.setMonth("0" + i);
            }else {
                dataStatisticsOrderYearResp.setMonth(String.valueOf(i));
            }
            dataStatisticsOrderYearResp.setOrderCount(0);
            resps.add(dataStatisticsOrderYearResp);
        }
        if(!CollectionUtils.isEmpty(data)){
            for (DataStatisticsOrderYearResp datum : data) {
                for (DataStatisticsOrderYearResp resp : resps) {
                    if(resp.getMonth().equals(datum.getMonth())){
                        resp.setOrderCount(datum.getOrderCount());
                        break;
                    }
                }
            }
        }
        model.addAttribute("monthResp",resps);
        map.put("monthResp",resps);
    }
 
 
    @Override
    public Integer getValidOrderCount(Integer driverId, BigDecimal orderMoney, String month) {
        // 查询司机当月有效订单数量
        return tOrderMapper.getValidOrderCount(driverId,orderMoney,month);
    }
}