package com.nutz.module;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Attr;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Filters;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import com.nutz.bean.Student;
@IocBean
@At("/student")
@Ok("json:{locked:'password',ignoreNull:true}")
@Fail("http:500")
public class StudentModule {
@Inject
protected Dao dao;
@At
public int count(){
return dao.count(Student.class);
}
@At
@Ok(">>:/")
public void logout(HttpSession session){
session.invalidate();
}
@At
@Filters
public Object login(@Param("studentName")String name,@Param("password")String password,HttpSession session) {
Student student=dao.fetch(Student.class, Cnd.where("studentName","=",name).and("password", "=",password));
if(student==null){
return false;
}else{
session.setAttribute("me", student.getId());
return true;
}
}
protected String checkStudent(Student student,boolean create){
if(student==null){
return "空对象";
}
if(create){
if(Strings.isBlank(student.getStudentName())||Strings.isBlank(student.getPassword()))
return "用户名/密码不能为空";
}else{
if(Strings.isBlank(student.getPassword()))
return "密码不能为空";
}
String password=student.getPassword().trim();
if(6>password.length()||password.length()>12){
return "密码长度6~12位";
}
student.setPassword(password);
if(create){
int count=dao.count(Student.class, Cnd.where("name", "=", student.getStudentName()));
if(count !=0){
return "学生已存在";
}
}else{
if(student.getId()<1){
return "学生Id非法";
}
}
if(student.getStudentName() !=null)
student.setStudentName(student.getStudentName().trim());
return null;
}
@At
@Ok("json:full")
public Object add(@Param("..")Student student){
NutMap re=new NutMap();
String msg=checkStudent(student,true);
if(msg !=null){
return re.setv("ok", false).setv("msg", msg);
}
student=dao.insert(student);
return re.setv("ok", true).setv("data", student);
}
@At
public Object update(@Param("..")Student student){
NutMap re=new NutMap();
String msg=checkStudent(student,false);
if(msg !=null){
return re.setv("ok", false).setv("msg", msg);
}
student.setStudentName(null);
dao.updateIgnoreNull(student);
re.setv("ok", true);
return student;
}
@At
public Object delete(@Param("id")int id, @Attr("me")int me) {
if (me == id) {
return new NutMap().setv("ok", false).setv("msg", "不能删除当前学生!!");
}
dao.delete(Student.class, id);
return new NutMap().setv("ok", true);
}
@At
public List query(@Param("name")String name,HttpServletRequest request){
Cnd cnd=Strings.isBlank(name)? null: Cnd.where("name","like","%"+name+"%");
QueryResult qr=new QueryResult();
qr.setList(dao.query(Student.class, cnd));
return qr.getList();
}
}