github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/crud/coin/setting/crud.go (about)

     1  package setting
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/NpoolPlatform/chain-middleware/pkg/db/ent"
     7  	entsetting "github.com/NpoolPlatform/chain-middleware/pkg/db/ent/setting"
     8  	"github.com/NpoolPlatform/libent-cruder/pkg/cruder"
     9  
    10  	"github.com/google/uuid"
    11  	"github.com/shopspring/decimal"
    12  )
    13  
    14  type Req struct {
    15  	EntID                       *uuid.UUID
    16  	CoinTypeID                  *uuid.UUID
    17  	FeeCoinTypeID               *uuid.UUID
    18  	WithdrawFeeByStableUSD      *bool
    19  	WithdrawFeeAmount           *decimal.Decimal
    20  	CollectFeeAmount            *decimal.Decimal
    21  	HotWalletFeeAmount          *decimal.Decimal
    22  	LowFeeAmount                *decimal.Decimal
    23  	HotLowFeeAmount             *decimal.Decimal
    24  	HotWalletAccountAmount      *decimal.Decimal
    25  	PaymentAccountCollectAmount *decimal.Decimal
    26  	LeastTransferAmount         *decimal.Decimal
    27  	NeedMemo                    *bool
    28  	RefreshCurrency             *bool
    29  	CheckNewAddressBalance      *bool
    30  	DeletedAt                   *uint32
    31  }
    32  
    33  func CreateSet(c *ent.SettingCreate, req *Req) *ent.SettingCreate { //nolint
    34  	if req.EntID != nil {
    35  		c.SetEntID(*req.EntID)
    36  	}
    37  	if req.CoinTypeID != nil {
    38  		c.SetCoinTypeID(*req.CoinTypeID)
    39  	}
    40  	if req.FeeCoinTypeID != nil {
    41  		c.SetFeeCoinTypeID(*req.FeeCoinTypeID)
    42  	}
    43  	if req.WithdrawFeeByStableUSD != nil {
    44  		c.SetWithdrawFeeByStableUsd(*req.WithdrawFeeByStableUSD)
    45  	}
    46  	if req.WithdrawFeeAmount != nil {
    47  		c.SetWithdrawFeeAmount(*req.WithdrawFeeAmount)
    48  	}
    49  	if req.CollectFeeAmount != nil {
    50  		c.SetCollectFeeAmount(*req.CollectFeeAmount)
    51  	}
    52  	if req.HotWalletFeeAmount != nil {
    53  		c.SetHotWalletFeeAmount(*req.HotWalletFeeAmount)
    54  	}
    55  	if req.LowFeeAmount != nil {
    56  		c.SetLowFeeAmount(*req.LowFeeAmount)
    57  	}
    58  	if req.HotLowFeeAmount != nil {
    59  		c.SetHotLowFeeAmount(*req.HotLowFeeAmount)
    60  	}
    61  	if req.HotWalletAccountAmount != nil {
    62  		c.SetHotWalletAccountAmount(*req.HotWalletAccountAmount)
    63  	}
    64  	if req.PaymentAccountCollectAmount != nil {
    65  		c.SetPaymentAccountCollectAmount(*req.PaymentAccountCollectAmount)
    66  	}
    67  	if req.LeastTransferAmount != nil {
    68  		c.SetLeastTransferAmount(*req.LeastTransferAmount)
    69  	}
    70  	if req.NeedMemo != nil {
    71  		c.SetNeedMemo(*req.NeedMemo)
    72  	}
    73  	if req.RefreshCurrency != nil {
    74  		c.SetRefreshCurrency(*req.RefreshCurrency)
    75  	}
    76  	if req.CheckNewAddressBalance != nil {
    77  		c.SetCheckNewAddressBalance(*req.CheckNewAddressBalance)
    78  	}
    79  	return c
    80  }
    81  
    82  func UpdateSet(u *ent.SettingUpdateOne, req *Req) *ent.SettingUpdateOne {
    83  	if req.FeeCoinTypeID != nil {
    84  		u.SetFeeCoinTypeID(*req.FeeCoinTypeID)
    85  	}
    86  	if req.WithdrawFeeByStableUSD != nil {
    87  		u.SetWithdrawFeeByStableUsd(*req.WithdrawFeeByStableUSD)
    88  	}
    89  	if req.WithdrawFeeAmount != nil {
    90  		u.SetWithdrawFeeAmount(*req.WithdrawFeeAmount)
    91  	}
    92  	if req.CollectFeeAmount != nil {
    93  		u.SetCollectFeeAmount(*req.CollectFeeAmount)
    94  	}
    95  	if req.HotWalletFeeAmount != nil {
    96  		u.SetHotWalletFeeAmount(*req.HotWalletFeeAmount)
    97  	}
    98  	if req.LowFeeAmount != nil {
    99  		u.SetLowFeeAmount(*req.LowFeeAmount)
   100  	}
   101  	if req.HotLowFeeAmount != nil {
   102  		u.SetHotLowFeeAmount(*req.HotLowFeeAmount)
   103  	}
   104  	if req.HotWalletAccountAmount != nil {
   105  		u.SetHotWalletAccountAmount(*req.HotWalletAccountAmount)
   106  	}
   107  	if req.PaymentAccountCollectAmount != nil {
   108  		u.SetPaymentAccountCollectAmount(*req.PaymentAccountCollectAmount)
   109  	}
   110  	if req.LeastTransferAmount != nil {
   111  		u.SetLeastTransferAmount(*req.LeastTransferAmount)
   112  	}
   113  	if req.NeedMemo != nil {
   114  		u.SetNeedMemo(*req.NeedMemo)
   115  	}
   116  	if req.RefreshCurrency != nil {
   117  		u.SetRefreshCurrency(*req.RefreshCurrency)
   118  	}
   119  	if req.CheckNewAddressBalance != nil {
   120  		u.SetCheckNewAddressBalance(*req.CheckNewAddressBalance)
   121  	}
   122  	if req.DeletedAt != nil {
   123  		u.SetDeletedAt(*req.DeletedAt)
   124  	}
   125  
   126  	return u
   127  }
   128  
   129  type Conds struct {
   130  	EntID         *cruder.Cond
   131  	CoinTypeID    *cruder.Cond
   132  	FeeCoinTypeID *cruder.Cond
   133  }
   134  
   135  func SetQueryConds(q *ent.SettingQuery, conds *Conds) (*ent.SettingQuery, error) {
   136  	if conds.EntID != nil {
   137  		id, ok := conds.EntID.Val.(uuid.UUID)
   138  		if !ok {
   139  			return nil, fmt.Errorf("invalid entid")
   140  		}
   141  		switch conds.EntID.Op {
   142  		case cruder.EQ:
   143  			q.Where(entsetting.EntID(id))
   144  		default:
   145  			return nil, fmt.Errorf("invalid setting field")
   146  		}
   147  	}
   148  	if conds.CoinTypeID != nil {
   149  		id, ok := conds.CoinTypeID.Val.(uuid.UUID)
   150  		if !ok {
   151  			return nil, fmt.Errorf("invalid cointypeid")
   152  		}
   153  		switch conds.CoinTypeID.Op {
   154  		case cruder.EQ:
   155  			q.Where(entsetting.CoinTypeID(id))
   156  		default:
   157  			return nil, fmt.Errorf("invalid setting field")
   158  		}
   159  	}
   160  	if conds.FeeCoinTypeID != nil {
   161  		id, ok := conds.FeeCoinTypeID.Val.(uuid.UUID)
   162  		if !ok {
   163  			return nil, fmt.Errorf("invalid feecointypeid")
   164  		}
   165  		switch conds.FeeCoinTypeID.Op {
   166  		case cruder.EQ:
   167  			q.Where(entsetting.FeeCoinTypeID(id))
   168  		default:
   169  			return nil, fmt.Errorf("invalid setting field")
   170  		}
   171  	}
   172  	q.Where(entsetting.DeletedAt(0))
   173  	return q, nil
   174  }