/
commodity.go
99 lines (84 loc) · 2.96 KB
/
commodity.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package models
import (
"github.com/jinzhu/gorm"
)
// Commodity 商品表
type Commodity struct {
ID string `gorm:"primary_key"`
Name string
Colour string
Size string
Brand string
Number uint
PresaleNumber uint
SalesVolume uint
Price float32
PurchasePrice float32
}
// CreateCommodity 创建新商品
func CreateCommodity(commodity *Commodity) error {
return db.Table("commodities").Create(commodity).Error
}
// DeleteCommodity 删除商品
func DeleteCommodity(commodityID string) error {
return db.Table("commodities").Where("id = ?", commodityID).Delete(Commodity{}).Error
}
// UpdateCommodity 更新商品信息
func UpdateCommodity(commodityID string, commodity *Commodity) error {
return db.Table("commodities").Where("id = ?", commodityID).Updates(*commodity).Error
}
// GetCommoditiesWithPurchasePrice 获取所有商品信息(包含进价)
func GetCommoditiesWithPurchasePrice() (commodities []Commodity, err error) {
err = db.Table("commodities").Find(&commodities).Error
return
}
// GetCommodities 获取所有商品信息
func GetCommodities() (commodities []Commodity, err error) {
err = db.Table("commodities").Select(
"id, name, colour, size, brand, number, presale_number, sales_volume, price").Find(&commodities).Error
return
}
// HaveCommodity 查询是否有此商品
func HaveCommodity(id string) bool {
return !db.Table("commodities").Where("id = ?", id).First(&Commodity{}).RecordNotFound()
}
// GetCommodity 获取商品信息
func GetCommodity(id string) *Commodity {
var commodity Commodity
db.Table("commodities").Where("id = ?", id).Select(
"id, name, colour, size, brand, number, presale_number, sales_volume, price").First(&commodity)
return &commodity
}
func getPresaleNumber(tx *gorm.DB, goodsID string) (number uint, err error) {
info := struct {
PresaleNumber uint
}{}
err = tx.Table("commodities").Select("presale_number").Where("id = ?", goodsID).Scan(&info).Error
number = info.PresaleNumber
return
}
func updatePresaleNumber(tx *gorm.DB, goodsID string, number uint) error {
return tx.Table("commodities").Where("id = ?", goodsID).Update("presale_number", number).Error
}
func getNumber(tx *gorm.DB, goodsID string) (number uint, err error) {
info := struct {
Number uint
}{}
err = tx.Table("commodities").Select("number").Where("id = ?", goodsID).Scan(&info).Error
number = info.Number
return
}
func updateNumber(tx *gorm.DB, goodsID string, number uint) error {
return tx.Table("commodities").Where("id = ?", goodsID).Update("number", number).Error
}
func getSalesVolume(tx *gorm.DB, goodsID string) (number uint, err error) {
info := struct {
SalesVolume uint
}{}
err = tx.Table("commodities").Select("sales_volume").Where("id = ?", goodsID).Scan(&info).Error
number = info.SalesVolume
return
}
func updateSalesVolume(tx *gorm.DB, goodsID string, number uint) error {
return tx.Table("commodities").Where("id = ?", goodsID).Update("sales_volume", number).Error
}