package com.ann.demo.service;

import com.ann.demo.entity.filing.ArchiveDetail;
import com.ann.demo.entity.filing.MessageScanning;
import com.ann.demo.entity.filing.dto.MessageDto;
import com.ann.demo.entity.interfaceEntity.ArchiveMaster;
import com.ann.demo.entity.normalEntity.OutPara;
import com.ann.demo.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.Objects;

/**
 * 解析逻辑
 *
 * @Author: LeiJiaXin
 * @Date: 2019/8/20 16:29
 */
@Component
public class AnalysisService {
    @Autowired
    private ArchiveMasterService archiveMasterService;
    @Autowired
    private ArchiveDetailService archiveDetailService;

    private static final Logger logger = LoggerFactory.getLogger(AnalysisService.class);


    public static String getReturnResult(boolean resultCode,String content)  throws Exception {
        OutPara outPara = new OutPara();
        if (!resultCode) {
            outPara.setRESULT_CODE(false);
            outPara.setRESULT_CONTENT(content);
        } else {
            outPara.setRESULT_CODE(true);
            outPara.setRESULT_CONTENT("成功");
        }
        return XMLUtils.generateXmlStr(outPara);
    }


    public String handlePdfFile(MessageDto messageDto, ArchiveMaster archiveMaster) {
        String pdfPath = null;
        ArchiveDetail archiveDetail = null;
        try {
            archiveDetail = archiveDetailService.isExit(archiveMaster.getId(), messageDto.getType(), "嘉时扫描系统", messageDto.getApplyId());
            if(Objects.equals(messageDto.getAddFlag(),"INSERT") || Objects.equals(messageDto.getAddFlag(),"UPDATE")){

                if (archiveDetail == null) {
                    archiveDetail = new ArchiveDetail(archiveMaster.getId(), new Date(), messageDto.getType(), "嘉时扫描系统", messageDto.getApplyId(), pdfPath,Integer.valueOf(messageDto.getPageNum()));
                    archiveDetail.setTitle(messageDto.getDetailType());
                    archiveDetailService.save(archiveDetail);
                }
                File imageFile = FileUtils.createFile("images", messageDto.getInpNo(), messageDto.getVisitId(),archiveDetail.getId());
                if(DownloadUtils.downloadImageFile(messageDto.getReportAddress(), imageFile) == null){
                    if (archiveDetail != null) {
                        if (archiveDetail.getId() != null) {
                            logger.error("删除了archiveDetail的apply_id:{},masterId:{}",archiveDetail.getApplyId(),archiveDetail.getMasterID());
                            archiveDetailService.delete(archiveDetail.getId());
                            return "下载图像出错";
                        }
                    }

                }
                File pdfFile = FileUtils.createFile("pdfs", messageDto.getInpNo(), messageDto.getVisitId(), archiveDetail.getId());
                //存入pdf地址
                pdfPath = PdfUtils.imagesToPdf(imageFile, pdfFile);
                if (pdfPath == null) {
                    return "image转成pdf出错了";
                }
                //删除FTP图像位置
                if(!FTPUtil.deleteFile(messageDto.getReportAddress())){
                    // 成功
                    return "删除FTP路径失败,其中路径为："+messageDto.getReportAddress();
                }
                if(archiveDetail.getId() != null){
                    if(!Objects.equals(archiveMaster.getIsScanning(), "1")){
                        archiveMaster.setIsScanning("1");
                        archiveMasterService.save(archiveMaster);
                    }
                    archiveDetail.setPdfPath(pdfPath);
                    archiveDetailService.save(archiveDetail);
                    return null;
                }
            }else if(Objects.equals(messageDto.getAddFlag(),"DELETE")){
                if(archiveDetail == null){
                    return "文件还未生成，不能作废";
                }else{
                    if(archiveDetailService.delete(archiveDetail.getId())){
                        return null;
                    }
                }
            }else{
                return "没有按指定标识传";
            }
        }catch (Exception e){
            if (archiveDetail != null) {
                if (archiveDetail.getId() != null) {
                    logger.error("删除了archiveDetail的apply_id:{},masterId:{}",archiveDetail.getApplyId(),archiveDetail.getMasterID());
                    archiveDetailService.delete(archiveDetail.getId());
                }
            }
        }
        return "出错了";
    }
}
