pg_material_service.go 10.9 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/dao"
	"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)
		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
	}
	_, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": 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),
	}
	return productMaterialRepository.Save(newProductMaterial)
}

// 物料分组

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)

		productMaterialGroupDao *dao.ProductMaterialGroupDao
		latestNumber            string
		total                   int
	)
	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("物料分组编码已存在")
	}
	productMaterialGroupDao, _ = dao.NewProductMaterialGroupDao(ptr.transactionContext)
	latestNumber, total, err = productMaterialGroupDao.CurrentGroupLatestNumber(opt.CompanyId, item.Pid)
	if err != nil {
		return nil, err
	}

	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.MaterialGroupNumber = newProductMaterialGroup.GroupNumberComposeUp(parent, latestNumber, total)
	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 (ptr *PGMaterialService) AllMaterialGroupParentByBacktracking(opt *domain.OperateInfo, productMaterialGroupId int) ([]*domain.ProductMaterialGroup, []int, error) {
	var (
		err                               error
		listId                            []int
		productMaterialGroup              *domain.ProductMaterialGroup
		productMaterialGroupRepository, _ = repository.NewProductMaterialGroupRepository(ptr.transactionContext)
		pid                               = productMaterialGroupId
		mapExists                         = make(map[int]int)
	)
	var result = make([]*domain.ProductMaterialGroup, 0)
	for {
		if pid == 0 {
			break
		}
		if _, exists := mapExists[pid]; exists {
			break
		}
		mapExists[pid] = pid
		productMaterialGroup, err = productMaterialGroupRepository.FindOne(map[string]interface{}{"companyId": opt.CompanyId, "productMaterialGroupId": pid, "allWithDeleted": "1"})
		if err != nil || productMaterialGroup == nil {
			return nil, listId, fmt.Errorf("物料分组不存在")
		}
		pid = productMaterialGroup.Pid
		result = append([]*domain.ProductMaterialGroup{productMaterialGroup}, result...)
	}
	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
	}
}