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.AdminOrgUnitInfo;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.synctask.ActionTypeEnum;
import com.kingdee.eas.custom.synctask.SyncTranForIOTFacadeFactory;
import com.kingdee.eas.custom.synctask.SyncTranForOAFacadeFactory;
import org.apache.log4j.Logger;

import java.util.Date;
import java.util.List;
import java.util.Map;

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

    /**
     * 修改HR组织
     *
     * @param ctx
     * @param unitPk
     * @param hrOrgUnitInfo
     * @return
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected IObjectPK _updateHROrg(Context ctx, IObjectPK unitPk, HROrgUnitInfo hrOrgUnitInfo) throws BOSException, EASBizException {
        logger.error("OrgUnitOptFacadeControllerBeanEx--------_updateHROrg");
        IObjectPK iObjectPK = super._updateHROrg(ctx, unitPk, hrOrgUnitInfo);
        //同步组织到OA
        SyncTranForOAFacadeFactory.getLocalInstance(ctx).syncOrgUnitToOA(iObjectPK.toString(), null);
        //同步组织到IOT
        SyncTranForIOTFacadeFactory.getLocalInstance(ctx).syncOrgUnitToIot(iObjectPK.toString());

        return iObjectPK;
    }

    @Override
    protected IObjectPK _addNewAdminOrg(Context ctx, AdminOrgUnitInfo adminInfo) throws BOSException, EASBizException {
        logger.error("OrgUnitOptFacadeControllerBeanEx--------_addNewAdminOrg");
        IObjectPK iObjectPK = super._addNewAdminOrg(ctx, adminInfo);
        String id = adminInfo.getId().toString();
        String number = adminInfo.getNumber().toString();
        adminOrgError(ctx, id, number);


        return iObjectPK;

    }

    @Override
    protected IObjectPK _updateAdminOrg(Context ctx, AdminOrgUnitInfo adminInfo) throws BOSException, EASBizException {
        logger.error("OrgUnitOptFacadeControllerBeanEx--------_updateAdminOrg");
        IObjectPK iObjectPK = super._updateAdminOrg(ctx, adminInfo);
        String id = adminInfo.getId().toString();
        String number = adminInfo.getNumber().toString();
        adminOrgError(ctx, id, number);
        return iObjectPK;
    }

    /**
     * 组织移动
     *
     * @param ctx
     * @param orgID
     * @param parentID
     * @param effectDate
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _moveAdminOrg(Context ctx, String orgID, String parentID, Date effectDate) throws BOSException, EASBizException {
        logger.error("OrgUnitOptFacadeControllerBeanEx--------_moveAdminOrg");
        super._moveAdminOrg(ctx, orgID, parentID, effectDate);
        //同步组织到OA
        SyncTranForOAFacadeFactory.getLocalInstance(ctx).syncOrgUnitToOA(orgID, null);
        //同步Iot
        SyncTranForIOTFacadeFactory.getLocalInstance(ctx).syncOrgUnitToIot(orgID);

    }

    /**
     * 封存组织
     *
     * @param ctx
     * @param unitID
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _sealUpOrg(Context ctx, String unitID) throws BOSException, EASBizException {
        logger.error("OrgUnitOptFacadeControllerBeanEx--------_sealUpOrg");
        super._sealUpOrg(ctx, unitID);
        //同步组织到OA
        SyncTranForOAFacadeFactory.getLocalInstance(ctx).syncOrgUnitToOA(unitID, ActionTypeEnum.DELETE);
        //同步Iot
        SyncTranForIOTFacadeFactory.getLocalInstance(ctx).syncOrgUnitToIot(unitID);


    }

    /**
     * 组织反封存
     *
     * @param ctx
     * @param unitId
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _unSealUpOrg(Context ctx, String unitId) throws BOSException, EASBizException {
        logger.error("OrgUnitOptFacadeControllerBeanEx--------_unSealUpOrg");
        super._unSealUpOrg(ctx, unitId);
        SyncTranForOAFacadeFactory.getLocalInstance(ctx).syncOrgUnitToOA(unitId, ActionTypeEnum.DELETE);
        //同步Iot
        SyncTranForIOTFacadeFactory.getLocalInstance(ctx).syncOrgUnitToIot(unitId);

    }

    /**
     * 修改行政组织类型
     *
     * @param ctx
     * @param orgId
     * @param unitLayerTypeId
     * @throws BOSException
     * @throws EASBizException
     */
    @Override
    protected void _orgModifyLayerType(Context ctx, String orgId, String unitLayerTypeId) throws BOSException, EASBizException {
        logger.error("OrgUnitOptFacadeControllerBeanEx--------_orgModifyLayerType");
        super._orgModifyLayerType(ctx, orgId, unitLayerTypeId);
        SyncTranForOAFacadeFactory.getLocalInstance(ctx).syncOrgUnitToOA(orgId, null);
        //同步Iot
        SyncTranForIOTFacadeFactory.getLocalInstance(ctx).syncOrgUnitToIot(orgId);

    }


    private void adminOrgError(Context ctx, String id, String number) throws BOSException, EASBizException {
        StringBuilder sb = new StringBuilder();
        //同步组织到OA
        Map<String, Map<String, String>> oaMap = SyncTranForOAFacadeFactory.getLocalInstance(ctx).syncOrgUnitToOA(id, null);
        Map<String, String> result = oaMap.get(number);
        if (result.get("state").equals("fail")) {
            //throw new BOSException(map.get("msg").toString());
            sb.append("OA同步错误信息：").append(oaMap.get("msg").toString()).append("\n");
        }
        //同步组织到IOT
        //部署测试环境需要注释此行
        Map<String, List<Map<String, String>>> iotMap = SyncTranForIOTFacadeFactory.getLocalInstance(ctx).syncOrgUnitToIot(id);
        List<Map<String, String>> data = iotMap.get("data");
        if (data.size() > 0) {
            sb.append("Iot同步错误信息：");
            for (int i = 0; i < data.size(); i++) {
                Map<String, String> map = data.get(i);
                sb.append(map.get("msg")).append("\n");
            }
        }
        if (sb.length() > 0) {
            throw new BOSException(sb.toString());
        }
    }
}
