package com.yihu.wlyy.service.app.scheduling;

import com.yihu.wlyy.entity.doctor.profile.Doctor;
import com.yihu.wlyy.entity.doctor.schedule.DoctorFamousConsultTimesRemain;
import com.yihu.wlyy.entity.doctor.schedule.WlyyDoctorWorkTime;
import com.yihu.wlyy.entity.doctor.schedule.WlyyDoctorWorkWeek;
import com.yihu.wlyy.repository.doctor.DoctorDao;
import com.yihu.wlyy.repository.doctor.DoctorWorkTimeDao;
import com.yihu.wlyy.repository.doctor.DoctorWorkWeekDao;
import com.yihu.wlyy.repository.doctor.FamousDoctorTimesRemainDao;
import com.yihu.wlyy.service.BaseService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.eval.StringValueEval;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.print.Doc;
import javax.transaction.Transactional;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 医生工作排班
 * <p>
 * Created by lyr on 2016/08/19.
 */
@Service
@Transactional
public class DoctorWorkTimeService extends BaseService {

    static Object obj = new Object();
    @Autowired
    private DoctorWorkTimeDao doctorWorkTimeDao;
    @Autowired
    private DoctorWorkWeekDao doctorWorkWeekDao;
    @Autowired
    private DoctorDao doctorDao;
    @Autowired
    private FamousDoctorTimesRemainDao timesRemainDao;

    /**
     * 查询医生工作时间列表
     *
     * @param doctor 医生标识
     * @return
     */
    public JSONObject findDoctorWorkTime(String doctor) throws Exception {
        JSONObject result = new JSONObject();
        Doctor doc = doctorDao.findByCode(doctor);
        if (doc == null) {
            throw new Exception("doctor not exist");
        }
        WlyyDoctorWorkTime workTime = doctorWorkTimeDao.findDoctorWorkTime(doctor);
        List<WlyyDoctorWorkWeek> workWeekList = doctorWorkWeekDao.findDoctorWorkWeeks(doctor);

        result.put("workTime", workTime != null ? new JSONObject(workTime) : "");
        result.put("workWeeks", workWeekList != null ? new JSONArray(workWeekList) : "");
        result.put("doctor", doc != null ? new JSONObject(doc) : "");

        return result;
    }

    /**
     * 查询医生某天工作时间
     *
     * @param doctor 医生标识
     * @param week   某天
     * @return
     */
    public JSONObject findDoctorWeekWorkTime(String doctor, String week) throws Exception {
        JSONObject result = new JSONObject();
        WlyyDoctorWorkTime workTime = doctorWorkTimeDao.findDoctorWorkTime(doctor);
        WlyyDoctorWorkWeek workWeek = doctorWorkWeekDao.findDoctorWorkWeek(doctor, week);

        result.put("workTime", workTime != null ? new JSONObject(workTime) : "");
        result.put("workWeek", workWeek != null ? new JSONObject(workWeek) : "");

        return result;
    }

    /**
     * 查询医生某天工作时间
     *
     * @param doctor 医生标识
     * @param week   某天
     * @return
     */
    public Map<String, Object> findDoctorWeekWork(String doctor, String week) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Doctor doc = doctorDao.findByCode(doctor);
        if (doc == null) {
            throw new Exception("doctor-worktime-error:doctor not exist");
        }
        WlyyDoctorWorkTime workTime = doctorWorkTimeDao.findDoctorWorkTime(doctor);
        WlyyDoctorWorkWeek workWeek = doctorWorkWeekDao.findDoctorWorkWeek(doctor, week);

        map.put("workTime", workTime);
        map.put("workWeek", workWeek);


        return map;
    }

    /**
     * 保存医生工作排班
     *
     * @param doctor         医生
     * @param workScheduling 排班配置
     * @return
     */
    public boolean saveDoctorWorkTime(String doctor, JSONObject workScheduling) throws Exception {

        JSONObject workTime = workScheduling.has("workTime") ? workScheduling.getJSONObject("workTime") : null;
        JSONArray workTWeeks = workScheduling.has("workWeeks") ? workScheduling.getJSONArray("workWeeks") : null;

        WlyyDoctorWorkTime doctorWorkTime = doctorWorkTimeDao.findDoctorWorkTime(doctor);
        Doctor doc = doctorDao.findByCode(doctor);

        if (StringUtils.isEmpty(doctor) || doc == null) {
            throw new Exception("找不到对应医生!");
        }
        if (workTime == null) {
            throw new Exception("工作时间段不能为空!");
        }

        if (doctorWorkTime == null) {
            doctorWorkTime = new WlyyDoctorWorkTime();
            doctorWorkTime.setReceiveConsult("1");
            doctorWorkTime.setFamousConsultTimes(0);
        }

        // 医生
        doctorWorkTime.setDoctor(doctor);
        // 医生姓名
        doctorWorkTime.setDoctorName(doc.getName());
        // 上午开始时间
        doctorWorkTime.setMorningBegin(workTime.getString("morningBegin"));
        // 上午结束时间
        doctorWorkTime.setMorningEnd(workTime.getString("morningEnd"));
        // 下午开始时间
        doctorWorkTime.setAfternoonBegin(workTime.getString("afternoonBegin"));
        // 下午结束时间
        doctorWorkTime.setAfternoonEnd(workTime.getString("afternoonEnd"));
        // 晚上开始时间
        doctorWorkTime.setNightBegin(workTime.getString("nightBegin"));
        // 晚上结束时间
        doctorWorkTime.setNightEnd(workTime.getString("nightEnd"));
        // 更新时间
        doctorWorkTime.setCzrq(new Date());

        doctorWorkTimeDao.save(doctorWorkTime);
        doctorWorkWeekDao.deleteByDoctor(doctor);

        // 排班保存
        if (workTWeeks != null) {
            List<WlyyDoctorWorkWeek> weeks = new ArrayList<>();

            for (int i = 0; i < workTWeeks.length(); i++) {
                JSONObject object = workTWeeks.getJSONObject(i);
                WlyyDoctorWorkWeek week = new WlyyDoctorWorkWeek();

                week.setDoctor(doctor);
                week.setDoctorName(doc.getName());
                week.setWeek(object.getString("week"));
                week.setMorning(object.getString("morning"));
                week.setAfternoon(object.getString("afternoon"));
                week.setNight(object.getString("night"));
                week.setCzrq(new Date());

                weeks.add(week);
            }

            doctorWorkWeekDao.save(weeks);
        }

        if(doc.getIsFamous() != null && doc.getIsFamous() == 1){
            JSONObject json =  isDoctorWorkingToday(doctor);

            if(json.getString("status").equals("1")){
                DoctorFamousConsultTimesRemain timesRemain = timesRemainDao.findByDoctorAndConsultDate(doctor,
                        new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

                if (timesRemain == null) {
                    timesRemain = new DoctorFamousConsultTimesRemain();
                    timesRemain.setDoctor(doctor);
                    timesRemain.setTimesRemain(doctorWorkTime.getFamousConsultTimes());
                    timesRemain.setConsultDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

                    timesRemainDao.save(timesRemain);
                }
            }
        }

        return true;
    }

    /**
     * 医生设置是否接受咨询
     *
     * @param doctor         医生
     * @param receiveConsult 是否接受咨询
     * @return
     */
    public boolean setDoctorReceiveConsult(String doctor, String receiveConsult) throws Exception {
        try {
            WlyyDoctorWorkTime workTime = doctorWorkTimeDao.findDoctorWorkTime(doctor);
            Doctor doc = doctorDao.findByCode(doctor);

            if (doc == null) {
                throw new Exception("doctor not exist");
            }

            // 未有设置，则新建医生设置
            if (workTime == null) {
                workTime = new WlyyDoctorWorkTime();
                workTime.setDoctor(doctor);
                workTime.setDoctorName(doc.getName());
                workTime.setFamousConsultTimes(0);
            }

            // 设置是否接受咨询
            workTime.setReceiveConsult(receiveConsult);
            workTime.setCzrq(new Date());

            doctorWorkTimeDao.save(workTime);

            if (doc.getIsFamous() != null && doc.getIsFamous() == 1) {
                JSONObject json = isDoctorWorkingToday(doctor);

                if (json.getString("status").equals("1")) {
                    DoctorFamousConsultTimesRemain timesRemain = timesRemainDao.findByDoctorAndConsultDate(doctor,
                            new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

                    if (timesRemain == null) {
                        timesRemain = new DoctorFamousConsultTimesRemain();
                        timesRemain.setDoctor(doctor);
                        timesRemain.setTimesRemain(workTime.getFamousConsultTimes());
                        timesRemain.setConsultDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

                        timesRemainDao.save(timesRemain);
                    }
                }
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 医生是否在工作
     *
     * @param doctor 医生
     * @return
     */
    public JSONObject isDoctorWorking(String doctor) throws Exception {
        JSONObject json = new JSONObject();
        Calendar calendar = Calendar.getInstance();
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;

        if (week == 0) {
            week = 7;
        }

        // 医生工作时间设置
        Map<String, Object> result = findDoctorWeekWork(doctor, String.valueOf(week));

        if (result.get("workTime") == null) {
            // 医生未设置时，默认7*24小时工作
            json.put("status", "1");
            json.put("msg", "医生当前接受咨询");
        } else {
            WlyyDoctorWorkTime doctorWorkTime = (WlyyDoctorWorkTime) result.get("workTime");

            if (doctorWorkTime.getReceiveConsult().equals("0")) {
                // 医生设置不接受咨询
                json.put("status", "0");
                json.put("msg", "医生不接受咨询");
            } else {
                if (StringUtils.isEmpty(doctorWorkTime.getMorningBegin()) && StringUtils.isEmpty(doctorWorkTime.getMorningEnd())
                        && StringUtils.isEmpty(doctorWorkTime.getAfternoonBegin()) && StringUtils.isEmpty(doctorWorkTime.getAfternoonEnd())
                        && StringUtils.isEmpty(doctorWorkTime.getNightBegin()) && StringUtils.isEmpty(doctorWorkTime.getNightEnd())) {
                    // 医生未设置工作时间，默认7*24小时工作
                    json.put("status", "1");
                    json.put("msg", "医生当前接受咨询");
                } else {
                    if (result.get("workWeek") != null) {
                        // 当前工作日已设置工作时间
                        int flag = 0;
                        WlyyDoctorWorkTime workTime = (WlyyDoctorWorkTime) result.get("workTime");
                        WlyyDoctorWorkWeek workWeek = (WlyyDoctorWorkWeek) result.get("workWeek");
                        int hour = calendar.get(Calendar.HOUR_OF_DAY);
                        int minute = calendar.get(Calendar.MINUTE);
                        String current = (hour < 10 ? ("0" + hour) : hour) + ":" + (hour < 10 ? ("0" + minute) : minute);

                        // 早上
                        if (workWeek.getMorning().equals("1")) {
                            String currentStart = workTime.getMorningBegin();
                            String currentEnd = workTime.getMorningEnd();
                            if (currentStart.length() == 4) {
                                currentStart = "0" + currentStart;
                            }
                            if (currentEnd.length() == 4) {
                                currentEnd = "0" + currentEnd;
                            }
                            if (current.compareTo(currentStart) >= 0 &&
                                    current.compareTo(currentEnd) < 0) {
                                flag = 1;
                            }
                        }
                        // 下午
                        if (workWeek.getAfternoon().equals("1")) {
                            String currentStart = workTime.getAfternoonBegin();
                            String currentEnd = workTime.getAfternoonEnd();
                            if (currentStart.length() == 4) {
                                currentStart = "0" + currentStart;
                            }
                            if (currentEnd.length() == 4) {
                                currentEnd = "0" + currentEnd;
                            }
                            if (current.compareTo(currentStart) >= 0 &&
                                    current.compareTo(currentEnd) < 0) {
                                flag = 1;
                            }
                        }
                        //　晚上
                        if (workWeek.getNight().equals("1")) {
                            String currentStart = workTime.getNightBegin();
                            String currentEnd = workTime.getNightEnd();
                            if (currentStart.length() == 4) {
                                currentStart = "0" + currentStart;
                            }
                            if (currentEnd.length() == 4) {
                                currentEnd = "0" + currentEnd;
                            }
                            if (current.compareTo(currentStart) >= 0 &&
                                    current.compareTo(currentEnd) < 0) {
                                flag = 1;
                            }
                        }

                        if (flag == 1) {
                            json.put("status", "1");
                            json.put("msg", "医生当前接受咨询");
                        } else {
                            json.put("status", "2");
                            json.put("msg", "医生当前不在工作时间");
                        }
                    } else {
                        json.put("status", "2");
                        json.put("msg", "医生当前不在工作时间");
                    }
                }
            }
        }

        return json;
    }

    /**
     * 名医是否在工作查询
     *
     * @param doctor
     * @return
     * @throws Exception
     */
    public JSONObject isFamousDoctorWorking(String doctor) throws Exception{
        JSONObject json = new JSONObject();
        Calendar calendar = Calendar.getInstance();
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;

        if (week == 0) {
            week = 7;
        }

        // 医生工作时间设置
        Map<String, Object> result = findDoctorWeekWork(doctor, String.valueOf(week));

        if (result.get("workTime") == null) {
            // 医生未设置时，默认7*24小时工作
            json.put("status", "1");
            json.put("msg", "医生当前接受咨询");
        } else {
            WlyyDoctorWorkTime doctorWorkTime = (WlyyDoctorWorkTime) result.get("workTime");

            if (doctorWorkTime.getReceiveConsult().equals("0")) {
                // 医生设置不接受咨询
                json.put("status", "0");
                json.put("msg", "医生不接受咨询");
            } else {
                if (StringUtils.isEmpty(doctorWorkTime.getMorningBegin()) && StringUtils.isEmpty(doctorWorkTime.getMorningEnd())
                        && StringUtils.isEmpty(doctorWorkTime.getAfternoonBegin()) && StringUtils.isEmpty(doctorWorkTime.getAfternoonEnd())
                        && StringUtils.isEmpty(doctorWorkTime.getNightBegin()) && StringUtils.isEmpty(doctorWorkTime.getNightEnd())) {
                    // 医生未设置工作时间，默认7*24小时工作
                    json.put("status", "1");
                    json.put("msg", "医生当前接受咨询");
                } else {
                    if (result.get("workWeek") != null) {
                        // 当前工作日已设置工作时间
                        int flag = 0;
                        WlyyDoctorWorkTime workTime = (WlyyDoctorWorkTime) result.get("workTime");
                        WlyyDoctorWorkWeek workWeek = (WlyyDoctorWorkWeek) result.get("workWeek");
                        int hour = calendar.get(Calendar.HOUR_OF_DAY);
                        int minute = calendar.get(Calendar.MINUTE);
                        String start = "";
                        String end ="";
                        String current = (hour < 10 ? ("0" + hour) : hour) + ":" + (hour < 10 ? ("0" + minute) : minute);

                        // 早上
                        if (workWeek.getMorning().equals("1")) {
                            String currentStart = workTime.getMorningBegin();
                            String currentEnd = workTime.getMorningEnd();
                            if (currentStart.length() == 4) {
                                currentStart = "0" + currentStart;
                            }
                            if (currentEnd.length() == 4) {
                                currentEnd = "0" + currentEnd;
                            }

                            start = currentStart;
                            end = currentEnd;
                        }
                        // 下午
                        if (workWeek.getAfternoon().equals("1")) {
                            String currentStart = workTime.getAfternoonBegin();
                            String currentEnd = workTime.getAfternoonEnd();
                            if (currentStart.length() == 4) {
                                currentStart = "0" + currentStart;
                            }
                            if (currentEnd.length() == 4) {
                                currentEnd = "0" + currentEnd;
                            }
                            if(StringUtils.isEmpty(start)) {
                                start = currentStart;
                            }
                            end = currentEnd;
                        }
                        //　晚上
                        if (workWeek.getNight().equals("1")) {
                            String currentStart = workTime.getNightBegin();
                            String currentEnd = workTime.getNightEnd();
                            if (currentStart.length() == 4) {
                                currentStart = "0" + currentStart;
                            }
                            if (currentEnd.length() == 4) {
                                currentEnd = "0" + currentEnd;
                            }
                            if(StringUtils.isEmpty(start)) {
                                start = currentStart;
                            }
                            end = currentEnd;
                        }

                        if(current.compareTo(start) >= 0 && current.compareTo(end) < 0){
                            flag = 1;
                        }

                        if (flag == 1) {
                            json.put("status", "1");
                            json.put("msg", "医生当前接受咨询");
                        } else {
                            json.put("status", "2");
                            json.put("msg", "医生当前不在工作时间");
                        }
                    } else {
                        json.put("status", "2");
                        json.put("msg", "医生当前不在工作时间");
                    }
                }
            }
        }

        return json;
    }

    /**
     * 查询名医当天是否接受咨询
     *
     * @param doctor
     * @return
     * @throws Exception
     */
    public JSONObject isDoctorWorkingToday(String doctor) throws Exception {
        JSONObject json = new JSONObject();
        Calendar calendar = Calendar.getInstance();
        int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;

        if (week == 0) {
            week = 7;
        }

        // 医生工作时间设置
        Map<String, Object> result = findDoctorWeekWork(doctor, String.valueOf(week));

        if (result.get("workTime") == null) {
            // 医生未设置时，默认7*24小时工作
            json.put("status", "1");
            json.put("times", 0);
        } else {
            WlyyDoctorWorkTime doctorWorkTime = (WlyyDoctorWorkTime) result.get("workTime");

            if (doctorWorkTime.getReceiveConsult().equals("0")) {
                json.put("status", "0");
                json.put("times", 0);
            } else {
                if (StringUtils.isEmpty(doctorWorkTime.getMorningBegin()) && StringUtils.isEmpty(doctorWorkTime.getMorningEnd())
                        && StringUtils.isEmpty(doctorWorkTime.getAfternoonBegin()) && StringUtils.isEmpty(doctorWorkTime.getAfternoonEnd())
                        && StringUtils.isEmpty(doctorWorkTime.getNightBegin()) && StringUtils.isEmpty(doctorWorkTime.getNightEnd())) {
                    // 医生未设置工作时间，默认7*24小时工作
                    json.put("status", "1");
                    json.put("times", doctorWorkTime.getFamousConsultTimes());
                } else {
                    if (result.get("workWeek") != null) {
                        WlyyDoctorWorkWeek workWeek = (WlyyDoctorWorkWeek) result.get("workWeek");
                        boolean flag = false;

                        if (StringUtils.isNotEmpty(workWeek.getMorning()) && workWeek.getMorning().equals("1")) {
                            json.put("status", "1");
                            json.put("times", doctorWorkTime.getFamousConsultTimes());
                            flag = true;
                        }
                        if (StringUtils.isNotEmpty(workWeek.getAfternoon()) && workWeek.getAfternoon().equals("1")) {
                            json.put("status", "1");
                            json.put("times", doctorWorkTime.getFamousConsultTimes());
                            flag = true;
                        }
                        if (StringUtils.isNotEmpty(workWeek.getNight()) && workWeek.getNight().equals("1")) {
                            json.put("status", "1");
                            json.put("times", doctorWorkTime.getFamousConsultTimes());
                            flag = true;
                        }

                        if (!flag) {
                            json.put("status", "0");
                            json.put("times", 0);
                        }
                    } else {
                        json.put("status", "0");
                        json.put("times", 0);
                    }
                }
            }
        }

        return json;
    }

    /**
     * 设置名医咨询次数
     *
     * @param doctor 医生
     * @return
     */
    public boolean setFamousDoctorConsultTimes(String doctor, int consultTimes) {
        try {
            WlyyDoctorWorkTime workTime = doctorWorkTimeDao.findDoctorWorkTime(doctor);
            Doctor doc = doctorDao.findByCode(doctor);
            int plus = 0;

            if (doc == null) {
                throw new Exception("doctor can not find");
            }

            // 未有设置，则新建医生设置
            if (workTime == null) {
                workTime = new WlyyDoctorWorkTime();
                workTime.setDoctor(doctor);
                workTime.setDoctorName(doc.getName());
                workTime.setReceiveConsult("1");
            } else {
                plus = consultTimes - workTime.getFamousConsultTimes();
            }

            // 设置名医咨询次数
            workTime.setFamousConsultTimes(consultTimes);
            workTime.setCzrq(new Date());

            doctorWorkTimeDao.save(workTime);

            if (doc.getIsFamous() != null && doc.getIsFamous() == 1) {
                JSONObject json = isDoctorWorkingToday(doctor);

                if (json.getString("status").equals("1")) {
                    DoctorFamousConsultTimesRemain timesRemain = timesRemainDao.findByDoctorAndConsultDate(doctor,
                            new SimpleDateFormat("yyyy-MM-dd").format(new Date()));

                    if (timesRemain == null) {
                        timesRemain = new DoctorFamousConsultTimesRemain();
                        timesRemain.setDoctor(doctor);
                        timesRemain.setTimesRemain(consultTimes);
                        timesRemain.setConsultDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
                    } else {
                        if (plus >= 0) {
                            timesRemain.setTimesRemain(timesRemain.getTimesRemain() + plus);
                        } else {
                            if(workTime.getFamousConsultTimes() >= Math.abs(plus)){
                                timesRemain.setTimesRemain(timesRemain.getTimesRemain()  - Math.abs(plus));
                            }else{
                                timesRemain.setTimesRemain(0);
                            }
                        }
                    }

                    timesRemainDao.save(timesRemain);
                }
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 名医咨询当前剩余次数查询
     *
     * @param doctor 医生
     * @return
     */
    public int getDoctorConsultTimesRemain(String doctor) {
        int result = 0;
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        DoctorFamousConsultTimesRemain timesRemain = timesRemainDao.findByDoctorAndConsultDate(doctor, df.format(new Date()));

        if (timesRemain != null) {
            result = timesRemain.getTimesRemain();
        } else {
            WlyyDoctorWorkTime workTime = doctorWorkTimeDao.findDoctorWorkTime(doctor);

            if (workTime != null) {
                result = workTime.getFamousConsultTimes();
            }
        }

        return result;
    }

    /**
     * 名医咨询当前剩余次数减一
     *
     * @param doctor 医生
     * @return
     */
    public boolean setDoctorCurrentConsultTimesRemain(String doctor) {
        try {
            synchronized (obj) {
                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                DoctorFamousConsultTimesRemain timesRemain = timesRemainDao.findByDoctorAndConsultDate(doctor, df.format(new Date()));

                if (timesRemain != null) {
                    int times = timesRemain.getTimesRemain();

                    if (times < 1) {
                        throw new Exception("consult-times-error:doctor does not have not consult times remain");
                    }

                    timesRemain.setTimesRemain(times - 1);
                    timesRemainDao.save(timesRemain);
                } else {
                    DoctorFamousConsultTimesRemain timesRemainNew = new DoctorFamousConsultTimesRemain();
                    WlyyDoctorWorkTime workTime = doctorWorkTimeDao.findDoctorWorkTime(doctor);

                    if (workTime == null || workTime.getFamousConsultTimes() < 1) {
                        throw new Exception("consult-times-error:doctor does not have consult times setting");
                    }

                    timesRemainNew.setDoctor(doctor);
                    timesRemainNew.setConsultDate(df.format(new Date()));
                    timesRemainNew.setTimesRemain(workTime.getFamousConsultTimes() - 1);
                    timesRemainDao.save(timesRemainNew);
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 名医咨询次数更新
     *
     * @param date
     */
    @Transactional
    public void consultTimesRemain(String date) {
        try {
            List<Doctor> doctors = doctorDao.findByIsFamous(1);

            for (Doctor doc : doctors) {
                JSONObject json = isDoctorWorkingToday(doc.getCode());

                if (json.getString("status").equals("1")) {
                    DoctorFamousConsultTimesRemain timesRemain = timesRemainDao.findByDoctorAndConsultDate(doc.getCode(), date);
                    if (timesRemain == null) {
                        timesRemain = new DoctorFamousConsultTimesRemain();
                    }else{
                        continue;
                    }
                    timesRemain.setDoctor(doc.getCode());
                    timesRemain.setConsultDate(date);
                    timesRemain.setTimesRemain(json.getInt("times"));
                    timesRemainDao.save(timesRemain);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
