package appservice

import (
	"go-admin/app/admin/models"
	adminservice "go-admin/app/admin/service"
	"go-admin/app/admin/service/dto"
	cDto "go-admin/common/dto"
	statuscode "go-admin/common/status_code"
	"time"

	"github.com/go-admin-team/go-admin-core/sdk/service"
	"github.com/jinzhu/copier"
)

type MemberBalanceLog struct {
	service.Service
}

// 分页查询个人资金记录
func (e *MemberBalanceLog) GetPage(req *dto.MemberBalanceLogPageAppReq, list *[]dto.MemberBalanceLogAppResp, count *int64) int {
	var data models.MemberBalanceLog
	datas := make([]models.MemberBalanceLog, 0)
	item := dto.MemberBalanceLogAppResp{}

	err := e.Orm.Model(&data).
		Where("user_id =?", req.UserId).
		Scopes(
			cDto.MakeCondition(req.GetNeedSearch()),
			cDto.Paginate(req.GetPageSize(), req.GetPageIndex()),
		).
		Find(&datas).Limit(-1).Offset(-1).
		Count(count).Error

	if err != nil {
		return statuscode.ServerError
	}
	dictService := adminservice.SysDictData{Service: e.Service}
	dicts, _ := dictService.GetByType("member_change_source")

	for _, v := range datas {
		copier.Copy(&item, v)

		item.CreateTimeUnix = v.CreatedAt.UnixNano() / int64(time.Millisecond)
		item.ChangeSourceName, _ = dictService.GetLanguageByDatas(&dicts, item.ChangeSource, req.Language)

		*list = append(*list, item)
	}

	return statuscode.OK
}
