package com.emr.service.quality;

import com.emr.dao.quality.QualityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;


/**
 * @ClassName QualityServiceImpl
 * @Description
 * @Author linjj
 * @Date 2023/4/13 15:26
 * @Version 1.0
 */

@Service
@Transactional
public class QualityServiceImpl implements QualityService {

    @Autowired
    QualityMapper qualityMapper;

    @Override
    public String getPhAndDocumentNum(String startRange, String endRange) {
        StringBuffer sb = new StringBuffer();
        Integer phNum = qualityMapper.getPhNum(startRange, endRange);
        Integer fileNum = qualityMapper.getFileNum(startRange, endRange);
        String s = sb.append("查询结果:" + phNum + "盘," + fileNum + "个文件数据").toString();
        return s;
    }

    @Override
    public String examineDocument(String startRange, String endRange) {
        //用来记录确实记录
        int i = 0;
        //用来拼接缺失文件地址
        StringBuffer lackPath = new StringBuffer();
        //用来返回记录
        StringBuffer sb = new StringBuffer();
        List<String> phList = qualityMapper.getPh(startRange, endRange);
        for (String ph : phList) {
            List<String> fliePathList = qualityMapper.getFliePath(ph);
            for (String fliePath : fliePathList) {
                File file = new File(fliePath);// 图片存放路径
                if (!file.exists() && !file.isDirectory()) {
                    i++;
                    lackPath.append(fliePath + ",");
                }
            }
        }
        String s = sb.append("缺失" + i + "个文件," + "缺失文件为:" + lackPath).toString();
        return s;
    }

    @Override
    public String SeniorExamine(String startRange, String endRange) {
        //用来记录缺失数量
        int i = 0;
        //用来继续故障数量
        int faultNum = 0;
        //用来拼接缺失文件地址
        StringBuffer lackPath = new StringBuffer();
        //用来拼接故障文件地址
        StringBuffer faultPath = new StringBuffer();
        //用来返回记录
        StringBuffer sb = new StringBuffer();
        List<String> phList = qualityMapper.getPh(startRange, endRange);
        for (String ph : phList) {
            List<String> fliePathList = qualityMapper.getFliePath(ph);
            for (String fliePath : fliePathList) {
                File file = new File(fliePath);// 图片存放路径
                if (!file.exists() && !file.isDirectory()) {
                    i++;
                    lackPath.append(fliePath + ",");
                }
            }
            for (String list : fliePathList) {
                try {
                    Image image = ImageIO.read(new FileInputStream(list));
                } catch (IOException e) {
                    faultNum++;
                    faultPath.append(list + ",");
                }
            }
            //记录完成盘号
            qualityMapper.addPh(ph);
        }
        String s = sb.append("缺失" + i + "个文件," + "缺失文件为:" + lackPath+"故障文件"+faultNum+"个文件,"+"故障文件为:"+faultPath).toString();
        return s;
    }

}
