package com.diagbot.facade.data;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.data.AMedCrisisDTO;
import com.diagbot.entity.MedCrisisInfo;
import com.diagbot.service.impl.MedCrisisInfoServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.vo.data.AMedCrisisVO;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class AMedCrisisFacade extends MedCrisisInfoServiceImpl {

    @Autowired
    @Qualifier("medCrisisInfoServiceImpl")
    private MedCrisisInfoServiceImpl medCrisisInfoService;
    @Autowired
    private AMedAbnormalInfoFacade aMedAbnormalInfoFacade;
    @Value("${log_switch.enable}")
    private boolean logSwitch;

    public RespDTO<List<AMedCrisisDTO>> executeMedCrisis(List<AMedCrisisVO> list) {
        try{
            if (list != null && list.size() > 0) {
                //循环验证数据有效性
                for (AMedCrisisVO aMedCrisisVO : list) {
                    if ("".equals(aMedCrisisVO.getRecId())) {
                        return RespDTO.onError("请输入文书编码!");
                    } else if ("".equals(aMedCrisisVO.getBehospitalCode())) {
                        return RespDTO.onError("请输入病人住院编码!");
                    } else if (aMedCrisisVO.getHospitalId() == null) {
                        return RespDTO.onError("请输入医院编码!");
                    } else if ("".equals(aMedCrisisVO.getRecType())) {
                        return RespDTO.onError("请输入危急值类型!");
                    }
                }

                List<MedCrisisInfo> medCrisisList = Lists.newArrayList();
                list.stream().forEach(s -> {
                    MedCrisisInfo medCrisisInfo = new MedCrisisInfo();
                    BeanUtil.copyProperties(s, medCrisisInfo);
                    medCrisisInfo.setRepDate(DateUtil.parseDateTime(s.getRepDate()));
                    medCrisisInfo.setSendDate(DateUtil.parseDateTime(s.getSendDate()));
                    medCrisisList.add(medCrisisInfo);
                });
                execute(medCrisisList);

                if(logSwitch){
                    medCrisisList.forEach(s->{
                        aMedAbnormalInfoFacade.saveAbnormalInfo("危急值信息-正常","", JSON.toJSONString(list),"","");
                    });
                }
                return RespDTO.onSuc("操作正常！");
            } else {
                return RespDTO.onError("未接收到数据!");
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            aMedAbnormalInfoFacade.saveAbnormalInfo("危急值信息-异常","", JSON.toJSONString(list),"",e.getMessage());
            return RespDTO.onError("数据处理异常，详情查看日志表");
        }
    }

    public void execute(List<MedCrisisInfo> medCrisisList) {
        List<MedCrisisInfo> addE = Lists.newLinkedList();
        List<MedCrisisInfo> updateE = Lists.newLinkedList();
        if (medCrisisList != null && medCrisisList.size() > 0) {
            medCrisisList.forEach(s -> {
                try {
                    MedCrisisInfo medCrisisInfo = this.getOne(new QueryWrapper<MedCrisisInfo>()
                            .eq("rec_id", s.getRecId())
                            .eq("hospital_id", s.getHospitalId())
                            .eq("behospital_code", s.getBehospitalCode())
                            .eq("rec_type", s.getRecType()), false);
                    if (medCrisisInfo != null) {
                        s.setGmtModified(new Date());
                        updateE.add(s);
                    } else {
                        s.setGmtCreate(new Date());
                        addE.add(s);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    aMedAbnormalInfoFacade.saveAbnormalInfo("数据解析", s.getRecId(), JSON.toJSONString(medCrisisList), "", e.getMessage());
                }
            });
        }
        if (addE.size() > 0) {
            medCrisisInfoService.saveBatch(addE);
        }
        if (updateE.size() > 0) {
            medCrisisInfoService.updateBatchByKey(updateE);
        }
    }
}
