package com.yunfeiyun.agmp.common.framework.web.service;

import com.yunfeiyun.agmp.common.constant.UserConstants;
import com.yunfeiyun.agmp.common.core.domain.entity.SysUser;
import com.yunfeiyun.agmp.common.core.domain.model.RegisterBody;
import com.yunfeiyun.agmp.common.utils.SecurityUtils;
import com.yunfeiyun.agmp.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 注册校验方法
 * 
 * @author 杨晓辉
 */
@Component
public class SysRegisterService
{
    @Autowired
    private ISysUserService userService;


    /**
     * 注册
     */
    public String register(RegisterBody registerBody)
    {
        String msg = "", loginname = registerBody.getLoginname(), password = registerBody.getPassword();
        SysUser sysUser = new SysUser();
        sysUser.setUserLoginname(loginname);

        if (StringUtils.isEmpty(loginname))
        {
            msg = "登录名不能为空";
        }
        else if (StringUtils.isEmpty(password))
        {
            msg = "用户密码不能为空";
        }
        else if (loginname.length() < UserConstants.USERNAME_MIN_LENGTH
                || loginname.length() > UserConstants.USERNAME_MAX_LENGTH)
        {
            msg = "账户长度必须在2到20个字符之间";
        }
        else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            msg = "密码长度必须在5到20个字符之间";
        }
        else if (!userService.checkUserNameUnique(sysUser))
        {
            msg = "保存用户'" + loginname + "'失败，注册账号已存在";
        }
        else
        {
            sysUser.setUserName(loginname);
            sysUser.setUserLoginpwd(SecurityUtils.encryptPassword(password));
            boolean regFlag = userService.registerUser(sysUser);
            if (!regFlag)
            {
                msg = "注册失败,请联系系统管理人员";
            }
            else
            {

            }
        }
        return msg;
    }


}
