作者 yangfu

增加设备档案导入功能

... ... @@ -3,12 +3,14 @@ package service
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/application/device/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"time"
)
... ... @@ -287,6 +289,33 @@ func (deviceService *DeviceService) SearchDevice(operateInfo *domain.OperateInfo
return count, result, nil
}
// 批量添加产品服务
func (deviceService *DeviceService) BatchAddProduct(opt *domain.OperateInfo, list []*domain.ImportDeviceItem) ([]interface{}, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
batchAddProductService, _ := domainService.NewPGBatchAddDeviceService(transactionContext.(*pgTransaction.TransactionContext))
var failRows []interface{}
if failRows, err = batchAddProductService.BatchAddDevice(opt, list); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if len(failRows) != 0 {
return failRows, nil //有错误行,回滚
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return failRows, nil
}
func NewDeviceService(options map[string]interface{}) *DeviceService {
newDeviceService := &DeviceService{}
return newDeviceService
... ...
... ... @@ -292,7 +292,7 @@ func (productService *ProductService) BatchAddProduct(opt *domain.OperateInfo, l
batchAddProductService, _ := domainService.NewPGBatchAddProductService(transactionContext.(*pgTransaction.TransactionContext))
var failRows []interface{}
if failRows, err = batchAddProductService.BatchAddProduct(opt, list); err != nil {
if failRows, err = batchAddProductService.BatchAddProduct(opt, list, false); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if len(failRows) != 0 {
... ...
... ... @@ -18,6 +18,19 @@ const (
DeviceStatusScrapped = 3 // 3:报废
)
var (
mapRiskLevel = map[string]int{
"高": RiskLevelHigh,
"中": RiskLevelMiddle,
"低": RiskLevelLow,
}
mapDeviceStatus = map[string]int{
"正常": DeviceStatusNormal,
"封存": DeviceStatusArchive,
"报废": DeviceStatusScrapped,
}
)
// 设备
type Device struct {
// 设备Id
... ... @@ -99,3 +112,60 @@ func (device *Device) Valid() error {
}
return nil
}
// 导入设备数据体
type ImportDeviceItem struct {
// 设备编号
DeviceCode string `json:"deviceCode,omitempty"`
// 设备名称
DeviceName string `json:"deviceName,omitempty"`
// 设备型号
DeviceModel string `json:"deviceModel,omitempty"`
// 设备类型
DeviceType string `json:"deviceType,omitempty"`
// 品牌
Brand string `json:"brand,omitempty"`
// 设备状态 1:正常 2:封存 3:报废
Status string `json:"status,omitempty"`
// 风险等级 1:高 2:中 3:低
Level string `json:"level,omitempty"`
// 失败理由
FailReason string `json:"failReason"`
// 设备状态 1:正常 2:封存 3:报废
DeviceStatus int `json:"-"`
// 风险等级 1:高 2:中 3:低
RiskLevel int `json:"-"`
}
func (item *ImportDeviceItem) Valid() error {
if len(item.DeviceCode) == 0 {
return fmt.Errorf("设备编号不能为空")
}
if len(item.DeviceName) == 0 {
return fmt.Errorf("设备名称不能为空")
}
if len(item.DeviceModel) == 0 {
return fmt.Errorf("设备型号不能为空")
}
if len(item.DeviceType) == 0 {
return fmt.Errorf("设备类型不能为空")
}
if len(item.Status) == 0 {
return fmt.Errorf("设备状态不能为空")
}
if len(item.Level) == 0 {
return fmt.Errorf("风险等级不能为空")
}
if v, ok := mapDeviceStatus[item.Status]; !ok {
return fmt.Errorf("设备状态格式有误")
} else {
item.DeviceStatus = v
}
if v, ok := mapRiskLevel[item.Level]; !ok {
return fmt.Errorf("风险等级格式有误")
} else {
item.RiskLevel = v
}
return nil
}
... ...
... ... @@ -54,9 +54,9 @@ func (product *Product) Update(data map[string]interface{}) error {
if productCategory, ok := data["productCategory"]; ok {
product.ProductCategory = productCategory.(string)
}
//if quantity, ok := data["quantity"]; ok {
// product.ProductSpec.Quantity = quantity.(float64)
//}
if product.ProductSpec == nil {
product.ProductSpec = &UnitQuantity{}
}
if unit, ok := data["unit"]; ok {
product.ProductSpec.Unit = unit.(string)
}
... ...
package domainService
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
"time"
)
type PGBatchAddDeviceService struct {
transactionContext *pgTransaction.TransactionContext
}
func (ptr *PGBatchAddDeviceService) BatchAddDevice(opt *domain.OperateInfo, list []*domain.ImportDeviceItem) ([]interface{}, error) {
var failRows = make([]interface{}, 0)
deviceRepository, _ := repository.NewDeviceRepository(ptr.transactionContext)
_, devices, _ := deviceRepository.Find(map[string]interface{}{"companyId": opt.CompanyId, "orgId": opt.OrgId})
var mapProduct = make(map[string]*domain.Device)
for i := range devices {
mapProduct[devices[i].DeviceCode] = devices[i]
}
for i := range list {
item := list[i]
if err := item.Valid(); err != nil {
item.FailReason = err.Error()
failRows = append(failRows, item)
continue
}
newItem := &domain.Device{
CompanyId: opt.CompanyId,
OrgId: opt.OrgId,
DeviceCode: item.DeviceCode,
DeviceName: item.DeviceName,
DeviceModel: item.DeviceCode,
DeviceType: item.DeviceType,
Brand: item.Brand,
DeviceStatus: item.DeviceStatus,
RiskLevel: item.RiskLevel,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
WorkStation: &domain.WorkStation{},
}
if _, ok := mapProduct[newItem.DeviceCode]; !ok {
mapProduct[newItem.DeviceCode] = newItem
} else {
item.FailReason = "导入的设备编号已存在"
failRows = append(failRows, item)
continue
}
if _, err := deviceRepository.Save(newItem); err != nil {
log.Logger.Error(err.Error())
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常")
}
}
return failRows, nil
}
func NewPGBatchAddDeviceService(transactionContext *pgTransaction.TransactionContext) (*PGBatchAddDeviceService, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &PGBatchAddDeviceService{
transactionContext: transactionContext,
}, nil
}
}
... ...
... ... @@ -4,6 +4,7 @@ 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"
... ... @@ -16,7 +17,10 @@ type PGBatchAddProductService struct {
transactionContext *pgTransaction.TransactionContext
}
func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, list []*domain.ImportProductItem) ([]interface{}, error) {
// 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)
... ... @@ -48,6 +52,15 @@ func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, li
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
// 存在旧数据->>覆盖
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 {
... ... @@ -72,6 +85,18 @@ func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, li
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")
... ...
... ... @@ -5,6 +5,7 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/service"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
)
type DeviceController struct {
... ... @@ -66,3 +67,13 @@ func (controller *DeviceController) SearchDevice() {
total, data, err := deviceService.SearchDevice(ParseOperateInfo(controller.BaseController), listDeviceQuery)
ResponseGrid(controller.BaseController, total, data, err)
}
func (controller *DeviceController) BatchAddDevice() {
productService := service.NewDeviceService(nil)
cmd := &struct {
List []*domain.ImportDeviceItem `json:"list"`
}{}
Must(controller.Unmarshal(cmd))
data, err := productService.BatchAddProduct(ParseOperateInfo(controller.BaseController), cmd.List)
controller.Response(data, err)
}
... ...
... ... @@ -12,4 +12,5 @@ func init() {
web.Router("/devices/:deviceId", &controllers.DeviceController{}, "Delete:RemoveDevice")
web.Router("/devices/", &controllers.DeviceController{}, "Get:ListDevice")
web.Router("/devices/search", &controllers.DeviceController{}, "Post:SearchDevice")
web.Router("/devices/batch-add", &controllers.DeviceController{}, "Post:BatchAddDevice")
}
... ...