customers.go 700 B

1234567891011121314151617181920212223242526
  1. package datas
  2. import (
  3. "github.com/gookit/color"
  4. "time"
  5. "gin-vue-admin/model"
  6. "gorm.io/gorm"
  7. )
  8. var Customers = []model.ExaCustomer{
  9. {Model: gorm.Model{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}, CustomerName: "测试客户", CustomerPhoneData: "1761111111", SysUserID: 1, SysUserAuthorityID: "888"},
  10. }
  11. func InitExaCustomer(db *gorm.DB) (err error) {
  12. return db.Transaction(func(tx *gorm.DB) error {
  13. if tx.Where("id IN ? ", []int{1}).Find(&[]model.ExaCustomer{}).RowsAffected == 1 {
  14. color.Danger.Println("exa_customers表的初始数据已存在!")
  15. return nil
  16. }
  17. if tx.Create(&Customers).Error != nil { // 遇到错误时回滚事务
  18. return err
  19. }
  20. return nil
  21. })
  22. }