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.ABasDeptInfoDTO;
import com.diagbot.entity.BasDeptInfo;
import com.diagbot.service.impl.BasDeptInfoServiceImpl;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.TZDBConn;
import com.diagbot.vo.data.ABasDeptInfoVO;
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 ABasDeptInfoFacade extends BasDeptInfoServiceImpl{

    @Autowired
    @Qualifier("basDeptInfoServiceImpl")
    private BasDeptInfoServiceImpl basDeptInfoService;
    @Autowired
    private AMedAbnormalInfoFacade aMedAbnormalInfoFacade;
    @Value("${log_switch.enable}")
    private boolean logSwitch;

    private TZDBConn tzDBConn = new TZDBConn();

    /**
     * 同步前一天科室的信息
     */
    public void executeDept() {
        String sql="select * from hi_deptinfo where cjcxrq>=dateadd(day,-2,getdate()) and cjcxrq<=getdate()";
        List<BasDeptInfo> basDeptInfoList = tzDBConn.getDeptInfo(sql);
        execute(basDeptInfoList);
    }

    /**
     * 同步历史数据
     */
    public void executeDeptPast(){
        String sql="select * from hi_deptinfo where cjcxrq>=dateadd(day,-1,getdate()) and cjcxrq<=getdate()";
        List<BasDeptInfo> basDeptInfoList = tzDBConn.getDeptInfo(sql);
        execute(basDeptInfoList);
    }

    /**
     * 通过接口同步数据
     * @param list
     * @return
     */
    public RespDTO<List<ABasDeptInfoDTO>> executeDept(List<ABasDeptInfoVO> list) {
        try{
            if(list!=null && list.size()>0){
                List<BasDeptInfo> basDeptInfoList=Lists.newArrayList();
                basDeptInfoList=BeanUtil.listCopyTo(list,BasDeptInfo.class);

                //循环验证数据有效性
                for (BasDeptInfo basDeptInfo:basDeptInfoList) {
                    if("".equals(basDeptInfo.getDeptId())) {
                        return RespDTO.onError("请输入科室编码!");
                    }else if(basDeptInfo.getHospitalId()==null){
                        return RespDTO.onError("请输入医院编码!");
                    }else if("".equals(basDeptInfo.getDeptName())){
                        return RespDTO.onError("请输入科室名称!");
                    }
                }
                execute(basDeptInfoList);

                if(logSwitch){
                    basDeptInfoList.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<BasDeptInfo> basDeptInfoList){
        List<BasDeptInfo> addE=Lists.newArrayList();
        List<BasDeptInfo> updateE=Lists.newArrayList();
        basDeptInfoList.stream().forEach(s -> {
            BasDeptInfo basDeptInfo =this.getOne(new QueryWrapper<BasDeptInfo>()
                    .eq("dept_id", s.getDeptId())
                    .eq("hospital_id", s.getHospitalId()), false);
            if (basDeptInfo != null) {
                s.setGmtModified(new Date());
                updateE.add(s);
            } else {
                s.setGmtCreate(new Date());
                addE.add(s);
            }
        });
        if(updateE.size()>0){
            basDeptInfoService.updateBatchByKey(updateE);
        }
        if(addE.size()>0){
            basDeptInfoService.saveBatch(addE);
        }
    }
}
