package com.ho.filecenter.controller;

import com.ho.business.entity.Picture;
import com.ho.common.tools.annotation.LogAnnotation;
import com.ho.common.tools.annotation.TokenIgnore;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.constant.ContextConstant;
import com.ho.common.tools.constant.RedisKeyConstant;
import com.ho.common.tools.entity.SimpleUser;
import com.ho.common.tools.entity.UserDetailRespVO;
import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.CommonBytesUtil;
import com.ho.common.tools.util.MqttUtil;
import com.ho.common.tools.util.PageResult;
import com.ho.common.tools.util.TranslateUtils;
import com.ho.filecenter.entity.MediaFile;
import com.ho.filecenter.service.AnotherFileService;
import com.ho.filecenter.service.FileService;
import com.ho.filecenter.util.MqttConfigUtil;
import com.ho.filecenter.vo.mqtt.*;
import com.ho.filecenter.vo.resp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author fancl
 * @desc: 文件Controller
 * @date 2022/11/7
 */
@RestController
@RequestMapping(ContextConstant.MEDIA_CONTEXT + "file")
@Api(tags = "多媒体-文件模块")
@Slf4j
public class FileController {


    @Autowired
    RedisService redisService;

    @Autowired
    FileService fileService;

    @Autowired
    CommonBytesUtil bytesUtil;

    @Autowired
    AnotherFileService anotherFileService;

    @PostMapping("filePage")
    @ApiOperation(value = "分页查询文件")
    public DataResult<PageResult<MediaFile>> filePage(@RequestBody FilePageVO vo, HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        UserDetailRespVO userDetailRespVO = redisService.getUserDetailByToken(token);
        String userName = userDetailRespVO.getUsername();
        String userNameKey = redisService.getUserNameKey(userDetailRespVO, userName);
        SimpleUser user = (SimpleUser) redisService.get(userNameKey);
        PageResult<MediaFile> pageResult = fileService.page(vo, user);
        return DataResult.success(pageResult);
    }

    @PostMapping("fileUpload")
    @ApiOperation(value = "文件上传")
    public DataResult fileUpload(@RequestPart("file") MultipartFile file, HttpServletRequest request) {
        //判断文件是否为空
        if (file.isEmpty()) {
            throw new BusinessException(BaseResponseCode.FILE_IS_EMPTY);
        }
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        UserDetailRespVO userDetailRespVO = redisService.getUserDetailByToken(token);
        String userName = userDetailRespVO.getUsername();
        String userNameKey = redisService.getUserNameKey(userDetailRespVO, userName);
        SimpleUser user = (SimpleUser) redisService.get(userNameKey);
        fileService.fileUpload(file, user);
        return DataResult.success();
    }

    @PostMapping("fileIssued")
    @ApiOperation(value = "文件下发")
    public DataResult<HeartbeatResp> fileIssued(@RequestBody FileIssuedVO vo, HttpServletRequest request) {
        HeartbeatResp heartbeatResp = new HeartbeatResp();
        String msg = CommonConstant.Heartbeat.MSG + vo.getSerialNo();
        if (fileService.checkHeartbeat(vo.getSerialNo())) {
            log.info("文件下发超过5分钟没有检测到心跳");
            heartbeatResp.setHeartbeatStatus(CommonConstant.ZERO);
            heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.FAIL);
            return DataResult.success(heartbeatResp);
        } else {
            heartbeatResp.setMsg(msg + CommonConstant.Heartbeat.SUCCESS);
            heartbeatResp.setHeartbeatStatus(CommonConstant.ONE);
            if (null == vo.getDestinationAddress()) {
                DataResult<HeartbeatResp> dataResult = new DataResult<>();
                dataResult.setMsg(BaseResponseCode.PARAM_CHECK_FAIL.getMsg());
                dataResult.setData(heartbeatResp);
                return dataResult;
            }
            String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
            UserDetailRespVO userDetailRespVO = redisService.getUserDetailByToken(token);
            String userName = userDetailRespVO.getUsername();
            String userNameKey = redisService.getUserNameKey(userDetailRespVO, userName);
            SimpleUser user = (SimpleUser) redisService.get(userNameKey);
            log.info("文件准备开始下发");
            fileService.fileIssued(vo, user);
            return DataResult.success(heartbeatResp);
        }
    }

    @PostMapping("progressBar")
    @ApiOperation(value = "文件下发进度")
    public DataResult<FileProcessRespVO> progressBar(@RequestBody FileProcessReqVO vo, HttpServletRequest request) {
        String fileSendAllKey = vo.getStationId() + ":" + RedisKeyConstant.FILE_SEND_ALL;
        FileProcessRespVO fileProcessRespVO = fileService.getFileProcess(vo.getStationId(), fileSendAllKey);
        return DataResult.success(fileProcessRespVO);
    }

    @PostMapping("deleteFiles")
    @ApiOperation(value = "删除文件")
    @LogAnnotation(title = "文件服务", action = "删除文件")
    public DataResult fileIssued(@RequestBody List<Long> ids) {
        fileService.deleteFiles(ids);
        return DataResult.success();
    }

    @PostMapping("readFile")
    @ApiOperation(value = "读取文件")
    public DataResult readFile(@RequestBody FileIssuedVO vo, HttpServletRequest request) {
        String token = request.getHeader(RedisKeyConstant.User.ACCESS_TOKEN);
        UserDetailRespVO userDetailRespVO = redisService.getUserDetailByToken(token);
        String userName = userDetailRespVO.getUsername();
        String userNameKey = redisService.getUserNameKey(userDetailRespVO, userName);
        SimpleUser user = (SimpleUser) redisService.get(userNameKey);
        fileService.readFile(vo, user);
        return DataResult.success();
    }

    @PostMapping("readFileAttribute")
    @ApiOperation(value = "读取文件属性")
    public DataResult<FileAttributeResp> readFileAttribute(@RequestBody FileAttributeReqVO vo, HttpServletRequest request) {
        String lang = request.getHeader(CommonConstant.LANG);
        FileAttributeResp fileAttributeResp = null;
        String msg = CommonConstant.Heartbeat.MSG + vo.getSerialNo();
        if (fileService.checkHeartbeat(vo.getSerialNo())) {
            fileAttributeResp = new FileAttributeResp();
            fileAttributeResp.setIsEnd(CommonConstant.TWO);
            fileAttributeResp.setMsg(msg + CommonConstant.Heartbeat.FAIL);
            fileAttributeResp.setResponse(CommonConstant.NEGATIV_ONE);
            fileAttributeResp.setList(new ArrayList<>());
            if(CommonConstant.langTemp.EN_US.equals(lang)){
                fileAttributeResp.setMsg(TranslateUtils.translate(fileAttributeResp.getMsg(),CommonConstant.lang.EN));
            }else if (CommonConstant.langTemp.FR_FR.equals(lang)){
                fileAttributeResp.setMsg(TranslateUtils.translate(fileAttributeResp.getMsg(),CommonConstant.lang.FRA));
            }else if (CommonConstant.langTemp.ES_ES.equals(lang)){
                fileAttributeResp.setMsg(TranslateUtils.translate(fileAttributeResp.getMsg(),CommonConstant.lang.SPA));
            }else if (CommonConstant.langTemp.DE_DE.equals(lang)){
                fileAttributeResp.setMsg(TranslateUtils.translate(fileAttributeResp.getMsg(),CommonConstant.lang.DE));
            }else if (CommonConstant.langTemp.PL_PL.equals(lang)){
                fileAttributeResp.setMsg(TranslateUtils.translate(fileAttributeResp.getMsg(),CommonConstant.lang.PL));
            }else if (CommonConstant.langTemp.AR_EG.equals(lang)){
                fileAttributeResp.setMsg(TranslateUtils.translate(fileAttributeResp.getMsg(),CommonConstant.lang.ARA));
            }
            return DataResult.success(fileAttributeResp);
        }
        fileAttributeResp = fileService.getFileAttribute(vo);
        if(CommonConstant.langTemp.EN_US.equals(lang)){
            fileAttributeResp.setMsg(TranslateUtils.translate(fileAttributeResp.getMsg(),CommonConstant.lang.EN));
        }else if (CommonConstant.langTemp.FR_FR.equals(lang)){
            fileAttributeResp.setMsg(TranslateUtils.translate(fileAttributeResp.getMsg(),CommonConstant.lang.FRA));
        }else if (CommonConstant.langTemp.ES_ES.equals(lang)){
            fileAttributeResp.setMsg(TranslateUtils.translate(fileAttributeResp.getMsg(),CommonConstant.lang.SPA));
        }else if (CommonConstant.langTemp.DE_DE.equals(lang)){
            fileAttributeResp.setMsg(TranslateUtils.translate(fileAttributeResp.getMsg(),CommonConstant.lang.DE));
        }else if (CommonConstant.langTemp.PL_PL.equals(lang)){
            fileAttributeResp.setMsg(TranslateUtils.translate(fileAttributeResp.getMsg(),CommonConstant.lang.PL));
        }else if (CommonConstant.langTemp.AR_EG.equals(lang)){
            fileAttributeResp.setMsg(TranslateUtils.translate(fileAttributeResp.getMsg(),CommonConstant.lang.ARA));
        }
        return DataResult.success(fileAttributeResp);
    }

    @PostMapping("deleteDeviceFiles")
    @ApiOperation(value = "删除文件(删除边端文件)")
    @LogAnnotation(title = "文件服务", action = "删除文件(删除边端文件)")
    public DataResult<FileDeleteResp> deleteDeviceFiles(@RequestBody FileForDeviceReqVO fileDeleteReqVO, HttpServletRequest request) {
        String lang = request.getHeader(CommonConstant.LANG);
        FileDeleteResp fileDeleteResp = null;
        String msg = CommonConstant.Heartbeat.MSG + fileDeleteReqVO.getSerialNo();
        if (fileService.checkHeartbeat(fileDeleteReqVO.getSerialNo())) {
            fileDeleteResp = new FileDeleteResp();
            fileDeleteResp.setDeleteStatus(CommonConstant.TWO);
            fileDeleteResp.setMsg(msg + CommonConstant.Heartbeat.FAIL);
            if(CommonConstant.langTemp.EN_US.equals(lang)){
                fileDeleteResp.setMsg(TranslateUtils.translate(fileDeleteResp.getMsg(),CommonConstant.lang.EN));
            }else if (CommonConstant.langTemp.FR_FR.equals(lang)){
                fileDeleteResp.setMsg(TranslateUtils.translate(fileDeleteResp.getMsg(),CommonConstant.lang.FRA));
            }else if (CommonConstant.langTemp.ES_ES.equals(lang)){
                fileDeleteResp.setMsg(TranslateUtils.translate(fileDeleteResp.getMsg(),CommonConstant.lang.SPA));
            }else if (CommonConstant.langTemp.DE_DE.equals(lang)){
                fileDeleteResp.setMsg(TranslateUtils.translate(fileDeleteResp.getMsg(),CommonConstant.lang.DE));
            }else if (CommonConstant.langTemp.PL_PL.equals(lang)){
                fileDeleteResp.setMsg(TranslateUtils.translate(fileDeleteResp.getMsg(),CommonConstant.lang.PL));
            }else if (CommonConstant.langTemp.AR_EG.equals(lang)){
                fileDeleteResp.setMsg(TranslateUtils.translate(fileDeleteResp.getMsg(),CommonConstant.lang.ARA));
            }
            return DataResult.success(fileDeleteResp);
        }

        fileDeleteResp = fileService.deleteDeviceFiles(fileDeleteReqVO);

        if(CommonConstant.langTemp.EN_US.equals(lang)){
            fileDeleteResp.setMsg(TranslateUtils.translate(fileDeleteResp.getMsg(),CommonConstant.lang.EN));
        }else if (CommonConstant.langTemp.FR_FR.equals(lang)){
            fileDeleteResp.setMsg(TranslateUtils.translate(fileDeleteResp.getMsg(),CommonConstant.lang.FRA));
        }else if (CommonConstant.langTemp.ES_ES.equals(lang)){
            fileDeleteResp.setMsg(TranslateUtils.translate(fileDeleteResp.getMsg(),CommonConstant.lang.SPA));
        }else if (CommonConstant.langTemp.DE_DE.equals(lang)){
            fileDeleteResp.setMsg(TranslateUtils.translate(fileDeleteResp.getMsg(),CommonConstant.lang.DE));
        }else if (CommonConstant.langTemp.PL_PL.equals(lang)){
            fileDeleteResp.setMsg(TranslateUtils.translate(fileDeleteResp.getMsg(),CommonConstant.lang.PL));
        }else if (CommonConstant.langTemp.AR_EG.equals(lang)){
            fileDeleteResp.setMsg(TranslateUtils.translate(fileDeleteResp.getMsg(),CommonConstant.lang.ARA));
        }
        return DataResult.success(fileDeleteResp);
    }

    @PostMapping("fileUploadForDevice")
    @ApiOperation(value = "文件上传(向边端上传)")
    //@HzPermission(value = PermissionConstant.REMOTECONTROL_FILE_UPLOAD)
    @LogAnnotation(title = "文件服务", action = "文件上传(向边端上传)")
    public DataResult<HeartbeatResp> fileUploadForDevice(HttpServletRequest request,@RequestPart("file") MultipartFile file, @RequestParam("stationId") Integer stationId, @RequestParam("serialNo") String serialNo, @RequestParam("filePath") String filePath) {
        String lang = request.getHeader(CommonConstant.LANG);
        HeartbeatResp resp = fileService.checkHeartAndFileUsing(stationId, serialNo);
        if (null == resp) {
            resp = new HeartbeatResp();
            //文件为空，不给上传
            if (file.isEmpty()) {
                resp.setHeartbeatStatus(CommonConstant.ZERO);
                resp.setMsg(BaseResponseCode.FILE_IS_EMPTY.getMsg());
                if(CommonConstant.langTemp.EN_US.equals(lang)){
                    resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.EN));
                }else if (CommonConstant.langTemp.FR_FR.equals(lang)){
                    resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.FRA));
                }else if (CommonConstant.langTemp.ES_ES.equals(lang)){
                    resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.SPA));
                }else if (CommonConstant.langTemp.DE_DE.equals(lang)){
                    resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.DE));
                }else if (CommonConstant.langTemp.PL_PL.equals(lang)){
                    resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.PL));
                }else if (CommonConstant.langTemp.AR_EG.equals(lang)){
                    resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.ARA));
                }
                return DataResult.success(resp);
            }
            resp.setMsg(CommonConstant.Heartbeat.MSG + serialNo + CommonConstant.Heartbeat.SUCCESS);
            resp.setHeartbeatStatus(CommonConstant.ONE);
            log.info("文件上传(向边端上传)开始上传");
            fileService.fileUploadForDevice(file, stationId, serialNo, filePath);
        }
        if(CommonConstant.langTemp.EN_US.equals(lang)){
            resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.EN));
        }else if (CommonConstant.langTemp.FR_FR.equals(lang)){
            resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.FRA));
        }else if (CommonConstant.langTemp.ES_ES.equals(lang)){
            resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.SPA));
        }else if (CommonConstant.langTemp.DE_DE.equals(lang)){
            resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.DE));
        }else if (CommonConstant.langTemp.PL_PL.equals(lang)){
            resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.PL));
        }else if (CommonConstant.langTemp.AR_EG.equals(lang)){
            resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.ARA));
        }
        return DataResult.success(resp);
    }


    @PostMapping("downloadFromDevice")
    @ApiOperation(value = "文件下载(从边端下载到浏览器)")
    @LogAnnotation(title = "文件服务", action = "文件下载")
    public DataResult<HeartbeatResp> downloadFromDevice(@RequestBody FileForDeviceReqVO fileForDeviceReqVO,HttpServletRequest request) {
        //todo  测试模拟返回成功
        String lang = request.getHeader(CommonConstant.LANG);
        HeartbeatResp resp = fileService.checkHeartAndFileUsing(fileForDeviceReqVO.getStationId(), fileForDeviceReqVO.getSerialNo());
        if (null == resp) {
            resp = new HeartbeatResp();
            resp.setMsg(CommonConstant.Heartbeat.MSG + fileForDeviceReqVO.getSerialNo() + CommonConstant.Heartbeat.SUCCESS);
            resp.setHeartbeatStatus(CommonConstant.ONE);
            log.info("文件下载(从边端下载到云端)开始下载");
            fileService.downloadFromDevice(fileForDeviceReqVO);
            fileService.downloadFromDevice(fileForDeviceReqVO);
        }
        if(CommonConstant.langTemp.EN_US.equals(lang)){
            resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.EN));
        }else if (CommonConstant.langTemp.FR_FR.equals(lang)){
            resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.FRA));
        }else if (CommonConstant.langTemp.ES_ES.equals(lang)){
            resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.SPA));
        }else if (CommonConstant.langTemp.DE_DE.equals(lang)){
            resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.DE));
        }else if (CommonConstant.langTemp.PL_PL.equals(lang)){
            resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.PL));
        }else if (CommonConstant.langTemp.AR_EG.equals(lang)){
            resp.setMsg(TranslateUtils.translate(resp.getMsg(),CommonConstant.lang.ARA));
        }
        return DataResult.success(resp);
    }

    @PostMapping("downloadProgressBar")
    @ApiOperation(value = "文件下载进度(从边端下载到浏览器)")
    public DataResult<FileProcessRespVO> downloadProgressBar(@RequestBody FileProcessReqVO vo, HttpServletResponse response,HttpServletRequest request) {
        //todo  测试模拟返回成功
        String lang = request.getHeader(CommonConstant.LANG);
        FileProcessRespVO fileProcessRespVO = fileService.getDownloadProgress(vo);
        if(CommonConstant.langTemp.EN_US.equals(lang)){
            fileProcessRespVO.setMsg(TranslateUtils.translate(fileProcessRespVO.getMsg(),CommonConstant.lang.EN));
        }else if (CommonConstant.langTemp.FR_FR.equals(lang)){
            fileProcessRespVO.setMsg(TranslateUtils.translate(fileProcessRespVO.getMsg(),CommonConstant.lang.FRA));
        }else if (CommonConstant.langTemp.ES_ES.equals(lang)){
            fileProcessRespVO.setMsg(TranslateUtils.translate(fileProcessRespVO.getMsg(),CommonConstant.lang.SPA));
        }else if (CommonConstant.langTemp.DE_DE.equals(lang)){
            fileProcessRespVO.setMsg(TranslateUtils.translate(fileProcessRespVO.getMsg(),CommonConstant.lang.DE));
        }else if (CommonConstant.langTemp.PL_PL.equals(lang)){
            fileProcessRespVO.setMsg(TranslateUtils.translate(fileProcessRespVO.getMsg(),CommonConstant.lang.PL));
        }else if (CommonConstant.langTemp.AR_EG.equals(lang)){
            fileProcessRespVO.setMsg(TranslateUtils.translate(fileProcessRespVO.getMsg(),CommonConstant.lang.ARA));
        }
        return DataResult.success(fileProcessRespVO);
    }


    @PostMapping("downloadFromDeviceForWeb")
    @ApiOperation(value = "文件下载(从边端下载到浏览器准备完成,浏览器已经可以下载)")
    public void downloadFromDeviceForWeb(@RequestBody FileProcessReqVO vo, HttpServletResponse response) {
        fileService.downloadFile(vo, response);
    }

    @PostMapping("downloadFile")
    @ApiOperation(value = "文件下载(前端分片，从边端下载到浏览器准备完成,浏览器已经可以下载)")
    public void downloadFile(@RequestBody FileProcessReqVO vo,HttpServletRequest request, HttpServletResponse response) {
        fileService.downloadFile(vo,request, response);
    }

    @PostMapping("cancelDownload")
    @ApiOperation("取消下载")
    public DataResult cancelDownload(@RequestBody FileProcessReqVO vo) {
        vo.setCancelStatus(CommonConstant.ZERO);
        vo.setFileStatus(CommonConstant.ONE);
        fileService.cancelDownloadFromDevice(vo);
        return DataResult.success();
    }

    @PostMapping("stopDownload")
    @ApiOperation("暂停下载")
    public DataResult stopDownload(@RequestBody FileProcessReqVO vo) {
        vo.setCancelStatus(CommonConstant.ONE);
        vo.setFileStatus(CommonConstant.ONE);
        fileService.cancelDownloadFromDevice(vo);
        return DataResult.success();
    }

    @PostMapping("cancelUpLoad")
    @ApiOperation("取消上传")
    public DataResult cancelUpLoad(@RequestBody FileProcessReqVO vo) {
        vo.setCancelStatus(CommonConstant.ZERO);
        vo.setFileStatus(CommonConstant.ZERO);
        fileService.cancelDownloadFromDevice(vo);
        return DataResult.success();
    }

    @PostMapping("stopUpLoad")
    @ApiOperation("暂停上传")
    public DataResult stopUpLoad(@RequestBody FileProcessReqVO vo) {
        vo.setCancelStatus(CommonConstant.ONE);
        vo.setFileStatus(CommonConstant.ZERO);
        fileService.cancelDownloadFromDevice(vo);
        return DataResult.success();
    }

    @PostMapping("fileUploadForDeviceList")
    @ApiOperation(value = "文件批量下发")
    public DataResult<HeartbeatResp> fileUploadForDeviceList(@RequestBody FileBatchSendReq vo) {
        List<FileBatchSendDetailReq> list = vo.getList();
        StringBuffer str = new StringBuffer();
        //判断设备有没有sn
        for (FileBatchSendDetailReq f : list) {
            if (null == f.getSerialNo()) {
                str.append(f.getDeviceName()).append(",");
            }
        }
        HeartbeatResp heartbeatResp = new HeartbeatResp();
        if (str.length() > 0) {//若有设备没有配置sn,则不能下发
            heartbeatResp.setHeartbeatStatus(CommonConstant.ZERO);
            heartbeatResp.setMsg(str.toString() + CommonConstant.NOT_SN);
            return DataResult.success(heartbeatResp);
        }
        Map<String, List<FileBatchSendDetailReq>> mapList = list.stream().collect(Collectors.groupingBy(s -> s.getSerialNo()));
        for (Map.Entry<String, List<FileBatchSendDetailReq>> m : mapList.entrySet()) {
            HeartbeatResp resp = fileService.checkHeartAndFileUsing(m.getValue().get(0).getStationId(), m.getKey());
            if (resp != null) {
                return DataResult.success(resp);
            }
        }
        heartbeatResp.setMsg(CommonConstant.Heartbeat.MSG + CommonConstant.Heartbeat.SUCCESS);
        heartbeatResp.setHeartbeatStatus(CommonConstant.ONE);
        log.info("准备文件批量下发");
        fileService.fileUploadForDeviceList(vo);
        return DataResult.success(heartbeatResp);
    }

    @PostMapping("batchProgressBar")
    @ApiOperation(value = "文件批量下发进度")
    public DataResult<FileBatchProcessResp> batchProgressBar(@RequestBody FileBatchProcessReqVO vo) {
        FileBatchProcessResp fileBatchProcessResp = fileService.getFileBatchProcessResp(vo);
        return DataResult.success(fileBatchProcessResp);
    }

    @PostMapping("modifyDeviceFiles")
    @ApiOperation(value = "修改文件(文件夹)属性")
    public DataResult<FileModifyResp> modifyDeviceFiles(@RequestBody FileForDeviceReqVO fileDeleteReqVO) {
        FileModifyResp fileModifyResp = null;
        String msg = CommonConstant.Heartbeat.MSG + fileDeleteReqVO.getSerialNo();
        if (fileService.checkHeartbeat(fileDeleteReqVO.getSerialNo())) {
            fileModifyResp = new FileModifyResp();
            fileModifyResp.setModifyStatus(CommonConstant.TWO);
            fileModifyResp.setMsg(msg + CommonConstant.Heartbeat.FAIL);
            return DataResult.success(fileModifyResp);
        }
        fileModifyResp = fileService.modifyDeviceFiles(fileDeleteReqVO);
        return DataResult.success(fileModifyResp);
    }

    @PostMapping("splitDownloadFromDeviceForWeb")
    @ApiOperation(value = "文件下载(分片下载)")
    public void splitDownloadFromDeviceForWeb(@RequestBody FileSplitDownloadReq vo, HttpServletResponse response) {
        fileService.splitDownloadFromDeviceForWeb(vo, response);
    }

    @PostMapping("fileUploadForStation")
    @ApiOperation(value = "文件上传")
    //@HzPermission(value = PermissionConstant.REMOTECONTROL_FILE_UPLOAD)
    @TokenIgnore
    public DataResult<HeartbeatResp> fileUploadForStation(@RequestPart("file") MultipartFile file, @RequestParam("stationId") Integer stationId, @RequestParam("serialNo") String serialNo, @RequestParam("filePath") String filePath) {
        HeartbeatResp resp = fileService.checkHeartAndFileUsing(stationId, serialNo);
        if (null == resp) {
            resp = new HeartbeatResp();
            //文件为空，不给上传
            if (file.isEmpty()) {
                resp.setHeartbeatStatus(CommonConstant.ZERO);
                resp.setMsg(BaseResponseCode.FILE_IS_EMPTY.getMsg());
                return DataResult.success(resp);
            }
//            long fileSize = file.getSize();
//            if (5 * 1024 * 1024 < fileSize) {
//                throw new BusinessException(BaseResponseCode.FILE_IS_EXCEED_5M);
//            }
            resp.setMsg(CommonConstant.Heartbeat.MSG + serialNo + CommonConstant.Heartbeat.SUCCESS);
            resp.setHeartbeatStatus(CommonConstant.ONE);
            log.info("文件上传(向边端上传)开始上传");
            fileService.fileUploadForDevice(file, stationId, serialNo, filePath);
        }
        return DataResult.success(resp);
    }

    @PostMapping("chargeFilePermission")
    @ApiOperation(value = "修改文件权限")
    public DataResult<HeartbeatResp> chargeFilePermission(@RequestBody FilePermissionReq vo) {
        HeartbeatResp heartbeatResp = fileService.chargeFilePermission(vo);
        return DataResult.success(heartbeatResp);
    }


    @PostMapping("chargeFilePermissionProgressBar")
    @ApiOperation(value = "修改文件权限下发进度")
    public DataResult<FilePermissionResp> chargeFilePermissionProgressBar(@RequestBody FilePermissionReq vo) {
        FilePermissionResp filePermissionResp = fileService.chargeFilePermissionProgressBar(vo);
        return DataResult.success(filePermissionResp);
    }

    @PostMapping("uploadFileToMinIo")
    @ApiOperation(value = "将文件传入MinIo")
    public DataResult<Picture> uploadFileToMinIo(@RequestBody FileForDeviceReqVO vo, HttpServletRequest request) {
        Picture picture = fileService.uploadFileToMinIo(vo, request);
        return DataResult.success(picture);
    }
}
