package cn.meowrain.aioj.backend.userservice.dto.chains;

import cn.meowrain.aioj.backend.framework.core.designpattern.chains.AbstractChianHandler;
import cn.meowrain.aioj.backend.framework.core.errorcode.ErrorCode;
import cn.meowrain.aioj.backend.framework.core.exception.ClientException;
import cn.meowrain.aioj.backend.userservice.common.enums.ChainMarkEnums;
import cn.meowrain.aioj.backend.userservice.dto.req.UserRegisterRequestDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class UserRegisterRequestParamVerifyChain implements AbstractChianHandler<UserRegisterRequestDTO> {

	@Override
	public void handle(UserRegisterRequestDTO requestParam) {
		// 校验参数里面用户名和密码是不是空的
		if (StringUtils.isAnyBlank(requestParam.getUserAccount(), requestParam.getUserPassword())) {
			throw new ClientException("参数为空", ErrorCode.PARAMS_ERROR);
		}
		if (requestParam.getUserAccount().length() < 4) {
			throw new ClientException("账号长度不小于4位", ErrorCode.PARAMS_ERROR);
		}
		if (requestParam.getUserPassword().length() < 8) {
			throw new ClientException("密码长度不小于8位", ErrorCode.PARAMS_ERROR);
		}
		// 密码和校验密码相同
		if (!requestParam.getUserPassword().equals(requestParam.getCheckPassword())) {
			throw new ClientException("两次输入的密码不一致", ErrorCode.PARAMS_ERROR);
		}

	}

	@Override
	public String mark() {
		return ChainMarkEnums.USER_REGISTER_REQ_PARAM_VERIFY.getMarkName();
	}

	@Override
	public int getOrder() {
		return 10;
	}

}
