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.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.ActionTypeEnum;
import com.kingdee.eas.custom.synctask.ISyncTranForIOTFacade;
import com.kingdee.eas.custom.synctask.SyncTranForIOTFacadeFactory;
import com.kingdee.eas.custom.synctask.SyncTranForOAFacadeFactory;
import com.kingdee.eas.hr.ats.AtsUtil;
import org.apache.log4j.Logger;

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

/**
 * @author qingwu
 * @date 2024/10/25
 * @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;
        String id = info.getId().toString();
        String number = info.getNumber().toString();
        StringBuilder sb = new StringBuilder();
        //同步人员到OA
        Map<String, Map<String, String>> oaMap = SyncTranForOAFacadeFactory.getLocalInstance(ctx).syncPersonToOA(id, ActionTypeEnum.ADD);
        Map<String, String> result = oaMap.get(number);
        logger.error("PersonControllerBeanEx------------result--------" + result);
        if (result != null) {
            if (!result.get("state").equals("success")) {
                //throw new BOSException(map.get("msg").toString());
                sb.append("OA同步错误信息：").append(oaMap.get("msg").toString());
            }
        }
        if (sb.length() > 0) {
            throw new BOSException(sb.toString());
        }


        return iObjectPK;
    }


    @Override
    protected void _update(Context ctx, IObjectPK pk, IObjectValue model) throws BOSException, EASBizException {
        super._update(ctx, pk, model);
        logger.error("PersonControllerBeanEx--------------------_update");
        StringBuilder sb = new StringBuilder();
        PersonInfo personInfo = (PersonInfo) model;
        personError(ctx, personInfo.getId().toString(), personInfo.getNumber());

    }

    @Override
    protected void _updatePartial(Context ctx, IObjectValue model, SelectorItemCollection selector) throws BOSException, EASBizException {
        super._updatePartial(ctx, model, selector);
        logger.error("PersonControllerBeanEx--------------------_updatePartial");
        StringBuilder sb = new StringBuilder();
        PersonInfo personInfo = (PersonInfo) model;
        personError(ctx, personInfo.getId().toString(), personInfo.getNumber());

    }

    private void personError(Context ctx, String id, String number) throws BOSException, EASBizException {
        StringBuilder sb = new StringBuilder();
        //同步人员到OA
        Map<String, Map<String, String>> oaMap = SyncTranForOAFacadeFactory.getLocalInstance(ctx).syncPersonToOA(id, null);
        Map<String, String> result = oaMap.get(number);
        logger.error("PersonControllerBeanEx------------result--------" + result);
        if (result != null) {
            if (!result.get("state").equals("success")) {
                //throw new BOSException(map.get("msg").toString());
                sb.append("OA同步错误信息：").append(oaMap.get("msg").toString());
            }
        }
        //PersonInfo personInfo = PersonFactory.getLocalInstance(ctx).getPersonCollection("select employeeType.number where id = '" + id + "'").get(0);
        //String employeeTypeNumber = personInfo.getEmployeeType().getNumber();
        //if (!employeeTypeNumber.equals("009")) {
        //    //同步人员到IOT
        //    Map<String, String> iotMap = SyncTranForIOTFacadeFactory.getLocalInstance(ctx).syncPersonToIot(id);
        //    logger.error("PersonControllerBeanEx------------iotMap--------" + iotMap);
        //    logger.error("PersonControllerBeanEx------------number--------" + number);
        //    if (iotMap.containsKey(number)) {
        //        sb.append("Iot同步错误信息：").append(iotMap.get(number));
        //    }
        //}

        if (sb.length() > 0) {
            throw new BOSException(sb.toString());
        }

    }

}
