package cn.iocoder.yudao.module.chain.controller.admin.blockcertificate;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.chain.controller.admin.blockcertificate.vo.BlockCertificatePageReqVO;
import cn.iocoder.yudao.module.chain.controller.admin.blockcertificate.vo.BlockCertificateRespVO;
import cn.iocoder.yudao.module.chain.convert.blockcertificate.BlockCertificateConvert;
import cn.iocoder.yudao.module.chain.dal.dataobject.blockcertificate.BlockCertificateDO;
import cn.iocoder.yudao.module.chain.service.blockcertificate.BlockCertificateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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


@Api(tags = "管理后台 - 区块证书")
@RestController
@RequestMapping("/chain/block-certificate")
@Validated
public class BlockCertificateController {

    @Resource
    private BlockCertificateService blockCertificateService;

    @GetMapping("/page")
    @ApiOperation("获得区块证书分页")
    @PreAuthorize("@ss.hasPermission('chain:block-certificate:query')")
    public CommonResult<PageResult<BlockCertificateRespVO>> getBlockCertificatePage(@Valid BlockCertificatePageReqVO pageVO) {
        PageResult<BlockCertificateDO> pageResult = blockCertificateService.getBlockCertificatePage(pageVO);
        return CommonResult.success(BlockCertificateConvert.INSTANCE.convertPage(pageResult));
    }

    @PutMapping("/upChain")
    @ApiOperation("更新区块证书的上链状态")
    @ApiImplicitParam(name = "id", value = "ID", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('chain:block-certificate:update')")
    public CommonResult<Boolean> upChain(@RequestParam("id") Long id) {
        blockCertificateService.upChain(id);
        return CommonResult.success(true);
    }

}
