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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.eas.basedata.org.PositionInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.synctask.SyncTranForOAFacadeFactory;
import org.apache.log4j.Logger;

import java.util.Map;

/**
 * @author qingwu
 * @date 2024/10/28
 * @apiNote
 */
public class PositionOptFacadeControllerBeanEx extends PositionOptFacadeControllerBean {
    private static Logger logger = Logger.getLogger(OrgUnitOptFacadeControllerBeanEx.class);


    @Override
    protected IObjectPK _addNewPosition(Context ctx, PositionInfo info) throws BOSException, EASBizException {
        logger.error("PositionOptFacadeControllerBeanEx---------_addNewPosition");
        IObjectPK iObjectPK = super._addNewPosition(ctx, info);
        String number = info.getNumber();
        positionError(ctx, iObjectPK.toString(), number);
        return iObjectPK;

    }

    @Override
    protected void _updatePosition(Context ctx, IObjectPK pk, PositionInfo info) throws BOSException, EASBizException {
        logger.error("PositionOptFacadeControllerBeanEx---------_updatePosition");
        super._updatePosition(ctx, pk, info);
        String number = info.getNumber();
        positionError(ctx, pk.toString(), number);

    }

    /**
     * 职位移动
     *
     * @param ctx
     * @param positionInfo
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _movePosition(Context ctx, PositionInfo positionInfo) throws BOSException, EASBizException {
        logger.error("PositionOptFacadeControllerBeanEx---------_updatePosition");
        super._movePosition(ctx, positionInfo);
        String number = positionInfo.getNumber();
        String id = positionInfo.getId().toString();
        //同步岗位到OA
        positionError(ctx, id, number);


    }

    ///**
    // * 职位启用
    // *
    // * @param ctx
    // * @param positionID
    // * @throws BOSException
    // * @throws EASBizException
    // */
    //@Override
    //protected void _enable(Context ctx, String positionID) throws BOSException, EASBizException {
    //    logger.error("PositionOptFacadeControllerBeanEx---------_enable");
    //    super._enable(ctx, positionID);
    //    //同步岗位到OA
    //    PositionInfo info = PositionFactory.getLocalInstance(ctx).getPositionInfo(new ObjectUuidPK(positionID));
    //    String number = info.getNumber();
    //    positionError(ctx, positionID, number);
    //
    //}
    //
    ///**
    // * 职位禁用
    // *
    // * @param ctx
    // * @param positionID
    // * @throws BOSException
    // * @throws EASBizException
    // */
    //@Override
    //protected void _disable(Context ctx, String positionID) throws BOSException, EASBizException {
    //    logger.error("PositionOptFacadeControllerBeanEx---------_disable");
    //    super._disable(ctx, positionID);
    //    //同步岗位到OA
    //    PositionInfo info = PositionFactory.getLocalInstance(ctx).getPositionInfo(new ObjectUuidPK(positionID));
    //    String number = info.getNumber();
    //    positionError(ctx, positionID, number);
    //}

    /**
     * 覆蓋导入
     *
     * @param ctx
     * @param positionInfo
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _importUpdate(Context ctx, PositionInfo positionInfo) throws BOSException, EASBizException {
        super._importUpdate(ctx, positionInfo);
        String positionid = positionInfo.getId().toString();
        String number = positionInfo.getNumber().toString();
        positionError(ctx, positionid, number);
    }

    private void positionError(Context ctx, String id, String number) throws BOSException, EASBizException {
        StringBuilder sb = new StringBuilder();
        //同步人员到OA
        Map<String, Map<String, String>> oaMap = SyncTranForOAFacadeFactory.getLocalInstance(ctx).syncPositionToOA(id, null);
        logger.error("positionError======----" + oaMap.toString());
        Map<String, String> result = oaMap.get(number);
        logger.error("positionError======number----" + number);
        if (!result.get("state").equals("success")) {
            sb.append("OA同步错误信息：").append(result.get("msg").toString());
        }
        if (sb.length() > 0) {
            throw new BOSException(sb.toString());
        }
    }
}
