package com.kingdee.eas.custom.certification;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.certification.utils.CreateCertification;
import com.kingdee.eas.custom.certification.utils.DateUtil;
import com.kingdee.eas.custom.sendmessage.utils.SendMessageUtils;
import com.kingdee.eas.hr.base.HRBillStateEnum;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.jdbc.rowset.IRowSet;
import com.kingdee.shr.base.syssetting.exception.ShrWebBizException;
import com.kingdee.shr.certificationservice.*;
import com.kingdee.shr.certificationservice.app.CertificationDrawUpMode;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import java.sql.SQLException;
import java.util.Date;


/**
 * 自动生成离职证明
 * 20241121
 */
public class AutoGenCertificationControllerBean extends AbstractAutoGenCertificationControllerBean
{
	private static Logger logger =
			Logger.getLogger("com.kingdee.eas.custom.certification.AutoGenCertificationControllerBean");


	@Override
	protected void _autoGenToResign(Context ctx, String curDate, int offset,int auditType) throws BOSException, EASBizException
	{
		super._autoGenToResign(ctx, curDate, offset,auditType);
		Date bizDate = new Date();
		//查询所有当天日期的离职单
		if (StringUtils.isNotBlank(curDate)) {
			bizDate = DateUtil.stringToDate(curDate, "yyyy-MM-dd");
		}
		Date startDate = DateUtil.getDateStartTime(bizDate);
		Date endDate = DateUtil.getDateEndTime(bizDate);
		if( offset > 0 ){
			endDate = DateUtil.add(endDate, offset) ;
		}else if( offset < 0 ) {
			startDate = DateUtil.add(startDate, offset) ;
		}
		//根据离职单生成离职证明
//		IResignBizBillEntry reIns = ResignBizBillEntryFactory.getLocalInstance(ctx);
//		EntityViewInfo viewInfo = new EntityViewInfo();
//		FilterInfo filterInfo = new FilterInfo();
//		filterInfo.getFilterItems()
//				.add(new FilterItemInfo("bizdate", DateUtil.getDateStartTime(bizDate), CompareType.GREATER_EQUALS));
//		filterInfo.getFilterItems()
//				.add(new FilterItemInfo("bizdate", DateUtil.getDateEndTime(bizDate), CompareType.LESS_EQUALS));
//		viewInfo.setFilter(filterInfo);
//		SelectorItemCollection sc = new SelectorItemCollection();
//		sc.add("bill.id");
//		viewInfo.setSelector(sc);
//		ResignBizBillEntryCollection entryCol = reIns.getResignBizBillEntryCollection(viewInfo);
//		// 循环获取bill.id
//		for (int i = 0; i < entryCol.size(); i++) {
//			ResignBizBillEntryInfo entryInfo = entryCol.get(i);
//			ResignBizBillInfo bill = entryInfo.getBill();
//			CreateCertification.createCertificationById(ctx, bill.getId().toString());
//		}

		// 根据离职单生成离职证明使用sql查询
		StringBuffer sbu = new StringBuffer();
		sbu.append("SELECT entry.fid entryId  FROM T_HR_ResignBizBill bill	");
		sbu.append("left join  T_HR_ResignBizBillEntry entry ON bill.fid = entry.fbillid  ")
				.append(" WHERE ")
				.append(" bill.FBillState = '3' ")
				.append(" and ( ( ")
				.append("  bill.FLastUpdateTime >=  '")
				.append( DateUtil.dateToString(startDate,"yyyy-MM-dd HH:mm:ss")  )
				.append("' AND bill.FLastUpdateTime <= '")
				.append( DateUtil.dateToString(endDate,"yyyy-MM-dd HH:mm:ss")  )
				.append(" 'and bill.fbizdate <= '")
				.append( DateUtil.dateToString(endDate,"yyyy-MM-dd HH:mm:ss")  )
				.append(" ' )")
				.append(" OR ( ")
				.append("  bill.fbizdate >=  '")
				.append( DateUtil.dateToString(startDate,"yyyy-MM-dd HH:mm:ss")  )
				.append("' AND bill.fbizdate <= '")
				.append( DateUtil.dateToString(endDate,"yyyy-MM-dd HH:mm:ss")  )
				.append(" '  ) ) ")
				.append(" and bill.fid not in (select CFSouBillId  from T_HR_HRCertificationBill where CFSouBillId is not null ) ");
		try {
			IRowSet iRowSet = DbUtil.executeQuery(ctx, sbu.toString());
			while (iRowSet.next()) {
				String entryId = iRowSet.getString("entryId");
				// 生成电子证明
				CreateCertification.createCert(ctx, entryId,auditType);
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	protected void _autoSendMsgByCert(Context ctx, String curDate, int offSet) throws BOSException, EASBizException {
		super._autoSendMsgByCert(ctx, curDate, offSet);
		Date bizDate = new Date();
		//查询所有当天日期的离职单
		if (StringUtils.isNotBlank(curDate)) {
			bizDate = DateUtil.stringToDate(curDate, "yyyy-MM-dd");
		}
		Date startDate = DateUtil.getDateStartTime(bizDate);
		Date endDate = DateUtil.getDateEndTime(bizDate);
		if( offSet > 0 ){
			endDate = DateUtil.add(endDate, offSet) ;
		}else if( offSet < 0 ) {
			startDate = DateUtil.add(startDate, offSet) ;
		}
		IHRCertificationBill cartIns = HRCertificationBillFactory.getLocalInstance(ctx);
		EntityViewInfo viewInfo = new EntityViewInfo();
		FilterInfo filterInfo = new FilterInfo();
		filterInfo.getFilterItems()
				.add(new FilterItemInfo("bizDate", DateUtil.getDateStartTime(startDate), CompareType.GREATER_EQUALS));
		filterInfo.getFilterItems()
				.add(new FilterItemInfo("bizDate", DateUtil.getDateEndTime(endDate), CompareType.LESS_EQUALS));
		filterInfo.getFilterItems().add(new FilterItemInfo("certificationStatus", "103", CompareType.NOTEQUALS));
		filterInfo.getFilterItems().add(new FilterItemInfo("isSign", true , CompareType.EQUALS));
		filterInfo.getFilterItems().add(new FilterItemInfo("isAutoGen", true, CompareType.EQUALS));
		filterInfo.getFilterItems().add(new FilterItemInfo("isSend", false, CompareType.EQUALS));
		filterInfo.getFilterItems().add(new FilterItemInfo("billState", HRBillStateEnum.AUDITED_VALUE, CompareType.EQUALS));
		filterInfo.getFilterItems().add(new FilterItemInfo("drawUpMode", CertificationDrawUpMode.ELECTRONIC_VALUE, CompareType.EQUALS));
		

		viewInfo.setFilter(filterInfo);
		//101未开
		//102已开
		SelectorItemCollection sc = new SelectorItemCollection();
		sc.add("id");
//		sc.add("certificationStatus");
		viewInfo.setSelector(sc);
		HRCertificationBillCollection certCol = cartIns.getHRCertificationBillCollection(viewInfo);
		try {
			for (int i = 0; i < certCol.size(); i++) {
				HRCertificationBillInfo hrCertificationBillInfo = certCol.get(i);
				String certiId = hrCertificationBillInfo.getId().toString();
				SendMessageUtils.sendMessage(ctx, certiId);
			}
		} catch (ShrWebBizException e) {
			e.printStackTrace();
		}  
	}
	
	
// 
	@Override
	public void autoStamp(Context ctx, String curDate, int offSet) throws BOSException, EASBizException {
		super.autoStamp(ctx, curDate, offSet);
		Date bizDate = new Date();
		//查询所有当天日期的离职单
		if (StringUtils.isNotBlank(curDate)) {
			bizDate = DateUtil.stringToDate(curDate, "yyyy-MM-dd");
		}
		Date startDate = DateUtil.getDateStartTime(bizDate);
		Date endDate = DateUtil.getDateEndTime(bizDate);
		if( offSet > 0 ){
			endDate = DateUtil.add(endDate, offSet) ;
		}else if( offSet < 0 ) {
			startDate = DateUtil.add(startDate, offSet) ;
		}
		IHRCertificationBill cartIns = HRCertificationBillFactory.getLocalInstance(ctx);
		EntityViewInfo viewInfo = new EntityViewInfo();
		FilterInfo filterInfo = new FilterInfo();
		filterInfo.getFilterItems()
				.add(new FilterItemInfo("bizDate", DateUtil.getDateStartTime(startDate), CompareType.GREATER_EQUALS));
		filterInfo.getFilterItems()
				.add(new FilterItemInfo("bizDate", DateUtil.getDateEndTime(endDate), CompareType.LESS_EQUALS));
		//101未开证明
		//102已开
		filterInfo.getFilterItems().add(new FilterItemInfo("certificationStatus", "101", CompareType.EQUALS));
		filterInfo.getFilterItems().add(new FilterItemInfo("isSign", false , CompareType.EQUALS));
		filterInfo.getFilterItems().add(new FilterItemInfo("isAutoGen", true, CompareType.EQUALS));
		filterInfo.getFilterItems().add(new FilterItemInfo("billState", HRBillStateEnum.AUDITED_VALUE, CompareType.EQUALS));
		filterInfo.getFilterItems().add(new FilterItemInfo("drawUpMode", CertificationDrawUpMode.ELECTRONIC_VALUE, CompareType.EQUALS));
		viewInfo.setFilter(filterInfo);
	
		SelectorItemCollection sc = new SelectorItemCollection();
		sc.add("id");
//		sc.add("certificationStatus");
		viewInfo.setSelector(sc);
		HRCertificationBillCollection certCol = cartIns.getHRCertificationBillCollection(viewInfo);
		for (int i = 0; i < certCol.size(); i++) {
			HRCertificationBillInfo hrCertificationBillInfo = certCol.get(i);
			String certiId = hrCertificationBillInfo.getId().toString();
			IECertificationOptFacade eCertificationOptFacade = ECertificationOptFacadeFactory.getLocalInstance(ctx);
			//是否需要修改证明状态true
			eCertificationOptFacade.createECertification(BOSUuid.read(certiId), true);
//				CertificateStatusEnum certificationStatus = hrCertificationBillInfo.getCertificationStatus();
//				if(certificationStatus.getValue() == 101){
//				}else if(certificationStatus.getValue() == 103){
//					continue;
//				}
			
		}
	}

}