package credit_balance

import (
	"context"
	"github.com/suyuan32/simple-admin-common/msg/errormsg"
	"github.com/suyuan32/simple-admin-common/utils/pointy"
	"github.com/suyuan32/simple-admin-core/rpc/types/core"
	"github.com/zeromicro/go-zero/core/errorx"
	"github.com/zeromicro/go-zero/core/logx"
	"wechat-api/database/dao/wechat/model"
	"wechat-api/internal/svc"
	"wechat-api/internal/types"
)

type GetCreditBalanceListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetCreditBalanceListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCreditBalanceListLogic {
	return &GetCreditBalanceListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetCreditBalanceListLogic) GetCreditBalanceList(req *types.CreditBalanceListReq) (*types.CreditBalanceListResp, error) {
	isAdmin := l.ctx.Value("isAdmin").(bool)
	if !isAdmin {
		return nil, errorx.NewInvalidArgumentError("权限不足")
	}

	query := l.svcCtx.WechatDB.Find(&model.CreditBalance{})
	if req.OrganizationId != nil && *req.OrganizationId > 0 {
		query = query.Where("organization_id = ?", *req.OrganizationId)
	}
	creditBalanceList := make([]model.CreditBalance, 0)
	err := query.Order("created_at DESC").Find(&creditBalanceList).Error

	if err != nil {
		l.Logger.Error(err)
		return nil, errorx.NewInvalidArgumentError("查询列表失败")
	}

	var count int64
	err = query.Count(&count).Error
	if err != nil {
		l.Logger.Error(err)
		return nil, errorx.NewInvalidArgumentError("查询个数失败")
	}

	resp := &types.CreditBalanceListResp{}
	resp.Msg = errormsg.Success
	resp.Data.Total = uint64(count)

	for _, v := range creditBalanceList {
		departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: uint64(v.OrganizationID)})
		if err != nil {
			return nil, err
		}

		status := int(v.Status)
		id := uint64(v.ID)
		balance, _ := v.Balance.Float64()
		organizationId := uint64(v.OrganizationID)
		resp.Data.Data = append(resp.Data.Data,
			types.CreditBalanceInfo{
				BaseIDInfo: types.BaseIDInfo{
					Id:        &id,
					CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
					UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
				},
				Status:           &status,
				Balance:          &balance,
				OrganizationId:   &organizationId,
				OrganizationName: departmentInfo.Name,
			})
	}

	return resp, nil

	//var predicates []predicate.CreditBalance
	//if req.OrganizationId != nil && *req.OrganizationId > 0 {
	//	predicates = append(predicates, creditbalance.OrganizationID(*req.OrganizationId))
	//}
	//data, err := l.svcCtx.DB.CreditBalance.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
	//
	//if err != nil {
	//	return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
	//}
	//
	//resp := &types.CreditBalanceListResp{}
	//resp.Msg = errormsg.Success
	//resp.Data.Total = data.PageDetails.Total
	//
	//for _, v := range data.List {
	//	departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: v.OrganizationID})
	//	if err != nil {
	//		return nil, err
	//	}
	//
	//	resp.Data.Data = append(resp.Data.Data,
	//		types.CreditBalanceInfo{
	//			BaseIDInfo: types.BaseIDInfo{
	//				Id:        &v.ID,
	//				CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
	//				UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
	//			},
	//			Status:           &v.Status,
	//			Balance:          &v.Balance,
	//			OrganizationId:   &v.OrganizationID,
	//			OrganizationName: departmentInfo.Name,
	//		})
	//}
	//
	//return resp, nil
}
