pg_material_service.go 9.3 KB
package domainService

import (
	"fmt"
	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/infrastructure/utils"
	"sort"
	"time"
)

type PGMaterialService struct {
	transactionContext *pgTransaction.TransactionContext
}

// 物料

func (ptr *PGMaterialService) AddMaterial(opt *domain.OperateInfo, item *domain.ProductMaterial) (*domain.ProductMaterial, error) {
	var (
		user                         *domain.User
		org                          *domain.Org
		err                          error
		newProductMaterial           *domain.ProductMaterial
		productMaterialRepository, _ = repository.NewProductMaterialRepository(ptr.transactionContext)
		productGroupRepository, _    = repository.NewProductGroupRepository(ptr.transactionContext)
	)
	if user, err = NewUserService().User(opt.UserId); err != nil {
		return nil, err
	}
	if org, err = NewUserService().Organization(opt.OrgId); err != nil {
		return nil, err
	}
	_, err = productGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productGroupId": item.ProductMaterialGroupId})
	if err != nil {
		return nil, err
	}

	if newProductMaterial, err = productMaterialRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialNumber": item.MaterialNumber}); err == nil || newProductMaterial != nil {
		return nil, fmt.Errorf("物料编码已存在")
	}
	newProductMaterial = &domain.ProductMaterial{
		CompanyId:              item.CompanyId,
		OrgId:                  item.OrgId,
		ProductMaterialGroupId: item.ProductMaterialGroupId,
		MaterialName:           item.MaterialName,
		MaterialNumber:         item.MaterialNumber,
		MaterialAttribute:      item.MaterialAttribute,
		MaterialCategory:       item.MaterialCategory,
		ProductMaterialExt:     item.ProductMaterialExt,
		CreatedAt:              time.Now(),
		UpdatedAt:              time.Now(),
		Ext:                    domain.NewExt(org.OrgName).WithOperator(user),
	}
	newProductMaterial, err = productMaterialRepository.Save(newProductMaterial)
	return newProductMaterial, err
}

// 物料分组

func (ptr *PGMaterialService) AddMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
	var (
		user                              *domain.User
		org                               *domain.Org
		err                               error
		parent                            *domain.ProductMaterialGroup
		newProductMaterialGroup           *domain.ProductMaterialGroup
		productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
	)
	if user, err = NewUserService().User(opt.UserId); err != nil {
		return nil, err
	}
	if org, err = NewUserService().Organization(opt.OrgId); err != nil {
		return nil, err
	}
	if item.Pid != 0 {
		if parent, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.Pid}); err != nil || parent == nil {
			return nil, fmt.Errorf("上级物料分组不存在")
		}
	}
	if newProductMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || newProductMaterialGroup != nil {
		return nil, fmt.Errorf("物料分组编码已存在")
	}
	newProductMaterialGroup = &domain.ProductMaterialGroup{
		CompanyId: opt.CompanyId,
		OrgId:     opt.OrgId,
		Pid:       item.Pid,
		//Path:  item.GetPath(parent),
		MaterialGroupName:   item.MaterialGroupName,
		MaterialGroupNumber: item.MaterialGroupNumber,
		CreatedAt:           time.Now(),
		UpdatedAt:           time.Now(),
		Ext:                 domain.NewExt(org.OrgName).WithOperator(user),
	}
	newProductMaterialGroup, err = productMaterialGroupRepository.Save(newProductMaterialGroup)
	return newProductMaterialGroup, err
}

func (ptr *PGMaterialService) UpdateMaterialGroup(opt *domain.OperateInfo, item *domain.ProductMaterialGroup) (*domain.ProductMaterialGroup, error) {
	var (
		user                              *domain.User
		org                               *domain.Org
		err                               error
		parent                            *domain.ProductMaterialGroup
		productMaterialGroup              *domain.ProductMaterialGroup
		productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
	)
	if user, err = NewUserService().User(opt.UserId); err != nil {
		return nil, err
	}
	if org, err = NewUserService().Organization(opt.OrgId); err != nil {
		return nil, err
	}
	if item.ProductMaterialGroupId == item.Pid {
		return nil, fmt.Errorf("当前物料分组不能做为自己上级")
	}
	if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.ProductMaterialGroupId}); err != nil || productMaterialGroup == nil {
		return nil, fmt.Errorf("物料分组不存在")
	}
	if item.Pid != productMaterialGroup.ProductMaterialGroupId && item.Pid != 0 {
		if parent, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": item.Pid}); err != nil || parent == nil {
			return nil, fmt.Errorf("上级物料分组不存在")
		}
	}
	if productMaterialGroup.MaterialGroupNumber != item.MaterialGroupNumber {
		if group, err := productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "materialGroupNumber": item.MaterialGroupNumber}); err == nil || group != nil {
			return nil, fmt.Errorf("物料分组编码已存在")
		}
	}

	productMaterialGroup.MaterialGroupNumber = item.MaterialGroupNumber
	productMaterialGroup.MaterialGroupName = item.MaterialGroupName
	productMaterialGroup.UpdatedAt = item.UpdatedAt
	productMaterialGroup.Ext = domain.NewExt(org.OrgName).WithOperator(user)

	productMaterialGroup, err = productMaterialGroupRepository.Save(productMaterialGroup)
	return productMaterialGroup, err
}

func (ptr *PGMaterialService) AllMaterialGroupChild(opt *domain.OperateInfo, productMaterialGroupId int) ([]*domain.ProductMaterialGroup, []int, error) {
	var (
		err                               error
		listId                            []int
		productMaterialGroup              *domain.ProductMaterialGroup
		productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
		groups                            domain.ProductMaterialGroups
	)
	if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": productMaterialGroupId}); err != nil || productMaterialGroup == nil {
		return nil, listId, fmt.Errorf("物料分组不存在")
	}
	_, groups, err = productMaterialGroupRepository.Find(map[string]interface{}{"companyId": opt.CompanyId}) //,"orderByProductMaterialGroupId":"asc"
	if len(groups) == 0 || err != nil {
		return nil, listId, err
	}
	// 先排序 sort pid,id asc
	sort.Stable(groups)
	var treeNodes = make([]utils.TreeNode, len(groups))
	for i := 0; i < len(groups); i++ {
		treeNodes[i] = groups[i]
	}
	tree := utils.NewTree(treeNodes)
	child := tree.AllChildNode(productMaterialGroup)
	var result = make([]*domain.ProductMaterialGroup, 0)
	for i := range child {
		if v, ok := child[i].(*domain.ProductMaterialGroup); ok {
			result = append(result, v)
			listId = append(listId, v.ProductMaterialGroupId)
		}
	}
	return result, listId, err
}

func (ptr *PGMaterialService) AllMaterialGroupParent(opt *domain.OperateInfo, productMaterialGroupId int) ([]*domain.ProductMaterialGroup, []int, error) {
	var (
		err                               error
		listId                            []int
		productMaterialGroup              *domain.ProductMaterialGroup
		productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
		groups                            domain.ProductMaterialGroups
	)
	if productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": productMaterialGroupId}); err != nil || productMaterialGroup == nil {
		return nil, listId, fmt.Errorf("物料分组不存在")
	}
	_, groups, err = productMaterialGroupRepository.Find(map[string]interface{}{"companyId": opt.CompanyId}) //,"orderByProductMaterialGroupId":"asc"
	if len(groups) == 0 || err != nil {
		return nil, listId, err
	}
	// 先排序 sort pid,id asc
	sort.Stable(groups)
	var treeNodes = make([]utils.TreeNode, len(groups))
	for i := 0; i < len(groups); i++ {
		treeNodes[i] = groups[i]
	}
	tree := utils.NewTree(treeNodes)
	child := tree.TreeNodePaths(productMaterialGroup)
	var result = make([]*domain.ProductMaterialGroup, 0)
	for i := range child {
		if v, ok := child[i].(*domain.ProductMaterialGroup); ok {
			result = append(result, v)
			listId = append(listId, v.ProductMaterialGroupId)
		}
	}
	return result, listId, err
}

func NewPGMaterialService(transactionContext *pgTransaction.TransactionContext) (*PGMaterialService, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	} else {
		return &PGMaterialService{
			transactionContext: transactionContext,
		}, nil
	}
}