package com.freak.controller;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
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.freak.page.Page;
import com.freak.pojo.Doctor;
import com.freak.service.DoctorService;
/**
* ҽ����Ϣ����
*
*
*/
@RequestMapping("/doctor")
@Controller
public class DoctorController {
@Autowired
private DoctorService doctorService;
/**
* ҽ����Ϣ�б�
*
* @param model
* @return
*/
@RequestMapping(value = "/list", method = RequestMethod.GET)
public ModelAndView tolist(ModelAndView model) {
model.setViewName("doctor/doctor_list");
return model;
}
/**
* ��ȡҽ����Ϣ�б�
*
* @param aName
* @param page
* @return
*/
@RequestMapping(value = "/get_list", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> getList(
@RequestParam(value = "dName", required = false, defaultValue = "") String dName,
Page page) {
Map<String, Object> map = new HashMap<String, Object>();
Map<String, Object> queryMap = new HashMap<String, Object>();
queryMap.put("dName", "%" + dName + "%");
queryMap.put("offset", page.getOffset());
queryMap.put("pageSize", page.getRows());
map.put("rows", doctorService.findList(queryMap));
map.put("total", doctorService.getTotal(queryMap));
return map;
}
/**
* ���ҽ������
*
* @param doctor
* @return
*/
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> toadd(Doctor doctor) {
Map<String, String> ret = new HashMap<String, String>();
if (doctor == null) {
ret.put("type", "error");
ret.put("msg", "���ݰ�������ϵ��̨����Ա!");
return ret;
}
if (StringUtils.isEmpty(doctor.getdName())) {
ret.put("type", "error");
ret.put("msg", "����������!");
return ret;
}
if (StringUtils.isEmpty(doctor.getdSex())) {
ret.put("type", "error");
ret.put("msg", "�Ա���Ϊ��!");
return ret;
}
if (StringUtils.isEmpty(doctor.getdYear())) {
ret.put("type", "error");
ret.put("msg", "��ҽ������Ϊ��!");
return ret;
}
if (StringUtils.isEmpty(doctor.getdPhone())) {
ret.put("type", "error");
ret.put("msg", "�ֻ��Ų���Ϊ��!");
return ret;
}
if (StringUtils.isEmpty(doctor.getdRole())) {
ret.put("type", "error");
ret.put("msg", "��ɫ����Ϊ��!");
return ret;
}
Doctor existDoctor = doctorService.findBydName(doctor.getdName());
if (existDoctor != null) {
ret.put("type", "error");
ret.put("msg", "��ҽ�������Ѿ�����!");
return ret;
}
if (doctorService.add(doctor) <= 0) {
ret.put("type", "error");
ret.put("msg", "���ʧ��!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "��ӳɹ�!");
return ret;
}
/**
* �༭����Ա����
*
* @param doctor
* @return
*/
@RequestMapping(value = "/edit", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> edit(Doctor doctor) {
Map<String, String> ret = new HashMap<String, String>();
if (doctor == null) {
ret.put("type", "error");
ret.put("msg", "���ݰ�������ϵ��̨����Ա!");
return ret;
}
if (StringUtils.isEmpty(doctor.getdName())) {
ret.put("type", "error");
ret.put("msg", "����������!");
return ret;
}
if (StringUtils.isEmpty(doctor.getdSex())) {
ret.put("type", "error");
ret.put("msg", "�Ա���Ϊ��!");
return ret;
}
if (StringUtils.isEmpty(doctor.getdYear())) {
ret.put("type", "error");
ret.put("msg", "��ҽ������Ϊ��!");
return ret;
}
if (StringUtils.isEmpty(doctor.getdPhone())) {
ret.put("type", "error");
ret.put("msg", "�ֻ��Ų���Ϊ��!");
return ret;
}
if (StringUtils.isEmpty(doctor.getdRole())) {
ret.put("type", "error");
ret.put("msg", "��ɫ����Ϊ��!");
return ret;
}
Doctor existDoctor = doctorService.findBydName(doctor.getdName());
if (existDoctor != null) {
ret.put("type", "error");
ret.put("msg", "��ҽ�������Ѿ�����!");
return ret;
}
if (doctorService.edit(doctor) <= 0) {
ret.put("type", "error");
ret.put("msg", "��ʧ��!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "�ijɹ�!");
return ret;
}
/**
* ɾ������Ա����
*
* @param doctor
* @return
*/
@RequestMapping(value = "/delete", method = RequestMethod.POST)
@ResponseBody
public Map<String, String> delete(
@RequestParam(value = "dIds[]", required = true) int[] dIds) {
Map<String, String> ret = new HashMap<String, String>();
if (dIds == null) {
ret.put("type", "error");
ret.put("msg", "��ѡ��Ҫɾ��������!");
return ret;
}
String dIdsString = "";
for (int dId : dIds) {
dIdsString += dId + ",";
}
dIdsString = dIdsString.substring(0, dIdsString.length() - 1);
if (doctorService.delete(dIdsString) <= 0) {
ret.put("type", "error");
ret.put("msg", "ɾ��ʧ��!");
return ret;
}
ret.put("type", "success");
ret.put("msg", "ɾ���ɹ�!");
return ret;
}
/**
* �ϴ�ͼƬ
* @param dPhoto
* @param request
* @return
*/
@RequestMapping(value="/upload_dPhoto",method=RequestMethod.POST)
@ResponseBody
public Map<String, String> uploaddPhoto(MultipartFile dPhoto,HttpServletRequest request){
Map<String, String> ret = new HashMap<String, String>();
if(dPhoto == null){
ret.put("type", "error");
ret.put("msg", "ѡ��Ҫ�ϴ����ļ���");
return ret;
}
if(dPhoto.getSize() > 1024*1024*1024){
ret.put("type", "error");
ret.put("msg", "�ļ���С���ܳ���10M��");
return ret;
}
//��ȡ�ļ���
String suffix = dPhoto.getOriginalFilename().substring(dPhoto.getOriginalFilename().lastIndexOf(".")+1,dPhoto.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 {
//���ļ�������ָ��Ŀ¼
dPhoto.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;
}
}