package com.miaxis.app.controller.userInfo;

import com.miaxis.common.constant.Constants;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.*;

import com.miaxis.newgzpt.domain.GzptVideoVip;
import com.miaxis.newgzpt.vo.GzptVideoVipVO;
import com.miaxis.userInfo.dto.UserVipDTO;
import com.miaxis.userInfo.vo.UserVipVO;
import io.swagger.annotations.*;
import com.miaxis.common.core.domain.Response;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.miaxis.common.annotation.Log;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.enums.BusinessTypeEnum;
import com.miaxis.userInfo.domain.UserVip;
import com.miaxis.userInfo.service.IUserVipService;
import com.miaxis.common.utils.poi.ExcelUtil;
import com.miaxis.common.core.page.ResponsePageInfo;

/**
 * 【会员信息】Controller
 *
 * @author miaxis
 * @date 2022-06-02
 */
@RestController
@RequestMapping(Constants.OPEN_PREFIX + "/userInfo/vip")
@Api(tags = {"【app-会员信息】"})
public class UserVipController extends BaseController {
    @Autowired
    private IUserVipService userVipService;

    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==";


    /**
     * 获取会员信息详细信息
     *
     * @return
     */
    @GetMapping(value = "/{userId}")
    @ApiOperation("获取会员信息详细信息")
    public Response<UserVipVO> getUserInfoByUserId(
            @ApiParam(name = "userId", value = "会员信息参数", required = true)
            @PathVariable("userId") Long userId
    ) throws Exception {
        UserVip userVip = userVipService.getUserVipByUserId(userId);
        UserVipVO vo = new UserVipVO();
        if (userVip != null) {
            BeanUtils.copyProperties(userVip, vo);
            String sign = getSign(vo.getUserId(),vo.getSubject1(), vo.getSubject2(), vo.getSubject3(),vo.getSubject4());
            vo.setSign(sign);
            return Response.success(vo);
        } else {
            return Response.success(vo);
        }
    }


    /**
     * 删除会员信息
     */
    @Log(title = "会员信息", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{userId}")
    @ApiOperation("删除会员信息")
    public Response<Integer> removeByUserId(
            @ApiParam(name = "userId", value = "会员信息ids参数", required = true)
            @PathVariable Long userId
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        return toResponse(userVipService.removeByMap(map) ? 1 : 0);
    }


    private String getSign(Long userId, Date subject1, Date subject2, Date subject3, Date subject4) 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() + "";
        }
        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);

    }

}
