package com.miaxis.userInfo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miaxis.userInfo.domain.UserVip;
import com.miaxis.userInfo.dto.UserVipDTO;
import com.miaxis.userInfo.mapper.UserVipMapper;
import com.miaxis.userInfo.service.IUserVipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;
import java.util.Date;
import java.util.List;

/**
 * 会员信息Service业务层处理
 *
 * @author miaxis
 * @date 2022-06-02
 */
@Service
public class UserVipServiceImpl extends ServiceImpl<UserVipMapper, UserVip> implements IUserVipService {
    @Autowired
    private UserVipMapper userVipMapper;


    private String rsaPrivateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMLNHTgsyYOXDJVF8r4qCyb+8Uw4" +
            "XKq8aThAhUIaPYIop+HJxETGbXk3e0o46i4iOX8RTv0i3jmhL6W0mSxy9ismfXPP7VtKgEt6VnMm" +
            "ufsrQ2kcXyRsKmuwUd3rTH5gniXwLdIPF8V5ujiSq94nPHok1krDMWbss8Q7lcH2kQFHAgMBAAEC" +
            "gYBh6Eu2TzRdMg79+7EciO8gA2Tt5SoWGipcPnl7lsqxX5O5dPpzgrPFB/CshlyVIVh6mA12Rw8g" +
            "MfZLt7CA2asFdgGHfgCvMjsCnL2U1Nf1QFnsnGXmFXeVUvdy7XPILi75rVyOEAw+/f3KzjSA7B3w" +
            "URE8M2QlIUcjm8LLyCVC4QJBAO+taJH7pVZGVn2upsOk14X08QU0lsQ7ufe/8VXe+IujwGMh34U6" +
            "BHGV7grodtA7TvQ3mDggQLHB2+IxBWdDCrcCQQDQEVMI+CD7afEyN9rK/MAJwevtxNaTyEd1bhKw" +
            "t6WKr1YbAdUz2gvnpCQjD1Gqt5yXHKu279msX9W4euXePG3xAkBUCYZn4UdIp+L9sHZSlQLoukik" +
            "HZtbdOGw58Ez7blSJPl5CNB5wyz5sqtDcdzvCEyXvsKFs3FbonB5r9yIJ26jAkEAugGC1dXcxmWj" +
            "dl1wf18M3qw6GkmJ+ntlRpmso8bJev3cYDB3RO99DQw0MhOT0qZqmrzK1bP+SJc0HMPhk5c6QQJA" +
            "WRLIxmeM/P9X2mDjMrI46Wnn27QoKGgYSHdPxmLD0y7NUytf5HyCsY2vhA9FZcnikibax7SwzC/B" +
            "D7yOXFAy/Q==";

    /**
     * 查询会员信息列表
     *
     * @param userVip 会员信息
     * @return 会员信息
     */
    @Override
    public List<UserVip> selectUserVipList(UserVip userVip){
        return userVipMapper.selectUserVipList(userVip);
    }

    @Override
    public UserVip getUserVipByUserId(Long userId) {
        return userVipMapper.getUserVipByUserId(userId);
    }

    @Override
    public int removeByUserId(Long userId) {
        return userVipMapper.removeByUserId(userId);
    }

    @Override
    public String getSignInt(Long userId, Date subject1, Date subject2, Date subject3, Date subject4, Integer subject1Int, Integer subject2Int, Integer subject3Int, Integer subject4Int) throws Exception {
        String str = userId + "";
        // System.out.println(userId);
        if (subject1 != null) {
            str += subject1.getTime() + "";
        }
        if (subject2 != null) {
            str += subject2.getTime() + "";
        }
        if (subject3 != null) {
            str += subject3.getTime() + "";
        }
        if (subject4 != null) {
            str += subject4.getTime() + "";
        }
        if (subject1Int != null) {
            str += subject1Int + "";
        }
        if (subject2Int != null) {
            str += subject2Int + "";
        }
        if (subject3Int != null) {
            str += subject3Int + "";
        }
        if (subject4Int != null) {
            str += subject4Int + "";
        }
        System.out.println("str：" + str);
        // 进行签名服务
        Signature signature = Signature.getInstance("SHA256withRSA");
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(rsaPrivateKey)));
        signature.initSign(privateKey);
        signature.update(str.getBytes("UTF-8"));
        byte[] signedData = signature.sign();

        return Base64.getEncoder().encodeToString(signedData);

    }

}
