package com.diagbot.facade.data;

import com.alibaba.fastjson.JSON;
import com.diagbot.dto.OutputInfo;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.Response;
import com.diagbot.enums.CacheKeyEnum;
import com.diagbot.enums.ModeIdEnum;
import com.diagbot.facade.QcClientFacade;
import com.diagbot.facade.str.*;
import com.diagbot.service.impl.ColumnServiceImpl;
import com.diagbot.vo.QueryVo;
import com.diagbot.vo.str.StrContentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

@Slf4j
@Component
public class ColumnFacade extends ColumnServiceImpl {
    @Autowired
    private QcClientFacade qcClientFacade;
    @Autowired
    private AMedAbnormalInfoFacade aMedAbnormalInfoFacade;

    /**
     * 解析
     * @param queryVo
     */
    public void analyseRec(QueryVo queryVo) {
        try {
            Response<OutputInfo> resp=qcClientFacade.analyseField(queryVo);
            if(resp.getData()!=null && resp.getData().getPageData()!=null){
                Map<String, Object> pageData=resp.getData().getPageData();
                pageData.forEach((key,value)->{
                    List<Map<String, Object>> listMap=(List<Map<String,Object>>)resp.getData().getPageData().get(key);
                    Map<String, String> columnMap=this.getColumn(CacheKeyEnum.getName(key));

                    Map<String, Object> entityMap=new HashMap<String, Object>();
                    listMap.forEach(s->{
                        columnMap.forEach((k, v) -> {
                            entityMap.put(v,s.get(k));
                        });
                        //数据解析
                        analysisData(key,entityMap,Long.valueOf(queryVo.getHospitalId()),JSON.toJSONString(s));
                    });
                });
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            aMedAbnormalInfoFacade.saveAbnormalInfo("数据解析","", JSON.toJSONString(queryVo),"",e.getMessage());
        }

    }

    /**
     * @Description:解析his传输过来的wholeData
     * @Author:liuqq
     * @time: ${DATE} ${TIME}
     **/
    public void analyseWholeData(StrContentVO strContentVO) throws Exception{
        //调用解析接口
        Response<OutputInfo> resp=null;
        //qcClientFacade.analyseField(queryVo);
        if(resp.getData()!=null && resp.getData().getPageData()!=null){
            Map<String, Object> pageData=resp.getData().getPageData();
            pageData.forEach((key,value)->{
                List<Map<String, Object>> listMap=(List<Map<String,Object>>)resp.getData().getPageData().get(key);
                Map<String, String> columnMap=this.getColumn(CacheKeyEnum.getName(key));

                Map<String, Object> entityMap=new HashMap<String, Object>();
                listMap.forEach(s->{
                    columnMap.forEach((k, v) -> {
                        entityMap.put(v,s.get(k));
                    });
                    //数据解析
                    analysisData(ModeIdEnum.getName(Integer.parseInt(strContentVO.getModelId())),entityMap,strContentVO.getHospitalId(),JSON.toJSONString(s));
                });
            });
        }
    }

    /**
     * @Description: 不解析时，先入库
     * @Author:liuqq
     * @time: ${DATE} ${TIME}
     **/
    public void addWholeData(StrContentVO strContentVO){
        String modelName=ModeIdEnum.getName(Integer.parseInt(strContentVO.getModelId()));
        if("入院记录".equals(modelName)){
            bStrAdmissionNoteFacade.dealData(strContentVO);
        }else if("首次病程录".equals(modelName)){
            bStrFirstRecordFacade.dealData(strContentVO);
        }else if("输血后效果评价".equals(modelName)){
            bStrBloodResultFacade.dealData(strContentVO);
        }else if("输血/血制品病程记录".equals(modelName)){
            bStrBloodTransfusionFacade.dealData(strContentVO);
        }else if("转入记录".equals(modelName)){
            bStrTransferInNoteFacade.dealData(strContentVO);
        }else if("转出记录".equals(modelName)){
            bStrTransferOutNoteFacade.dealData(strContentVO);
        }else if("病危通知书".equals(modelName)){
            bStrIllCriticallyFacade.dealData(strContentVO);
        }else if("病重通知书".equals(modelName)){
            bStrIllSeriouslFacade.dealData(strContentVO);
        }else if("疑难病例讨论记录".equals(modelName)){
            bStrDifficultCaseFacade.dealData(strContentVO);
        }else if("死亡记录".equals(modelName)){
            bStrDeathNoteFacade.dealData(strContentVO);
        }else if("死亡病例讨论记录".equals(modelName)){
            bStrDeathDiscussionFacade.dealData(strContentVO);
        }else if("查房记录".equals(modelName)){
            bStrWardRecordFacade.dealData(strContentVO);
        }else if("术后首次病程及谈话记录".equals(modelName)){
            bStrOperativeFirstRecordFacade.dealData(strContentVO);
        }else if("术前讨论、术前小结".equals(modelName)){
            bStrPreoperativeDiscussionFacade.dealData(strContentVO);
        }else if("手术记录".equals(modelName)){
            bStrOperativeNoteFacade.dealData(strContentVO);
        }else if("抢救记录".equals(modelName)){
            bStrRescueNoteFacade.dealData(strContentVO);
        }else if("危急值记录".equals(modelName)){
            bStrCrisisNoteFacade.dealData(strContentVO);
        }else if("出院小结".equals(modelName)){
            bStrLeaveHospitalFacade.dealData(strContentVO);
        }else if("会诊申请单".equals(modelName)){
            bStrConsultationApplyFacade.dealData(strContentVO);
        }else if("会诊结果单".equals(modelName)){
            bStrConsultationResultFacade.dealData(strContentVO);
        }else if("会诊单".equals(modelName)){
            bStrConsultationNoteFacade.dealData(strContentVO);
        }else if ("会诊记录".equals(modelName)){
            bStrConsultationRecordFacade.dealData(strContentVO);
        } else if("阶段小结".equals(modelName)){
            bStrPeriodConclusionFacade.dealData(strContentVO);
        }
    }

    /**
     * 数据解析
     * @param modelName
     * @param entityMap
     * @param hospitalId
     */
    private void analysisData(String modelName,Map<String, Object> entityMap,Long hospitalId,String columnMap){
        try{
            if("入院记录".equals(modelName)){
                bStrAdmissionNoteFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("首次病程录".equals(modelName)){
                bStrFirstRecordFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("输血后效果评价".equals(modelName)){
                bStrBloodResultFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("输血/血制品病程记录".equals(modelName)){
                bStrBloodTransfusionFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("转入记录".equals(modelName)){
                bStrTransferInNoteFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("转出记录".equals(modelName)){
                bStrTransferOutNoteFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("病危通知书".equals(modelName)){
                bStrIllCriticallyFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("病重通知书".equals(modelName)){
                bStrIllSeriouslFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("疑难病例讨论记录".equals(modelName)){
                bStrDifficultCaseFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("死亡记录".equals(modelName)){
                bStrDeathNoteFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("死亡病例讨论记录".equals(modelName)){
                bStrDeathDiscussionFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("查房记录".equals(modelName)){
                bStrWardRecordFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("术后首次病程及谈话记录".equals(modelName)){
                bStrOperativeFirstRecordFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("术前讨论、术前小结".equals(modelName)){
                bStrPreoperativeDiscussionFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("手术记录".equals(modelName)){
                bStrOperativeNoteFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("抢救记录".equals(modelName)){
                bStrRescueNoteFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("危急值记录".equals(modelName)){
                bStrCrisisNoteFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("出院小结".equals(modelName)){
                bStrLeaveHospitalFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("会诊申请单".equals(modelName)){
                bStrConsultationApplyFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("会诊结果单".equals(modelName)){
                bStrConsultationResultFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if("会诊单".equals(modelName)){
                bStrConsultationNoteFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }else if ("会诊记录".equals(modelName)){
                bStrConsultationRecordFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            } else if("阶段小结".equals(modelName)){
                bStrPeriodConclusionFacade.dealData(columnMap,entityMap,Long.valueOf(hospitalId));
            }
            log.info(modelName+"---- 解析完成！");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            aMedAbnormalInfoFacade.saveAbnormalInfo("数据解析异常", "",JSON.toJSONString(columnMap),"",e.getMessage());
        }
    }

    @Autowired
    private BStrAdmissionNoteFacade bStrAdmissionNoteFacade;
    @Autowired
    private BStrFirstRecordFacade bStrFirstRecordFacade;
    @Autowired
    private BStrPeriodConclusionFacade bStrPeriodConclusionFacade;
    @Autowired
    private BStrBloodResultFacade bStrBloodResultFacade;
    @Autowired
    private BStrBloodTransfusionFacade bStrBloodTransfusionFacade;
    @Autowired
    private BStrTransferInNoteFacade bStrTransferInNoteFacade;
    @Autowired
    private BStrTransferOutNoteFacade bStrTransferOutNoteFacade;
    @Autowired
    private BStrIllSeriouslFacade bStrIllSeriouslFacade;
    @Autowired
    private BStrIllCriticallyFacade bStrIllCriticallyFacade;
    @Autowired
    private BStrDifficultCaseFacade bStrDifficultCaseFacade;
    @Autowired
    private BStrDeathNoteFacade bStrDeathNoteFacade;
    @Autowired
    private BStrDeathDiscussionFacade bStrDeathDiscussionFacade;
    @Autowired
    private BStrWardRecordFacade bStrWardRecordFacade;
    @Autowired
    private BStrOperativeFirstRecordFacade bStrOperativeFirstRecordFacade;
    @Autowired
    private BStrPreoperativeDiscussionFacade bStrPreoperativeDiscussionFacade;
    @Autowired
    private BStrOperativeNoteFacade bStrOperativeNoteFacade;
    @Autowired
    private BStrRescueNoteFacade bStrRescueNoteFacade;
    @Autowired
    private BStrCrisisNoteFacade bStrCrisisNoteFacade;
    @Autowired
    private BStrLeaveHospitalFacade bStrLeaveHospitalFacade;
    @Autowired
    private BStrConsultationApplyFacade bStrConsultationApplyFacade;
    @Autowired
    private BStrConsultationResultFacade bStrConsultationResultFacade;
    @Autowired
    private BStrConsultationNoteFacade bStrConsultationNoteFacade;
    @Autowired
    private BStrConsultationRecordFacade bStrConsultationRecordFacade;
}
