EmployeeController.java 30 KB
Newer Older
liqin's avatar
liqin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
package cn.wisenergy.chnmuseum.party.web.controller;

import cn.wisenergy.chnmuseum.party.auth.SHA256PasswordEncryptionService;
import cn.wisenergy.chnmuseum.party.auth.SecureRandomSaltService;
import cn.wisenergy.chnmuseum.party.common.util.RandomUtil;
import cn.wisenergy.chnmuseum.party.core.annotations.OperationLog;
import cn.wisenergy.chnmuseum.party.model.BankBranchInfo;
import cn.wisenergy.chnmuseum.party.model.Employee;
import cn.wisenergy.chnmuseum.party.model.EmployeeRole;
import cn.wisenergy.chnmuseum.party.service.RoleService;
import cn.wisenergy.chnmuseum.party.service.impl.BankBranchInfoServiceImpl;
import cn.wisenergy.chnmuseum.party.service.impl.EmployeeRoleServiceImpl;
import cn.wisenergy.chnmuseum.party.service.impl.EmployeeServiceImpl;
import cn.wisenergy.chnmuseum.party.web.controller.base.BaseController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
wzp's avatar
wzp committed
20
import org.apache.shiro.authz.annotation.RequiresAuthentication;
liqin's avatar
liqin committed
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
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>
 * 组织成员 前端控制器
 * </p>
 *
 * @author 杨智平
 * @since 2018-08-02
 */
@Api(tags = "成员相关接口")
@RestController
@RequestMapping("/employee")
public class EmployeeController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(EmployeeController.class);

    @Resource
    private EmployeeServiceImpl employeeService;

    @Resource
    private EmployeeRoleServiceImpl employeeRoleService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    private static final String SHIRO_JWT_TOKEN = "shiro:jwt:token";
    //用户登录是否被锁定    一小时 redisKey 前缀
    private String SHIRO_IS_LOCK = "shiro_is_lock_";

    @Resource
    private BankBranchInfoServiceImpl bankBranchInfoService;

    @Resource
    private RoleService roleService;

    /**
     * 获取单个成员信息
     *
     * @param Id
     * @return
     */
    @ApiOperation(value = "获取单个成员信息")
    @GetMapping(value = "/get/")
wzp's avatar
wzp committed
76
    @RequiresAuthentication  //@RequiresPermissions("/employee/get/")
liqin's avatar
liqin committed
77 78 79
    public ResponseEntity<Employee> getById(String Id) {
        try {
            Employee employee = employeeService.selectByEmpId(Id);
liqin's avatar
liqin committed
80
            BankBranchInfo bankBranch = this.bankBranchInfoService.getById(employee.getBankBranchId());
liqin's avatar
liqin committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
            if (bankBranch != null) {
                employee.setBankBranchName(bankBranch.getName());
            }
            if (null == employee) {
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(employee);
        } catch (Exception e) {
            logger.error("查询成员信息错误!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 查询成员列表
     *
     * @param employName (用户名称,账号,手机号,角色名称有一个满足即可返回值)
     * @return
     */
    @ApiOperation(value = "查询成员列表")
    @RequestMapping(value = "/getUserList", method = RequestMethod.GET)
wzp's avatar
wzp committed
102
    @RequiresAuthentication  //@RequiresPermissions("/employee/getUserList")
liqin's avatar
liqin committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    public ResponseEntity<Page<Employee>> queryUserList(String employName) {
        try {
            employName = StringUtils.trimToNull(employName);
            Page<Employee> page = getPage();
            Page<Employee> employeePage = employeeService.selectRolenameList(page, employName);
            return ResponseEntity.ok(employeePage);
        } catch (Exception e) {
            logger.error("查询成员列表出错!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    //新增
    @OperationLog("新增成员")
    @ApiOperation(value = "新增成员")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
wzp's avatar
wzp committed
119
    @RequiresAuthentication  //@RequiresPermissions("/employee/add")
liqin's avatar
liqin committed
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
    public ResponseEntity<Map<String, Object>> add(Employee employee) {
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        try {
            if (StringUtils.isBlank(employee.getUsername())) {
                resultMap.put("status", 400);
                resultMap.put("message", "账号不能为空!");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            } else {
                employee.setUsername(StringUtils.trimToNull(employee.getUsername()));
            }
            if (StringUtils.isBlank(employee.getPassword())) {
                resultMap.put("status", 400);
                resultMap.put("message", "密码不能为空!");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            } else {
                employee.setPassword(StringUtils.trimToNull(employee.getPassword()));
            }
            if (StringUtils.isBlank(employee.getName())) {
                resultMap.put("status", 400);
                resultMap.put("message", "姓名不能为空!");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            } else {
                employee.setName(StringUtils.trimToNull(employee.getName()));
            }
            if (StringUtils.isBlank(employee.getCode())) {
                resultMap.put("status", 400);
                resultMap.put("message", "员工号不能为空!");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            } else {
                employee.setCode(StringUtils.trimToNull(employee.getCode()));
            }
            if (StringUtils.isBlank(employee.getRoleId())) {
                resultMap.put("status", 400);
                resultMap.put("message", "请选择角色!");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            }
            if (StringUtils.isBlank(employee.getBankBranchId())) {
                resultMap.put("status", 400);
                resultMap.put("message", "请选择归属网点!");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            }

liqin's avatar
liqin committed
162
            QueryWrapper<Employee> ew = new QueryWrapper<>();
liqin's avatar
liqin committed
163 164 165 166
            if (StringUtils.isNoneBlank(employee.getUsername())) {
                employee.setUsername(employee.getUsername().trim());
                ew.eq("is_deleted", 0);
                ew.eq("username", employee.getUsername());
liqin's avatar
liqin committed
167
                Employee one = this.employeeService.getOne(ew);
liqin's avatar
liqin committed
168 169 170 171 172 173 174 175 176 177 178
                if (one != null) {
                    resultMap.put("status", 400);
                    resultMap.put("message", "账号已存在!");
                    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
                }
            }
            /*if (StringUtils.isNoneBlank(employee.getCode())) {
                employee.setCode(employee.getCode().trim());
                ew = new QueryWrapper<>();
                ew.eq("is_deleted", 0);
                ew.eq("code", employee.getCode());
liqin's avatar
liqin committed
179
                Employee one = this.employeeService.getOne(ew);
liqin's avatar
liqin committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
                if (one != null) {
                    resultMap.put("status", 400);
                    resultMap.put("message", "该员工号已存在!");
                    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
                }
            }*/
            //判断是否存在已启用的大堂主管
            if ("4".equals(employee.getRoleId())) {
                Boolean hasEnHallDirector = this.employeeService.isHasEnHallDirector(null, employee.getBankBranchId());
                if (hasEnHallDirector) {
                    resultMap.put("status", 400);
                    resultMap.put("message", "该网点已存在启用的大堂主管!");
                    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
                }
            }

            boolean ret = false;
            if (employee.getSortorder() == null) {
                ew = new QueryWrapper<>();
liqin's avatar
liqin committed
199 200
                ew.select("max(sortorder) as sortorder");
                Employee e = this.employeeService.getOne(ew);
liqin's avatar
liqin committed
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
                if (e == null) {
                    employee.setSortorder(1);
                } else {
                    employee.setSortorder(e.getSortorder() + 1);
                }
            }
            byte[] passwordSalt = SecureRandomSaltService.generateSalt();
            byte[] passwordHash = SHA256PasswordEncryptionService
                    .createPasswordHash(employee.getPassword(), passwordSalt);
            employee.setPasswordSalt(passwordSalt);
            employee.setPasswordHash(passwordHash);
            employee.setCreateTime(new Date(System.currentTimeMillis()));
            employee.setUpdateTime(employee.getCreateTime());
            employee.setSex(employee.getSex());
            ret = this.employeeService.save(employee);

            EmployeeRole entity = new EmployeeRole();
            entity.setEmployeeId(employee.getId());
            entity.setRoleId(employee.getRoleId());
            entity.setCreateTime(new Date(System.currentTimeMillis()));
            entity.setUpdateTime(entity.getCreateTime());
            this.employeeRoleService.save(entity);

            if (!ret) {
                // 新增失败, 500
                resultMap.put("status", 500);
                resultMap.put("message", "服务器忙");
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                        .body(resultMap);
            }
liqin's avatar
liqin committed
231
            resultMap.put("status", 200);
liqin's avatar
liqin committed
232 233
            resultMap.put("message", "添加成功");
            // 201
liqin's avatar
liqin committed
234
            return ResponseEntity.status(HttpStatus.OK).body(resultMap);
liqin's avatar
liqin committed
235 236 237 238 239 240 241 242 243 244 245 246 247
        } catch (Exception e) {
            resultMap.put("status", 500);
            resultMap.put("message", "服务器忙");
            logger.error("新增成员错误!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultMap);
    }


    // 编辑用户信息
    @OperationLog("修改成员信息")
    @ApiOperation(value = "编辑用户信息(必须传 1username 2name 3roleId)")
    @PutMapping(value = "/modify")
wzp's avatar
wzp committed
248
    @RequiresAuthentication  //@RequiresPermissions("/employee/modify")
liqin's avatar
liqin committed
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
    public ResponseEntity<Map<String, Object>> edit(Employee employee) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            boolean ret = false;
            if (employee.getId() != null) {
                if ("1".equals(employee.getId()) && employee.getStatus() == false) {
                    resultMap.put("status", 400);
                    resultMap.put("message", "该账号不能被禁用");
                    return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
                }
                //判断是否存在已启用的大堂主管
                if ("4".equals(employee.getRoleId()) && employee.getStatus()) {
                    Boolean hasEnHallDirector = this.employeeService.isHasEnHallDirector(employee.getId(), employee.getBankBranchId());
                    if (hasEnHallDirector) {
                        resultMap.put("status", 400);
                        resultMap.put("message", "该网点已存在启用的大堂主管!");
                        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
                    }
                }

                employee.setUsername(StringUtils.trimToNull(employee.getUsername()));
                employee.setPassword(StringUtils.trimToNull(employee.getPassword()));
                employee.setName(StringUtils.trimToNull(employee.getName()));
                employee.setUpdateTime(new Date(System.currentTimeMillis()));
                ret = employeeService.updateById(employee);
                //查询当前用户拥有的角色
liqin's avatar
liqin committed
275
                QueryWrapper<EmployeeRole> employeeRoleWrapper = new QueryWrapper<>();
liqin's avatar
liqin committed
276
                employeeRoleWrapper.eq("employee_id", employee.getId());
liqin's avatar
liqin committed
277
                EmployeeRole employeeRole = this.employeeRoleService.getOne(employeeRoleWrapper);
liqin's avatar
liqin committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299

                if (employeeRole != null && employee.getRoleId() != null
                        && employee.getRoleId() != employeeRole.getRoleId()) {
                    // 角色有变化即修改角色
                    employeeRole.setRoleId(employee.getRoleId());
                    employeeRole.setUpdateTime(employee.getUpdateTime());
                    ret = this.employeeRoleService.updateById(employeeRole);
                }
            } else {
                // 更新失败, 400
                resultMap.put("status", 400);
                resultMap.put("message", "请选择用户");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            }

            if (!ret) {
                // 更新失败, 500
                resultMap.put("status", 500);
                resultMap.put("message", "服务器忙");
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultMap);
            }
            // 204
liqin's avatar
liqin committed
300
            resultMap.put("status", 200);
liqin's avatar
liqin committed
301
            resultMap.put("message", "更新成功");
liqin's avatar
liqin committed
302
            return ResponseEntity.status(HttpStatus.OK).body(resultMap);
liqin's avatar
liqin committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
        } catch (Exception e) {
            logger.error("更新错误!", e);
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 删除
     *
     * @param employeeId
     * @return
     */
    @OperationLog("删除成员")
    @ApiOperation(value = "删除成员")
    @DeleteMapping(value = "/delete")
wzp's avatar
wzp committed
319
    @RequiresAuthentication  //@RequiresPermissions("/employee/delete")
liqin's avatar
liqin committed
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    public ResponseEntity<Map<String, Object>> delete(String employeeId) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            if ("1".equals(employeeId)) {
                resultMap.put("status", 400);
                resultMap.put("message", "该账号不能被删除");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            }
            Employee entity = new Employee();
            entity.setId(employeeId);
            entity.setUpdateTime(new Date(System.currentTimeMillis()));
            entity.setIsDeleted(1);
            boolean ret1 = this.employeeService.updateById(entity);

            QueryWrapper<EmployeeRole> employeeRoleWrapper = new QueryWrapper<>();
            employeeRoleWrapper.eq("employee_id", employeeId);
            boolean ret2 = this.employeeRoleService.remove(employeeRoleWrapper);

            if (!ret1 || !ret2) {
                resultMap.put("status", 400);
                resultMap.put("message", "删除失败");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            }
liqin's avatar
liqin committed
343
            resultMap.put("status", 200);
liqin's avatar
liqin committed
344
            resultMap.put("message", "删除成功");
liqin's avatar
liqin committed
345
            return ResponseEntity.status(HttpStatus.OK).body(resultMap);
liqin's avatar
liqin committed
346 347 348 349 350 351 352 353 354 355
        } catch (Exception e) {
            logger.error("删除用户出错!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    // 批量删除
    @OperationLog("批量删除成员")
    @ApiOperation(value = "批量删除")
    @DeleteMapping(value = "/batchDel")
wzp's avatar
wzp committed
356
    @RequiresAuthentication  //@RequiresPermissions("/employee/batchDel")
liqin's avatar
liqin committed
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
    public ResponseEntity<Map<String, Object>> batchDel(String employeeIds) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            boolean flag = false;
            boolean batchDelflag = false;
            String empId = "";
            if (StringUtils.isNotBlank(employeeIds)) {
                String[] employeeIdArr = employeeIds.split(",");
                Employee entity = new Employee();
                entity.setIsDeleted(1);
                for (String employeeId : employeeIdArr) {
                    if ("1".equals(employeeId)) {
                        empId = "1";
                        continue;
                    }
                    entity.setId(employeeId);
                    flag = this.employeeService.updateById(entity);

                    QueryWrapper<EmployeeRole> employeeRoleWrapper = new QueryWrapper<>();
                    employeeRoleWrapper.eq("employee_id", employeeId);
                    flag = this.employeeRoleService.remove(employeeRoleWrapper);
                    if (!flag) {
                        batchDelflag = true;
                        break;
                    }
                }
            } else {
                resultMap.put("status", 400);
                resultMap.put("message", "删除失败");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            }
            if (batchDelflag) {
                resultMap.put("status", 400);
                resultMap.put("message", "删除失败");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            }
            if (!"".equals(empId) && !flag) {
                resultMap.put("status", 400);
                resultMap.put("message", "系统用户不能被删除");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            }

liqin's avatar
liqin committed
399
            resultMap.put("status", 200);
liqin's avatar
liqin committed
400 401 402 403 404
            String message = "删除成功";
            if (!"".equals(empId) && flag) {
                message = "admin不能被删除,其他选中用户删除成功";
            }
            resultMap.put("message", message);
liqin's avatar
liqin committed
405
            return ResponseEntity.status(HttpStatus.OK).body(resultMap);
liqin's avatar
liqin committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
        } catch (Exception e) {
            logger.error("删除失败!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 禁用
     *
     * @param employeeId
     * @return
     */
    @OperationLog("禁用成员")
    @ApiOperation(value = "禁用")
    @RequestMapping(value = "/disable", method = RequestMethod.PUT)
wzp's avatar
wzp committed
421
    @RequiresAuthentication  //@RequiresPermissions("/employee/disable")
liqin's avatar
liqin committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
    public ResponseEntity<Map<String, Object>> disableEmployee(String employeeId) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            Employee entity = new Employee();
            entity.setId(employeeId);
            entity.setStatus(false);
            boolean ret = this.employeeService.updateById(entity);

            //获取该用户的登陆token
            String userToken = stringRedisTemplate.opsForValue().get(SHIRO_JWT_TOKEN + employeeId);
            if (null != userToken) {
                stringRedisTemplate.delete(userToken);
                stringRedisTemplate.delete(SHIRO_JWT_TOKEN + employeeId);
            }
            if (!ret) {
                resultMap.put("status", 400);
                resultMap.put("message", "禁用失败");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            }
liqin's avatar
liqin committed
441
            resultMap.put("status", 200);
liqin's avatar
liqin committed
442
            resultMap.put("message", "禁用成功");
liqin's avatar
liqin committed
443
            return ResponseEntity.status(HttpStatus.OK).body(resultMap);
liqin's avatar
liqin committed
444 445 446 447 448 449 450 451 452 453
        } catch (Exception e) {
            logger.error("禁用用户出错!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    // 批量禁用
    @OperationLog("批量禁用成员")
    @ApiOperation(value = "批量禁用")
    @RequestMapping(value = "/batchDis", method = RequestMethod.PUT)
wzp's avatar
wzp committed
454
    @RequiresAuthentication  //@RequiresPermissions("/employee/batchDis")
liqin's avatar
liqin committed
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
    public ResponseEntity<Map<String, Object>> batchDis(String employeeIds) {
        Map<String, Object> resultMap = new HashMap<>();
        try {
            boolean flag = false;
            if (StringUtils.isNotBlank(employeeIds)) {
                String[] employeeIdArr = employeeIds.split(",");
                Employee entity = new Employee();
                entity.setStatus(false);
                for (String employeeId : employeeIdArr) {
                    if ("1".equals(employeeId)) {
                        continue;
                    }
                    entity.setId(employeeId);
                    flag = this.employeeService.updateById(entity);
                    //获取该用户的登陆token
                    String userToken = stringRedisTemplate.opsForValue().get(SHIRO_JWT_TOKEN + employeeId);
                    if (null != userToken) {
                        stringRedisTemplate.delete(userToken);
                        stringRedisTemplate.delete(SHIRO_JWT_TOKEN + employeeId);
                    }
                    if (!flag) {
                        break;
                    }
                }
            } else {
                resultMap.put("status", 400);
                resultMap.put("message", "禁用失败");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            }
            if (!flag) {
                resultMap.put("status", 400);
                resultMap.put("message", "禁用失败");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            }
liqin's avatar
liqin committed
489
            resultMap.put("status", 200);
liqin's avatar
liqin committed
490
            resultMap.put("message", "禁用成功");
liqin's avatar
liqin committed
491
            return ResponseEntity.status(HttpStatus.OK).body(resultMap);
liqin's avatar
liqin committed
492 493 494 495 496 497 498 499 500 501
        } catch (Exception e) {
            logger.error("用户禁用失败!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    // 启动
    @OperationLog("启用成员")
    @ApiOperation(value = "启用")
    @RequestMapping(value = "/enable", method = RequestMethod.PUT)
wzp's avatar
wzp committed
502
    @RequiresAuthentication  //@RequiresPermissions("/employee/enable")
liqin's avatar
liqin committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
    public ResponseEntity<Map<String, Object>> enableUser(String employeeId, String currBankId) {
        try {
            Map<String, Object> map = new HashMap<>();

            //判断该网点下是否有启用的大堂主管
            Boolean hasEnHallDirector = this.employeeService.isHasEnHallDirector(employeeId, currBankId);
            if (hasEnHallDirector) {
                map.put("status", "400");
                map.put("message", "该网点存在启用的大堂主管,如需启用,请先禁用其他大堂主管");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(map);
            }

            Employee entity = new Employee();
            entity.setId(employeeId);
            entity.setStatus(true);
            boolean ret = this.employeeService.updateById(entity);
            if (!ret) {
                map.put("status", "500");
                map.put("message", "服务器错误");
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(map);
            }
            map.put("status", "201");
            map.put("message", "启用成功");
liqin's avatar
liqin committed
526
            return ResponseEntity.status(HttpStatus.OK).body(map);
liqin's avatar
liqin committed
527 528 529 530 531 532 533 534 535 536 537
        } catch (Exception e) {
            logger.error("用户启用出错!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
                null);
    }

    // 批量启动
    @OperationLog("批量启用成员")
    @ApiOperation(value = "批量启动")
    @RequestMapping(value = "/batchEnable", method = RequestMethod.PUT)
wzp's avatar
wzp committed
538
    @RequiresAuthentication  //@RequiresPermissions("/employee/batchEnable")
liqin's avatar
liqin committed
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
    public ResponseEntity<Map<String, Object>> batchEnable(String employeeIds) {
        try {
            Map<String, Object> map = new HashMap<>();
            boolean flag = false;
            if (StringUtils.isNotBlank(employeeIds)) {
                String[] employeeIdArr = employeeIds.split(",");
                Employee entity = new Employee();
                entity.setStatus(true);
                for (String employeeId : employeeIdArr) {
                    entity.setId(employeeId);
                    flag = this.employeeService.updateById(entity);
                    if (!flag) {
                        break;
                    }
                }
            } else {
                map.put("status", "400");
                map.put("message", "请选择要启动的用户");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(map);
            }
            if (!flag) {
                map.put("status", "400");
                map.put("message", "批量启动失败");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(map);
            } else {
                map.put("status", "201");
                map.put("message", "批量启动成功");
liqin's avatar
liqin committed
566
                return ResponseEntity.status(HttpStatus.OK).body(map);
liqin's avatar
liqin committed
567 568 569 570 571 572 573 574 575 576
            }
        } catch (Exception e) {
            logger.error("批量启动失败!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @OperationLog("修改密码")
    @ApiOperation(value = "管理员更改自己的登录密码", notes = "管理员更改自己的登录密码")
    @RequestMapping(value = "/editPwd", method = RequestMethod.PUT)
wzp's avatar
wzp committed
577
    @RequiresAuthentication  //@RequiresPermissions("/employee/editPwd")
liqin's avatar
liqin committed
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 619
    public ResponseEntity<Map<String, Object>> editPwd(@RequestParam(value = "oldPassWord", required = true) String oldPassWord,
                                                       @RequestParam(value = "password", required = true) String password) {
        Map<String, Object> resultMap = new LinkedHashMap<String, Object>();
        try {
            boolean ret = false;
            Employee employee = this.employeeService.getById(this.getUserId());
            byte[] salt = employee.getPasswordSalt();
            if (new String(SHA256PasswordEncryptionService.createPasswordHash(oldPassWord, salt))
                    .equals(new String(employee.getPasswordHash()))) {
                salt = SecureRandomSaltService.generateSalt();
                employee.setPasswordSalt(salt);
                employee.setPasswordHash((SHA256PasswordEncryptionService.createPasswordHash(password, salt)));
                employee.setUpdateTime(new Date(System.currentTimeMillis()));
                ret = this.employeeService.updateById(employee);
            } else {
                logger.error("旧密码不正确");
                resultMap.put("status", 400);
                resultMap.put("message", "旧密码不正确");
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(resultMap);
            }
            if (!ret) {
                resultMap.put("status", 500);
                resultMap.put("message", "修改失败");
                // 更新失败, 500
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultMap);
            }
            resultMap.put("status", 200);
            resultMap.put("message", "修改成功");
            return ResponseEntity.status(HttpStatus.OK).body(resultMap);
        } catch (Exception e) {
            logger.error("更新密码错误!", e);
        }
        // 500
        resultMap.put("status", 500);
        resultMap.put("message", "修改失败");
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultMap);
    }

    // 管理员重置密码
    @OperationLog("重置密码")
    @ApiOperation(value = "管理员重置密码", notes = "管理员重置密码")
    @RequestMapping(value = "/resetPassword", method = RequestMethod.PUT)
wzp's avatar
wzp committed
620
    @RequiresAuthentication  //@RequiresPermissions("/employee/resetPassword")
liqin's avatar
liqin committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
    public ResponseEntity<Map<Object, String>> resetPassword(String employeeId) {
        try {
            Map<Object, String> map = new LinkedHashMap<>();
            Employee employee = new Employee();
            employee.setId(employeeId);
            String newPassword = RandomUtil.createRandom(6);
            employee.setPassword(newPassword);
            byte[] passwordSalt = SecureRandomSaltService.generateSalt();
            byte[] passwordHash = SHA256PasswordEncryptionService.createPasswordHash(employee.getPassword(), passwordSalt);
            employee.setPasswordSalt(passwordSalt);
            employee.setPasswordHash(passwordHash);
            boolean ret = this.employeeService.updateById(employee);
            if (!ret) {
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
            }
            stringRedisTemplate.delete(SHIRO_IS_LOCK + this.employeeService.selectByEmpId(employeeId).getUsername());
            map.put("status", "201");
            map.put("message", "重置密码成功");
            map.put("password", newPassword);
liqin's avatar
liqin committed
640
            return ResponseEntity.status(HttpStatus.OK).body(map);
liqin's avatar
liqin committed
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
        } catch (Exception e) {
            logger.error("重置密码出错!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 给支行管理员查询自己网点人员
     * 2019-01-24增加新的模块-yjl
     *
     * @param employName (用户名称,账号,手机号,角色名称有一个满足即可返回值)
     * @return
     */
    @ApiOperation(value = "给支行管理员查询自己网点人员")
    @RequestMapping(value = "/selectOwnEmpList", method = RequestMethod.GET)
wzp's avatar
wzp committed
656
    @RequiresAuthentication  //@RequiresPermissions("/employee/selectOwnEmpList")
liqin's avatar
liqin committed
657 658 659 660 661 662 663 664 665 666 667 668 669
    public ResponseEntity<Page<Employee>> selectOwnEmpList(String employName, String currBankID) {
        try {
            employName = StringUtils.trimToNull(employName);
            Page<Employee> page = getPage();
            Page<Employee> employeePage = employeeService.selectOwnEmpList(page, employName, currBankID);
            return ResponseEntity.ok(employeePage);
        } catch (Exception e) {
            logger.error("查询成员列表出错!", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
}