package com.ads.business.controller;

import com.ads.business.service.DocHistoryService;
import com.ads.business.service.DocService;
import com.ads.business.service.model.request.DocRequest;
import com.ads.business.service.model.request.DocListRequest;
import com.ads.business.service.model.request.UpdateSwitchRequest;
import com.ads.common.base.BaseController;
import com.ads.common.data.JsonResult;
import com.ads.common.util.UtilMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author 作者 Ludilong
 * @date 创建时间 2021年10月25日
 * @description 文件管理
 */
@Api(tags = "文件管理")
@RestController
@RequestMapping(value = "/doc/")
public class DocController extends BaseController {

    @Resource
    private DocService docService;

    @Resource
    private DocHistoryService docHistoryService;

    /**
     * 新增文件信息
     *
     * @param inData 文件信息
     * @return 执行结果
     */
    @PostMapping(value = "addDoc")
    @ApiOperation(value = "新增文件信息", notes = "新增文件信息")
    public JsonResult addDoc(@RequestBody DocRequest inData) {
        String message = docService.addDoc(inData);
        if (StringUtils.isNotEmpty(message)) {
            return JsonResult.fail(message);
        }
        return JsonResult.success(null,UtilMessage.SAVE_MESSAGE_SUCCESS);
    }

    /**
     * 编辑文件信息
     *
     * @param inData 文件信息
     * @return 执行结果信息
     */
    @PutMapping(value = "updateDoc")
    @ApiOperation(value = "编辑文件信息", notes = "编辑文件信息")
    public JsonResult updateDoc(@RequestBody DocRequest inData) {
        String message = docService.updateDoc(inData);
        if (StringUtils.isNotEmpty(message)) {
            return JsonResult.fail(message);
        }
        return JsonResult.success(null, UtilMessage.SAVE_MESSAGE_SUCCESS);
    }

    /**
     * 获取文件详情
     *
     * @param docId 文件ID
     * @return 返回结果信息
     */
    @ApiOperation(value = "获取文件详情", notes = "获取文件详情")
    @GetMapping(value = "getDetail")
    public JsonResult getDetail(@RequestParam(value = "id", required = true) String docId) {
        return JsonResult.success(docService.detail(Long.valueOf(docId)), UtilMessage.GET_MESSAGE_SUCCESS);
    }

    /**
     * 状态修改
     *
     * @param request 修改状态入参
     * @return JsonResult
     */
    @PutMapping("/updateSwitch")
    @ApiOperation(value = "状态修改", notes = "状态修改")
    public JsonResult updateSwitch(@RequestBody @Valid UpdateSwitchRequest request) {
        return docService.updateSwitch(Long.valueOf(request.getId()), request.getStatus());
    }

    /**
     * 获取文件列表信息
     *
     * @param request 参数信息
     * @return 返回文件列表结果
     */
    @ApiOperation(value = "获取文件列表信息", notes = "获取文件列表信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK")
    })
    @PostMapping(value = "getDocList")
    public JsonResult getDocList(@RequestBody DocListRequest request) {
        return docService.docList(request, 2);
    }

    /**
     * 获取文件的历史记录列表
     *
     * @param docId 文件ID
     * @return 返回历史文件列表
     */
    @GetMapping(value = "getDocHistoryList")
    @ApiOperation(value = "获取文件的历史记录列表", notes = "获取文件的历史记录列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK")
    })
    public JsonResult getDocHistoryList(@RequestParam(value = "id", required = true) String docId) {
        return JsonResult.success(docHistoryService.getDocHistoryList(Long.valueOf(docId)), UtilMessage.GET_MESSAGE_SUCCESS);
    }
}
