package com.sunyard.chsm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.constant.AuditLogConst;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.model.dto.CertDTO;
import com.sunyard.chsm.service.CaCertService;
import com.sunyard.ssp.common.annotation.AuditControllerLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * CA管理接口
 *
 * @author liulu
 * @since 2024/11/11
 */
@Slf4j
@RestController
@RequestMapping("/ca/cert")
public class CaController {

    @Resource
    private CaCertService caCertService;

    /**
     * 分页查询CA列表
     *
     * @param query query
     * @return 列表
     */
    @GetMapping("/pageList")
    public R<Page<CertDTO.CAView>> queryPageList(CertDTO.CAQuery query) {

        Page<CertDTO.CAView> page = caCertService.selectPageList(query);

        return R.data(page);
    }

    /**
     * 新增CA
     *
     * @param caInfo 证书
     */
    @PostMapping
    public R<Long> save(@Valid @RequestBody CertDTO.CaInfo caInfo) {
        Long id = caCertService.save(caInfo);
        return R.data(id);
    }

    /**
     * 修改CA
     *
     * @param update 参数
     * @return void
     */
    @PutMapping
    @AuditControllerLog(description = "修改CA", operateType = AuditLogConst.UPDATE)
    public R<Void> update(@Valid @RequestBody CertDTO.CaInfo update) {
        caCertService.update(update);
        return R.ok();
    }

    /**
     * 删除CA
     *
     * @param id id
     * @return void
     */
    @DeleteMapping
    @AuditControllerLog(description = "删除CA", operateType = AuditLogConst.DELETE)
    public R<Void> delete(Long id) {
        caCertService.delete(id);
        return R.ok();
    }


}
