package processor

import (
	"encoding/json"
	"fmt"

	"be.ems/src/framework/logger"
	"be.ems/src/framework/resp"
	"be.ems/src/framework/utils/parse"
	neInfoService "be.ems/src/modules/ne/service"
	neDataService "be.ems/src/modules/ne_data/service"
)

// GetCDRConnect 获取CDR会话事件
func GetCDRConnect(requestID string, data any) ([]byte, error) {
	if data == nil {
		logger.Errorf("ws processor GetCDRConnect 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)
	}
	// 分页检查
	pageNum := parse.Number(query["pageNum"])
	pageSize := parse.Number(query["pageSize"])
	if pageNum == 0 || pageSize == 0 {
		return nil, fmt.Errorf("pageNum or pageSize not is empty")
	}
	// 查询网元信息 rmUID
	neInfo := neInfoService.NewNeInfo.FindByNeTypeAndNeID(query["neType"], query["neId"])
	if neInfo.NeType == "" {
		return nil, fmt.Errorf("query neinfo not found")
	}
	query["rmUID"] = neInfo.RmUID
	rows, total := neDataService.NewCDREvent.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
}
