首页>代码>ssm+easyui开发的校医院上网行为管理系统>/school_guahao/src/com/gxw/controller/HospitalController.java
package com.gxw.controller;

import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.gxw.page.Page;
import com.gxw.pojo.Hospital;
import com.gxw.service.HospitalService;
import com.gxw.util.StringUtil;

import net.sf.json.JSONArray;

/**
 * 医院管理控制器
 * 
 *@author GXWadmin
 *
 */
@RequestMapping("/hospital")
@Controller
public class HospitalController {
	@Autowired
	private HospitalService hospitalService;
	
	/**
	 * 医院列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public ModelAndView list(ModelAndView model){
		model.setViewName("hospital/hospital_list");
		return model;
	}
	
	/**
	 * 获取医院列表页面
	 * @param page
	 * @param hName
	 * @param hTelephone
	 * @param hAddress
	 * @return
	 */
	@RequestMapping(value="/get_list",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getList(
			@RequestParam(value="hName",required=false,defaultValue="") String hName,
			Page page
			){
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("hName", "%"+hName+"%");
		queryMap.put("offset", page.getOffset());
		queryMap.put("pageSize", page.getRows());
		map.put("rows", hospitalService.findList(queryMap));
		map.put("total", hospitalService.getTotal(queryMap));
		return map;
	}
	
	/**
	 * 编辑医院信息
	 * @param hospital
	 * @return
	 */
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> edit(Hospital hospital){
		Map<String, String> ret = new HashMap<String, String>();
		if(StringUtils.isEmpty(hospital.gethName())){
			ret.put("type", "error");
			ret.put("msg", "名称不能为空!");
			return ret;
		}
		if(StringUtils.isEmpty(hospital.gethTelephone())){
			ret.put("type", "error");
			ret.put("msg", "电话不能为空!");
			return ret;
		}
		if(StringUtils.isEmpty(hospital.gethAddress())){
			ret.put("type", "error");
			ret.put("msg", "地址不能为空!");
			return ret;
		}
		if(hospitalService.edit(hospital) <= 0){
			ret.put("type", "error");
			ret.put("msg", "医院信息添加失败!");
			return ret;
		}
		ret.put("type", "success");
		ret.put("msg", "医院信息修改成功!");
		return ret;
	}
	
	/**
	 * 添加医院信息
	 * @param hospital
	 * @return
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> add(Hospital hospital){
		Map<String, String> ret = new HashMap<String, String>();
		if(StringUtils.isEmpty(hospital.gethName())){
			ret.put("type", "error");
			ret.put("msg", "名称不能为空!");
			return ret;
		}
		if(StringUtils.isEmpty(hospital.gethTelephone())){
			ret.put("type", "error");
			ret.put("msg", "电话不能为空!");
			return ret;
		}
		if(StringUtils.isEmpty(hospital.gethAddress())){
			ret.put("type", "error");
			ret.put("msg", "地址不能为空!");
			return ret;
		}
		if(hospitalService.add(hospital)<= 0){
			ret.put("type", "error");
			ret.put("msg", "医院信息添加失败");
			return ret;
		}
		ret.put("type", "success");
		ret.put("msg", "医院信息修改成功!");
		return ret;
	}
	
	/**
	 * 删除医院信息
	 * @param hids
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> delete(
			@RequestParam(value="hids[]",required=true) Long[] hids
			){
		Map<String, String> ret = new HashMap<String, String>();
		//id为空或长度为0
		if(hids == null || hids.length == 0){
			ret.put("type", "error");
			ret.put("msg", "请选择要删除的数据!");
			return ret;
		}
		try {
			if(hospitalService.delete(StringUtil.joinString(Arrays.asList(hids), ",")) <= 0){
				ret.put("type", "error");
				ret.put("msg", "删除失败!");
				return ret;
			}
		} catch (Exception e) {
			// TODO: handle exception
			ret.put("type", "error");
			ret.put("msg", "该医院信息已经存在!");
			return ret;
		}
		ret.put("type", "success");
		ret.put("msg", "信息删除成功!");
		return ret;
	}
	
	/**
	 * 上传图片
	 * @param hPhoto
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/upload_hPhoto",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, String> uploadhPhoto(MultipartFile hPhoto,HttpServletRequest request){
		Map<String, String> ret = new HashMap<String, String>();
		if(hPhoto == null){
			ret.put("type", "error");
			ret.put("msg", "选择要上传的文件!");
			return ret;
		}
		if(hPhoto.getSize() > 1024*1024*1024){
			ret.put("type", "error");
			ret.put("msg", "文件大小不能超过10M!");
			return ret;
		}
		//获取文件后缀
		String suffix = hPhoto.getOriginalFilename().substring(hPhoto.getOriginalFilename().lastIndexOf(".")+1,hPhoto.getOriginalFilename().length());
		if(!"jpg,jpeg,gif,png".toUpperCase().contains(suffix.toUpperCase())){
			ret.put("type", "error");
			ret.put("msg", "请选择jpg,jpeg,gif,png格式的图片!");
			return ret;
		}
		String savePath = request.getServletContext().getRealPath("/") + "/resources/upload/";
		File savePathFile = new File(savePath);
		if(!savePathFile.exists()){
			//若不存在改目录,则创建目录
			savePathFile.mkdir();
		}
		String filename = new Date().getTime()+"."+suffix;
		try {
			//将文件保存至指定目录
			hPhoto.transferTo(new File(savePath+filename));
		}catch (Exception e) {
			// TODO Auto-generated catch block
			ret.put("type", "error");
			ret.put("msg", "保存文件异常!");
			e.printStackTrace();
			return ret;
		}
		ret.put("type", "success");
		ret.put("msg", "用户删除成功!");
		ret.put("filepath",request.getServletContext().getContextPath() + "/resources/upload/" + filename );
		return ret;
	}
}
最近下载更多
tangguo666666  LV4 2023年6月5日
zxc131313  LV12 2022年10月23日
AlanLi  LV18 2022年6月17日
109683670  LV9 2022年4月18日
java代写  LV7 2022年3月15日
wanglinddad  LV54 2022年2月15日
lilong007  LV20 2021年12月20日
893213895  LV18 2021年12月16日
deck222  LV9 2021年12月12日
KSY247  LV5 2021年11月10日
最近浏览更多
爱丽淇  LV5 3月19日
a318888331  LV13 3月10日
FF加菲猫  LV4 2月15日
iiiiiiixiiiiii  LV1 2023年12月28日
ziv5466123  LV7 2023年12月15日
cyz521213  LV1 2023年12月9日
mukoooo  LV2 2023年12月5日
内心向阳  LV4 2023年11月8日
lyq6666666  LV5 2023年10月25日
沉默66666 2023年10月5日
暂无贡献等级
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友