package beilv.system.service.impl;

import beilv.common.core.text.Convert;
import beilv.common.utils.DateUtils;
import beilv.common.utils.rsa.RsaUtil;

import beilv.system.domain.SysMember;
import beilv.system.mapper.SysMemberMapper;
import beilv.system.service.ISysMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 会员用户信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-02
 */
@Service
public class SysMemberServiceImpl implements ISysMemberService {
    @Autowired
    private SysMemberMapper sysMemberMapper;
    @Value("${rsa.privateKey}")
    private String privateKey;

    /**
     * 查询会员用户信息
     *
     * @param id 会员用户信息主键
     * @return 会员用户信息
     */
    @Override
    public SysMember selectSysMemberById(Long id) {
        return sysMemberMapper.selectSysMemberById(id);
    }

    /**
     * 查询会员用户信息列表
     *
     * @param sysMember 会员用户信息
     * @return 会员用户信息
     */
    @Override
    public List<SysMember> selectSysMemberList(SysMember sysMember) {
        return sysMemberMapper.selectSysMemberList(sysMember);
    }

    /**
     * 新增会员用户信息
     *
     * @param sysMember 会员用户信息
     * @return 结果
     */
    @Override
    public int insertSysMember(SysMember sysMember) {
        sysMember.setCreateTime(DateUtils.getNowDate());
        return sysMemberMapper.insertSysMember(sysMember);
    }

    /**
     * 修改会员用户信息
     *
     * @param sysMember 会员用户信息
     * @return 结果
     */
    @Override
    public int updateSysMember(SysMember sysMember) {
        sysMember.setUpdateTime(DateUtils.getNowDate());
        return sysMemberMapper.updateSysMember(sysMember);
    }

    /**
     * 批量删除会员用户信息
     *
     * @param ids 需要删除的会员用户信息主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberByIds(String ids) {
        return sysMemberMapper.deleteSysMemberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除会员用户信息信息
     *
     * @param id 会员用户信息主键
     * @return 结果
     */
    @Override
    public int deleteSysMemberById(Long id) {
        return sysMemberMapper.deleteSysMemberById(id);
    }



    /**
     * 获取登录用户信息
     * @param request
     * @return
     * @throws Exception
     */
    @Override
    public SysMember getLoginSysMember(HttpServletRequest request) {
        String token = request.getHeader("accessToken");
        String str = null;
        try {
            str = RsaUtil.decrypt(token, privateKey);
        } catch (Exception e) {
            throw new RuntimeException("获取用户信息失败");
        }
        return sysMemberMapper.selectSysMemberById(Long.parseLong(str));
    }


}
