From 911fcf9b1c3e4ac0aa8c197c475a897f300fdac1 Mon Sep 17 00:00:00 2001 From: henry Date: Fri, 3 Dec 2021 15:22:23 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E5=AE=8C=E5=96=84=E5=85=A5?= =?UTF-8?q?=E9=A9=BB=E4=BF=A1=E6=81=AF=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/init.go | 12 ++++--- app/common/model/activity_apply.go | 2 +- app/common/model/activity_instance.go | 2 +- app/common/model/common.go | 2 +- app/common/model/manage_agent.go | 2 +- app/common/model/manage_equipment.go | 2 +- app/common/model/manage_research.go | 2 +- app/common/model/service_demand.go | 2 +- app/common/model/sys_log.go | 2 +- app/common/model/sys_patent.go | 4 +-- app/common/model/technology_instance.go | 2 +- cmd/serve/serve.go | 10 ++++-- serve/orm/orm.go | 45 ++++++++++++++++++++++--- tools/init.go | 4 +-- lib/config.go => utils/load.go | 2 +- utils/load_test.go | 32 ++++++++++++++++++ 16 files changed, 100 insertions(+), 27 deletions(-) rename lib/config.go => utils/load.go (98%) create mode 100644 utils/load_test.go diff --git a/app/common/init.go b/app/common/init.go index 4c251fa..63d83ff 100644 --- a/app/common/init.go +++ b/app/common/init.go @@ -5,6 +5,7 @@ import ( "SciencesServer/app/common/model" "SciencesServer/config" "SciencesServer/serve/orm" + "SciencesServer/utils" ) type synchronized struct { @@ -25,8 +26,10 @@ func initModel() { function := func(synchronized ...*synchronized) { for _, v := range synchronized { if !db.Migrator().HasTable(v.iModel) { - _ = db.Migrator().CreateTable(v.iModel) - + err := db.Migrator().CreateTable(v.iModel) + if err != nil { + panic(err) + } if v.iValues != nil && v.iValues() != nil { db.Table(v.iModel.TableName()).Create(v.iValues()) } @@ -40,10 +43,10 @@ func initModel() { &synchronized{iModel: model.NewSysConfig()}, &synchronized{iModel: model.NewSysMenu()}, &synchronized{iModel: model.NewSysAuth()}, &synchronized{iModel: model.NewSysUser(), iValues: func() interface{} { - return &model.SysUser{Account: "admin", Name: "商挈智能", Mobile: "13888888888", Password: "123456", + return &model.SysUser{Account: "admin", Name: "商挈智能", Mobile: "13888888888", Password: utils.Md5String("123456"), IsAdmin: model.SysUserAdministratorForAdmin, Remark: "超级管理员"} }}, - &synchronized{iModel: model.NewSysUserTenant()}, + &synchronized{iModel: model.NewSysUserRole()}, &synchronized{iModel: model.NewSysUserTenant()}, &synchronized{iModel: model.NewSysDepartment()}, &synchronized{iModel: model.NewSysRole()}, &synchronized{iModel: model.NewSysRoleMenu()}, &synchronized{iModel: model.NewSysRoleAuth()}, // 配置管理 @@ -72,7 +75,6 @@ func initModel() { return out }}, // 日志管理 - &synchronized{iModel: model.NewSysUserRole()}, &synchronized{iModel: model.NewSysLog()}, &synchronized{iModel: model.NewSysUserLoginLog()}, // 用户管理 &synchronized{iModel: model.NewUserInstance()}, &synchronized{iModel: model.NewUserIdentity()}, diff --git a/app/common/model/activity_apply.go b/app/common/model/activity_apply.go index d7aed39..facc103 100644 --- a/app/common/model/activity_apply.go +++ b/app/common/model/activity_apply.go @@ -7,7 +7,7 @@ type ActivityApply struct { MUid uint64 `gorm:"column:m_uid;type:int;default:0;comment:用户manage_uuid" json:"-"` Mode ActivityInstanceMode `gorm:"column:mode;type:tinyint(1);default:1;comment:活动模式" json:"mode"` ActivityInstanceBasic - Content string `gorm:"column:title;type:text;default:'';comment:活动详情" json:"content"` + Content string `gorm:"column:title;type:text;comment:活动详情" json:"content"` MaxNumber int `gorm:"column:max_number;type:int(6);default:0;comment:报名限制人数,0不做限制" json:"max_number"` Status ActivityApplyStatus `gorm:"column:status;type:tinyint(1);default:0;comment:审核状态" json:"status"` ModelDeleted diff --git a/app/common/model/activity_instance.go b/app/common/model/activity_instance.go index ba0e6f0..2f7467e 100644 --- a/app/common/model/activity_instance.go +++ b/app/common/model/activity_instance.go @@ -9,7 +9,7 @@ type ActivityInstance struct { Identity int `gorm:"column:identity;type:tinyint(3);default:0;comment:身份来源" json:"-"` Mode ActivityInstanceMode `gorm:"column:mode;type:tinyint(1);default:1;comment:活动模式" json:"mode"` ActivityInstanceBasic - Content string `gorm:"column:title;type:text;default:'';comment:活动详情" json:"content"` + Content string `gorm:"column:title;type:text;comment:活动详情" json:"content"` MaxNumber int `gorm:"column:max_number;type:int(6);default:0;comment:报名限制人数,0不做限制" json:"max_number"` Status ActivityInstanceStatus `gorm:"column:status;type:tinyint(1);default:1;comment:活动状态(1:显示,2:隐藏)" json:"status"` ModelDeleted diff --git a/app/common/model/common.go b/app/common/model/common.go index e0336c7..09e25ac 100644 --- a/app/common/model/common.go +++ b/app/common/model/common.go @@ -39,7 +39,7 @@ func (m *Image) Analysis(domain string) string { // Images 多个图片信息 type Images struct { - Images string `gorm:"column:images;type:text;default:'';comment:图片" json:"images"` + Images string `gorm:"column:images;type:text;comment:图片" json:"images"` } // AnalysisSlice Slice解析 diff --git a/app/common/model/manage_agent.go b/app/common/model/manage_agent.go index e936d4f..188bad8 100644 --- a/app/common/model/manage_agent.go +++ b/app/common/model/manage_agent.go @@ -13,7 +13,7 @@ type ManageAgent struct { Keyword string `gorm:"column:keyword;type:varchar(255);default:'';comment:关键词" json:"-"` WorkExperience string `gorm:"column:work_experience;type:varchar(255);default:'';comment:工作经历" json:"work_experience"` WorkPlace string `gorm:"column:work_place;type:varchar(255);default:0;comment:工作地点" json:"work_place"` - IDImage string `gorm:"column:id_image;type:text;default:'';comment:身份证图片" json:"-"` + IDImage string `gorm:"column:id_image;type:text;comment:身份证图片" json:"-"` CredentialImage string `gorm:"column:credential_image;type:varchar(255);default:'';comment:资格证书" json:"credential_image"` Examine ModelDeleted diff --git a/app/common/model/manage_equipment.go b/app/common/model/manage_equipment.go index 930a6b5..8678188 100644 --- a/app/common/model/manage_equipment.go +++ b/app/common/model/manage_equipment.go @@ -12,7 +12,7 @@ type ManageEquipment struct { Title string `gorm:"column:title;type:varchar(100);default:'';comment:器材名称" json:"title"` Params string `gorm:"column:params;type:varchar(255);default:'';comment:器材参数" json:"params"` PurchaseAt time.Time `gorm:"column:purchase_at;type:datetime;default:'';comment:购买时间" json:"purchase_at"` - Description string `gorm:"column:description;type:text;default:'';comment:器材描述" json:"description"` + Description string `gorm:"column:description;type:text;comment:器材描述" json:"description"` ModelDeleted ModelAt } diff --git a/app/common/model/manage_research.go b/app/common/model/manage_research.go index c0387ff..71cde31 100644 --- a/app/common/model/manage_research.go +++ b/app/common/model/manage_research.go @@ -23,7 +23,7 @@ type ManageResearch struct { } func (m *ManageResearch) TableName() string { - return "manage_expert" + return "manage_research" } func (m *ManageResearch) GetIndustryAttribute() []string { diff --git a/app/common/model/service_demand.go b/app/common/model/service_demand.go index 2ad9917..15be18f 100644 --- a/app/common/model/service_demand.go +++ b/app/common/model/service_demand.go @@ -11,7 +11,7 @@ type ServiceDemand struct { Title string `gorm:"column:title;type:varchar(50);default:'';comment:需求名称" json:"title"` Name string `gorm:"column:name;type:varchar(50);default:'';comment:联系人" json:"name"` Mobile string `gorm:"column:mobile;type:varchar(15);default:'';comment:联系人手机号" json:"mobile"` - Description string `gorm:"column:description;type:text;default:'';comment:需求描述" json:"description"` + Description string `gorm:"column:description;type:text;comment:需求描述" json:"description"` Status ServiceDemandStatus `gorm:"column:status;type:tinyint(1);default:1;comment:状态" json:"status"` ModelDeleted ModelAt diff --git a/app/common/model/sys_log.go b/app/common/model/sys_log.go index d768b43..042411e 100644 --- a/app/common/model/sys_log.go +++ b/app/common/model/sys_log.go @@ -8,7 +8,7 @@ type SysLog struct { Name string `gorm:"column:name;type:varchar(20);default:'';comment:真实姓名" json:"name"` Method string `gorm:"column:method;type:varchar(8);default:'';comment:请求方式" json:"method"` Path string `gorm:"column:path;type:varchar(8);default:0;comment:请求地址" json:"event"` - Params string `gorm:"column:params;type:text;default:'';comment:参数信息" json:"params"` + Params string `gorm:"column:params;type:text;comment:参数信息" json:"params"` IP string `gorm:"column:ip;type:char(16);default:'';comment:IP地址" json:"ip"` ModelDeleted ModelAt diff --git a/app/common/model/sys_patent.go b/app/common/model/sys_patent.go index 71b54a9..838cf01 100644 --- a/app/common/model/sys_patent.go +++ b/app/common/model/sys_patent.go @@ -13,8 +13,8 @@ type SysPatent struct { ApplyName string `gorm:"column:apply_name;type:varchar(100);default:'';comment:申请(专利权)人" json:"apply_name"` ApplyAddress string `gorm:"column:apply_address;type:varchar(255);default:'';comment:申请人地址" json:"apply_address"` Inventor string `gorm:"column:inventor;type:varchar(100);default:'';comment:发明人" json:"inventor"` - Description string `gorm:"column:description;type:text;default:'';comment:摘要" json:"description"` - PrincipalClaim string `gorm:"column:principal_claim;type:text;default:'';comment:主权项" json:"principal_claim"` + Description string `gorm:"column:description;type:text;comment:摘要" json:"description"` + PrincipalClaim string `gorm:"column:principal_claim;type:text;comment:主权项" json:"principal_claim"` IPCCode string `gorm:"column:ipc_code;type:varchar(50);default:'';comment:IPC主分类号" json:"ipc_code"` Shelf Status SysParentStatus `gorm:"column:status;type:tinyint(1);default:1;comment:专利状态(1:授权,2:实审,3:公开)" json:"-"` diff --git a/app/common/model/technology_instance.go b/app/common/model/technology_instance.go index 19bb77d..6f88ba5 100644 --- a/app/common/model/technology_instance.go +++ b/app/common/model/technology_instance.go @@ -21,7 +21,7 @@ type TechnologyInstance struct { Territory uint64 `gorm:"column:territory;type:int(11);default:0;comment:技术领域" json:"territory"` Transaction int `gorm:"column:transaction;type:tinyint(3);default:0;comment:交易方式" json:"transaction"` Images - ProveImages string `gorm:"column:prove_images;type:text;default:'';comment:证明材料图片" json:"prove_images"` + ProveImages string `gorm:"column:prove_images;type:text;comment:证明材料图片" json:"prove_images"` Introduce string `gorm:"column:introduce;type:text;comment:介绍描述" json:"introduce"` Purpose string `gorm:"column:purpose;type:text;comment:意图-承担科研项目" json:"purpose"` Remark string `gorm:"column:remark;type:varchar(255);default:'';comment:备注信息" json:"remark"` diff --git a/cmd/serve/serve.go b/cmd/serve/serve.go index f504f61..b9d91dc 100644 --- a/cmd/serve/serve.go +++ b/cmd/serve/serve.go @@ -5,7 +5,6 @@ import ( "SciencesServer/app/common" "SciencesServer/config" "SciencesServer/cron" - "SciencesServer/lib" "SciencesServer/router" "SciencesServer/rpc/client" "SciencesServer/serve/cache" @@ -32,12 +31,12 @@ type Option struct { func (this *Serve) Run() { // 载入配置 - lib.LoadConfig(this.Option.Config, config.SettingInfo, func(i interface{}) { + utils.LoadConfig(this.Option.Config, config.SettingInfo, func(i interface{}) { obj := i.(*config.Setting) obj.Upload.Exts = strings.Split(obj.Upload.Ext, ",") logger.NewLogger().Init(&logger.Option{File: obj.Log.File, LeastDay: obj.Log.LeastDay, Level: obj.Log.Level, IsStdout: false}).Load() }) - lib.LoadConfig(this.Option.RpcConfig, config.RPCServerSettingInfo, func(i interface{}) { + utils.LoadConfig(this.Option.RpcConfig, config.RPCServerSettingInfo, func(i interface{}) { obj := i.(*config.RPCServerSetting) go utils.TryCatch(func() { options := make(map[string]*client.ServerOption, 0) @@ -56,6 +55,11 @@ func (this *Serve) Run() { orm.NewInstance( orm.WithDebug(config.SettingInfo.Engine.Debug), orm.WithDBMode(config.SettingInfo.Engine.DBMode), + orm.WithTablePrefix(config.SettingInfo.Engine.TablePrefix), + orm.WithSingularTable(!config.SettingInfo.Engine.Complex), + orm.WithMaxIdleConns(config.SettingInfo.Engine.MaxIdleConns), + orm.WithMaxOpenConns(config.SettingInfo.Engine.MaxOpenConns), + orm.WithMaxLifetime(config.SettingInfo.Engine.MaxLifetime), //orm.WithMysqlUser(config.SettingInfo.Engine.Mysql.User), ).Init() task.Init() diff --git a/serve/orm/orm.go b/serve/orm/orm.go index d09c964..b8b79fb 100644 --- a/serve/orm/orm.go +++ b/serve/orm/orm.go @@ -23,8 +23,11 @@ var ( ) type Instance struct { - debug bool - dbMode string + debug bool + dbMode string + tablePrefix string + singularTable bool + maxIdleConns, maxOpenConns, maxLifetime int *logic.Mysql *logic.Sqlite } @@ -43,14 +46,46 @@ func WithDBMode(dbMode string) Option { } } -func WithMysqlUser(user string) Option { +func WithTablePrefix(tablePrefix string) Option { + return func(instance *Instance) { + instance.tablePrefix = tablePrefix + } +} + +func WithSingularTable(singularTable bool) Option { + return func(instance *Instance) { + instance.singularTable = singularTable + } +} + +func WithMaxIdleConns(maxIdleConns int) Option { + return func(instance *Instance) { + instance.maxIdleConns = maxIdleConns + } +} + +func WithMaxOpenConns(maxOpenConns int) Option { + return func(instance *Instance) { + instance.maxOpenConns = maxOpenConns + } +} + +func WithMaxLifetime(maxLifetime int) Option { + return func(instance *Instance) { + instance.maxLifetime = maxLifetime + } +} + +func WithMysqlOption(user string) Option { return func(instance *Instance) { instance.Mysql.User = user } } -func WithMysql() { - +func WithSqliteOption(user string) Option { + return func(instance *Instance) { + instance.Mysql.User = user + } } func (this *Instance) Init() { diff --git a/tools/init.go b/tools/init.go index 1226988..d4226c3 100644 --- a/tools/init.go +++ b/tools/init.go @@ -2,8 +2,8 @@ package tools import ( "SciencesServer/config" - "SciencesServer/lib" "SciencesServer/tools/ip" + "SciencesServer/utils" ) func initIP() { @@ -11,7 +11,7 @@ func initIP() { } func initJSON() { - lib.LoadConfig("./file/area.json", &config.SettingAreaInfo) + utils.LoadConfig("./file/area.json", &config.SettingAreaInfo) } func Init() { diff --git a/lib/config.go b/utils/load.go similarity index 98% rename from lib/config.go rename to utils/load.go index 5e2ecf7..5c98c69 100644 --- a/lib/config.go +++ b/utils/load.go @@ -1,4 +1,4 @@ -package lib +package utils import ( "encoding/json" diff --git a/utils/load_test.go b/utils/load_test.go new file mode 100644 index 0000000..f60b155 --- /dev/null +++ b/utils/load_test.go @@ -0,0 +1,32 @@ +package utils + +import ( + "fmt" + "testing" +) + +type ManagePatent struct { + Kind string `json:"kind"` + Title string `json:"title"` + FileUrl string `json:"file_url"` + ApplyCode string `json:"apply_code"` + ApplyAt string `json:"apply_at"` + OpenCode string `json:"open_code"` + OpenAt string `json:"open_at"` + ApplyName string `json:"apply_name"` + ApplyAddress string `json:"apply_address"` + Inventor string `json:"inventor"` + Description string `json:"description"` + PrincipalClaim string `json:"principal_claim"` + IpcCode string `json:"ipc_code"` +} + +func TestLoadConfig(t *testing.T) { + file := "../file/manage_patent.json" + + out := make([]*ManagePatent, 0) + + LoadConfig(file, &out) + + fmt.Println(AnyToJSON(out)) +}