package com.ruoyi.errand.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.AddCommunityDTO; import com.ruoyi.errand.object.dto.sys.CommunityPageListDTO; import com.ruoyi.errand.object.dto.sys.EditCommunityDTO; import com.ruoyi.errand.object.vo.app.CommunityListVO; import com.ruoyi.errand.object.vo.sys.AllCommunityListVO; import com.ruoyi.errand.object.vo.sys.CommunityPageListVO; import com.ruoyi.errand.object.vo.sys.CommunitySysDetailVO; import com.ruoyi.errand.object.vo.sys.CourierPageListVO; import com.ruoyi.errand.service.CommunityService; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.util.Collections; import java.util.List; import java.util.Objects; @Service public class CommunityServiceImpl extends ServiceImpl implements CommunityService { private final CommunityCourierMapper communityCourierMapper; private final OrderMapper orderMapper; private final RegionMapper regionMapper; private final CourierMapper courierMapper; private final PhoneMapper phoneMapper; public CommunityServiceImpl(CommunityCourierMapper communityCourierMapper, OrderMapper orderMapper, RegionMapper regionMapper, CourierMapper courierMapper, PhoneMapper phoneMapper) { this.communityCourierMapper = communityCourierMapper; this.orderMapper = orderMapper; this.regionMapper = regionMapper; this.courierMapper = courierMapper; this.phoneMapper = phoneMapper; } @Override public R> getCommunity(Integer regionId) { List communityListVOS = this.getBaseMapper().selectListByRegionId(regionId); return R.ok(communityListVOS); } @Override public R> getTotalCommunityList() { List communityListVOS = this.getBaseMapper().getTotalCommunityList(); return R.ok(communityListVOS); } @Override public List getAllCommunityList() { List list=communityCourierMapper.getAllCommunityList(); return this.baseMapper.getAllCommunityList(list); } @Override public IPage getCommunityPageList(CommunityPageListDTO dto) { IPage page = new Page<>(dto.getPageNum(),dto.getPageSize()); IPage iPage=this.baseMapper.getCommunityPageList(page,dto); iPage.getRecords().forEach(x->{ List orderList = orderMapper.selectList(new LambdaQueryWrapper() .eq(Order::getCommunityId, x.getId()) .eq(Order::getDelFlag, DelFlagConstant.UNDELETE) .eq(Order::getPayStatus,2)); if (orderList!=null&& !orderList.isEmpty()){ x.setTotal(orderList.size());//总订单数 LocalDateTime start = LocalDateTime.of(LocalDate.now(), LocalTime.MIN); LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX); long today = orderList.stream().filter(order -> order.getOrderTime().isAfter(start.minusNanos(1))) .filter(order -> order.getOrderTime().isBefore(end.plusNanos(1))).count(); x.setToday((int) today);//今日订单数 } }); return iPage; } @Override @Transactional public void add(AddCommunityDTO addCommunityDTO) { //城市地址是否存在 Region region = regionMapper.selectById(addCommunityDTO.getRegionId()); if (region==null){ throw new ServiceException("所在城市id错误"); } //跑腿人员是否存在 Courier courier = courierMapper.selectById(addCommunityDTO.getCourierId()); if (courier==null|| Objects.equals(courier.getDelFlag(), DelFlagConstant.DELETE)){ throw new ServiceException("跑腿人员不存在"); } //是否被绑定 Long count = communityCourierMapper.selectCount(new LambdaQueryWrapper() .eq(CommunityCourier::getCourierId, addCommunityDTO.getCourierId())); if (count>0){ throw new ServiceException("跑腿人员已绑定"); } //添加小区 Community community = new Community(); BeanUtils.copyProperties(addCommunityDTO,community); this.save(community); if (addCommunityDTO.getServicePhone()!=null){ //保存客服电话 Phone phone = new Phone(); phone.setType(2); phone.setCommunity_id(community.getId()); phone.setPhone(addCommunityDTO.getServicePhone()); phoneMapper.insert(phone); } //保存跑腿员关系 CommunityCourier communityCourier = new CommunityCourier(); communityCourier.setCommunityId(community.getId()); communityCourier.setCourierId(addCommunityDTO.getCourierId()); communityCourier.setCreateTime(LocalDateTime.now()); communityCourierMapper.insert(communityCourier); } @Override @Transactional public void edit(EditCommunityDTO editCommunityDTO) { //检查小区是否存在 Community community = this.baseMapper.selectById(editCommunityDTO.getId()); if (community==null|| Objects.equals(community.getDelFlag(), DelFlagConstant.DELETE)){ throw new ServiceException("该小区不存在"); } //检查所在城市是否更改 if (!Objects.equals(editCommunityDTO.getRegionId(), community.getRegionId())){ //检查城市是否存在 Region region = regionMapper.selectById(editCommunityDTO.getRegionId()); if (region==null){ throw new ServiceException("所在城市id错误"); } } //检查客服电话是否更改 if (editCommunityDTO.getServicePhone()!=null){ Phone phone = phoneMapper.selectOne(new LambdaQueryWrapper() .eq(Phone::getCommunity_id, editCommunityDTO.getId()) .eq(Phone::getType, 2)); if (phone==null){ //添加 //保存客服电话 phone = new Phone(); phone.setType(2); phone.setCommunity_id(community.getId()); phone.setPhone(editCommunityDTO.getServicePhone()); phoneMapper.insert(phone); }else { //修改 phone.setPhone(editCommunityDTO.getServicePhone()); phoneMapper.updateById(phone); } } //检查跑腿员是否存在 Courier courier = courierMapper.selectById(editCommunityDTO.getCourierId()); if (courier==null|| Objects.equals(courier.getDelFlag(), DelFlagConstant.DELETE)){ throw new ServiceException("跑腿人员不存在"); } //检查跑腿员是否绑定 Long count = communityCourierMapper.selectCount(new LambdaQueryWrapper() .eq(CommunityCourier::getCourierId, editCommunityDTO.getCourierId())); if (count>0){ throw new ServiceException("跑腿人员已绑定"); } //检查跑腿员是否更改 CommunityCourier communityCourier = communityCourierMapper.selectOne(new LambdaQueryWrapper() .eq(CommunityCourier::getCommunityId, editCommunityDTO.getId())); if (communityCourier==null){ //新增 communityCourier = new CommunityCourier(); communityCourier.setCommunityId(editCommunityDTO.getId()); communityCourier.setCourierId(editCommunityDTO.getCourierId()); }else { //修改 communityCourier.setCourierId(editCommunityDTO.getCourierId()); communityCourierMapper.updateById(communityCourier); } } @Override public void delete(Integer id) { Community community = this.baseMapper.selectById(id); if (community==null|| Objects.equals(community.getDelFlag(), DelFlagConstant.DELETE)){ throw new ServiceException("未找到该小区"); } //删除关系 CommunityCourier communityCourier = communityCourierMapper.selectOne(new LambdaQueryWrapper() .eq(CommunityCourier::getCommunityId, id)); if (communityCourier!=null){ communityCourierMapper.deleteById(communityCourier.getId()); } //删除小区 community.setDelFlag(DelFlagConstant.DELETE); community.setUpdateTime(LocalDateTime.now()); this.baseMapper.updateById(community); } @Override public void froze(Integer id) { //冻结/解冻 Community community = this.baseMapper.selectById(id); if (community==null|| Objects.equals(community.getDelFlag(), DelFlagConstant.DELETE)){ throw new ServiceException("未找到该小区"); } community.setStatus(community.getStatus()==0?1:0); community.setUpdateTime(LocalDateTime.now()); this.baseMapper.updateById(community); } @Override public CommunitySysDetailVO detail(Integer id) { Community community = this.getById(id); if (community==null|| Objects.equals(community.getDelFlag(), DelFlagConstant.DELETE)){ throw new ServiceException("小区不存在"); } CommunitySysDetailVO vo = new CommunitySysDetailVO(); BeanUtils.copyProperties(community,vo); // //所在城市Str 根据城市id查询整个字符 String regionStr =getAllStrById(community.getRegionId()); vo.setRegionStr(regionStr); //跑腿员id和名称 CommunityCourier communityCourier = communityCourierMapper.selectOne(new LambdaQueryWrapper().eq(CommunityCourier::getCommunityId, community.getId())); if (communityCourier!=null){ vo.setCourierId(communityCourier.getCourierId()); Courier courier = courierMapper.selectById(vo.getCourierId()); if (null!=courier && Objects.equals(courier.getDelFlag(), DelFlagConstant.UNDELETE)){ vo.setCourierName(courier.getName()); } } //客服电话 Phone phone = phoneMapper.selectOne(new LambdaQueryWrapper().eq(Phone::getCommunity_id, community.getId())); if (phone!=null){ vo.setServicePhone(phone.getPhone()); } return vo; } private String getAllStrById(Integer regionId) { if (regionId == null||regionId==0) { return ""; } Region region = regionMapper.selectById(regionId); if (region == null) { return ""; } // 获取父级区域的完整名称 String parentName = getAllStrById(region.getParentId()); // 拼接当前区域名称 if (parentName.isEmpty()) { return region.getName(); } else { return parentName + "-" + region.getName(); } } }