package com.kingdee.eas.hr.affair.app;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectCollection;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.service.job.JobServiceFactory;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.basedata.person.PersonCollection;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.facade.leave.LeaveAllowanceFacade;
import com.kingdee.eas.custom.facade.leave.LeaveAllowanceFacadeFactory;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.affair.HRAffairBizBillEntryInfo;
import com.kingdee.eas.hr.ats.syn.BackTransactionJobUtils;
import com.kingdee.eas.hr.base.HRBillBaseEntryInfo;
import com.kingdee.eas.util.app.DbUtil;
import com.kingdee.util.DateTimeUtils;

import java.sql.Timestamp;
import java.util.*;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

/**
 * @ClassName EmpEnrollBizBillControllerBeanEx
 * @Description 入职单ControllerBean扩展
 * @Author VenkoXie
 * @Date 2024/2/22 10:59
 * @Version 1.0
 **/
public class EmpEnrollBizBillControllerBeanEx extends EmpEnrollBizBillControllerBean{
	
	private static Logger logger =
	        Logger.getLogger("com.kingdee.eas.hr.affair.app.EmpEnrollBizBillControllerBeanEx");

    @Override
    protected void _entryEffectOthers(Context ctx, HRBillBaseEntryInfo entry) throws BOSException, EASBizException {
        super._entryEffectOthers(ctx,entry);
        HRAffairBizBillEntryInfo affairEntry = (HRAffairBizBillEntryInfo)entry;
        String empNumber = affairEntry.getString("empNumber");
        PersonCollection personCollection = PersonFactory.getLocalInstance(ctx).getPersonCollection("where number = '" + empNumber + "'");
        int year = DateTimeUtils.getYear(new Date());
        if (personCollection != null && personCollection.size() > 0) {
            PersonInfo pInfo = PersonFactory.getLocalInstance(ctx).getPersonInfo(new ObjectUuidPK(personCollection.get(0).getId()));
            ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);
            scheduledThreadPoolExecutor.schedule(()->{
                try {
                    LeaveAllowanceFacadeFactory.getLocalInstance(ctx).createAnnualLeave(year, pInfo.getNumber());
                } catch (BOSException e) {
                    e.printStackTrace();
                }
            }, 5, TimeUnit.MINUTES);
        }
    }
    
//    @Override
//    protected void _auditEntryEffect(Context ctx, BOSUuid billID) throws BOSException, EASBizException {
//    	super._auditEntryEffect(ctx, billID);
//    	String emailSql = "/*dialect*/ UPDATE T_HR_PersonContactMethod AS a SET CFPrivateemail = b.CFPEMAIL "
//    			+ "FROM T_HR_EmpEnrollBizBillEntry AS b "
//    			+ "JOIN t_bd_person AS c ON c.fnumber = b.FEMPNUMBER "
//    			+ "WHERE a.FPERSONID = c.fid AND b.FBILLID = '"+billID+"'";
//    	logger.error("更新员工联系方式私人邮箱SQL："+emailSql);
//    	DbUtil.execute(ctx, emailSql);
//    }
    @Override
    protected void _auditEntryEffect(Context ctx, BOSUuid billID) throws BOSException, EASBizException {
        super._auditEntryEffect(ctx, billID);
        String emailSql = "/*dialect*/ UPDATE T_HR_PersonContactMethod AS a SET CFPrivateemail = b.CFPEMAIL FROM T_HR_EmpEnrollBizBillEntry AS b JOIN t_bd_person AS c ON c.fnumber = b.FEMPNUMBER WHERE a.FPERSONID = c.fid AND b.FBILLID = '" + billID + "'";
        logger.error("更新员工联系方式私人邮箱SQL：" + emailSql);
        DbUtil.execute(ctx, emailSql);
        String gtiitEmailSql = "/*dialect*/ UPDATE T_BD_Person as a set CFGtiitemail = b.CFGTIITEMAIL from T_HR_EmpEnrollBizBillEntry as b where a.fnumber=b.FEMPNUMBER and b.FBILLID = '" + billID + "'";
        logger.error("更新员工信息是否有广以邮箱SQL：" + gtiitEmailSql);
        DbUtil.execute(ctx, gtiitEmailSql);
    }
}
