init: 后端框架脚手架 (Go + Gin + GORM + MySQL)
- 项目目录结构:backend/ deploy/ schema/ migrations/ - 数据库 Schema:所有建表 SQL,含 hotel_id 多租户隔离 - Go 后端:config、model、handler、service、middleware、router - 认证:账号密码登录 + JWT(Access + Refresh Token) - 许可证:HMAC-SHA256 激活码生成 + 设备绑定验证 - 业务模块:商品、仓库、往来单位、入库、出库、库存、盘点 - 库存事务:入库/出库审核时原子更新库存 + 流水记录 - 数据导入:Excel/CSV 批量导入商品、往来单位 - Docker Compose:本地 MySQL + Adminer Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// JSON 类型,用于 custom_fields
|
||||
type JSON map[string]interface{}
|
||||
|
||||
func (j JSON) Value() (driver.Value, error) {
|
||||
if j == nil {
|
||||
return nil, nil
|
||||
}
|
||||
b, err := json.Marshal(j)
|
||||
return string(b), err
|
||||
}
|
||||
|
||||
func (j *JSON) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
*j = nil
|
||||
return nil
|
||||
}
|
||||
var bytes []byte
|
||||
switch v := value.(type) {
|
||||
case string:
|
||||
bytes = []byte(v)
|
||||
case []byte:
|
||||
bytes = v
|
||||
default:
|
||||
return fmt.Errorf("cannot scan type %T into JSON", value)
|
||||
}
|
||||
return json.Unmarshal(bytes, j)
|
||||
}
|
||||
|
||||
// Base 公共字段
|
||||
type Base struct {
|
||||
ID uint64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
DeletedAt *time.Time `gorm:"index" json:"-"`
|
||||
}
|
||||
|
||||
// TenantBase 含租户隔离的公共字段
|
||||
type TenantBase struct {
|
||||
Base
|
||||
HotelID uint64 `gorm:"not null;index" json:"hotel_id"`
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
type FinanceRecord struct {
|
||||
Base
|
||||
HotelID uint64 `gorm:"not null;index" json:"hotel_id"`
|
||||
PartnerID *uint64 `json:"partner_id"`
|
||||
Type string `gorm:"type:enum('receivable','payable','receipt','payment')" json:"type"`
|
||||
Amount float64 `gorm:"type:decimal(14,2)" json:"amount"`
|
||||
Balance float64 `gorm:"type:decimal(14,2)" json:"balance"`
|
||||
RefType string `gorm:"size:30" json:"ref_type"`
|
||||
RefID *uint64 `json:"ref_id"`
|
||||
OperatorID uint64 `gorm:"not null" json:"operator_id"`
|
||||
RecordDate time.Time `gorm:"type:date" json:"record_date"`
|
||||
CustomFields JSON `gorm:"type:json" json:"custom_fields,omitempty"`
|
||||
Remark string `gorm:"size:500" json:"remark"`
|
||||
|
||||
Partner *Partner `gorm:"foreignKey:PartnerID" json:"partner,omitempty"`
|
||||
}
|
||||
|
||||
type NumberRule struct {
|
||||
ID uint64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
HotelID uint64 `gorm:"not null;uniqueIndex:uk_hotel_type" json:"hotel_id"`
|
||||
Type string `gorm:"size:30;uniqueIndex:uk_hotel_type" json:"type"`
|
||||
Prefix string `gorm:"size:20;default:''" json:"prefix"`
|
||||
CurrentNo int `gorm:"default:0" json:"current_no"`
|
||||
DateFormat string `gorm:"size:20;default:'YYYYMMDD'" json:"date_format"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package model
|
||||
|
||||
type Hotel struct {
|
||||
Base
|
||||
Name string `gorm:"size:100;not null" json:"name"`
|
||||
Code string `gorm:"size:50;uniqueIndex" json:"code"`
|
||||
Address string `gorm:"size:255" json:"address"`
|
||||
Phone string `gorm:"size:30" json:"phone"`
|
||||
CustomFields JSON `gorm:"type:json" json:"custom_fields,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
type License struct {
|
||||
Base
|
||||
HotelID uint64 `gorm:"not null;index" json:"hotel_id"`
|
||||
LicenseKey string `gorm:"size:255;uniqueIndex" json:"license_key"`
|
||||
DeviceID string `gorm:"size:255" json:"device_id"`
|
||||
Type string `gorm:"type:enum('trial','annual','lifetime');default:'trial'" json:"type"`
|
||||
ExpiresAt *time.Time `json:"expires_at"`
|
||||
IsActive bool `gorm:"default:true" json:"is_active"`
|
||||
Features JSON `gorm:"type:json" json:"features,omitempty"`
|
||||
ActivatedAt *time.Time `json:"activated_at"`
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package model
|
||||
|
||||
type Partner struct {
|
||||
TenantBase
|
||||
Code string `gorm:"size:50" json:"code"`
|
||||
Name string `gorm:"size:200;not null" json:"name"`
|
||||
Type string `gorm:"type:set('supplier','customer');default:'supplier'" json:"type"`
|
||||
Contact string `gorm:"size:50" json:"contact"`
|
||||
Phone string `gorm:"size:30" json:"phone"`
|
||||
Address string `gorm:"size:255" json:"address"`
|
||||
BankAccount string `gorm:"size:100" json:"bank_account"`
|
||||
CreditLimit float64 `gorm:"type:decimal(12,2)" json:"credit_limit"`
|
||||
Balance float64 `gorm:"type:decimal(12,2);default:0" json:"balance"`
|
||||
CustomFields JSON `gorm:"type:json" json:"custom_fields,omitempty"`
|
||||
Remark string `gorm:"size:500" json:"remark"`
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package model
|
||||
|
||||
type ProductCategory struct {
|
||||
TenantBase
|
||||
Name string `gorm:"size:100;not null" json:"name"`
|
||||
ParentID *uint64 `json:"parent_id"`
|
||||
SortOrder int `gorm:"default:0" json:"sort_order"`
|
||||
}
|
||||
|
||||
type Product struct {
|
||||
TenantBase
|
||||
Code string `gorm:"size:50" json:"code"`
|
||||
Barcode string `gorm:"size:100" json:"barcode"`
|
||||
Name string `gorm:"size:200;not null" json:"name"`
|
||||
Series string `gorm:"size:100" json:"series"`
|
||||
Spec string `gorm:"size:100" json:"spec"`
|
||||
Unit string `gorm:"size:20" json:"unit"`
|
||||
CategoryID *uint64 `json:"category_id"`
|
||||
Brand string `gorm:"size:100" json:"brand"`
|
||||
PurchasePrice float64 `gorm:"type:decimal(12,2)" json:"purchase_price"`
|
||||
SalePrice float64 `gorm:"type:decimal(12,2)" json:"sale_price"`
|
||||
MinStock int `gorm:"default:0" json:"min_stock"`
|
||||
CustomFields JSON `gorm:"type:json" json:"custom_fields,omitempty"`
|
||||
Remark string `gorm:"size:500" json:"remark"`
|
||||
|
||||
Category *ProductCategory `gorm:"foreignKey:CategoryID" json:"category,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// -------- 入库单 --------
|
||||
|
||||
type StockInOrder struct {
|
||||
TenantBase
|
||||
OrderNo string `gorm:"size:50;uniqueIndex:uk_hotel_order_no" json:"order_no"`
|
||||
Type string `gorm:"size:30;default:'purchase'" json:"type"`
|
||||
WarehouseID uint64 `gorm:"not null" json:"warehouse_id"`
|
||||
PartnerID *uint64 `json:"partner_id"`
|
||||
OperatorID uint64 `gorm:"not null" json:"operator_id"`
|
||||
ReviewerID *uint64 `json:"reviewer_id"`
|
||||
Status string `gorm:"type:enum('draft','pending','approved','rejected');default:'draft'" json:"status"`
|
||||
OrderDate time.Time `gorm:"type:date" json:"order_date"`
|
||||
TotalAmount float64 `gorm:"type:decimal(14,2);default:0" json:"total_amount"`
|
||||
ReviewedAt *time.Time `json:"reviewed_at"`
|
||||
CustomFields JSON `gorm:"type:json" json:"custom_fields,omitempty"`
|
||||
Remark string `gorm:"size:500" json:"remark"`
|
||||
|
||||
Items []StockInItem `gorm:"foreignKey:OrderID" json:"items,omitempty"`
|
||||
Warehouse *Warehouse `gorm:"foreignKey:WarehouseID" json:"warehouse,omitempty"`
|
||||
Partner *Partner `gorm:"foreignKey:PartnerID" json:"partner,omitempty"`
|
||||
Operator *User `gorm:"foreignKey:OperatorID" json:"operator,omitempty"`
|
||||
}
|
||||
|
||||
type StockInItem struct {
|
||||
Base
|
||||
OrderID uint64 `gorm:"not null;index" json:"order_id"`
|
||||
HotelID uint64 `gorm:"not null" json:"hotel_id"`
|
||||
ProductID uint64 `gorm:"not null" json:"product_id"`
|
||||
Quantity float64 `gorm:"type:decimal(12,3);not null" json:"quantity"`
|
||||
UnitPrice float64 `gorm:"type:decimal(12,2);default:0" json:"unit_price"`
|
||||
TotalPrice float64 `gorm:"type:decimal(14,2);default:0" json:"total_price"`
|
||||
BatchNo string `gorm:"size:50" json:"batch_no"`
|
||||
CustomFields JSON `gorm:"type:json" json:"custom_fields,omitempty"`
|
||||
Remark string `gorm:"size:255" json:"remark"`
|
||||
|
||||
Product *Product `gorm:"foreignKey:ProductID" json:"product,omitempty"`
|
||||
}
|
||||
|
||||
// -------- 出库单 --------
|
||||
|
||||
type StockOutOrder struct {
|
||||
TenantBase
|
||||
OrderNo string `gorm:"size:50;uniqueIndex:uk_hotel_order_no" json:"order_no"`
|
||||
Type string `gorm:"size:30;default:'sale'" json:"type"`
|
||||
WarehouseID uint64 `gorm:"not null" json:"warehouse_id"`
|
||||
PartnerID *uint64 `json:"partner_id"`
|
||||
OperatorID uint64 `gorm:"not null" json:"operator_id"`
|
||||
ReviewerID *uint64 `json:"reviewer_id"`
|
||||
Status string `gorm:"type:enum('draft','pending','approved','rejected');default:'draft'" json:"status"`
|
||||
OrderDate time.Time `gorm:"type:date" json:"order_date"`
|
||||
TotalAmount float64 `gorm:"type:decimal(14,2);default:0" json:"total_amount"`
|
||||
ReviewedAt *time.Time `json:"reviewed_at"`
|
||||
CustomFields JSON `gorm:"type:json" json:"custom_fields,omitempty"`
|
||||
Remark string `gorm:"size:500" json:"remark"`
|
||||
|
||||
Items []StockOutItem `gorm:"foreignKey:OrderID" json:"items,omitempty"`
|
||||
Warehouse *Warehouse `gorm:"foreignKey:WarehouseID" json:"warehouse,omitempty"`
|
||||
Partner *Partner `gorm:"foreignKey:PartnerID" json:"partner,omitempty"`
|
||||
Operator *User `gorm:"foreignKey:OperatorID" json:"operator,omitempty"`
|
||||
}
|
||||
|
||||
type StockOutItem struct {
|
||||
Base
|
||||
OrderID uint64 `gorm:"not null;index" json:"order_id"`
|
||||
HotelID uint64 `gorm:"not null" json:"hotel_id"`
|
||||
ProductID uint64 `gorm:"not null" json:"product_id"`
|
||||
Quantity float64 `gorm:"type:decimal(12,3);not null" json:"quantity"`
|
||||
UnitPrice float64 `gorm:"type:decimal(12,2);default:0" json:"unit_price"`
|
||||
TotalPrice float64 `gorm:"type:decimal(14,2);default:0" json:"total_price"`
|
||||
CustomFields JSON `gorm:"type:json" json:"custom_fields,omitempty"`
|
||||
Remark string `gorm:"size:255" json:"remark"`
|
||||
|
||||
Product *Product `gorm:"foreignKey:ProductID" json:"product,omitempty"`
|
||||
}
|
||||
|
||||
// -------- 实时库存 --------
|
||||
|
||||
type Inventory struct {
|
||||
ID uint64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
HotelID uint64 `gorm:"not null;uniqueIndex:uk_hotel_wh_product" json:"hotel_id"`
|
||||
WarehouseID uint64 `gorm:"not null;uniqueIndex:uk_hotel_wh_product" json:"warehouse_id"`
|
||||
ProductID uint64 `gorm:"not null;uniqueIndex:uk_hotel_wh_product" json:"product_id"`
|
||||
Quantity float64 `gorm:"type:decimal(12,3);default:0" json:"quantity"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
Product *Product `gorm:"foreignKey:ProductID" json:"product,omitempty"`
|
||||
Warehouse *Warehouse `gorm:"foreignKey:WarehouseID" json:"warehouse,omitempty"`
|
||||
}
|
||||
|
||||
// -------- 库存流水 --------
|
||||
|
||||
type InventoryLog struct {
|
||||
ID uint64 `gorm:"primaryKey;autoIncrement" json:"id"`
|
||||
HotelID uint64 `gorm:"not null;index:idx_hotel_product" json:"hotel_id"`
|
||||
WarehouseID uint64 `gorm:"not null" json:"warehouse_id"`
|
||||
ProductID uint64 `gorm:"not null;index:idx_hotel_product" json:"product_id"`
|
||||
Direction string `gorm:"type:enum('in','out')" json:"direction"`
|
||||
Quantity float64 `gorm:"type:decimal(12,3)" json:"quantity"`
|
||||
QtyBefore float64 `gorm:"type:decimal(12,3)" json:"qty_before"`
|
||||
QtyAfter float64 `gorm:"type:decimal(12,3)" json:"qty_after"`
|
||||
RefType string `gorm:"size:30" json:"ref_type"`
|
||||
RefID uint64 `json:"ref_id"`
|
||||
OperatorID *uint64 `json:"operator_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// -------- 库存盘点 --------
|
||||
|
||||
type InventoryCheck struct {
|
||||
TenantBase
|
||||
CheckNo string `gorm:"size:50;uniqueIndex:uk_hotel_check_no" json:"check_no"`
|
||||
WarehouseID uint64 `gorm:"not null" json:"warehouse_id"`
|
||||
OperatorID uint64 `gorm:"not null" json:"operator_id"`
|
||||
Status string `gorm:"type:enum('draft','completed');default:'draft'" json:"status"`
|
||||
CheckDate time.Time `gorm:"type:date" json:"check_date"`
|
||||
Remark string `gorm:"size:500" json:"remark"`
|
||||
|
||||
Items []InventoryCheckItem `gorm:"foreignKey:CheckID" json:"items,omitempty"`
|
||||
}
|
||||
|
||||
type InventoryCheckItem struct {
|
||||
Base
|
||||
CheckID uint64 `gorm:"not null;index" json:"check_id"`
|
||||
HotelID uint64 `gorm:"not null" json:"hotel_id"`
|
||||
ProductID uint64 `gorm:"not null" json:"product_id"`
|
||||
SystemQty float64 `gorm:"type:decimal(12,3)" json:"system_qty"`
|
||||
ActualQty float64 `gorm:"type:decimal(12,3)" json:"actual_qty"`
|
||||
DiffQty float64 `gorm:"->" json:"diff_qty"` // generated column,只读
|
||||
Remark string `gorm:"size:255" json:"remark"`
|
||||
|
||||
Product *Product `gorm:"foreignKey:ProductID" json:"product,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package model
|
||||
|
||||
type User struct {
|
||||
TenantBase
|
||||
Username string `gorm:"size:50;uniqueIndex:uk_hotel_username" json:"username"`
|
||||
PasswordHash string `gorm:"size:255" json:"-"`
|
||||
RealName string `gorm:"size:50" json:"real_name"`
|
||||
Phone string `gorm:"size:30" json:"phone"`
|
||||
Role string `gorm:"type:enum('admin','operator');default:'operator'" json:"role"`
|
||||
IsActive bool `gorm:"default:true" json:"is_active"`
|
||||
CustomFields JSON `gorm:"type:json" json:"custom_fields,omitempty"`
|
||||
}
|
||||
|
||||
type OAuthProvider struct {
|
||||
Base
|
||||
UserID uint64 `gorm:"not null;index" json:"user_id"`
|
||||
Provider string `gorm:"type:enum('wechat','google','apple')" json:"provider"`
|
||||
ProviderUserID string `gorm:"size:255" json:"provider_user_id"`
|
||||
AccessToken string `gorm:"type:text" json:"-"`
|
||||
RefreshToken string `gorm:"type:text" json:"-"`
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package model
|
||||
|
||||
type Warehouse struct {
|
||||
TenantBase
|
||||
Name string `gorm:"size:100;not null" json:"name"`
|
||||
Location string `gorm:"size:200" json:"location"`
|
||||
IsDefault bool `gorm:"default:false" json:"is_default"`
|
||||
CustomFields JSON `gorm:"type:json" json:"custom_fields,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user