代码初始化
This commit is contained in:
137
dao/knapsack/knapsack_dao.go
Normal file
137
dao/knapsack/knapsack_dao.go
Normal file
@@ -0,0 +1,137 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
|
||||
knapsackConsts "shop-user-trade/consts/knapsack"
|
||||
"shop-user-trade/consts/public"
|
||||
knapsackDto "shop-user-trade/model/dto/knapsack"
|
||||
knapsackEntity "shop-user-trade/model/entity/knapsack"
|
||||
)
|
||||
|
||||
var Knapsack = new(knapsackDao)
|
||||
|
||||
type knapsackDao struct{}
|
||||
|
||||
// Insert 插入背包项
|
||||
func (d *knapsackDao) Insert(ctx context.Context, req *knapsackDto.CreateKnapsackReq) (id int64, err error) {
|
||||
var entity *knapsackEntity.Knapsack
|
||||
if err = gconv.Struct(req, &entity); err != nil {
|
||||
return
|
||||
}
|
||||
entity.Status = knapsackConsts.KnapsackStatusActive
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).Data(entity).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// Update 更新背包项
|
||||
func (d *knapsackDao) Update(ctx context.Context, req *knapsackDto.UpdateKnapsackReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).
|
||||
Data(req).
|
||||
Where(knapsackEntity.KnapsackCol.Id, req.Id).
|
||||
OmitEmpty().
|
||||
Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// Delete 删除背包项(软删除)
|
||||
func (d *knapsackDao) Delete(ctx context.Context, req *knapsackDto.DeleteKnapsackReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).
|
||||
Where(knapsackEntity.KnapsackCol.Id, req.Id).
|
||||
Delete()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetOne 获取单个背包项
|
||||
func (d *knapsackDao) GetOne(ctx context.Context, req *knapsackDto.GetKnapsackReq) (res *knapsackEntity.Knapsack, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).
|
||||
Where(knapsackEntity.KnapsackCol.Id, req.Id).
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取背包项
|
||||
func (d *knapsackDao) GetByID(ctx context.Context, id int64) (res *knapsackEntity.Knapsack, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).
|
||||
Where(knapsackEntity.KnapsackCol.Id, id).
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// Count 获取背包数量
|
||||
func (d *knapsackDao) Count(ctx context.Context, req *knapsackDto.ListKnapsackReq) (count int, err error) {
|
||||
return d.buildListFilter(ctx, req).Count()
|
||||
}
|
||||
|
||||
// List 获取背包列表
|
||||
func (d *knapsackDao) List(ctx context.Context, req *knapsackDto.ListKnapsackReq) (res []knapsackEntity.Knapsack, total int, err error) {
|
||||
model := d.buildListFilter(ctx, req).OrderDesc(knapsackEntity.KnapsackCol.CreatedAt)
|
||||
if req.Page != nil {
|
||||
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||||
}
|
||||
r, total, err := model.AllAndCount(false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// ListExpired 获取过期背包项列表
|
||||
func (d *knapsackDao) ListExpired(ctx context.Context) (res []knapsackEntity.Knapsack, err error) {
|
||||
now := time.Now().Unix()
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).
|
||||
WhereIn(knapsackEntity.KnapsackCol.Status, []knapsackConsts.KnapsackStatus{
|
||||
knapsackConsts.KnapsackStatusActive,
|
||||
knapsackConsts.KnapsackStatusListed,
|
||||
}).
|
||||
WhereLT(knapsackEntity.KnapsackCol.ExpireAt, now).
|
||||
WhereNotNull(knapsackEntity.KnapsackCol.ExpireAt).
|
||||
All()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询过滤条件
|
||||
func (d *knapsackDao) buildListFilter(ctx context.Context, req *knapsackDto.ListKnapsackReq) *gdb.Model {
|
||||
model := gfdb.DB(ctx).Model(ctx, public.TableNameKnapsack).OmitEmpty()
|
||||
if !g.IsEmpty(req.UserID) {
|
||||
model = model.Where(knapsackEntity.KnapsackCol.UserID, req.UserID)
|
||||
}
|
||||
if req.Status != nil {
|
||||
model = model.Where(knapsackEntity.KnapsackCol.Status, *req.Status)
|
||||
}
|
||||
if !g.IsEmpty(req.Type) {
|
||||
model = model.Where(knapsackEntity.KnapsackCol.Type, req.Type)
|
||||
}
|
||||
if !g.IsEmpty(req.Keyword) {
|
||||
model = model.WhereLike(knapsackEntity.KnapsackCol.AssetName, "%"+req.Keyword+"%")
|
||||
}
|
||||
return model
|
||||
}
|
||||
151
dao/market/market_dao.go
Normal file
151
dao/market/market_dao.go
Normal file
@@ -0,0 +1,151 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
|
||||
marketConsts "shop-user-trade/consts/market"
|
||||
"shop-user-trade/consts/public"
|
||||
marketDto "shop-user-trade/model/dto/market"
|
||||
marketEntity "shop-user-trade/model/entity/market"
|
||||
)
|
||||
|
||||
var Market = new(marketDao)
|
||||
|
||||
type marketDao struct{}
|
||||
|
||||
// Insert 插入市场物品
|
||||
func (d *marketDao) Insert(ctx context.Context, req *marketDto.CreateMarketReq) (id int64, err error) {
|
||||
var entity *marketEntity.Market
|
||||
if err = gconv.Struct(req, &entity); err != nil {
|
||||
return
|
||||
}
|
||||
entity.Status = marketConsts.MarketStatusActive
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameMarket).Data(entity).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// Update 更新市场物品
|
||||
func (d *marketDao) Update(ctx context.Context, req *marketDto.UpdateMarketReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameMarket).
|
||||
Data(req).
|
||||
Where(marketEntity.MarketCol.Id, req.Id).
|
||||
OmitEmpty().
|
||||
Update()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// Delete 删除市场物品(软删除)
|
||||
func (d *marketDao) Delete(ctx context.Context, req *marketDto.DeleteMarketReq) (rows int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameMarket).
|
||||
Where(marketEntity.MarketCol.Id, req.Id).
|
||||
Delete()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.RowsAffected()
|
||||
}
|
||||
|
||||
// GetOne 获取单个市场物品
|
||||
func (d *marketDao) GetOne(ctx context.Context, req *marketDto.GetMarketReq) (res *marketEntity.Market, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameMarket).
|
||||
Where(marketEntity.MarketCol.Id, req.Id).
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取市场物品
|
||||
func (d *marketDao) GetByID(ctx context.Context, id int64) (res *marketEntity.Market, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameMarket).
|
||||
Where(marketEntity.MarketCol.Id, id).
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// GetByKnapsackID 根据背包项ID获取市场物品
|
||||
func (d *marketDao) GetByKnapsackID(ctx context.Context, knapsackID int64) (res *marketEntity.Market, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameMarket).
|
||||
Where(marketEntity.MarketCol.KnapsackID, knapsackID).
|
||||
WhereNotIn(marketEntity.MarketCol.Status, []marketConsts.MarketStatus{
|
||||
marketConsts.MarketStatusSold,
|
||||
marketConsts.MarketStatusInactive,
|
||||
marketConsts.MarketStatusExpired,
|
||||
}).
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// Count 获取市场物品数量
|
||||
func (d *marketDao) Count(ctx context.Context, req *marketDto.ListMarketReq) (count int, err error) {
|
||||
return d.buildListFilter(ctx, req).Count()
|
||||
}
|
||||
|
||||
// List 获取市场列表
|
||||
func (d *marketDao) List(ctx context.Context, req *marketDto.ListMarketReq) (res []marketEntity.Market, total int, err error) {
|
||||
model := d.buildListFilter(ctx, req).OrderDesc(marketEntity.MarketCol.CreatedAt)
|
||||
if req.Page != nil {
|
||||
model.Page(int(req.Page.PageNum), int(req.Page.PageSize))
|
||||
}
|
||||
r, total, err := model.AllAndCount(false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// ListExpired 获取过期市场物品列表
|
||||
func (d *marketDao) ListExpired(ctx context.Context) (res []marketEntity.Market, err error) {
|
||||
now := time.Now().Unix()
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameMarket).
|
||||
Where(marketEntity.MarketCol.Status, marketConsts.MarketStatusActive).
|
||||
WhereLT(marketEntity.MarketCol.ListExpireAt, now).
|
||||
WhereNotNull(marketEntity.MarketCol.ListExpireAt).
|
||||
All()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// buildListFilter 构建列表查询过滤条件
|
||||
func (d *marketDao) buildListFilter(ctx context.Context, req *marketDto.ListMarketReq) *gdb.Model {
|
||||
model := gfdb.DB(ctx).Model(ctx, public.TableNameMarket).OmitEmpty()
|
||||
if !g.IsEmpty(req.UserID) {
|
||||
model = model.Where(marketEntity.MarketCol.UserID, req.UserID)
|
||||
}
|
||||
if req.Status != nil {
|
||||
model = model.Where(marketEntity.MarketCol.Status, *req.Status)
|
||||
}
|
||||
if !g.IsEmpty(req.Type) {
|
||||
model = model.Where(marketEntity.MarketCol.Type, req.Type)
|
||||
}
|
||||
if !g.IsEmpty(req.Keyword) {
|
||||
model = model.WhereLike(marketEntity.MarketCol.AssetName, "%"+req.Keyword+"%")
|
||||
}
|
||||
return model
|
||||
}
|
||||
134
dao/wallet/wallet_dao.go
Normal file
134
dao/wallet/wallet_dao.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
|
||||
"shop-user-trade/consts/public"
|
||||
walletConsts "shop-user-trade/consts/wallet"
|
||||
walletDto "shop-user-trade/model/dto/wallet"
|
||||
walletEntity "shop-user-trade/model/entity/wallet"
|
||||
)
|
||||
|
||||
var Wallet = new(walletDao)
|
||||
|
||||
type walletDao struct{}
|
||||
|
||||
// GetByUserID 根据用户ID获取钱包
|
||||
func (d *walletDao) GetByUserID(ctx context.Context, userID int64) (res *walletEntity.Wallet, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameWallet).
|
||||
Where(walletEntity.WalletCol.UserID, userID).
|
||||
WhereNot(walletEntity.WalletCol.Status, int(walletConsts.WalletStatusFrozen)).
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if r.IsEmpty() {
|
||||
return nil, nil
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// GetByID 根据ID获取钱包
|
||||
func (d *walletDao) GetByID(ctx context.Context, walletID int64) (res *walletEntity.Wallet, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameWallet).
|
||||
Where(walletEntity.WalletCol.Id, walletID).
|
||||
One()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if r.IsEmpty() {
|
||||
return nil, nil
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// Create 创建钱包
|
||||
func (d *walletDao) Create(ctx context.Context, req *walletDto.CreateWalletReq) (id int64, err error) {
|
||||
entity := &walletEntity.Wallet{
|
||||
UserID: req.UserId,
|
||||
Balance: 0,
|
||||
Currency: req.Currency,
|
||||
Status: walletConsts.WalletStatusEnabled,
|
||||
Version: 1,
|
||||
}
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameWallet).Data(entity).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// UpdateBalance 更新余额(乐观锁)
|
||||
func (d *walletDao) UpdateBalance(ctx context.Context, walletID int64, amount int64, opType string, version int64) (bool, error) {
|
||||
var updateData gdb.Map
|
||||
switch opType {
|
||||
case "income":
|
||||
updateData = gdb.Map{
|
||||
"balance": gdb.Raw("balance + " + gconv.String(amount)),
|
||||
"version": gdb.Raw("version + 1"),
|
||||
}
|
||||
case "expense":
|
||||
updateData = gdb.Map{
|
||||
"balance": gdb.Raw("balance - " + gconv.String(amount)),
|
||||
"version": gdb.Raw("version + 1"),
|
||||
}
|
||||
case "freeze":
|
||||
updateData = gdb.Map{
|
||||
"status": int(walletConsts.WalletStatusFrozen),
|
||||
}
|
||||
case "unfreeze":
|
||||
updateData = gdb.Map{
|
||||
"status": int(walletConsts.WalletStatusEnabled),
|
||||
}
|
||||
default:
|
||||
return false, errors.New("unsupported operation type")
|
||||
}
|
||||
|
||||
result, err := gfdb.DB(ctx).Model(ctx, public.TableNameWallet).
|
||||
Data(updateData).
|
||||
Where(walletEntity.WalletCol.Id, walletID).
|
||||
Where(walletEntity.WalletCol.Version, version).
|
||||
Update()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
rows, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return rows > 0, nil
|
||||
}
|
||||
|
||||
// CreateLog 创建钱包日志
|
||||
func (d *walletDao) CreateLog(ctx context.Context, req *walletDto.CreateWalletLogReq) (id int64, err error) {
|
||||
var entity *walletEntity.WalletLog
|
||||
if err = gconv.Struct(req, &entity); err != nil {
|
||||
return
|
||||
}
|
||||
r, err := gfdb.DB(ctx).Model(ctx, public.TableNameWalletLog).Data(entity).Insert()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// ListLogs 获取钱包日志列表
|
||||
func (d *walletDao) ListLogs(ctx context.Context, userID int64, page, pageSize int) (res []walletEntity.WalletLog, total int, err error) {
|
||||
r, total, err := gfdb.DB(ctx).Model(ctx, public.TableNameWalletLog).
|
||||
Where(walletEntity.WalletLogCol.UserID, userID).
|
||||
OrderDesc(walletEntity.WalletLogCol.CreatedAt).
|
||||
Page(page, pageSize).
|
||||
AllAndCount(false)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = r.Structs(&res)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user