package ink.wgink.register.base.service.impl;

import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.interfaces.expand.register.IRegisterHandlerService;
import ink.wgink.register.base.pojo.vos.RegisterDefaultVO;
import ink.wgink.register.base.pojo.vos.RegisterPhoneVO;
import ink.wgink.register.base.service.IRegisterService;
import ink.wgink.service.user.enums.UserStateEnum;
import ink.wgink.service.user.enums.UserTypeEnum;
import ink.wgink.service.user.pojo.vos.UserVO;
import ink.wgink.service.user.service.IUserService;
import ink.wgink.util.UUIDUtil;
import ink.wgink.util.string.WStringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: RegisterServiceImpl
 * @Description: 注册
 * @Author: wanggeng
 * @Date: 2021/5/1 10:33 上午
 * @Version: 1.0
 */
@Service
public class RegisterServiceImpl extends DefaultBaseService implements IRegisterService {

    private static final Logger LOG = LoggerFactory.getLogger(RegisterServiceImpl.class);
    @Autowired(required = false)
    private IRegisterHandlerService registerHandler;
    @Autowired
    private IUserService userService;

    @Override
    public void registerDefault(RegisterDefaultVO registerDefaultVO, Map<String, Object> params) throws Exception {
        register(registerDefaultVO.getUsername(), registerDefaultVO.getPassword(), params);
    }

    @Override
    public void registerPhone(RegisterPhoneVO registerPhoneVO, Map<String, Object> params) throws Exception {
        // 生成8位随机密码
        register(registerPhoneVO.getPhone(), WStringUtil.randomSubStr(UUIDUtil.get32UUID(), 8), params);
    }

    private void register(String username, String password, Map<String, Object> params) throws Exception {
        UserVO userVO = new UserVO();
        userVO.setUserUsername(username);
        userVO.setUserPassword(password);
        userVO.setUserName(username);
        userVO.setUserState(UserStateEnum.NORMAL.getValue());
        userVO.setUserType(UserTypeEnum.NORMAL.getValue());
        String userId = userService.saveAndReturnId(userVO, true);
        if (registerHandler != null) {
            registerHandler.handler(userId, params);
        }
    }


}
