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


import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.hr.hbp.common.constants.HRBaseConstants;
import kd.sdk.hr.hom.business.activity.IActivityDomainService;
import nckd.jimin.jyyy.hr.wtc.wtis.task.SyncPunchCardTask;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Tyx 2025-11-03
 * 协作活动埋点处理扩展类
 */
public class ActivityDomainServiceEx implements IActivityDomainService {
    private static HRBaseServiceHelper ACTIVIHANDLE_HELPER = new HRBaseServiceHelper("hom_activityoverview");
    private static Log logger = LogFactory.getLog(ActivityDomainServiceEx.class);
    private static String SSAP_NUMBER = "ssap";
    @Override
    public void triggerTrdActivity(DynamicObject dynamicObject, DynamicObject dynamicObject1, Long aLong) {

    }

    @Override
    public Long triggerTrdActivityNew(DynamicObject activityDy, DynamicObject onbrdbillDyn, Long instanceId) {
        logger.info("-------- begin triggerTrdActivityNew -------- ");
        String activityNumber = activityDy.getString(HRBaseConstants.NUMBER);
        // 如果活动编码为宿舍安排编码ssap，则返回宿舍安排这个协作任务的主键ID；
        if (SSAP_NUMBER.equals(activityNumber)) {
            logger.info("当前活动编码：{}", activityNumber);
//            QFilter filter = new QFilter("activity.id", QCP.equals, activityDy.getPkValue());
//            filter.and("onbrdid.id", QCP.equals, onbrdbillDyn.getPkValue());
//            DynamicObject dyn = ACTIVIHANDLE_HELPER.queryOne("entryid", filter);
//            return dyn.getLong("id");
            QFilter filter = new QFilter("onbrd", QCP.equals,onbrdbillDyn.get("id"));
            DynamicObject dynamicObject = ACTIVIHANDLE_HELPER.loadDynamicObject(filter);
            DynamicObjectCollection entryentity = dynamicObject.getDynamicObjectCollection("entryentity");
            List<Long> collect = entryentity.stream().filter(row -> row.get("activity.id").equals(activityDy.get("id"))).map(rowData -> rowData.getLong("id")).collect(Collectors.toList());
            Long aLong = collect.get(0);
            logger.info("当前查询通用协作ID：{}", aLong);
            return aLong;
        }
        else {
            return null;
        }
    }

    @Override
    public void handleTrdActivity(List<Long> list) {

    }
}
