package com.diagbot.facade.data;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.data.ABasDoctorInfoDTO;
import com.diagbot.entity.BasDoctorInfo;
import com.diagbot.service.impl.BasDoctorInfoServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.TZDBConn;
import com.diagbot.vo.data.ABasDoctorInfoVO;
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 ABasDoctorInfoFacade extends BasDoctorInfoServiceImpl {
    @Autowired
    @Qualifier("basDoctorInfoServiceImpl")
    private BasDoctorInfoServiceImpl basDoctorInfoService;
    @Autowired
    private AMedAbnormalInfoFacade aMedAbnormalInfoFacade;
    @Value("${log_switch.enable}")
    private boolean logSwitch;
    private TZDBConn tzDBConn = new TZDBConn();

    /**
     * 同步历史数据
     */
    public void executeDoctorPast() {
        try{
            String sql="select * from v_gy_zgxx_ys";
            List<BasDoctorInfo> basDeptInfoList = tzDBConn.getBasDoctorInfo(sql);
            execute(basDeptInfoList);
            aMedAbnormalInfoFacade.saveAbnormalInfo("医生信息更新成功","",JSON.toJSONString(basDeptInfoList), "", "");
        }catch (Exception e){
            aMedAbnormalInfoFacade.saveAbnormalInfo("医生信息更新失败","","", "", e.getMessage());
        }


    }
    /**
     * 同步前一天医生信息
     */
    public void executeDoctor() {
        String sql="select * from gi_userinfo";
        List<BasDoctorInfo> basDeptInfoList = tzDBConn.getBasDoctorInfo(sql);
        execute(basDeptInfoList);
    }
    /**
     * 从接口接收医生信息
     * @param list
     * @return
     */
    public RespDTO<List<ABasDoctorInfoDTO>> executeDoctor(List<ABasDoctorInfoVO> list) {
        try {
            if(list!=null && list.size()>0){
                List<BasDoctorInfo> basDoctorInfoList=Lists.newArrayList();
                basDoctorInfoList=BeanUtil.listCopyTo(list,BasDoctorInfo.class);
                //循环验证数据有效性
                for (BasDoctorInfo basDoctorInfo:basDoctorInfoList) {
                    if("".equals(basDoctorInfo.getDoctorId())) {
                        return RespDTO.onError("请输入医生编码!");
                    }else if(basDoctorInfo.getHospitalId()==null){
                        return RespDTO.onError("请输入医院编码!");
                    }
                }
                execute(basDoctorInfoList);

                if(logSwitch){
                    basDoctorInfoList.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<BasDoctorInfo> basDoctorInfoList){
        List<BasDoctorInfo> addE=Lists.newArrayList();
        List<BasDoctorInfo> updateE=Lists.newArrayList();
        basDoctorInfoList.stream().forEach(s -> {
            BasDoctorInfo basDoctorInfo =this.baseMapper.selectOne(new QueryWrapper<BasDoctorInfo>()
                    .eq("doctor_id", s.getDoctorId())
                    .eq("hospital_id", s.getHospitalId()));
            if (basDoctorInfo != null) {
                s.setGmtModified(new Date());
//                updateE.add(s);
                QueryWrapper<BasDoctorInfo> updateWrapper = Wrappers.query();
                updateWrapper.eq("doctor_id",s.getDoctorId());
                this.update(s,updateWrapper);
            } else {
                s.setGmtCreate(new Date());
                this.save(s);
//                addE.add(s);
            }
        });
        if(updateE.size()>0){
            basDoctorInfoService.updateBatchByKey(updateE);
        }
        if(addE.size()>0){
            basDoctorInfoService.saveBatch(addE);
        }
    }
}
