package cn.iocoder.yudao.module.base.controller.app;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.infra.controller.app.file.vo.AppFileUploadReqVO;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import cn.iocoder.yudao.module.jx.service.DrivePayService;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@RestController
@RequestMapping("/small-upload")
public class SmallProgramSwiperController {

    @Resource
    private FileService fileService;

    @Autowired
    private DrivePayService drivePayService;

    @PostMapping({"/common/uploadImg","/common/upload"})
    @Operation(summary = "上传文件")
    @TenantIgnore
    public CommonResult uploadFile(AppFileUploadReqVO uploadReqVO) throws Exception {
        MultipartFile file = uploadReqVO.getFile();
        String path = uploadReqVO.getPath();
        String url = fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream()));

        Map<String,Object> map = new HashMap<>();
        map.put("url", url);
        map.put("fileName", file.getName());
        map.put("newFileName", FileUtil.getName(url));
        map.put("originalFilename", file.getOriginalFilename());

        return success(map);
    }

    @PostMapping(value = "/idOcr")
    public CommonResult idOcr(@RequestParam("imagePath") String imagePath) throws TencentCloudSDKException {
        return success(drivePayService.infoCardOCR(imagePath));
    }
}
