pg_batch_add_product_service.go
3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package domainService
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
"strconv"
"time"
)
type PGBatchAddProductService struct {
transactionContext *pgTransaction.TransactionContext
}
// BatchAddProduct 批量添加产品
// replaceOld true:已存在的做更新操作
func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, list []*domain.ImportProductItem, replaceOld bool) ([]interface{}, error) {
var failRows = make([]interface{}, 0)
productRepository, _ := repository.NewProductRepository(ptr.transactionContext)
_, products, _ := productRepository.Find(map[string]interface{}{"companyId": opt.CompanyId, "orgId": opt.OrgId})
var mapProduct = make(map[string]*domain.Product)
for i := range products {
mapProduct[products[i].ProductCode] = products[i]
}
var userService = NewUserService()
var org *domain.Org
org, err := userService.Organization(opt.OrgId)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
var generator = redis.NewProductCodeCache(opt.CompanyId)
for i := range list {
item := list[i]
if err := item.Valid(); err != nil {
item.FailReason = err.Error()
failRows = append(failRows, item)
continue
}
unitWeight, _ := strconv.ParseFloat(item.UnitWeight, 64)
newItem := &domain.Product{
CompanyId: opt.CompanyId,
OrgId: opt.OrgId,
ProductCode: item.ProductCode,
ProductName: item.ProductName,
ProductCategory: item.ProductCategory,
ProductSpec: &domain.UnitQuantity{
Unit: item.Unit,
UnitWeight: unitWeight,
},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
Ext: domain.NewExt(org.OrgName),
}
// 存在旧数据->>覆盖
if replaceOld {
if old, ok := mapProduct[newItem.ProductCode]; ok {
if err := ptr.updateProduct(opt, item, old); err != nil {
return failRows, err
}
continue
}
}
if len(newItem.ProductCode) == 0 {
code, err := redis.GenCode(generator)
if err != nil {
log.Logger.Error(err.Error())
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常")
}
newItem.ProductCode = code
}
if _, ok := mapProduct[newItem.ProductCode]; !ok {
mapProduct[newItem.ProductCode] = newItem
} else {
item.FailReason = "导入的产品编号已存在"
failRows = append(failRows, item)
continue
}
if _, err := productRepository.Save(newItem); err != nil {
log.Logger.Error(err.Error())
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常")
}
}
return failRows, nil
}
func (ptr *PGBatchAddProductService) updateProduct(opt *domain.OperateInfo, item *domain.ImportProductItem, old *domain.Product) error {
productRepository, _ := repository.NewProductRepository(ptr.transactionContext)
if err := old.Update(tool_funs.SimpleStructToMap(item)); err != nil {
return err
}
if _, err := productRepository.Save(old); err != nil {
log.Logger.Error(err.Error())
return application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常")
}
return nil
}
func NewPGBatchAddProductService(transactionContext *pgTransaction.TransactionContext) (*PGBatchAddProductService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PGBatchAddProductService{
transactionContext: transactionContext,
}, nil
}
}