You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
6.6 KiB

package com.example.demo.serviceImpl;
import com.example.demo.domain.entity.Admin;
import com.example.demo.domain.entity.AdminRole;
import com.example.demo.domain.entity.Role;
import com.example.demo.domain.vo.AdminVo;
import com.example.demo.domain.vo.Password;
import com.example.demo.domain.vo.Permission;
import com.example.demo.domain.vo.Result;
import com.example.demo.mapper.AdminMapper;
import com.example.demo.mapper.PermissionMapper;
import com.example.demo.service.AdminService;
import com.example.demo.service.PermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import java.util.Collections;
import java.util.List;
/**
* @program: GOLD
* @ClassName PermissionServiceImpl
* @description:
* @author: huangqizhen
* @create: 2025−06-26 13:20
* @Version 1.0
**/
@Service
public class PermissionServiceImpl implements PermissionService {
@Autowired
private PermissionMapper permissionMapper;
@Autowired
private AdminMapper adminMapper;
@Autowired
private AdminService adminService;
@Override
public List<String> getposition(String token) {
return permissionMapper.getposition(token);
}
@Override
public List<String> getmarket(String token) {
return permissionMapper.getmarket(token);
}
@Override
public PageInfo<Permission> getpermission(Integer pageNum, Integer pageSize, Permission permission) {
PageHelper.startPage(pageNum, pageSize);
List<Permission> list = permissionMapper.getPermission(permission);
return new PageInfo<>(list);
}
@Override
@Transactional
public Integer addpermission(Admin admin) throws Exception {
if (!ObjectUtils.isEmpty(adminMapper.getAdmin(admin.getAccount()))) {
throw new Exception("账号已存在");
}
if (ObjectUtils.isEmpty(admin.getAccount())) {
throw new Exception("账号为空!");
}
if (ObjectUtils.isEmpty(admin.getMarket())) {
throw new Exception("地区为空!");
}
if (admin.getMarket().contains("总部") && admin.getMarket().size() > 1) {
throw new Exception("总部不可与其他地区共存");
}
if (ObjectUtils.isEmpty(admin.getPostiton())) {
throw new Exception("职位为空!");
}
if (ObjectUtils.isEmpty(admin.getRoleId())) {
throw new Exception("权限类别为空!");
}
if (ObjectUtils.isEmpty(admin.getAdminName())) {
throw new Exception("用户名为空!");
}
if (ObjectUtils.isEmpty(admin.getMachineId())) {
throw new Exception("机器编号为空!");
}
if (admin.getMachineId().contains(",")) {
throw new Exception("机器编号格式错误");
} else {
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
admin.setPassword(passwordEncoder.encode(("123456")));
// 将地区列表转换为逗号分隔的字符串
String markets = String.join(",", admin.getMarket());
admin.setMarket(Collections.singletonList(markets));
permissionMapper.addPermission(admin);
AdminRole adminRole = new AdminRole();
adminRole.setAdminId(admin.getId());
adminRole.setRoleId(admin.getRoleId());
return permissionMapper.addadminRole(adminRole);
}
}
@Override
public List<Role> getRole(String token) {
return permissionMapper.getRole(token);
}
@Override
@Transactional
public Integer deleteAdmin(Integer id) {
if (id == null) {
return -1;
}
permissionMapper.deleteAdminRole(id);
return permissionMapper.deleteAdmin(id);
}
@Override
public Integer updateAdminRole(AdminRole adminRole) {
return permissionMapper.updateAdminRole(adminRole);
}
//修改管理员状态(启用 不启用)
@Override
public Integer upadatePermission(Admin admin) throws Exception {
return permissionMapper.updatePermission(admin);
}
//更新管理员信息
@Override
public Result updateAdmin(AdminVo adminVo) throws Exception {
// 校验账号是否为空(账号是唯一标识,必须存在)
if (ObjectUtils.isEmpty(adminVo.getAccount())) {
throw new Exception("账号为空!");
}
// 校验用户名是否为空(管理员姓名为必填项)
if (ObjectUtils.isEmpty(adminVo.getAdminName())) {
throw new Exception("用户名为空!");
}
// 校验地区是否为空(管理员所属地区为必填项)
if (ObjectUtils.isEmpty(adminVo.getMarket())) {
throw new Exception("地区为空!");
}
if (adminVo.getMarket().contains("总部") && adminVo.getMarket().size() > 1) {
throw new Exception("总部不可与其他地区共存");
}
// 校验职位是否为空(管理员职位为必填项)
if (ObjectUtils.isEmpty(adminVo.getPostiton())) {
throw new Exception("职位为空!");
}
// 校验机器编号格式(不允许包含逗号,避免与后续拼接逻辑冲突
if (adminVo.getMachineId().contains(",")) {
throw new Exception("机器编号格式错误");
} else {
Admin admin = new Admin();
admin.setAccount(adminVo.getAccount());
admin.setAdminName(adminVo.getAdminName());
admin.setMarket(adminVo.getMarket());
admin.setPostiton(adminVo.getPostiton());
// 将地区列表转换为逗号分隔的字符串
String markets = String.join(",", admin.getMarket());
admin.setMarket(Collections.singletonList(markets));
// 处理机器编号:若存在额外机器编号(machineIds),则与原编号拼接(用逗号分隔)
if (adminVo.getMachineIds() == null) {
admin.setMachineId(adminVo.getMachineId());
} else {
admin.setMachineId(adminVo.getMachineId() + "," + adminVo.getMachineIds());
}
admin.setRemark(adminVo.getRemark());
permissionMapper.updateAdmin(admin);
return Result.success("成功");
}
}
}