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,orderStatus,appuser.getCourierId());
|
}
|
|
@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.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();
|
}
|
|
|
}
|