package com.zd.laboratory.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.zd.common.core.domain.R;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.core.web.domain.ResultData;
import com.zd.common.swagger.config.Knife4jConfiguration;
import com.zd.laboratory.domain.LabConfig;
import com.zd.laboratory.service.ILabConfigService;
import com.zd.system.api.RemoteFileService;
import com.zd.system.api.domain.SysFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: zhoupan
 * @Date: 2021/11/11/14:12
 * @Description:
 */
@Api(tags = {"实验室配置"})
@ApiSupport(author = Knife4jConfiguration.Author.ZP)
@RestController
@RequestMapping("/labconfig")
public class LabConfigController {

    @Autowired
    ILabConfigService labConfigService;

    @Autowired
    private RemoteFileService remoteFileService;



    @ApiOperation("查询实验室项目配置")
    @GetMapping()
    public ResultData query()
    {
        return ResultData.success(labConfigService.queryConfig());
    }


    @ApiOperation("更新首页图")
    @PutMapping()
    public ResultData update(@RequestParam("file") MultipartFile file)
    {
        if (!file.isEmpty()) {
            R<SysFile> fileResult = remoteFileService.upload(file);
            if (StringUtils.isNull(fileResult) || StringUtils.isNull(fileResult.getData())) {
                return ResultData.fail("文件服务异常，请联系管理员");
            }
            String url = fileResult.getData().getUrl();
            LabConfig config = new LabConfig();
            config.setAppCover(url);
            if (labConfigService.updateConfig(config)) {
                return ResultData.success();
            }
        }
        return ResultData.fail("上传图片异常，请联系管理员");
    }



}
