作者 yangfu

feat: 单位换算增加物料表关联

... ... @@ -439,6 +439,32 @@ func FastProductMaterialGroup(transactionContext application.TransactionContext,
return rep, mod, err
}
// FastProductMaterialGroup 快速返回物料
//
// transactionContext 事务
// id 对象唯一标识
func FastProductMaterial(transactionContext application.TransactionContext, id int, options ...option) (domain.ProductMaterialRepository, *domain.ProductMaterial, error) {
var rep domain.ProductMaterialRepository
var mod *domain.ProductMaterial
var err error
if value, err := CreateProductMaterialRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
rep = value
}
if id > 0 {
if mod, err = rep.FindOne(map[string]interface{}{"productMaterialGroupId": id}); err != nil {
if err == domain.ErrorNotFound {
return nil, nil, application.ThrowError(application.RES_NO_FIND_ERROR, "该物料分组不存在")
}
return nil, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
}
return rep, mod, err
}
/***** 2.配置 *****/
type FastOptions struct {
... ...
... ... @@ -13,10 +13,13 @@ type MaterialUnitConversionDto struct {
ToUnitQuantity *domain.UnitQuantity `json:"toUnitQuantity,omitempty"`
}
func (d *MaterialUnitConversionDto) LoadDto(m *domain.UnitConversion) *MaterialUnitConversionDto {
func (d *MaterialUnitConversionDto) LoadDto(m *domain.UnitConversion, material *domain.ProductMaterial) *MaterialUnitConversionDto {
d.UnitConversionId = m.UnitConversionId
d.MaterialName = m.Material.MaterialName
d.FromUnitQuantity = m.FromUnitQuantity
d.ToUnitQuantity = m.ToUnitQuantity
if material != nil {
d.MaterialName = material.MaterialName
}
return d
}
... ...
... ... @@ -25,7 +25,7 @@ type UnitConversionDto struct {
AuthFlag bool `json:"authFlag"`
}
func (d *UnitConversionDto) LoadDto(m *domain.UnitConversion, orgId int) *UnitConversionDto {
func (d *UnitConversionDto) LoadDto(m *domain.UnitConversion, orgId int, productMaterial *domain.ProductMaterial) *UnitConversionDto {
d.UnitConversionId = m.UnitConversionId
d.WorkStation = m.WorkStation
d.Material = m.Material
... ... @@ -36,5 +36,8 @@ func (d *UnitConversionDto) LoadDto(m *domain.UnitConversion, orgId int) *UnitCo
if m.Ext != nil {
d.OrgName = m.Ext.OrgName
}
if productMaterial != nil {
d.Material.MaterialName = productMaterial.MaterialName
}
return d
}
... ...
... ... @@ -91,12 +91,21 @@ func (unitConversionService *UnitConversionService) GetUnitConversion(getUnitCon
//var unitConversionRepository domain.UnitConversionRepository
var unitConversion *domain.UnitConversion
_, unitConversion, err = factory.FastPgUnitConversion(transactionContext, getUnitConversionQuery.UnitConversionId)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
var productMaterial *domain.ProductMaterial
if unitConversion.Material.ProductMaterialId > 0 {
_, productMaterial, err = factory.FastProductMaterial(transactionContext, unitConversion.Material.ProductMaterialId)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
result := &dto.UnitConversionDto{}
result.LoadDto(unitConversion, 0)
result.LoadDto(unitConversion, 0, productMaterial)
return result, nil
}
... ... @@ -293,17 +302,29 @@ func (unitConversionService *UnitConversionService) SearchUnitConversion(operate
if err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
var mapProductMaterial = make(map[int]*domain.ProductMaterial, 0)
productMaterialIds := domain.UnitConversions(unitConversions).ProductMaterialIds()
if len(productMaterialIds) > 0 {
productMaterialRepository, _, _ := factory.FastProductMaterial(transactionContext, 0)
_, productMaterials, err := productMaterialRepository.Find(map[string]interface{}{"productMaterialIds": productMaterialIds})
if err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
mapProductMaterial = domain.ProductMaterials(productMaterials).ToMapById()
}
var result = make([]*dto.UnitConversionDto, 0)
for i := range unitConversions {
item := unitConversions[i]
newItem := &dto.UnitConversionDto{}
newItem.LoadDto(item, operateInfo.OrgId)
newItem.LoadDto(item, operateInfo.OrgId, mapProductMaterial[item.Material.ProductMaterialId])
result = append(result, newItem)
}
if err := transactionContext.CommitTransaction(); err != nil {
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return count, result, nil
}
... ... @@ -331,11 +352,22 @@ func (unitConversionService *UnitConversionService) SearchMaterial(operateInfo *
return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var mapProductMaterial = make(map[int]*domain.ProductMaterial, 0)
productMaterialIds := domain.UnitConversions(unitConversions).ProductMaterialIds()
if len(productMaterialIds) > 0 {
productMaterialRepository, _, _ := factory.FastProductMaterial(transactionContext, 0)
_, productMaterials, err := productMaterialRepository.Find(map[string]interface{}{"productMaterialIds": productMaterialIds})
if err != nil {
return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
mapProductMaterial = domain.ProductMaterials(productMaterials).ToMapById()
}
var result = make([]*dto.MaterialUnitConversionDto, 0)
for i := range unitConversions {
item := unitConversions[i]
newItem := &dto.MaterialUnitConversionDto{}
newItem.LoadDto(item)
newItem.LoadDto(item, mapProductMaterial[item.Material.ProductMaterialId])
result = append(result, newItem)
}
return count, map[string]interface{}{
... ...
... ... @@ -2,9 +2,11 @@ package domain
// 物料
type Material struct {
// 物料名称
// 物料ID
ProductMaterialId int `json:"productMaterialId"`
// 物料名称 (最新数据需要查询物料表)
MaterialName string `json:"materialName,omitempty"`
// 物料类别
// 物料类别 ‘南瓜饼串’、‘ 包装袋(空)’、‘包装袋(件数)’、装箱(件数)
MaterialCategory string `json:"materialCategory,omitempty"`
}
... ...
... ... @@ -49,3 +49,13 @@ func (productMaterial *ProductMaterial) Identify() interface{} {
func (productMaterial *ProductMaterial) Update(data map[string]interface{}) error {
return nil
}
type ProductMaterials []*ProductMaterial
func (productMaterials ProductMaterials) ToMapById() map[int]*ProductMaterial {
var mapProductMaterial = make(map[int]*ProductMaterial, 0)
for _, v := range productMaterials {
mapProductMaterial[v.ProductMaterialId] = v
}
return mapProductMaterial
}
... ...
package domain
import "time"
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"time"
)
// 单位换算
type UnitConversion struct {
... ... @@ -57,3 +60,16 @@ func (unitConversion *UnitConversion) Update(data map[string]interface{}) error
unitConversion.UpdatedAt = time.Now()
return nil
}
type UnitConversions []*UnitConversion
func (unitConversions UnitConversions) ProductMaterialIds() []int {
set := utils.NewSet()
for i := range unitConversions {
if unitConversions[i].Material.ProductMaterialId == 0 {
continue
}
set.Add(unitConversions[i].Material.ProductMaterialId)
}
return set.KeysInt()
}
... ...
... ... @@ -153,7 +153,11 @@ func (repository *ProductMaterialRepository) Find(queryOptions map[string]interf
var productMaterialModels []*models.ProductMaterial
productMaterials := make([]*domain.ProductMaterial, 0)
query := sqlbuilder.BuildQuery(tx.Model(&productMaterialModels), queryOptions)
query.SetOffsetAndLimit(20)
query.SetWhereByQueryOption("company_id = ?", "companyId")
if v, ok := queryOptions["productMaterialIds"]; ok && len(v.([]int)) > 0 {
query.Where("product_material_id in (?)", pg.In(v))
}
query.SetOffsetAndLimit(domain.MaxQueryRow)
query.SetOrderDirect("product_material_id", "DESC")
if count, err := query.SelectAndCount(); err != nil {
return 0, productMaterials, err
... ...
package utils
const (
unmanaged = iota
untyped
intType
int64Type
uintType
uint64Type
stringType
)
type PlaceholderType = struct{}
// Set is not thread-safe, for concurrent use, make sure to use it with synchronization.
type Set struct {
data map[interface{}]PlaceholderType
tp int
}
// NewSet returns a managed Set, can only put the values with the same type.
func NewSet() *Set {
return &Set{
data: make(map[interface{}]PlaceholderType),
tp: untyped,
}
}
// NewUnmanagedSet returns a unmanaged Set, which can put values with different types.
func NewUnmanagedSet() *Set {
return &Set{
data: make(map[interface{}]PlaceholderType),
tp: unmanaged,
}
}
// Add adds i into s.
func (s *Set) Add(i ...interface{}) {
for _, each := range i {
s.add(each)
}
}
// AddInt adds int values ii into s.
func (s *Set) AddInt(ii ...int) {
for _, each := range ii {
s.add(each)
}
}
// AddInt64 adds int64 values ii into s.
func (s *Set) AddInt64(ii ...int64) {
for _, each := range ii {
s.add(each)
}
}
// AddUint adds uint values ii into s.
func (s *Set) AddUint(ii ...uint) {
for _, each := range ii {
s.add(each)
}
}
// AddUint64 adds uint64 values ii into s.
func (s *Set) AddUint64(ii ...uint64) {
for _, each := range ii {
s.add(each)
}
}
// AddStr adds string values ss into s.
func (s *Set) AddStr(ss ...string) {
for _, each := range ss {
s.add(each)
}
}
// Contains checks if i is in s.
func (s *Set) Contains(i interface{}) bool {
if len(s.data) == 0 {
return false
}
s.validate(i)
_, ok := s.data[i]
return ok
}
// Keys returns the keys in s.
func (s *Set) Keys() []interface{} {
var keys []interface{}
for key := range s.data {
keys = append(keys, key)
}
return keys
}
// KeysInt returns the int keys in s.
func (s *Set) KeysInt() []int {
var keys []int
for key := range s.data {
if intKey, ok := key.(int); ok {
keys = append(keys, intKey)
}
}
return keys
}
// KeysInt64 returns int64 keys in s.
func (s *Set) KeysInt64() []int64 {
var keys []int64
for key := range s.data {
if intKey, ok := key.(int64); ok {
keys = append(keys, intKey)
}
}
return keys
}
// KeysUint returns uint keys in s.
func (s *Set) KeysUint() []uint {
var keys []uint
for key := range s.data {
if intKey, ok := key.(uint); ok {
keys = append(keys, intKey)
}
}
return keys
}
// KeysUint64 returns uint64 keys in s.
func (s *Set) KeysUint64() []uint64 {
var keys []uint64
for key := range s.data {
if intKey, ok := key.(uint64); ok {
keys = append(keys, intKey)
}
}
return keys
}
// KeysStr returns string keys in s.
func (s *Set) KeysStr() []string {
var keys []string
for key := range s.data {
if strKey, ok := key.(string); ok {
keys = append(keys, strKey)
}
}
return keys
}
// Remove removes i from s.
func (s *Set) Remove(i interface{}) {
s.validate(i)
delete(s.data, i)
}
// Count returns the number of items in s.
func (s *Set) Count() int {
return len(s.data)
}
func (s *Set) add(i interface{}) {
switch s.tp {
case unmanaged:
// do nothing
case untyped:
s.setType(i)
default:
s.validate(i)
}
s.data[i] = PlaceholderType{}
}
func (s *Set) setType(i interface{}) {
// s.tp can only be untyped here
switch i.(type) {
case int:
s.tp = intType
case int64:
s.tp = int64Type
case uint:
s.tp = uintType
case uint64:
s.tp = uint64Type
case string:
s.tp = stringType
}
}
func (s *Set) validate(i interface{}) {
if s.tp == unmanaged {
return
}
switch i.(type) {
case int:
if s.tp != intType {
//logx.Errorf("Error: element is int, but set contains elements with type %d", s.tp)
}
case int64:
if s.tp != int64Type {
//logx.Errorf("Error: element is int64, but set contains elements with type %d", s.tp)
}
case uint:
if s.tp != uintType {
//logx.Errorf("Error: element is uint, but set contains elements with type %d", s.tp)
}
case uint64:
if s.tp != uint64Type {
//logx.Errorf("Error: element is uint64, but set contains elements with type %d", s.tp)
}
case string:
if s.tp != stringType {
//logx.Errorf("Error: element is string, but set contains elements with type %d", s.tp)
}
}
}
... ...