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.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.synctask.SyncTranForIOTFacadeFactory;
import com.kingdee.eas.custom.synctask.SyncTranForOAFacadeFactory;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.hr.emp.app.PersonOptBaseInfo;
import com.kingdee.eas.hr.emp.app.PersonOptFacadeControllerBean;
import org.apache.log4j.Logger;

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

/**
 * @author qingwu
 * @date 2024/11/7
 * @apiNote
 */
public class PersonOptFacadeControllerBeanEx extends PersonOptFacadeControllerBean {
    private static Logger logger = Logger.getLogger(PersonOptFacadeControllerBeanEx.class);

    @Override
    protected IObjectPK _personAddNew(Context ctx, Date EFFDT, PersonOptBaseInfo personOptBaseInfo) throws BOSException, EASBizException {
        logger.error("PersonOptFacadeControllerBeanEx--------_personAddNew");
        IObjectPK iObjectPK = super._personAddNew(ctx, EFFDT, personOptBaseInfo);
        PersonInfo person = personOptBaseInfo.getPerson();
        String number = person.getNumber();
        String id = person.getId().toString();
        personError(ctx, id, number);
        return iObjectPK;

    }

    @Override
    protected void _personUpdate(Context ctx, Date EFFDT, PersonOptBaseInfo personOptBaseInfo) throws BOSException, EASBizException {
        logger.error("PersonOptFacadeControllerBeanEx--------_personUpdate");
        super._personUpdate(ctx, EFFDT, personOptBaseInfo);
        PersonInfo person = personOptBaseInfo.getPerson();
        String number = person.getNumber();
        String id = person.getId().toString();
        personError(ctx, id, number);

    }

    @Override
    protected void _personUpdatePartial(Context ctx, CoreBaseInfo model, SelectorItemCollection selector) throws BOSException, EASBizException {
        logger.error("PersonOptFacadeControllerBeanEx--------_personUpdatePartial");
        super._personUpdatePartial(ctx, model, selector);
        PersonInfo person = (PersonInfo) model;
        String number = person.getNumber();
        String personId = person.getId().toString();
        //Map<String, String> map = SyncTranForIOTFacadeFactory.getLocalInstance(ctx).syncPersonToIot(personId);
        //if (map != null) {
        //    if (StringUtils.isEmpty(number)) {
        //        PersonInfo personInfo = PersonFactory.getLocalInstance(ctx).getPersonInfo(new ObjectUuidPK(personId));
        //        number = personInfo.getNumber();
        //    }
        //    throw new BOSException(map.get(number));
        //}
    }

    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);
        if (!result.get("state").equals("success")) {
            //throw new BOSException(map.get("msg").toString());
            sb.append("OA同步错误信息：").append(oaMap.get("msg").toString());
        }
        //同步人员到IOT
        //部署测试环境需要注释此行
        Map<String, List<Map<String, String>>> iotMap = SyncTranForIOTFacadeFactory.getLocalInstance(ctx).syncPersonToIot(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());
        }
    }
}
