package com.chushang.oss.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.core.web.Result;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.oss.entity.dto.OcrDTO;
import com.chushang.oss.entity.vo.FileSourceVo;
import com.chushang.oss.enums.OcrTypeEnum;
import com.chushang.oss.service.FileSourceService;
import com.chushang.oss.service.OcrService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/file")
public class FileController {

    @Resource
    FileSourceService fileSourceService;
    @Resource
    OcrService ocrService;

    /**
     * todo
     */
    @SysLog(value = "文件上传", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload")
    public AjaxResult uploadFile(@RequestParam(value = "file") MultipartFile file,
                                 @RequestParam(value = "ocrType", required = false) String ocrType) {
        FileSourceVo vo = BeanUtil.copyProperties(fileSourceService.addFile(file, "ip"), FileSourceVo.class);
        if (StrUtil.isNotEmpty(ocrType)) {
            OcrDTO ocr = new OcrDTO();
            OcrTypeEnum ocrTypeEnum = OcrTypeEnum.findByCode(ocrType);
            if (ocrTypeEnum != OcrTypeEnum.NONE) {
                ocr.setOcrType(ocrTypeEnum);
                ocr.setImgPath(vo.getFilePath());
                Result<JSONObject> ocrInfo = ocrService.ocr(ocr);
                if (ocrInfo.isSuccess()) {
                    vo.setOcr(ocrInfo.getData());
                }
            }
        }
        return AjaxResult.success(vo);
    }

    @SysLog(value = "文件下载", businessType = BusinessType.OTHER)
    @GetMapping(value = "/download/{fid}")
    public AjaxResult downloadFile(@PathVariable String fid) {
        return AjaxResult.success();
    }

    @SysLog(value = "文件", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/del/{fid}")
    public AjaxResult delFile() {
        return AjaxResult.success();
    }

}
