dictionaries.go 3.7 KB

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