device.go 14.6 KB
package service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"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/domainService"
	"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())
	}

	var userService = domainService.NewUserService()
	var org *domain.Org
	org, err = userService.Organization(operateInfo.OrgId)
	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(),
		Ext:          domain.NewExt(org.OrgName),
	}
	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, 0)
		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) BatchRemoveDevice(cmd *command.BatchRemoveDeviceCommand) (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
	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
	}
	for i := range cmd.IdList {
		id := cmd.IdList[i]
		device, err := deviceRepository.FindOne(map[string]interface{}{"deviceId": id})
		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("%d", id))
		}
		if _, err := deviceRepository.Remove(device); 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 (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, "设备编号重复")
		}
	}

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

	data := tool_funs.SimpleStructToMap(cmd)
	data["orgName"] = org.OrgName
	if err := device.Update(data); 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) {
	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())
	}
	workshops, _ := factory.FastPgWorkshops(transactionContext, operateInfo.CompanyId)

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

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

// 批量添加产品服务
func (deviceService *DeviceService) BatchAddProduct(opt *domain.OperateInfo, list []*domain.ImportDeviceItem) ([]interface{}, 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()
	}()

	batchAddProductService, _ := domainService.NewPGBatchAddDeviceService(transactionContext.(*pgTransaction.TransactionContext))
	var failRows []interface{}
	if failRows, err = batchAddProductService.BatchAddDevice(opt, list); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if len(failRows) != 0 {
		return failRows, nil //有错误行,回滚
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return failRows, nil
}

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