package com.njcn.system.controller;

import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.system.pojo.po.Resinformation;
import com.njcn.system.service.IResourceAdministrationService;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


/**
 * @version 1.0.0
 * @author: zbj
 * @date: 2023/04/17
 */
@Slf4j
@Api(tags = "资源管理")
@RestController
@RequestMapping("/resourceAdministration")
@RequiredArgsConstructor
public class ResourceAdministrationController extends BaseController {

    private final IResourceAdministrationService iResourceAdministrationService;

    /**
     * 上传资源
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/uploadFile")
    @ApiOperation("上传资源")
    public HttpResult<Boolean> uploadFile(@ApiParam(value = "文件", required = true) @RequestPart("multipartFile") MultipartFile multipartFile,
                                          @ApiParam(value = "资源名称", required = true) String name,
                                          @ApiParam(value = "资源类型", required = true) String type,
                                          @ApiParam(value = "资源描述", required = true) String description,
                                          @ApiParam(value = "系统类型Guid") String systemType) {
        String methodDescribe = getMethodDescribe("uploadFile");
        Boolean flag = iResourceAdministrationService.uploadFile(multipartFile, name, type, description, systemType);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }

    /**
     * 查询数据
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/queryData")
    @ApiOperation("查询数据")
    @ApiImplicitParam(name = "resinformation", value = "查询数据", required = true)
    public HttpResult<List<Resinformation>> queryData(@RequestBody Resinformation resinformation) {
        String methodDescribe = getMethodDescribe("queryData");
        List<Resinformation> result = iResourceAdministrationService.queryData(resinformation);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
    }

    /**
     * 修改资源
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/updateFile")
    @ApiOperation("修改资源")
    public HttpResult<Boolean> updateFile(@ApiParam(value = "文件")  MultipartFile multipartFile,
                                          @ApiParam(value = "id", required = true) String id,
                                          @ApiParam(value = "资源名称", required = true) String name,
                                          @ApiParam(value = "资源类型", required = true) String type,
                                          @ApiParam(value = "资源描述", required = true) String description,
                                          @ApiParam(value = "系统类型Guid") String systemType) {
        String methodDescribe = getMethodDescribe("updateFile");
        Boolean flag = iResourceAdministrationService.updateFile(multipartFile, id, name, type, description, systemType);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }

    /**
     * 删除资源
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/deleteFile")
    @ApiOperation("删除资源")
    @ApiImplicitParam(name = "id",value = "id",required = true)
    public HttpResult<Boolean> deleteFile(@RequestParam("id") String id) {
        String methodDescribe = getMethodDescribe("deleteFile");
        Boolean flag = iResourceAdministrationService.deleteFile(id);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
    }

    /**
     * 下载资源
     */
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @GetMapping("/downloadFile")
    @ApiOperation("下载资源")
    @ApiImplicitParam(name = "id",value = "id",required = true)
    public HttpResult<Object> downloadFile(@RequestParam("id")String id,HttpServletResponse response) throws IOException {
        String methodDescribe = getMethodDescribe("downloadFile");
        iResourceAdministrationService.downloadFile(id,response);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "下载成功", methodDescribe);
    }

}
