package com.ts.action;

import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;
import com.ts.service.AgentService;
import com.ts.service.CardService;
import com.ts.service.ReaderService;
import com.ts.service.UserService;
import com.ts.vo.Agent;
import com.ts.vo.Card;
import com.ts.vo.Reader;
import com.ts.vo.Users;

public class AjaxAction extends ActionSupport {

	private int userindex;
	private Users user;

	private ReaderService readerService;
	private AgentService agentService;
	private UserService userService;
	private CardService cardService;

	public String checkUserIdAjax() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		Users cuser=userService.getUserById(user.getUserid());
		if(cuser!=null){
			System.out.println("找到user");
			Gson gson=new Gson();
			String rs=gson.toJson(cuser);
			response.getWriter().print(rs);
			System.out.println(rs);
		}else{
			System.out.println("没找到user");
			response.getWriter().print(0);
		}
		
		return null;
	}
	public String checkUserNameAjax() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		Users cuser=userService.getUserByName(user.getUsername());
		if(cuser!=null){
			System.out.println("找到user");
			Gson gson=new Gson();
			String rs=gson.toJson(cuser);
			response.getWriter().print(rs);
			System.out.println(rs);
		}else{
			System.out.println("没找到user");
			response.getWriter().print(0);
		}
		return null;
	}
	// ------------------------------------Reader
	// CRUD-------------------------------
	public String getUserAjax() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Users currUser = (Users) session.getAttribute("user");
		Users fromuser = userService.getUserByName(user.getUsername());
		Card ccard =null;
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json");
		response.setCharacterEncoding("utf-8");
		String result = "";
		if(fromuser != null){
			ccard=cardService.getCardByFromUser(fromuser, currUser);
			if(ccard!=null){
				result = "1";
				response.getWriter().print(result);
				return null;
			}else{
				Gson gson = new Gson();
				result = gson.toJson(fromuser);
			}
		}
		response.getWriter().print(result);
		System.out.println(result);
		return null;
	}

	public String addReaderAjax() throws Exception {
		System.out.println("AjaxAction.addReaderAjax()");
		HttpSession session = ServletActionContext.getRequest().getSession();
		Users currUser = (Users) session.getAttribute("user");
		List<Users> userlist = (List<Users>) session.getAttribute("userlist");
		List<Integer> readerFlags = (List<Integer>) session
				.getAttribute("readerFlags");
		Reader reader = new Reader();
		reader.setReader(userlist.get(userindex));
		reader.setUsers(currUser);
		HttpServletResponse response = ServletActionContext.getResponse();
		if (readerService.addReader(reader)) {
			System.out.println("reader添加成功");
			readerFlags.set(userindex, 1);
			session.setAttribute("readerFlags", readerFlags);
			response.getWriter().print("true");
			return null;
		} else {
			System.out.println("reader添加失败");
			response.getWriter().print(0);
			return null;
		}
	}

	public String deleteReaderAjax() throws Exception {
		System.out.println("AjaxAction.deleteReaderAjax()");
		;
		HttpSession session = ServletActionContext.getRequest().getSession();
		Users currUser = (Users) session.getAttribute("user");
		List<Users> userlist = (List<Users>) session.getAttribute("userlist");
		List<Integer> readerFlags = (List<Integer>) session
				.getAttribute("readerFlags");
		Reader delreader = readerService.getReader(currUser.getUserid(),
				userlist.get(userindex).getUserid());
		System.out.println(delreader);
		HttpServletResponse response = ServletActionContext.getResponse();
		if (readerService.deleteReader(delreader)) {
			System.out.println("删除成功");
			readerFlags.set(userindex, 0);
			session.setAttribute("readerFlags", readerFlags);
			response.getWriter().print("true");
			return null;
		} else {
			System.out.println("删除 失败");
			response.getWriter().print(0);
			return null;
		}
	}

	// --------------------------------Agent CRUD---------------------------

	public String addAgentAjax() throws Exception {
		System.out.println("AjaxAction.addAgentAjax()");
		HttpSession session = ServletActionContext.getRequest().getSession();
		Users currUser = (Users) session.getAttribute("user");
		List<Users> userlist = (List<Users>) session.getAttribute("userlist");
		List<Integer> agentFlags = (List<Integer>) session
				.getAttribute("agentFlags");
		Agent agent = new Agent();
		agent.setAgent(userlist.get(userindex));
		agent.setUsers(currUser);
		HttpServletResponse response = ServletActionContext.getResponse();
		if (agentService.addAgent(agent)) {
			System.out.println("reader添加成功");
			agentFlags.set(userindex, 1);
			session.setAttribute("agentFlags", agentFlags);
			response.getWriter().print("true");
			return null;
		} else {
			System.out.println("reader添加失败");
			response.getWriter().print(0);
			return null;
		}
	}

	public String deleteAgentAjax() throws Exception {
		System.out.println("AjaxAction.deleteAgentAjax()");
		HttpSession session = ServletActionContext.getRequest().getSession();
		Users currUser = (Users) session.getAttribute("user");
		List<Users> userlist = (List<Users>) session.getAttribute("userlist");
		List<Integer> agentFlags = (List<Integer>) session
				.getAttribute("agentFlags");
		Agent delagent = agentService.getAgent(currUser.getUserid(), userlist
				.get(userindex).getUserid());
		HttpServletResponse response = ServletActionContext.getResponse();
		if (agentService.deleteAgent(delagent)) {
			System.out.println("删除成功");
			agentFlags.set(userindex, 0);
			session.setAttribute("agentFlags", agentFlags);
			response.getWriter().print("true");
			return null;
		} else {
			System.out.println("删除 失败");
			response.getWriter().print("false");
			return null;
		}
	}

	private static final long serialVersionUID = 1L;

	public int getUserindex() {
		return userindex;
	}

	public void setUserindex(int userindex) {
		this.userindex = userindex;
	}

	public ReaderService getReaderService() {
		return readerService;
	}

	public void setReaderService(ReaderService readerService) {
		this.readerService = readerService;
	}

	public AgentService getAgentService() {
		return agentService;
	}

	public void setAgentService(AgentService agentService) {
		this.agentService = agentService;
	}

	public Users getUser() {
		return user;
	}

	public void setUser(Users user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public CardService getCardService() {
		return cardService;
	}

	public void setCardService(CardService cardService) {
		this.cardService = cardService;
	}

}
最近下载更多
微信网友_5963854197018624  LV7 2023年4月6日
akbar2020  LV9 2022年9月4日
19857460815  LV1 2022年6月20日
zeze2580  LV2 2022年5月9日
wanglinddad  LV55 2022年3月26日
habiya  LV3 2022年3月14日
ds9527  LV2 2022年3月3日
MarkLee 琥珀川  LV13 2022年1月21日
小豆芽  LV8 2021年6月10日
xufei123  LV4 2021年6月2日
最近浏览更多
张泽帅  LV6 10月22日
krispeng  LV13 8月12日
9605451tjb  LV4 8月7日
yunsgui  LV1 6月25日
liyan54188  LV2 6月25日
暂无贡献等级
26737721  LV4 5月27日
jhyan127 2月18日
暂无贡献等级
米继宝是憨憨  LV4 1月29日
森sdfgf  LV8 1月24日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友