From 8546acdc2a1f06253f30e0543fc202ebf5219bd0 Mon Sep 17 00:00:00 2001
From: wangpengfei <274878379@qq.com>
Date: 星期五, 18 八月 2023 16:31:40 +0800
Subject: [PATCH] fix

---
 model/severity.go              |   16 ++
 model/invoiceType.go           |   16 ++
 model/timeSpent.go             |   16 ++
 model/city.go                  |   21 ++++
 model/timelyRate.go            |   16 ++
 model/satisfaction.go          |   15 ++
 model/serviceContractStatus.go |   16 ++
 model/solveRate.go             |   16 ++
 model/priorityLevel.go         |   16 ++
 model/serviceType.go           |   15 ++
 model/courierCompany.go        |   16 ++
 model/invoiceStatus.go         |   16 ++
 model/faultType.go             |   16 ++
 model/index.go                 |   24 +++-
 model/isVisit.go               |   15 ++
 model/serviceOrderStatus.go    |   16 ++
 model/serviceContractType.go   |   16 ++
 main.go                        |    9 -
 18 files changed, 221 insertions(+), 70 deletions(-)

diff --git a/main.go b/main.go
index 0c78964..4fc671d 100644
--- a/main.go
+++ b/main.go
@@ -9,7 +9,6 @@
 	"aps_crm/router"
 	"aps_crm/service"
 	"fmt"
-	"github.com/robfig/cron/v3"
 	"net/http"
 	"os"
 	"os/signal"
@@ -50,9 +49,9 @@
 	go v1.InitProductServiceConn()
 	go service.InitUserConn()
 
-	c := cron.New()
-	c.AddFunc("@every 15s", service.SyncUserInfo) // 姣�15绉掑悓姝ヤ竴娆�
-	c.Start()
+	//c := cron.New()
+	//c.AddFunc("@every 15s", service.SyncUserInfo) // 姣�15绉掑悓姝ヤ竴娆�
+	//c.Start()
 
 	logx.Error(server.ListenAndServe().Error())
 }
@@ -65,6 +64,6 @@
 	v1.CloseProductServiceConn()
 	service.CloseUserConn()
 
-	logx.Infof("aps-admin exited...")
+	logx.Infof("aps-crm exited...")
 	os.Exit(0)
 }
diff --git a/model/city.go b/model/city.go
index 4bfa0e7..ade9ca7 100644
--- a/model/city.go
+++ b/model/city.go
@@ -108,3 +108,24 @@
 	err := db.Find(&list).Error
 	return list, err
 }
+
+// InitDefaultCity 鍒濆鍖栭粯璁ゅ煄甯�
+func InitDefaultCity() error {
+	records := []*City{
+		{
+			Name: "鍖椾含甯�",
+		},
+	}
+
+	var db = mysqlx.GetDB()
+	for _, record := range records {
+		var city City
+		if err := db.Where("name = ?", record.Name).First(&city).Error; err != nil {
+			if err := db.Create(record).Error; err != nil {
+				return err
+			}
+		}
+	}
+
+	return nil
+}
diff --git a/model/courierCompany.go b/model/courierCompany.go
index 2744a76..da1e65d 100644
--- a/model/courierCompany.go
+++ b/model/courierCompany.go
@@ -6,6 +6,7 @@
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -124,21 +125,28 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *CourierCompanySearch) InitDefaultData() error {
+func (slf *CourierCompanySearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*CourierCompany{
 		{1, "椤轰赴"},
 		{2, "寰烽偊"},
 		{3, "涓繙娴疯繍"},
 	}
-	return slf.CreateBatch(records)
+
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }
diff --git a/model/faultType.go b/model/faultType.go
index 82e9a65..248233e 100644
--- a/model/faultType.go
+++ b/model/faultType.go
@@ -6,6 +6,7 @@
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -119,16 +120,18 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *FaultTypeSearch) InitDefaultData() error {
+func (slf *FaultTypeSearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*FaultType{
 		{1, "璁惧瀹夎"},
@@ -138,5 +141,10 @@
 		{5, "璁惧鏁呴殰"},
 		{6, "璁惧鑹搧鐜囦綆"},
 	}
-	return slf.CreateBatch(records)
+
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }
diff --git a/model/index.go b/model/index.go
index 7bd2e13..7a9f652 100644
--- a/model/index.go
+++ b/model/index.go
@@ -5,7 +5,7 @@
 	"aps_crm/pkg/logx"
 	"aps_crm/pkg/mysqlx"
 	"aps_crm/pkg/safe"
-	"gorm.io/gorm/schema"
+	"sync"
 )
 
 func Init() error {
@@ -94,10 +94,13 @@
 }
 
 type InitDefaultData interface {
-	InitDefaultData() error
+	InitDefaultData(errCh chan<- error, wg *sync.WaitGroup)
 }
 
 func InsertDefaultData() {
+	errCh := make(chan error, 2)
+	var wg sync.WaitGroup
+
 	models := []interface{}{
 		NewServiceTypeSearch(),
 		NewPriorityLevelSearch(),
@@ -115,11 +118,22 @@
 		NewTimelyRateSearch(),
 		NewFaultTypeSearch(),
 	}
+
 	for _, model := range models {
 		if id, ok := model.(InitDefaultData); ok {
-			if err := id.InitDefaultData(); err != nil {
-				logx.Errorf("InitDefaultData for table: %v, err :%v", model.(schema.Tabler).TableName(), err.Error())
-			}
+			wg.Add(1)
+			go id.InitDefaultData(errCh, &wg)
+		}
+	}
+
+	go func() {
+		wg.Wait()    // 绛夊緟鎵�鏈塯oroutine瀹屾垚
+		close(errCh) // 鍏抽棴閿欒閫氶亾
+	}()
+
+	for err := range errCh {
+		if err != nil {
+			logx.Errorf("InitDefaultData err: %v", err.Error())
 		}
 	}
 }
diff --git a/model/invoiceStatus.go b/model/invoiceStatus.go
index a4f7dac..19a4cde 100644
--- a/model/invoiceStatus.go
+++ b/model/invoiceStatus.go
@@ -6,6 +6,7 @@
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -124,21 +125,28 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *InvoiceStatusSearch) InitDefaultData() error {
+func (slf *InvoiceStatusSearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*InvoiceStatus{
 		{1, "宸插垱寤�"},
 		{2, "宸插瘎閫�"},
 		{3, "鑷姩鍒涘缓"},
 	}
-	return slf.CreateBatch(records)
+
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }
diff --git a/model/invoiceType.go b/model/invoiceType.go
index 14e1d3a..8c2057b 100644
--- a/model/invoiceType.go
+++ b/model/invoiceType.go
@@ -6,6 +6,7 @@
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -124,21 +125,28 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *InvoiceTypeSearch) InitDefaultData() error {
+func (slf *InvoiceTypeSearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*InvoiceType{
 		{1, "澧炵エ6%"},
 		{2, "澧炵エ16%"},
 		{3, "澧炵エ17%"},
 	}
-	return slf.CreateBatch(records)
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+
+	}
 }
diff --git a/model/isVisit.go b/model/isVisit.go
index ec04ee3..7398c7c 100644
--- a/model/isVisit.go
+++ b/model/isVisit.go
@@ -3,6 +3,7 @@
 import (
 	"aps_crm/pkg/mysqlx"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -90,20 +91,26 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *IsVisitSearch) InitDefaultData() error {
+func (slf *IsVisitSearch) InitDefaultData(errCh chan<- error, wg *sync.WaitGroup) {
 	var (
 		db          = slf.Orm.Table(slf.TableName())
 		total int64 = 0
 	)
 	if err := db.Count(&total).Error; err != nil {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*IsVisit{
 		{1, "鏄�"},
 		{2, "鍚�"},
 	}
-	return slf.CreateBatch(records)
+
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }
diff --git a/model/priorityLevel.go b/model/priorityLevel.go
index ee2f3fe..b1902f4 100644
--- a/model/priorityLevel.go
+++ b/model/priorityLevel.go
@@ -6,6 +6,7 @@
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -119,16 +120,19 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *PriorityLevelSearch) InitDefaultData() error {
+func (slf *PriorityLevelSearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*PriorityLevel{
 		{1, "浣�"},
@@ -136,5 +140,9 @@
 		{3, "楂�"},
 		{4, "绱ф��"},
 	}
-	return slf.CreateBatch(records)
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }
diff --git a/model/satisfaction.go b/model/satisfaction.go
index e8c3e14..5b77f9e 100644
--- a/model/satisfaction.go
+++ b/model/satisfaction.go
@@ -3,6 +3,7 @@
 import (
 	"aps_crm/pkg/mysqlx"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -84,16 +85,18 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *SatisfactionSearch) InitDefaultData() error {
+func (slf *SatisfactionSearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*Satisfaction{
 		{1, "100%"},
@@ -107,5 +110,9 @@
 		{9, "20%"},
 		{10, "10%"},
 	}
-	return slf.CreateBatch(records)
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }
diff --git a/model/serviceContractStatus.go b/model/serviceContractStatus.go
index 6b4d6e3..f7e271b 100644
--- a/model/serviceContractStatus.go
+++ b/model/serviceContractStatus.go
@@ -3,6 +3,7 @@
 import (
 	"aps_crm/pkg/mysqlx"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -90,21 +91,28 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *ServiceContractStatusSearch) InitDefaultData() error {
+func (slf *ServiceContractStatusSearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*ServiceContractStatus{
 		{1, "宸插垱寤�"},
 		{2, "宸插鎵�"},
 		{3, "宸蹭綔搴�"},
 	}
-	return slf.CreateBatch(records)
+
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }
diff --git a/model/serviceContractType.go b/model/serviceContractType.go
index 3524737..4b64b32 100644
--- a/model/serviceContractType.go
+++ b/model/serviceContractType.go
@@ -3,6 +3,7 @@
 import (
 	"aps_crm/pkg/mysqlx"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -90,16 +91,18 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *ServiceContractTypeSearch) InitDefaultData() error {
+func (slf *ServiceContractTypeSearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*ServiceContractType{
 		{1, "鎸夊勾"},
@@ -107,5 +110,10 @@
 		{3, "鍏朵粬"},
 		{4, "铏氭嫙鍚堝悓"},
 	}
-	return slf.CreateBatch(records)
+
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }
diff --git a/model/serviceOrderStatus.go b/model/serviceOrderStatus.go
index 44ff749..fdb01e9 100644
--- a/model/serviceOrderStatus.go
+++ b/model/serviceOrderStatus.go
@@ -6,6 +6,7 @@
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -129,16 +130,19 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *ServiceOrderStatusSearch) InitDefaultData() error {
+func (slf *ServiceOrderStatusSearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*ServiceOrderStatus{
 		{1, "鏈鐞�"},
@@ -146,5 +150,9 @@
 		{3, "绛夊緟鍥炲簲"},
 		{4, "鎴愬姛鍏抽棴"},
 	}
-	return slf.CreateBatch(records)
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }
diff --git a/model/serviceType.go b/model/serviceType.go
index 9938c03..b3e6348 100644
--- a/model/serviceType.go
+++ b/model/serviceType.go
@@ -6,6 +6,7 @@
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -119,16 +120,18 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *ServiceTypeSearch) InitDefaultData() error {
+func (slf *ServiceTypeSearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*ServiceType{
 		{1, "鐢佃瘽"},
@@ -137,5 +140,9 @@
 		{4, "涓婇棬"},
 		{5, "鍏朵粬"},
 	}
-	return slf.CreateBatch(records)
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }
diff --git a/model/severity.go b/model/severity.go
index 51c7f1b..bccf576 100644
--- a/model/severity.go
+++ b/model/severity.go
@@ -6,6 +6,7 @@
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -119,16 +120,19 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *SeveritySearch) InitDefaultData() error {
+func (slf *SeveritySearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*Severity{
 		{1, "杞�"},
@@ -136,5 +140,9 @@
 		{3, "杈冧弗閲�"},
 		{4, "鐗逛弗閲�"},
 	}
-	return slf.CreateBatch(records)
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }
diff --git a/model/solveRate.go b/model/solveRate.go
index 6f3e24e..5f7bb55 100644
--- a/model/solveRate.go
+++ b/model/solveRate.go
@@ -3,6 +3,7 @@
 import (
 	"aps_crm/pkg/mysqlx"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -90,16 +91,18 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *SolveRateSearch) InitDefaultData() error {
+func (slf *SolveRateSearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*SolveRate{
 		{1, "100%"},
@@ -113,5 +116,10 @@
 		{9, "20%"},
 		{10, "10%"},
 	}
-	return slf.CreateBatch(records)
+
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }
diff --git a/model/timeSpent.go b/model/timeSpent.go
index 3be7ac0..926d4de 100644
--- a/model/timeSpent.go
+++ b/model/timeSpent.go
@@ -6,6 +6,7 @@
 	"errors"
 	"fmt"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -119,16 +120,19 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *TimeSpentSearch) InitDefaultData() error {
+func (slf *TimeSpentSearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*TimeSpent{
 		{1, "1灏忔椂"},
@@ -138,5 +142,9 @@
 		{5, "2涓伐浣滄棩"},
 		{6, "2涓伐浣滄棩浠ヤ笂"},
 	}
-	return slf.CreateBatch(records)
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }
diff --git a/model/timelyRate.go b/model/timelyRate.go
index 143e456..4704fe8 100644
--- a/model/timelyRate.go
+++ b/model/timelyRate.go
@@ -3,6 +3,7 @@
 import (
 	"aps_crm/pkg/mysqlx"
 	"gorm.io/gorm"
+	"sync"
 )
 
 type (
@@ -90,16 +91,18 @@
 }
 
 // InitDefaultData 鍒濆鍖栨暟鎹�
-func (slf *TimelyRateSearch) InitDefaultData() error {
+func (slf *TimelyRateSearch) 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 {
-		return err
+		errCh <- err
+		return
 	}
 	if total != 0 {
-		return nil
+		return
 	}
 	records := []*TimelyRate{
 		{1, "100%"},
@@ -113,5 +116,10 @@
 		{9, "20%"},
 		{10, "10%"},
 	}
-	return slf.CreateBatch(records)
+
+	err := slf.CreateBatch(records)
+	if err != nil {
+		errCh <- err
+		return
+	}
 }

--
Gitblit v1.8.0