package com.kingdee.eas.custom.facade.plurality;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.affair.*;
import com.kingdee.eas.hr.base.*;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.util.DateTimeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import com.kingdee.bos.*;
import com.kingdee.eas.common.EASBizException;
 

import java.lang.String;


/**
 * 兼职终止
 * @author kkuan
 *
 */
public class PluralitySubFacadeControllerBean extends AbstractPluralitySubFacadeControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.custom.facade.plurality.PluralitySubFacadeControllerBean");
    
    @Override
    protected void _autoPluralitEnd(Context ctx, String personId, String curdate, int offset) throws BOSException, EASBizException {
    	super._autoPluralitEnd(ctx, personId,curdate, offset);

    	SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    	//变动类型
		AffairActionReasonInfo reasonInfo = AffairActionReasonFactory.getLocalInstance(ctx).getAffairActionReasonInfo(" where number = '0211' ");
		HRBizDefineInfo defineInfo = HRBizDefineFactory.getLocalInstance(ctx).getHRBizDefineInfo( " where number = 'SHR19' " );
		HashMap<String, Object> params = new HashMap  ();
		params.put("reasonInfo", reasonInfo);
		params.put("defineInfo", defineInfo);
		
		//查询所有当天日期的离职单
		try {
			//转 00:00:00
			Date bizDate = DateTimeUtils.parseDate(DateTimeUtils.format( new Date(), "yyyy-MM-dd"),  "yyyy-MM-dd")  ;  
			if (StringUtils.isNotBlank(curdate)) {
				bizDate = DateTimeUtils.parseDate(curdate, "yyyy-MM-dd");
			} 
			Date startDate = bizDate;
			Date endDate = DateTimeUtils.addDay(bizDate, 1) ;
			if( offset > 0 ){
				endDate = DateTimeUtils.addDay(endDate, offset) ;
			}else if( offset < 0 ) {
				startDate = DateTimeUtils.addDay(startDate, offset) ;
			}

			//查询兼职单
			StringBuilder sbu = new StringBuilder();
			sbu.append(" select  bill.fid billId   from T_HR_PluralityAddBizBill bill ");
			sbu.append(" left join  T_HR_PluralityAddBizBillEntry entry on bill.FEntryId = entry.fbillid ");
			sbu.append(" where  bill.fbillstate = '3'  "  );
			sbu.append(" and  entry.fenddate >= '" + startDate+"' " );
			sbu.append(" and  entry.fenddate < '" + endDate+"' " );
			if( StringUtils.isNotBlank(personId)){
				sbu.append(" and  entry.fpersonid = " + personId );
			}
			IRowSet rowSet = DbUtil.executeQuery(ctx, sbu.toString());
		 
			while(rowSet.next()) {
				String billId = rowSet.getString("billId");
				newDelBill(  ctx,   billId , params);
			}
		} catch (SQLException | ParseException e) {
			e.printStackTrace();
		}
	 

	}


	private  void  newDelBill(Context ctx, String billId,HashMap<String, Object> params) throws BOSException, EASBizException {

		IPluralityAddBizBill addIns = PluralityAddBizBillFactory.getLocalInstance(ctx);
		PluralityAddBizBillInfo addInfo = addIns.getPluralityAddBizBillInfo(new ObjectUuidPK(billId));
		//创建新集合
		CoreBaseCollection delCol = new CoreBaseCollection();
		PluralityDelBizBillInfo delInfo = new PluralityDelBizBillInfo();
//		delInfo.setAdminOrg(addInfo.getAdminOrg());
		delInfo.setHrOrgUnit(addInfo.getHrOrgUnit());
		delInfo.setBizDate(new Date());
		delInfo.setCU(addInfo.getCU());
		delInfo.setApplier(addInfo.getApplier());
		//分录数据
		PluralityAddBizBillEntryCollection addEntrys = addInfo.getEntrys();
		//分录赋值
		PluralityDelBizBillEntryCollection addEntryData = addEntryData(addEntrys,  params);
		delInfo.put("entrys", addEntryData);
		delInfo.setApplyDate(new Date());
		delInfo.setBizDate(new Date());
		delInfo.setCreator(ContextUtil.getCurrentUserInfo(ctx));
		delInfo.setHrOrgUnit(addInfo.getHrOrgUnit());
		delInfo.setIsMulti(addInfo.isIsMulti() );
		delInfo.setUseDefault(addInfo.isUseDefault());
		delInfo.setIsMultiEntry(addInfo.isIsMultiEntry());
		delInfo.setBillState(HRBillStateEnum.SAVED);
		delInfo.setApproveType(ApproveTypeEnum.DIRECT);
	 
//		delCol.add(delInfo);
		//保存数据库
		IPluralityDelBizBill delIns = PluralityDelBizBillFactory.getLocalInstance(ctx);
		delIns.addnew(delInfo);
		delInfo.setBillState(HRBillStateEnum.AUDITED);
		delIns.submitEffect((CoreBaseInfo)delInfo);
		logger.error("兼职单审核完");
	}



	/**
	 * 分录赋值
	 * @param addEntrys
	 * @return
	 */
	private PluralityDelBizBillEntryCollection  addEntryData(PluralityAddBizBillEntryCollection addEntrys,HashMap<String, Object> params){
		PluralityDelBizBillEntryCollection delEntryCol  = new PluralityDelBizBillEntryCollection();
		for(int i =0 ;i< addEntrys.size();i++){
			PluralityAddBizBillEntryInfo addEntryInfo = addEntrys.get(i);
			PluralityDelBizBillEntryInfo delEntryInfo = new PluralityDelBizBillEntryInfo();
			delEntryInfo.setPerson(addEntryInfo.getPerson());
			delEntryInfo.setDescription("自动终止");


			delEntryInfo.setAffairActionReason((AffairActionReasonInfo)params.get("reasonInfo"));
			delEntryInfo.setHrBizDefine((HRBizDefineInfo)params.get("defineInfo"));
			delEntryInfo.setDefaultLocale(addEntryInfo.getDefaultLocale());
			delEntryInfo.setHrOrgUnit(addEntryInfo.getHrOrgUnit());
			//兼职组织- 
			delEntryInfo.setOldEmpType(addEntryInfo.getEmpType());
			delEntryInfo.setOldAdminOrg(addEntryInfo.getAdminOrg());
			delEntryInfo.setOldPosition(addEntryInfo.getPosition());
			delEntryInfo.setOldCompany(addEntryInfo.getCompany());
			//任职职位-现任职位
			delEntryInfo.setEmpType(addEntryInfo.getOldEmpType());
			delEntryInfo.setMainPosition(addEntryInfo.getOldPosition());
			delEntryInfo.setMainAdminOrg(addEntryInfo.getOldAdminOrg());
			//任职部门-现任部门
			delEntryInfo.setMainDepartment(addEntryInfo.getOldAdminOrg());
			delEntryInfo.setMainCompany(addEntryInfo.getOldCompany());
			//不需要
//			delEntryInfo.setPosition(addEntryInfo.getOldPosition());
//			delEntryInfo.setAdminOrg(addEntryInfo.getOldAdminOrg());
			delEntryInfo.setBeginDate(addEntryInfo.getBizDate());
			delEntryInfo.setEmployerUnit(addEntryInfo.getEmployerUnit());
			delEntryInfo.put("pthwage", addEntryInfo.get("pthwage"));
			delEntryInfo.put("ptlmanager", addEntryInfo.get("ptlmanager"));
//			if(params.containsKey("isResign") && Boolean.getBoolean(params.get("isResign").toString()) ) {
//				
//			}
			//结束日期
			if(params.containsKey("endDate") ) {
				try {
					Date  endDate = DateTimeUtils.parseDate( params.get("endDate").toString(),  "yyyy-MM-dd");
					delEntryInfo.setBizDate(endDate);
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}else {
				delEntryInfo.setBizDate(addEntryInfo.getEndDate());
			}
			delEntryCol.add(delEntryInfo);
		}
		return delEntryCol;
	}
    
    
	
	
	@Override
	protected void _resignCreateEnd(Context ctx, String personId, String endDate) throws BOSException, EASBizException {
		super._resignCreateEnd(ctx, personId, endDate);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		if(StringUtils.isBlank(endDate)) {
			 endDate = DateTimeUtils.format (new Date(), "yyyy-MM-dd");
		}
		
		//变动类型
		AffairActionReasonInfo reasonInfo = AffairActionReasonFactory.getLocalInstance(ctx).getAffairActionReasonInfo(" where number = '0211' ");
		HRBizDefineInfo defineInfo = HRBizDefineFactory.getLocalInstance(ctx).getHRBizDefineInfo( " where number = 'SHR19' " );
		HashMap<String, Object> params = new HashMap<String,Object> ();
		params.put("reasonInfo", reasonInfo);
		params.put("defineInfo", defineInfo);
		params.put("isResign", true);
		params.put("endDate", endDate);
    	//查询兼职单
		StringBuilder sbu = new StringBuilder();
		sbu.append(" select  bill.fid billId  , delen.fid  delId from T_HR_PluralityAddBizBill bill ");
		sbu.append(" left join  T_HR_PluralityAddBizBillEntry entry on bill.FId = entry.fbillid "); 
		sbu.append(" left join T_HR_PluralityDelBizBillEntry  delen   ");
		sbu.append(" on  entry.fpersonid = delen.fpersonid   ");
		
		sbu.append(" and  entry.FBizDate = delen.FBeginDate  ");
        sbu.append(" and  entry.fpositionid = delen.FOldPositionID  ");
        sbu.append(" and   (   ");
        sbu.append(" entry.cfpthwage = delen.cfpthwage   ");
        sbu.append(" OR  ( entry.cfpthwage IS NULL AND delen.cfpthwage IS NULL  ) ");
        sbu.append("  )   ");
        sbu.append(" and   (   ");
        sbu.append(" entry.cfptlmanagerid = delen.cfptlmanagerid   ");
        sbu.append(" OR  ( entry.cfptlmanagerid IS NULL AND delen.cfptlmanagerid IS NULL  ) ");
        sbu.append("  )   ");
		sbu.append(" where  bill.fbillstate = '3'  ");
		sbu.append(" and  entry.fpersonid = '" + personId+"'" );
		sbu.append(" and  ( entry.fenddate >= {" + endDate+"}  or entry.fenddate is null ) ");
		
		 
		IRowSet rowSet = DbUtil.executeQuery(ctx, sbu.toString());
 
			try {
				while(rowSet.next()) {
					String billId = rowSet.getString("billId");
					String delId = rowSet.getString("delId");
					if(StringUtils.isBlank(delId)) {
						newDelBill(  ctx,   billId ,params);
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
 
		
		
		
		
	}
    
    
}