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

// 设备服务
type DeviceService struct {
}

// 创建设备服务
func (deviceService *DeviceService) CreateDevice(operateInfo *domain.OperateInfo, cmd *command.CreateDeviceCommand) (interface{}, error) {
	cmd.OrgId = operateInfo.OrgId
	cmd.CompanyId = operateInfo.CompanyId
	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())
	}

	newDevice := &domain.Device{
		CompanyId:    cmd.CompanyId,
		OrgId:        cmd.OrgId,
		DeviceCode:   cmd.DeviceCode,
		DeviceName:   cmd.DeviceName,
		DeviceModel:  cmd.DeviceModel,
		DeviceType:   cmd.DeviceType,
		WorkStation:  workStation,
		Brand:        cmd.Brand,
		DeviceStatus: cmd.DeviceStatus,
		RiskLevel:    cmd.RiskLevel,
		CreatedAt:    time.Now(),
		UpdatedAt:    time.Now(),
	}
	deviceRepository, _, _ := factory.FastPgDevice(transactionContext, 0)

	if total, _, err := deviceRepository.Find(map[string]interface{}{
		"deviceCode": cmd.DeviceCode,
		"companyId":  cmd.CompanyId,
		"orgId":      cmd.OrgId,
		"limit":      1,
	}); err == nil && total > 0 {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "设备编号重复")
	}

	if err := newDevice.Valid(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if device, err := deviceRepository.Save(newDevice); 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 device, nil
	}
}

// 返回设备服务
func (deviceService *DeviceService) GetDevice(getDeviceQuery *query.GetDeviceQuery) (interface{}, error) {
	if err := getDeviceQuery.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 deviceRepository domain.DeviceRepository
	if value, err := factory.CreateDeviceRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		deviceRepository = value
	}
	device, err := deviceRepository.FindOne(map[string]interface{}{"deviceId": getDeviceQuery.DeviceId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if device == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getDeviceQuery.DeviceId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}

		result := &dto.DeviceDto{}
		result.LoadDto(device)
		return result, nil
	}
}

// 返回设备服务列表
func (deviceService *DeviceService) ListDevice(listDeviceQuery *query.ListDeviceQuery) (interface{}, error) {
	if err := listDeviceQuery.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 deviceRepository domain.DeviceRepository
	if value, err := factory.CreateDeviceRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		deviceRepository = value
	}
	if count, devices, err := deviceRepository.Find(tool_funs.SimpleStructToMap(listDeviceQuery)); 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,
			"devices": devices,
		}, nil
	}
}

// 移除设备服务
func (deviceService *DeviceService) RemoveDevice(removeDeviceCommand *command.RemoveDeviceCommand) (interface{}, error) {
	if err := removeDeviceCommand.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 deviceRepository domain.DeviceRepository
	if value, err := factory.CreateDeviceRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		deviceRepository = value
	}
	device, err := deviceRepository.FindOne(map[string]interface{}{"deviceId": removeDeviceCommand.DeviceId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if device == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeDeviceCommand.DeviceId)))
	}
	if device, err := deviceRepository.Remove(device); 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 device, nil
	}
}

// 更新设备服务
func (deviceService *DeviceService) UpdateDevice(cmd *command.UpdateDeviceCommand) (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 deviceRepository domain.DeviceRepository
	var device *domain.Device
	deviceRepository, device, err = factory.FastPgDevice(transactionContext, cmd.DeviceId)
	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())
	}
	device.WorkStation = workStation

	if cmd.DeviceCode != device.DeviceCode {
		if total, _, err := deviceRepository.Find(map[string]interface{}{
			"deviceCode": cmd.DeviceCode,
			"companyId":  device.CompanyId,
			"orgId":      device.OrgId,
			"limit":      1,
		}); err == nil && total > 0 {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "设备编号重复")
		}
	}
	if err := device.Update(tool_funs.SimpleStructToMap(cmd)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if err := device.Valid(); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if device, err := deviceRepository.Save(device); 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 device, nil
	}
}

// 返回设备服务列表
func (deviceService *DeviceService) SearchDevice(operateInfo *domain.OperateInfo, listDeviceQuery *query.SearchDeviceQuery) (int64, interface{}, error) {
	listDeviceQuery.OrgId = operateInfo.OrgId
	listDeviceQuery.CompanyId = operateInfo.CompanyId
	if err := listDeviceQuery.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()
	}()
	deviceRepository, _, _ := factory.FastPgDevice(transactionContext, 0)
	count, devices, err := deviceRepository.Find(utils.ObjectToMap(listDeviceQuery))
	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.DeviceDto, 0)
	for i := range devices {
		item := devices[i]
		newJobDto := &dto.DeviceDto{}
		newJobDto.LoadDto(item)
		result = append(result, newJobDto)
	}
	return count, result, nil
}

func NewDeviceService(options map[string]interface{}) *DeviceService {
	newDeviceService := &DeviceService{}
	return newDeviceService
}