package com.eapt.reports.manage.action;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.eapt.base.EAPTBaseAction;
import com.eapt.constants.PubConstantsType;
import com.eapt.query.EmployeeQuery;
import com.eapt.query.FinancialPaymentcustomerQuery;
import com.eapt.reports.manage.form.FtcontractreportSearchCondition;
import com.eapt.reports.manage.form.FtcontractreportSearchForm;
import com.eapt.vo.EaptFinancialpaymentcustomer;
import com.stony.core.util.ConstantsContainer;
import com.stony.core.util.CoreUtils;
public class OperationreportExcelAction extends EAPTBaseAction {
private FtcamountComparetor c1 = new FtcamountComparetor();
private BusiamountComparetor c2 = new BusiamountComparetor();
private AgentfeeComparetor c3 = new AgentfeeComparetor();
private GetfeeComparetor c4 = new GetfeeComparetor();
private static ConstantsContainer instance = ConstantsContainer
.getInstants();
private static int MAXRESULTS = 2000;
private static String[] excelheader = new String[] { "序号", "年", "月",
"结算客户", "开发人", "开发单位", "经营单位", "合同金额(RMB)", "业务量(RMB)", "代理费(RMB)",
"税后毛利(RMB)", "进口业务量(RMB)", "出口业务量(RMB)", "销售业务量(RMB)"
};
protected ActionForward doWork(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String filename = "operationreport.xls";
FtcontractreportSearchForm sform = (FtcontractreportSearchForm) form;
sform.initConditionPost();
FtcontractreportSearchCondition condition = (FtcontractreportSearchCondition) sform
.getCondition();
FinancialPaymentcustomerQuery query = new FinancialPaymentcustomerQuery();
query.setPaymentcustomerno(getPaymentcustomernos(request));
query.setPaymentcustomercode(condition.getPaymentcustomercode());
query.setPaymentcustomernamecn(condition.getPaymentcustomernamecn());
if (StringUtils.isNotBlank(condition.getDeveloper())) {
String developer = EmployeeQuery.DEVELOPERQUERY + " '"
+ condition.getDeveloper().trim() + "' ";
query.setDeveloper(developer);
}
query.setDevelopedept(condition.getDevelopedept());
query.setPaymentcustomerOperationdept(condition.getOperationdept());
query.setFinancialyear(condition.getYear());
query.setFinancialmonth(condition.getMonth());
query.setFinancialmonth2(condition.getMonth2());
if (StringUtils.isNotBlank(condition.getOrderby())) {
query.setOrderbyDes(condition.getOrderby());
} else {
query.setOrderbyAsc("paymentcustomer.customercode");
query.setOrderbyAsc("financialyear");
query.setOrderbyAsc("financialmonth");
}
query.fetchInnerPaymentcustomer();
query.setMaxresults(MAXRESULTS);
query.noCheckOutOfMemory();
query.execute();
List results1 = query.getResults();
if (StringUtils.isNotBlank(condition.getSum())) {
results1 = new ArrayList();
Map resultsMap = new HashMap();
for (int i = 0; i < query.getResults().size(); i++) {
EaptFinancialpaymentcustomer arrearage = (EaptFinancialpaymentcustomer) query
.getResults().get(i);
EaptFinancialpaymentcustomer arrearage1 = (EaptFinancialpaymentcustomer) resultsMap
.get(arrearage.getPaymentcustomer().getCustomerno());
if (arrearage1 == null) {
arrearage.setFinancialmonth(null);
arrearage.setNianyue(arrearage.getFinancialyear() + "-");
resultsMap.put(arrearage.getPaymentcustomer()
.getCustomerno(), arrearage);
results1.add(arrearage);
} else {
arrearage1.addEaptFinancialpaymentcustomer(arrearage);
}
}
if ("ftcamount".equals(condition.getOrderby())) {
Collections.sort(results1, c1);
}
if ("busiamount".equals(condition.getOrderby())) {
Collections.sort(results1, c2);
}
if ("agentfee".equals(condition.getOrderby())) {
Collections.sort(results1, c3);
}
if ("getfee".equals(condition.getOrderby())) {
Collections.sort(results1, c4);
}
}
List results = new ArrayList();
for (int i = 0; i < results1.size(); i++) {
EaptFinancialpaymentcustomer fee = (EaptFinancialpaymentcustomer) results1
.get(i);
Object[] excelrow = new Object[14];
excelrow[0] = (i + 1) + "";
excelrow[1] = fee.getFinancialyear();
excelrow[2] = fee.getFinancialmonth();
excelrow[3] = instance.getLabel(PubConstantsType.Customer, fee
.getPaymentcustomer().getCustomerno().toString());
excelrow[4] = instance.getLabel(PubConstantsType.User, fee
.getPaymentcustomer().getPrincipal1());
excelrow[5] = instance.getLabel(PubConstantsType.DEPT, fee
.getPaymentcustomer().getDevelopedept());
if (fee.getPaymentcustomer().getOperationdept() != null)
excelrow[6] = instance.getLabel(PubConstantsType.Customer, fee
.getPaymentcustomer().getOperationdept().toString());
excelrow[7] = fee.getFtcamount();
excelrow[8] = fee.getBusiamount();
excelrow[9] = fee.getAgentfee();
excelrow[10] = fee.getGetfee();
excelrow[11] = fee.getBusiamountimp();
excelrow[12] = fee.getBusiamountexp();
excelrow[13] = fee.getBusiamountsal();
results.add(excelrow);
}
setIsStream(response);
response.reset();
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment; filename="
+ filename);
OutputStream outputStream = response.getOutputStream();
Locale locale = (Locale) request.getSession().getAttribute(
Globals.LOCALE_KEY);
if (locale != null && locale == Locale.TRADITIONAL_CHINESE) {
CoreUtils.exportExcel(excelheader, outputStream, results, true);
} else {
CoreUtils.exportExcel(excelheader, outputStream, results, false);
}
return null;
}
class FtcamountComparetor implements Comparator {
public int compare(Object o1, Object o2) {
EaptFinancialpaymentcustomer arrearage1 = (EaptFinancialpaymentcustomer) o1;
EaptFinancialpaymentcustomer arrearage2 = (EaptFinancialpaymentcustomer) o2;
return arrearage2.getFtcamount().compareTo(
arrearage1.getFtcamount());
}
}
class BusiamountComparetor implements Comparator {
public int compare(Object o1, Object o2) {
EaptFinancialpaymentcustomer arrearage1 = (EaptFinancialpaymentcustomer) o1;
EaptFinancialpaymentcustomer arrearage2 = (EaptFinancialpaymentcustomer) o2;
return arrearage2.getBusiamount().compareTo(
arrearage1.getBusiamount());
}
}
class AgentfeeComparetor implements Comparator {
public int compare(Object o1, Object o2) {
EaptFinancialpaymentcustomer arrearage1 = (EaptFinancialpaymentcustomer) o1;
EaptFinancialpaymentcustomer arrearage2 = (EaptFinancialpaymentcustomer) o2;
return arrearage2.getAgentfee().compareTo(arrearage1.getAgentfee());
}
}
class GetfeeComparetor implements Comparator {
public int compare(Object o1, Object o2) {
EaptFinancialpaymentcustomer arrearage1 = (EaptFinancialpaymentcustomer) o1;
EaptFinancialpaymentcustomer arrearage2 = (EaptFinancialpaymentcustomer) o2;
return arrearage2.getGetfee().compareTo(arrearage1.getGetfee());
}
}
}