You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
3.3 KiB

  1. package datas
  2. import (
  3. "time"
  4. "gin-vue-admin/model"
  5. "gorm.io/gorm"
  6. )
  7. type SysDictionaryToPostgresql struct {
  8. gorm.Model
  9. Name string `json:"name" form:"name" gorm:"column:name;comment:字典名(中)"`
  10. Type string `json:"type" form:"type" gorm:"column:type;comment:字典名(英)"`
  11. Status *bool `json:"status" form:"status" gorm:"column:status;comment:状态"`
  12. Description string `json:"description" form:"description" gorm:"column:description;comment:'描述'"`
  13. SysDictionaryDetails []model.SysDictionaryDetail `json:"sysDictionaryDetails" form:"sysDictionaryDetails"`
  14. }
  15. func InitSysDictionary(db *gorm.DB) (err error) {
  16. var status = new(bool)
  17. *status = true
  18. Dictionaries := []model.SysDictionary{
  19. {Model: gorm.Model{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "性别", Type: "sex", Status: status, Desc: "性别字典"},
  20. {Model: gorm.Model{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库int类型", Type: "int", Status: status, Desc: "int类型对应的数据库类型"},
  21. {Model: gorm.Model{ID: 3, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库时间日期类型", Type: "time.Time", Status: status, Desc: "数据库时间日期类型"},
  22. {Model: gorm.Model{ID: 4, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库浮点型", Type: "float64", Status: status, Desc: "数据库浮点型"},
  23. {Model: gorm.Model{ID: 5, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库字符串", Type: "string", Status: status, Desc: "数据库字符串"},
  24. {Model: gorm.Model{ID: 6, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库bool类型", Type: "bool", Status: status, Desc: "数据库bool类型"},
  25. }
  26. return db.Transaction(func(tx *gorm.DB) error {
  27. if tx.Create(&Dictionaries).Error != nil { // 遇到错误时回滚事务
  28. return err
  29. }
  30. return nil
  31. })
  32. }
  33. func InitSysDictionaryToPostgresql(db *gorm.DB) (err error) {
  34. status := new(bool)
  35. *status = true
  36. tx := db.Begin() // 开始事务
  37. insert := []SysDictionaryToPostgresql{
  38. {Model: gorm.Model{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "性别", Type: "sex", Status: status, Description: "性别字典"},
  39. {Model: gorm.Model{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库int类型", Type: "int", Status: status, Description: "int类型对应的数据库类型"},
  40. {Model: gorm.Model{ID: 3, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库时间日期类型", Type: "time.Time", Status: status, Description: "数据库时间日期类型"},
  41. {Model: gorm.Model{ID: 4, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库浮点型", Type: "float64", Status: status, Description: "数据库浮点型"},
  42. {Model: gorm.Model{ID: 5, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库字符串", Type: "string", Status: status, Description: "数据库字符串"},
  43. {Model: gorm.Model{ID: 6, CreatedAt: time.Now(), UpdatedAt: time.Now()}, Name: "数据库bool类型", Type: "bool", Status: status, Description: "数据库bool类型"},
  44. }
  45. if tx.Create(&insert).Error != nil { // 遇到错误时回滚事务
  46. tx.Rollback()
  47. }
  48. return tx.Commit().Error
  49. }