package nckd.jimin.jyyy.hr.haos.explugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.hr.haos.business.servicehelper.AdminOrgQueryServiceHelper;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.hr.homs.business.service.ext.function.IBatchEffectServiceExtend;
import kd.hr.homs.common.model.batcheffect.BatchEffectBeforeArgs;
import nckd.jimin.jyyy.hr.haos.util.OrgUtils;
import nckd.jimin.jyyy.hr.hrmp.hbpm.opplugin.web.position.PositionDataProviderEx;

import java.util.*;
import java.util.stream.Collectors;


/**
 * Tyx 2025-06-20
 * 组织保存前扩展插件，处理长名称
 * haos_adminorghr和haos_adminorg增加文本字段nckd_longname，其他地方增加引用属性
 */
public class BatchEffectServiceExtendEx implements IBatchEffectServiceExtend {
    private static final Log log = LogFactory.getLog(BatchEffectServiceExtendEx.class);
    private static String adminOrgDetail_entity = "haos_adminorgdetail";
    @Override
    public void beforeSaveAdminOrg(BatchEffectBeforeArgs batchEffectBeforeArgs) {
        log.info("---- inter beforeSaveAdminOrg ----");
        //调整上级的情况 structlongnumber 不会发生变化
        //这里仅处理非调整上级的情况，调整上级情况通过协同处理DealOrgLongNameServiceImpl.consumerSaveMsg
        if(batchEffectBeforeArgs.getBill().getDynamicObjectType().getName().equals("homs_orgfastchgparent"))
            return;
        List<DynamicObject> list = batchEffectBeforeArgs.getAdminOrgList();
        for (DynamicObject org : list) {
            //4VTX1ACRU8A9!4VU/45/K1PJZ!4VU/45/K1PK8!4VU/45/K1PK9!50A4ILMNS91S
            String structLongNumber = org.getString("structlongnumber");
            String structNumber = org.getString("structnumber");
            log.info("---- beforeSaveAdminOrg处理中：{}", org.getString("number"));
            Map map = new HashMap();
            map.put("name", org.getString("name"));
            map.put("number", org.getString("number"));
            List<Map<String, Object>> subOrgList = AdminOrgQueryServiceHelper.batchQueryAllSubOrg(Arrays.asList(org.getLong("boid")), new Date());
            Set subOrgIdset = subOrgList.stream().map(i -> i.get("orgId")).collect(Collectors.toSet());
            //排除掉本次生效的组织，让标品处理
            subOrgIdset.remove(org.getLong("id"));
            String selectProperties = "id,boid,number,name,structlongnumber,structnumber,nckd_longname,nckd_longnumber";
            QFilter idQFilter = new QFilter("id", "in", subOrgIdset);
            DynamicObject[] subOrgDyns = new HRBaseServiceHelper(adminOrgDetail_entity).query(selectProperties, new QFilter[]{idQFilter});
            //查出所有涉及到的structNumber，不含本次修改的组织
            Set structLongNumberSet = Arrays.asList(subOrgDyns).stream()
                    .map(dyx -> dyx.getString("structlongnumber"))
                    .flatMap(longNumber -> Arrays.stream(longNumber.split("!")))
                    .collect(Collectors.toSet());
            Set set1 = Arrays.asList(structLongNumber.split("!")).stream().collect(Collectors.toSet());
            structLongNumberSet.addAll(set1);
            structLongNumberSet.remove(structNumber);
            Map<String, Map<String, String>> structMap = OrgUtils.getStructNumberMap(structLongNumberSet, null);
            //把本次修改的组织加进structMap
            structMap.put(structNumber, map);
            //处理本次修改组织的长名称和长编码
            OrgUtils.dealLongNameAndNumber(org, structMap);
            //处理本次修改组织下级组织的长名称和长编码
            for (DynamicObject subOrg : subOrgDyns) {
                OrgUtils.dealLongNameAndNumber(subOrg, structMap);
            }
            SaveServiceHelper.update(subOrgDyns);
        }
    }




}
