workshop.go 14.5 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/workshop/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/workshop/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/workshop/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"
	"strings"
	"time"
)

// 车间服务
type WorkshopService struct {
}

// 创建车间服务
func (workshopService *WorkshopService) CreateWorkshop(operateInfo *domain.OperateInfo, createWorkshopCommand *command.CreateWorkshopCommand) (interface{}, error) {
	if err := createWorkshopCommand.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 user *domain.User
	userService := domainService.NewUserService()
	user, err = userService.User(createWorkshopCommand.PrincipalId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	newWorkshop := &domain.Workshop{
		CompanyId:    operateInfo.CompanyId,
		OrgId:        operateInfo.OrgId,
		WorkshopName: createWorkshopCommand.WorkshopName,
		Principal:    user,
		CreatedAt:    time.Now(),
		UpdatedAt:    time.Now(),
		ProductLines: []*domain.ProductLine{},
	}

	var workshopRepository domain.WorkshopRepository
	workshopRepository, _, _ = factory.FastPgWorkshop(transactionContext, 0)
	if item, err := workshopRepository.FindOne(map[string]interface{}{
		"workshopName": createWorkshopCommand.WorkshopName,
		"companyId":    operateInfo.CompanyId,
		"orgId":        operateInfo.OrgId,
	}); err == nil && item != nil && strings.EqualFold(item.WorkshopName, createWorkshopCommand.WorkshopName) {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "车间名称已存在")
	}
	if workshop, err := workshopRepository.Save(newWorkshop); 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 workshop, nil
	}
}

// 返回车间服务
func (workshopService *WorkshopService) GetWorkshop(getWorkshopQuery *query.GetWorkshopQuery) (interface{}, error) {
	if err := getWorkshopQuery.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 workshopRepository domain.WorkshopRepository
	if value, err := factory.CreateWorkshopRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		workshopRepository = value
	}
	workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": getWorkshopQuery.WorkshopId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	userService := domainService.NewUserService()
	if workshop.Principal.UserId > 0 {
		u, _ := userService.User(workshop.Principal.UserId)
		if u != nil {
			workshop.Principal = u
		}
	}
	if workshop == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getWorkshopQuery.WorkshopId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		result := &dto.WorkshopDto{}
		result.LoadDto(workshop)
		return result, nil
	}
}

// 返回车间服务列表
func (workshopService *WorkshopService) ListWorkshop(listWorkshopQuery *query.ListWorkshopQuery) (interface{}, error) {
	if err := listWorkshopQuery.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()
	}()
	workshopRepository, _, err := factory.FastPgWorkshop(transactionContext, 0)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	var count int64
	var workshops []*domain.Workshop
	if count, workshops, err = workshopRepository.Find(tool_funs.SimpleStructToMap(listWorkshopQuery)); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return map[string]interface{}{
		"count":     count,
		"workshops": workshops,
	}, nil

}

// 移除车间服务
func (workshopService *WorkshopService) RemoveWorkshop(removeWorkshopCommand *command.RemoveWorkshopCommand) (interface{}, error) {
	if err := removeWorkshopCommand.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 workshopRepository domain.WorkshopRepository
	if value, err := factory.CreateWorkshopRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		workshopRepository = value
	}
	workshop, err := workshopRepository.FindOne(map[string]interface{}{"workshopId": removeWorkshopCommand.WorkshopId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if workshop == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeWorkshopCommand.WorkshopId)))
	}
	if !workshop.CanRemove() {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "当前车间下存在线别,不可删除")
	}
	if workshop, err := workshopRepository.Remove(workshop); 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 workshop, nil
	}
}

// 更新车间服务
func (workshopService *WorkshopService) UpdateWorkshop(updateWorkshopCommand *command.UpdateWorkshopCommand) (interface{}, error) {
	if err := updateWorkshopCommand.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 workshopRepository domain.WorkshopRepository
	var workshop *domain.Workshop
	workshopRepository, workshop, err = factory.FastPgWorkshop(transactionContext, updateWorkshopCommand.WorkshopId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if workshop.WorkshopName != updateWorkshopCommand.WorkshopName {
		if item, err := workshopRepository.FindOne(map[string]interface{}{
			"workshopName": updateWorkshopCommand.WorkshopName,
		}); err == nil && item != nil && strings.EqualFold(item.WorkshopName, updateWorkshopCommand.WorkshopName) {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "车间名称已存在")
		}
		workshop.WorkshopName = updateWorkshopCommand.WorkshopName
	}

	if workshop.Principal.UserId != updateWorkshopCommand.PrincipalId && updateWorkshopCommand.PrincipalId > 0 {
		var user *domain.User
		userService := domainService.NewUserService()
		user, err = userService.User(updateWorkshopCommand.PrincipalId)
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		workshop.Principal = user
	}

	if workshop, err := workshopRepository.Save(workshop); 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())
		}
		result := &dto.WorkshopDto{}
		result.LoadDto(workshop)
		return result, nil
	}
}

// 搜索车间服务列表
func (workshopService *WorkshopService) SearchWorkshop(operateInfo *domain.OperateInfo, listWorkshopQuery *query.SearchWorkshopQuery) (interface{}, error) {
	if err := listWorkshopQuery.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()
	}()
	workshopRepository, _, err := factory.FastPgWorkshop(transactionContext, 0)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	var count int64
	var workshops []*domain.Workshop
	options := utils.ObjectToMap(listWorkshopQuery)
	options["companyId"] = operateInfo.CompanyId
	options["orgId"] = operateInfo.OrgId
	if count, workshops, err = workshopRepository.Find(options); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}

	var results = make([]*dto.WorkshopDto, 0)
	for i := range workshops {
		newItem := &dto.WorkshopDto{}
		results = append(results, newItem.LoadDto(workshops[i]))
	}
	return map[string]interface{}{
		"count":     count,
		"workshops": results,
	}, nil

}

// 搜索车间服务列表
func (workshopService *WorkshopService) SelectorWorkshop(operateInfo *domain.OperateInfo, listWorkshopQuery *query.SearchWorkshopQuery) (interface{}, error) {
	if err := listWorkshopQuery.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()
	}()
	workshopRepository, _, err := factory.FastPgWorkshop(transactionContext, 0)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	var count int64
	var workshops []*domain.Workshop
	options := utils.ObjectToMap(listWorkshopQuery)
	options["companyId"] = operateInfo.CompanyId
	options["orgId"] = operateInfo.OrgId
	if count, workshops, err = workshopRepository.Find(options); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}

	var results = make([]*dto.WorkshopDto, 0)
	for i := range workshops {
		newItem := &dto.WorkshopDto{}
		results = append(results, newItem.LoadDto(workshops[i]))
	}
	return map[string]interface{}{
		"count":     count,
		"workshops": results,
	}, nil

}

// 车间工段列表
func (workshopService *WorkshopService) SelectorWorkshopSection(operateInfo *domain.OperateInfo, getWorkshopQuery *query.GetWorkshopQuery) (interface{}, error) {
	if err := getWorkshopQuery.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 workshop *domain.Workshop
	_, workshop, err = factory.FastPgWorkshop(transactionContext, getWorkshopQuery.WorkshopId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	sections := workshop.AllProductSections(domain.All)
	return map[string]interface{}{
		"count":    len(sections),
		"sections": sections,
	}, nil
}

func NewWorkshopService(options map[string]interface{}) *WorkshopService {
	newWorkshopService := &WorkshopService{}
	return newWorkshopService
}