package com.saye.hrs.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

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.commons.uuid.UUIDGenerator;
import com.saye.hrs.model.StatusDefine;
import com.saye.hrs.model.Users;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.HospitalService;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;

/**
 * 
 *
 * @author dqzhang
 * @since 2021-09-15 09:43:02
 */
@RequestMapping("hospital")
@Controller
public class HospitalController {
   
    @Resource
    private HospitalService hospitalService;

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

		//查询所有角色
		return "hospital/hospital";
	}
    /**
     * 分页查询列表
     * @param  page limit
     * @param 
     * @return 查询结果
     */
    @RequestMapping("/findHospitalPageList")
    @ResponseBody
    public TemplatePage findHospitalPageList(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.hospitalService.findHospitalList(map));
		return PageUtil.loadJsonPage(appsPageInfo);
    }

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

    /**
     * 新增数据
     * @param map
     * @return map
     */
    @RequestMapping("/insertHospital")
    @ResponseBody
    public HashMap<Object,Object> insertHospital(@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{  
            this.hospitalService.insertHospital(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("/updateHospital")
    @ResponseBody
    public HashMap<Object,Object> updateHospital(@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{  
            this.hospitalService.updateHospital(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("/deleteHospitalById")
    @ResponseBody
    public HashMap<Object,Object> deleteHospitalById(@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{  
            this.hospitalService.deleteHospitalById(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  modelMap
     * @param
     * @return
     */
    @RequestMapping("/toHospitalAbout")
    public String toHospitalAbout(ModelMap modelMap){
        List<HashMap<Object, Object>> list = this.hospitalService.findHospitalList(new HashMap<Object,Object>());
        modelMap.addAttribute("list",list);
        return "hospital/hospitalAbout";
    }

    /**
     * 查询医院信息列表
     * @return 查询结果
     */
    @RequestMapping("/findHospitalAbout")
    @ResponseBody
    public List<HashMap<Object, Object>> findHospitalAbout() {

        return this.hospitalService.findHospitalList(new HashMap<Object,Object>());
    }

    /**
     * 更新医院介绍
     * @param map
     * @return map
     */
    @RequestMapping("/updateHospitalAbout")
    @ResponseBody
    public HashMap<Object,Object> updateHospitalAbout(@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{
            this.hospitalService.updateHospitalAbout(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  modelMap
     * @param
     * @return
     */
    @RequestMapping("/toHospitalNotice")
    public String toHospitalNotice(ModelMap modelMap){

        return "hospital/hospitalNotice";
    }

    /** 查询医院通知列表
     * @param title
     * @return
     */
    @RequestMapping("/findHospitalNoticeList")
    @ResponseBody
    public TemplatePage findHospitalNoticeList(String title) {
        HashMap<Object,Object> map = new HashMap<Object,Object>();
        map.put("title",title);
        List<HashMap<Object, Object>> list = this.hospitalService.findHospitalNoticeList(map);
        TemplatePage templetJson = new TemplatePage();
        templetJson.setCode(0);
        templetJson.setMsg("");
        templetJson.setData(list);
        templetJson.setCount(list.size());
        return templetJson;
    }

    /** 查询医院通知列表
     * @param title
     * @return
     */
    @RequestMapping("/findHospitalNotice")
    @ResponseBody
    public List<HashMap<Object, Object>> findHospitalNotice(String title) {
        HashMap<Object,Object> map = new HashMap<Object,Object>();
        map.put("title",title);
        return this.hospitalService.findHospitalNoticeList(map);
    }

    @RequestMapping("/uploadImg")
    @ResponseBody
    public HashMap<Object, Object> uploadImg(MultipartFile file) {

        HashMap<String,String> map=new HashMap<String,String>();
        String errCode = "0";
        String errMsg = "";
        String relative_path = "";
        String uuid = UUIDGenerator.getUUID();
        try {

            String realFileName = file.getOriginalFilename();//获取文件名称
            String suffix = realFileName.substring(realFileName.lastIndexOf(".") + 1);//获取文件后缀名

            String create_date= DateDUtil.getCurrentDate();
            relative_path = "hospitalNotice" + "/" + create_date + "/" + System.currentTimeMillis() + "." + suffix;
            String real_path = StatusDefine.filePath + relative_path;
            Path path = Paths.get(real_path);//设置保存路径
            File f = new File(real_path);
            if (!f.getParentFile().exists()) {
                f.getParentFile().mkdirs();
            }
            byte[] bytes = file.getBytes();
            Files.write(path, bytes);
        } catch (IOException e) {
            errCode = "999";
            errMsg = "上传图片失败！";
            LogUtil.error(this.getClass(), errMsg+"，原因:"+ExceptionDUtil.getDetailExceptionMsg(e));
        }
        HashMap<Object,Object> resMap = new HashMap<Object,Object>();
        resMap.put("relative_path",relative_path);
        resMap.put("errCode", errCode);
        resMap.put("errMsg", errMsg);
        return resMap;
    }

    /**
     * 新增医院通知
     * @param map
     * @return
     */
    @RequestMapping("/addHospitalNotice")
    @ResponseBody
    public HashMap<Object,Object> addHospitalNotice(HttpServletRequest request, @RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{
            Users user = (Users) request.getSession().getAttribute("user");
            map.put("user_name",user.getUserName());
            this.hospitalService.addHospitalNotice(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
     */
    @RequestMapping("/updateHospitalNotice")
    @ResponseBody
    public HashMap<Object,Object> updateHospitalNotice(@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{
            this.hospitalService.updateHospitalNotice(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
     */
    @RequestMapping("/deleteHospitalNotice")
    @ResponseBody
    public HashMap<Object,Object> deleteHospitalNotice(@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{
            this.hospitalService.deleteHospitalNotice(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  modelMap
     * @param
     * @return
     */
    @RequestMapping("/toHospitalArticle")
    public String toHospitalArticle(ModelMap modelMap){

        return "hospital/hospitalArticle";
    }

    /** 查询健康百科列表
     * @param title
     * @return
     */
    @RequestMapping("/findHospitalArticleList")
    @ResponseBody
    public TemplatePage findHospitalArticleList(String title) {
        HashMap<Object,Object> map = new HashMap<Object,Object>();
        map.put("title",title);
        List<HashMap<Object, Object>> list = this.hospitalService.findHospitalArticleList(map);
        TemplatePage templetJson = new TemplatePage();
        templetJson.setCode(0);
        templetJson.setMsg("");
        templetJson.setData(list);
        templetJson.setCount(list.size());
        return templetJson;
    }

    /**
     * 新增健康百科
     * @param map
     * @return
     */
    @RequestMapping("/addHospitalArticle")
    @ResponseBody
    public HashMap<Object,Object> addHospitalArticle(HttpServletRequest request,@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{
            this.hospitalService.addHospitalArticle(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
     */
    @RequestMapping("/updateHospitalArticle")
    @ResponseBody
    public HashMap<Object,Object> updateHospitalArticle(@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{
            this.hospitalService.updateHospitalArticle(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
     */
    @RequestMapping("/deleteHospitalArticle")
    @ResponseBody
    public HashMap<Object,Object> deleteHospitalArticle(@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{
            this.hospitalService.deleteHospitalArticle(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;
    }
    
    /**
     * @description 去就医指南配置页面
     * @author dqzhang       
     * @created 2021年11月15日 下午4:04:23 
     * @param modelMap
     * @return
     */
    @RequestMapping("/toMedicalGuide")
	public String toMedicalGuide(ModelMap modelMap){

		//查询所有角色
		return "hospital/medicalGuide";
	}
    
    /** 查询健康百科列表
     * @param title
     * @return
     */
    @RequestMapping("/findMedicalGuideList")
    @ResponseBody
    public TemplatePage findMedicalGuideList(String name) {
        HashMap<Object,Object> map = new HashMap<Object,Object>();
        map.put("name",name);
        List<HashMap<Object, Object>> list = this.hospitalService.findMedicalGuideList(map);
        TemplatePage templetJson = new TemplatePage();
        templetJson.setCode(0);
        templetJson.setMsg("");
        templetJson.setData(list);
        templetJson.setCount(list.size());
        return templetJson;
    }
    
    /**
     * 新增就医指南
     * @param map
     * @return
     */
    @RequestMapping("/addMedicalGuide")
    @ResponseBody
    public HashMap<Object,Object> addMedicalGuide(HttpServletRequest request,@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{
            Users user = (Users) request.getSession().getAttribute("user");
            map.put("user_name",user.getUserName());
            this.hospitalService.addMedicalGuide(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
     */
    @RequestMapping("/updateMedicalGuide")
    @ResponseBody
    public HashMap<Object,Object> updateMedicalGuide(@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{
            this.hospitalService.updateMedicalGuide(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
     */
    @RequestMapping("/deleteMedicalGuide")
    @ResponseBody
    public HashMap<Object,Object> deleteMedicalGuide(@RequestBody HashMap<Object,Object> map) {
        String errCode = "0";
        String errMsg = "";
        try{
            this.hospitalService.deleteMedicalGuide(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 title
     * @return
     */
    @RequestMapping("/findHospitalFloorList")
    @ResponseBody
    public TemplatePage findHospitalFloorList(String hospital_id) {
        HashMap<Object,Object> map = new HashMap<Object,Object>();
        map.put("HOSPITAL_ID",hospital_id);
        List<HashMap<Object, Object>> list = this.hospitalService.findHospitalFloorList(map);
        TemplatePage templetJson = new TemplatePage();
        templetJson.setCode(0);
        templetJson.setMsg("");
        templetJson.setData(list);
        templetJson.setCount(list.size());
        return templetJson;
    }
    
    /** 新增医院楼层信息
     * @param title
     * @return
     */
    @RequestMapping("/addHospitalFloor")
    @ResponseBody
    public HashMap<Object, Object> addHospitalFloor(@RequestBody HashMap<Object,Object> map) {
    	String errCode = "0";
        String errMsg = "";
        try{
            this.hospitalService.addHospitalFloor(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 title
     * @return
     */
    @RequestMapping("/updateHospitalFloor")
    @ResponseBody
    public HashMap<Object, Object> updateHospitalFloor(@RequestBody HashMap<Object,Object> map) {
    	String errCode = "0";
        String errMsg = "";
        try{
            this.hospitalService.updateHospitalFloor(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 title
     * @return
     */
    @RequestMapping("/deleteHospitalFloor")
    @ResponseBody
    public HashMap<Object, Object> deleteHospitalFloor(@RequestBody HashMap<Object,Object> map) {
    	String errCode = "0";
        String errMsg = "";
        try{
            this.hospitalService.deleteHospitalFloor(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;
    }
    
}

