MerchantServiceImpl.java
6.72 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
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) {
var city = cityService.getById(dto.getCityId());
if (city == null) throw new BizException("城市不存在");
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) {
MerchantStore existing = storeMapper.selectById(dto.getId());
if (existing == null) throw new BizException("店铺不存在");
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 offset = (page - 1) * PAGE_SIZE;
wrapper.last("LIMIT " + offset + "," + PAGE_SIZE);
return storeMapper.selectList(wrapper);
}
@Override
public MerchantStore getStore(Long storeId) {
return storeMapper.selectById(storeId);
}
@Override
public void setOperatingState(Long storeId, int state) {
storeMapper.update(null, new LambdaUpdateWrapper<MerchantStore>()
.eq(MerchantStore::getId, storeId)
.set(MerchantStore::getOperatingState, state));
}
@Override
public void setAutoOrder(Long storeId, int auto) {
storeMapper.update(null, new LambdaUpdateWrapper<MerchantStore>()
.eq(MerchantStore::getId, storeId)
.set(MerchantStore::getAutomaticOrder, auto));
}
@Override
public void updateFeeConfig(Long storeId, BigDecimal freeShipping, BigDecimal upToSend) {
storeMapper.update(null, new LambdaUpdateWrapper<MerchantStore>()
.eq(MerchantStore::getId, storeId)
.set(MerchantStore::getFreeShipping, freeShipping)
.set(MerchantStore::getUpToSend, upToSend));
}
@Override
public void delStore(Long storeId) {
storeMapper.update(null, new LambdaUpdateWrapper<MerchantStore>()
.eq(MerchantStore::getId, storeId)
.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("外部门店编号不能为空");
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 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;
}
}