package com.bjtdba.merchant.service.impl;

import at.favre.lib.crypto.bcrypt.BCrypt;
import com.bjtdba.merchant.dao.UserDao;
import com.bjtdba.merchant.entity.User;
import com.bjtdba.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;

@Service
public class Impl {
    @Autowired
    private UserDao userDao;

    /**
     * 添加用户信息返回用户uid
     * @param user
     * @return uid
     */
    public  Object addUser(User user) {
        Map<String, Object> data = new HashMap<String, Object>();
//        user.setAccount(phone);
        int i=0;
        try{
        user.setPwd(BCrypt.with(BCrypt.Version.VERSION_2Y).hashToString(10, "123456".toCharArray()));//默认密码123456
        user.setNickname("用户"+user.getPhone().substring(7));
        user.setAvatar("https://app.bjtdba.com/uploads/logo.jpg");
//        user.setPhone(phone);

            user.setLast_ip(InetAddress.getLocalHost().getHostAddress());
            user.setIdentity(5);//推广员
            i = userDao.addSave(user);
        }catch (Exception e){
            e.printStackTrace();
        }

        if(i>1){
            //添加成功
            return ResponseUtil.ok();
        }else{
            //添加失败

            return ResponseUtil.fail();
        }

    }
}
