package com.sunyard.chsm.controller;


import com.sunyard.chsm.model.R;
import com.sunyard.chsm.model.dto.CertDTO;
import com.sunyard.chsm.param.*;
import com.sunyard.chsm.service.AppCertService;
import com.sunyard.chsm.service.CertService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * 证书管理类接口
 */
@RestController
@RequestMapping("/cert")
public class CertController {


    @Resource
    private AppCertService appCertService;

    @Resource
    private CertService certService;

    /**
     * 导出证书
     *
     * @param req
     * @return
     */
    @PostMapping("/exportCert")
    public R<ExportCertResp> exportCert(@Valid @RequestBody ExportCertReq req) {
        ExportCertResp resp = certService.exportCert(req);
        return R.data(resp);
    }

    /**
     * 获取证书信息
     *
     * @param certString 证书内容
     */
    @GetMapping("/info")
    public R<CertInfoResp> getCertInfo(@RequestParam String certString) {
        CertInfoResp certInfo = certService.getCertInfo(certString);
        return R.data(certInfo);
    }

    /**
     * 获取证书拓展信息
     *
     * @param certString 证书内容
     */
    @GetMapping("/exinfo")
    public R<CertExinfoResp> getCertExinfo(@RequestParam String certString) {
        CertExinfoResp certExinfo = certService.getCertExinfo(certString);
        return R.data(certExinfo);
    }


    /**
     * 验证证书
     *
     * @param certString 证书内容
     */
    @GetMapping("/check")
    public R<Boolean> certCheck(@RequestParam String certString) {
        Boolean res = certService.checkCert(certString);
        return R.data(res);
    }


    /**
     * 导入证书
     *
     * @param req 证书
     */
    @PostMapping("/import")
    public R<Void> importCert(@Valid @RequestBody ImportCertReq req) {
        CertDTO.ImportCert importCert = new CertDTO.ImportCert();
        BeanUtils.copyProperties(req, importCert);
        appCertService.importCert(importCert);
        return R.ok();
    }

    /**
     * 删除证书
     *
     * @param id id
     * @return void
     */
    @DeleteMapping
    public R<Void> delete(Long id) {
        appCertService.delete(id);
        return R.ok();
    }

}
