正在显示
8 个修改的文件
包含
213 行增加
和
5 行删除
| @@ -3,12 +3,14 @@ package service | @@ -3,12 +3,14 @@ package service | ||
| 3 | import ( | 3 | import ( |
| 4 | "fmt" | 4 | "fmt" |
| 5 | "github.com/linmadan/egglib-go/core/application" | 5 | "github.com/linmadan/egglib-go/core/application" |
| 6 | + pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | ||
| 6 | "github.com/linmadan/egglib-go/utils/tool_funs" | 7 | "github.com/linmadan/egglib-go/utils/tool_funs" |
| 7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/command" | 8 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/command" |
| 8 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/dto" | 9 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/dto" |
| 9 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/query" | 10 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/query" |
| 10 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory" | 11 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory" |
| 11 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | 12 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" |
| 13 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService" | ||
| 12 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils" | 14 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils" |
| 13 | "time" | 15 | "time" |
| 14 | ) | 16 | ) |
| @@ -287,6 +289,33 @@ func (deviceService *DeviceService) SearchDevice(operateInfo *domain.OperateInfo | @@ -287,6 +289,33 @@ func (deviceService *DeviceService) SearchDevice(operateInfo *domain.OperateInfo | ||
| 287 | return count, result, nil | 289 | return count, result, nil |
| 288 | } | 290 | } |
| 289 | 291 | ||
| 292 | +// 批量添加产品服务 | ||
| 293 | +func (deviceService *DeviceService) BatchAddProduct(opt *domain.OperateInfo, list []*domain.ImportDeviceItem) ([]interface{}, error) { | ||
| 294 | + transactionContext, err := factory.CreateTransactionContext(nil) | ||
| 295 | + if err != nil { | ||
| 296 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 297 | + } | ||
| 298 | + if err := transactionContext.StartTransaction(); err != nil { | ||
| 299 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 300 | + } | ||
| 301 | + defer func() { | ||
| 302 | + transactionContext.RollbackTransaction() | ||
| 303 | + }() | ||
| 304 | + | ||
| 305 | + batchAddProductService, _ := domainService.NewPGBatchAddDeviceService(transactionContext.(*pgTransaction.TransactionContext)) | ||
| 306 | + var failRows []interface{} | ||
| 307 | + if failRows, err = batchAddProductService.BatchAddDevice(opt, list); err != nil { | ||
| 308 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 309 | + } | ||
| 310 | + if len(failRows) != 0 { | ||
| 311 | + return failRows, nil //有错误行,回滚 | ||
| 312 | + } | ||
| 313 | + if err := transactionContext.CommitTransaction(); err != nil { | ||
| 314 | + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | ||
| 315 | + } | ||
| 316 | + return failRows, nil | ||
| 317 | +} | ||
| 318 | + | ||
| 290 | func NewDeviceService(options map[string]interface{}) *DeviceService { | 319 | func NewDeviceService(options map[string]interface{}) *DeviceService { |
| 291 | newDeviceService := &DeviceService{} | 320 | newDeviceService := &DeviceService{} |
| 292 | return newDeviceService | 321 | return newDeviceService |
| @@ -292,7 +292,7 @@ func (productService *ProductService) BatchAddProduct(opt *domain.OperateInfo, l | @@ -292,7 +292,7 @@ func (productService *ProductService) BatchAddProduct(opt *domain.OperateInfo, l | ||
| 292 | 292 | ||
| 293 | batchAddProductService, _ := domainService.NewPGBatchAddProductService(transactionContext.(*pgTransaction.TransactionContext)) | 293 | batchAddProductService, _ := domainService.NewPGBatchAddProductService(transactionContext.(*pgTransaction.TransactionContext)) |
| 294 | var failRows []interface{} | 294 | var failRows []interface{} |
| 295 | - if failRows, err = batchAddProductService.BatchAddProduct(opt, list); err != nil { | 295 | + if failRows, err = batchAddProductService.BatchAddProduct(opt, list, false); err != nil { |
| 296 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | 296 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) |
| 297 | } | 297 | } |
| 298 | if len(failRows) != 0 { | 298 | if len(failRows) != 0 { |
| @@ -18,6 +18,19 @@ const ( | @@ -18,6 +18,19 @@ const ( | ||
| 18 | DeviceStatusScrapped = 3 // 3:报废 | 18 | DeviceStatusScrapped = 3 // 3:报废 |
| 19 | ) | 19 | ) |
| 20 | 20 | ||
| 21 | +var ( | ||
| 22 | + mapRiskLevel = map[string]int{ | ||
| 23 | + "高": RiskLevelHigh, | ||
| 24 | + "中": RiskLevelMiddle, | ||
| 25 | + "低": RiskLevelLow, | ||
| 26 | + } | ||
| 27 | + mapDeviceStatus = map[string]int{ | ||
| 28 | + "正常": DeviceStatusNormal, | ||
| 29 | + "封存": DeviceStatusArchive, | ||
| 30 | + "报废": DeviceStatusScrapped, | ||
| 31 | + } | ||
| 32 | +) | ||
| 33 | + | ||
| 21 | // 设备 | 34 | // 设备 |
| 22 | type Device struct { | 35 | type Device struct { |
| 23 | // 设备Id | 36 | // 设备Id |
| @@ -99,3 +112,60 @@ func (device *Device) Valid() error { | @@ -99,3 +112,60 @@ func (device *Device) Valid() error { | ||
| 99 | } | 112 | } |
| 100 | return nil | 113 | return nil |
| 101 | } | 114 | } |
| 115 | + | ||
| 116 | +// 导入设备数据体 | ||
| 117 | +type ImportDeviceItem struct { | ||
| 118 | + // 设备编号 | ||
| 119 | + DeviceCode string `json:"deviceCode,omitempty"` | ||
| 120 | + // 设备名称 | ||
| 121 | + DeviceName string `json:"deviceName,omitempty"` | ||
| 122 | + // 设备型号 | ||
| 123 | + DeviceModel string `json:"deviceModel,omitempty"` | ||
| 124 | + // 设备类型 | ||
| 125 | + DeviceType string `json:"deviceType,omitempty"` | ||
| 126 | + // 品牌 | ||
| 127 | + Brand string `json:"brand,omitempty"` | ||
| 128 | + // 设备状态 1:正常 2:封存 3:报废 | ||
| 129 | + Status string `json:"status,omitempty"` | ||
| 130 | + // 风险等级 1:高 2:中 3:低 | ||
| 131 | + Level string `json:"level,omitempty"` | ||
| 132 | + // 失败理由 | ||
| 133 | + FailReason string `json:"failReason"` | ||
| 134 | + | ||
| 135 | + // 设备状态 1:正常 2:封存 3:报废 | ||
| 136 | + DeviceStatus int `json:"-"` | ||
| 137 | + // 风险等级 1:高 2:中 3:低 | ||
| 138 | + RiskLevel int `json:"-"` | ||
| 139 | +} | ||
| 140 | + | ||
| 141 | +func (item *ImportDeviceItem) Valid() error { | ||
| 142 | + if len(item.DeviceCode) == 0 { | ||
| 143 | + return fmt.Errorf("设备编号不能为空") | ||
| 144 | + } | ||
| 145 | + if len(item.DeviceName) == 0 { | ||
| 146 | + return fmt.Errorf("设备名称不能为空") | ||
| 147 | + } | ||
| 148 | + if len(item.DeviceModel) == 0 { | ||
| 149 | + return fmt.Errorf("设备型号不能为空") | ||
| 150 | + } | ||
| 151 | + if len(item.DeviceType) == 0 { | ||
| 152 | + return fmt.Errorf("设备类型不能为空") | ||
| 153 | + } | ||
| 154 | + if len(item.Status) == 0 { | ||
| 155 | + return fmt.Errorf("设备状态不能为空") | ||
| 156 | + } | ||
| 157 | + if len(item.Level) == 0 { | ||
| 158 | + return fmt.Errorf("风险等级不能为空") | ||
| 159 | + } | ||
| 160 | + if v, ok := mapDeviceStatus[item.Status]; !ok { | ||
| 161 | + return fmt.Errorf("设备状态格式有误") | ||
| 162 | + } else { | ||
| 163 | + item.DeviceStatus = v | ||
| 164 | + } | ||
| 165 | + if v, ok := mapRiskLevel[item.Level]; !ok { | ||
| 166 | + return fmt.Errorf("风险等级格式有误") | ||
| 167 | + } else { | ||
| 168 | + item.RiskLevel = v | ||
| 169 | + } | ||
| 170 | + return nil | ||
| 171 | +} |
| @@ -54,9 +54,9 @@ func (product *Product) Update(data map[string]interface{}) error { | @@ -54,9 +54,9 @@ func (product *Product) Update(data map[string]interface{}) error { | ||
| 54 | if productCategory, ok := data["productCategory"]; ok { | 54 | if productCategory, ok := data["productCategory"]; ok { |
| 55 | product.ProductCategory = productCategory.(string) | 55 | product.ProductCategory = productCategory.(string) |
| 56 | } | 56 | } |
| 57 | - //if quantity, ok := data["quantity"]; ok { | ||
| 58 | - // product.ProductSpec.Quantity = quantity.(float64) | ||
| 59 | - //} | 57 | + if product.ProductSpec == nil { |
| 58 | + product.ProductSpec = &UnitQuantity{} | ||
| 59 | + } | ||
| 60 | if unit, ok := data["unit"]; ok { | 60 | if unit, ok := data["unit"]; ok { |
| 61 | product.ProductSpec.Unit = unit.(string) | 61 | product.ProductSpec.Unit = unit.(string) |
| 62 | } | 62 | } |
| 1 | +package domainService | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "github.com/linmadan/egglib-go/core/application" | ||
| 6 | + pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | ||
| 7 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
| 8 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository" | ||
| 9 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log" | ||
| 10 | + "time" | ||
| 11 | +) | ||
| 12 | + | ||
| 13 | +type PGBatchAddDeviceService struct { | ||
| 14 | + transactionContext *pgTransaction.TransactionContext | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | +func (ptr *PGBatchAddDeviceService) BatchAddDevice(opt *domain.OperateInfo, list []*domain.ImportDeviceItem) ([]interface{}, error) { | ||
| 18 | + var failRows = make([]interface{}, 0) | ||
| 19 | + | ||
| 20 | + deviceRepository, _ := repository.NewDeviceRepository(ptr.transactionContext) | ||
| 21 | + _, devices, _ := deviceRepository.Find(map[string]interface{}{"companyId": opt.CompanyId, "orgId": opt.OrgId}) | ||
| 22 | + var mapProduct = make(map[string]*domain.Device) | ||
| 23 | + for i := range devices { | ||
| 24 | + mapProduct[devices[i].DeviceCode] = devices[i] | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + for i := range list { | ||
| 28 | + item := list[i] | ||
| 29 | + if err := item.Valid(); err != nil { | ||
| 30 | + item.FailReason = err.Error() | ||
| 31 | + failRows = append(failRows, item) | ||
| 32 | + continue | ||
| 33 | + } | ||
| 34 | + newItem := &domain.Device{ | ||
| 35 | + CompanyId: opt.CompanyId, | ||
| 36 | + OrgId: opt.OrgId, | ||
| 37 | + DeviceCode: item.DeviceCode, | ||
| 38 | + DeviceName: item.DeviceName, | ||
| 39 | + DeviceModel: item.DeviceCode, | ||
| 40 | + DeviceType: item.DeviceType, | ||
| 41 | + Brand: item.Brand, | ||
| 42 | + DeviceStatus: item.DeviceStatus, | ||
| 43 | + RiskLevel: item.RiskLevel, | ||
| 44 | + CreatedAt: time.Now(), | ||
| 45 | + UpdatedAt: time.Now(), | ||
| 46 | + WorkStation: &domain.WorkStation{}, | ||
| 47 | + } | ||
| 48 | + if _, ok := mapProduct[newItem.DeviceCode]; !ok { | ||
| 49 | + mapProduct[newItem.DeviceCode] = newItem | ||
| 50 | + } else { | ||
| 51 | + item.FailReason = "导入的设备编号已存在" | ||
| 52 | + failRows = append(failRows, item) | ||
| 53 | + continue | ||
| 54 | + } | ||
| 55 | + if _, err := deviceRepository.Save(newItem); err != nil { | ||
| 56 | + log.Logger.Error(err.Error()) | ||
| 57 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常") | ||
| 58 | + } | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + return failRows, nil | ||
| 62 | +} | ||
| 63 | + | ||
| 64 | +func NewPGBatchAddDeviceService(transactionContext *pgTransaction.TransactionContext) (*PGBatchAddDeviceService, error) { | ||
| 65 | + if transactionContext == nil { | ||
| 66 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
| 67 | + } else { | ||
| 68 | + return &PGBatchAddDeviceService{ | ||
| 69 | + transactionContext: transactionContext, | ||
| 70 | + }, nil | ||
| 71 | + } | ||
| 72 | +} |
| @@ -4,6 +4,7 @@ import ( | @@ -4,6 +4,7 @@ import ( | ||
| 4 | "fmt" | 4 | "fmt" |
| 5 | "github.com/linmadan/egglib-go/core/application" | 5 | "github.com/linmadan/egglib-go/core/application" |
| 6 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" | 6 | pgTransaction "github.com/linmadan/egglib-go/transaction/pg" |
| 7 | + "github.com/linmadan/egglib-go/utils/tool_funs" | ||
| 7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | 8 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" |
| 8 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis" | 9 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis" |
| 9 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository" | 10 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/repository" |
| @@ -16,7 +17,10 @@ type PGBatchAddProductService struct { | @@ -16,7 +17,10 @@ type PGBatchAddProductService struct { | ||
| 16 | transactionContext *pgTransaction.TransactionContext | 17 | transactionContext *pgTransaction.TransactionContext |
| 17 | } | 18 | } |
| 18 | 19 | ||
| 19 | -func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, list []*domain.ImportProductItem) ([]interface{}, error) { | 20 | +// BatchAddProduct 批量添加产品 |
| 21 | + | ||
| 22 | +// replaceOld true:已存在的做更新操作 | ||
| 23 | +func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, list []*domain.ImportProductItem, replaceOld bool) ([]interface{}, error) { | ||
| 20 | var failRows = make([]interface{}, 0) | 24 | var failRows = make([]interface{}, 0) |
| 21 | 25 | ||
| 22 | productRepository, _ := repository.NewProductRepository(ptr.transactionContext) | 26 | productRepository, _ := repository.NewProductRepository(ptr.transactionContext) |
| @@ -48,6 +52,15 @@ func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, li | @@ -48,6 +52,15 @@ func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, li | ||
| 48 | CreatedAt: time.Now(), | 52 | CreatedAt: time.Now(), |
| 49 | UpdatedAt: time.Now(), | 53 | UpdatedAt: time.Now(), |
| 50 | } | 54 | } |
| 55 | + // 存在旧数据->>覆盖 | ||
| 56 | + if replaceOld { | ||
| 57 | + if old, ok := mapProduct[newItem.ProductCode]; ok { | ||
| 58 | + if err := ptr.updateProduct(opt, item, old); err != nil { | ||
| 59 | + return failRows, err | ||
| 60 | + } | ||
| 61 | + continue | ||
| 62 | + } | ||
| 63 | + } | ||
| 51 | if len(newItem.ProductCode) == 0 { | 64 | if len(newItem.ProductCode) == 0 { |
| 52 | code, err := redis.GenCode(generator) | 65 | code, err := redis.GenCode(generator) |
| 53 | if err != nil { | 66 | if err != nil { |
| @@ -72,6 +85,18 @@ func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, li | @@ -72,6 +85,18 @@ func (ptr *PGBatchAddProductService) BatchAddProduct(opt *domain.OperateInfo, li | ||
| 72 | return failRows, nil | 85 | return failRows, nil |
| 73 | } | 86 | } |
| 74 | 87 | ||
| 88 | +func (ptr *PGBatchAddProductService) updateProduct(opt *domain.OperateInfo, item *domain.ImportProductItem, old *domain.Product) error { | ||
| 89 | + productRepository, _ := repository.NewProductRepository(ptr.transactionContext) | ||
| 90 | + if err := old.Update(tool_funs.SimpleStructToMap(item)); err != nil { | ||
| 91 | + return err | ||
| 92 | + } | ||
| 93 | + if _, err := productRepository.Save(old); err != nil { | ||
| 94 | + log.Logger.Error(err.Error()) | ||
| 95 | + return application.ThrowError(application.INTERNAL_SERVER_ERROR, "服务器异常") | ||
| 96 | + } | ||
| 97 | + return nil | ||
| 98 | +} | ||
| 99 | + | ||
| 75 | func NewPGBatchAddProductService(transactionContext *pgTransaction.TransactionContext) (*PGBatchAddProductService, error) { | 100 | func NewPGBatchAddProductService(transactionContext *pgTransaction.TransactionContext) (*PGBatchAddProductService, error) { |
| 76 | if transactionContext == nil { | 101 | if transactionContext == nil { |
| 77 | return nil, fmt.Errorf("transactionContext参数不能为nil") | 102 | return nil, fmt.Errorf("transactionContext参数不能为nil") |
| @@ -5,6 +5,7 @@ import ( | @@ -5,6 +5,7 @@ import ( | ||
| 5 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/command" | 5 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/command" |
| 6 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/query" | 6 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/query" |
| 7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/service" | 7 | "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/device/service" |
| 8 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
| 8 | ) | 9 | ) |
| 9 | 10 | ||
| 10 | type DeviceController struct { | 11 | type DeviceController struct { |
| @@ -66,3 +67,13 @@ func (controller *DeviceController) SearchDevice() { | @@ -66,3 +67,13 @@ func (controller *DeviceController) SearchDevice() { | ||
| 66 | total, data, err := deviceService.SearchDevice(ParseOperateInfo(controller.BaseController), listDeviceQuery) | 67 | total, data, err := deviceService.SearchDevice(ParseOperateInfo(controller.BaseController), listDeviceQuery) |
| 67 | ResponseGrid(controller.BaseController, total, data, err) | 68 | ResponseGrid(controller.BaseController, total, data, err) |
| 68 | } | 69 | } |
| 70 | + | ||
| 71 | +func (controller *DeviceController) BatchAddDevice() { | ||
| 72 | + productService := service.NewDeviceService(nil) | ||
| 73 | + cmd := &struct { | ||
| 74 | + List []*domain.ImportDeviceItem `json:"list"` | ||
| 75 | + }{} | ||
| 76 | + Must(controller.Unmarshal(cmd)) | ||
| 77 | + data, err := productService.BatchAddProduct(ParseOperateInfo(controller.BaseController), cmd.List) | ||
| 78 | + controller.Response(data, err) | ||
| 79 | +} |
| @@ -12,4 +12,5 @@ func init() { | @@ -12,4 +12,5 @@ func init() { | ||
| 12 | web.Router("/devices/:deviceId", &controllers.DeviceController{}, "Delete:RemoveDevice") | 12 | web.Router("/devices/:deviceId", &controllers.DeviceController{}, "Delete:RemoveDevice") |
| 13 | web.Router("/devices/", &controllers.DeviceController{}, "Get:ListDevice") | 13 | web.Router("/devices/", &controllers.DeviceController{}, "Get:ListDevice") |
| 14 | web.Router("/devices/search", &controllers.DeviceController{}, "Post:SearchDevice") | 14 | web.Router("/devices/search", &controllers.DeviceController{}, "Post:SearchDevice") |
| 15 | + web.Router("/devices/batch-add", &controllers.DeviceController{}, "Post:BatchAddDevice") | ||
| 15 | } | 16 | } |
-
请 注册 或 登录 后发表评论