package com.diagbot.facade.str;

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

import com.alibaba.fastjson.JSON;
import com.diagbot.dto.RespDTO;
import com.diagbot.enums.ModeIdEnum;
import com.diagbot.facade.data.AMedAbnormalInfoFacade;
import com.diagbot.facade.data.ColumnFacade;
import com.diagbot.vo.str.StrContentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class BStrContentFacade {

    @Autowired
    private AMedAbnormalInfoFacade aMedAbnormalInfoFacade;

    @Autowired
    private ColumnFacade columnFacade;

    @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{
                //是否进行xml、html、json解析为标准结构化
                if(xmlAnalyse){
                    //解析时，调用解析接口
                    columnFacade.analyseWholeData(strContentVO);
                }else{
                    //不解析时，只存储入库
                    columnFacade.addWholeData(strContentVO);
                }

                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("数据处理异常，详情查看日志表");
        }
    }
}
