product_calendar.go 15.6 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/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/productCalendar/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 ProductCalendarService struct {
}

// 创建工厂日历服务
func (productCalendarService *ProductCalendarService) CreateProductCalendar(operateInfo *domain.OperateInfo, cmd *command.CreateProductCalendarCommand) (interface{}, error) {
	cmd.CompanyId = operateInfo.CompanyId
	cmd.OrgId = operateInfo.OrgId
	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 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())
	}

	// 判断同一个工作位置班次下,是否已重复排班
	productCalendarRepository, _, _ := factory.FastPgProductCalendar(transactionContext, 0)
	if total, _, err := productCalendarRepository.Find(map[string]interface{}{
		"companyId":     cmd.CompanyId,
		"orgId":         cmd.OrgId,
		"workStationId": workStation.WorkStationId,
		"workOn":        cmd.WorkOn,
		"limit":         1,
	}); err == nil && total > 0 {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "工厂日历已存在")
	}

	var userService = domainService.NewUserService()
	var org *domain.Org
	org, err = userService.Organization(cmd.OrgId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	newProductCalendar := &domain.ProductCalendar{
		CompanyId:        cmd.CompanyId,
		OrgId:            cmd.OrgId,
		WorkStation:      workStation,
		WorkOn:           cmd.WorkOn,
		CalendarSelected: utils.ToArrayString(cmd.CalendarSelected),
		InWorkAt:         cmd.InWorkAt,
		OutWorkAt:        cmd.OutWorkAt,
		BreakTime:        cmd.BreakTime,
		//WorkTime:         cmd.WorkTime,
		CreatedAt: time.Now(),
		UpdatedAt: time.Now(),
		Ext:       domain.NewExt(org.OrgName),
	}
	if err = newProductCalendar.ResetWorkTime(cmd.WorkTime); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	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())
		}

		result := &dto.ProductCalendarDto{}
		result.LoadDto(productCalendar, 0)
		return result, 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())
		}

		result := &dto.ProductCalendarDto{}
		result.LoadDto(productCalendar, 0)
		return result, 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) BatchRemoveProductCalendar(cmd *command.BatchRemoveProductCalendarCommand) (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 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
	}
	for i := range cmd.IdList {
		id := cmd.IdList[i]
		productCalendar, err := productCalendarRepository.FindOne(map[string]interface{}{"productCalendarId": id})
		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("%d", id))
		}
		if _, err := productCalendarRepository.Remove(productCalendar); 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 (productCalendarService *ProductCalendarService) UpdateProductCalendar(cmd *command.UpdateProductCalendarCommand) (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 productCalendarRepository domain.ProductCalendarRepository
	var productCalendar *domain.ProductCalendar
	productCalendarRepository, productCalendar, err = factory.FastPgProductCalendar(transactionContext, cmd.ProductCalendarId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	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())
	}

	// 判断同一个工作位置班次下,是否已重复排班
	if productCalendar.WorkOn != cmd.WorkOn || productCalendar.WorkStation.WorkStationId != workStation.WorkStationId {
		if total, _, err := productCalendarRepository.Find(map[string]interface{}{
			"companyId":     productCalendar.CompanyId,
			"orgId":         productCalendar.OrgId,
			"workStationId": workStation.WorkStationId,
			"workOn":        cmd.WorkOn,
			"limit":         1,
		}); err == nil && total > 0 {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "工厂日历已存在")
		}
	}

	var userService = domainService.NewUserService()
	var org *domain.Org
	org, err = userService.Organization(productCalendar.OrgId)
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	productCalendar.WorkStation = workStation
	productCalendar.WorkOn = cmd.WorkOn
	productCalendar.CalendarSelected = utils.ToArrayString(cmd.CalendarSelected)
	productCalendar.InWorkAt = cmd.InWorkAt
	productCalendar.OutWorkAt = cmd.OutWorkAt
	productCalendar.BreakTime = cmd.BreakTime
	productCalendar.Ext = domain.NewExt(org.OrgName)
	if err = productCalendar.ResetWorkTime(cmd.WorkTime); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}

	if err := productCalendar.Update(tool_funs.SimpleStructToMap(cmd)); 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())
		}

		result := &dto.ProductCalendarDto{}
		result.LoadDto(productCalendar, 0)
		return result, nil
	}
}

// 返回工厂日历服务列表
func (productCalendarService *ProductCalendarService) SearchProductCalendar(operateInfo *domain.OperateInfo, cmd *query.SearchProductCalendarQuery) (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 productCalendarRepository domain.ProductCalendarRepository
	productCalendarRepository, _, _ = factory.FastPgProductCalendar(transactionContext, 0)

	workshops, _ := factory.FastPgWorkshops(transactionContext, operateInfo.CompanyId)
	queryOptions := utils.ObjectToMap(cmd)
	delete(queryOptions, "workshopName")
	queryOptions = workshops.FindByNameWithQuery(queryOptions, cmd.WorkshopName, "", "")

	count, productCalendars, err := productCalendarRepository.Find(queryOptions)
	if err != nil {
		return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}

	var result = make([]*dto.ProductCalendarDto, 0)
	for i := range productCalendars {
		item := productCalendars[i]
		newJobDto := &dto.ProductCalendarDto{}
		newJobDto.LoadDto(item, operateInfo.OrgId)
		newJobDto.WorkStation = workshops.FindWorkStation(item.WorkStation.WorkshopId, item.WorkStation.LineId, item.WorkStation.SectionId)
		result = append(result, newJobDto)
	}

	return count, result, nil
}

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