From fd85a0bc722eb2d84cb751d6d521971f41684b7a Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 24 八月 2023 11:23:00 +0800
Subject: [PATCH] 初始化仓库数据

---
 model/repository.go |   35 ++++++++++++++++++++++++++++++++++-
 1 files changed, 34 insertions(+), 1 deletions(-)

diff --git a/model/repository.go b/model/repository.go
index 15e079e..309dee7 100644
--- a/model/repository.go
+++ b/model/repository.go
@@ -3,6 +3,7 @@
 import (
 	"aps_crm/pkg/mysqlx"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -11,7 +12,7 @@
 		Id   int    `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
 		Name string `json:"name" gorm:"column:name;type:varchar(255);comment:鍟嗘満闃舵鍚嶇О"`
 	}
-   
+
 	RepositorySearch struct {
 		Repository
 		Orm *gorm.DB
@@ -83,3 +84,35 @@
 	var db = slf.build()
 	return db.Updates(data).Error
 }
+
+func (slf *RepositorySearch) CreateBatch(records []*Repository) error {
+	var db = slf.build()
+	return db.Create(records).Error
+}
+
+// InitDefaultData 鍒濆鍖栨暟鎹�
+func (slf *RepositorySearch) InitDefaultData(errCh chan<- error, wg *sync.WaitGroup) {
+	var (
+		db          = slf.Orm.Table(slf.TableName())
+		total int64 = 0
+	)
+	defer wg.Done()
+
+	if err := db.Count(&total).Error; err != nil {
+		errCh <- err
+		return
+	}
+	if total != 0 {
+		return
+	}
+	records := []*Repository{
+		{1, "鎬讳粨"},
+		{2, "璧犲搧浠�"},
+		{3, "鍏朵粬浠�"},
+	}
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
+}

--
Gitblit v1.8.0