package com.kingdee.shr.compensation.web.handler.integrate;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.customer.util.handler.ConfigurationHandler;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.context.SHRContext;
import com.kingdee.shr.base.syssetting.exception.SHRWebException;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.compensation.app.integrate.BatchSubmitShemeBillCollection;
import com.kingdee.shr.compensation.app.integrate.BatchSubmitShemeBillEntryCollection;
import com.kingdee.shr.compensation.app.integrate.BatchSubmitShemeBillEntryInfo;
import com.kingdee.shr.compensation.app.integrate.BatchSubmitShemeBillFactory;
import com.kingdee.shr.compensation.app.integrate.BatchSubmitShemeBillInfo;
import com.kingdee.shr.compensation.app.integrate.CalSubmitSchemeInfo;
import com.kingdee.shr.customer.gtiit.util.DateTimeUtils;

/**
 * 专业应用提报列表
 * @author 86133
 *
 */
public class BatchSubmitShemeBillListHandlerEx extends BatchSubmitShemeBillListHandler{

	private static Logger logger =
	        Logger.getLogger("com.kingdee.shr.compensation.web.handler.integrate.BatchSubmitShemeBillListHandlerEx");
	
	@Override
	protected void beforeSubmit(HttpServletRequest request, HttpServletResponse response, CoreBaseInfo model)
			throws SHRWebException {
		super.beforeSubmit(request, response, model);
		Context ctx = SHRContext.getInstance().getContext();
		BatchSubmitShemeBillInfo info = (BatchSubmitShemeBillInfo) model;
		if(StringUtils.isNotBlank(info.getString("id"))) {
			try {
				info = BatchSubmitShemeBillFactory.getLocalInstance(ctx).getBatchSubmitShemeBillInfo(new ObjectUuidPK(info.getString("id")));
			} catch (EASBizException e) {
				e.printStackTrace();
			} catch (BOSException e) {
				e.printStackTrace();
			}
		}
		//校验员工+提报月+方案只能提交一次
		BatchSubmitSchemeUtils.checkSubmitCount(info);
		//参数校验
		BatchSubmitSchemeUtils.checkExists(info);
		//校验分录人员是否重复
		BatchSubmitSchemeUtils.checkPersonRepeat(info);
		//校验只能提交发生日期和提报日期在一个月内的单据
		BatchSubmitSchemeUtils.checkSubmitDate(info);
		checkAmount(info);
		//校验员工+方案+发生日期不能重复
		BatchSubmitSchemeUtils.checkSubmitRepeat(info);
	}
	
	/**
	 * 校验提报项目不能都为0或空
	 * @param info
	 * @throws ShrWebBizException
	 */
	private void checkAmount(BatchSubmitShemeBillInfo info) throws ShrWebBizException{
		Context ctx = SHRContext.getInstance().getContext();
		CalSubmitSchemeInfo submitScheme = info.getSubmitScheme();
		String submitSchemeId = submitScheme.getId().toString();
		String sql = "SELECT a.FIELDSN,c.FBILLENTRYTABLENAME FROM T_HR_SCalSubmitItem a left join T_HR_SCalSubmitSchemeItem b on a.fid=b.fcalsubmititemid left join T_HR_SCalShemeTableRelation c on b.FCALSUBMITSCHEMEID = c.fsubmitschemeid where b.FCALSUBMITSCHEMEID = '"+submitSchemeId+"' and b.FISSHOW = '1' and b.FSTATE = '1'";
		logger.error("查询提报项目字段名和表面SQL："+sql);
		List<String> list = new ArrayList<String>();
		String tableName = "";
		try {
			IRowSet iRowSet = DbUtil.executeQuery(ctx, sql);
			while(iRowSet.next()) {
				list.add("S"+iRowSet.getString("FIELDSN"));
				tableName = iRowSet.getString("FBILLENTRYTABLENAME");
			}
			logger.error("字段集合："+list+",表名："+tableName);
			if(list.size()==0) {
				return;
			}
			String fields = String.join(",", list);
			String schemeSql = "SELECT "+fields+" FROM "+tableName+" where fbillid = '"+info.getId().toString()+"' ";
			logger.error("查询提报项目数据SQL："+schemeSql);
			IRowSet schemeRow = DbUtil.executeQuery(ctx, schemeSql);
			while(schemeRow.next()) {
				int count = 0;
				for(String itemField:list) {
					Object fieldObject = schemeRow.getObject(itemField);
					if(fieldObject instanceof Number) {
						BigDecimal itemNumber = schemeRow.getBigDecimal(itemField)==null?BigDecimal.ZERO:schemeRow.getBigDecimal(itemField);
						if(itemNumber.compareTo(BigDecimal.ZERO) == 0) {
							count++;
						}
					}else {
						if(StringUtils.isBlank(schemeRow.getString(itemField))) {
							count++;
						}
					}
				}
				if(count == list.size()) {
					throw new ShrWebBizException("The submitted project must have a non empty one");
				}
			}
		} catch (BOSException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
}
