product_calendar.go 9.8 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/productCalendar/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productCalendar/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
)

// 工厂日历服务
type ProductCalendarService struct {
}

// 创建工厂日历服务
func (productCalendarService *ProductCalendarService) CreateProductCalendar(createProductCalendarCommand *command.CreateProductCalendarCommand) (interface{}, error) {
	if err := createProductCalendarCommand.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()
	}()
	newProductCalendar := &domain.ProductCalendar{
		CompanyId: createProductCalendarCommand.CompanyId,
		OrgId:     createProductCalendarCommand.OrgId,
		//WorkshopId:       createProductCalendarCommand.WorkshopId,
		//LineId:           createProductCalendarCommand.LineId,
		//SectionId:        createProductCalendarCommand.SectionId,
		//WorkOn:           createProductCalendarCommand.WorkOn,
		CalendarSelected: createProductCalendarCommand.CalendarSelected,
		InWorkAt:         createProductCalendarCommand.InWorkAt,
		OutWorkAt:        createProductCalendarCommand.OutWorkAt,
		BreakTime:        createProductCalendarCommand.BreakTime,
		WorkTime:         createProductCalendarCommand.WorkTime,
	}
	var productCalendarRepository domain.ProductCalendarRepository
	if value, err := factory.CreateProductCalendarRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productCalendarRepository = value
	}
	if productCalendar, err := productCalendarRepository.Save(newProductCalendar); 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 productCalendar, nil
	}
}

// 返回工厂日历服务
func (productCalendarService *ProductCalendarService) GetProductCalendar(getProductCalendarQuery *query.GetProductCalendarQuery) (interface{}, error) {
	if err := getProductCalendarQuery.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 productCalendarRepository domain.ProductCalendarRepository
	if value, err := factory.CreateProductCalendarRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productCalendarRepository = value
	}
	productCalendar, err := productCalendarRepository.FindOne(map[string]interface{}{"productCalendarId": getProductCalendarQuery.ProductCalendarId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productCalendar == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProductCalendarQuery.ProductCalendarId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return productCalendar, nil
	}
}

// 返回工厂日历服务列表
func (productCalendarService *ProductCalendarService) ListProductCalendar(listProductCalendarQuery *query.ListProductCalendarQuery) (interface{}, error) {
	if err := listProductCalendarQuery.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 productCalendarRepository domain.ProductCalendarRepository
	if value, err := factory.CreateProductCalendarRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productCalendarRepository = value
	}
	if count, productCalendars, err := productCalendarRepository.Find(tool_funs.SimpleStructToMap(listProductCalendarQuery)); 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,
			"productCalendars": productCalendars,
		}, nil
	}
}

// 移除工厂日历服务
func (productCalendarService *ProductCalendarService) RemoveProductCalendar(removeProductCalendarCommand *command.RemoveProductCalendarCommand) (interface{}, error) {
	if err := removeProductCalendarCommand.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 productCalendarRepository domain.ProductCalendarRepository
	if value, err := factory.CreateProductCalendarRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productCalendarRepository = value
	}
	productCalendar, err := productCalendarRepository.FindOne(map[string]interface{}{"productCalendarId": removeProductCalendarCommand.ProductCalendarId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productCalendar == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProductCalendarCommand.ProductCalendarId)))
	}
	if productCalendar, err := productCalendarRepository.Remove(productCalendar); 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 productCalendar, nil
	}
}

// 更新工厂日历服务
func (productCalendarService *ProductCalendarService) UpdateProductCalendar(updateProductCalendarCommand *command.UpdateProductCalendarCommand) (interface{}, error) {
	if err := updateProductCalendarCommand.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 productCalendarRepository domain.ProductCalendarRepository
	if value, err := factory.CreateProductCalendarRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		productCalendarRepository = value
	}
	productCalendar, err := productCalendarRepository.FindOne(map[string]interface{}{"productCalendarId": updateProductCalendarCommand.ProductCalendarId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if productCalendar == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProductCalendarCommand.ProductCalendarId)))
	}
	if err := productCalendar.Update(tool_funs.SimpleStructToMap(updateProductCalendarCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if productCalendar, err := productCalendarRepository.Save(productCalendar); 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 productCalendar, nil
	}
}

func NewProductCalendarService(options map[string]interface{}) *ProductCalendarService {
	newProductCalendarService := &ProductCalendarService{}
	return newProductCalendarService
}