huliguo
2 天以前 5d7b65670282a4fad015e37d567cfa171b162052
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
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<CommunityMapper, Community> 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<List<CommunityListVO>> getCommunity(Integer regionId) {
        List<CommunityListVO> communityListVOS = this.getBaseMapper().selectListByRegionId(regionId);
        return R.ok(communityListVOS);
 
    }
 
    @Override
    public R<List<CommunityListVO>> getTotalCommunityList() {
        List<CommunityListVO> communityListVOS = this.getBaseMapper().getTotalCommunityList();
        return R.ok(communityListVOS);
    }
 
    @Override
    public List<AllCommunityListVO> getAllCommunityList() {
        List<Integer> list=communityCourierMapper.getAllCommunityList();
        return this.baseMapper.getAllCommunityList(list);
    }
 
    @Override
    public IPage<CommunityPageListVO> getCommunityPageList(CommunityPageListDTO dto) {
        IPage<CommunityPageListVO> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        IPage<CommunityPageListVO> iPage=this.baseMapper.getCommunityPageList(page,dto);
        iPage.getRecords().forEach(x->{
            List<Order> orderList = orderMapper.selectList(new LambdaQueryWrapper<Order>()
                    .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<CommunityCourier>()
                .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<Phone>()
                    .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<CommunityCourier>()
                .eq(CommunityCourier::getCourierId, editCommunityDTO.getCourierId()));
        if (count>0){
            throw new ServiceException("跑腿人员已绑定");
        }
        //检查跑腿员是否更改
        CommunityCourier communityCourier = communityCourierMapper.selectOne(new LambdaQueryWrapper<CommunityCourier>()
                .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<CommunityCourier>()
                .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<CommunityCourier>().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<Phone>().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();
        }
    }
}