package com.saye.hrs.controller;

import javax.annotation.Resource;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saye.hrs.commons.date.DateDUtil;
import com.saye.hrs.commons.log.ExceptionDUtil;
import com.saye.hrs.commons.log.LogUtil;
import com.saye.hrs.commons.page.PageUtil;
import com.saye.hrs.commons.page.TemplatePage;
import com.saye.hrs.service.HospitalService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.saye.hrs.service.DoctorService;
import java.util.HashMap;
import java.util.List;

import org.springframework.ui.ModelMap;

/**
 * 
 *
 * @author dqzhang
 * @since 2021-09-16 15:17:12
 */
@RequestMapping("doctor")
@Controller
public class DoctorController {
   
    @Resource
    private DoctorService doctorService;
    @Resource
    private HospitalService hospitalService;

    /**
     * 跳转页面
     * @param  modelMap
     * @param 
     * @return 
     */
    @RequestMapping("/toDoctor")
	public String toUserManager(ModelMap modelMap){

		//查询所有角色
		return "hospital/doctor";
	}
    /**
     * 分页查询列表
     * @param  page limit
     * @param 
     * @return 查询结果
     */
    @RequestMapping("/findDoctorPageList")
    @ResponseBody
    public TemplatePage findDoctorPageList(Integer page,Integer limit) {
        HashMap<Object,Object> map=new HashMap<Object,Object>();
        PageHelper.startPage(page, limit);
		PageInfo<HashMap<Object,Object>> appsPageInfo = new PageInfo<HashMap<Object,Object>>(this.doctorService.findDoctorList(map));
		return PageUtil.loadJsonPage(appsPageInfo);
    }

    /**
     * 通过主键查询单条数据
     * @param map
     * @return 单条数据
     */
    @RequestMapping("/findDoctorById")
    @ResponseBody
    public HashMap<Object,Object> findDoctorById(@RequestBody HashMap<Object,Object> map) {
        return this.doctorService.findDoctorById(map);
    }

    /**
     * 新增数据
     * @param map
     * @return map
     */
    @RequestMapping("/insertDoctor")
    @ResponseBody
    public HashMap<Object,Object> insertDoctor(@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{  
            this.doctorService.insertDoctor(map);
        } catch (Exception e) {
            errCode = "999";
            errMsg = "新增失败";
			LogUtil.error(this.getClass(), "新增失败，原因:"+ExceptionDUtil.getDetailExceptionMsg(e));
		}
		HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
		resultMap.put("errCode",errCode);
		resultMap.put("errMsg",errMsg);
		return resultMap;
    }

    /**
     * 编辑数据
     * @param map
     * @return map
     */
    @RequestMapping("/updateDoctor")
    @ResponseBody
    public HashMap<Object,Object> updateDoctor(@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{  
            this.doctorService.updateDoctor(map);
        } catch (Exception e) {
            errCode = "999";
            errMsg = "修改失败";
			LogUtil.error(this.getClass(), "修改失败，原因:"+ExceptionDUtil.getDetailExceptionMsg(e));
		}
		HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
		resultMap.put("errCode",errCode);
		resultMap.put("errMsg",errMsg);
		return resultMap;
    }

    /**
     * 删除数据
     * @param map
     * @return map
     */
    @RequestMapping("/deleteDoctorById")
    @ResponseBody
    public HashMap<Object,Object> deleteDoctorById(@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{  
            this.doctorService.deleteDoctorById(map);
        } catch (Exception e) {
            errCode = "999";
            errMsg = "删除失败";
			LogUtil.error(this.getClass(), "删除失败，原因:"+ExceptionDUtil.getDetailExceptionMsg(e));
		}
		HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
		resultMap.put("errCode",errCode);
		resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    @RequestMapping("/toDoctorSc")
    public String toDoctorSc(ModelMap modelMap){

        //查询所有角色
        return "hospital/doctorSc";
    }

    /** 去医生排班页面
     * @param modelMap
     * @return
     */
    @RequestMapping("/toDoctorSchedual")
    public String toDoctorSchedual(ModelMap modelMap){
        modelMap.addAttribute("list", this.hospitalService.findHospitalList(new HashMap<Object, Object>()));
        //查询所有角色
        return "hospital/doctorSchedual";
    }

    /** 查询医生排班列表
     * @param hospital_id week
     * @return
     */
    @RequestMapping("/findDoctorSchedualList")
    @ResponseBody
    public HashMap<Object, Object> findDoctorSchedualList(String hospital_id, String week) {
        HashMap<Object,Object> map = new HashMap<Object,Object>();
        map.put("hospital_id",hospital_id);
        List<String> array = DateDUtil.findWeekDays(week);
        map.put("dayList", array);
        List<HashMap<Object, Object>> list = this.doctorService.findDoctorSchedualList(map);
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("dayList",array);
        resultMap.put("list",list);
        return resultMap;
    }
}

