package com.diagbot.facade.str;

/**
 * @Description:文书核心业务处理类
 * @Author:liuqq
 * @time: ${DATE} ${TIME}
 **/

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.RespDTO;
import com.diagbot.entity.ModelHospital;
import com.diagbot.entity.StrTotalInfo;
import com.diagbot.enums.ModeIdEnum;
import com.diagbot.facade.ModelHospitalFacade;
import com.diagbot.facade.data.AMedAbnormalInfoFacade;
import com.diagbot.facade.data.ColumnFacade;
import com.diagbot.vo.str.StrContentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class BStrContentFacade {

    @Autowired
    private AMedAbnormalInfoFacade aMedAbnormalInfoFacade;

    @Autowired
    private ColumnFacade columnFacade;
    @Autowired
    private ModelHospitalFacade modelHospitalFacade;
    @Autowired
    private StrTotalInfoFacade strTotalInfoFacade;

    @Value("${log_switch.enable}")
    private boolean logSwitch;

    @Value("${xml_analyse.enable}")
    private boolean xmlAnalyse;

    public RespDTO execDealData(StrContentVO strContentVO){
        try{
            if("".equals(strContentVO.getModelId())){
                return RespDTO.onError("请输入模板类型ID!");
            }else if("".equals(strContentVO.getRecId())) {
                return RespDTO.onError("请输入记录编号!");
            }else if(strContentVO.getHospitalId()==null){
                return RespDTO.onError("请输入医院编码!");
            }else if("".equals(strContentVO.getBehospitalCode())){
                return RespDTO.onError("请输入病人住院编码!");
            }else{
                //根据医院模板类型ID+名称，初始化modelId,用于区分存到哪张表
                Long modelId=initModeId(strContentVO);
                if(modelId!=0L){
                    //是否进行xml、html、json解析为标准结构化
                    if(xmlAnalyse){
                        //解析时，调用解析接口
                        columnFacade.analyseWholeData(strContentVO);
                    }else{
                        //不解析时，只存储入库
                        columnFacade.addWholeData(strContentVO);
                    }
                }else{
                    //无对应的modelId时，存入对应表中，方便后面追述
                    StrTotalInfo strTotalInfo=new StrTotalInfo();
                    BeanUtils.copyProperties(strContentVO,strTotalInfo);
                    strTotalInfo.setGmtCreate(new Date());
                    strTotalInfoFacade.save(strTotalInfo);
                }

                if(logSwitch){
                    aMedAbnormalInfoFacade.saveAbnormalInfo("结构化-文书-正常", strContentVO.getRecId(), JSON.toJSONString(strContentVO), "", ModeIdEnum.getName(Integer.parseInt(strContentVO.getModelId())));
                }
                return RespDTO.onSuc("操作正常！");
            }
        }catch (Exception e){
            aMedAbnormalInfoFacade.saveAbnormalInfo("结构化-文书-异常", ModeIdEnum.getName(Integer.parseInt(strContentVO.getModelId())), JSON.toJSONString(strContentVO), "", e.getMessage());
            return RespDTO.onError("数据处理异常，详情查看日志表");
        }
    }

    /**
     * 初始化模型ID,根据医院模板类型ID及名称，具体逻辑已项目为主
     * @param strContentVO
     * @return
     */
    private Long initModeId(StrContentVO strContentVO) {
        Long modelId=0L;
        try{
            QueryWrapper<ModelHospital> wrapper = new QueryWrapper<>();
            wrapper.eq("hospital_id", strContentVO.getHospitalId())
                    .eq("hospital_model_name", strContentVO.getModelName())
                    .eq("remark", strContentVO.getModelId());
            ModelHospital mode = modelHospitalFacade.getOne(wrapper, false);
            if (mode != null) {
                modelId = mode.getStandModelId();
            } else {
                modelId=0L;
            }
        }catch (Exception e){
            modelId=0L;
            e.printStackTrace();
        }

        return modelId;
    }
}
