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.exam.domain.ExamInfo;
import com.miaxis.exam.dto.ExamInfoDto;
import com.miaxis.exam.service.IExamInfoService;
import com.miaxis.exam.vo.ExamInfoVipVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
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.List;

/**
 * 【考场信息】Controller
 *
 * @author miaxis
 * @date 2023-03-20
 */
@RestController
@RequestMapping(Constants.OPEN_PREFIX + "/exam/info")
@Api(tags = {"【APP-考场信息】"})
public class ExamInfoController extends BaseController {
    @Autowired
    private IExamInfoService examInfoService;


    /**
     * 查询考场信息列表
     */
    @GetMapping("/list")
    @ApiOperation("查询考场信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页码", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页数据量", dataType = "int", paramType = "query", required = false),
    })
    public ResponsePageInfo<ExamInfoVipVo> list(@ModelAttribute ExamInfoDto examInfoDto) throws Exception {
        startPage();
        List<ExamInfoVipVo> examInfoVipVoList = examInfoService.selectExamInfoList(examInfoDto);
        this.doSign(examInfoVipVoList);
        return toResponsePageInfo(examInfoVipVoList);
    }

    /**
     * 获取考场信息详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取考场信息详细信息")
    public Response<ExamInfo> getInfo(
            @ApiParam(name = "id", value = "考场信息参数", required = true)
            @PathVariable("id") Long id
    ) {
        return Response.success(examInfoService.getById(id));
    }


    private void doSign(List<ExamInfoVipVo> examInfoVipVoList) throws Exception {

        for (ExamInfoVipVo examInfoVipVo : examInfoVipVoList) {
            String str = "";
            if (examInfoVipVo.getUserId() != null) {
                str += examInfoVipVo.getUserId();
            }
            if (examInfoVipVo.getExpirationTime() != null) {
                str += examInfoVipVo.getExpirationTime().getTime();
            }
            if (examInfoVipVo.getIsVip() != null) {
                str += examInfoVipVo.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);
            examInfoVipVo.setSign(sign);
        }
    }


}
