package mysql import ( orm "dysy/database" "dysy/tools" "github.com/jinzhu/gorm" ) type DysyTwoUrl struct { CreateTime string `gorm:"column:create_time" json:"create_time"` ID int64 `gorm:"column:id;primary_key" json:"id"` Number int `gorm:"column:number" json:"number"` TwoURL string `gorm:"column:two_url" json:"two_url"` UpdateTime string `gorm:"column:update_time" json:"update_time"` } // TableName sets the insert table name for this struct type func (d DysyTwoUrl) TableName() string { return "dysy_two_url" } func (d *DysyTwoUrl) Create(twoUrl string) (DysyTwoUrl, error) { var doc DysyTwoUrl var count = 0 orm.Eloquent.Where("two_url = ? ", twoUrl).Table(d.TableName()).Count(&count) if count != 0 { //更新数字加1 //return doc, errors.New(app.ExtOrderIdAgain) if err := orm.Eloquent.Table(d.TableName()).Model(&doc).Where("two_url = ? ", twoUrl).Updates( map[string]interface{}{ "number": gorm.Expr("number + 1"), "update_time": tools.GetCurrntTimeStr()}).Error; err != nil { return doc, err } return doc, nil } result := orm.Eloquent.Table(d.TableName()).Create(&d) if result.Error != nil { err := result.Error return doc, err } doc = *d return doc, nil }