sync_data_service.go 3.5 KB
package service

import (
	"encoding/json"
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/position/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)

type SyncDataPositionService struct{}

func (service SyncDataPositionService) FromBusinessAdmin(param *domain.MessageBody) error {
	var err error
	switch param.Action {
	//新增-编辑职位
	case "add", "edit":
		createPositionCommand := &command.SavePositionCommand{}
		err = json.Unmarshal(param.Data, createPositionCommand)
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		err = service.CreateOrUpdatePosition(createPositionCommand)
	//批量删除职位
	case "batchDelete":
		batchDeletePositionCommand := &command.BatchDeletePositionCommand{}
		err = json.Unmarshal(param.Data, batchDeletePositionCommand)
		if err != nil {
			return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
		err = service.BatchDeletePosition(batchDeletePositionCommand)
	}
	return err
}

// CreateOrUpdatePosition 新增职位
func (service SyncDataPositionService) CreateOrUpdatePosition(positionCommand *command.SavePositionCommand) error {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	positionRepository := factory.CreatePositionRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	position := &domain.Position{
		Id:        positionCommand.Id,
		CompanyId: positionCommand.CompanyId,
		Name:      positionCommand.Name,
		ParentId:  positionCommand.ParentId,
		Path:      positionCommand.Path,
		Level:     positionCommand.Level,
	}
	pdm, _ := positionRepository.FindOne(map[string]interface{}{"id": position.Id})
	if pdm != nil && pdm.Id > 0 {
		_, err = positionRepository.Update(position)
	} else {
		_, err = positionRepository.Insert(position)
	}
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil
}

// BatchDeletePosition 批量删除职位
func (service SyncDataPositionService) BatchDeletePosition(batchDeletePositionCommand *command.BatchDeletePositionCommand) error {
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		_ = transactionContext.RollbackTransaction()
	}()
	positionRepository := factory.CreatePositionRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	})
	err = positionRepository.Remove(batchDeletePositionCommand.Ids)
	if err != nil {
		return application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil
}