package com.fs.chat.controller;

import cn.hutool.json.JSONUtil;
import com.fs.ai.service.IBaiduAIService;
import com.fs.chat.config.QwConfig;
import com.fs.chat.param.ChatUploadFileCParam;
import com.fs.common.config.FSConfig;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.R;
import com.fs.common.exception.file.OssException;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.file.FileUploadUtils;
import com.fs.common.utils.security.OssUploadSecurityUtils;
import com.fs.company.domain.CompanyConfig;
import com.fs.company.service.ICompanyConfigService;
import com.fs.framework.security.LoginUser;
import com.fs.system.oss.CloudStorageService;
import com.fs.system.oss.OSSFactory;
import com.fs.wx.kf.dto.WeixinKfGetFileDTO;
import com.fs.wx.kf.dto.WeixinKfUploadDTO;
import com.fs.wx.kf.service.IWeixinKfService;
import com.fs.wx.kf.vo.WeixinKfUploadVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/chat/upload")
public class ChatUploadController extends BaseController
{
    @Autowired
    private ICompanyConfigService companyConfigService;
    @Autowired
    private IWeixinKfService weixinKfService;
    @Autowired
    private IBaiduAIService baiduAIService;
    @PostMapping("/uploadBaiduDoc")
    public R uploadBaiduDoc(@RequestParam("file") MultipartFile file,@RequestParam("datasetId") String datasetId) throws Exception
    {
        if (file.isEmpty())
        {
            throw new OssException("上传文件不能为空");
        }
        // 上传文件路径
        String filePath = FSConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileName = FileUploadUtils.upload(filePath, file);
        String url =filePath + fileName.replaceAll("/profile/upload","");
        // 上传文件
        String[] documentIds =baiduAIService.addDocument(datasetId,url);
        return R.ok().put("data",documentIds);
    }


    @PostMapping("/uploadFile/{companyId}")
    public R uploadFile(@PathVariable("companyId")Long companyId,@RequestParam("media") MultipartFile file) throws Exception
    {

        CompanyConfig config=companyConfigService.selectCompanyConfigByKey(companyId,"qw:config");
        QwConfig qwConfig= JSONUtil.toBean(config.getConfigValue(), QwConfig.class);

        if (file.isEmpty())
        {
            throw new OssException("上传文件不能为空");
        }
        WeixinKfUploadDTO dto=new WeixinKfUploadDTO();
        dto.setFile(file);
        dto.setType("image");
        WeixinKfUploadVO vo= weixinKfService.uploadFile(qwConfig.getCorpId(),qwConfig.getSecret(),dto);
        WeixinKfGetFileDTO fileDTO=new WeixinKfGetFileDTO();
        fileDTO.setMedia_id(vo.getMedia_id());
        String url=weixinKfService.getFile(qwConfig.getCorpId(),qwConfig.getSecret(),fileDTO);
        vo.setUrl(url);
        // 上传文件（后缀白名单校验）
        String suffix = OssUploadSecurityUtils.validateAndGetDotSuffix(file);
        CloudStorageService storage = OSSFactory.build();
        String ossUrl = storage.uploadSuffix(file.getBytes(), suffix);
        return R.ok().put("data",vo).put("ossUrl",ossUrl);
    }
    @PostMapping("/getUrl/{companyId}")
    public R getUrl(@PathVariable("companyId")Long companyId, @RequestBody ChatUploadFileCParam param)
    {
        CompanyConfig config=companyConfigService.selectCompanyConfigByKey(companyId,"qw:config");
        QwConfig qwConfig= JSONUtil.toBean(config.getConfigValue(), QwConfig.class);

        WeixinKfGetFileDTO dto=new WeixinKfGetFileDTO();
        dto.setMedia_id(param.getMediaId());
        String url=weixinKfService.getFile(qwConfig.getCorpId(),qwConfig.getSecret(),dto);
        return R.ok().put("data",url);
    }
}
