package com.willalp.control.userinfo.service.impl;

import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.control.userinfo.domain.HsSysUserinfo;
import com.willalp.control.userinfo.mapper.HsSysUserinfoMapper;
import com.willalp.control.userinfo.service.IHsSysUserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 注册用户信息Service业务层处理
 *
 * @author willalp
 * @date 2021-11-24
 */
@Service
public class HsSysUserinfoServiceImpl extends ServiceImpl<HsSysUserinfoMapper, HsSysUserinfo> implements IHsSysUserinfoService
{
    @Autowired
    private HsSysUserinfoMapper hsSysUserinfoMapper;

    /**
     * 查询注册用户信息列表
     *
     * @param hsSysUserinfo 注册用户信息
     * @return 注册用户信息
     */
    @Override
    public List<HsSysUserinfo> selectHsSysUserinfoList(HsSysUserinfo hsSysUserinfo) {
        return hsSysUserinfoMapper.selectHsSysUserinfoList(hsSysUserinfo);
    }

    @Override
    public void saveUserInfo(HsSysUserinfo hsSysUserinfo) {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsSysUserinfo byId = hsSysUserinfoMapper.selectById(idStr);
            if (null == byId) {
                hsSysUserinfo.setUserId(idStr);
                break;
            }
        }
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        hsSysUserinfo.setUserPassword(md5.digestHex(hsSysUserinfo.getUserPassword()));
        hsSysUserinfo.setCreateBy(SecurityUtils.getUsername());
        hsSysUserinfo.setCreateTime(DateUtils.getNowDate());
        hsSysUserinfoMapper.insert(hsSysUserinfo);
    }

}
