package com.lantone.qc.dbanaly.facade.comsis;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lantone.qc.dbanaly.facade.changx.CxXmlUtil;
import com.lantone.qc.dbanaly.facade.taizhou.TzXmlUtil;
import com.lantone.qc.dbanaly.lt.entity.MedicalRecord;
import com.lantone.qc.dbanaly.lt.entity.MedicalRecordContent;
import com.lantone.qc.dbanaly.lt.service.impl.MedicalRecordContentServiceImpl;
import com.lantone.qc.dbanaly.lt.service.impl.MedicalRecordServiceImpl;
import com.lantone.qc.pub.util.EncrypDES;
import com.lantone.qc.pub.util.FileUtil;
import com.lantone.qc.pub.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @author: rengb
 * @time: 2020/8/26 9:56
 */
@Component
public class DataExtractFacade {
    @Autowired
    private MedicalRecordServiceImpl medicalRecordService;
    @Autowired
    private MedicalRecordContentServiceImpl medicalRecordContentService;

    public void extractCrfNeedData() throws Exception {
        EncrypDES encrypDES = new EncrypDES();

        long hospid = 3l;
        long modid = 1l;
        QueryWrapper<MedicalRecord> medicalRecordQueryWrapper = new QueryWrapper<>();
        medicalRecordQueryWrapper.eq("hospital_id", hospid);
        medicalRecordQueryWrapper.eq("mode_id", 1l);
        medicalRecordQueryWrapper.select("rec_id,behospital_code");
        List<MedicalRecord> medicalRecordList = medicalRecordService.list(medicalRecordQueryWrapper);

        for (MedicalRecord medicalRecord : medicalRecordList) {
            QueryWrapper<MedicalRecordContent> medicalRecordContentQueryWrapper = new QueryWrapper<>();
            medicalRecordContentQueryWrapper.eq("hospital_id", hospid);
            medicalRecordContentQueryWrapper.eq("rec_id", medicalRecord.getRecId());
            medicalRecordContentQueryWrapper.select("xml_text");
            MedicalRecordContent medicalRecordContent = medicalRecordContentService.getOne(medicalRecordContentQueryWrapper);
            if (medicalRecordContent != null && StringUtil.isNotBlank(medicalRecordContent.getXmlText())) {
                String dexml = encrypDES.decryptor(medicalRecordContent.getXmlText());
                if (StringUtil.isNotBlank(dexml)) {
                    Map<String, String> sourceMap = TzXmlUtil.getXmlToMapForTZ(dexml);
                    String result = sourceMap.get("专科检查");
                    if (StringUtil.isNotBlank(result)) {
                        FileUtil.fileWrite("C:\\Users\\Administrator\\Desktop\\专科检查\\台州", medicalRecord.getBehospitalCode(), result);
                    }
                }
            }
        }
    }

    private String concatString(String item) {
        if (StringUtil.isBlank(item)) {
            return "";
        } else {
            return item + "。";
        }
    }

    private String concatSpecialCheck(Map<String, String> sourceMap) {
        return concatString(sourceMap.get("一般情况")) +
                concatString(sourceMap.get("皮肤情况")) +
                concatString(sourceMap.get("淋巴")) +
                concatString(sourceMap.get("头部检查")) +
                concatString(sourceMap.get("颈部")) +
                concatString(sourceMap.get("胸部检查")) +
                concatString(sourceMap.get("肺部检查")) +
                concatString(sourceMap.get("心脏检查")) +
                concatString(sourceMap.get("血管")) +
                concatString(sourceMap.get("腹部检查")) +
                concatString(sourceMap.get("外生殖器")) +
                concatString(sourceMap.get("直肠肛门")) +
                concatString(sourceMap.get("四肢脊柱检查")) +
                concatString(sourceMap.get("神经系统检查")) +
                concatString(sourceMap.get("其他说明"));
    }

}