package com.kingdee.eas.custom.gggl.app;

import org.apache.log4j.Logger;
import javax.ejb.*;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.kingdee.bos.*;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.bos.metadata.IMetaDataPK;
import com.kingdee.bos.metadata.rule.RuleExecutor;
import com.kingdee.bos.metadata.MetaDataPK;
//import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.framework.ejb.AbstractEntityControllerBean;
import com.kingdee.bos.framework.ejb.AbstractBizControllerBean;
//import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.service.ServiceContext;
import com.kingdee.bos.service.IServiceContext;

import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.report.app.CommRptBaseControllerBean;
import com.kingdee.eas.framework.report.util.RptParams;
import com.kingdee.eas.framework.report.util.RptRowSet;
import com.kingdee.eas.framework.report.util.RptTableColumn;
import com.kingdee.eas.framework.report.util.RptTableHeader;
import com.kingdee.eas.util.app.ContextUtil;

public class ZtPaidReportFacadeControllerBean extends AbstractZtPaidReportFacadeControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.gggl.app.ZtPaidReportFacadeControllerBean");
	protected RptParams _init(Context ctx, RptParams params)
	throws BOSException, EASBizException {
		RptParams pp = new RptParams();
		return pp;
	}
	private void initColoum(RptTableHeader header,RptTableColumn col,String name,int width,boolean isHide){
		col= new RptTableColumn(name);
		col.setWidth(width);
		col.setHided(isHide);
		header.addColumn(col);
	}
	protected RptParams _createTempTable(Context ctx, RptParams params)
	throws BOSException, EASBizException {
		Calendar calendar=Calendar.getInstance();
		int year=0;
		year=calendar.get(Calendar.YEAR);
		
		RptTableHeader header = new RptTableHeader();
		RptTableColumn col = null;


		initColoum(header, col, "HTNUM", 80, false);
		initColoum(header, col, "NAME", 80, false);
		initColoum(header, col, "BIZDATE", 80, false);
		initColoum(header, col, "CUNAME", 80, false);
		initColoum(header, col, "SNW", 80, false);
		initColoum(header, col, "HTAMOUNT", 80, false);
		initColoum(header, col, "PP", 80, false);
		initColoum(header, col, "fnumber", 80, false);
		initColoum(header, col, "ZLSTARTDATE", 80, false);
		initColoum(header, col, "ZLENDDATE", 80, false);
		initColoum(header, col, "ZLZQ", 80, false);
		initColoum(header, col, "ZLAMOUNT", 80, false);
		initColoum(header, col, "ZZPAYDATE", 80, false);
		initColoum(header, col, "ZZAMOUNT", 80, false);
		initColoum(header, col, "ZZPAYDATE", 80, false);
		initColoum(header, col, "sumAmount", 80, false);
		
	
		
		List<String> firstHead = new ArrayList<String>();
		List<String> firstHead2 = new ArrayList<String>();
		List<String> firstHead3 = new ArrayList<String>();
		String[] fixedHead1 =  new String[]{"租赁合同详情表","租赁合同详情表","租赁合同详情表","租赁合同详情表","租赁合同详情表","租赁合同详情表","租赁合同详情表","租赁合同详情表","租赁合同详情表","租赁合同详情表","租赁合同详情表","租赁合同详情表","租赁合同详情表","租赁合同详情表","租赁合同详情表","租赁合同详情表"};
		String[] fixedHead2 =  new String[]{"合同号","公司","签约日期","客户","室内/室外","合同金额","品牌","灯箱号","租赁周期","租赁周期","租赁周期","租赁费","租赁费","制作费","制作费","合计"};
		String[] fixedHead3 =  new String[]{"合同号","公司","签约日期","客户","室内/室外","合同金额","品牌","灯箱号","租赁起始日期","租赁到期日期","租赁周期/月","租赁金额","租赁付款日期","制作费金额","制作费日期","合计"};

		initFixeHeader(firstHead, fixedHead1);
		initFixeHeader(firstHead2, fixedHead2);
		initFixeHeader(firstHead3, fixedHead3);
		Object[] fhead = firstHead.toArray();
		Object[] fhead2 = firstHead2.toArray();
		Object[] fhead3 = firstHead3.toArray();
		header.setLabels(new Object[][]{ 
				fhead,fhead2,fhead3
		},true);
		params.setObject("header", header);
		return params;
	}
	private void initFixeHeader(List<String> list, String[] array) {
		for (String s : array) {
			list.add(s);
		}
	}
	protected RptParams _query(Context ctx, RptParams params) throws BOSException, EASBizException {
		RptRowSet rowSet = executeQuery(getSql(ctx,params), null, ctx);
		if(rowSet!=null){
		params.setObject("rowSet", rowSet);
		}
		
		return params;
		
	}
	private String getSql(Context ctx,RptParams params) {
		StringBuilder sql = new StringBuilder("");
		sql.append("SELECT SALECONTRACT.CFHtNum AS HTNUM,COMPANY.FName_l2 AS NAME, ");
		sql.append(" to_char(SALECONTRACT.FBizDate,'YYYY-MM-DD') AS BIZDATE,CUSTOMER.FName_l2 AS CUNAME,case when E1_GGW.CFSNW='sn' then '室内' else '户外 ' end  AS SNW,");
		sql.append(" SALECONTRACT.CFHtAmount AS HTAMOUNT,SALECONTRACT.CFPP as pp,E1_GGW.FName_l2 AS fnumber,");
		sql.append(" to_char(E1.CFZlStartDate,'YYYY-MM-DD') AS ZLSTARTDATE,to_char(E1.CFZlEndDate,'YYYY-MM-DD') AS ZLENDDATE,  ");
		sql.append(" E1.CFZlzq AS ZLZQ,");
		sql.append(" E1.CFZlAmount AS ZLAMOUNT, ");
		sql.append(" to_char(E1.CFZlPayDate,'YYYY-MM-DD') AS ZlPAYDATE,");
		sql.append(" E1.CFZzAmount AS ZZAMOUNT, ");
		sql.append(" to_char(E1.CFZzPayDate,'YYYY-MM-DD') AS ZZPAYDATE,  ");
		sql.append(" E1.CFZzAmount + E1.CFZlAmount AS sumAmount ");
		sql.append(" FROM CT_GGL_SaleContract AS SALECONTRACT");
		sql.append(" LEFT OUTER JOIN CT_GGL_SaleContractE1 AS E1");
		sql.append(" ON SALECONTRACT.FID = E1.FParentID");
		sql.append(" LEFT OUTER JOIN CT_GGL_Advert AS E1_GGW");
		sql.append(" ON E1.CFGgwID = E1_GGW.FID");
		sql.append(" LEFT OUTER JOIN T_BD_Customer AS CUSTOMER");
		sql.append(" ON SALECONTRACT.CFCustomerID = CUSTOMER.FID");
		

		
		sql.append(" LEFT OUTER JOIN T_ORG_Company AS COMPANY");
		sql.append(" ON SALECONTRACT.CFCompanyID = COMPANY.FID");
		sql.append(" where 1=1 and SALECONTRACT.FSTATUS=4");
		sql.append(" and SALECONTRACT.cfdjtype=2");
		if(ContextUtil.getCurrentFIUnit(ctx).getNumber()!=null){
			if(!ContextUtil.getCurrentFIUnit(ctx).getNumber().toString().equals("HY")){
			sql.append(" and  COMPANY.FNUMBER ='"+ContextUtil.getCurrentFIUnit(ctx).getNumber()+"'");
			}
		}
		sql.append(" order by  SALECONTRACT.CFHtNum, to_char(SALECONTRACT.FBizDate,'YYYY-MM-DD'),E1_GGW.FName_l2 ");
//		if(number!=null && ContextUtil.getCurrentFIUnit(ctx).getNumber()==null){
//			sql.append(" and  E1.FNUMBER ='"+number+"'");
//		}else if(number==null && ContextUtil.getCurrentFIUnit(ctx).getNumber()!=null){
//			sql.append(" and  COMPANY.FNUMBER ='"+ContextUtil.getCurrentFIUnit(ctx).getNumber()+"'");
//		}else if(number!=null &&ContextUtil.getCurrentFIUnit(ctx).getNumber()!=null){
//			sql.append(" and  COMPANY.FNUMBER ='"+ContextUtil.getCurrentFIUnit(ctx).getNumber()+"'");
//			sql.append(" and  E1_GGW.FNUMBER ='"+number+"'");
//		}
	 return sql.toString();
	 
	 
	}

}