huliguo
昨天 d15bb55822001421572bfee603b3d503cd63e07d
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package com.ruoyi.errand.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.errand.constant.DelFlagConstant;
import com.ruoyi.errand.domain.*;
import com.ruoyi.errand.mapper.*;
import com.ruoyi.errand.object.dto.sys.AddCourierDTO;
import com.ruoyi.errand.object.dto.sys.CourierPageListDTO;
import com.ruoyi.errand.object.dto.sys.EditCourierDTO;
import com.ruoyi.errand.object.vo.app.CompleteOrderDTO;
import com.ruoyi.errand.object.vo.app.CourierInfoVO;
import com.ruoyi.errand.object.vo.app.CourierOrderListVO;
import com.ruoyi.errand.object.vo.app.CourierStatisticsVO;
import com.ruoyi.errand.object.vo.sys.AllCourierListVO;
import com.ruoyi.errand.object.vo.sys.AppUserPageListVO;
import com.ruoyi.errand.object.vo.sys.CourierPageListVO;
import com.ruoyi.errand.object.vo.sys.CourierSysDetailVO;
import com.ruoyi.errand.service.AppUserService;
import com.ruoyi.errand.service.CourierService;
import com.ruoyi.errand.utils.RedisService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
@Service
public class CourierServiceImpl extends ServiceImpl<CourierMapper, Courier> implements CourierService {
 
    @Resource
    private  EvaluationMapper evaluationMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CommunityCourierMapper communityCourierMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private AppUserMapper appUserMapper;
    @Autowired
    private CommunityMapper communityMapper;
 
 
    @Override
    public CourierInfoVO getCourierInfo() {
        AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return this.baseMapper.getCourierInfo(appuser.getCourierId());
    }
 
    @Override
    public CourierStatisticsVO getDatStatistics() {
        AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
 
        //本日 本周 本月
        CourierStatisticsVO vo =this.baseMapper.getDatStatistics(appuser.getCourierId());
        //统计五星好评的订单数量
        Integer fiveCount=evaluationMapper.getFiveCount(appuser.getCourierId());
        vo.setFive(fiveCount);
        return vo;
    }
 
    @Override
    public IPage<CourierOrderListVO> getCourierOrderList(Integer pageNum, Integer pageSize, Integer orderStatus) {
        AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        IPage<CourierOrderListVO> page=new Page<>(pageNum, pageSize);
        return this.baseMapper.getCourierOrderList(page,appuser.getCourierId(),orderStatus);
    }
 
    @Override
    public void receiveOrder(Integer id) {
        AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (appuser.getCourierId()==null){
            throw new ServiceException("您还不是跑腿员");
        }
        CommunityCourier communityCourier = communityCourierMapper.selectOne(new LambdaQueryWrapper<CommunityCourier>().eq(CommunityCourier::getCourierId, appuser.getCourierId()));
        if(communityCourier==null){
            throw new ServiceException("您未绑定小区");
        }
        //判断订单是否存在
        Order order = orderMapper.selectById(id);
        if (order==null || order.getDelFlag().equals(DelFlagConstant.DELETE)){
            throw new ServiceException("订单不存在");
        }
        if (order.getOrderStatus()!=1){
            throw new ServiceException("订单状态错误");
        }
        if (!order.getCommunityId().equals(communityCourier.getCommunityId())){
            throw new ServiceException("该订单不属于您绑定的小区");
        }
 
        //修改订单状态
        order.setOrderStatus(2);
        order.setCourierId(appuser.getCourierId());
        order.setReceivingTime(LocalDateTime.now());
        orderMapper.updateById(order);
 
    }
 
    @Override
    public void completeOrder(CompleteOrderDTO completeOrderDTO) {
        AppUser appuser = (AppUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (appuser.getCourierId()==null){
            throw new ServiceException("您还不是跑腿员");
        }
        CommunityCourier communityCourier = communityCourierMapper.selectOne(new LambdaUpdateWrapper<CommunityCourier>()
                .eq(CommunityCourier::getCourierId, appuser.getCourierId()));
        if(communityCourier==null){
            throw new ServiceException("您未绑定小区");
        }
        //判断订单是否存在
        Order order = orderMapper.selectById(completeOrderDTO.getId());
        if (order==null || order.getDelFlag().equals(DelFlagConstant.DELETE)){
            throw new ServiceException("订单不存在");
        }
        if (order.getOrderStatus()!=2){
            throw new ServiceException("订单状态错误");
        }
        if (!order.getCommunityId().equals(communityCourier.getCommunityId())){
            throw new ServiceException("该订单不属于您绑定的小区");
        }
 
        order.setOrderStatus(4);
        order.setCourierPics(completeOrderDTO.getCourierPics());
        order.setFinishTime(LocalDateTime.now());
        orderMapper.updateById(order);
        //评价
        if (completeOrderDTO.getRating()!=null){
            Evaluation evaluation = new Evaluation();
            evaluation.setOrderId(order.getId());
            evaluation.setType(1);
            evaluation.setRating(completeOrderDTO.getRating());
            if (null!=completeOrderDTO.getContent()){
                evaluation.setContent(completeOrderDTO.getContent());
            }
            evaluationMapper.insert(evaluation);
        }
    }
 
    @Override
    public IPage<CourierPageListVO> getCourierPageList(CourierPageListDTO dto) {
        IPage<CourierPageListVO> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        IPage<CourierPageListVO> iPage=this.baseMapper.getCourierPageList(page,dto);
 
        iPage.getRecords().forEach(x->{
            List<Order> orderList = orderMapper.selectList(new LambdaUpdateWrapper<Order>()
                    .eq(Order::getCourierId, x.getId())
                    .eq(Order::getDelFlag,DelFlagConstant.UNDELETE)
                    .eq(Order::getPayMethod,2));
            if (orderList!=null&& !orderList.isEmpty()){
                //当天接单量
                LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
                LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
                long todayNum = countByTimeRange(orderList, todayStart, todayEnd);
                x.setToday((int) todayNum);
                //本周接单量
                LocalDate today = LocalDate.now();
                LocalDate monday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
                LocalDateTime weekStart = LocalDateTime.of(monday, LocalTime.MIN);
                LocalDate sunday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
                LocalDateTime weekEnd = LocalDateTime.of(sunday, LocalTime.MAX);
                long week = countByTimeRange(orderList, weekStart, weekEnd);
                x.setWeek((int) week);
                //本月接单量
                LocalDate firstDay = today.withDayOfMonth(1);
                LocalDateTime mouthStart = LocalDateTime.of(firstDay, LocalTime.MIN);
                LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfMonth());
                LocalDateTime mouthEnd = LocalDateTime.of(lastDay, LocalTime.MAX);
                long mouth = countByTimeRange(orderList, mouthStart, mouthEnd);
                x.setMouth((int) mouth);
                //五星好评数
                List<Integer> orderIds = orderList.stream().map(Order::getId).collect(Collectors.toList());
                Long five = evaluationMapper.selectCount(new LambdaQueryWrapper<Evaluation>()
                        .in(Evaluation::getOrderId, orderIds)
                        .eq(Evaluation::getType, 0)
                        .eq(Evaluation::getRating, 5.0));
                x.setFive(five.intValue());
            }
 
        });
        return iPage;
    }
 
    @Override
    public CourierSysDetailVO detail(Integer id) {
        CourierSysDetailVO vo=this.baseMapper.detail(id);
        List<Order> orderList = orderMapper.selectList(new LambdaUpdateWrapper<Order>()
                .eq(Order::getCourierId, id)
                .eq(Order::getDelFlag,DelFlagConstant.UNDELETE)
                .eq(Order::getPayMethod,2));
        if (orderList!=null&& !orderList.isEmpty()){
            //当天接单量
            LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
            LocalDateTime todayEnd = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
            long todayNum = countByTimeRange(orderList, todayStart, todayEnd);
            vo.setToday((int) todayNum);
            //本周接单量
            LocalDate today = LocalDate.now();
            LocalDate monday = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
            LocalDateTime weekStart = LocalDateTime.of(monday, LocalTime.MIN);
            LocalDate sunday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
            LocalDateTime weekEnd = LocalDateTime.of(sunday, LocalTime.MAX);
            long week = countByTimeRange(orderList, weekStart, weekEnd);
            vo.setWeek((int) week);
            //本月接单量
            LocalDate firstDay = today.withDayOfMonth(1);
            LocalDateTime mouthStart = LocalDateTime.of(firstDay, LocalTime.MIN);
            LocalDate lastDay = today.with(TemporalAdjusters.lastDayOfMonth());
            LocalDateTime mouthEnd = LocalDateTime.of(lastDay, LocalTime.MAX);
            long mouth = countByTimeRange(orderList, mouthStart, mouthEnd);
            vo.setMouth((int) mouth);
            //五星好评
            List<Integer> orderIds = orderList.stream().map(Order::getId).collect(Collectors.toList());
            Long five = evaluationMapper.selectCount(new LambdaQueryWrapper<Evaluation>()
                    .in(Evaluation::getOrderId, orderIds)
                    .eq(Evaluation::getType, 0)
                    .eq(Evaluation::getRating, 5.0));
            vo.setFive(five.intValue());
            //总共接单量
            vo.setTotal(orderIds.size());
        }
 
 
        return vo;
    }
 
    @Override
    public void add(AddCourierDTO addCourierDTO) {
        AppUser appUser = appUserMapper.selectOne(new LambdaQueryWrapper<AppUser>().eq(AppUser::getPhone, addCourierDTO.getPhone()).
                eq(AppUser::getDelFlag, DelFlagConstant.UNDELETE));
        if (appUser == null) {
            throw new ServiceException("请先注册小程序");
        }
        //手机号是否存在
        Courier courier = this.getBaseMapper().selectOne(new LambdaQueryWrapper<Courier>().eq(Courier::getPhone, addCourierDTO.getPhone())
                .eq(Courier::getDelFlag, DelFlagConstant.UNDELETE));
        if (courier != null) {
            throw new ServiceException("该手机号的跑腿员已存在");
        }
        if (addCourierDTO.getCommunityId() != null) {
            //绑定的小区是否存在
            Community community = communityMapper.selectById(addCourierDTO.getCommunityId());
            if (community == null|| Objects.equals(community.getDelFlag(), DelFlagConstant.DELETE)) {
                throw new ServiceException("绑定的小区不存在");
            }
            //绑定的小区是否被别人绑定了
            CommunityCourier communityCourier = communityCourierMapper.selectOne(new LambdaQueryWrapper<CommunityCourier>()
                    .eq(CommunityCourier::getCommunityId, addCourierDTO.getCommunityId()));
            if (communityCourier != null) {
                throw new ServiceException("该小区已被绑定");
            }
 
        }
        //保存跑腿人员
        Courier courier1 = new Courier();
        BeanUtils.copyProperties(addCourierDTO, courier1);
        this.save(courier1);
        //绑定小区
        if (addCourierDTO.getCommunityId() != null) {
            CommunityCourier communityCourier = new CommunityCourier();
            communityCourier.setCommunityId(addCourierDTO.getCommunityId());
            communityCourier.setCourierId(courier1.getId());
            communityCourierMapper.insert(communityCourier);
        }
    }
 
    @Override
    public void edit(EditCourierDTO editCourierDTO) {
        Courier courier = this.getById(editCourierDTO.getId());
        //检查是否存在
        if (courier == null || courier.getDelFlag().equals(DelFlagConstant.DELETE)) {
            throw new ServiceException("该跑腿员不存在");
        }
        //是否更改了手机号
        if (!courier.getPhone().equals(editCourierDTO.getPhone())) {
            //更改了手机号
            AppUser appUser = appUserMapper.selectOne(new LambdaQueryWrapper<AppUser>().eq(AppUser::getPhone, editCourierDTO.getPhone()).
                    eq(AppUser::getDelFlag, DelFlagConstant.UNDELETE));
            if (appUser == null) {
                throw new ServiceException("请先注册小程序");
            }
            Courier courier1 = this.getBaseMapper().selectOne(new LambdaQueryWrapper<Courier>().eq(Courier::getPhone, editCourierDTO.getPhone())
                    .eq(Courier::getDelFlag, DelFlagConstant.UNDELETE));
            if (courier1 != null ) {
                throw new ServiceException("该手机号的跑腿员已存在");
            }
        }
        //是否更改了小区
        CommunityCourier communityCourier = communityCourierMapper.selectOne(new LambdaQueryWrapper<CommunityCourier>().eq(CommunityCourier::getCourierId, editCourierDTO.getId()));
        if (communityCourier != null) {
            //之前有绑定小区
            if (editCourierDTO.getCommunityId() == null) {
                //现在没有绑定,之前的要解绑
                communityCourierMapper.deleteById(communityCourier.getId());
 
            }else if (!editCourierDTO.getCommunityId().equals(communityCourier.getCommunityId())) {
                //现在绑定了,不和之前的一样
                //绑定的小区是否存在
                Community community = communityMapper.selectById(editCourierDTO.getCommunityId());
                if (community == null|| Objects.equals(community.getDelFlag(), DelFlagConstant.DELETE)) {
                    throw new ServiceException("绑定的小区不存在");
                }
                //绑定的小区是否被别人绑定了
                CommunityCourier communityCourier1 = communityCourierMapper.selectOne(new LambdaQueryWrapper<CommunityCourier>()
                        .eq(CommunityCourier::getCommunityId, editCourierDTO.getCommunityId()));
                if (communityCourier1 != null) {
                    throw new ServiceException("该小区已被绑定");
                }
                //修改
                communityCourier.setCommunityId(editCourierDTO.getCommunityId());
                communityCourier.setUpdateTime(LocalDateTime.now());
                communityCourierMapper.updateById(communityCourier);
            }
        }
        //修改
        BeanUtils.copyProperties(editCourierDTO, courier);
        this.updateById(courier);
 
    }
 
    @Override
    public void froze(Integer id) {
        Courier  courier= this.getById(id);
        if (courier == null||courier.getDelFlag().equals(DelFlagConstant.DELETE)) {
            throw new ServiceException("该跑腿员不存在");
        }
        courier.setStatus(courier.getStatus()==0?1:0);
        courier.setUpdateTime(LocalDateTime.now());
        this.updateById(courier);
    }
 
    @Override
    public void delete(Integer id) {
        Courier  courier= this.getById(id);
        if (courier == null||courier.getDelFlag().equals(DelFlagConstant.DELETE)) {
            throw new ServiceException("该跑腿员不存在");
        }
        //判断跑腿员是否绑定了小区
        CommunityCourier communityCourier = communityCourierMapper.selectOne(new LambdaQueryWrapper<CommunityCourier>().eq(CommunityCourier::getCourierId, id));
        if (communityCourier != null) {
            communityCourierMapper.deleteById(communityCourier.getId());//解绑
        }
        courier.setDelFlag(DelFlagConstant.DELETE);
        courier.setUpdateTime(LocalDateTime.now());
        this.updateById(courier);
 
    }
 
    @Override
    public List<AllCourierListVO> getAllCourierList() {
        List<Integer> couriers= communityCourierMapper.getAllCourierList();
        return this.baseMapper.getAllCourierList(couriers);
 
    }
 
    private static long countByTimeRange(List<Order> orderList, LocalDateTime start, LocalDateTime end) {
        return orderList.stream()
                .filter(order -> order.getOrderTime() != null) // 过滤空时间
                .filter(order -> order.getOrderTime().isAfter(start.minusNanos(1))) // 大于等于 start
                .filter(order -> order.getOrderTime().isBefore(end.plusNanos(1))) // 小于等于 end
                .count();
    }
 
 
}