4-1 新增、更新用户接口

创建表单视图对象

@Getter
@Setter
public class UserParam {

    private Integer id;

    @NotBlank(message = "用户名不可以为空")
    @Length(min = 1, max = 20, message = "用户名长度需要在20个字以内")
    private String username;

    @NotBlank(message = "电话不可以为空")
    @Length(min = 1, max = 13, message = "电话长度需要在13个字以内")
    private String telephone;

    @NotBlank(message = "邮箱不允许为空")
    @Length(min = 5, max = 50, message = "邮箱长度需要在50个字符以内")
    private String mail;

    @NotNull(message = "必须提供用户所在的部门")
    private Integer deptId;

    @NotNull(message = "必须指定用户的状态")
    @Min(value = 0, message = "用户状态不合法")
    @Max(value = 2, message = "用户状态不合法")
    private Integer ststus;

    @Length(min = 0, max = 200, message = "备注长度需要在200个字以内")
    private String remark = "";
}

新增用户 service

@Service
public class SysUserService {
	
	@Autowired
	private SysUserMapper sysUserMapper;
	
	public void save(@Valid UserParam param) {
		if(checkTelephoneExist(param.getTelephone(), param.getId())) {
			throw new ParamException("电话已经被占用");
		}
		if(checkEmailExist(param.getMail(), param.getId())) {
			throw new ParamException("邮箱已经被占用");
		}
		
		String password = "123456";
		String encryptedPassword = MD5Util.encrypt(password);
		
		SysUser sysUser = SysUser.builder().username(param.getUsername())
								  .telephone(param.getTelephone())
								  .mail(param.getMail())
								  .password(encryptedPassword)
								  .deptId(param.getDeptId())
								  .stasus(param.getStstus())
								  .remark(param.getRemark())
								  .build();
		sysUser.setOperator(RequestHolder.getCurrentUser().getUsername());	// 获取当前线程的用户名
		sysUser.setOperateIp("127.0.0.1"); // TODO
		sysUser.setOperateTime(new Date());
		
		// TODO:send email
		
		sysUserMapper.insert(sysUser);
	}
	
}

附:MD5加密工具类

@Slf4j
public class MD5Util {

    public final static String encrypt(String s) {
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char str[] = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            log.error("generate md5 error, {}", s, e);
            return null;
        }
    }
}

更新用户  service

public void update(@Valid UserParam param) {
	if(checkTelephoneExist(param.getTelephone(), param.getId())) {
		throw new ParamException("电话已经被占用");
	}
	if(checkEmailExist(param.getMail(), param.getId())) {
		throw new ParamException("邮箱已经被占用");
	}
	
	SysUser before = sysUserMapper.selectByPrimaryKey(param.getId());
	Preconditions.checkNotNull(before, "待更新的用户不存在");
	SysUser after = SysUser.builder().id(param.getId())
									 .username(param.getUsername())
									 .telephone(param.getTelephone())
									 .mail(param.getMail())
									 .deptId(param.getDeptId())
									 .stasus(param.getStstus())
									 .remark(param.getRemark())
									 .build();
	after.setOperator(RequestHolder.getCurrentUser().getUsername());	// 获取当前线程的用户名
	after.setOperateIp("127.0.0.1"); // TODO
	after.setOperateTime(new Date());
	
	sysUserMapper.updateByPrimaryKeySelective(after);	// 字段为空不更新
}

新增用户、更新用户 Controller

@Controller
@RequestMapping("/sys/user")
public class SysUserController {
	
	@Autowired
	private SysUserService sysUserService;
	
	@RequestMapping("/save")
	@ResponseBody
	public JsonData saveDept(UserParam param) {
		sysUserService.save(param);
		return JsonData.success();
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public JsonData updateDept(UserParam param) {
		sysUserService.update(param);
		return JsonData.success();
	}
	
}


你可能感兴趣的:(pemission)