package nckd.jimin.jyyy.hr.haos.msg.impl;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.serialization.SerializationUtils;
import kd.bos.exception.KDBizException;
import kd.bos.orm.query.QFilter;
import kd.hr.haos.business.servicehelper.AdminOrgQueryServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.DispatchServiceHelper;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.hr.hbp.common.mservice.HRMServiceResult;
import nckd.jimin.jyyy.hr.haos.msg.api.IDealOrgLongNameService;
import nckd.jimin.jyyy.hr.haos.util.OrgUtils;
import kd.bos.servicehelper.operation.SaveServiceHelper;

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

/**
 * 2025-06-23 Tyx
 * 处理组织长名称微服务实现类
 * HR通用服务-业务事件订阅，订阅事件：行政组织上级调整生效
 */
public class DealOrgLongNameServiceImpl implements IDealOrgLongNameService {

    private static String adminOrgDetail_entity = "haos_adminorgdetail";


    @Override
    public HRMServiceResult consumerSaveMsg(DynamicObject dynamicObject) {
        Map<String, Object> msgContentMap = (Map) SerializationUtils.deSerializeFromBase64(dynamicObject.getString("msgcontent"));
        long billId = Long.parseLong(msgContentMap.get("billid").toString());
        long boid = Long.parseLong(msgContentMap.get("boid").toString());
        Map<String, Object> resultMap = (Map) DispatchServiceHelper.invokeBizService("odc", "homs", "IHAOSAdminOrgMsgQueryService", "orgParentChgEffectQueryDetail", new Object[]{billId, boid});
        // 4VTX1ACRU8A9!4VU/45/K1PJZ!4VU/45/K1PK8!4VU/45/K1PK9!50A4ILMNS91S
        Map afterMap = (Map) resultMap.get("afterChgInfo");
        DynamicObject org = BusinessDataServiceHelper.loadSingle(afterMap.get("fid"), adminOrgDetail_entity);
        String structLongNumber = org.getString("structlongnumber");
        String structNumber = org.getString("structnumber");
        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());
        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());
        Map<String, Map<String, String>> structMap = OrgUtils.getStructNumberMap(structLongNumberSet, null);
        //把本次修改的组织加进structMap
        structMap.put(structNumber, map);
        //处理本次修改组织下级组织的长名称和长编码
        for (DynamicObject subOrg : subOrgDyns) {
            OrgUtils.dealLongNameAndNumber(subOrg, structMap);
        }
        SaveServiceHelper.update(subOrgDyns);
        //return null;
        return HRMServiceResult.success();
    }
}
