package com.miaxis.app.controller.exam;

import com.miaxis.common.constant.Constants;
import com.miaxis.common.core.controller.BaseController;
import com.miaxis.common.core.domain.Response;
import com.miaxis.common.core.page.ResponsePageInfo;
import com.miaxis.common.utils.PrivateKeyUtil;
import com.miaxis.examvip.domain.VipUserExam;
import com.miaxis.examvip.service.IVipUserExamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

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

/**
 * 【考场会员】Controller
 *
 * @author miaxis
 * @date 2023-03-20
 */
@RestController
@RequestMapping(Constants.OPEN_PREFIX + "/vip/exam")
@Api(tags = {"【APP-考场会员】"})
public class VipUserExamController extends BaseController {
    @Autowired
    private IVipUserExamService vipUserExamService;

    /**
     * 查询当前用户考试会员列表
     */
    @GetMapping("/list")
    @ApiOperation("查询当前用户考试会员列表")
    public ResponsePageInfo<VipUserExam> list(Long userId, Long examId) throws Exception {
        VipUserExam vipUserExam = new VipUserExam();
        vipUserExam.setUserId(userId);
        vipUserExam.setExamId(examId);
        List<VipUserExam> list = vipUserExamService.selectVipUserExamList(vipUserExam);
        this.doSign(list);
        return toResponsePageInfo(list);
    }


    /**
     * 删除考场会员信息
     */
    @DeleteMapping("/{userId}")
    @ApiOperation("删除考场会员信息")
    public Response<Integer> removeByUserId(
            @ApiParam(name = "userId", value = "考试会员信息userId", required = true)
            @PathVariable Long userId
    ) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);

        boolean isDel = vipUserExamService.removeByMap(map);
        if(isDel) {
            Response response = new Response(200,"删除成功");
            return response;
        } else {
            Response response = new Response(200,"该记录不存在");
            return response;
        }

    }


    private void doSign(List<VipUserExam> userExamList) throws Exception {

        for (VipUserExam userExam : userExamList) {
            String str = "";
            if (userExam.getUserId() != null) {
                str += userExam.getUserId();
            }
            if (userExam.getExpirationTime() != null) {
                str += userExam.getExpirationTime().getTime();
            }
            if (userExam.getIsVip() != null) {
                str += userExam.getIsVip();
            }
            // 进行签名服务
            Signature signature = Signature.getInstance("SHA256withRSA");
            KeyFactory kf = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = kf.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(PrivateKeyUtil.rsaPrivateKey)));
            signature.initSign(privateKey);
            signature.update(str.getBytes("UTF-8"));
            byte[] signedData = signature.sign();
            String sign = Base64.getEncoder().encodeToString(signedData);
            userExam.setSign(sign);
        }
    }
}