Go 實戰 -二-: 基金分析系統 - 通過數據模型建表

  1. 介紹

數據模型指的是和表結構一一對應的結構體,通過編寫結構體可通過遷移直接創建表結構, 後續作爲對錶的增刪改查的參數。

  1. 用戶模型

2.1 用戶表

文件:model/entity/user.go

// 用戶表
type User struct {
 global.BaseModel
 NickName string   `json:"nickName" gorm:"type:varchar(20);not null;default:'';comment:暱稱"`
 Phone    string   `json:"phone" gorm:"type:char(11);unique:un_phone;comment:手機號"`
 Password string   `json:"-" gorm:"type:varchar(40);comment:密碼"`
 Status   int      `json:"status" gorm:"size:4;default:1;comment:狀態 1:正常 2:白名單 3:黑名單"`
 UserInfo UserInfo `json:"userInfo" gorm:"-"`
 Token    string   `json:"token" gorm:"-"`
}

2.2 用戶信息表

文件:model/entity/user.go

// 用戶信息表
type UserInfo struct {
 global.BaseModel
 Uid      uint   `json:"uid" gorm:"comment:用戶id"`
 Birthday string `json:"birthday" gorm:"type:varchar(10);comment:生日"`
 Address  string `json:"address" gorm:"type:text;comment:地址"`
}
  1. 基金模型

3.1 基金基礎表

文件:model/entity/fund.go

// 基金基礎信息表
type FundBasis struct {
 global.BaseModel
 Code            string  `json:"code" gorm:"type:char(6);unique:un_code;comment:基金代碼"`
 FullName        string  `json:"fullName" gorm:"type:varchar(50);comment:基金全稱"`
 ShortName       string  `json:"shortName" gorm:"type:varchar(50);comment:基金簡稱"`
 Type            string  `json:"type" gorm:"type:varchar(50);comment:基金類型"`
 Company         string  `json:"company" gorm:"type:varchar(50);comment:基金公司"`
 ReleaseDate     string  `json:"releaseDate" gorm:"type:varchar(12);comment:發佈時間"`
 EstablishDate   string  `json:"establishDate" gorm:"type:varchar(12);comment:成立時間"`
 EstablishShares float64 `json:"establishShares" gorm:"type:decimal(12,4);comment:成立時規模(單位:億份)"`
 ManageFeeRate   float64 `json:"manageFeeRate" gorm:"type:decimal(4,2);comment:管理費率(百分比)"`
 CustodyFeeRate  float64 `json:"custodyFeeRate" gorm:"type:decimal(4,2);comment:託管費率(百分比)"`
 SaleFeeRate     float64 `json:"saleFeeRate" gorm:"type:decimal(4,2);comment:銷售服務費率(百分比)"`
 Benchmark       string `json:"benchmark" gorm:"type:varchar(255);comment:業績比較基準"`
}

3.2 基金持倉表

文件:model/entity/fund.go

// 基金股票持倉
type FundStock struct {
 global.BaseModel
 FundCode   string  `json:"fundCode" gorm:"type:varchar(10);index;comment:基金code"`
 StockCode  string  `json:"stockCode" gorm:"type:varchar(10);index;comment:股票code"`
 Percentage float64 `json:"percentage" gorm:"type:decimal(4,2);comment:持倉佔比(百分比)"`
 Quantity   float64 `json:"quantity" gorm:"type:decimal(5,2);comment:持股數(萬股)"`
 Amount     float64 `json:"amount" gorm:"type:decimal(5,2);comment:持股市值(萬元)"`
 CutOffDate string  `json:"cutOffDate" gorm:"type:char(10);comment:截止時間"`
}

3.3 基金每日排行表

文件:model/entity/fund.go

// FundDayTop 基金每日排行
type FundDayTop struct {
 global.BaseModel
 FundCode         string  `json:"fundCode" gorm:"type:varchar(10);index;comment:基金code"`
 FundName         string  `json:"fundName" gorm:"type:varchar(10);index;comment:基金名稱"`
 TopDate          string  `json:"topDate" gorm:"type:varchar(12);index;comment:排名日期"`
 NetWorth         float64 `json:"netWorth" gorm:"type:decimal(10,4);comment:單位淨值"`
 TotalWorth       float64 `json:"totalWorth" gorm:"type:decimal(10,4);comment:累計淨值"`
 DayChange        float64 `json:"dayChange" gorm:"type:decimal(10,4);comment:日增長率"`
 WeekChange       float64 `json:"weekChange" gorm:"type:decimal(10,4);comment:近一週"`
 MouthChange      float64 `json:"mouthChange" gorm:"type:decimal(10,4);comment:近一月"`
 ThreeMouthChange float64 `json:"threeMouthChange" gorm:"type:decimal(10,4);comment:近3個月"`
 SixMouthChange   float64 `json:"sixMouthChange" gorm:"type:decimal(10,4);comment:近6個月"`
 YearChange       float64 `json:"yearChange" gorm:"type:decimal(10,4);comment:近1年"`
 TwoYearChange    float64 `json:"twoYearChange" gorm:"type:decimal(10,4);comment:近2年"`
 ThreeYearChange  float64 `json:"threeYearChange" gorm:"type:decimal(10,4);comment:近3年"`
 CurrentChange    float64 `json:"CurrentChange" gorm:"type:decimal(10,4);comment:今年來"`
 CreateChange     float64 `json:"createChange" gorm:"type:decimal(10,4);comment:成立以來"`
}
  1. 股票模型

4.1 股票基礎表

文件:model/entity/stock.go

// 股票基礎信息
type Stock struct {
 global.BaseModel
 Code           string  `json:"code" gorm:"type:varchar(10);unique:un_code;comment:股票代碼"`
 Name           string  `json:"name" gorm:"type:varchar(50);comment:股票名稱"`
 Industry       string  `json:"industry" gorm:"type:varchar(20);comment:所屬行業"`
 ExchangeCode   string  `json:"exchangeCode" gorm:"type:varchar(5);comment:所屬交易所,SZ:深圳 SH:上海 HK:港股"`
 Tag            string  `json:"tag" gorm:"type:text;comment:所屬概念"`
 SetUpDate      string  `json:"setUpDate" gorm:"type:char(10);comment:公司成立時間"`
 MarketDate     string  `json:"marketDate" gorm:"type:char(10);comment:上市時間"`
 MarketPE       float64 `json:"marketPE" gorm:"type:decimal(10,2);comment:發行市盈率"`
 MarketQuantity float64 `json:"marketQuantity" gorm:"type:decimal(10,2);comment:發行量(萬股)"`
 MarketPrice    float64 `json:"marketPrice" gorm:"type:decimal(10,2);comment:發行價格"`
 MarketAmount   float64 `json:"marketAmount" gorm:"type:decimal(10,2);comment:實際募資(億)"`
 Company        string  `json:"company" gorm:"type:varchar(255);comment:公司名稱"`
 Employees      uint    `json:"employees" gorm:"comment:公司員工"`
 MoneyUnit      string  `json:"moneyUnit" gorm:"type:varchar(10);comment:貨幣單位"`
}

4.2 股票行情表

文件:model/entity/stock.go

// 股票行情
type StockQuotes struct {
 global.BaseModel
 StockCode         uint    `json:"stockCode" gorm:"index;comment:股票ID"`
 RecordDate        string  `json:"recordDate" gorm:"type:char(10);comment:記錄時間"`
 TotalQuantity     float64 `json:"totalQuantity" gorm:"type:decimal(10,2);comment:總股本(萬股)"`
 TotalAmount       float64 `json:"totalAmount" gorm:"type:decimal(10,2);comment:總市值(億元)"`
 CirculateQuantity float64 `json:"circulateQuantity" gorm:"type:decimal(10,2);comment:流通股本(萬股)"`
 CirculateAmount   float64 `json:"circulateAmount" gorm:"type:decimal(10,2);comment:流通市值(億)"`
 TodayOpenPrice    float64 `json:"todayOpenPrice" gorm:"type:decimal(10,2);comment:今日開盤價格"`
 TodayMaxPrice     float64 `json:"todayMaxPrice" gorm:"type:decimal(10,2);comment:今日最高價格"`
 TodayPrice        float64 `json:"todayPrice" gorm:"type:decimal(10,2);comment:今日收盤價格"`
 LastPrice         float64 `json:"lastPrice" gorm:"type:decimal(10,2);comment:昨日收盤價格"`
 MoneyUnit         string  `json:"moneyUnit" gorm:"type:varchar(10);comment:貨幣單位"`
 ChangeRatio       float64 `json:"changeRatio" gorm:"type:decimal(5,2);comment:換手率(百分比)"`
 DynamicPE         float64 `json:"dynamicPE" gorm:"type:decimal(10,2);comment:市盈率(動)"`
 StaticPE          float64 `json:"staticPE" gorm:"type:decimal(10,2);comment:市盈率(靜)"`
 PeTtm             float64 `json:"peTTM" gorm:"type:decimal(10,2);comment:市盈率(TTM)"`
 PB                float64 `json:"PB" gorm:"type:decimal(10,2);comment:市淨率"`
 EarningsPerShare  float64 `json:"earningsPerShare" gorm:"type:decimal(10,2);comment:每股收益"`
 NetAssetsPerShare float64 `json:"netAssetsPerShare" gorm:"type:decimal(10,2);comment:每股淨資產"`
 Dividends         float64 `json:"dividends" gorm:"type:decimal(10,2);comment:股息(TTM)"`
 DividendsRatio    float64 `json:"dividendsRatio" gorm:"type:decimal(5,2);comment:股息率(TTM)"`
}
  1. 編寫遷移代碼

5.1 添加對應的方法

文件:model/migrate/migrate.go

// 設置表信息
func setTableOption(tableComment string) *gorm.DB {
 value := fmt.Sprintf("ENGINE=InnoDB COMMENT='%s'", tableComment)
 return global.GvaMysqlClient.Set("gorm:table_options", value)
}

// 用戶相關表
func userMigrate() {
 // 用戶賬號表
 _ = setTableOption("用戶表").AutoMigrate(&entity.User{})
 // 用戶信息表
 _ = setTableOption("用戶信息表").AutoMigrate(&entity.UserInfo{})
}
// 基金錶
func fundMigrate()  {
 // 基金基礎表
 _ = setTableOption("基金錶").AutoMigrate(&entity.FundBasis{})
 // 基金持倉表
 _ = setTableOption("基金持倉表").AutoMigrate(&entity.FundStock{})
  // 基金每日排行  
  _ = setTableOption("基金每日排行").AutoMigrate(&entity.FundDayTop{})  
}
// 股票相關表
func stockMigrate()  {
 // 股票基礎表
 _ = setTableOption("股票表").AutoMigrate(&entity.Stock{})
 // 股票行情表
 _ = setTableOption("股票行情表").AutoMigrate(&entity.StockQuotes{})
}

5.2 修改遷移入口函數

文件:model/migrate/migrate.go

// 數據表遷移
func AutoMigrate() {
 // 用戶相關表
 userMigrate()
 // 基金相關表
 fundMigrate()
 // 股票相關表
 stockMigrate()
}
  1. 啓動項目

6.1 修改配置

文件:./config.yaml

...
mysql:
  enable: true
  ....
  database: fund_analye_system # 庫需要手動創建
  autoMigrate: true # 開啓時,每次服務啓動都會根據實體創建/更新表結構
  ...
  gorm: 
    tablePrefix: "fas_" #設置表前綴
    ...

6.2 自動創建表

當在配置中, 打開autoMigrate=true時,成功啓動項目後,會創建以下表信息。

i

  1. 表關係圖

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/Ax8B2t-lsHGKiyxRuKTFzw