package com.emr.service.tScanAssort;

import com.emr.dao.CommomMapper;
import com.emr.dao.tScanAssort.T_Scan_AssortMapper;
import com.emr.entity.tScanAssort.T_Scan_Assort;
import com.emr.vo.FontVo.ScanAssortVo;
import com.emr.vo.ZhFyImageVo;
import com.emr.vo.PicListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName:
 * @Description:
 * @Param 传输参数
 * @Return
 * @Author: 曾文和
 * @CreateDate: 2019/6/6 15:19
 * @UpdateUser: 曾文和
 * @UpdateDate: 2019/6/6 15:19
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@Service
@Transactional
public class T_Scan_AssortServiceImpl implements T_Scan_AssortService{
    @Autowired
    private T_Scan_AssortMapper scanAssortMapper;
    @Autowired
    private CommomMapper commomMapper;
    @Value("${NEW_EMR_RECORD_JSP}")
    private String NEW_EMR_RECORD_JSP;
    @Override
    public void updateByPrimaryKeySelective(T_Scan_Assort newAssort, T_Scan_Assort oldAssort) {
        scanAssortMapper.updateByPrimaryKeySelective(newAssort,oldAssort);
    }

    @Override
    public int SimpleUpdate(String patientId, String assortId, List<String> list) {
        return scanAssortMapper.SimpleUpdate(patientId, assortId, list);
    }

    @Override
    public int SimpleUpdateByPatientIdAndScanPages(String patientId, String newAssortId, String scanPages) {
        return scanAssortMapper.SimpleUpdateByPatientIdAndScanPages(patientId, newAssortId, scanPages);
    }

    @Override
    public int SimpleDelByPatientIdAndScanPages(String patientId, String scanPages) {
        return scanAssortMapper.SimpleDelByPatientIdAndScanPages(patientId,scanPages);
    }

    @Override
    public void SimpleInsert(List<T_Scan_Assort> list) {
        if(!CollectionUtils.isEmpty(list)) {
            //根据字段多少批量新增
            ///查询表列数
            int colCount = commomMapper.selectColByTableName("t_scan_assort");
            int simpleInsertCount = 2100 / colCount;
            List<T_Scan_Assort> scanAssorts = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                //处理批量添加sql太长问题，设定个数为180
                if (i != 0 && list.size() % simpleInsertCount == 0) {
                    scanAssorts.clear();
                }
                scanAssorts.add(list.get(i));
                //是180的倍数或最后一个
                if (list.size() % simpleInsertCount == 0 || i == list.size() - 1) {
                    scanAssortMapper.SimpleInsert(scanAssorts);
                }
            }
        }
    }

    @Override
    public List<ScanAssortVo> selectPicPaths(String patientId, String scanPages, String flag) {
        return scanAssortMapper.selectPicPaths(patientId, scanPages, flag);
    }

    @Override
    public void recoverPic(String patientId, String scanPages) {
        scanAssortMapper.recoverPic(patientId, scanPages);
    }

    @Override
    public List<ZhFyImageVo> getPathListByInpatientNo(String inpatientNo, String admissTimes) {
        List<ZhFyImageVo> list = scanAssortMapper.getPathListByInpatientNo(inpatientNo, admissTimes);
        for (ZhFyImageVo scanAssortList:list){
            List<PicListVo> picList = scanAssortList.getPicList();
            //编辑图片信息
            for (PicListVo picPathList:picList){
                //获取盘符并转换映射地址的头部地址
                String root = selectRootByNotWater(picPathList.getPicUrl());
                //获取盘符后面的地址
                String str1 = picPathList.getPicUrl().substring(0, picPathList.getPicUrl().indexOf(File.separator));
                String picPath = picPathList.getPicUrl().substring(str1.length() + 1, picPathList.getPicUrl().length());
                String fileUrl = NEW_EMR_RECORD_JSP+ File.separator + root + picPath;
                picPathList.setPicUrl(fileUrl);
                //生成文件id
                String fileId = scanAssortList.getPatientId() + picPathList.getScanPage();
                String s = DigestUtils.md5DigestAsHex(fileId.getBytes());
                picPathList.setFileId(s);
            }
        }
        return list;
    }

    @Override
    public List<ZhFyImageVo> getPathListByDisDate(String startTime,String endTime) {
        List<ZhFyImageVo> list = scanAssortMapper.getPathListByDisDate(startTime, endTime);
        for (ZhFyImageVo scanAssortList:list){
            List<PicListVo> picList = scanAssortList.getPicList();
            for (PicListVo picPathList:picList){
                //获取盘符并转换映射地址的头部地址
                String root = selectRootByNotWater(picPathList.getPicUrl());
                //获取盘符后面的地址
                String str1 = picPathList.getPicUrl().substring(0, picPathList.getPicUrl().indexOf(File.separator));
                String picPath = picPathList.getPicUrl().substring(str1.length() + 1, picPathList.getPicUrl().length());
                String fileUrl = NEW_EMR_RECORD_JSP+ File.separator + root + picPath;
                picPathList.setPicUrl(fileUrl);
                //生成文件id
                String fileId = scanAssortList.getPatientId() + picPathList.getScanPage();
                String s = DigestUtils.md5DigestAsHex(fileId.getBytes());
                picPathList.setFileId(s);
            }
        }
        return list;
    }

    private String selectRootByNotWater(String srcPath) {
        //获取盘符
        String str = srcPath.substring(0, 1).toUpperCase();
        switch (str) {
            case "C":
                return "reloadC\\";
            case "D":
                return "reloadD\\";
            case "E":
                return "reloadE\\";
            case "F":
                return "reloadF\\";
            case "G":
                return "reloadG\\";
            case "H":
                return "reloadH\\";
            case "I":
                return "reloadI\\";
            case "J":
                return "reloadJ\\";
            case "K":
                return "reloadK\\";
            default:
                ;
        }
        return null;
    }
}
