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.MedPacsResult;
import com.diagbot.enums.IsDeleteEnum;
import com.diagbot.service.impl.MedPacsResultServiceImpl;
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.AMedPacsResultVO;
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 AMedPacsResultFacade extends MedPacsResultServiceImpl {
    @Autowired
    private AMedAbnormalInfoFacade aMedAbnormalInfoFacade;
    @Value("${log_switch.enable}")
    private boolean logSwitch;

    private TZDBConn tzDBConn = new TZDBConn();

    /**
     * 同步前一天
     */
    public void executeMedPacsResult() {
        //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<MedPacsResult> medPacsResultList= tzDBConn.getPacsResult(sql);
        execute(medPacsResultList);
    }

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

                List<MedPacsResult> medPacsResultList=Lists.newArrayList();
                list.stream().forEach(s->{
                    MedPacsResult medPacsResult=new MedPacsResult();
                    BeanUtil.copyProperties(s,medPacsResult);
                    medPacsResultList.add(medPacsResult);
                });
                execute(medPacsResultList);
                if(logSwitch){
                    medPacsResultList.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<MedPacsResult> medPacsResultList){
        List<MedPacsResult> addE = Lists.newLinkedList();
        List<MedPacsResult> updateE = Lists.newLinkedList();
        if (medPacsResultList != null && medPacsResultList.size() > 0) {
            medPacsResultList.stream().forEach(s -> {
                MedPacsResult medPacsResult = this.getOne(new QueryWrapper<MedPacsResult>()
                        .eq("rep_no", s.getRepNo())
                        .eq("hospital_id", s.getHospitalId()), false);
                if (medPacsResult != null) {
                    s.setGmtModified(new Date());
                    updateE.add(s);
                } else {
                    s.setGmtCreate(new Date());
                    this.save(s);
                    addE.add(s);
                }
            });
        }
        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<MedPacsResult> 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 MedPacsResult(),updateWrapper);
                return RespDTO.onSuc(flag);
            }
        }catch (Exception e){
            aMedAbnormalInfoFacade.saveAbnormalInfo("检查-删除","",JSON.toJSONString(aDeleteFlagVO),"",e.getMessage());
            return RespDTO.onError(e.getMessage());
        }
    }
}
