package com.diagbot.facade.data;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.diagbot.dto.RespDTO;
import com.diagbot.entity.MedPacsInfo;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.impl.MedPacsInfoServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.util.StringUtil;
import com.diagbot.util.TZDBConn;
import com.diagbot.vo.data.ADeleteFlagVO;
import com.diagbot.vo.data.AMedPacsInfoVO;
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 AMedPacsInfoFacade extends MedPacsInfoServiceImpl {
    private TZDBConn tzDBConn = new TZDBConn();

    @Autowired
    @Qualifier("medPacsInfoServiceImpl")
    private MedPacsInfoServiceImpl medPacsInfoServiceImpl;
    @Autowired
    private AMedAbnormalInfoFacade aMedAbnormalInfoFacade;
    @Value("${log_switch.enable}")
    private boolean logSwitch;

    
    public RespDTO dealData(List<AMedPacsInfoVO> list) {
        try {
            if(list!=null && list.size()>0){
                //循环验证数据有效性
                for (AMedPacsInfoVO aMedPacsInfoVO:list) {
                    if(StringUtil.isEmpty(aMedPacsInfoVO.getRepNo())){
                        return RespDTO.onError("请输入报告单号!");
                    }else if(aMedPacsInfoVO.getHospitalId()==null){
                        return RespDTO.onError("请输入医院编码!");
                    }else if(StringUtil.isEmpty(aMedPacsInfoVO.getBehospitalCode())) {
                        return RespDTO.onError("请输入病人住院编码!");
                    }
                }

                List<MedPacsInfo> medPacsInfoList=Lists.newArrayList();
                list.stream().forEach(s->{
                    MedPacsInfo medPacsInfo=new MedPacsInfo();
                    BeanUtil.copyProperties(s,medPacsInfo);
                    medPacsInfoList.add(medPacsInfo);
                });
                execute(medPacsInfoList);

                if(logSwitch){
                    medPacsInfoList.forEach(s->{
                        aMedAbnormalInfoFacade.saveAbnormalInfo("检查信息-正常","", JSON.toJSONString(list),"","");
                    });
                }
                return RespDTO.onSuc("操作正常！");
            }else{
                return RespDTO.onError("未接收到数据!");
            }
        }catch (Exception e){
            aMedAbnormalInfoFacade.saveAbnormalInfo("检查信息-异常","",JSON.toJSONString(list),"",e.getMessage());
            return RespDTO.onError(e.getMessage());
        }
    }
    
    public void execute(List<MedPacsInfo> medPacsInfoList){
        List<MedPacsInfo> addE = Lists.newLinkedList();
        List<MedPacsInfo> updateE = Lists.newLinkedList();
        if (medPacsInfoList != null && medPacsInfoList.size() > 0) {
            medPacsInfoList.stream().forEach(s -> {
                MedPacsInfo medPacsInfo = this.getOne(new QueryWrapper<MedPacsInfo>()
                        .eq("rep_no", s.getRepNo())
                        .eq("hospital_id", s.getHospitalId()), false);
                if (medPacsInfo != null) {
                    s.setGmtModified(new Date());
                    updateE.add(s);
                } else {
                    s.setGmtCreate(new Date());
                    addE.add(s);
                }
            });
        }
        if(addE.size()>0){
            medPacsInfoServiceImpl.saveBatch(addE);
        }
        if(updateE.size()>0){
            this.baseMapper.updateBatchByKey(updateE);
        }
    }

    public RespDTO deleteFlag(ADeleteFlagVO aDeleteFlagVO){
        try {
            //验证数据是否存在
            if(StringUtil.isEmpty(aDeleteFlagVO.getRepNo())){
                return RespDTO.onError("请输入报告单号!");
            }else if(aDeleteFlagVO.getHospitalId()==null){
                return RespDTO.onError("请输入医院编码!");
            }else {
                UpdateWrapper<MedPacsInfo> updateWrapper=new UpdateWrapper<>();
                updateWrapper.eq("rep_no", aDeleteFlagVO.getRepNo())
                        .eq("hospital_id", aDeleteFlagVO.getHospitalId())
                        .eq("is_deleted", IsDeleteEnum.N)
                        .set("is_deleted",IsDeleteEnum.Y)
                        .set("gmt_modified", DateUtil.now());

                Boolean flag=update(new MedPacsInfo(),updateWrapper);
                //aMedAbnormalInfoFacade.saveAbnormalInfo("检查-删除",JSON.toJSONString(aDeleteFlagVO),JSON.toJSONString(RespDTO.onSuc(flag)),"操作成功!");
                return RespDTO.onSuc(flag);
            }
        }catch (Exception e){
            aMedAbnormalInfoFacade.saveAbnormalInfo("检查信息-删除异常",aDeleteFlagVO.getRecId(), JSON.toJSONString(aDeleteFlagVO),"",e.getMessage());
            return RespDTO.onError(e.getMessage());
        }
    }
}
