package com.huatek.busi.controller;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.huatek.busi.common.util.FileUtils;
import com.huatek.busi.service.ISysFileService;
import com.huatek.busi.where.SysFileSelectVo;
import com.huatek.torch.frame.tools.ConstantUtil;
import com.huatek.torch.frame.tools.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Api(tags={"文件上传管理接口"})
@RestController
@RequestMapping("/api/sysFile")
public class SysFileController {

    @Reference(check = false)
    ISysFileService sysFileService;

    @ApiOperation(value = "查询文件列表",httpMethod="POST", notes= "查询文件列表", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/list", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> list(@RequestBody SysFileSelectVo sysFileSelectVo){
        return sysFileService.list(sysFileSelectVo);
    }

    @ApiOperation(value = "初次加载查询文件列表",httpMethod="POST", notes= "查询文件列表", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/firstList", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> firstList(@RequestBody SysFileSelectVo sysFileSelectVo){
        return sysFileService.list(sysFileSelectVo);
    }

    @ApiOperation(value = "附件查看",httpMethod="POST", notes= "查看附件", produces = "application/json;charset=utf-8", response= Map.class)
    @PostMapping(value = "/findListAnnex", produces = { "application/json;charset=utf-8" })
    public Map<String, Object> findListAnnex(@RequestBody SysFileSelectVo sysFileSelectVo){
        return sysFileService.findListAnnex(sysFileSelectVo);
    }

    @ApiOperation("附件文件下载")
    @GetMapping("/downloadFiles")
    public Map<String, Object> downloadFiles(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        String id = params.get("id").toString();
        Map<String, Object> map = sysFileService.downloadFiles(id);
        if(CollUtil.isNotEmpty(map)){
            if(map.get("filePath") == null && map.get("fileName") == null){
                return map;
            }
            String filePath = map.get("filePath").toString();
            String fileName = map.get("fileName").toString();
            String path = FileUtils.fileDownload(new File(filePath), fileName, response);
            if(Objects.equals(path, "文件下载失败，未找到您要下载的文件") || Objects.equals(path, "文件下载失败,未知错误")){
                return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "BUSINESS.FILE.DOWNLOAD.FAIL", "");
            }
            return ResultUtil.put(ConstantUtil.REQUEST_SUCCESS, "BUSINESS.FILE.DOWNLOAD.SUCCESS", path);
        }
        return ResultUtil.put(ConstantUtil.REQUEST_FAIL, "BUSINESS.FILE.DOWNLOAD.FAIL", "");
    }

    @ApiOperation("删除excel数据及附件记录")
    @PostMapping("/deleteFiles")
    public Map<String, Object> deleteFiles(@RequestParam("refIds") List<String> refIds, @RequestParam("equType") String equType) {
        return sysFileService.deleteFiles(refIds, equType);
    }
}
