package com.ts.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ts.dao.imp.NoticeDAO;
import com.ts.service.DepartmentService;
import com.ts.service.RoleService;
import com.ts.service.UserService;
import com.ts.vo.Department;
import com.ts.vo.Notice;
import com.ts.vo.Page;
import com.ts.vo.Roles;
import com.ts.vo.Users;

public class AdminAction extends ActionSupport {
	private Department dept;
	private Roles role;
	private Users user;
	private int departmentid;
	private int roleid;
	private int userid;
	private int currPage;
	private int select_index;
	private String select_key;
	private int select_dept;
	private int select_contactor;
	private String flag;
	private int index;
	private int noticeid;
	private Notice notice;
	private String content;
	// ===============up:Field Objects--------- down:Services=======================
	private DepartmentService deptService;
	private RoleService roleService;
	private UserService userService;
	
	private NoticeDAO noticeDao;

	// ----------------------------Action Methods
	// ---------------------------------

	public String assignRoleAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Roles role = roleService.getRoleById(roleid);
		Users olduser = userService.getUserById(userid);
		if (role != null && olduser != null) {
			olduser.setRoles(role);
			if (userService.editUser(olduser)) {
				return "listuser";
			}else
				session.setAttribute("errorinfo", "角色分配失败!可能该用户或者角色数据已经被并发修改");
				return "error";
		} else {
			session.setAttribute("errorinfo", "角色分配失败!可能该用户或者角色数据已经被并发修改");
			return "error";
		}
	}
	
	public String getRolesAndDeptsAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		List<Department> dlist=deptService.listDepartment();
		List<Roles> rlist=roleService.listRole();
		session.setAttribute("dlist", dlist);
		session.setAttribute("rlist", rlist);
		if("edit".equals(flag)){
			List<Users> userlist=(List<Users>)session.getAttribute("userlist");
			Users edituser=userlist.get(index);
			session.setAttribute("edituser", edituser);
			return "gotoEditUser";
		}else{
			return "gotoAddUser";
		}
	}
	public String addUserAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (userService.addUser(user)) {
			return "listuser";
		} else {
			session.setAttribute("errorinfo", "增加失败!请确认输入数据正确!");
			return "error";
		}
	}

	public String editUserAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Users olduser=userService.getUserById(user.getUserid());
		olduser.setPassword(user.getPassword());
		olduser.setPosition(user.getPosition());
		olduser.setDepartment(deptService.getDepartmentById(user.getDepartment().getDepartmentid()));
		olduser.setRoles(roleService.getRoleById(user.getRoles().getRoleid()));
		olduser.setSex(user.getSex());
		olduser.setIntroduction(user.getIntroduction());
		if (userService.editUser(olduser)) {
			session.setAttribute("editinfo", "修改成功!");
			return "listuser";
		} else {
			session.setAttribute("errorinfo", "修改失败,请确认输入数据正确");
			return "error";

		}

	}

	public String listUserAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();

		int counts = userService.getUserCount();
		int pageSize = 12;
		int pages = counts % pageSize == 0 ? counts / pageSize : counts
				/ pageSize + 1;
		if (currPage >= pages) {
			currPage = pages;
		}
		if (currPage <= 1) {
			currPage = 1;
		}
		int startIndex = (currPage - 1) * pageSize + 1;
		int endIndex = startIndex + pageSize - 1 > counts ? counts : startIndex
				+ pageSize - 1;
		Page p = new Page();
		p.setCounts(counts);
		p.setCurrPage(currPage);
		p.setEndIndex(endIndex);
		p.setPageSize(pageSize);
		p.setStartIndex(startIndex);
		p.setPages(pages);
		List<Users> userlist = userService.listUser(p);
		System.out.println(p.getCounts());
		System.out.println(p.getCurrPage());
		System.out.println(userlist.size());
		session.setAttribute("userlist", userlist);
		session.setAttribute("currPage", currPage);
		session.setAttribute("page", p);
		return "listusersuc";
	}

	public String conditionlistUserAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		String hqlcount = "";
		String hqllist = "";
		switch (select_index) {
		case 0:
			hqlcount = "select count(*) from com.ts.vo.Users where "
					+ "userid like '%"
					+ select_key
					+ "%' or "
					+ "username like '%"
					+ select_key
					+ "%' or "
					+ " phone like '%"
					+ select_key
					+ "%' or "
					+ " mobilephone like '%"
					+ select_key
					+ "%' or "
					+ "email like '%"
					+ select_key
					+ "%' or "
					+ "position like '%"
					+ select_key
					+ "%' or "
					+ "address like '%"
					+ select_key
					+ "%' or "
					+ "sex like '%"
					+ select_key + "%'";
			hqllist = "from com.ts.vo.Users where " + "userid like '%"
					+ select_key + "%' or " + "username like '%" + select_key
					+ "%' or " + " phone like '%" + select_key + "%' or "
					+ " mobilephone like '%" + select_key + "%' or "
					+ "email like '%" + select_key + "%' or "
					+ "position like '%" + select_key + "%' or "
					+ "address like '%" + select_key + "%' or " + "sex like '%"
					+ select_key + "%'";
			break;
		case 1:
			hqlcount = "select count(*) from com.ts.vo.Users where userid like '%"
					+ select_key + "%'";
			hqllist = "from com.ts.vo.Users where userid like '%" + select_key
					+ "%'";
			break;
		case 2:
			hqlcount = "select count(*) from com.ts.vo.Users where username like '%"
					+ select_key + "%'";
			hqllist = "from com.ts.vo.Users where username like '%"
					+ select_key + "%'";
			break;
		case 3:
			Department dept = deptService.getDepartmentById(select_dept);
			hqlcount = "select count(*) from com.ts.vo.Users where departmentid="
					+ dept.getDepartmentid();
			hqllist = "from com.ts.vo.Users where departmentid="
					+ dept.getDepartmentid();
			break;
		case 4:
			if (select_contactor == 0) {
				hqlcount = "select count(*) from com.ts.vo.Users";
				hqllist = "from com.ts.vo.Users";
			} else if (select_contactor == 1) {
				hqlcount = "select count(*) from com.ts.vo.Users where phone like '%"
						+ select_key + "%'";
				hqllist = "from com.ts.vo.Users where  phone like '%"
						+ select_key + "%'";
			} else if (select_contactor == 2) {
				hqlcount = "select count(*) from com.ts.vo.Users where mobilephone like '%"
						+ select_key + "%'";
				hqllist = "from com.ts.vo.Users where  mobilephone like '%"
						+ select_key + "%'";
			} else {
				hqlcount = "select count(*) from com.ts.vo.Users where email like '%"
						+ select_key + "%'";
				hqllist = "from com.ts.vo.Users where email like '%"
						+ select_key + "%'";
			}
			break;
		}
		System.out.println(hqlcount);
		System.out.println(hqllist);
		int counts = userService.getUserCount(hqlcount);
		int pageSize = 10;
		int pages = counts % pageSize == 0 ? counts / pageSize : counts
				/ pageSize + 1;
		if (currPage >= pages) {
			currPage = pages;
		}
		if (currPage <= 1) {
			currPage = 1;
		}
		int startIndex = (currPage - 1) * pageSize + 1;
		int endIndex = startIndex + pageSize - 1 > counts ? counts : startIndex
				+ pageSize - 1;
		Page p = new Page();
		p.setCounts(counts);
		p.setCurrPage(currPage);
		p.setEndIndex(endIndex);
		p.setPageSize(pageSize);
		p.setStartIndex(startIndex);
		p.setPages(pages);
		List<Users> userlist = userService.listUser(hqllist, p);
		System.out.println(p.getCounts());
		System.out.println(p.getCurrPage());
		System.out.println(userlist.size());
		session.setAttribute("userlist", userlist);
		session.setAttribute("currPage", currPage);
		session.setAttribute("page", p);
		return "listusersuc";
	}

	public String deleteUserAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Users duser=userService.getUserById(userid);
		if (userService.deleteUser(duser)) {
			return "listuser";
		} else {
			System.out.println("fail to delete");
			session.setAttribute("errorinfo", "删除失败.可能是该员工的某些关联数据尚未删除");
			return "error";
		}
	}

	// ------------------------------------Department Operation-------------
	public String addDepartmentAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (deptService.addDepartment(dept)) {
			List<Department> dlist = deptService.listDepartment();
			session.setAttribute("dlist", dlist);
			return "suc";
		} else {
			session.setAttribute("errorinfo", "增加失败!请确认输入数据正确!");
			return "error";
		}
	}

	public String editDepartmentAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (deptService.editDepartment(dept)) {
			List<Department> dlist = deptService.listDepartment();
			session.setAttribute("dlist", dlist);
			return "suc";
		} else {
			session.setAttribute("errorinfo", "修改失败!请确认输入数据正确!");
			return "error";
		}
	}

	public String deleteDepartmentAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		System.out.println(departmentid);
		Department deldept = deptService.getDepartmentById(departmentid);
		if (deptService.deleteDepartment(deldept)) {
			List<Department> dlist = deptService.listDepartment();
			session.setAttribute("dlist", dlist);
			System.out.println("Here--------");
			return "suc";
		} else {
			System.out.println("fail to delete");
			session.setAttribute("errorinfo",
					"删除失败!建议删除该部门前先 清空 部门下属员工或者子部门");
			return "error";
		}
	}

	// ------------------------------------Roles Operation-------------

	public String listRoleAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		System.out.println("=-=-=-=-=-=-=-=-=-");
		List<Roles> rlist = roleService.listRole();
		session.setAttribute("rlist", rlist);
		session.setAttribute("rolecount", rlist.size());
		return "listrolesuc";
	}
	public String addRoleAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (roleService.addRole(role)) {
			return "listrole";
		} else {
			session.setAttribute("errorinfo", "增加角色失败!请确认输入数据正确!");
			return "error";
		}
	}

	public String editRoleAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (roleService.editRole(role)) {
			return "listrole";
		} else {
			session.setAttribute("errorinfo", "角色修改失败!可能是该角色关联用户限制!");
			return "error";
		}
	}

	public String deleteRoleAdmin() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Roles oldrole=roleService.getRoleById(roleid);
		if (roleService.deleteRole(oldrole)) {
			return "listrole";
		} else {
			session.setAttribute("errorinfo",
					"删除失败!建议删除该部门前先 清空 部门下属员工或者子部门");
			return "error";
		}
	}
	
	//公告管理
	public String listNoticeAdmin(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		System.out.println("=-=-=-=-=-=-=-=-=-");
		List<Notice> rlist = noticeDao.findAll();
		session.setAttribute("nlist", rlist);
		session.setAttribute("ncount", rlist.size());
		return "listnoticesuc";
	}
	public String deleteNoticeAdmin(){
		System.out.println("noticeid is"+noticeid);
		Notice oldrole=noticeDao.findById(noticeid);
		noticeDao.delete(oldrole);
		return "listnotice";
	}
	public String editNoticeAdmin(){
		System.out.println("更新----------");
		System.out.println(content+" "+notice.getId());
		Notice notice2 = noticeDao.findById(notice.getId());
		System.out.println(content+" "+notice2.getContent());
		notice2.setContent(content);
		noticeDao.update(notice2);
		System.out.println(content);
		return "listnotice";
	}
	public String addNoticeAdmin() throws ParseException{
		HttpSession session = ServletActionContext.getRequest().getSession();
		Users users = (Users) session.getAttribute("user");
		System.out.println(users.getAddress());
		Date sdate=new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(new Date().toLocaleString());
		notice.setPublicdate(sdate);
		notice.setPublicname(users.getUsername());
		System.out.println(notice.getContent()+""+notice.getPublicname()+""+notice.getPublicdate());
		noticeDao.save(notice);
		return "listnotice";
	}
	// =============================Setters & Getters =========================
	private static final long serialVersionUID = 1L;

	public DepartmentService getDeptService() {
		return deptService;
	}

	public void setDeptService(DepartmentService deptService) {
		this.deptService = deptService;
	}

	public Department getDept() {
		return dept;
	}

	public void setDept(Department dept) {
		this.dept = dept;
	}

	public int getDepartmentid() {
		return departmentid;
	}

	public void setDepartmentid(int departmentid) {
		this.departmentid = departmentid;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public Roles getRole() {
		return role;
	}

	public void setRole(Roles role) {
		this.role = role;
	}

	public int getRoleid() {
		return roleid;
	}

	public void setRoleid(int roleid) {
		this.roleid = roleid;
	}

	public Users getUser() {
		return user;
	}

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

	public int getUserid() {
		return userid;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public UserService getUserService() {
		return userService;
	}

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

	public int getSelect_index() {
		return select_index;
	}

	public void setSelect_index(int selectIndex) {
		select_index = selectIndex;
	}

	public String getSelect_key() {
		return select_key;
	}

	public void setSelect_key(String selectKey) {
		select_key = selectKey;
	}

	public int getSelect_dept() {
		return select_dept;
	}

	public void setSelect_dept(int selectDept) {
		select_dept = selectDept;
	}

	public int getSelect_contactor() {
		return select_contactor;
	}

	public void setSelect_contactor(int selectContactor) {
		select_contactor = selectContactor;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public NoticeDAO getNoticeDao() {
		return noticeDao;
	}

	public void setNoticeDao(NoticeDAO noticeDao) {
		this.noticeDao = noticeDao;
	}

	public int getNoticeid() {
		return noticeid;
	}

	public void setNoticeid(int noticeid) {
		this.noticeid = noticeid;
	}

	

	public Notice getNotice() {
		return notice;
	}

	public void setNotice(Notice notice) {
		this.notice = notice;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
}
最近下载更多
微信网友_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日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友