package com.ywt.outpatient.service.batch;

import com.ywt.biz.common.util.Checker;
import com.ywt.biz.common.util.DateUtil;
import com.ywt.gapi.doctor.DoctorHisCode;
import com.ywt.gapi.system.Dept;
import com.ywt.gapi.system.HospitalDept;
import com.ywt.gapi.third.taihe.Schedule;
import com.ywt.outpatient.batch.DoctorHisCodePojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author daiyihua
 * @create 2023-11-12 20:20
 * @program ywt-platform-outpatient-parent
 * @description doctor his code 相关方法
 **/
@Service
public class DoctorHisCodeService {

    @Autowired
    private DoctorServiceSrv doctorServiceSrv;

    @Autowired
    private TaiheServiceSrv taiheServiceSrv;

    @Autowired
    private SystemServiceSrv systemServiceSrv;


    public void syncDoctorHisCodeInfo() throws Exception {
        List<DoctorHisCodePojo> doctorHisCodePojoList = new ArrayList<>();
        List<DoctorHisCodePojo> byDoctorHisCodePojoList = syncDoctorHisCodeInfo(41, 541);
        List<DoctorHisCodePojo> thDoctorHisCodePojoList = syncDoctorHisCodeInfo(12, 0);
        if (!Checker.isNone(byDoctorHisCodePojoList)) {
            doctorHisCodePojoList.addAll(byDoctorHisCodePojoList);
        }
        if (!Checker.isNone(thDoctorHisCodePojoList)) {
            doctorHisCodePojoList.addAll(thDoctorHisCodePojoList);
        }
        if (Checker.isNone(doctorHisCodePojoList)) {
            return;
        }
        // 对 doctorHisCodePojoList 进行去重（deptCode、hisCode、hospitalId）这三个能确定唯一
        List newDoctorHisCodeList = new ArrayList<>();
        Map map = new HashMap();
        for (DoctorHisCodePojo pojo : doctorHisCodePojoList) {
            String key = pojo.getDeptCode() + "_" + pojo.getHisCode() + "_" + pojo.getHospitalId();
            if (Checker.isNone(map.get(key))) {
                map.put(key, key);
                DoctorHisCode doctorHisCode = DoctorHisCode.newBuilder()
                        .setDoctorId(pojo.getDoctorId())
                        .setHospitalId(pojo.getHospitalId())
                        .setDeptCode(pojo.getDeptCode())
                        .setDoctorCode(pojo.getHisCode())
                        .setDoctorName(pojo.getDoctorName())
                        .setDoctorJobNum(pojo.getDoctorJobNum())
                        .build();
                newDoctorHisCodeList.add(doctorHisCode);
            }
        }
        doctorServiceSrv.saveDoctorHisCodeList(newDoctorHisCodeList);
    }


    public List<DoctorHisCodePojo> syncDoctorHisCodeInfo(int hospitalId, int subHospitalId) throws Exception {
        List<HospitalDept> deptList = systemServiceSrv.getHospitalDeptList(hospitalId, subHospitalId);
        if (Checker.isNone(deptList)) {
            return null;
        }
        List<Dept> secondDeptList = new ArrayList<>();
        for (HospitalDept dept : deptList) {
            if (!Checker.isNone(dept.getDeptListList())) {
                secondDeptList.addAll(dept.getDeptListList());
            }
        }
        if (Checker.isNone(secondDeptList)) {
            return null;
        }
        List<DoctorHisCodePojo> doctorHisCodePojoList = new ArrayList<>();
        for (Dept dept : secondDeptList) {
            Calendar calendar = Calendar.getInstance();
            String strDate = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
            // 不包括今天
            calendar.add(Calendar.DATE, 7);
            // 日期
            String endDate = DateUtil.formatDate(calendar.getTime(), "yyyy-MM-dd");
            List<Schedule> scheduleList = taiheServiceSrv.getScheduleList(strDate, endDate, "", dept.getDeptCode(), hospitalId);
            if (!Checker.isNone(scheduleList)) {
                for (Schedule schedule : scheduleList) {
                    DoctorHisCodePojo pojo = new DoctorHisCodePojo();
                    pojo.setDeptCode(dept.getDeptCode());
                    pojo.setDoctorId(0);
                    pojo.setDoctorJobNum("");
                    pojo.setDoctorName(schedule.getDoctorName());
                    pojo.setHisCode(schedule.getDoctorCode());
                    pojo.setHospitalId(hospitalId);
                    doctorHisCodePojoList.add(pojo);
                }
            }
        }
        return doctorHisCodePojoList;
    }
}
