package com.post.action;

import java.util.List;

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

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.post.excelTools.ExcelUtils;
import com.post.excelTools.JsGridReportBase;
import com.post.excelTools.TableColumn;
import com.post.excelTools.TableData;
import com.post.excelTools.TableHeaderMetaData;
import com.post.pageModel.Budget;
import com.post.pageModel.Json;
import com.post.service.BudgetServiceI;

/**
 * 审计
 */
@Action(value = "auditAction", results = {
		@Result(name = "auditManager", location = "/busi/audit/auditManager.jsp"),
		@Result(name = "auditEdit", location = "/busi/audit/auditEdit.jsp"),
		@Result(name = "auditView", location = "/busi/audit/auditView.jsp")
		})
public class AuditAction extends BaseAction implements ModelDriven<Budget> {

	private Budget budget = new Budget();
	private BudgetServiceI budgetService;

	public BudgetServiceI getBudgetService() {
		return budgetService;
	}

	@Autowired
	public void setBudgetService(BudgetServiceI budgetService) {
		this.budgetService = budgetService;
	}

	@Override
	public Budget getModel() {
		return budget;
	}

	public String auditManager() {
		return "auditManager";
	}


	public String auditEdit() {
		return "auditEdit";
	}
	
	public String auditView() {
		return "auditView";
	}
	

	public void do_datagrid() {
		super.writeJsonDate(budgetService.datagrid(budget));
	}


	public void edit() {
		Json j = new Json();
		try {
			budgetService.auditEdit(budget);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			j.setMsg("编辑失败！");
		}
		super.writeJson(j);
	}

	
	public void do_totalBudget(){
		Json j = new Json();
		j.setSuccess(true);
		j.setObj(budgetService.totalBudget(budget.getFileId()));
		super.writeJson(j);
	}
	
	public void exportExcel() throws Exception{
		HttpServletRequest request=ServletActionContext.getRequest();
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("application/msexcel;charset=GBK");
        List<Budget> list=budgetService.excelAudit(budget);
        String title = budget.getDeptName()+"成本费用清算表";
        String[] hearders = new String[] {"单位","文件制定日期", "文件字号", "文件名称", "考核开始日期", "考核结束日期", "所属专业","所属项目","预计业务量收","预计费用（不含税）","预计利润","预计清算时间","实际清算时间","实际清算（不含税）","清算与预计差异","已预提","清算与预提差异","税金","总成本费用"};//表头数组
        String[] fields = new String[] {"deptName","fileDate", "fileNumber", "fileName", "startdate", "enddate", "zrzxName","projectName","cyjywls","cyjfy","cyjlr","cyjqssj","csjqssj","csjfy","cfycy","cyt","cytcy","csj","ctotal"};//对象属性数组
        int[]  columnwidths = new int[]{16,12,22,40,12,12,20,20,14,16,14,12,12,16,14,10,14,10,16};
        TableData td = ExcelUtils.createTableData(list, getTableHeader(hearders),fields);
        td.compute();
        JsGridReportBase report = new JsGridReportBase(request, response);
        report.exportToExcel(title, "admin",columnwidths, td);
	}
	
	private TableHeaderMetaData getTableHeader(String[] hearders){
	    int spanCount = 4;//需要合并的列数。从第1列开始到指定列。
	    //创建表头对象
		TableHeaderMetaData headMeta = new TableHeaderMetaData();
		for(int i=0;i<hearders.length;i++){
			TableColumn tc = new TableColumn();
			tc.setDisplay(hearders[i]);
			//设置按指定列统计
			if(i==2)//按第2列文件字号进行小计
				tc.setDisplaySummary(true);
			
			if(i>7 && i<11)//进行求和
				tc.setAggregateRule("sum");
			
			if (i>12 && i<19)
				tc.setAggregateRule("sum");
			
			if(i<spanCount)//前3列行合并
				tc.setGrouped(true);
			headMeta.addColumn(tc);
		}
		return headMeta;
	}
}