MerchantServiceImpl.java
9.38 KB
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
package com.diligrp.rider.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.diligrp.rider.common.exception.BizException;
import com.diligrp.rider.dto.MerchantStoreDTO;
import com.diligrp.rider.entity.MerchantStore;
import com.diligrp.rider.entity.MerchantUsers;
import com.diligrp.rider.mapper.MerchantStoreMapper;
import com.diligrp.rider.mapper.MerchantUsersMapper;
import com.diligrp.rider.service.CityService;
import com.diligrp.rider.service.MerchantService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.List;
@Service
@RequiredArgsConstructor
public class MerchantServiceImpl implements MerchantService {
private final MerchantStoreMapper storeMapper;
private final MerchantUsersMapper usersMapper;
private final CityService cityService;
private static final int PAGE_SIZE = 20;
@Override
@Transactional
public Long addStore(MerchantStoreDTO dto) {
return addStore(dto, null);
}
@Override
@Transactional
public Long addStore(MerchantStoreDTO dto, Long scopedCityId) {
Long effectiveCityId = resolveCityId(dto.getCityId(), scopedCityId);
dto.setCityId(effectiveCityId);
ensureCityExists(effectiveCityId);
MerchantStore store = toEntity(dto);
store.setAddTime(System.currentTimeMillis() / 1000);
store.setIsDel(0);
storeMapper.insert(store);
if (dto.getAccountMobile() != null && !dto.getAccountMobile().isBlank()) {
Long exists = usersMapper.selectCount(new LambdaQueryWrapper<MerchantUsers>()
.eq(MerchantUsers::getMobile, dto.getAccountMobile()));
if (exists > 0) throw new BizException("该手机号已有账号");
MerchantUsers user = new MerchantUsers();
user.setStoreId(store.getId());
user.setMobile(dto.getAccountMobile());
user.setUserNickname(dto.getName());
user.setUserStatus(1);
user.setType(1);
user.setCreateTime(System.currentTimeMillis() / 1000);
usersMapper.insert(user);
}
return store.getId();
}
@Override
public void editStore(MerchantStoreDTO dto) {
editStore(dto, null);
}
@Override
public void editStore(MerchantStoreDTO dto, Long scopedCityId) {
MerchantStore existing = requireStore(dto.getId(), scopedCityId);
Long effectiveCityId = resolveCityId(dto.getCityId(), scopedCityId);
if (scopedCityId != null && !scopedCityId.equals(existing.getCityId())) {
throw new BizException("无权操作其他租户的店铺");
}
dto.setCityId(effectiveCityId);
ensureCityExists(effectiveCityId);
MerchantStore store = toEntity(dto);
store.setId(dto.getId());
storeMapper.updateById(store);
}
@Override
public List<MerchantStore> storeList(Long cityId, String keyword, int page) {
LambdaQueryWrapper<MerchantStore> wrapper = new LambdaQueryWrapper<MerchantStore>()
.eq(MerchantStore::getIsDel, 0)
.orderByAsc(MerchantStore::getListOrder)
.orderByDesc(MerchantStore::getId);
if (cityId != null && cityId > 0) wrapper.eq(MerchantStore::getCityId, cityId);
if (keyword != null && !keyword.isBlank()) wrapper.like(MerchantStore::getName, keyword);
int currentPage = Math.max(page, 1);
int offset = (currentPage - 1) * PAGE_SIZE;
wrapper.last("LIMIT " + offset + "," + PAGE_SIZE);
return storeMapper.selectList(wrapper);
}
@Override
public MerchantStore getStore(Long storeId) {
return getStore(storeId, null);
}
@Override
public MerchantStore getStore(Long storeId, Long scopedCityId) {
return requireStore(storeId, scopedCityId);
}
@Override
public void setOperatingState(Long storeId, int state) {
setOperatingState(storeId, state, null);
}
@Override
public void setOperatingState(Long storeId, int state, Long scopedCityId) {
MerchantStore store = requireStore(storeId, scopedCityId);
storeMapper.update(null, new LambdaUpdateWrapper<MerchantStore>()
.eq(MerchantStore::getId, store.getId())
.set(MerchantStore::getOperatingState, state));
}
@Override
public void setAutoOrder(Long storeId, int auto) {
MerchantStore store = requireStore(storeId, null);
storeMapper.update(null, new LambdaUpdateWrapper<MerchantStore>()
.eq(MerchantStore::getId, store.getId())
.set(MerchantStore::getAutomaticOrder, auto));
}
@Override
public void updateFeeConfig(Long storeId, BigDecimal freeShipping, BigDecimal upToSend) {
updateFeeConfig(storeId, freeShipping, upToSend, null);
}
@Override
public void updateFeeConfig(Long storeId, BigDecimal freeShipping, BigDecimal upToSend, Long scopedCityId) {
MerchantStore store = requireStore(storeId, scopedCityId);
storeMapper.update(null, new LambdaUpdateWrapper<MerchantStore>()
.eq(MerchantStore::getId, store.getId())
.set(MerchantStore::getFreeShipping, freeShipping)
.set(MerchantStore::getUpToSend, upToSend));
}
@Override
public void delStore(Long storeId) {
delStore(storeId, null);
}
@Override
public void delStore(Long storeId, Long scopedCityId) {
MerchantStore store = requireStore(storeId, scopedCityId);
storeMapper.update(null, new LambdaUpdateWrapper<MerchantStore>()
.eq(MerchantStore::getId, store.getId())
.set(MerchantStore::getIsDel, 1));
}
@Override
@Transactional
public MerchantStore syncStore(String appKey, MerchantStoreDTO dto) {
if (appKey == null || appKey.isBlank()) throw new BizException("AppKey不能为空");
if (dto.getOutStoreId() == null || dto.getOutStoreId().isBlank()) throw new BizException("外部门店编号不能为空");
ensureCityExists(dto.getCityId());
long now = System.currentTimeMillis() / 1000;
MerchantStore existing = getByOutStoreId(appKey, dto.getOutStoreId());
if (existing == null) {
MerchantStore store = toEntity(dto);
store.setAppKey(appKey);
store.setAddTime(now);
store.setIsDel(0);
storeMapper.insert(store);
return store;
} else {
MerchantStore store = toEntity(dto);
store.setId(existing.getId());
store.setAppKey(appKey);
storeMapper.updateById(store);
return storeMapper.selectById(existing.getId());
}
}
@Override
public MerchantStore getByOutStoreId(String appKey, String outStoreId) {
return storeMapper.selectOne(new LambdaQueryWrapper<MerchantStore>()
.eq(MerchantStore::getAppKey, appKey)
.eq(MerchantStore::getOutStoreId, outStoreId)
.eq(MerchantStore::getIsDel, 0)
.last("LIMIT 1"));
}
private void ensureCityExists(Long cityId) {
if (cityId == null || cityId < 1) {
throw new BizException("城市不能为空");
}
var city = cityService.getById(cityId);
if (city == null) {
throw new BizException("城市不存在");
}
}
private Long resolveCityId(Long dtoCityId, Long scopedCityId) {
if (scopedCityId != null) {
return scopedCityId;
}
if (dtoCityId == null || dtoCityId < 1) {
throw new BizException("城市不能为空");
}
return dtoCityId;
}
private MerchantStore requireStore(Long storeId, Long scopedCityId) {
MerchantStore store = storeMapper.selectOne(new LambdaQueryWrapper<MerchantStore>()
.eq(MerchantStore::getId, storeId)
.eq(MerchantStore::getIsDel, 0)
.last("LIMIT 1"));
if (store == null) {
throw new BizException("店铺不存在");
}
if (scopedCityId != null && !scopedCityId.equals(store.getCityId())) {
throw new BizException("无权操作其他租户的店铺");
}
return store;
}
private MerchantStore toEntity(MerchantStoreDTO dto) {
MerchantStore store = new MerchantStore();
store.setName(dto.getName());
store.setCityId(dto.getCityId());
store.setThumb(dto.getThumb());
store.setAddress(dto.getAddress());
store.setLng(dto.getLng());
store.setLat(dto.getLat());
store.setOperatingState(dto.getOperatingState() != null ? dto.getOperatingState() : 1);
store.setAutomaticOrder(dto.getAutomaticOrder() != null ? dto.getAutomaticOrder() : 0);
store.setShippingType(dto.getShippingType() != null ? dto.getShippingType() : 1);
store.setFreeShipping(dto.getFreeShipping() != null ? dto.getFreeShipping() : BigDecimal.ZERO);
store.setUpToSend(dto.getUpToSend() != null ? dto.getUpToSend() : BigDecimal.ZERO);
store.setOpenDate(dto.getOpenDate());
store.setOpenTime(dto.getOpenTime());
store.setAbout(dto.getAbout());
store.setOutStoreId(dto.getOutStoreId());
return store;
}
}