package com.yida.data.system.controller;

import cn.hutool.core.util.StrUtil;
import com.yida.data.common.core.common.ModuleName;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.system.EduSysFile;
import com.yida.data.common.core.exception.FebsException;
import com.yida.data.log.annotation.OperationLog;
import com.yida.data.school.vo.smart.WxConfigVO;
import com.yida.data.system.service.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "通用接口")
@RestController
@RequestMapping("/common")
public class CommonController {

    @Resource
    private CommonService commonService;

    @ApiOperation("公共上传接口")
    @PostMapping("/upload")
    @OperationLog(module = ModuleName.SYSTEM, methods = "上传文件")
    public ResultBean upload(MultipartFile file,
        @ApiParam("限制大小(单位：MB)") @RequestParam(required = false) Integer maxSize) {
        if (maxSize != null) {
            if (maxSize * 1024 * 1024 < file.getSize()) {
                throw new FebsException(String.format("超过最大限制大小%dMB", maxSize));
            }
        }
        EduSysFile eduSysFile = commonService.upload(file);
        StringBuilder stringBuilder = new StringBuilder(eduSysFile.getUrl());
        if (StrUtil.isNotBlank(eduSysFile.getCoverUrl())) {
            stringBuilder.append(",")
                .append(eduSysFile.getCoverUrl());
        }
        return ResultBean.buildSuccess(stringBuilder.toString());
    }

    @GetMapping("/getWxJsConfig")
    @ApiOperation("获取企业微信JS-SDK配置")
    ResultBean<WxConfigVO> getWxJsConfig(
        @RequestParam @ApiParam(value = "部门id", required = true) Long deptId,
        @RequestParam @ApiParam(value = "当前网页的URL，不包含#及其后面部分", required = true) String url) {
        return ResultBean.buildSuccess(commonService.getWxJsConfig(deptId, url));
    }

    @GetMapping("/getWxPublicJsConfig")
    @ApiOperation("获取微信JS-SDK配置")
    ResultBean<WxConfigVO> getWxPublicJsConfig(
        @RequestParam @ApiParam(value = "部门id", required = true) Long deptId,
        @RequestParam @ApiParam(value = "当前网页的URL，不包含#及其后面部分", required = true) String url) {
        return ResultBean.buildSuccess(commonService.getWxPublicJsConfig(deptId, url));
    }
}
