product_group.go 12.3 KB
package service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productGroup/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productGroup/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productGroup/query"
	"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"
)

// 生产班组服务
type ProductGroupService struct {
}

// 创建生产班组服务
func (productGroupService *ProductGroupService) CreateProductGroup(operateInfo *domain.OperateInfo, cmd *command.CreateProductGroupCommand) (interface{}, error) {
	if err := cmd.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()

	var productGroupRepository domain.ProductGroupRepository
	productGroupRepository, _, _ = factory.FastPgProductGroup(transactionContext, 0)

	var workStation *domain.WorkStation
	_, workStation, err = factory.FastPgWorkstation(transactionContext, cmd.WorkshopId, cmd.LineId, cmd.SectionId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	var leader *domain.User
	var members []*domain.User
	userService := domainService.NewUserService()
	leader, err = userService.User(cmd.GroupLeaderId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	members, err = userService.Users(cmd.GroupMembers)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	newProductGroup := &domain.ProductGroup{
		CompanyId:    operateInfo.CompanyId,
		OrgId:        operateInfo.OrgId,
		GroupName:    cmd.GroupName,
		GroupLeader:  leader,
		GroupMembers: members,
		WorkStation:  workStation,
		WorkOn:       cmd.WorkOn,
		CreatedAt:    time.Now(),
		UpdatedAt:    time.Now(),
	}
	if group, err := productGroupRepository.FindOne(map[string]interface{}{
		"groupName":  cmd.GroupName,
		"workshopId": workStation.WorkshopId,
		"lineId":     workStation.LineId,
	}); err == nil && group != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "有重复的生产班组")
	}

	if productGroup, err := productGroupRepository.Save(newProductGroup); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return productGroup, nil
	}
}

// 返回生产班组服务
func (productGroupService *ProductGroupService) GetProductGroup(getProductGroupQuery *query.GetProductGroupQuery) (interface{}, error) {
	if err := getProductGroupQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()
	var productGroupRepository domain.ProductGroupRepository
	if value, err := factory.CreateProductGroupRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productGroupRepository = value
	}
	productGroup, err := productGroupRepository.FindOne(map[string]interface{}{"productGroupId": getProductGroupQuery.ProductGroupId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productGroup == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductGroupQuery.ProductGroupId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return productGroup, nil
	}
}

// 返回生产班组服务列表
func (productGroupService *ProductGroupService) ListProductGroup(listProductGroupQuery *query.ListProductGroupQuery) (interface{}, error) {
	if err := listProductGroupQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()
	var productGroupRepository domain.ProductGroupRepository
	if value, err := factory.CreateProductGroupRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productGroupRepository = value
	}
	if count, productGroups, err := productGroupRepository.Find(tool_funs.SimpleStructToMap(listProductGroupQuery)); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return map[string]interface{}{
			"count":         count,
			"productGroups": productGroups,
		}, nil
	}
}

// 移除生产班组服务
func (productGroupService *ProductGroupService) RemoveProductGroup(removeProductGroupCommand *command.RemoveProductGroupCommand) (interface{}, error) {
	if err := removeProductGroupCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()
	var productGroupRepository domain.ProductGroupRepository
	if value, err := factory.CreateProductGroupRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productGroupRepository = value
	}
	productGroup, err := productGroupRepository.FindOne(map[string]interface{}{"productGroupId": removeProductGroupCommand.ProductGroupId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productGroup == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProductGroupCommand.ProductGroupId)))
	}
	if productGroup, err := productGroupRepository.Remove(productGroup); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return productGroup, nil
	}
}

// 更新生产班组服务
func (productGroupService *ProductGroupService) UpdateProductGroup(cmd *command.UpdateProductGroupCommand) (interface{}, error) {
	if err := cmd.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.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()
	}()
	var productGroupRepository domain.ProductGroupRepository
	var productGroup *domain.ProductGroup
	productGroupRepository, productGroup, err = factory.FastPgProductGroup(transactionContext, cmd.ProductGroupId)

	var workStation *domain.WorkStation
	_, workStation, err = factory.FastPgWorkstation(transactionContext, cmd.WorkshopId, cmd.LineId, cmd.SectionId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	// 更新名称
	if productGroup.GroupName != cmd.GroupName {
		if group, err := productGroupRepository.FindOne(map[string]interface{}{
			"groupName":  cmd.GroupName,
			"workshopId": workStation.WorkshopId,
			"lineId":     workStation.LineId,
		}); err == nil && group != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "有重复的生产班组")
		}
	}

	var leader *domain.User
	var members []*domain.User
	userService := domainService.NewUserService()
	leader, err = userService.User(cmd.GroupLeaderId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	members, err = userService.Users(cmd.GroupMembers)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	productGroup.GroupLeader = leader
	productGroup.GroupMembers = members
	productGroup.WorkOn = cmd.WorkOn
	productGroup.WorkStation = workStation
	productGroup.GroupName = cmd.GroupName

	if err := productGroup.Update(tool_funs.SimpleStructToMap(cmd)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if productGroup, err := productGroupRepository.Save(productGroup); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return productGroup, nil
	}
}

// 返回生产班组服务列表
func (productGroupService *ProductGroupService) SearchProductGroup(operateInfo *domain.OperateInfo, q *query.SearchProductGroupQuery) (int64, interface{}, error) {
	q.OrgId = operateInfo.OrgId
	q.CompanyId = operateInfo.CompanyId
	if err := q.ValidateQuery(); err != nil {
		return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var productGroupRepository domain.ProductGroupRepository
	productGroupRepository, _, _ = factory.FastPgProductGroup(transactionContext, 0)
	count, productGroups, err := productGroupRepository.Find(utils.ObjectToMap(q))
	if err != nil {
		return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	var results = make([]*dto.ProductGroupDto, 0)
	for i := range productGroups {
		newItem := &dto.ProductGroupDto{}
		results = append(results, newItem.LoadDto(productGroups[i]))
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return count, results, nil
}

func NewProductGroupService(options map[string]interface{}) *ProductGroupService {
	newProductGroupService := &ProductGroupService{}
	return newProductGroupService
}