dysy_two_url.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package mysql
  2. import (
  3. orm "dysy/database"
  4. "dysy/tools"
  5. "github.com/jinzhu/gorm"
  6. )
  7. type DysyTwoUrl struct {
  8. CreateTime string `gorm:"column:create_time" json:"create_time"`
  9. ID int64 `gorm:"column:id;primary_key" json:"id"`
  10. Number int `gorm:"column:number" json:"number"`
  11. TwoURL string `gorm:"column:two_url" json:"two_url"`
  12. UpdateTime string `gorm:"column:update_time" json:"update_time"`
  13. }
  14. // TableName sets the insert table name for this struct type
  15. func (d DysyTwoUrl) TableName() string {
  16. return "dysy_two_url"
  17. }
  18. func (d *DysyTwoUrl) Create(twoUrl string) (DysyTwoUrl, error) {
  19. var doc DysyTwoUrl
  20. var count = 0
  21. orm.Eloquent.Where("two_url = ? ", twoUrl).Table(d.TableName()).Count(&count)
  22. if count != 0 {
  23. //更新数字加1
  24. //return doc, errors.New(app.ExtOrderIdAgain)
  25. if err := orm.Eloquent.Table(d.TableName()).Model(&doc).Where("two_url = ? ", twoUrl).Updates(
  26. map[string]interface{}{
  27. "number": gorm.Expr("number + 1"),
  28. "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
  29. return doc, err
  30. }
  31. return doc, nil
  32. }
  33. result := orm.Eloquent.Table(d.TableName()).Create(&d)
  34. if result.Error != nil {
  35. err := result.Error
  36. return doc, err
  37. }
  38. doc = *d
  39. return doc, nil
  40. }