baizonghao
2023-03-18 e69d41203f2171dce23231a09e17c3731b41dbea
ycl-common/src/main/java/com/ycl/service/user/impl/UmsAdminServiceImpl.java
@@ -8,7 +8,9 @@
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.bo.AdminUserDetails;
import com.ycl.dto.UmsAdminParam;
import com.ycl.dto.user.UmsUserDto1;
import com.ycl.dto.UpdateAdminPasswordParam;
import com.ycl.entity.depart.UmsDepart;
import com.ycl.entity.user.*;
import com.ycl.exception.ApiException;
import com.ycl.exception.Asserts;
@@ -42,9 +44,11 @@
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
/**
@@ -101,7 +105,7 @@
        BeanUtils.copyProperties(umsAdminParam, umsAdmin);
        umsAdmin.setCreateTime(new Date());
        umsAdmin.setStatus(1);
        umsAdmin.setMacAddress(MacUtils.getMac());
//        umsAdmin.setMacAddress(MacUtils.getMac());
        //查询是否有相同用户名的用户
        QueryWrapper<UmsAdmin> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UmsAdmin::getUsername, umsAdmin.getUsername());
@@ -117,6 +121,57 @@
        }
        if(umsAdmin.getUsername().equals(umsAdmin.getPassword())){
           Asserts.fail("密码不能和登录名完全一致");
        }
        umsAdmin.setPassword(encodePassword);
        baseMapper.insert(umsAdmin);
        //添加角色授权
        List<Long> ids = umsAdminParam.getRoleIds();
        if (!ids.isEmpty()) {
            List<UmsAdminRoleRelation> roleIds = new ArrayList<>();
            for (Long id : ids) {
                UmsAdminRoleRelation urr = new UmsAdminRoleRelation();
                urr.setAdminId(umsAdmin.getId());
                urr.setRoleId(id);
                roleIds.add(urr);
            }
            umsAdminRoleRelationService.saveBatch(roleIds);
        }
        //添加部门
        UmsDepartManage departManage = new UmsDepartManage();
        departManage.setUserId(umsAdmin.getId());
        departManage.setDepartId(umsAdminParam.getDepartmentId());
        departManage.setCreateTime(new Date());
        departManage.setUpdateTime(new Date());
        umsDepartManageMapper.insert(departManage);
        baseMapper.updateById(umsAdmin);
        return umsAdmin;
    }
    @Override
    public void importExcl(UmsAdminParam umsAdminParam) {
        UmsAdmin umsAdmin = new UmsAdmin();
        BeanUtils.copyProperties(umsAdminParam, umsAdmin);
        umsAdmin.setCreateTime(new Date());
        umsAdmin.setStatus(1);
        umsAdmin.setMacAddress(MacUtils.getMac());
        //查询是否有相同用户名的用户
        QueryWrapper<UmsAdmin> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UmsAdmin::getUsername, umsAdmin.getUsername());
        List<UmsAdmin> umsAdminList = list(wrapper);
        if (umsAdminList.size() > 0) {
            throw new RuntimeException("用户已存在");
        }
        //将密码进行加密操作
        String encodePassword = passwordEncoder.encode(umsAdmin.getPassword());
        if ( !umsAdmin.getPassword().matches("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,16}$")) {
            Asserts.fail("密码长度8到16位且密码中的字符必须包含字母(大写或者小写)和必须包含数字,不能包含空格");
        }
        if(umsAdmin.getUsername().equals(umsAdmin.getPassword())){
            Asserts.fail("密码不能和登录名完全一致");
        }
        umsAdmin.setPassword(encodePassword);
        baseMapper.insert(umsAdmin);
@@ -167,7 +222,6 @@
        umsDepartManageMapper.insert(departManage);
        baseMapper.updateById(umsAdmin);
        return umsAdmin;
    }
    @Override
@@ -384,6 +438,56 @@
    }
    @Override
    public List<UmsUserDto1> userExp() {
        List<UmsAdmin> umsAdmins = baseMapper.selectToExp();
        List<UmsUserDto1> res = umsAdmins.stream()
                .map(item -> {
                    UmsUserDto1 res1 = new UmsUserDto1();
                    res1.setId(item.getId());
                    res1.setUsername(item.getUsername());
                    res1.setNickName(item.getNickName());
                    res1.setMobile(item.getMobile());
                    StringBuffer roleStrBuffer = new StringBuffer();
                    List<UmsRole> roles = item.getRoles();
                    roles.forEach(new Consumer<UmsRole>() {
                        @Override
                        public void accept(UmsRole o) {
                            roleStrBuffer.append(o.getName());
                            roleStrBuffer.append(",");
                        }
                    });
                    roleStrBuffer.deleteCharAt(roleStrBuffer.length()-1);
                    res1.setRoles(roleStrBuffer.toString());
                    StringBuffer departStrBuffer = new StringBuffer();
                    List<UmsDepart> departs = item.getDepart();
                    departs.forEach(new Consumer<UmsDepart>() {
                        @Override
                        public void accept(UmsDepart o) {
                            departStrBuffer.append(o.getDepartName());
                            departStrBuffer.append(",");
                        }
                    });
                    departStrBuffer.deleteCharAt(departStrBuffer.length()-1);
                    res1.setDepart(departStrBuffer.toString());
                    res1.setJobTitle(item.getJobTitle());
                    Date createTime = item.getCreateTime();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    if (createTime != null){
                        res1.setCreateTime(sdf.format(createTime));
                    }
                    Integer status = item.getStatus();
                    if (status.equals(1)){
                        res1.setStatus("启用");
                    }else {
                        res1.setStatus("禁用");
                    }
                    return res1;
                }).collect(Collectors.toList());
        return res;
    }
    @Override
    public boolean deleteBatch(List<Long> ids) {
        boolean success = removeByIds(ids);
        getCacheService().delBatchAdmin(ids);
@@ -432,4 +536,6 @@
        }
        return str;
    }
}