初始化项目
This commit is contained in:
1 parent
45a1702948
commit
934b8aad40
155 files changed
+2649
-68
No files matched your search
@@ -0,0 +1,118 @@
|
||||
package com.bycrm.service.impl;
|
||||
|
||||
import com.bycrm.common.Constants;
|
||||
import com.bycrm.dto.DealerDTO;
|
||||
import com.bycrm.entity.Dealer;
|
||||
import com.bycrm.entity.User;
|
||||
import com.bycrm.exception.BusinessException;
|
||||
import com.bycrm.mapper.DealerMapper;
|
||||
import com.bycrm.mapper.UserMapper;
|
||||
import com.bycrm.service.DealerService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 经销商服务实现
|
||||
*/
|
||||
@Service
|
||||
public class DealerServiceImpl implements DealerService {
|
||||
|
||||
private final DealerMapper dealerMapper;
|
||||
private final UserMapper userMapper;
|
||||
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
|
||||
public DealerServiceImpl(DealerMapper dealerMapper, UserMapper userMapper) {
|
||||
this.dealerMapper = dealerMapper;
|
||||
this.userMapper = userMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Dealer> getDealerList(String name, String code, Integer status) {
|
||||
return dealerMapper.selectList(name, code, status);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dealer getDealerById(Long id) {
|
||||
Dealer dealer = dealerMapper.selectById(id);
|
||||
if (dealer == null) {
|
||||
throw new BusinessException("经销商不存在");
|
||||
}
|
||||
return dealer;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void createDealer(DealerDTO dealerDTO) {
|
||||
// 检查编码是否重复
|
||||
Dealer existingDealer = dealerMapper.selectByCode(dealerDTO.getCode());
|
||||
if (existingDealer != null) {
|
||||
throw new BusinessException("经销商账号已存在");
|
||||
}
|
||||
|
||||
Dealer dealer = new Dealer();
|
||||
BeanUtils.copyProperties(dealerDTO, dealer);
|
||||
dealer.setCreatedAt(LocalDateTime.now());
|
||||
dealer.setUpdatedAt(LocalDateTime.now());
|
||||
|
||||
dealerMapper.insert(dealer);
|
||||
|
||||
// 创建对应的用户账号
|
||||
User user = new User();
|
||||
user.setUsername(dealerDTO.getCode()); // 用户名使用经销商编码
|
||||
user.setRealName(dealerDTO.getContactPerson());
|
||||
user.setDealerId(dealer.getId());
|
||||
user.setRole(Constants.USER_ROLE_DEALER); // 经销商用户角色
|
||||
user.setStatus(dealerDTO.getStatus());
|
||||
|
||||
// 设置密码:如果提供了密码则使用提供的密码,否则使用默认密码
|
||||
String password = dealerDTO.getPassword();
|
||||
if (password == null || password.trim().isEmpty()) {
|
||||
password = "123456"; // 默认密码
|
||||
}
|
||||
user.setPassword(passwordEncoder.encode(password));
|
||||
user.setCreatedAt(LocalDateTime.now());
|
||||
user.setUpdatedAt(LocalDateTime.now());
|
||||
|
||||
userMapper.insert(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void updateDealer(Long id, DealerDTO dealerDTO) {
|
||||
Dealer existingDealer = dealerMapper.selectById(id);
|
||||
if (existingDealer == null) {
|
||||
throw new BusinessException("经销商不存在");
|
||||
}
|
||||
|
||||
// 如果修改了编码,需要检查新编码是否与其他经销商重复
|
||||
if (!existingDealer.getCode().equals(dealerDTO.getCode())) {
|
||||
Dealer codeDealer = dealerMapper.selectByCode(dealerDTO.getCode());
|
||||
if (codeDealer != null && !codeDealer.getId().equals(id)) {
|
||||
throw new BusinessException("经销商账号已存在");
|
||||
}
|
||||
}
|
||||
|
||||
Dealer dealer = new Dealer();
|
||||
BeanUtils.copyProperties(dealerDTO, dealer);
|
||||
dealer.setId(id);
|
||||
dealer.setUpdatedAt(LocalDateTime.now());
|
||||
|
||||
dealerMapper.update(dealer);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteDealer(Long id) {
|
||||
Dealer dealer = dealerMapper.selectById(id);
|
||||
if (dealer == null) {
|
||||
throw new BusinessException("经销商不存在");
|
||||
}
|
||||
|
||||
dealerMapper.deleteById(id);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user