package com.yihu.wlyy.web.doctor.scheduling;

import com.yihu.wlyy.entity.doctor.profile.Doctor;
import com.yihu.wlyy.service.app.account.DoctorInfoService;
import com.yihu.wlyy.service.app.scheduling.DoctorWorkTimeService;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * Created by lyr on 2016/08/19.
 */
@Controller
@RequestMapping(value = "/doctor/work_scheduling")
@Api(description = "医生端-排班时间")
public class DoctorWorkTimeController extends BaseController {

    @Autowired
    private DoctorInfoService doctorInfoService;
    @Autowired
    private DoctorWorkTimeService doctorWorkTimeService;

    /**
     * 查询医生工作时间设置
     *
     * @return
     */
    @RequestMapping(value = "/all")
    @ResponseBody
    public String getDoctorWorkTime() {
        try {
            JSONObject workTime = doctorWorkTimeService.findDoctorWorkTime(getUID());

            return write(200, "查询成功!", "data", workTime);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败!");
        }
    }

    /**
     * 获取医生某天工作时间
     *
     * @param week 某天
     * @return
     */
    @RequestMapping(value = "/week")
    @ResponseBody
    public String getDoctorWeekWorkTime(String week) {
        try {
            JSONObject workTime = doctorWorkTimeService.findDoctorWeekWorkTime(getUID(), week);

            return write(200, "查询成功!", "data", workTime);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败!");
        }
    }

    /**
     * 医生排班保存
     *
     * @param workScheduling 医生排班
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public String saveDoctorWorkTime(String workScheduling) {
        try {
            if (StringUtils.isEmpty(workScheduling)) {
                return error(-1, "排班信息不能为空!");
            }

            JSONObject object = new JSONObject(workScheduling);

            if (!(object.has("workTime") && object.getJSONObject("workTime") != null)) {
                return error(-1, "工作时间段不能为空!");
            }

            JSONObject workTime = object.getJSONObject("workTime");

            if (StringUtils.isEmpty(workTime.getString("morningBegin"))) {
                return error(-1, "上午起始时间不能为空!");
            }
            if (StringUtils.isEmpty(workTime.getString("morningEnd"))) {
                return error(-1, "上午结束时间不能为空!");
            }
            if (StringUtils.isEmpty(workTime.getString("afternoonBegin"))) {
                return error(-1, "下午起始时间不能为空!");
            }
            if (StringUtils.isEmpty(workTime.getString("afternoonEnd"))) {
                return error(-1, "下午结束时间不能为空!");
            }
            if (StringUtils.isEmpty(workTime.getString("nightBegin"))) {
                return error(-1, "晚上起始时间不能为空!");
            }
            if (StringUtils.isEmpty(workTime.getString("nightEnd"))) {
                return error(-1, "晚上结束时间不能为空!");
            }

            if (doctorWorkTimeService.saveDoctorWorkTime(getUID(), object)) {
                return write(200, "保存成功!");
            } else {
                return write(-1, "保存失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "保存失败!");
        }
    }


    /**
     * 设置医生是否接受咨询
     *
     * @param receiveConsult 是否接受咨询
     * @return
     */
    @RequestMapping(value = "/receive_consult_setting")
    @ResponseBody
    public String setDoctorReceiveConsult(String receiveConsult) {
        try {
            if (StringUtils.isEmpty(receiveConsult)) {
                return error(-1, "是否接受咨询参数不能为空");
            }
            if (doctorWorkTimeService.setDoctorReceiveConsult(getUID(), receiveConsult)) {
                return error(200, "设置成功");
            } else {
                return error(-1, "设置失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "设置失败");
        }
    }

    /**
     * 设置名医咨询次数
     *
     * @param consultTimes 名医咨询次数
     * @return
     */
    @RequestMapping(value = "/consult_times_setting")
    @ResponseBody
    public String setFamousDoctorConsultTime(int consultTimes) {
        try {
            Doctor doc = doctorInfoService.findDoctorByCode(getUID());

            if (doc == null) {
                return error(-1,"医生不存在");
            }
            if (doc.getIsFamous() == null || doc.getIsFamous() != 1) {
                return error(-1,"医生不是名医，无法设置名医咨询次数");
            }

            if (doctorWorkTimeService.setFamousDoctorConsultTimes(getUID(), consultTimes)) {
                return error(200, "设置成功");
            } else {
                return error(-1, "设置失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "设置失败");
        }
    }

    /**
     * 查询某个医生是否在工作时间
     *
     * @param doctor 医生
     * @return
     */
    @RequestMapping(value = "/is_doctor_working")
    @ResponseBody
    public String isDoctorWorking(String doctor) {
        try {
            if (StringUtils.isEmpty(doctor)) {
                return error(-1, "参数不能为空");
            }
            JSONObject result = doctorWorkTimeService.isDoctorWorking(doctor);
            return write(200, "查询成功", "data", result);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }

    /**
     * 名医是否在工作
     *
     * @param doctor
     * @return
     */
    @RequestMapping(value = "is_famous_doctor_working")
    @ResponseBody
    public String isFamousDoctorAtWorking(String doctor){
        try{
            JSONObject result = doctorWorkTimeService.isFamousDoctorWorking(doctor);
            return write(200,result.getString("msg"),"data",result.getString("status"));
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"查询失败");
        }
    }

    /**
     * 查询某个医生的工作时间
     *
     * @param doctor
     * @return
     */
    @RequestMapping(value = "/doctor_worktime")
    @ResponseBody
    public String getDoctorWorkTime(String doctor) {
        try {
            JSONObject json = doctorWorkTimeService.findDoctorWorkTime(doctor);
            return write(200, "查询成功", "data", json);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }

    /**
     * 查询某个医生某天的工作时间
     *
     * @param doctor 医生
     * @param week   周几
     * @return
     */
    @RequestMapping(value = "/doctor_week_worktime")
    @ResponseBody
    public String getDoctorWeekWorkTime(String doctor, String week) {
        try {
            JSONObject json = doctorWorkTimeService.findDoctorWeekWorkTime(doctor, week);
            return write(200, "查询成功", "data", json);
        } catch (Exception e) {
            e.printStackTrace();
            return error(-1, "查询失败");
        }
    }

    /**
     * 名医咨询剩余次数查询
     *
     * @return
     */
    @RequestMapping(value = "/consult_times_remain")
    @ResponseBody
    public String famousDoctorTimesRemain(){
        try{
            Doctor doc = doctorInfoService.findDoctorByCode(getUID());

            if (doc == null) {
                return error(-1,"医生不存在");
            }
            if (doc.getIsFamous() != 1) {
                return error(-1,"医生不是名医，没有名医咨询次数");
            }
            int result = doctorWorkTimeService.getDoctorConsultTimesRemain(getUID());
            return write(200,"查询成功","data",result);
        }catch (Exception e){
            e.printStackTrace();
            return error(-1,"查询失败");
        }
    }
}
