package com.gaowj.test.demo1;



import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JasperCompileManager;

import org.apache.struts2.ServletActionContext;

import com.gaowj.business.Demo1OpeBusiness;
import com.gaowj.business.SystemOpeBusiness;
import com.gaowj.business.action.BasicAction;
import com.gaowj.business.aop.LogProxy;
import com.gaowj.business.comp.BusinessManager;
import com.gaowj.business.exception.BusinessException;
import com.gaowj.util.Person;
import com.gaowj.util.zy_com_pm;



public class testAction extends BasicAction {
	
	private static final long serialVersionUID = -8875314370440335487L;
	
	private Connection conn;
	private Statement st;
	private ResultSet rs;
	private List<zy_com_pm> list;
	private List<Person> myList;

	public Connection getConn() {
		return conn;
	}

	/*
	 * 
	 */
	public void setConn(Connection conn) {
		this.conn = conn;
	}

	public List<zy_com_pm> getList() {
		return list;
	}

	public void setList(List<zy_com_pm> list) {
		this.list = list;
	}

	public List<Person> getMyList() {
		return myList;
	}

	public void setMyList(List<Person> myList) {
		this.myList = myList;
	}

	public String execute() throws BusinessException {
		Demo1OpeBusiness business = BusinessManager.getBusiness(Demo1OpeBusiness.class);
		business.sayHello("spring3.122222222");
		return SUCCESS;
	}
	
	
	public String listDate() throws BusinessException{
//		Demo1OpeBusiness business = BusinessManager.getBusiness(Demo1OpeBusiness.class);
//		business.sayHello("spring3.1");
		System.out.println(0);
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		//System.out.println(systemOpeBusiness.getLeftMenu2((systemOpeBusiness.getUserAllModule("6666")),"2009001",null));
		List<Map<String,Object>> ite=systemOpeBusiness.getTopMenu(systemOpeBusiness.getUserAllModule("6666"));
		System.out.println(ite);
		 for(Map<String,Object> mt:ite){ 
		     System.out.println(mt.get("MT_ID"));
		     System.out.println(3);
		 }
		 System.out.println(2);
		return "listDate";
	}
	
	public String listAjaxDate() throws BusinessException, IOException{
		Demo1OpeBusiness business = BusinessManager.getBusiness(Demo1OpeBusiness.class);
		
		List<Map<String,Object>> l= business.sayHello("spring3.1");
		response.getWriter().print(l);
		return null;
	}
	
	public String insert() throws BusinessException{
//		Demo1OpeBusiness dip = (Demo1OpeBusiness) LogProxy.get(demo1OpeBusiness,"测试类增加数据");
//		dip.insert(null);
		int i = 1/0;
		return "insert";
	}
	
	public String modify() throws BusinessException{
		//Demo1OpeBusiness business = BusinessManager.getBusiness(Demo1OpeBusiness.class);
		Demo1OpeBusiness businessLog = (Demo1OpeBusiness) LogProxy.get(BusinessManager.getBusiness(Demo1OpeBusiness.class),"测试类修改数据");
		businessLog.update(null);
		return "modify";
	}
	
	public String delete() throws BusinessException{
//		Demo1OpeBusiness dip = (Demo1OpeBusiness) LogProxy.get(demo1OpeBusiness,"测试类删除数据");
//		dip.delete(null);
		return "delete";
	}
	
	public String report1() throws BusinessException, SQLException{
		conn=com.gaowj.util.ConnectionFactory.getConn();
		st=conn.createStatement();
		rs=st.executeQuery("select * from zy_com_pm order by spbm");
		list=new ArrayList<zy_com_pm>();
		while(rs.next()){
			zy_com_pm pm=new zy_com_pm(rs.getString("spbm"),rs.getString("spmc"),rs.getString("spywm"),rs.getString("hgdm"),rs.getString("hgdw"),rs.getDouble("zsl"),rs.getDouble("tsl"),rs.getString("creator"),rs.getTimestamp("createdate"),rs.getString("modifier"),rs.getTimestamp("modifydate"),rs.getString("tbflag"));
			list.add(pm);
		}
		try {
		       String reportSource;
		       reportSource=ServletActionContext.getServletContext().getRealPath("test");
		       JasperCompileManager.compileReportToFile(reportSource+"/reports/demo1.jrxml",reportSource+"/reports/demo1.jasper");

		     }catch (Exception e) {
		       e.printStackTrace();
		       return ERROR;
		     }
		     return SUCCESS;
	}
	
	public String report2() throws BusinessException{
		
		Person p1 = new Person(new Long(1), "Patrick", "Lightbuddie");

	     Person p2 = new Person(new Long(2), "Jason", "Carrora");

	     Person p3 = new Person(new Long(3), "Alexandru", "Papesco");

	     Person p4 = new Person(new Long(4), "Jay", "Boss");

	     myList = new ArrayList<Person>();

	     myList.add(p1);

	     myList.add(p2);

	     myList.add(p3);

	     myList.add(p4);
	     try {

		       String reportSource;

		       reportSource=ServletActionContext.getServletContext().getRealPath("test");

		       JasperCompileManager.compileReportToFile(reportSource+"/reports/demo2.jrxml",reportSource+"/reports/demo2.jasper");

		     }catch (Exception e) {

		       e.printStackTrace();

		       return ERROR;

		     }

		     return SUCCESS;

    }
	
	

}