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.MedLisResult;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.impl.MedLisResultServiceImpl;
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.AMedLisResultVO;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

@Component
public class AMedLisResultFacade extends MedLisResultServiceImpl {
    @Autowired
    private AMedAbnormalInfoFacade aMedAbnormalInfoFacade;
    @Value("${log_switch.enable}")
    private boolean logSwitch;
    private TZDBConn tzDBConn = new TZDBConn();

    /**
     * 同步前一天
     */
    public void executeMedLisResult() {
        //String sql="select * from br_rechome where cjcxrq>=dateadd(day,-2,getdate()) and cjcxrq<=getdate()";
        //同步前一天的数据
        String sql="select * from br_rechome where cjcxrq>=(select CONVERT(varchar,GETDATE()-2,23)) and cjcxrq<(select CONVERT(varchar,GETDATE(),23))";
        List<MedLisResult> medLisResultList= tzDBConn.getLisResult(sql);
        execute(medLisResultList);
    }

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

                List<MedLisResult> medLisResultList=Lists.newArrayList();
                list.stream().forEach(s->{
                    MedLisResult medLisResult=new MedLisResult();
                    BeanUtil.copyProperties(s,medLisResult);
                    medLisResultList.add(medLisResult);
                });
                execute(medLisResultList);
                if(logSwitch){
                    medLisResultList.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<MedLisResult> medLisResultList){
        if (medLisResultList != null && medLisResultList.size() > 0) {
            medLisResultList.stream().forEach(s -> {
                    s.setGmtCreate(new Date());
                    this.save(s);
            });
        }
    }

    public RespDTO deleteFlag(ADeleteFlagVO aDeleteFlagVO){
        try {
            //验证数据是否存在
            if(StringUtil.isEmpty(aDeleteFlagVO.getRepNo())){
                return RespDTO.onError("请输入报告单号!");
            }else if(aDeleteFlagVO.getHospitalId()==null){
                return RespDTO.onError("请输入医院编码!");
            }else {
                UpdateWrapper<MedLisResult> 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 MedLisResult(),updateWrapper);
                //aMedAbnormalInfoFacade.saveAbnormalInfo("检验-删除",JSON.toJSONString(aDeleteFlagVO),JSON.toJSONString(RespDTO.onSuc(flag)),"操作成功!");
                return RespDTO.onSuc(flag);
            }
        }catch (Exception e){
            aMedAbnormalInfoFacade.saveAbnormalInfo("检验-删除","",JSON.toJSONString(aDeleteFlagVO),"",e.getMessage());
            return RespDTO.onError(e.getMessage());
        }
    }
}
