package com.sunyard.chsm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.constant.AuditLogConst;
import com.sunyard.chsm.dto.KeyTemplateDTO;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.service.KeyTemplateService;
import com.sunyard.ssp.common.annotation.AuditControllerLog;
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;

/**
 * 密钥模版管理接口
 *
 * @author liulu
 * @since 2024/10/22
 */
@RestController
@RequestMapping("/key/template")
public class KeyTemplateController {

    @Resource
    private KeyTemplateService keyTemplateService;

    /**
     * 分页查询密钥模版
     *
     * @param query 查询条件
     * @return 分页列表
     */
    @GetMapping("/pageList")
    public R<Page<KeyTemplateDTO.KTView>> servicePageList(KeyTemplateDTO.Query query) {

        Page<KeyTemplateDTO.KTView> page = keyTemplateService.selectPageList(query);

        return R.data(page);
    }

    /**
     * 添加密钥模版
     *
     * @param save 参数
     * @return id
     */
    @PostMapping
    @AuditControllerLog(description = "添加密钥模版", operateType = AuditLogConst.ADD)
    public R<String> save(@Valid @RequestBody KeyTemplateDTO.KTSave save) {
        Long id = keyTemplateService.save(save);
        return R.data(String.valueOf(id));
    }

    /**
     * 修改密钥模版
     *
     * @param update 参数
     * @return void
     */
    @PutMapping
    @AuditControllerLog(description = "修改密钥模版", operateType = AuditLogConst.UPDATE)
    public R<Void> update(@Valid @RequestBody KeyTemplateDTO.KTSave update) {
        keyTemplateService.update(update);
        return R.ok();
    }

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

}
