product_job.go 13.9 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/productJob/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productJob/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productJob/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
	"time"
)

// 工位服务
type ProductJobService struct {
}

// 创建工位服务
func (productJobService *ProductJobService) CreateProductJob(operateInfo *domain.OperateInfo, cmd *command.CreateProductJobCommand) (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 workshopRepository domain.WorkshopRepository
	var workStation *domain.WorkStation
	_, workStation, err = factory.FastPgWorkstation(transactionContext, cmd.WorkshopId, cmd.LineId, cmd.SectionId, factory.WithSetPrincipal())
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	var productJobRepository domain.ProductJobRepository
	var productJob *domain.ProductJob
	productJobRepository, productJob, _ = factory.FastPgProductJob(transactionContext, 0)

	if job, err := productJobRepository.FindOne(map[string]interface{}{
		"jobName":       cmd.JobName,
		"workStationId": workStation.WorkStationId,
	}); err == nil && job != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "有重复的工位")
	}

	productJob = &domain.ProductJob{
		CompanyId:      operateInfo.CompanyId,
		OrgId:          operateInfo.OrgId,
		JobName:        cmd.JobName,
		ProcessName:    cmd.ProcessName,
		CreatedAt:      time.Now(),
		UpdatedAt:      time.Now(),
		WorkStation:    workStation,
		RelatedDevices: cmd.RelatedDevices,
	}
	if productJob, err := productJobRepository.Save(productJob); 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 productJob, nil
	}
}

// 返回工位服务
func (productJobService *ProductJobService) GetProductJob(getProductJobQuery *query.GetProductJobQuery) (interface{}, error) {
	if err := getProductJobQuery.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 productJobRepository domain.ProductJobRepository
	_, productJob, err := factory.FastPgProductJob(transactionContext, getProductJobQuery.ProductJobId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	_, workshop, err := factory.FastPgWorkshop(transactionContext, productJob.WorkStation.WorkshopId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	productJob.WorkStation, _ = workshop.FindWorkStation(productJob.WorkStation.WorkshopId, productJob.WorkStation.LineId, productJob.WorkStation.SectionId)
	productJob.WorkStation.Principal = workshop.Principal
	newJobDto := &dto.ProductJobDto{}
	newJobDto.LoadDto(productJob, 0)

	//if len(productJob.RelatedDevices) > 0 {
	//	deviceRepository, _, _ := factory.FastPgDevice(transactionContext, 0)
	//	_, devices, err := deviceRepository.Find(map[string]interface{}{
	//		"companyId":  productJob.CompanyId,
	//		"orgId":      productJob.OrgId,
	//		"inDeviceId": productJob.RelatedDevices,
	//	})
	//	if err != nil {
	//		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//	}
	//	newJobDto.WithRelatedDevicesList(devices)
	//}

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}

	return newJobDto, nil
}

// 返回工位服务列表
func (productJobService *ProductJobService) ListProductJob(listProductJobQuery *query.ListProductJobQuery) (interface{}, error) {
	if err := listProductJobQuery.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 productJobRepository domain.ProductJobRepository
	if value, err := factory.CreateProductJobRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productJobRepository = value
	}
	if count, productJobs, err := productJobRepository.Find(tool_funs.SimpleStructToMap(listProductJobQuery)); 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,
			"productJobs": productJobs,
		}, nil
	}
}

// 移除工位服务
func (productJobService *ProductJobService) RemoveProductJob(removeProductJobCommand *command.RemoveProductJobCommand) (interface{}, error) {
	if err := removeProductJobCommand.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 productJobRepository domain.ProductJobRepository
	if value, err := factory.CreateProductJobRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productJobRepository = value
	}
	productJob, err := productJobRepository.FindOne(map[string]interface{}{"productJobId": removeProductJobCommand.ProductJobId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productJob == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProductJobCommand.ProductJobId)))
	}
	if productJob, err := productJobRepository.Remove(productJob); 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 productJob, nil
	}
}

// 移除工位服务
func (productJobService *ProductJobService) BatchRemoveProductJob(cmd *command.BatchRemoveProductJobCommand) (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 productJobRepository domain.ProductJobRepository
	if value, err := factory.CreateProductJobRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productJobRepository = value
	}
	for i := range cmd.IdList {
		id := cmd.IdList[i]
		productJob, err := productJobRepository.FindOne(map[string]interface{}{"productJobId": id})
		if err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		if productJob == nil {
			return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%d", id))
		}
		if _, err := productJobRepository.Remove(productJob); 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 struct {
	}{}, nil
}

// 更新工位服务
func (productJobService *ProductJobService) UpdateProductJob(cmd *command.UpdateProductJobCommand) (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 workshopRepository domain.WorkshopRepository
	var workStation *domain.WorkStation
	_, workStation, err = factory.FastPgWorkstation(transactionContext, cmd.WorkshopId, cmd.LineId, cmd.SectionId, factory.WithSetPrincipal())
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	var productJobRepository domain.ProductJobRepository
	var productJob *domain.ProductJob
	var checkDuplicateJobName = false
	productJobRepository, productJob, _ = factory.FastPgProductJob(transactionContext, cmd.ProductJobId)

	if cmd.JobName != productJob.JobName {
		checkDuplicateJobName = true
	}
	if workStation.WorkStationId != productJob.WorkStation.WorkStationId {
		checkDuplicateJobName = true
	}
	if checkDuplicateJobName {
		if job, err := productJobRepository.FindOne(map[string]interface{}{
			"jobName":       cmd.JobName,
			"companyId":     productJob.CompanyId,
			"orgId":         productJob.OrgId,
			"workStationId": workStation.WorkStationId,
		}); err == nil && job != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "有重复的工位")
		}
	}

	productJob.WorkStation = workStation
	productJob.JobName = cmd.JobName
	productJob.RelatedDevices = cmd.RelatedDevices
	productJob.UpdatedAt = time.Now()
	productJob.ProcessName = cmd.ProcessName

	if productJob, err := productJobRepository.Save(productJob); 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 productJob, nil
	}
}

// 搜索工位服务列表
func (productJobService *ProductJobService) SearchProductJob(operateInfo *domain.OperateInfo, cmd *query.SearchProductJobQuery) (int64, interface{}, error) {
	if err := cmd.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 productJobRepository domain.ProductJobRepository
	if value, err := factory.CreateProductJobRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productJobRepository = value
	}
	workshops, _ := factory.FastPgWorkshops(transactionContext, operateInfo.CompanyId)
	queryOptions := utils.ObjectToMap(cmd)
	queryOptions = workshops.FindByNameWithQuery(queryOptions, cmd.WorkshopName, cmd.LineName, cmd.SectionName)
	count, productJobs, err := productJobRepository.Find(queryOptions)
	if err != nil {
		return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	var result = make([]*dto.ProductJobDto, 0)
	for i := range productJobs {
		item := productJobs[i]

		newJobDto := &dto.ProductJobDto{}
		newJobDto.LoadDto(item, operateInfo.OrgId)
		newJobDto.WorkStation = workshops.FindWorkStation(item.WorkStation.WorkshopId, item.WorkStation.LineId, item.WorkStation.SectionId)
		newJobDto.WorkStation.Principal = item.WorkStation.Principal
		result = append(result, newJobDto)
	}
	return count, result, nil
}

func NewProductJobService(options map[string]interface{}) *ProductJobService {
	newProductJobService := &ProductJobService{}
	return newProductJobService
}