package com.mzl.flower.service.supplier;
|
|
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.register.RegisterDTO;
|
import com.mzl.flower.dto.request.supplier.QuerySupplierSubDTO;
|
import com.mzl.flower.dto.request.supplier.SupplierSubDTO;
|
import com.mzl.flower.dto.response.supplier.SupplierDTO;
|
import com.mzl.flower.dto.response.supplier.SupplierSubVO;
|
import com.mzl.flower.entity.supplier.SupplierSub;
|
import com.mzl.flower.entity.system.User;
|
import com.mzl.flower.mapper.supplier.SupplierSubMapper;
|
import com.mzl.flower.mapper.system.UserMapper;
|
import com.mzl.flower.service.BaseService;
|
import com.mzl.flower.utils.DateUtils;
|
import com.mzl.flower.utils.UUIDGenerator;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.StringUtils;
|
|
import java.util.ArrayList;
|
import java.util.List;
|
|
@Service
|
@Transactional
|
@RequiredArgsConstructor
|
public class SupplierSubService {
|
|
private final SupplierSubMapper supplierSubMapper;
|
private final PasswordEncoder passwordEncoder;
|
private final UserMapper userMapper;
|
private final BaseService baseService;
|
|
|
public void addOrUpdateSupplier(SupplierSubDTO dto) {
|
|
|
SupplierSub supplierSub;
|
if (dto.getId() == null) {
|
|
if (StringUtils.isEmpty(dto.getSupplierId())) {
|
throw new ValidationException("供应商主账号ID不能为空");
|
}
|
|
//检查该供应商下是否有名称一样的子账号
|
if (checkExistName(dto.getName(), dto.getSupplierId())) {
|
throw new ValidationException("子账号名称已存在");
|
}
|
|
//检查手机号
|
String userType = Constants.USER_TYPE.supplier.name();
|
if (checkUserExist(dto.getPhone(), userType)) {
|
throw new ValidationException("该手机号码已经注册");
|
}
|
|
supplierSub = new SupplierSub();
|
BeanUtils.copyProperties(dto, supplierSub, "id");
|
supplierSub.create(SecurityUtils.getUserId());
|
supplierSub.setIsEnabled(true);
|
|
//用户信息注册
|
RegisterDTO registerDTO = new RegisterDTO();
|
registerDTO.setTel(supplierSub.getPhone());
|
String uuid = UUIDGenerator.getUUID();
|
String password = passwordEncoder.encode(supplierSub.getPassword());
|
registerDTO.setPassword(password);
|
registerUser(registerDTO, uuid, password);
|
supplierSub.setUserId(uuid);
|
//保存子账号信息
|
supplierSubMapper.insert(supplierSub);
|
} else {
|
|
supplierSub = supplierSubMapper.selectById(dto.getId());
|
if (supplierSub == null) {
|
throw new ValidationException("供应商子信息未登记");
|
}
|
|
if (!StringUtils.isEmpty(dto.getName()) && !dto.getName().equals(supplierSub.getName())) {
|
if (checkExistName(dto.getName(), supplierSub.getSupplierId())) {
|
throw new ValidationException("子账号名称已存在");
|
}
|
supplierSub.setName(dto.getName());
|
}
|
|
if (!StringUtils.isEmpty(dto.getContact()) && !dto.getContact().equals(supplierSub.getContact())) {
|
supplierSub.setContact(dto.getContact());
|
}
|
|
if (StringUtils.isEmpty(supplierSub.getUserId())) {
|
throw new ValidationException("该账号用户信息未注册");
|
}
|
|
User user = userMapper.selectById(supplierSub.getUserId());
|
if (!StringUtils.isEmpty(dto.getPhone()) && !dto.getPhone().equals(supplierSub.getPhone())) {
|
String userType = Constants.USER_TYPE.supplier.name();
|
if (checkUserExist(dto.getPhone(), userType)) {
|
throw new ValidationException("该手机号码已经注册");
|
}
|
supplierSub.setPhone(dto.getPhone());
|
}
|
|
|
if (!StringUtils.isEmpty(dto.getPassword())) {
|
//判断密码是否一致
|
if (!ObjectUtils.isEmpty(user)) {
|
String encode = passwordEncoder.encode(dto.getPassword());
|
if (!encode.equals(user.getPassword())) {
|
user.setPassword(encode);
|
userMapper.updateById(user);
|
supplierSub.setPassword(encode);
|
}
|
}
|
}
|
if (!StringUtils.isEmpty(dto.getPhone())) {
|
if (!ObjectUtils.isEmpty(user)) {
|
user.setLoginName(dto.getPhone());
|
user.setTel(dto.getPhone());
|
user.setNickName(dto.getPhone());
|
userMapper.updateById(user);
|
}
|
}
|
supplierSub.setIsEnabled(supplierSub.getIsEnabled());
|
supplierSub.update(SecurityUtils.getUserId());
|
supplierSubMapper.updateById(supplierSub);
|
}
|
}
|
|
|
private boolean checkExistName(String name, Long supplierId) {
|
if (StringUtils.isEmpty(name)) {
|
LambdaQueryWrapper<SupplierSub> lambdaQueryWrapper = new LambdaQueryWrapper();
|
lambdaQueryWrapper.eq(SupplierSub::getName, name).eq(SupplierSub::getSupplierId, supplierId);
|
return supplierSubMapper.selectCount(lambdaQueryWrapper) > 0;
|
}
|
return false;
|
}
|
|
|
public Page<SupplierDTO> querySupplier(QuerySupplierSubDTO dto, Page page) {
|
List<SupplierSubVO> list = supplierSubMapper.querySupplierSub(dto, page);
|
page.setRecords(list);
|
return page;
|
}
|
|
|
public void isEnable(Long id) {
|
SupplierSub supplierSub = supplierSubMapper.selectById(id);
|
if (supplierSub == null) {
|
throw new ValidationException("供应商信息不存在");
|
}
|
if (supplierSub.getIsEnabled()) {
|
supplierSub.setIsEnabled(false);
|
//强制下线
|
baseService.removeToken(supplierSub.getUserId());
|
} else {
|
supplierSub.setIsEnabled(true);
|
}
|
supplierSub.update(SecurityUtils.getUserId());
|
supplierSubMapper.updateById(supplierSub);
|
}
|
|
|
public void registerUser(RegisterDTO dto, String uuid, String password) {
|
User user = new User();
|
user.setId(uuid);
|
user.setLoginName(dto.getTel());
|
user.setTel(dto.getTel());
|
user.setNickName(dto.getTel());
|
user.setPassword(password);
|
user.setType(Constants.USER_TYPE.supplier.name());
|
user.setStatus(Constants.STATUS_ACTIVE);
|
user.setIsSys(Constants.N);
|
user.create();
|
userMapper.insert(user);
|
}
|
|
//供应商手机号唯一,主子不能用同一个手机号
|
private boolean checkUserExist(String tel, String userType) {
|
List<String> userTypes = new ArrayList<>();
|
userTypes.add(userType);
|
User user = userMapper.getActiveUser(tel, userTypes);
|
return user != null;
|
}
|
|
public void delete(Long id) {
|
SupplierSub supplierSub = supplierSubMapper.selectById(id);
|
if (!ObjectUtils.isEmpty(supplierSub)) {
|
if (!StringUtils.isEmpty(supplierSub.getUserId())) {
|
//删除用户表信息
|
userMapper.deleteById(supplierSub.getUserId());
|
}
|
//删除供应商子表信息
|
supplierSubMapper.deleteById(id);
|
}
|
}
|
}
|