package processor

import (
	"encoding/json"
	"fmt"

	"be.ems/src/framework/logger"
	"be.ems/src/framework/resp"
	neService "be.ems/src/modules/ne/service"
	neDataService "be.ems/src/modules/ne_data/service"
)

// GetUEConnect 获取UE会话事件
func GetUEConnect(requestID string, data any) ([]byte, error) {
	if data == nil {
		logger.Errorf("ws processor GetUEConnect data is nil")
		return nil, fmt.Errorf("query data error")
	}
	dataMap := data.(map[string]any)
	query := make(map[string]string, 0)
	for k, v := range dataMap {
		query[k] = fmt.Sprintf("%v", v)
	}

	// 查询网元信息 rmUID
	neInfo := neService.NewNeInfo.FindByNeTypeAndNeID(query["neType"], query["neId"])
	if neInfo.NeType == "" {
		return nil, fmt.Errorf("query neinfo not found")
	}
	query["rmUID"] = neInfo.RmUID
	rows, total := neDataService.NewUEEvent.FindByPage(neInfo.NeType, query)
	resultByte, err := json.Marshal(resp.Ok(map[string]any{
		"requestId": requestID,
		"data":      map[string]any{"rows": rows, "total": total},
	}))
	return resultByte, err
}
