1 Star 1 Fork 1

xiaoyutab / xgotool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
init.go 4.69 KB
一键复制 编辑 原始数据 按行查看 历史
xiaoyutab 提交于 2024-04-17 10:21 . xlog结构调整
// 基础数据统计模块
//
// 用于统计个人/家庭等的基础数据记录,用于明细分析
package xrecord
import "gorm.io/gorm"
type Config struct {
DB *gorm.DB // 数据库连接
RecordTable string // 基础数据记录表
RecordTypeTable string // 基础数据分类配置表
RecordStatisticsTable string // 基础数据统计配置表
}
var _default Config = Config{
RecordTable: "record",
RecordTypeTable: "record_type",
RecordStatisticsTable: "record_statistics",
}
// 注入配置项
//
// c 程序中需要使用的配置项信息
func Regedit(c *Config) {
if c == nil {
return
}
if c.DB != nil {
_default.DB = c.DB
}
if c.RecordTable != "" {
_default.RecordTable = c.RecordTable
}
if c.RecordTypeTable != "" {
_default.RecordTypeTable = c.RecordTypeTable
}
if c.RecordStatisticsTable != "" {
_default.RecordStatisticsTable = c.RecordStatisticsTable
}
if _default.DB != nil {
_default.DB.AutoMigrate(&Record{}, &RecordType{}, &RecordStatistics{})
}
}
// 基础数据记录表
type Record struct {
Id uint `gorm:"column:id;primaryKey;autoIncrement;not null" form:"id" json:"id"`
UserId uint `gorm:"column:user_id;comment:所属用户ID" form:"user_id" json:"user_id"` //所属用户ID
Value float64 `gorm:"column:value;type:decimal(12,4);comment:数据值记录" form:"value" json:"value"` //数据值记录
Type uint `gorm:"column:type;comment:所属数据类型" form:"type" json:"type"` //所属数据类型
Relevance uint `gorm:"column:relevance;comment:关联ID" form:"relevance" json:"relevance"` //关联ID
CreatedAt string `gorm:"column:created_at;type:datetime;comment:添加时间" form:"created_at" json:"created_at"` //添加时间
}
// 返回所属表名信息
func (c *Record) TableName() string {
return _default.RecordTable
}
// 基础数据类型配置表
type RecordType struct {
Id uint `gorm:"column:id;primaryKey;autoIncrement;not null" form:"id" json:"id"`
UserId uint `gorm:"column:user_id;comment:创建人ID" form:"user_id" json:"user_id"` //创建人ID
Name string `gorm:"column:name;type:varchar(200);comment:分类名称" form:"name" json:"name"` //分类名称
NameDy string `gorm:"column:name_dy;type:varchar(100);comment:分类数据单位" form:"name_dy" json:"name_dy"` //分类数据单位
HasMax uint8 `gorm:"column:has_max;type:tinyint unsigned;comment:是否统计最大值(每天)" form:"has_max" json:"has_max"` //是否统计最大值(每天)
HasMin uint8 `gorm:"column:has_min;type:tinyint unsigned;comment:是否统计最小值(每天)" form:"has_min" json:"has_min"` //是否统计最小值(每天)
HasAverage uint8 `gorm:"column:has_average;type:tinyint unsigned;comment:是否统计平均值(每天)" form:"has_average" json:"has_average"` //是否统计平均值(每天)
NextZero uint8 `gorm:"column:next_zero;type:tinyint unsigned;comment:是否空数据归零(当天无数据时方案 0-使用前一天数据 1-使用0值)" form:"next_zero" json:"next_zero"` //是否空数据归零(当天无数据时方案 0-使用前一天数据 1-使用0值)
CreatedAt string `gorm:"column:created_at;type:datetime;comment:创建时间" form:"created_at" json:"created_at"` //创建时间
}
// 返回所属表名信息
func (c *RecordType) TableName() string {
return _default.RecordTypeTable
}
// 基础数据统计表
type RecordStatistics struct {
Id uint `gorm:"column:id;primaryKey;autoIncrement;not null" form:"id" json:"id"`
RecordDate string `gorm:"column:record_date;type:date;comment:统计时间" form:"record_date" json:"record_date"` //统计时间
RecordType uint `gorm:"column:record_type;comment:统计数据类型" form:"record_type" json:"record_type"` //统计数据类型
Type uint8 `gorm:"column:type;type:tinyint unsigned;comment:数据类型 1-最大值 2-最小值 3-平均值" form:"type" json:"type"` //数据类型 1-最大值 2-最小值 3-平均值
Value float64 `gorm:"column:value;type:decimal(12,4);comment:数据值" form:"value" json:"value"` //数据值
CreatedAt string `gorm:"column:created_at;type:datetime;comment:计算时间" form:"created_at" json:"created_at"` //计算时间
}
// 返回所属表名信息
func (c *RecordStatistics) TableName() string {
return _default.RecordStatisticsTable
}
Go
1
https://gitee.com/xiaoyutab/xgotool.git
git@gitee.com:xiaoyutab/xgotool.git
xiaoyutab
xgotool
xgotool
v0.3.13

搜索帮助