package com.fs.app.controller;

import com.fs.app.annotation.Login;
import com.fs.app.dto.TaskTypeDto;
import com.fs.common.annotation.Log;
import com.fs.common.annotation.RepeatSubmit;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.exception.file.OssException;
import com.fs.system.domain.SysConfig;
import com.fs.system.oss.CloudStorageService;
import com.fs.system.oss.OSSFactory;
import com.fs.system.service.ISysConfigService;
import com.fs.system.service.ISysDictDataService;
import com.fs.system.vo.DictVO;
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.List;
import java.util.Map;


@Api("公共接口")
@RestController
@RequestMapping(value="/app/common")
public class CommonController {

    @Autowired
    private ISysDictDataService iSysDictDataService;
    @Autowired
    private ISysConfigService iSysConfigService;


    @ApiOperation("测试")
    @GetMapping(value = "/test")
    @RepeatSubmit
    @Login
    public AjaxResult test()
    {
        return AjaxResult.success("第一个接口是打通了");
    }

    @ApiOperation("获取任务类型列表")
    @PostMapping("/getTaskTypeData")
    public AjaxResult getTaskTypeData(@RequestBody TaskTypeDto taskTypeDto) {
        List<DictVO> dictTypes = iSysDictDataService.selectDictDataListByType(taskTypeDto.getDictType());
        return AjaxResult.success(dictTypes);
    }

    @ApiOperation("获取首页轮播图列表")
    @PostMapping("/getIndexBannerList")
    public AjaxResult getIndexBannerList(String sysConfig) {
        SysConfig s = iSysConfigService.selectConfigByConfigKey(sysConfig);
        String[] array = s.getConfigValue().split(",");
        return AjaxResult.success(array);
    }



    @ApiOperation("图片，文章等小文件上传")
    @PostMapping("/uploadOSS")
    public R uploadOSS(@RequestParam("file") MultipartFile file) throws Exception
    {
        if (file.isEmpty())
        {
            throw new OssException("上传文件不能为空");
        }
        // 上传文件
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        CloudStorageService storage = OSSFactory.build();
        String url = storage.uploadSuffix(file.getBytes(), suffix);
        return R.ok().put("url",url);
    }

    @ApiOperation("获取是否显示视频")
    @GetMapping("/getShowVideo")
    public AjaxResult getShowVideo() {
        // 0-不显示视频 1-显示视频
        return AjaxResult.success(0);
    }


}
