ai work
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package psql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"kurumibot/database"
|
||||
@@ -8,78 +9,146 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gorm"
|
||||
"github.com/vinovest/sqlx"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID int
|
||||
Balance decimal.Decimal
|
||||
Name string
|
||||
GroupID int `gorm:"default:1"`
|
||||
Group *Group
|
||||
Level int `gorm:"default:1"`
|
||||
Exp int `gorm:"default:0"`
|
||||
WorkID int `gorm:"default:1"`
|
||||
Work *Work
|
||||
WorkTime time.Time
|
||||
GroupID int `db:"group_id"`
|
||||
Level int
|
||||
Exp int
|
||||
WorkID int `db:"work_id"`
|
||||
WorkTime time.Time `db:"work_time"`
|
||||
|
||||
AutoID sql.NullInt64
|
||||
Auto *ShopAuto
|
||||
BusinessID sql.NullInt64
|
||||
Business *ShopBusiness
|
||||
MaidID sql.NullInt64
|
||||
Maid *ShopMaid
|
||||
MinerID sql.NullInt64
|
||||
Miner *ShopMiner
|
||||
AutoID sql.NullInt32 `db:"auto_id"`
|
||||
BusinessID sql.NullInt32 `db:"business_id"`
|
||||
MaidID sql.NullInt32 `db:"maid_id"`
|
||||
MinerID sql.NullInt32 `db:"miner_id"`
|
||||
|
||||
IncomeTime time.Time
|
||||
IncomeTime time.Time `db:"income_time"`
|
||||
BTC decimal.Decimal
|
||||
Invested decimal.Decimal
|
||||
PairID sql.NullInt64
|
||||
Pair *User
|
||||
Greeting string `gorm:"size:255,default:Привет"`
|
||||
Donat int `gorm:"default:0"`
|
||||
FractionID sql.NullInt64
|
||||
Fraction *Fraction
|
||||
PairID sql.NullInt64 `db:"pair_id"`
|
||||
Greeting string
|
||||
Donat int
|
||||
FractionID sql.NullInt32 `db:"fraction_id"`
|
||||
|
||||
MoneyIncome decimal.Decimal
|
||||
ExpIncome int
|
||||
BtcIncome decimal.Decimal
|
||||
MoneyIncome decimal.Decimal `db:"money_income"`
|
||||
ExpIncome int `db:"exp_income"`
|
||||
BtcIncome decimal.Decimal `db:"btc_income"`
|
||||
|
||||
WaifuSearchTime time.Time
|
||||
WaifuSearchTime time.Time `db:"waifu_search_time"`
|
||||
|
||||
Group *Group
|
||||
Work *Work
|
||||
Auto *ShopAuto
|
||||
Business *ShopBusiness
|
||||
Maid *ShopMaid
|
||||
Miner *ShopMiner
|
||||
Pair *User
|
||||
Fraction *Fraction
|
||||
}
|
||||
|
||||
func GetOrCreateUser(tgId int, name string) (*User, error) {
|
||||
user, err := GetUser(tgId)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
_, err = CreateUser(tgId, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return GetUser(tgId)
|
||||
user, err = GetUser(tgId)
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func CreateUser(id int, name string) (*User, error) {
|
||||
user := &User{
|
||||
ID: id,
|
||||
Name: name,
|
||||
}
|
||||
tx := database.PostgresDatabase.Create(user)
|
||||
return user, tx.Error
|
||||
user := new(User)
|
||||
err := database.PostgresDatabase.Get(user, "INSERT INTO users (id, name) VALUES (?, ?) RETURNING *;", id, name)
|
||||
return user, err
|
||||
}
|
||||
|
||||
func GetUser(telegramId int) (*User, error) {
|
||||
user := new(User)
|
||||
tx := database.PostgresDatabase.Joins("Group").Joins("Work").Joins("Auto").Joins("Business").Joins("Maid").Joins("Miner").Joins("Fraction").Preload("Pair").Take(user, "users.id=?", telegramId)
|
||||
return user, tx.Error
|
||||
err := sqlx.Transact(database.PostgresDatabase, func(ctx context.Context, db sqlx.Queryable) error {
|
||||
err := database.PostgresDatabase.Get(user, "SELECT * FROM users WHERE id=$1;", telegramId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
user.Group = new(Group)
|
||||
err = database.PostgresDatabase.Get(user.Group, "SELECT * FROM groups WHERE id=$1;", user.GroupID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
user.Work = new(Work)
|
||||
err = database.PostgresDatabase.Get(user.Work, "SELECT * FROM works WHERE id=$1;", user.WorkID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
shopRep := NewShopRepository(database.PostgresDatabase)
|
||||
if user.AutoID.Valid {
|
||||
user.Auto, err = shopRep.GetAuto(user.AutoID.Int32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if user.BusinessID.Valid {
|
||||
user.Business, err = shopRep.GetBusiness(user.BusinessID.Int32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if user.MaidID.Valid {
|
||||
user.Maid, err = shopRep.GetMaid(user.MaidID.Int32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if user.MinerID.Valid {
|
||||
user.Miner, err = shopRep.GetMiner(user.MinerID.Int32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if user.PairID.Valid {
|
||||
user.Pair, err = GetUser(int(user.PairID.Int64))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if user.FractionID.Valid {
|
||||
fractionRep := NewFractionRepository(database.PostgresDatabase)
|
||||
user.Fraction, err = fractionRep.GetFraction(user.FractionID.Int32)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return user, err
|
||||
}
|
||||
|
||||
func UpdateUser(user *User) (*User, error) {
|
||||
_, err := database.PostgresDatabase.NamedExec(
|
||||
`UPDATE users SET balance=:balance, name=:name, group_id=:group_id, level=:level, exp=:exp,
|
||||
work_id=:work_id, work_time=:work_time, auto_id=:auto_id, business_id=:business_id,
|
||||
maid_id=:maid_id, miner_id=:miner_id, income_time=:income_time, btc=:btc, invested=:invested,
|
||||
pair_id=:pair_id, greeting=:greeting, donat=:donat, fraction_id=:fraction_id,
|
||||
money_income=:money_income, exp_income=:exp_income, btc_income=:btc_income,
|
||||
waifu_search_time=:waifu_search_time
|
||||
WHERE id=:id;`,
|
||||
user,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return GetUser(user.ID)
|
||||
}
|
||||
|
||||
func GetAllUsers() ([]*User, error) {
|
||||
users := make([]*User, 0)
|
||||
tx := database.PostgresDatabase.Joins("Group").Joins("Work").Joins("Auto").Joins("Business").Joins("Maid").Joins("Miner").Joins("Fraction").Preload("Pair").Find(&users)
|
||||
return users, tx.Error
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func CountLevel(userXp int) (int, int) {
|
||||
|
||||
Reference in New Issue
Block a user