cloudroam
2025-03-28 4e5c47ee19caf98810b4ee50661fc141c4d7478f
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
package com.mzl.flower.service.supplier;
 
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.supplier.*;
import com.mzl.flower.dto.response.supplier.SupplierDTO;
import com.mzl.flower.entity.supplier.Supplier;
import com.mzl.flower.mapper.supplier.SupplierMapper;
import com.mzl.flower.service.BaseService;
import com.mzl.flower.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
 
@Service
@Transactional
public class SupplierService {
 
    private final static String SUPPLIER_STATUS_AUDIT = "U"; // 待审核
    private final static String SUPPLIER_STATUS_PASS = "P"; // 审核通过
    private final static String SUPPLIER_STATUS_REJECT = "R"; // 审核拒绝
 
    private final SupplierMapper supplierMapper;
 
    @Resource
    private BaseService baseService;
 
    public SupplierService(SupplierMapper supplierMapper) {
        this.supplierMapper = supplierMapper;
    }
 
    public void addOrUpdateSupplier(UpdateSupplierDTO dto) {
        Supplier supplier;
        if(dto.getId()==null){ //注册
            if(checkExist(dto.getUserId())){
                throw new ValidationException("供应商信息已登记");
            }
            if(checkExistName(dto.getName(),null)){
                throw new ValidationException("店铺名称已存在");
            }
            supplier = new Supplier();
            BeanUtils.copyProperties(dto, supplier,"id");
            supplier.setIdCards(JSON.toJSONString(dto.getIdCards()));
            supplier.setPictures(JSON.toJSONString(dto.getPictures()));
            supplier.create(SecurityUtils.getUserId());
            supplier.setShowed(true);
            supplier.setStatus(SUPPLIER_STATUS_AUDIT);
            supplier.setIsEnabled(true);
            supplierMapper.insert(supplier);
        }else{//重新修改
            supplier = supplierMapper.selectById(dto.getId());
            if(supplier==null){
                throw new ValidationException("供应商信息未登记");
            }
            if(checkExistName(dto.getName(),supplier.getId())){
                throw new ValidationException("店铺名称已存在");
            }
            BeanUtils.copyProperties(dto, supplier,"id");
            supplier.setIdCards(JSON.toJSONString(dto.getIdCards()));
            supplier.setPictures(JSON.toJSONString(dto.getPictures()));
            supplier.setStatus(SUPPLIER_STATUS_AUDIT);
            supplier.update(SecurityUtils.getUserId());
            supplierMapper.updateById(supplier);
        }
    }
 
    public void operationUpdate(UpdateSupplierOpDTO dto) {
        if(dto.getId()==null || dto.getId()==0){
            throw new ValidationException("id不能为空");
        }
        Supplier supplier = supplierMapper.selectById(dto.getId());
        if(supplier==null){
            throw new ValidationException("供应商信息未登记");
        }
        if(checkExistName(dto.getName(),supplier.getId())){
            throw new ValidationException("店铺名称已存在");
        }
        BeanUtils.copyProperties(dto, supplier,"id");
        supplier.update(SecurityUtils.getUserId());
        supplierMapper.updateById(supplier);
    }
 
    private boolean checkExistName(String name, Long id) {
        if(StringUtils.isNotBlank(name)){
            LambdaQueryWrapper<Supplier> lambdaQueryWrapper = new LambdaQueryWrapper();
            lambdaQueryWrapper.eq(Supplier::getName,name);
            if(id != null){
                lambdaQueryWrapper.ne(Supplier::getId,id);
            }
            return supplierMapper.selectCount(lambdaQueryWrapper)>0;
        }
        return false;
 
 
    }
 
    private boolean checkExist(String userId) {
       return supplierMapper.selectCount(new LambdaQueryWrapper<Supplier>().eq(Supplier::getUserId,userId))>0;
    }
 
    public Page<SupplierDTO> querySupplier(QuerySupplierDTO dto, Page page) {
        if(StringUtils.isNotBlank(dto.getCreateDateBeginStr())){
            dto.setCreateDateBegin(DateUtils.dateToLocalDateTime(dto.getCreateDateBeginStr(),true));
        }
        if(StringUtils.isNotBlank(dto.getCreateDateEndStr())){
            dto.setCreateDateEnd(DateUtils.dateToLocalDateTime(dto.getCreateDateEndStr(),false));
        }
        List<SupplierDTO> list = supplierMapper.querySupplier(dto,page);
        page.setRecords(list);
        return page;
    }
 
    public SupplierDTO findSupplierDetail(Long id) {
        SupplierDTO dto = supplierMapper.findSupplierDetail(id);
        return dto;
    }
 
    public SupplierDTO findSupplierByPhone(String phone) {
        SupplierDTO dto = supplierMapper.findSupplierByPhone(phone);
        dto.setOverTime(LocalDate.from(dto.getPassTime()));
        return dto;
    }
 
    public void auditSupplier(AuditSupplierDTO dto) {
        Supplier supplier = supplierMapper.selectById(dto.getId());
        if(supplier==null){
            throw new ValidationException("供应商信息不存在");
        }
        if(Constants.AUDIT_STATUS.Y.name().equals(dto.getResult())){
            supplier.setStatus(SUPPLIER_STATUS_PASS);
            supplier.setPassTime(LocalDateTime.now());
        }else if(Constants.AUDIT_STATUS.N.name().equals(dto.getResult())){
            supplier.setStatus(SUPPLIER_STATUS_REJECT);
            supplier.setRejectReason(dto.getRejectReason());
        }else{
            throw new ValidationException("审核结果不正确");
        }
        supplier.update(SecurityUtils.getUserId());
        supplierMapper.updateById(supplier);
    }
 
    public void changeStation(ChangeStationDTO dto) {
        Supplier supplier = supplierMapper.selectById(dto.getId());
        if(supplier==null){
            throw new ValidationException("供应商信息不存在");
        }
        supplier.setStationId(dto.getStationId());
        supplier.update(SecurityUtils.getUserId());
        supplierMapper.updateById(supplier);
    }
 
    public SupplierDTO getCurrentSupplier() {
        if(!Constants.USER_TYPE.supplier.name().equals(SecurityUtils.getUserType())){
            throw new ValidationException("当前登录用户不是供应商账号");
        }
        SupplierDTO dto = supplierMapper.getCurrentSupplier(SecurityUtils.getUserId());
        return dto;
    }
 
    public void updateSupplier(UpdateSupplierDTO dto) {
        if(dto.getId() == null || dto.getId() == 0){
            throw new ValidationException("id不能为空");
        }
        Supplier supplier = supplierMapper.selectById(dto.getId());
        if(supplier ==null) {
            throw new ValidationException("供应商信息不存在");
        }
        if(checkExistName(dto.getName(),supplier.getId())){
            throw new ValidationException("店铺名称已存在");
        }
        supplier.setName(dto.getName());
        supplier.setCover(dto.getCover());
        supplier.setDescription(dto.getDescription());
        supplier.update(SecurityUtils.getUserId());
        supplierMapper.updateById(supplier);
    }
 
 
    public void configShow(Long id) {
        Supplier supplier = supplierMapper.selectById(id);
        if(supplier ==null) {
            throw new ValidationException("供应商信息不存在");
        }
        if(supplier.getShowed()){
            supplier.setShowed(false);
        }else {
            supplier.setShowed(true);
        }
        supplier.update(SecurityUtils.getUserId());
        supplierMapper.updateById(supplier);
    }
 
    public void isEnable(Long id) {
        Supplier supplier = supplierMapper.selectById(id);
        if (supplier == null) {
            throw new ValidationException("供应商信息不存在");
        }
        if (supplier.getIsEnabled()) {
            supplier.setIsEnabled(false);
            //强制下线
            baseService.removeToken(supplier.getUserId());
        } else {
            supplier.setIsEnabled(true);
        }
        supplier.update(SecurityUtils.getUserId());
        supplierMapper.updateById(supplier);
    }
 
    public Supplier getSupplierById(Long supplierId) {
        return supplierMapper.selectById(supplierId);
    }
}