package qi.yun.dong.action;

import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFHeader;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import qi.yun.dong.entity.OptBackMsg;
import qi.yun.dong.model.MyAdmin;
import qi.yun.dong.service.IMyAdminService;
import qi.yun.dong.util.MyPagination;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public class MyAdminAction extends ActionSupport implements ModelDriven<MyAdmin> {

	Logger logger = Logger.getLogger(MyAdminAction.class);
	IMyAdminService myAdminService;

	public String test() {
		System.out.println("MyAdminAction.test()");
		MyAdmin myAdmin = myAdminService.findById(1);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("myAdmin", myAdmin);
		return "test";
	}

	/*
	 * page:1 start:0 limit:5
	 */
	Integer page, start, limit;
	// 封装分页对象
	protected MyPagination mypagination; // 分页对象

	public String findAdmins() {
		try {
			Integer total = myAdminService.getTotal();
			this.mypagination = new MyPagination(page, total, limit);
			List<MyAdmin> myAdmins = myAdminService.findByPage("", start, limit);
			this.mypagination.setDatas(myAdmins);
			return SUCCESS;
		} catch (Exception e) {
			logger.error("查询失败,错误原因是:[" + e.getMessage() + "]");
			e.printStackTrace();
		}
		return null;
	}

	protected OptBackMsg optBackMsg = new OptBackMsg();

	public String addAdmin() {
		try {
			myAdminService.addAdmin(myAdmin);
			this.optBackMsg.setMsg("ok");
			this.optBackMsg.setSuccess(true);
		} catch (Exception e) {
			logger.error("保存失败,失败原因是:[" + e.getMessage() + " ]");
			this.optBackMsg.setMsg("fail, message [ " + e.getMessage() + " ]");
			this.optBackMsg.setSuccess(false);
		}
		return SUCCESS;
	}

	public Integer id;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String delAdmin() {
		try {
			myAdmin = myAdminService.findById(id);
			myAdminService.delete(myAdmin);
			this.optBackMsg.setMsg("ok");
			this.optBackMsg.setSuccess(true);

		} catch (Exception e) {
			logger.error("删除失败,失败原因是:[" + e.getMessage() + " ]");
			this.optBackMsg.setMsg("fail, message [ " + e.getMessage() + " ]");
			this.optBackMsg.setSuccess(false);
		}
		return SUCCESS;
	}

	@SuppressWarnings("deprecation")
	public String excelExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			// 获取问题列表
			List<MyAdmin> myAdmins = myAdminService.findAll();
			// 创建一个新的Excel
			HSSFWorkbook workBook = new HSSFWorkbook();
			// 创建sheet页
			HSSFSheet sheet = workBook.createSheet();
			// sheet页名称
			workBook.setSheetName(0, "myAdminList");
			// 创建header页
			HSSFHeader header = sheet.getHeader();
			// 设置标题居中
			header.setCenter("标题");
			// 设置第一行为Header
			HSSFRow row = sheet.createRow(0);
			//构建第一行的三个单元格
			HSSFCell cell0 = row.createCell(Short.valueOf("0"));
			HSSFCell cell1 = row.createCell(Short.valueOf("1"));
			HSSFCell cell2 = row.createCell(Short.valueOf("2"));
			HSSFCell cell3 = row.createCell(Short.valueOf("3"));
			//设置每个单元格的名称
			cell0.setCellValue("账号");
			cell1.setCellValue("密码");
			cell2.setCellValue("性别");
			cell3.setCellValue("出生日期");
			if (myAdmins != null && !myAdmins.isEmpty()) {
				for (int i = 0; i < myAdmins.size(); i++) {
					MyAdmin myAdmin = myAdmins.get(i);
					row = sheet.createRow(i + 1);
					cell0 = row.createCell(Short.valueOf("0"));
					cell1 = row.createCell(Short.valueOf("1"));
					cell2 = row.createCell(Short.valueOf("2"));
					cell3 = row.createCell(Short.valueOf("3"));
					cell0.setCellValue(myAdmin.getUserName());
					cell1.setCellValue(myAdmin.getPassword());
					cell2.setCellValue(myAdmin.getSex());
					if (myAdmin.getBirthday() == null) {
						cell3.setCellValue("");
					} else {
						cell3.setCellValue(myAdmin.getBirthday());
					}
					sheet.setColumnWidth((short) 0, (short) 4000);
					sheet.setColumnWidth((short) 1, (short) 4000);
					sheet.setColumnWidth((short) 2, (short) 4000);
					sheet.setColumnWidth((short) 3, (short) 4000);
				}
			}
			// 通过Response把数据以Excel格式保存
			response.reset();
			response.setContentType("application/msexcel;charset=UTF-8");
			response.addHeader("Content-Disposition", "attachment;filename=\""
					+ new String(("用户信息" + ".xls").getBytes("GBK"), "ISO8859_1") + "\"");
			OutputStream out = response.getOutputStream();
			workBook.write(out);
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.error("导出报表失败,失败原因是:[" + e.getMessage() + " ]");
		}
		return null;
	}

	public OptBackMsg getOptBackMsg() {
		return optBackMsg;
	}

	public void setOptBackMsg(OptBackMsg optBackMsg) {
		this.optBackMsg = optBackMsg;
	}

	public MyPagination getMypagination() {
		return mypagination;
	}

	public void setMypagination(MyPagination mypagination) {
		this.mypagination = mypagination;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public IMyAdminService getMyAdminService() {
		return myAdminService;
	}

	public void setMyAdminService(IMyAdminService myAdminService) {
		this.myAdminService = myAdminService;
	}

	MyAdmin myAdmin = new MyAdmin();

	@Override
	public MyAdmin getModel() {
		return myAdmin;
	}

}
最近下载更多
newnu88  LV12 2020年2月17日
aa991215  LV17 2019年6月1日
admindiu  LV1 2019年3月19日
Marin_qing  LV1 2018年7月30日
ztz111  LV1 2018年7月11日
es123123  LV1 2018年7月7日
13545229093  LV1 2018年6月13日
zr616819363  LV2 2018年5月5日
zaihuishou1018  LV1 2018年4月24日
lvtao777  LV11 2018年4月12日
最近浏览更多
不知道取什么名字12  LV1 2022年12月14日
sssfyy 2022年7月26日
暂无贡献等级
zhao1546  LV3 2022年5月6日
wenglaoshi  LV2 2021年12月21日
hihihhjhjijijjjhjk  LV2 2021年12月4日
公司了 2021年6月25日
暂无贡献等级
小豆12138  LV1 2021年4月15日
liuguojun920  LV6 2021年2月24日
0592lyj  LV9 2020年12月10日
network_sec  LV6 2020年11月23日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友