package com.hzya.frame.webapp.entrance.controler;

import com.hzya.frame.sys.entity.EsbReturnEntity;
import com.hzya.frame.sys.file.download.entity.FileDownloadEntity;
import com.hzya.frame.sys.file.download.service.IFileDownloadService;
import com.hzya.frame.sys.file.upload.entity.FileResultEntity;
import com.hzya.frame.sys.file.upload.entity.FileUploadDto;
import com.hzya.frame.sysnew.application.service.ISysApplicationService;
import com.hzya.frame.sysnew.messageManageLog.entity.SysMessageManageLogEntity;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import com.hzya.frame.web.exception.BaseSystemException;
import com.hzya.frame.webapp.entrance.service.IEntranceService;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @Content程序唯一入口controller
 * @Author 👻👻👻👻👻👻👻👻👻👻 gjh
 * @Date 2022-12-07 8:46
 * @Version 1.0
 */
@RestControllerAdvice
@RestController
@RequestMapping("/entranceController")
public class EntranceController {
    private final Logger logger = LoggerFactory.getLogger(EntranceController.class);
    @Value("${savefile.path}")
    public String DSK;
    @Autowired
    @Qualifier(value = "entranceServiceImpl")
    private IEntranceService entranceService;
    @Autowired
    protected IFileDownloadService filedownloadService;
    @Resource
    protected ISysApplicationService sysApplicationService;
    @RequestMapping(value = "/option")
    @ResponseBody
    public JsonResultEntity option(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        return entranceService.doBusiness(servletRequest, servletResponse);
    }
    //平台接口
    @RequestMapping(value = "/platformInterface")
    @ResponseBody
    public Object centerInterFace(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        return entranceService.platformInterface(servletRequest, servletResponse);
    }

    /***
     * 文件上传接口 /entranceController/fileUpload
     * @param
     * @return@@
     */
    @RequestMapping(value = "/option/fileUpload", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity fileUpload(MultipartFile file, FileResultEntity entity, ServletRequest servletRequest, ServletResponse servletResponse) {
        JsonResultEntity jsonResultEntity = null;
        logger.info("-------------------开始调用上传文件upload接口-------------------");
        try {
            jsonResultEntity = entranceService.fileUpload(file, entity,servletRequest,servletResponse);
            entity.setSuccess(true);
        } catch (Exception e) {
            entity.setFileFlag(false);
            logger.error("文件上传失败"+e.getMessage());
        }
        logger.info("-------------------结束调用上传文件upload接口-------------------");

        return jsonResultEntity;
    }

    /***
     * 文件上传接口
     * @param
     * @return@@
     */
    @RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity fileUpload(MultipartFile file, FileUploadDto entity, ServletRequest servletRequest, ServletResponse servletResponse) {
        JsonResultEntity jsonResultEntity = null;
        logger.info("-------------------开始调用上传文件upload接口-------------------");
        try {
            jsonResultEntity = entranceService.fileUpload(file, entity,servletRequest,servletResponse);
        } catch (Exception e) {
            jsonResultEntity = BaseResult.getFailureMessageEntity(e.getMessage());
        }
        logger.info("-------------------结束调用上传文件upload接口-------------------");
        return jsonResultEntity;
    }
    /***
     * 文件上传接口
     * @param
     * @return@@
     */
    @RequestMapping(value = "/fileUploadlist", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity fileUploadlist(MultipartFile[] file, FileUploadDto entity, ServletRequest servletRequest, ServletResponse servletResponse) {
        entity.getBusinessType();
        return BaseResult.getSuccessMessageEntity("gc");
    }
    @RequestMapping(value = "/pluginfileUpload", method = RequestMethod.POST)
    @ResponseBody
    public JsonResultEntity pluginfileUpload(MultipartFile file, FileUploadDto entity,String pluginPackageName, ServletRequest servletRequest, ServletResponse servletResponse) {
        JsonResultEntity jsonResultEntity = null;
        logger.info("-------------------开始调用上传插件附件upload接口-------------------");
        try {
            jsonResultEntity = entranceService.pluginfileUpload(file, entity,pluginPackageName,servletRequest,servletResponse);
        } catch (Exception e) {
            jsonResultEntity = BaseResult.getFailureMessageEntity(e.getMessage());
        }
        logger.info("-------------------结束调用上传插件附件upload接口-------------------");
        return jsonResultEntity;
    }

    @ResponseBody
    @RequestMapping(value = "/pluginZipUpdate")
    public JsonResultEntity pluginZipUpate(FileUploadDto fileUploadDto){
        JsonResultEntity jsonResultEntity = null;
        logger.info("-------------------开始调用插件更新接口-------------------");
        try{
            jsonResultEntity = entranceService.pluginZipUpate(fileUploadDto);
        } catch (Exception e) {
            jsonResultEntity = BaseResult.getFailureMessageEntity(e.getMessage());
        }
        logger.info("-------------------结束调用插件更新接口------------------");
        return jsonResultEntity;
    }



            /**
             * 根据文件保存的新名称下载文件
             *
             * @param fileSaveName
             * @return
             */
    @RequestMapping(value = "/option/fileDownload")
    public ResponseEntity<byte[]> fileDownload(String fileSaveName) {
        ResponseEntity<byte[]> responseEntity = null;
        try {
            HttpHeaders headers = new HttpHeaders();
            FileDownloadEntity fileDownloadEntity = filedownloadService.fileDownload(fileSaveName);
            String fileName = fileDownloadEntity.getFilename();

            fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), "iso8859-1");

            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", fileName);

            responseEntity = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(fileDownloadEntity.getFile()), headers, HttpStatus.CREATED);
        } catch (Exception e) {
            logger.error("文件下载失败"+e.getMessage());
        }
        return responseEntity;
    }
    /**
     * 根据文件保存的新名称下载文件
     *
     * @return
     */
    @RequestMapping(value = "/fileDownloadNew")
    public ResponseEntity<byte[]> fileDownloadNew(String id) throws Exception  {
        ResponseEntity<byte[]> responseEntity = null;
        try {
            HttpHeaders headers = new HttpHeaders();
            FileUploadDto fileUploadDto = new FileUploadDto();
            fileUploadDto.setId(id);
            FileDownloadEntity fileDownloadEntity = filedownloadService.fileDownload(fileUploadDto);
            String fileName = fileDownloadEntity.getFilename();
            fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), "iso8859-1");
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", fileName);
            responseEntity = new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(fileDownloadEntity.getFile()), headers, HttpStatus.CREATED);
        } catch (IOException e) {
            logger.error("文件下载失败"+e.getMessage());
        } catch (BaseSystemException e) {
            logger.error("文件下载失败"+e.getMessage());
        }
        return responseEntity;
    }
    @RequestMapping(value = "/testOption")
    @ResponseBody
    public EsbReturnEntity testOption(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception  {
        return  entranceService.doTstsBusiness(servletRequest,servletResponse);
    }

    @RequestMapping(value = "/externalCallInterface")
    @ResponseBody
    public JsonResultEntity externalCallInterface(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception  {
        return  sysApplicationService.externalCallInterface(servletRequest,servletResponse);
    }

    @RequestMapping(value = "/externalCallInterfacefileUpload")
    @ResponseBody
    public JsonResultEntity externalCallInterfacefileUpload(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception  {
        return  sysApplicationService.externalCallInterfacefileUpload(servletRequest,servletResponse);
    }

    @RequestMapping(value = "/externalCallInterfaceResend")
    @ResponseBody
    public JsonResultEntity externalCallInterfaceResend(SysMessageManageLogEntity resendLogEntity) throws Exception  {
        return  sysApplicationService.externalCallInterfaceResend(resendLogEntity);
    }

    @RequestMapping(value = "/externalCallInterfaceToESB")
    @ResponseBody
    public JsonResultEntity externalCallInterfaceToESB(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception  {
        return  sysApplicationService.externalCallInterfaceToESB(servletRequest,servletResponse);
    }


}
