package com.jojubanking.boot.module.system.service.userinfo;

import cn.hutool.core.util.IdUtil;
import com.jojubanking.boot.framework.common.enums.CommonStatusEnum;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.jojubanking.boot.module.system.controller.app.userinfo.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.userinfo.UserInfoDO;
import com.jojubanking.boot.framework.common.pojo.PageResult;

import com.jojubanking.boot.module.system.convert.userinfo.UserInfoConvert;
import com.jojubanking.boot.module.system.dal.mysql.userinfo.UserInfoMapper;

import static com.jojubanking.boot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jojubanking.boot.module.system.enums.ErrorCodeConstants.*;

/**
 * 用户信息 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public UserInfoDO getUserByMobile(String mobile) {
        return userInfoMapper.selectByMobile(mobile);
    }

    @Override
    public UserInfoDO createUserIfAbsent(String mobile, String registerIp) {
        // 用户已经存在
        UserInfoDO user = userInfoMapper.selectByMobile(mobile);
        if (user != null) {
            return user;
        }
        // 用户不存在，则进行创建
        return this.createUser(mobile, registerIp);
    }
    private UserInfoDO createUser(String mobile, String registerIp) {
        // 生成密码
        String password = IdUtil.fastSimpleUUID();
        // 插入用户
        UserInfoDO user = new UserInfoDO();
        user.setPhone(mobile);
//        user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
        user.setPassword(encodePassword(password)); // 加密密码
//        user.setRegisterIp(registerIp);
        userInfoMapper.insert(user);
        return user;
    }
    /**
     * 对密码进行加密
     *
     * @param password 密码
     * @return 加密后的密码
     */
    private String encodePassword(String password) {
        return passwordEncoder.encode(password);
    }

    @Override
    public Long createUserInfo(AppUserInfoCreateReqVO createReqVO) {
        // 插入
        UserInfoDO userInfo = UserInfoConvert.INSTANCE.convert(createReqVO);
        userInfoMapper.insert(userInfo);
        // 返回
        return userInfo.getId();
    }

    @Override
    public void updateUserInfo(AppUserInfoUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateUserInfoExists(updateReqVO.getId());
        // 更新
        UserInfoDO updateObj = UserInfoConvert.INSTANCE.convert(updateReqVO);
        userInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserInfo(Long id) {
        // 校验存在
        this.validateUserInfoExists(id);
        // 删除
        userInfoMapper.deleteById(id);
    }

    private void validateUserInfoExists(Long id) {
        if (userInfoMapper.selectById(id) == null) {
            throw exception(USER_INFO_NOT_EXISTS);
        }
    }

    @Override
    public UserInfoDO getUserInfo(Long id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public List<UserInfoDO> getUserInfoList(Collection<Long> ids) {
        return userInfoMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<UserInfoDO> getUserInfoPage(AppUserInfoPageReqVO pageReqVO) {
        return userInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserInfoDO> getUserInfoList(AppUserInfoExportReqVO exportReqVO) {
        return userInfoMapper.selectList(exportReqVO);
    }

}
