/**
*
*/
package jxhk.action;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import jxhk.action.base.UserBaseAction;
import jxhk.model.Dept;
import jxhk.model.User;
import jxhk.util.ChangeUtil;
import jxhk.util.DateJsonValueProcessor;
/**
* @author lenovo
*
*/
public class UserAction extends UserBaseAction {
//封装请求参数
private User user;
//登录的验证码
private String vercode;
//处理登录后的提示信息
private String tip;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getVercode() {
return vercode;
}
public void setVercode(String vercode) {
this.vercode = vercode;
}
public String getTip() {
return tip;
}
public void setTip(String tip) {
this.tip = tip;
}
private JSONObject resultObj;
//处理用户请求
public String login()
throws Exception
{
//创建ActionContext实例
ActionContext ctx = ActionContext.getContext();
//获取HttpSession中的rand属性
String ver2 = (String)ctx.getSession().get("rand");
if (true) //vercode.equalsIgnoreCase(ver2)
{
User u;
//调用业务逻辑方法来处理登录请求
u = mgr.validLogin(getUser());
if (u!=null)
{
ctx.getSession().put(WebConstant.USERNAME , u.getName());
ctx.getSession().put(WebConstant.USERID, u.getUserId());
ctx.getSession().put(WebConstant.LEVEL, u.getPowerId());
setTip("您已经成功登录系统");
return SUCCESS;
}else{//用户名和密码不匹配
setTip("用户名/密码不匹配或无权限!");
return ERROR;
}
}else{ //验证码不匹配
setTip("验证码不匹配,请重新输入");
return ERROR;
}
}
//处理用户请求
@SuppressWarnings("unchecked")
public void getAllUser()
throws Exception{
HttpServletResponse response = ServletActionContext.getResponse();
Map<String,Object> map = new HashMap<String, Object>();
JSONObject json=new JSONObject();
JsonConfig config=new JsonConfig();
JSONArray jsonArray;
int tt=0;
try{
tt=mgr.getCount();
}catch(Exception ex){
System.out.println(ex.toString());
}
try{
List list = mgr.queryByPage(ChangeUtil.toInteger(this.getPage()), ChangeUtil.toInteger(this.getRows()));
for(int i =0;i<list.size();i++){
User user;
user = (User)list.get(i);
System.out.println( user.getName());
}
config.registerJsonValueProcessor(Timestamp.class,new DateJsonValueProcessor("yyyy-MM-dd HH:mm"));
jsonArray = JSONArray.fromObject(list,config);
json.accumulate("page", 1); //当前页
json.accumulate("records",list.size());// 总记录数
json.accumulate("total", tt);// 总页数
json.accumulate("rows", jsonArray);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(jsonArray.toString());
response.flushBuffer();
//System.out.println(jsonArray.toString());
}catch(Exception ex){
System.out.println(" " + ex.toString());
}
//return "root";
}
//处理用户请求
@SuppressWarnings("unchecked")
public void saveUser()
throws Exception{
//创建ActionContext实例
ActionContext ctx = ActionContext.getContext();
HttpServletResponse response = ServletActionContext.getResponse();
String msg;
String tip;
int cnt=0;
JSONObject json=new JSONObject();
User user ;
Boolean saveFlag = false;
try{
user = super.getUser();
if(user.getUserId() == 0 ){ //添加记录
saveFlag = mgr.saveUser(user);
tip ="添加";
}else{
tip ="修改";
saveFlag = mgr.update(user);
}
if (saveFlag) {
msg = tip + "成功!";
try{
cnt=mgr.getCount();
}catch(Exception ex){
System.out.println(ex.toString());
}
JsonConfig config=new JsonConfig();
List list = mgr.queryByPage(ChangeUtil.toInteger(this.getPage()), ChangeUtil.toInteger(this.getRows()));
config.registerJsonValueProcessor(Timestamp.class,new DateJsonValueProcessor("yyyy-MM-dd"));
JSONArray jsonArray=JSONArray.fromObject(list,config);
json.accumulate("msg", msg);
json.accumulate("success", "1");
json.accumulate("page", 1); //当前页
json.accumulate("records",list.size());// 总记录数
json.accumulate("total", cnt);// 总页数
json.accumulate("rows", jsonArray);
System.out.println(jsonArray.toString());
}else{
msg = tip + "失败!";
json.accumulate("msg", msg);
json.accumulate("success", "0");
System.out.println("部门信息保存出错!");
}
}catch(Exception ex){
System.out.println(ex.toString());
}
this.setResultObj(json);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(this.getResultObj().toString());
response.flushBuffer();
}
//删除部门信息
public void deleteUser()throws Exception{
//创建ActionContext实例
ActionContext ctx = ActionContext.getContext();
HttpServletResponse response = ServletActionContext.getResponse();
Map<String,Object> map = new HashMap<String, Object>();
JSONObject json=new JSONObject();
User user= new User();
user.setUserId(ChangeUtil.toInteger(this.getUserId()));
if(user.getUserId() == 0 ){
json.accumulate("success", "0");
json.accumulate("msg", "用户信息获取失败!");
}else{
try{
if (mgr.delById(user.getUserId())==true) {
int cnt=0;
try{
cnt=mgr.getCount();
}catch(Exception ex){
System.out.println(ex.toString());
}
JsonConfig config=new JsonConfig();
List list = mgr.queryByPage(ChangeUtil.toInteger(this.getPage()), ChangeUtil.toInteger(this.getRows()));
config.registerJsonValueProcessor(Timestamp.class,new DateJsonValueProcessor("yyyy-MM-dd"));
JSONArray jsonArray=JSONArray.fromObject(list,config);
json.accumulate("success", "1");
json.accumulate("page", 1); //当前页
json.accumulate("records",list.size());// 总记录数
json.accumulate("total", cnt);// 总页数
json.accumulate("rows", jsonArray);
}else{
json.accumulate("success", "0");
System.out.println("部门信息保存出错!");
}
}catch(Exception ex){
System.out.println(ex.toString());
}
}
this.setResultObj(json);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(this.getResultObj().toString());
response.flushBuffer();
}
//获取所有用户信息
public void userList()throws Exception{
System.out.println("userList....");
HttpServletResponse response = ServletActionContext.getResponse();
JSONArray jsonArray;
List<User> list= mgr.userList();
jsonArray=JSONArray.fromObject(list);
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(jsonArray.toString());
response.flushBuffer();
System.out.println("userList:"+ jsonArray.toString());
}
public JSONObject getResultObj() {
return resultObj;
}
public void setResultObj(JSONObject resultObj) {
this.resultObj = resultObj;
}
}