package com.bocloud.ims.booter.controller.resource.storage;

import com.bocloud.common.annotations.Log;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.GridBean;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.Backup;
import com.bocloud.ims.interfaces.resource.BackupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/v1/backups")
@Api(value = "Backup管理", tags = "Backup管理")
public class BackupController {

    @Autowired
    private BackupService backupService;

    /**
     * 查询Backup列表
     *
     * @param pager
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "Backup列表查询",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "Backup管理", operation = "查询Backup列表")
    @GetMapping
    public BocloudResult<GridBean<Backup>> list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return backupService.list(pager, context);
    }

    /**
     * 保存Backup
     *
     * @param backup
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "保存Backup",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "Backup管理", operation = "保存Backup")
    @PostMapping
    public BocloudResult<Backup> create(@RequestBody Backup backup, @Value(Common.REQ_CONTEXT) RequestContext context) {
        return backupService.create(backup, context);
    }

    /**
     * 删除Backup
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "删除Backup",  httpMethod = "DELETE")
    @Log(module = BoCloudService.IMS, target = "Backup管理", operation = "删除Backup")
    @DeleteMapping(value = "/{id}")
    public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
                                @Value(Common.REQ_CONTEXT) RequestContext context) {
        return backupService.remove(id, context);
    }

    /**
     * 更新Backup
     *
     * @param id
     * @param backup
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "更新Backup",  httpMethod = "PUT")
    @Log(module = BoCloudService.IMS, target = "Backup管理", operation = "更新Backup")
    @PutMapping(value = "/{id}")
    public BocloudResult<Backup> modify(@PathVariable(value = Common.ID) Long id, @RequestBody Backup backup,
                                        @Value(Common.REQ_CONTEXT) RequestContext context) {
        backup.setId(id);
        return backupService.modify(backup, context);
    }

    /**
     * 恢复Backup
     *
     * @param backup
     * @param context
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "恢复Backup",  httpMethod = "POST")
    @Log(module = BoCloudService.IMS, target = "Backup管理", operation = "恢复Backup")
    @PostMapping(value = "/restore")
    public BocloudResult restore(@RequestBody Backup backup, @Value(Common.REQ_CONTEXT) RequestContext context) {
        if (null != backup) {
            return backupService.restore(backup, context);
        } else {
            return ResultTools.formatErrResult();
        }
    }

    /**
     * 查询Backup详情
     *
     * @param id
     * @return
     */
    @ApiOperation(tags = {"CMC",
            "CSC"}, value = "查询Backup详情",  httpMethod = "GET")
    @Log(module = BoCloudService.IMS, target = "Backup管理", operation = "查询Backup详情")
    @GetMapping(value = "/{id}")
    public BocloudResult<Backup> detail(@PathVariable(value = Common.ID) Long id) {
        return backupService.detail(id);
    }

}
