package com.kingdee.eas.basedata.org.app;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.ctrl.swing.StringUtils;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.basedata.person.app.PersonControllerBean;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.synctask.task.ISynPersonToPunchCardFacade;
import com.kingdee.eas.custom.synctask.task.SynPersonToPunchCardFacadeFactory;
import org.apache.log4j.Logger;


/**
 * @author qingwu
 * @date 2025/5/19
 * @apiNote
 */
public class PersonControllerBeanEx extends PersonControllerBean {
    private static Logger logger =
            Logger.getLogger(PersonControllerBeanEx.class);

    @Override
    protected IObjectPK _addnew(Context ctx, IObjectValue model) throws BOSException, EASBizException {
        logger.error("PersonControllerBeanEx--------------------_addnew");
        IObjectPK iObjectPK = super._addnew(ctx, model);
        PersonInfo info = (PersonInfo) model;
        getUpdatePerson(ctx, info);
        return iObjectPK;
    }


    @Override
    protected void _update(Context ctx, IObjectPK pk, IObjectValue model) throws BOSException, EASBizException {
        super._update(ctx, pk, model);
        logger.error("PersonControllerBeanEx--------------------_update");
        PersonInfo personInfo = (PersonInfo) model;
        getUpdatePerson(ctx, personInfo);

    }

    @Override
    protected void _updatePartial(Context ctx, IObjectValue model, SelectorItemCollection selector) throws BOSException, EASBizException {
        super._updatePartial(ctx, model, selector);
        logger.error("PersonControllerBeanEx--------------------_updatePartial");
        PersonInfo personInfo = (PersonInfo) model;
        getUpdatePerson(ctx, personInfo);
    }

    /**
     * 用不人员信息到打卡记录
     *
     * @param ctx
     * @param model
     * @throws BOSException
     * @throws EASBizException
     */
    public void getUpdatePerson(Context ctx, PersonInfo model) throws BOSException, EASBizException {
        if (StringUtils.isEmpty(model.getNumber())) {
            model = PersonFactory.getLocalInstance(ctx).getPersonInfo(new ObjectUuidPK(model.getId()));
        }
        ISynPersonToPunchCardFacade iSynPersonToPunchCardFacade = SynPersonToPunchCardFacadeFactory.getLocalInstance(ctx);
        //人员更新
        iSynPersonToPunchCardFacade.updatePerson(model.getNumber());
        //人员删除
        iSynPersonToPunchCardFacade.dimissionPerson(model.getNumber());
    }
}
