package server

import (
	"encoding/json"
	"game/common/model/user"
	"game/common/proto/pb"
	"game/common/rpc/logRecord"
	"game/common/utils"
	"game/server/db/operation"
	"github.com/fox/fox/ipb"
	"github.com/fox/fox/log"
)

// 获取玩家金流日志
func (s *DbService) onGetUserResLogs(iMsg *ipb.InternalMsg) *ipb.InternalMsg {
	var req = &logRecord.LogUserResReq{}
	err := json.Unmarshal(iMsg.Msg, req)
	if err != nil {
		log.ErrorF("error unmarshalling user account %v", err)
		iMsg.RpcCode = int32(pb.ErrCode_SystemErr)
		return iMsg
	}
	var logs []user.UserResourcesLog
	condition := make(map[string]any)
	condition["uid"] = req.UserId
	if req.ResName != "" {
		condition["res_name"] = req.ResName
	}
	if req.GameId > 0 {
		condition["game_id"] = req.GameId
	}
	if req.GameNo != "" {
		condition["game_no"] = req.GameNo
	}
	db := operation.LogDB.Where(condition)
	if req.StartTime != "" {
		db = db.Where("time >= ?", req.StartTime)
	}
	if req.EndTime != "" {
		db = db.Where("time <= ?", req.EndTime)
	}
	db = db.Order("time desc")
	err = db.Find(&logs).Error
	if err != nil {
		log.ErrorF("find table:%v condition:%v err:%v", user.UserResourcesLog{}.TableName(), utils.JsonMarshal(condition), err)
		iMsg.RpcCode = int32(pb.ErrCode_SystemErr)
		return iMsg
	}
	iMsg.Msg, err = json.Marshal(logs)
	if err != nil {
		log.ErrorF("error marshalling user account %v", err)
		iMsg.RpcCode = int32(pb.ErrCode_SystemErr)
		return iMsg
	}
	return iMsg
}
