package co.yixiang.yshop.module.system.controller.admin.backuprecord;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.system.controller.admin.backuprecord.vo.BackupPageReqVO;
import co.yixiang.yshop.module.system.controller.admin.backuprecord.vo.BackupRecordRespVO;
import co.yixiang.yshop.module.system.convert.backuprecord.BackupRecordConvert;
import co.yixiang.yshop.module.system.dal.dataobject.backup.BackupRecordDO;
import co.yixiang.yshop.module.system.service.backuprecord.BackupRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 备份")
@RestController
@RequestMapping("/system/backup")
@Validated
public class BackupRecordController {

    @Resource
    private BackupRecordService backupRecordService;


    @GetMapping("/page")
    @Operation(summary = "获得备份记录分页")
    @PreAuthorize("@ss.hasPermission('system:store-backup-record:query')")
    public CommonResult<PageResult<BackupRecordRespVO>> backupPageList(@Valid BackupPageReqVO pageVO) {

        return success(backupRecordService.backupPageList(pageVO));
    }

    @Operation(summary = "备份信息")
    @PreAuthorize("@ss.hasPermission('system:store-backup-record:backup')")
    @PostMapping(value = "/backup")
    public CommonResult<Boolean> backup() {
        backupRecordService.backup();
        return success(true);
    }

    @Operation(summary = "还原备份")
    @PreAuthorize("@ss.hasPermission('system:store-backup-record:revert')")
    @PostMapping(value = "/revertBackup")
    public CommonResult<Boolean> backupRecord() {
        backupRecordService.revertBackup();
        return success(true);
    }

}
