package com.ruoyi.api.controller.common;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.FilePathSplicingType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.filepathsplicing.FilePathSplicing;
import com.ruoyi.common.filepathsplicing.FilePathSplicingUtil;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.service.ISysNoticeService;
import com.ruoyi.system.service.ISysOssService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * 公共管理
 *
 * @author Lion Li
 */
@Api(value = "公共", tags = {"公共管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("api/common")
public class CommonController extends AbstractApiController {

//    private final ICommonService commonService;
//    private final SmsSend smsSend;

    private final ISysOssService iSysOssService;

    private final ISysNoticeService noticeService;

    /**
     * 上传OSS对象存储
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("上传OSS对象存储")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "file", value = "文件", paramType = "query", dataTypeClass = File.class, required = true)
    })
    @RepeatSubmit()
    @PostMapping("/upload")
    public R<SysOss> upload(@RequestPart("file") MultipartFile file) {
        getUserId();
        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }
        SysOss oss = iSysOssService.apiUpload(file);
        return R.ok(oss);
    }

    /**
     * 获取公告列表
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("获取公告列表")
    @GetMapping("/notice/list")
    public R<List<SysNotice>> noticeList(SysNotice notice) {
        return R.ok(noticeService.selectNoticeList(notice));
    }

    /**
     * 获取公告列表
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("获取公告详情")
    @GetMapping("/notice/info/{noticeId}")
    public R<SysNotice> noticeDetail(@ApiParam("公告ID") @PathVariable Long noticeId) {
        SysNotice notice = noticeService.selectNoticeById(noticeId);
        return R.ok(notice);
    }

    @Resource
    private FilePathSplicingUtil filePathSplicingUtil;

//    /**
//     * 检查大文件
//     */
//    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
//    @ApiOperation("检查大文件")
//    @PostMapping("/check-big")
//    public R<SysOss> checkBig(@Validated(AddGroup.class) @RequestBody SysOssBigFileBo bo) {
//        SysOss oss = iSysOssService.checkBigFile(bo);
//        return R.ok(oss);
//    }

//    /**
//     * 上传OSS大文件分片
//     */
//    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
//    @ApiOperation("上传OSS大文件分片")
//    @PostMapping("/upload-tag")
//    public R uploadTag(@RequestPart(value = "file", required = true) MultipartFile file,
//                       @RequestParam(value = "fileMd5", required = true) String fileMd5,
//                       @RequestParam(value = "uploadId", required = true) String uploadId,
//                       @RequestParam(value = "curPartSize", required = true) Long curPartSize,
//                       @RequestParam(value = "partNumber", required = true) Integer partNumber) {
//        SysOssBigFilePartBo bo = new SysOssBigFilePartBo();
//        bo.setFile(file);
//        bo.setUploadId(uploadId);
//        bo.setFileMd5(fileMd5);
//        bo.setCurPartSize(curPartSize);
//        bo.setPartNumber(partNumber);
//        PartETag tag = iSysOssService.bigFileUploadTag(bo);
//        JSONObject resultJson = new JSONObject();
//        resultJson.put("tag", tag);
//        return R.ok(resultJson);
//    }

//    /**
//     * OSS大文件合并
//     */
//    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
//    @ApiOperation("OSS大文件合并")
//    @PostMapping("/marge-tag")
//    public R<SysOss> margeTag(@Validated(AddGroup.class) @RequestBody SysOssBigFileMargeBo bo) {
//        SysOss oss = iSysOssService.margeBigFile(bo);
//        return R.ok(oss);
//    }
//
//    /**
//     * 上传OSS大文件
//     */
//    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
//    @ApiOperation("上传OSS大文件")
//    @PostMapping("/upload-all")
//    public R<SysOss> uploadAll(@RequestPart(value = "file", required = true) MultipartFile file,
//                               @RequestParam(value = "fileMd5", required = true) String fileMd5,
//                               @RequestParam(value = "uploadId", required = true) String uploadId) {
//        SysOssBigFilePartBo bo = new SysOssBigFilePartBo();
//        bo.setFile(file);
//        bo.setUploadId(uploadId);
//        bo.setFileMd5(fileMd5);
//        SysOss sysOss = iSysOssService.bigFileUpload(bo);
//        return R.ok(sysOss);
//    }

//    /**
//     * 生成验证码
//     */
//    @ApiOperation("生成验证码")
//    @RepeatSubmit()
//    @PostMapping("/getVerfityCode")
//    public R<Void> getVerfityCode(@RequestBody String mobile) {
//        //发送验证码短信
//        boolean b = commonService.getVerfityCode(mobile);
//
//        if (b) {
//            return R.ok();
//        } else {
//            return R.fail();
//        }
//    }

//    /**
//     * 生成验证码
//     */
//    @ApiOperation("生成验证码")
//    @RepeatSubmit(interval = 10000)
//    @PostMapping("/sms-code")
//    public R<Void> smsCode(@RequestBody JSONObject paramJson) {
//        String mobile = paramJson.getString("mobile");
//        if (StrUtil.isEmpty(mobile)) {
//            throw new ServiceException("手机号不能为空");
//        }
//        smsSend.send(mobile);
//        return R.ok();
//    }

}
