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

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.eas.basedata.org.AdminOrgUnitInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.synctask.SyncTranForIOTFacadeFactory;
import com.kingdee.eas.custom.synctask.SyncTranForOAFacadeControllerBean;
import com.kingdee.eas.custom.synctask.SyncTranForOAFacadeFactory;
import org.apache.log4j.Logger;

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

/**
 * @author qingwu
 * @date 2024/10/23
 * @apiNote
 */
public class AdminOrgUnitControllerBeanEx extends AdminOrgUnitControllerBean {
    private static Logger logger =
            Logger.getLogger(AdminOrgUnitControllerBeanEx.class);

    @Override
    protected IObjectPK _addnew(Context ctx, IObjectValue model) throws BOSException, EASBizException {
        logger.error("AdminOrgUnitControllerBeanEx--------------------_addnew");
        IObjectPK iObjectPK = super._addnew(ctx, model);
        AdminOrgUnitInfo info = (AdminOrgUnitInfo) model;
        String id = info.getId().toString();
        String number = info.getNumber().toString();
        adminOrgError(ctx, id, number);

        return iObjectPK;
    }


    @Override
    protected void _update(Context ctx, IObjectPK pk, IObjectValue model) throws BOSException, EASBizException {
        super._update(ctx, pk, model);
        logger.error("AdminOrgUnitControllerBeanEx--------------------_update");
        AdminOrgUnitInfo info = (AdminOrgUnitInfo) model;
        String id = info.getId().toString();
        String number = info.getNumber().toString();
        adminOrgError(ctx, id, number);


    }

    @Override
    protected void _updatePartial(Context ctx, IObjectValue model, SelectorItemCollection selector) throws BOSException, EASBizException {
        super._updatePartial(ctx, model, selector);
        logger.error("AdminOrgUnitControllerBeanEx--------------------_updatePartial");
        AdminOrgUnitInfo info = (AdminOrgUnitInfo) model;
        String id = info.getId().toString();
        String number = info.getNumber().toString();
        adminOrgError(ctx, id, number);


    }

    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);
        //logger.error("AdminOrgUnitControllerBeanEx------------iotMap--------" + iotMap.toString());
        //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());
        }
    }
}
