UserServiceImpl.java 19.1 KB
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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
package com.huaheng.pc.system.user.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.company.domain.Company;
import com.huaheng.pc.config.company.mapper.CompanyMapper;
import com.huaheng.pc.config.company.service.CompanyService;
import com.huaheng.pc.config.warehouse.domain.Warehouse;
import com.huaheng.pc.config.warehouse.service.WarehouseService;
import com.huaheng.pc.system.user.domain.*;
import com.huaheng.pc.system.user.mapper.SysUserWarehouseMapper;
import com.huaheng.pc.system.user.mapper.UserCompanyMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huaheng.common.constant.UserConstants;
import com.huaheng.common.support.Convert;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.shiro.service.PasswordService;
import com.huaheng.pc.system.role.domain.Role;
import com.huaheng.pc.system.role.mapper.RoleMapper;
import com.huaheng.pc.system.user.mapper.UserMapper;
import com.huaheng.pc.system.user.mapper.UserRoleMapper;

import javax.annotation.Resource;

/**
 * 用户 业务层处理
 * 
 * @author huaheng
 */
@Service("UserServiceImpl")
public class UserServiceImpl implements IUserService
{
    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private CompanyMapper companyMapper;

    @Resource
    private UserCompanyMapper userCompanyMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Autowired
    private PasswordService passwordService;

    @Autowired
    private CompanyService companyService;

    @Resource
    private WarehouseService warehouseService;

    @Resource
    private SysUserWarehouseMapper sysUserWarehouseMapper;

    /**
     *  登陆验证
     * @param username  用户名
     * @param password  密  码
//     * @param warehouseId  仓库id
     * @param warehouseCode  仓库编码
     * @return
     */
    @Override
    public AjaxResult login(String username, String password, String warehouseCode, Boolean rememberMe)
    {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
        {
            return AjaxResult.error("用户名和密码不能为空");
        }
        if (StringUtils.isEmpty(warehouseCode))
        {
            return AjaxResult.error("请选择仓库");
        }
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
        Subject subject = SecurityUtils.getSubject();
        try
        {
            subject.login(token);
        }
        catch (AuthenticationException e)
        {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage()))
            {
                msg = e.getMessage();
            }
            return AjaxResult.error(msg);
        }
        User user = ShiroUtils.getUser();
        user.setWarehouseCode(warehouseCode);
        ShiroUtils.setUser(user);
        List<Company> Companys = companyService.selectCompanyByCurrentUserId();
        user.setCompanyIdList(Companys.stream().map(X -> X.getId()).collect(Collectors.toList()));
        user.setCompanyCodeList(Companys.stream().map(X -> X.getCode()).collect(Collectors.toList()));
        user.getCompanyCodeList().add("");
        ShiroUtils.setUser(user);
        return AjaxResult.success("登陆成功");
    }


    @Override
    public AjaxResult adminLogin(String username, String password,Boolean rememberMe)
    {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password))
        {
            return AjaxResult.error("用户名和密码不能为空");
        }
        UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
        Subject subject = SecurityUtils.getSubject();
        try
        {
            subject.login(token);
        }
        catch (AuthenticationException e)
        {
            String msg = "用户或密码错误";
            if (StringUtils.isNotEmpty(e.getMessage()))
            {
                msg = e.getMessage();
            }
            return AjaxResult.error(msg);
        }
        User user = ShiroUtils.getUser();
//        user.setWarehouseId(warehouseId);
//        user.setWarehouseCode(warehouseCode);
//        ShiroUtils.setUser(user);
//        List<Company> Companys = companyService.selectCompanyByCurrentUserId();
//        user.setCompanyIdList(Companys.stream().map(X -> X.getId()).collect(Collectors.toList()));
//        user.setCompanyCodeList(Companys.stream().map(X -> X.getCode()).collect(Collectors.toList()));
//        user.getCompanyCodeList().add("");
        ShiroUtils.setUser(user);
        return AjaxResult.success("登陆成功");
    }

    /**
     * 根据条件分页查询用户对象
     * 
     * @param user 用户信息
     * 
     * @return 用户信息集合信息
     */
    @Override
    public List<User> selectUserList(User user)
    {
        return userMapper.selectUserList(user);
    }

    /**
     * 通过用户名查询用户
     * 
     * @param userName 用户名
     * @return 用户对象信息
     */
    @Override
    public User selectUserByLoginName(String userName)
    {
        return userMapper.selectUserByLoginName(userName);
    }

    /**
     * 通过手机号码查询用户
     * 
     * @param phoneNumber 手机号
     * @return 用户对象信息
     */
    @Override
    public User selectUserByPhoneNumber(String phoneNumber)
    {
        return userMapper.selectUserByPhoneNumber(phoneNumber);
    }

    /**
     * 通过邮箱查询用户
     * 
     * @param email 邮箱
     * @return 用户对象信息
     */
    @Override
    public User selectUserByEmail(String email)
    {
        return userMapper.selectUserByEmail(email);
    }

    /**
     * 通过用户ID查询用户
     * 
     * @param userId 用户ID
     * @return 用户对象信息
     */
    @Override
    public User selectUserById(Integer userId)
    {
        return userMapper.selectUserById(userId);
    }

    /**
     * 通过用户ID删除用户
     * 
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int deleteUserById(Integer userId)
    {
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);
        // 删除用户与岗位表
        userCompanyMapper.deleteUserCompanyByUserId(userId);
        return userMapper.deleteUserById(userId);
    }

    /**
     * 批量删除用户信息
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteUserByIds(String ids) throws Exception
    {
        Integer[] userIds = Convert.toIntArray(ids);
        for (Integer userId : userIds)
        {
            if (User.isAdmin(userId))
            {
                throw new Exception("不允许删除超级管理员用户");
            }
        }
        return userMapper.deleteUserByIds(userIds);
    }

    /**
     * 新增保存用户信息
     * 
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int insertUser(User user)
    {
        user.randomSalt();
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
        user.setCreateBy(ShiroUtils.getLoginName());
        //新建用户名不能重复/
        User u = userMapper.selectUserByLoginName(user.getLoginName());
        int rows = 0;
        if(!StringUtils.isNull(u)){
            //新增用户时,如果已经有逻辑删除的则是修改
            if(u.getDeleted()){
                user.setId(u.getId());
                user.setDeleted(false);
                //修改用户
                rows = this.updateUser(user);
            }else{
                throw new ServiceException("用户名重复!");
            }
        }else{
            // 新增用户信息
            rows = userMapper.insertUser(user);
            // 新增用户货主关联
            this.insertUserCompany(user);
            // 新增用户与角色
            this.insertUserRole(user);
            // 新增用户与仓库
            this.insertUserWarehouse(user);
        }
        return rows;
    }

    /**
     * 修改保存用户信息
     * 
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int updateUser(User user)
    {
        Integer userId = user.getId();
        user.setUpdateBy(ShiroUtils.getLoginName());
        // 删除用户与角色关联
        userRoleMapper.deleteUserRoleByUserId(userId);
        // 新增用户与角色管理
        insertUserRole(user);
        // 删除用户与货主关联
        userCompanyMapper.deleteUserCompanyByUserId(userId);
        // 新增用户与货主管理
        insertUserCompany(user);
        // 删除用户与仓库关联
        sysUserWarehouseMapper.deleteUserWarehouseByUserId(userId);
        // 新增用户与仓库管理
        insertUserWarehouse(user);

        return userMapper.updateUser(user);
    }

    /**
     * 修改用户个人详细信息
     * 
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int updateUserInfo(User user)
    {
        return userMapper.updateUser(user);
    }

    /**
     * 修改用户密码
     * 
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int resetUserPwd(User user)
    {
        user.randomSalt();
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
        return updateUserInfo(user);
    }

    /**
     * 新增用户角色信息
     * 
     * @param user 用户对象
     */
    public void insertUserRole(User user)
    {
        // 新增用户与角色管理
        List<UserRole> list = new ArrayList<UserRole>();
        if (user.getRoleIds() != null) {
            for (Integer roleId : user.getRoleIds()) {
                UserRole ur = new UserRole();
                ur.setUserId(user.getId());
                ur.setRoleId(roleId);
                list.add(ur);
            }
            if (list.size() > 0) {
                userRoleMapper.batchUserRole(list);
            }
        }
    }


    /**
     * 新增用户仓库信息
     *
     * @param user 用户对象
     */
    public void insertUserWarehouse(User user)
    {
        // 新增用户与仓库管理
        List<SysUserWarehouse> list = new ArrayList<SysUserWarehouse>();
        if (user.getWarehouseCodeList() != null) {
            for (String warehouseCode : user.getWarehouseCodeList()) {
                SysUserWarehouse warehouse = new SysUserWarehouse();
                warehouse.setUserId(user.getId());
                warehouse.setWarehouseCode(warehouseCode);
                list.add(warehouse);
            }
            if (list.size() > 0) {
                sysUserWarehouseMapper.batchUserWarehouse(list);
            }
        }
    }

    /**
     * 新增用户货主信息
     * 
     * @param user 用户对象
     */
    public void insertUserCompany(User user)
    {
        // 新增用户与货主管理
        List<UserCompany> list = new ArrayList<UserCompany>();
        if (user.getCompanyIdList() != null)        {
            for (Integer companyId : user.getCompanyIdList())       {
                UserCompany up = new UserCompany();
                up.setUserId(user.getId());
                up.setCompanyId(companyId);
                list.add(up);
            }
            if (list.size() > 0)      {
                userCompanyMapper.batchUserCompany(list);
            }
        }
    }

    /**
     * 校验用户名称是否唯一
     * 
     * @param loginName 用户名
     * @return
     */
    @Override
    public String checkLoginNameUnique(String loginName)
    {
        int count = userMapper.checkLoginNameUnique(loginName);
        if (count > 0)
        {
            return UserConstants.USER_NAME_NOT_UNIQUE;
        }
        return UserConstants.USER_NAME_UNIQUE;
    }

    /**
     * 校验用户名称是否唯一
     *
     * @param user 用户名
     * @return
     */
    @Override
    public String checkPhoneUnique(User user)
    {
        Integer userId = StringUtils.isNull(user.getId()) ? -1 : user.getId();
        User info = userMapper.checkPhoneUnique(user.getPhoneNumber());
        if (StringUtils.isNotNull(info) && !info.getId().equals(userId))
        {
            return UserConstants.USER_PHONE_NOT_UNIQUE;
        }
        return UserConstants.USER_PHONE_UNIQUE;
    }

    /**
     * 校验email是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public String checkEmailUnique(User user)
    {
        Integer userId = StringUtils.isNull(user.getId()) ? -1 : user.getId();
        User info = userMapper.checkEmailUnique(user.getEmail());
        if (StringUtils.isNotNull(info) && !info.getId().equals(userId))
        {
            return UserConstants.USER_EMAIL_NOT_UNIQUE;
        }
        return UserConstants.USER_EMAIL_UNIQUE;
    }

    /**
     * 查询用户所属角色组
     * 
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public String selectUserRoleGroup(Integer userId)
    {
        List<Role> list = roleMapper.selectRolesByUserId(userId, ShiroUtils.getWarehouseCode());
        StringBuffer idsStr = new StringBuffer();
        for (Role role : list)
        {
            idsStr.append(role.getRoleName()).append(",");
        }
        if (StringUtils.isNotEmpty(idsStr.toString()))
        {
            return idsStr.substring(0, idsStr.length() - 1);
        }
        return idsStr.toString();
    }

    /**
     * 查询用户所属货主组
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public String selectUserCompanyGroup(Integer userId)
    {
        List<Company> list = companyMapper.selectCompanyByUserId(userId, ShiroUtils.getWarehouseCode());
        StringBuffer idsStr = new StringBuffer();
        for (Company company : list)
        {
            idsStr.append(company.getName()).append(",");
        }
        if (StringUtils.isNotEmpty(idsStr.toString()))
        {
            return idsStr.substring(0, idsStr.length() - 1);
        }
        return idsStr.toString();
    }

    @Override
    public List<Map<String, Object>> getWarehouseByUserId(Integer userId){
        return  userMapper.getWarehouseByUserId(userId);
    }

    @Override
    public List<Map<String, Object>> getWarehouseByUserCode(String loginName){
        List<Map<String, Object>> list = null;
        if (User.isAdmin(loginName)) {
            QueryWrapper<Warehouse> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("id","code","name");
            list = warehouseService.listMaps(queryWrapper);
        }
        else
        {
            list =  userMapper.getWarehouseByUserName(loginName);
            if (list.size()==1) {
                list=userMapper.getWarehouseByUserCode(loginName);
            }


        }
        return  list;
    }

    @Override
    public List<Map<String, Object>> getModules(Integer warehouseId, String warehouseCode, ClientType clientType)
    {
//        List<String> permList= new ArrayList();
//        List<String> moduleColumnList = new ArrayList();
//        moduleColumnList.add("id");
//        moduleColumnList.add("type");
//        moduleColumnList.add("parentId");
//        moduleColumnList.add("url");
//        moduleColumnList.add("moduleName");
//        moduleColumnList.add("modulePhto");
//        SysModules moduleCondition = new SysModules();
//        if (clientType == ClientType.PC)
//            moduleCondition.setIsMobile(false);
//        else if (clientType == ClientType.Mobile)
//            moduleCondition.setIsMobile(true);

        List<Map<String, Object>> returnList = new ArrayList();
//        List<Map<String, Object>> moduleList = sysModulesService.selectListMapByEqual(moduleColumnList, moduleCondition);
//
//        //从session取出用户信息
//        UserInfo userInfo = (UserInfo) SecurityUtils.getSubject().getPrincipal();
//        if (userInfo.getCode().equals("superAdmin"))
//        {
//            List<String> columnList = new ArrayList<>();
//            columnList.add("id");
//            SysPerm condition = new SysPerm();
//            condition.setEnable(true);
//            condition.setDeleted(false);
//            List<Map<String, Object>>  permListMap = sysPermService.selectListMapByEqual(columnList, condition);
//            for (Map<String, Object> map : permListMap) {
//                permList.add(map.get("id").toString());
//            }
//            returnList = moduleList;
//        }
//        else {
//            //不是超级管理员的话,需要验证对应的仓库权限
//            List<Map<String, Object>> permListMap = userInfoMapper.getPermByUserId(userInfo.getId(), warehouseId);
//            for (Map<String, Object> permMap : permListMap) {
//                permList.add(permMap.get("id").toString());
//            }
//            // 如果模块类型为2,且id等于权限的模块id,那么就添加到返回的模块列表
//            for (Map<String, Object> moduleMap : moduleList)   {
//                if(moduleMap.get("type").equals("2")) {
//                    for (Map<String, Object> permMap : permListMap)   {
//                        if (moduleMap.get("id").equals(permMap.get("moduleId")))   {
//                            returnList.add(moduleMap);
//                            break;
//                        }
//                    }
//                }
//            }
//            // 将模块列表和返回模块列表进行对比,把父模块也放进去
//            for (Map<String, Object> moduleMap : moduleList)
//                for (Map<String, Object> returnMap : returnList)
//                    if (moduleMap.get("id").equals(returnMap.get("parentId"))) {
//                        returnList.add(moduleMap);
//                        break;
//                    }
//        }
//        userInfo.setWarehouseId(warehouseId);
//        userInfo.setWarehouseCode(warehouseCode);
//        userInfo.setPerms(new HashSet(permList));

        return  returnList;
    }

    @Override
    public int insertupdateTime(Date date, String cPersonCode) {
        return userMapper.insertupdateTime(date,cPersonCode);
    }

    @Override
    public User selectmen(String loginName) {
        User user=userMapper.selectmen(loginName);
        return user;
    }

    @Override
    public int batchUserWarehouse(List<SysUserWarehouse> userWarehouseList) {
        return sysUserWarehouseMapper.batchUserWarehouse(userWarehouseList);
    }

    @Override
    public boolean checkWarehouseCodeAndUserName(String warehouseCode, String username) {
        return !userMapper.checkWarehouseCodeAndUserName(warehouseCode, username).isEmpty();
    }

}