film/model/orders.go

77 lines
1.6 KiB
Go
Raw Normal View History

2023-04-06 13:46:19 +08:00
package model
import (
2024-04-27 23:12:44 +08:00
"film/global"
2023-04-09 11:38:19 +08:00
"fmt"
"github.com/smbrave/goutil"
2023-04-06 13:46:19 +08:00
"gorm.io/gorm"
"time"
)
var (
tableNameOrder = "orders"
)
type Order struct {
2023-04-09 10:40:20 +08:00
Id int64
2023-04-09 12:21:11 +08:00
OrderNo string
2023-04-09 10:40:20 +08:00
StartTime string
StartDate string
TotalPrice int64
PayPrice int64
MinPrice int64
MaxPrice int64
MaoyanPrice int64
SeatNum int
Seats string
ShowTime int64
PayTime int64
OrderCreateTime int64
CityName string
ProvinceName string
Ting string
CinemaName string
2023-05-16 09:42:30 +08:00
CinemaBrand string
2023-04-09 10:40:20 +08:00
MovieName string
Address string
Count int
IsSeat int
LoverSeat int
CreateDate string
CreateTime int64
UpdateTime int64
2023-04-06 13:46:19 +08:00
}
2023-04-09 11:38:19 +08:00
func (o *Order) String() string {
return fmt.Sprintf("{%s|%d} city={%s} cinema={%s} film={%s} ting={%s} show={%s}",
2023-04-09 12:22:31 +08:00
o.OrderNo, o.Id, o.CityName, o.CinemaName, o.MovieName, o.Ting, goutil.TimeToDateTime(o.ShowTime))
2023-04-09 11:38:19 +08:00
}
2023-04-06 13:46:19 +08:00
func AddOrder(o *Order) (int64, error) {
o.CreateTime = time.Now().Unix()
2023-04-06 13:57:39 +08:00
o.CreateDate = time.Now().Format("2006-01-02")
2024-04-27 23:12:44 +08:00
res := global.GetDB().Table(tableNameOrder).Create(o)
2023-04-06 13:46:19 +08:00
return o.Id, res.Error
}
func GetOrder(id int64) (*Order, error) {
var u Order
2024-04-27 23:12:44 +08:00
tx := global.GetDB().Table(tableNameOrder)
2023-04-06 13:46:19 +08:00
tx = tx.Where("id = ?", id)
res := tx.First(&u)
if res.Error == gorm.ErrRecordNotFound {
return nil, nil
}
if res.Error != nil {
return nil, res.Error
}
return &u, nil
}
func UpdateOrder(o *Order) error {
o.UpdateTime = time.Now().Unix()
2024-04-27 23:12:44 +08:00
tx := global.GetDB().Table(tableNameOrder)
2023-04-06 13:46:19 +08:00
res := tx.Save(o)
return res.Error
}