From e9f3d0ac62d896032ca96f65fe5e0f562418f0f0 Mon Sep 17 00:00:00 2001 From: injoyai <1113655791@qq.com> Date: Fri, 21 Feb 2025 14:27:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8B=93=E5=B1=95,=E5=B0=81?= =?UTF-8?q?=E8=A3=85=E6=AF=8F=E6=97=A5=E6=9B=B4=E6=96=B0=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E5=92=8C=E5=B7=A5=E4=BD=9C=E6=97=A5=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes.go | 24 ++++++++++++------------ workday.go | 20 +------------------- 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/codes.go b/codes.go index c6ac18a..047a2c0 100644 --- a/codes.go +++ b/codes.go @@ -136,7 +136,7 @@ func (this *Codes) Code(byDatabase bool) ([]*CodeModel, error) { } //4. 插入或者更新数据库 - err := this.SessionFunc(func(session *xorm.Session) error { + err := NewSessionFunc(this.db, func(session *xorm.Session) error { for _, v := range insert { if _, err := session.Insert(v); err != nil { return err @@ -157,8 +157,17 @@ func (this *Codes) Code(byDatabase bool) ([]*CodeModel, error) { } -func (this *Codes) SessionFunc(fn func(session *xorm.Session) error) error { - session := this.db.NewSession() +type CodeModel struct { + ID int64 `json:"id"` //主键 + Name string `json:"name"` //名称,有时候名称会变,例STxxx + Code string `json:"code" xorm:"index"` //代码 + Exchange string `json:"exchange" xorm:"index"` //交易所 + EditDate int64 `json:"editDate" xorm:"updated"` //修改时间 + InDate int64 `json:"inDate" xorm:"created"` //创建时间 +} + +func NewSessionFunc(db *xorm.Engine, fn func(session *xorm.Session) error) error { + session := db.NewSession() defer session.Close() if err := session.Begin(); err != nil { session.Rollback() @@ -174,12 +183,3 @@ func (this *Codes) SessionFunc(fn func(session *xorm.Session) error) error { } return nil } - -type CodeModel struct { - ID int64 `json:"id"` //主键 - Name string `json:"name"` //名称,有时候名称会变,例STxxx - Code string `json:"code" xorm:"index"` //代码 - Exchange string `json:"exchange" xorm:"index"` //交易所 - EditDate int64 `json:"editDate" xorm:"updated"` //修改时间 - InDate int64 `json:"inDate" xorm:"created"` //创建时间 -} diff --git a/workday.go b/workday.go index a4d7ccb..b75ee1f 100644 --- a/workday.go +++ b/workday.go @@ -81,7 +81,7 @@ func (this *Workday) Update() error { return err } - this.SessionFunc(func(session *xorm.Session) error { + return NewSessionFunc(this.db, func(session *xorm.Session) error { for _, v := range resp.List { if unix := v.Time.Unix(); unix > lastWorkday.Unix { _, err = session.Insert(&WorkdayModel{Unix: unix, Date: v.Time.Format("20060102"), Is: true}) @@ -108,24 +108,6 @@ func (this *Workday) TodayIs() bool { return this.Is(time.Now()) } -func (this *Workday) SessionFunc(fn func(session *xorm.Session) error) error { - session := this.db.NewSession() - defer session.Close() - if err := session.Begin(); err != nil { - session.Rollback() - return err - } - if err := fn(session); err != nil { - session.Rollback() - return err - } - if err := session.Commit(); err != nil { - session.Rollback() - return err - } - return nil -} - // WorkdayModel 工作日 type WorkdayModel struct { ID int64 `json:"id"` //主键