package com.lantone.qc.trans.changx;

import com.google.common.collect.Maps;
import com.lantone.qc.dbanaly.util.ModuleMappingUtil;
import com.lantone.qc.pub.Content;
import com.lantone.qc.pub.model.doc.FirstPageRecordDoc;
import com.lantone.qc.pub.model.vo.MedrecVo;
import com.lantone.qc.pub.util.FastJsonUtils;
import com.lantone.qc.pub.util.ListUtil;
import com.lantone.qc.pub.util.StringUtil;
import com.lantone.qc.trans.ModelDocTrans;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Description: 病案首页文档生成
 * @author: 胡敬
 * @time: 2020/3/16 17:47
 */
public class ChangxFirstPageRecordDocTrans extends ModelDocTrans {

    @Override
    public FirstPageRecordDoc extract(MedrecVo medrecVo) {
        Map<String, String> content = (Map) medrecVo.getContent();
        FirstPageRecordDoc firstPageRecordDoc = new FirstPageRecordDoc();
        firstPageRecordDoc.setStructureMap(content);

        Map<String, Object> structureExtMap = Maps.newHashMap();
        structureExtMap.putAll(content);
        structureExtMap.put(Content.diagnose_cts, FastJsonUtils.getJsonToBean(content.get(Content.diagnose_cts), Object.class));
        //        structureExtMap.put(Content.outpatientEmergencyDiag, FastJsonUtils.getJsonToBean(content.get(Content.outpatientEmergencyDiag), Object.class));
        structureExtMap.put(Content.operative_information, FastJsonUtils.getJsonToBean(content.get(Content.operative_information), Object.class));
        structureExtMap.put(Content.dischargeDiag, FastJsonUtils.getJsonToBean(content.get(Content.dischargeDiag), Object.class));
        //        structureExtMap.put(Content.pathologyDiagnose, FastJsonUtils.getJsonToBean(content.get(Content.pathologyDiagnose), Object.class));
        firstPageRecordDoc.setStructureExtMap(structureExtMap);
        //病案首页出院诊断页面信息只保留主要诊断和其他诊断
        List<Map<String, String>> list = (List<Map<String, String>>) FastJsonUtils.getJsonToBean(content.get(Content.dischargeDiag), Object.class);
        if (ListUtil.isNotEmpty(list)) {
            Iterator<Map<String, String>> iterator = list.iterator();
            Map<String, String> map = new HashMap<>();
            while (iterator.hasNext()) {
                map = new HashMap<>();
                map = iterator.next();
                if (!((map.get("诊断类别") != null && map.get("诊断类别").equals("主要诊断"))
                        || (map.get("诊断类别") != null && map.get("诊断类别").equals("其他诊断")))) {
                    iterator.remove();
                }
            }
            medrecVo.getContent().put("出院诊断", list.toString());
        }

        Map<String, String> setMap = Maps.newHashMap();
        setMap.put("mode_id=6", "");
        String modeId = ModuleMappingUtil.getXmlDataTypeModuleId(setMap);
        if (StringUtil.isNotBlank(modeId)) {
            medrecVo.getContent().put("mode_id", modeId);
        }
        firstPageRecordDoc.setPageData(medrecVo.getContent());
        return firstPageRecordDoc;
    }

}