作者 yangfu

Merge branch 'dev' of http://gitlab.fjmaimaimai.com/allied-creation/allied-creat…

…ion-manufacture into dev

# Conflicts:
#	pkg/application/factory/repository.go
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type CreateDeviceCollectionCommand struct {
  12 + // 数据采集ID
  13 + DeviceCollectionId int64 `cname:"数据采集ID" json:"deviceCollectionId,string" valid:"Required"`
  14 + // 车间名
  15 + WorkShopName string `cname:"车间名" json:"workShopName" valid:"Required"`
  16 + // 启动状态 1-启动 0-停止
  17 + StartupStatus int64 `cname:"启动状态 1-启动 0-停止" json:"startupStatus,string" valid:"Required"`
  18 + // 设备名
  19 + DeviceSn string `cname:"设备名" json:"deviceSn" valid:"Required"`
  20 + // 通讯状态 1-通讯正常 0-设备未上电或与采集端通讯故障
  21 + ComStatus int64 `cname:"通讯状态 1-通讯正常 0-设备未上电或与采集端通讯故障" json:"comStatus,string" valid:"Required"`
  22 + // 设备数据值
  23 + Values string `cname:"设备数据值" json:"values" valid:"Required"`
  24 +}
  25 +
  26 +func (createDeviceCollectionCommand *CreateDeviceCollectionCommand) Valid(validation *validation.Validation) {
  27 + validation.SetError("CustomValid", "未实现的自定义认证")
  28 +}
  29 +
  30 +func (createDeviceCollectionCommand *CreateDeviceCollectionCommand) ValidateCommand() error {
  31 + valid := validation.Validation{}
  32 + b, err := valid.Valid(createDeviceCollectionCommand)
  33 + if err != nil {
  34 + return err
  35 + }
  36 + if !b {
  37 + elem := reflect.TypeOf(createDeviceCollectionCommand).Elem()
  38 + for _, validErr := range valid.Errors {
  39 + field, isExist := elem.FieldByName(validErr.Field)
  40 + if isExist {
  41 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  42 + } else {
  43 + return fmt.Errorf(validErr.Message)
  44 + }
  45 + }
  46 + }
  47 + return nil
  48 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type RemoveDeviceCollectionCommand struct {
  12 + // 数据采集ID
  13 + DeviceCollectionId int64 `cname:"数据采集ID" json:"deviceCollectionId,string" valid:"Required"`
  14 +}
  15 +
  16 +func (removeDeviceCollectionCommand *RemoveDeviceCollectionCommand) Valid(validation *validation.Validation) {
  17 + validation.SetError("CustomValid", "未实现的自定义认证")
  18 +}
  19 +
  20 +func (removeDeviceCollectionCommand *RemoveDeviceCollectionCommand) ValidateCommand() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(removeDeviceCollectionCommand)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(removeDeviceCollectionCommand).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package command
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type UpdateDeviceCollectionCommand struct {
  12 + // 数据采集ID
  13 + DeviceCollectionId int64 `cname:"数据采集ID" json:"deviceCollectionId,string" valid:"Required"`
  14 + // 车间名
  15 + WorkShopName string `cname:"车间名" json:"workShopName" valid:"Required"`
  16 + // 启动状态 1-启动 0-停止
  17 + StartupStatus int64 `cname:"启动状态 1-启动 0-停止" json:"startupStatus,string" valid:"Required"`
  18 + // 设备名
  19 + DeviceSn string `cname:"设备名" json:"deviceSn" valid:"Required"`
  20 + // 通讯状态 1-通讯正常 0-设备未上电或与采集端通讯故障
  21 + ComStatus int64 `cname:"通讯状态 1-通讯正常 0-设备未上电或与采集端通讯故障" json:"comStatus,string" valid:"Required"`
  22 + // 设备数据值
  23 + Values string `cname:"设备数据值" json:"values" valid:"Required"`
  24 +}
  25 +
  26 +func (updateDeviceCollectionCommand *UpdateDeviceCollectionCommand) Valid(validation *validation.Validation) {
  27 + validation.SetError("CustomValid", "未实现的自定义认证")
  28 +}
  29 +
  30 +func (updateDeviceCollectionCommand *UpdateDeviceCollectionCommand) ValidateCommand() error {
  31 + valid := validation.Validation{}
  32 + b, err := valid.Valid(updateDeviceCollectionCommand)
  33 + if err != nil {
  34 + return err
  35 + }
  36 + if !b {
  37 + elem := reflect.TypeOf(updateDeviceCollectionCommand).Elem()
  38 + for _, validErr := range valid.Errors {
  39 + field, isExist := elem.FieldByName(validErr.Field)
  40 + if isExist {
  41 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  42 + } else {
  43 + return fmt.Errorf(validErr.Message)
  44 + }
  45 + }
  46 + }
  47 + return nil
  48 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type GetDeviceCollectionQuery struct {
  12 + // 数据采集ID
  13 + DeviceCollectionId int64 `cname:"数据采集ID" json:"deviceCollectionId,string" valid:"Required"`
  14 +}
  15 +
  16 +func (getDeviceCollectionQuery *GetDeviceCollectionQuery) Valid(validation *validation.Validation) {
  17 + validation.SetError("CustomValid", "未实现的自定义认证")
  18 +}
  19 +
  20 +func (getDeviceCollectionQuery *GetDeviceCollectionQuery) ValidateQuery() error {
  21 + valid := validation.Validation{}
  22 + b, err := valid.Valid(getDeviceCollectionQuery)
  23 + if err != nil {
  24 + return err
  25 + }
  26 + if !b {
  27 + elem := reflect.TypeOf(getDeviceCollectionQuery).Elem()
  28 + for _, validErr := range valid.Errors {
  29 + field, isExist := elem.FieldByName(validErr.Field)
  30 + if isExist {
  31 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  32 + } else {
  33 + return fmt.Errorf(validErr.Message)
  34 + }
  35 + }
  36 + }
  37 + return nil
  38 +}
  1 +package query
  2 +
  3 +import (
  4 + "fmt"
  5 + "reflect"
  6 + "strings"
  7 +
  8 + "github.com/beego/beego/v2/core/validation"
  9 +)
  10 +
  11 +type ListDeviceCollectionQuery struct {
  12 + // 查询偏离量
  13 + Offset int `cname:"查询偏离量" json:"offset" valid:"Required"`
  14 + // 查询限制
  15 + Limit int `cname:"查询限制" json:"limit" valid:"Required"`
  16 +}
  17 +
  18 +func (listDeviceCollectionQuery *ListDeviceCollectionQuery) Valid(validation *validation.Validation) {
  19 + validation.SetError("CustomValid", "未实现的自定义认证")
  20 +}
  21 +
  22 +func (listDeviceCollectionQuery *ListDeviceCollectionQuery) ValidateQuery() error {
  23 + valid := validation.Validation{}
  24 + b, err := valid.Valid(listDeviceCollectionQuery)
  25 + if err != nil {
  26 + return err
  27 + }
  28 + if !b {
  29 + elem := reflect.TypeOf(listDeviceCollectionQuery).Elem()
  30 + for _, validErr := range valid.Errors {
  31 + field, isExist := elem.FieldByName(validErr.Field)
  32 + if isExist {
  33 + return fmt.Errorf(strings.Replace(validErr.Message, validErr.Field, field.Tag.Get("cname"), -1))
  34 + } else {
  35 + return fmt.Errorf(validErr.Message)
  36 + }
  37 + }
  38 + }
  39 + return nil
  40 +}
  1 +package service
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/linmadan/egglib-go/core/application"
  6 + "github.com/linmadan/egglib-go/utils/tool_funs"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/deviceCollection/command"
  8 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/deviceCollection/query"
  9 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/factory"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  11 +)
  12 +
  13 +type DeviceCollectionService struct {
  14 +}
  15 +
  16 +// 创建
  17 +func (deviceCollectionService *DeviceCollectionService) CreateDeviceCollection(createDeviceCollectionCommand *command.CreateDeviceCollectionCommand) (interface{}, error) {
  18 + if err := createDeviceCollectionCommand.ValidateCommand(); err != nil {
  19 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  20 + }
  21 + transactionContext, err := factory.CreateTransactionContext(nil)
  22 + if err != nil {
  23 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  24 + }
  25 + if err := transactionContext.StartTransaction(); err != nil {
  26 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  27 + }
  28 + defer func() {
  29 + transactionContext.RollbackTransaction()
  30 + }()
  31 + newDeviceCollection := &domain.DeviceCollection{
  32 + DeviceCollectionId: createDeviceCollectionCommand.DeviceCollectionId,
  33 + WorkShopName: createDeviceCollectionCommand.WorkShopName,
  34 + StartupStatus: createDeviceCollectionCommand.StartupStatus,
  35 + DeviceSn: createDeviceCollectionCommand.DeviceSn,
  36 + ComStatus: createDeviceCollectionCommand.ComStatus,
  37 + Values: createDeviceCollectionCommand.Values,
  38 + }
  39 + var deviceCollectionRepository domain.DeviceCollectionRepository
  40 + if value, err := factory.CreateDeviceCollectionRepository(map[string]interface{}{
  41 + "transactionContext": transactionContext,
  42 + }); err != nil {
  43 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  44 + } else {
  45 + deviceCollectionRepository = value
  46 + }
  47 + if deviceCollection, err := deviceCollectionRepository.Save(newDeviceCollection); err != nil {
  48 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  49 + } else {
  50 + if err := transactionContext.CommitTransaction(); err != nil {
  51 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  52 + }
  53 + return deviceCollection, nil
  54 + }
  55 +}
  56 +
  57 +// 返回
  58 +func (deviceCollectionService *DeviceCollectionService) GetDeviceCollection(getDeviceCollectionQuery *query.GetDeviceCollectionQuery) (interface{}, error) {
  59 + if err := getDeviceCollectionQuery.ValidateQuery(); err != nil {
  60 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  61 + }
  62 + transactionContext, err := factory.CreateTransactionContext(nil)
  63 + if err != nil {
  64 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  65 + }
  66 + if err := transactionContext.StartTransaction(); err != nil {
  67 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  68 + }
  69 + defer func() {
  70 + transactionContext.RollbackTransaction()
  71 + }()
  72 + var deviceCollectionRepository domain.DeviceCollectionRepository
  73 + if value, err := factory.CreateDeviceCollectionRepository(map[string]interface{}{
  74 + "transactionContext": transactionContext,
  75 + }); err != nil {
  76 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  77 + } else {
  78 + deviceCollectionRepository = value
  79 + }
  80 + deviceCollection, err := deviceCollectionRepository.FindOne(map[string]interface{}{"deviceCollectionId": getDeviceCollectionQuery.DeviceCollectionId})
  81 + if err != nil {
  82 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  83 + }
  84 + if deviceCollection == nil {
  85 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getDeviceCollectionQuery.DeviceCollectionId)))
  86 + } else {
  87 + if err := transactionContext.CommitTransaction(); err != nil {
  88 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  89 + }
  90 + return deviceCollection, nil
  91 + }
  92 +}
  93 +
  94 +// 返回列表
  95 +func (deviceCollectionService *DeviceCollectionService) ListDeviceCollection(listDeviceCollectionQuery *query.ListDeviceCollectionQuery) (interface{}, error) {
  96 + if err := listDeviceCollectionQuery.ValidateQuery(); err != nil {
  97 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  98 + }
  99 + transactionContext, err := factory.CreateTransactionContext(nil)
  100 + if err != nil {
  101 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  102 + }
  103 + if err := transactionContext.StartTransaction(); err != nil {
  104 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  105 + }
  106 + defer func() {
  107 + transactionContext.RollbackTransaction()
  108 + }()
  109 + var deviceCollectionRepository domain.DeviceCollectionRepository
  110 + if value, err := factory.CreateDeviceCollectionRepository(map[string]interface{}{
  111 + "transactionContext": transactionContext,
  112 + }); err != nil {
  113 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  114 + } else {
  115 + deviceCollectionRepository = value
  116 + }
  117 + if count, deviceCollections, err := deviceCollectionRepository.Find(tool_funs.SimpleStructToMap(listDeviceCollectionQuery)); err != nil {
  118 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  119 + } else {
  120 + if err := transactionContext.CommitTransaction(); err != nil {
  121 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  122 + }
  123 + return map[string]interface{}{
  124 + "count": count,
  125 + "deviceCollections": deviceCollections,
  126 + }, nil
  127 + }
  128 +}
  129 +
  130 +// 移除
  131 +func (deviceCollectionService *DeviceCollectionService) RemoveDeviceCollection(removeDeviceCollectionCommand *command.RemoveDeviceCollectionCommand) (interface{}, error) {
  132 + if err := removeDeviceCollectionCommand.ValidateCommand(); err != nil {
  133 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  134 + }
  135 + transactionContext, err := factory.CreateTransactionContext(nil)
  136 + if err != nil {
  137 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  138 + }
  139 + if err := transactionContext.StartTransaction(); err != nil {
  140 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  141 + }
  142 + defer func() {
  143 + transactionContext.RollbackTransaction()
  144 + }()
  145 + var deviceCollectionRepository domain.DeviceCollectionRepository
  146 + if value, err := factory.CreateDeviceCollectionRepository(map[string]interface{}{
  147 + "transactionContext": transactionContext,
  148 + }); err != nil {
  149 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  150 + } else {
  151 + deviceCollectionRepository = value
  152 + }
  153 + deviceCollection, err := deviceCollectionRepository.FindOne(map[string]interface{}{"deviceCollectionId": removeDeviceCollectionCommand.DeviceCollectionId})
  154 + if err != nil {
  155 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  156 + }
  157 + if deviceCollection == nil {
  158 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeDeviceCollectionCommand.DeviceCollectionId)))
  159 + }
  160 + if deviceCollection, err := deviceCollectionRepository.Remove(deviceCollection); err != nil {
  161 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  162 + } else {
  163 + if err := transactionContext.CommitTransaction(); err != nil {
  164 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  165 + }
  166 + return deviceCollection, nil
  167 + }
  168 +}
  169 +
  170 +// 更新
  171 +func (deviceCollectionService *DeviceCollectionService) UpdateDeviceCollection(updateDeviceCollectionCommand *command.UpdateDeviceCollectionCommand) (interface{}, error) {
  172 + if err := updateDeviceCollectionCommand.ValidateCommand(); err != nil {
  173 + return nil, application.ThrowError(application.ARG_ERROR, err.Error())
  174 + }
  175 + transactionContext, err := factory.CreateTransactionContext(nil)
  176 + if err != nil {
  177 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  178 + }
  179 + if err := transactionContext.StartTransaction(); err != nil {
  180 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  181 + }
  182 + defer func() {
  183 + transactionContext.RollbackTransaction()
  184 + }()
  185 + var deviceCollectionRepository domain.DeviceCollectionRepository
  186 + if value, err := factory.CreateDeviceCollectionRepository(map[string]interface{}{
  187 + "transactionContext": transactionContext,
  188 + }); err != nil {
  189 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  190 + } else {
  191 + deviceCollectionRepository = value
  192 + }
  193 + deviceCollection, err := deviceCollectionRepository.FindOne(map[string]interface{}{"deviceCollectionId": updateDeviceCollectionCommand.DeviceCollectionId})
  194 + if err != nil {
  195 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  196 + }
  197 + if deviceCollection == nil {
  198 + return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateDeviceCollectionCommand.DeviceCollectionId)))
  199 + }
  200 + if err := deviceCollection.Update(tool_funs.SimpleStructToMap(updateDeviceCollectionCommand)); err != nil {
  201 + return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
  202 + }
  203 + if deviceCollection, err := deviceCollectionRepository.Save(deviceCollection); err != nil {
  204 + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
  205 + } else {
  206 + if err := transactionContext.CommitTransaction(); err != nil {
  207 + return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
  208 + }
  209 + return deviceCollection, nil
  210 + }
  211 +}
  212 +
  213 +func NewDeviceCollectionService(options map[string]interface{}) *DeviceCollectionService {
  214 + newDeviceCollectionService := &DeviceCollectionService{}
  215 + return newDeviceCollectionService
  216 +}
@@ -118,6 +118,14 @@ func CreateProductPlanDispatchRecordRepository(options map[string]interface{}) ( @@ -118,6 +118,14 @@ func CreateProductPlanDispatchRecordRepository(options map[string]interface{}) (
118 return repository.NewProductPlanDispatchRecordRepository(transactionContext) 118 return repository.NewProductPlanDispatchRecordRepository(transactionContext)
119 } 119 }
120 120
  121 +func CreateDeviceCollectionRepository(options map[string]interface{}) (domain.DeviceCollectionRepository, error) {
  122 + var transactionContext *pg.TransactionContext
  123 + if value, ok := options["transactionContext"]; ok {
  124 + transactionContext = value.(*pg.TransactionContext)
  125 + }
  126 + return repository.NewDeviceCollectionRepository(transactionContext)
  127 +}
  128 +
121 func CreateDeviceRunningRecordRepository(options map[string]interface{}) (domain.DeviceRunningRecordRepository, error) { 129 func CreateDeviceRunningRecordRepository(options map[string]interface{}) (domain.DeviceRunningRecordRepository, error) {
122 var transactionContext *pg.TransactionContext 130 var transactionContext *pg.TransactionContext
123 if value, ok := options["transactionContext"]; ok { 131 if value, ok := options["transactionContext"]; ok {
  1 +package domain
  2 +
  3 +import "time"
  4 +
  5 +// 设备采集数据
  6 +type DeviceCollection struct {
  7 + // 数据采集ID
  8 + DeviceCollectionId int64 `json:"deviceCollectionId,string"`
  9 + // 车间名
  10 + WorkShopName string `json:"workShopName"`
  11 + // 采集时间
  12 + CollectionTime time.Time `json:"collectionTime"`
  13 + // 设备名
  14 + DeviceSn string `json:"deviceSn"`
  15 + // 设备类型
  16 + DeviceType string `json:"deviceType"`
  17 + // 启动状态 1-启动 0-停止
  18 + StartupStatus int64 `json:"startupStatus"`
  19 + // 通讯状态 1-通讯正常 0-设备未上电或与采集端通讯故障
  20 + ComStatus int64 `json:"comStatus"`
  21 + // 设备数据值
  22 + Values interface{} `json:"values"`
  23 +}
  24 +
  25 +var (
  26 + // 包馅机
  27 + DeviceTypeBaoXianJi = "BXJ"
  28 + // 打浆机
  29 + DeviceTypeDaJiangJi = "DJJ"
  30 + // 面包屑机
  31 + DeviceTypeMianBaoXieJi = "MBXJ"
  32 + // 油炸机
  33 + DeviceTypeYouZhaJi = "YZJ"
  34 + // 串串机
  35 + DeviceTypeChuanChuanJi = "CCJ"
  36 + // 速冻线
  37 + DeviceTypeSuDongXian = "SDX"
  38 + // 封口机
  39 + DeviceTypeFengKouJi = "FKJ"
  40 + // 封箱机
  41 + DeviceTypeFengXiangJi = "FXJ"
  42 +)
  43 +
  44 +type DeviceCollectionRepository interface {
  45 + Save(deviceCollection *DeviceCollection) (*DeviceCollection, error)
  46 + Remove(deviceCollection *DeviceCollection) (*DeviceCollection, error)
  47 + FindOne(queryOptions map[string]interface{}) (*DeviceCollection, error)
  48 + Find(queryOptions map[string]interface{}) (int64, []*DeviceCollection, error)
  49 +}
  50 +
  51 +func (deviceCollection *DeviceCollection) Identify() interface{} {
  52 + if deviceCollection.DeviceCollectionId == 0 {
  53 + return nil
  54 + }
  55 + return deviceCollection.DeviceCollectionId
  56 +}
  57 +
  58 +func (deviceCollection *DeviceCollection) Update(data map[string]interface{}) error {
  59 + if workShopName, ok := data["workShopName"]; ok {
  60 + deviceCollection.WorkShopName = workShopName.(string)
  61 + }
  62 + if deviceSn, ok := data["deviceSn"]; ok {
  63 + deviceCollection.DeviceSn = deviceSn.(string)
  64 + }
  65 + if deviceType, ok := data["deviceType"]; ok {
  66 + deviceCollection.DeviceSn = deviceType.(string)
  67 + }
  68 + if startupStatus, ok := data["startupStatus"]; ok {
  69 + deviceCollection.StartupStatus = startupStatus.(int64)
  70 + }
  71 + if comStatus, ok := data["comStatus"]; ok {
  72 + deviceCollection.ComStatus = comStatus.(int64)
  73 + }
  74 + if values, ok := data["values"]; ok {
  75 + deviceCollection.Values = values
  76 + }
  77 + return nil
  78 +}
  1 +package domain
  2 +
  3 +// 包馅机
  4 +type DeviceBaoXianJi struct {
  5 + InterSpeed int64 `json:"InterSpeed"` // 内包材速度:内包材运行速率
  6 + ExterSpeed int64 `json:"ExterSpeed"` // 外包材速度:内包材运行速率
  7 + KnifeSpeed int64 `json:"KnifeSpeed"` // 切刀速度:切刀运行速率
  8 + TransSpeed int64 `json:"TransSpeed"` // 输送速度:输送带运行速率
  9 + Count int64 `json:"Count"` // 生产计数:生产统计数量
  10 +}
  11 +
  12 +// 油炸机
  13 +type DeviceYouZhaJi struct {
  14 + FrontTemp float64 `json:"FontTemp"` // 炸机前段温度:炸机前段当前温度
  15 + BackTemp float64 `json:"BackTemp"` // 炸机后段温度:炸机后段当前温度
  16 + TankTemp float64 `json:"TankTemp"` // 储油罐温度 :储油罐当前温度
  17 + TubeTemp float64 `json:"TubeTemp"` // 管路温度:管路当前温度
  18 +}
  19 +
  20 +// 串串机
  21 +type DeviceChuanChuanJi struct {
  22 + Count int64 `json:"Count"` // 生产计数:生产统计数量
  23 + Year string `json:"Year"` // 年
  24 + Month string `json:"Month"` // 月
  25 + Day string `json:"Day"` // 日
  26 + ProductType string `json:"ProductType"` // 产品类型:当前产品种类
  27 +}
  28 +
  29 +// 速冻线
  30 +type DeviceSuDongXian struct {
  31 + CurrTemp float64 `json:"CurrTemp"` // 当前温度:当前温度
  32 +}
  33 +
  34 +// 封口机
  35 +type DeviceFengKouJi struct {
  36 + Count int64 `json:"Count"` // 生产计数:生产统计数量
  37 + Year string `json:"Year"` // 年
  38 + Month string `json:"Month"` // 月
  39 + Day string `json:"Day"` // 日
  40 + ProductType string `json:"ProductType"` // 产品类型:当前产品种类
  41 +}
  42 +
  43 +// 封箱机
  44 +type DeviceFengXiangJi struct {
  45 + Count int64 `json:"Count"` // 生产计数:生产统计数量
  46 + Year string `json:"Year"` // 年
  47 + Month string `json:"Month"` // 月
  48 + Day string `json:"Day"` // 日
  49 + ProductType string `json:"ProductType"` // 产品类型:当前产品种类
  50 +}
1 -package domain  
2 -  
3 -import "time"  
4 -  
5 -type DeviceWorkShop struct {  
6 - WorkShop string `json:"WorkShop"` // 车间名  
7 - DeviceSn string `json:"DeviceSn"` // 设备名称  
8 - CurrTime time.Time `json:"CurrTime"` // 当前时间  
9 - StartupState int64 `json:"StartupState"` // 启动状态:1:启动,0:停止  
10 - ComStatus int64 `json:"ComStatus"` // 通讯状态:1:通讯正常,0:设备未上电或与采集端通讯故障  
11 - InterSpeed int64 `json:"InterSpeed"` // 内包材速度:内包材运行速率  
12 - ExterSpeed int64 `json:"ExterSpeed"` // 外包材速度:内包材运行速率  
13 - KnifeSpeed int64 `json:"KnifeSpeed"` // 切刀速度:切刀运行速率  
14 - TransSpeed int64 `json:"TransSpeed"` // 输送速度:输送带运行速率  
15 - FrontTemp float64 `json:"FontTemp"` // 炸机前段温度:炸机前段当前温度  
16 - BackTemp float64 `json:"BackTemp"` // 炸机后段温度:炸机后段当前温度  
17 - TankTemp float64 `json:"TankTemp"` // 储油罐温度 :储油罐当前温度  
18 - TubeTemp float64 `json:"TubeTemp"` // 管路温度:管路当前温度  
19 - Temp1 float64 `json:"Temp1"` // 温度1:温度1当前温度  
20 - Temp2 float64 `json:"Temp2"` // 温度2:温度2当前温度  
21 - Temp3 float64 `json:"Temp3"` // 温度3:温度3当前温度  
22 - Year string `json:"Year"` // 年  
23 - Month string `json:"Month"` // 月  
24 - Day string `json:"Day"` // 日  
25 - ProductType string `json:"ProductType"` // 产品类型:当前产品种类  
26 - CurrTemp float64 `json:"CurrTemp"` // 当前温度:当前温度  
27 -}  
  1 +package models
  2 +
  3 +import "time"
  4 +
  5 +type DeviceCollection struct {
  6 + tableName string `comment:"设备采集数据" pg:"manufacture.device_collections,alias:device_collection"`
  7 + // 数据采集ID
  8 + DeviceCollectionId int64 `comment:"数据采集ID" pg:"pk:device_collection_id"`
  9 + // 车间名
  10 + WorkShopName string `comment:"车间名"`
  11 + // 设备名
  12 + DeviceSn string `comment:"设备名"`
  13 + // 设备类型
  14 + DeviceType string `comment:"设备类型"`
  15 + // 启动状态 1-启动 0-停止
  16 + StartupStatus int64 `comment:"启动状态 1-启动 0-停止" pg:",use_zero"`
  17 + // 通讯状态 1-通讯正常 0-设备未上电或与采集端通讯故障
  18 + ComStatus int64 `comment:"通讯状态 1-通讯正常 0-设备未上电或与采集端通讯故障" pg:",use_zero"`
  19 + // 设备数据值
  20 + Values interface{} `comment:"设备数据值"`
  21 + // 创建时间
  22 + CreatedAt time.Time `comment:"创建时间" pg:",default:now()"`
  23 + // 更新时间
  24 + UpdatedAt time.Time `comment:"更新时间" pg:",default:now()"`
  25 + // 删除时间
  26 + DeletedAt time.Time `pg:",soft_delete" comment:"删除时间"`
  27 +}
  1 +package transform
  2 +
  3 +import (
  4 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
  6 +)
  7 +
  8 +func TransformToDeviceCollectionDomainModelFromPgModels(deviceCollectionModel *models.DeviceCollection) (*domain.DeviceCollection, error) {
  9 + return &domain.DeviceCollection{
  10 + DeviceCollectionId: deviceCollectionModel.DeviceCollectionId,
  11 + WorkShopName: deviceCollectionModel.WorkShopName,
  12 + DeviceSn: deviceCollectionModel.DeviceSn,
  13 + StartupStatus: deviceCollectionModel.StartupStatus,
  14 + ComStatus: deviceCollectionModel.ComStatus,
  15 + Values: deviceCollectionModel.Values,
  16 + }, nil
  17 +}
  1 +package repository
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/go-pg/pg/v10"
  6 +
  7 + "github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
  8 + pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
  9 + "github.com/linmadan/egglib-go/utils/snowflake"
  10 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
  11 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/models"
  12 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg/transform"
  13 +)
  14 +
  15 +type DeviceCollectionRepository struct {
  16 + transactionContext *pgTransaction.TransactionContext
  17 +}
  18 +
  19 +func (repository *DeviceCollectionRepository) nextIdentify() (int64, error) {
  20 + IdWorker, err := snowflake.NewIdWorker(1)
  21 + if err != nil {
  22 + return 0, err
  23 + }
  24 + id, err := IdWorker.NextId()
  25 + return id, err
  26 +}
  27 +func (repository *DeviceCollectionRepository) Save(deviceCollection *domain.DeviceCollection) (*domain.DeviceCollection, error) {
  28 + sqlBuildFields := []string{
  29 + "device_collection_id",
  30 + "work_shop_name",
  31 + "device_sn",
  32 + "startup_status",
  33 + "com_status",
  34 + "values",
  35 + }
  36 + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
  37 + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
  38 + returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
  39 + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "deviceCollection_id")
  40 + updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
  41 + tx := repository.transactionContext.PgTx
  42 + if deviceCollection.Identify() == nil {
  43 + deviceCollectionId, err := repository.nextIdentify()
  44 + if err != nil {
  45 + return deviceCollection, err
  46 + } else {
  47 + deviceCollection.DeviceCollectionId = deviceCollectionId
  48 + }
  49 + if _, err := tx.QueryOne(
  50 + pg.Scan(
  51 + &deviceCollection.DeviceCollectionId,
  52 + &deviceCollection.WorkShopName,
  53 + &deviceCollection.DeviceSn,
  54 + &deviceCollection.StartupStatus,
  55 + &deviceCollection.ComStatus,
  56 + &deviceCollection.Values,
  57 + ),
  58 + fmt.Sprintf("INSERT INTO device_collections (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
  59 + deviceCollection.DeviceCollectionId,
  60 + deviceCollection.WorkShopName,
  61 + deviceCollection.DeviceSn,
  62 + deviceCollection.StartupStatus,
  63 + deviceCollection.ComStatus,
  64 + deviceCollection.Values,
  65 + ); err != nil {
  66 + return deviceCollection, err
  67 + }
  68 + } else {
  69 + if _, err := tx.QueryOne(
  70 + pg.Scan(
  71 + &deviceCollection.DeviceCollectionId,
  72 + &deviceCollection.WorkShopName,
  73 + &deviceCollection.DeviceSn,
  74 + &deviceCollection.StartupStatus,
  75 + &deviceCollection.ComStatus,
  76 + &deviceCollection.Values,
  77 + ),
  78 + fmt.Sprintf("UPDATE device_collections SET %s WHERE device_collection_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
  79 + deviceCollection.DeviceCollectionId,
  80 + deviceCollection.WorkShopName,
  81 + deviceCollection.DeviceSn,
  82 + deviceCollection.StartupStatus,
  83 + deviceCollection.ComStatus,
  84 + deviceCollection.Values,
  85 + deviceCollection.Identify(),
  86 + ); err != nil {
  87 + return deviceCollection, err
  88 + }
  89 + }
  90 + return deviceCollection, nil
  91 +}
  92 +func (repository *DeviceCollectionRepository) Remove(deviceCollection *domain.DeviceCollection) (*domain.DeviceCollection, error) {
  93 + tx := repository.transactionContext.PgTx
  94 + deviceCollectionModel := new(models.DeviceCollection)
  95 + deviceCollectionModel.DeviceCollectionId = deviceCollection.Identify().(int64)
  96 + if _, err := tx.Model(deviceCollectionModel).WherePK().Delete(); err != nil {
  97 + return deviceCollection, err
  98 + }
  99 + return deviceCollection, nil
  100 +}
  101 +func (repository *DeviceCollectionRepository) FindOne(queryOptions map[string]interface{}) (*domain.DeviceCollection, error) {
  102 + tx := repository.transactionContext.PgTx
  103 + deviceCollectionModel := new(models.DeviceCollection)
  104 + query := sqlbuilder.BuildQuery(tx.Model(deviceCollectionModel), queryOptions)
  105 + query.SetWhereByQueryOption("device_collection.device_collection_id = ?", "deviceCollectionId")
  106 + if err := query.First(); err != nil {
  107 + if err.Error() == "pg: no rows in result set" {
  108 + return nil, fmt.Errorf("没有此资源")
  109 + } else {
  110 + return nil, err
  111 + }
  112 + }
  113 + if deviceCollectionModel.DeviceCollectionId == 0 {
  114 + return nil, nil
  115 + } else {
  116 + return transform.TransformToDeviceCollectionDomainModelFromPgModels(deviceCollectionModel)
  117 + }
  118 +}
  119 +func (repository *DeviceCollectionRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.DeviceCollection, error) {
  120 + tx := repository.transactionContext.PgTx
  121 + var deviceCollectionModels []*models.DeviceCollection
  122 + deviceCollections := make([]*domain.DeviceCollection, 0)
  123 + query := sqlbuilder.BuildQuery(tx.Model(&deviceCollectionModels), queryOptions)
  124 + query.SetOffsetAndLimit(20)
  125 + query.SetOrderDirect("device_collection_id", "DESC")
  126 + if count, err := query.SelectAndCount(); err != nil {
  127 + return 0, deviceCollections, err
  128 + } else {
  129 + for _, deviceCollectionModel := range deviceCollectionModels {
  130 + if deviceCollection, err := transform.TransformToDeviceCollectionDomainModelFromPgModels(deviceCollectionModel); err != nil {
  131 + return 0, deviceCollections, err
  132 + } else {
  133 + deviceCollections = append(deviceCollections, deviceCollection)
  134 + }
  135 + }
  136 + return int64(count), deviceCollections, nil
  137 + }
  138 +}
  139 +func NewDeviceCollectionRepository(transactionContext *pgTransaction.TransactionContext) (*DeviceCollectionRepository, error) {
  140 + if transactionContext == nil {
  141 + return nil, fmt.Errorf("transactionContext参数不能为nil")
  142 + } else {
  143 + return &DeviceCollectionRepository{
  144 + transactionContext: transactionContext,
  145 + }, nil
  146 + }
  147 +}
@@ -352,3 +352,30 @@ func LoadQueryObject(queryOption map[string]interface{}, obj interface{}) error @@ -352,3 +352,30 @@ func LoadQueryObject(queryOption map[string]interface{}, obj interface{}) error
352 } 352 }
353 return err 353 return err
354 } 354 }
  355 +
  356 +// 字符串截取
  357 +func SubStr(str string, start, length int) string {
  358 + rs := []rune(str)
  359 + rl := len(rs)
  360 + end := 0
  361 + if start < 0 {
  362 + start = rl - 1 + start
  363 + }
  364 + end = start + length
  365 + if start > end {
  366 + start, end = end, start
  367 + }
  368 + if start < 0 {
  369 + start = 0
  370 + }
  371 + if start > rl {
  372 + start = rl
  373 + }
  374 + if end < 0 {
  375 + end = 0
  376 + }
  377 + if end > rl {
  378 + end = rl
  379 + }
  380 + return string(rs[start:end])
  381 +}
  1 +package controllers
  2 +
  3 +import (
  4 + "github.com/linmadan/egglib-go/web/beego"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/deviceCollection/command"
  6 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/deviceCollection/query"
  7 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/deviceCollection/service"
  8 +)
  9 +
  10 +type DeviceCollectionController struct {
  11 + beego.BaseController
  12 +}
  13 +
  14 +func (controller *DeviceCollectionController) CreateDeviceCollection() {
  15 + deviceCollectionService := service.NewDeviceCollectionService(nil)
  16 + createDeviceCollectionCommand := &command.CreateDeviceCollectionCommand{}
  17 + controller.Unmarshal(createDeviceCollectionCommand)
  18 + data, err := deviceCollectionService.CreateDeviceCollection(createDeviceCollectionCommand)
  19 + controller.Response(data, err)
  20 +}
  21 +
  22 +func (controller *DeviceCollectionController) UpdateDeviceCollection() {
  23 + deviceCollectionService := service.NewDeviceCollectionService(nil)
  24 + updateDeviceCollectionCommand := &command.UpdateDeviceCollectionCommand{}
  25 + controller.Unmarshal(updateDeviceCollectionCommand)
  26 + //Id, _ := controller.GetString(":Id")
  27 + //updateDeviceCollectionCommand.Id = Id
  28 + data, err := deviceCollectionService.UpdateDeviceCollection(updateDeviceCollectionCommand)
  29 + controller.Response(data, err)
  30 +}
  31 +
  32 +func (controller *DeviceCollectionController) GetDeviceCollection() {
  33 + deviceCollectionService := service.NewDeviceCollectionService(nil)
  34 + getDeviceCollectionQuery := &query.GetDeviceCollectionQuery{}
  35 + //Id, _ := controller.GetString(":Id")
  36 + //getDeviceCollectionQuery.Id = Id
  37 + data, err := deviceCollectionService.GetDeviceCollection(getDeviceCollectionQuery)
  38 + controller.Response(data, err)
  39 +}
  40 +
  41 +func (controller *DeviceCollectionController) RemoveDeviceCollection() {
  42 + deviceCollectionService := service.NewDeviceCollectionService(nil)
  43 + removeDeviceCollectionCommand := &command.RemoveDeviceCollectionCommand{}
  44 + controller.Unmarshal(removeDeviceCollectionCommand)
  45 + //Id, _ := controller.GetString(":Id")
  46 + //removeDeviceCollectionCommand.Id = Id
  47 + data, err := deviceCollectionService.RemoveDeviceCollection(removeDeviceCollectionCommand)
  48 + controller.Response(data, err)
  49 +}
  50 +
  51 +func (controller *DeviceCollectionController) ListDeviceCollection() {
  52 + deviceCollectionService := service.NewDeviceCollectionService(nil)
  53 + listDeviceCollectionQuery := &query.ListDeviceCollectionQuery{}
  54 + offset, _ := controller.GetInt("offset")
  55 + listDeviceCollectionQuery.Offset = offset
  56 + limit, _ := controller.GetInt("limit")
  57 + listDeviceCollectionQuery.Limit = limit
  58 + data, err := deviceCollectionService.ListDeviceCollection(listDeviceCollectionQuery)
  59 + controller.Response(data, err)
  60 +}
  1 +package routers
  2 +
  3 +import (
  4 + "github.com/beego/beego/v2/server/web"
  5 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego/controllers"
  6 +)
  7 +
  8 +func init() {
  9 + web.Router("/device-collections/", &controllers.DeviceCollectionController{}, "Post:CreateDeviceCollection")
  10 + web.Router("/device-collections/:Id", &controllers.DeviceCollectionController{}, "Put:UpdateDeviceCollection")
  11 + web.Router("/device-collections/:Id", &controllers.DeviceCollectionController{}, "Get:GetDeviceCollection")
  12 + web.Router("/device-collections/:Id", &controllers.DeviceCollectionController{}, "Delete:RemoveDeviceCollection")
  13 + web.Router("/device-collections/", &controllers.DeviceCollectionController{}, "Get:ListDeviceCollection")
  14 +}
@@ -3,10 +3,12 @@ package mqtt @@ -3,10 +3,12 @@ package mqtt
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
5 pahomqtt "github.com/eclipse/paho.mqtt.golang" 5 pahomqtt "github.com/eclipse/paho.mqtt.golang"
  6 + "github.com/tidwall/gjson"
6 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant" 7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
7 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" 8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
8 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/mqtt" 9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/mqtt"
9 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis" 10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/redis"
  11 + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
10 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log" 12 "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
11 "time" 13 "time"
12 ) 14 )
@@ -24,35 +26,96 @@ func Start() { @@ -24,35 +26,96 @@ func Start() {
24 if key == "WorkShop" { 26 if key == "WorkShop" {
25 continue 27 continue
26 } 28 }
27 - deviceWorkShop := &domain.DeviceWorkShop{}  
28 mBytes, err := json.Marshal(item) 29 mBytes, err := json.Marshal(item)
29 if err != nil { 30 if err != nil {
30 continue 31 continue
31 } 32 }
32 - err = json.Unmarshal(mBytes, deviceWorkShop)  
33 - if err != nil {  
34 - continue  
35 - }  
36 - // 获取当前时间  
37 - deviceWorkShop.CurrTime = time.Now()  
38 - // 车间名称  
39 - deviceWorkShop.WorkShop = workShop.(string)  
40 - // 设备名称  
41 - deviceWorkShop.DeviceSn = key  
42 - workShopBytes, err := json.Marshal(deviceWorkShop)  
43 - if err != nil {  
44 - continue 33 + deviceCollection := &domain.DeviceCollection{
  34 + WorkShopName: workShop.(string),
  35 + DeviceSn: key,
  36 + CollectionTime: time.Now(),
  37 + StartupStatus: gjson.Get(string(message.Payload()), key+".StartupState").Int(),
  38 + ComStatus: gjson.Get(string(message.Payload()), key+".ComStatus").Int(),
45 } 39 }
46 - err = redis.GetRedis().LPush(constant.REDIS_WORKSHOP_KEY, string(workShopBytes)).Err()  
47 - if err != nil {  
48 - log.Logger.Error("车间设备数据加入redis失败:" + err.Error()) 40 + if utils.SubStr(key, 0, 4) == domain.DeviceTypeMianBaoXieJi {
  41 + deviceCollection.DeviceType = domain.DeviceTypeMianBaoXieJi
  42 + } else {
  43 + deviceType := utils.SubStr(key, 0, 3)
  44 + deviceCollection.DeviceType = deviceType
  45 + switch deviceType {
  46 + //包馅机
  47 + case domain.DeviceTypeBaoXianJi:
  48 + deviceBaoXianJi := &domain.DeviceBaoXianJi{}
  49 + err = json.Unmarshal(mBytes, deviceBaoXianJi)
  50 + if err != nil {
  51 + continue
  52 + }
  53 + deviceCollection.Values = deviceBaoXianJi
  54 + break
  55 + //油炸机
  56 + case domain.DeviceTypeYouZhaJi:
  57 + deviceYouZhaJi := &domain.DeviceYouZhaJi{}
  58 + err = json.Unmarshal(mBytes, deviceYouZhaJi)
  59 + if err != nil {
  60 + continue
  61 + }
  62 + deviceCollection.Values = deviceYouZhaJi
  63 + break
  64 + //串串机
  65 + case domain.DeviceTypeChuanChuanJi:
  66 + deviceChuanChuanJi := &domain.DeviceChuanChuanJi{}
  67 + err = json.Unmarshal(mBytes, deviceChuanChuanJi)
  68 + if err != nil {
  69 + continue
  70 + }
  71 + deviceCollection.Values = deviceChuanChuanJi
  72 + break
  73 + //速冻线
  74 + case domain.DeviceTypeSuDongXian:
  75 + deviceSuDongXian := &domain.DeviceSuDongXian{}
  76 + err = json.Unmarshal(mBytes, deviceSuDongXian)
  77 + if err != nil {
  78 + continue
  79 + }
  80 + deviceCollection.Values = deviceSuDongXian
  81 + break
  82 + //封口机
  83 + case domain.DeviceTypeFengKouJi:
  84 + deviceFengKouJi := &domain.DeviceFengKouJi{}
  85 + err = json.Unmarshal(mBytes, deviceFengKouJi)
  86 + if err != nil {
  87 + continue
  88 + }
  89 + deviceCollection.Values = deviceFengKouJi
  90 + break
  91 + //封箱机
  92 + case domain.DeviceTypeFengXiangJi:
  93 + deviceFengXiangJi := &domain.DeviceFengXiangJi{}
  94 + err = json.Unmarshal(mBytes, deviceFengXiangJi)
  95 + if err != nil {
  96 + continue
  97 + }
  98 + deviceCollection.Values = deviceFengXiangJi
  99 + break
  100 + //打浆机
  101 + case domain.DeviceTypeDaJiangJi:
  102 + default:
  103 + }
  104 + workShopBytes, err := json.Marshal(deviceCollection)
  105 + if err != nil {
  106 + continue
  107 + }
  108 + err = redis.GetRedis().LPush(constant.REDIS_WORKSHOP_KEY, string(workShopBytes)).Err()
  109 + if err != nil {
  110 + log.Logger.Error("车间设备数据加入redis失败:" + err.Error())
  111 + }
49 } 112 }
50 } 113 }
  114 + log.Logger.Info("MQTT", map[string]interface{}{
  115 + "Topic": message.Topic(),
  116 + "MessageId": message.MessageID(),
  117 + "Message": payload,
  118 + })
51 } 119 }
52 - log.Logger.Info("MQTT", map[string]interface{}{  
53 - "Topic": message.Topic(),  
54 - "MessageId": message.MessageID(),  
55 - "Message": payload,  
56 - })  
57 }) 120 })
58 } 121 }
  1 +package device_collection
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("创建", func() {
  13 + Describe("提交数据创建", func() {
  14 + Context("提交正确的新设备采集数据数据", func() {
  15 + It("返回设备采集数据数据", func() {
  16 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  17 + body := map[string]interface{}{
  18 + "deviceCollectionId": "int64",
  19 + "workShopName": "string",
  20 + "startupStatus": "int64",
  21 + "deviceSn": "string",
  22 + "comStatus": "int64",
  23 + "values": "string",
  24 + }
  25 + httpExpect.POST("/device-collections/").
  26 + WithJSON(body).
  27 + Expect().
  28 + Status(http.StatusOK).
  29 + JSON().
  30 + Object().
  31 + ContainsKey("code").ValueEqual("code", 0).
  32 + ContainsKey("msg").ValueEqual("msg", "ok").
  33 + ContainsKey("data").Value("data").Object().
  34 + ContainsKey("deviceCollectionId").ValueNotEqual("deviceCollectionId", BeZero())
  35 + })
  36 + })
  37 + })
  38 + AfterEach(func() {
  39 + _, err := pG.DB.Exec("DELETE FROM device_collections WHERE true")
  40 + Expect(err).NotTo(HaveOccurred())
  41 + })
  42 +})
  1 +package device_collection
  2 +
  3 +import (
  4 + "net/http"
  5 + "net/http/httptest"
  6 + "testing"
  7 +
  8 + "github.com/beego/beego/v2/server/web"
  9 + . "github.com/onsi/ginkgo"
  10 + . "github.com/onsi/gomega"
  11 + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application"
  12 + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
  13 + _ "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/port/beego"
  14 +)
  15 +
  16 +func TestDeviceCollection(t *testing.T) {
  17 + RegisterFailHandler(Fail)
  18 + RunSpecs(t, "Beego Port DeviceCollection Correlations Test Case Suite")
  19 +}
  20 +
  21 +var handler http.Handler
  22 +var server *httptest.Server
  23 +
  24 +var _ = BeforeSuite(func() {
  25 + handler = web.BeeApp.Handlers
  26 + server = httptest.NewServer(handler)
  27 +})
  28 +
  29 +var _ = AfterSuite(func() {
  30 + server.Close()
  31 +})
  1 +package device_collection
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("返回", func() {
  13 + var deviceCollectionId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&deviceCollectionId),
  17 + "INSERT INTO device_collections (device_collection_id, work_shop_name, device_sn, startup_status, com_status, values) VALUES (?, ?, ?, ?, ?, ?) RETURNING device_collection_id",
  18 + "testDeviceCollectionId", "testWorkShopName", "testDeviceSn", "testStartupStatus", "testComStatus", "testValues")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("根据deviceCollectionId参数返回设备采集数据", func() {
  22 + Context("传入有效的deviceCollectionId", func() {
  23 + It("返回设备采集数据数据", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + httpExpect.GET("/device-collections/{Id}").
  26 + Expect().
  27 + Status(http.StatusOK).
  28 + JSON().
  29 + Object().
  30 + ContainsKey("code").ValueEqual("code", 0).
  31 + ContainsKey("msg").ValueEqual("msg", "ok").
  32 + ContainsKey("data").Value("data").Object()
  33 + })
  34 + })
  35 + })
  36 + AfterEach(func() {
  37 + _, err := pG.DB.Exec("DELETE FROM device_collections WHERE true")
  38 + Expect(err).NotTo(HaveOccurred())
  39 + })
  40 +})
  1 +package device_collection
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("返回列表", func() {
  13 + var deviceCollectionId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&deviceCollectionId),
  17 + "INSERT INTO device_collections (device_collection_id, work_shop_name, device_sn, startup_status, com_status, values) VALUES (?, ?, ?, ?, ?, ?) RETURNING device_collection_id",
  18 + "testDeviceCollectionId", "testWorkShopName", "testDeviceSn", "testStartupStatus", "testComStatus", "testValues")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("根据参数返回设备采集数据列表", func() {
  22 + Context("传入有效的参数", func() {
  23 + It("返回设备采集数据数据列表", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + httpExpect.GET("/device-collections/").
  26 + WithQuery("offset", "int").
  27 + WithQuery("limit", "int").
  28 + Expect().
  29 + Status(http.StatusOK).
  30 + JSON().
  31 + Object().
  32 + ContainsKey("code").ValueEqual("code", 0).
  33 + ContainsKey("msg").ValueEqual("msg", "ok").
  34 + ContainsKey("data").Value("data").Object().
  35 + ContainsKey("count").ValueEqual("count", 1).
  36 + ContainsKey("deviceCollections").Value("deviceCollections").Array()
  37 + })
  38 + })
  39 + })
  40 + AfterEach(func() {
  41 + _, err := pG.DB.Exec("DELETE FROM device_collections WHERE true")
  42 + Expect(err).NotTo(HaveOccurred())
  43 + })
  44 +})
  1 +package device_collection
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("移除", func() {
  13 + var deviceCollectionId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&deviceCollectionId),
  17 + "INSERT INTO device_collections (device_collection_id, work_shop_name, device_sn, startup_status, com_status, values) VALUES (?, ?, ?, ?, ?, ?) RETURNING device_collection_id",
  18 + "testDeviceCollectionId", "testWorkShopName", "testDeviceSn", "testStartupStatus", "testComStatus", "testValues")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("根据参数移除", func() {
  22 + Context("传入有效的deviceCollectionId", func() {
  23 + It("返回被移除设备采集数据的数据", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + httpExpect.DELETE("/device-collections/{Id}").
  26 + Expect().
  27 + Status(http.StatusOK).
  28 + JSON().
  29 + Object().
  30 + ContainsKey("code").ValueEqual("code", 0).
  31 + ContainsKey("msg").ValueEqual("msg", "ok").
  32 + ContainsKey("data").Value("data").Object()
  33 + })
  34 + })
  35 + })
  36 + AfterEach(func() {
  37 + _, err := pG.DB.Exec("DELETE FROM device_collections WHERE true")
  38 + Expect(err).NotTo(HaveOccurred())
  39 + })
  40 +})
  1 +package device_collection
  2 +
  3 +import (
  4 + "net/http"
  5 +
  6 + "github.com/gavv/httpexpect"
  7 + . "github.com/onsi/ginkgo"
  8 + . "github.com/onsi/gomega"
  9 + pG "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/pg"
  10 +)
  11 +
  12 +var _ = Describe("更新", func() {
  13 + var deviceCollectionId int64
  14 + BeforeEach(func() {
  15 + _, err := pG.DB.QueryOne(
  16 + pg.Scan(&deviceCollectionId),
  17 + "INSERT INTO device_collections (device_collection_id, work_shop_name, device_sn, startup_status, com_status, values) VALUES (?, ?, ?, ?, ?, ?) RETURNING device_collection_id",
  18 + "testDeviceCollectionId", "testWorkShopName", "testDeviceSn", "testStartupStatus", "testComStatus", "testValues")
  19 + Expect(err).NotTo(HaveOccurred())
  20 + })
  21 + Describe("提交数据更新", func() {
  22 + Context("提交正确的设备采集数据数据", func() {
  23 + It("返回更新后的设备采集数据数据", func() {
  24 + httpExpect := httpexpect.New(GinkgoT(), server.URL)
  25 + body := map[string]interface{}{
  26 + "deviceCollectionId": "int64",
  27 + "workShopName": "string",
  28 + "startupStatus": "int64",
  29 + "deviceSn": "string",
  30 + "comStatus": "int64",
  31 + "values": "string",
  32 + }
  33 + httpExpect.PUT("/device-collections/{Id}").
  34 + WithJSON(body).
  35 + Expect().
  36 + Status(http.StatusOK).
  37 + JSON().
  38 + Object().
  39 + ContainsKey("code").ValueEqual("code", 0).
  40 + ContainsKey("msg").ValueEqual("msg", "ok").
  41 + ContainsKey("data").Value("data").Object().
  42 + ContainsKey("deviceCollectionId").ValueEqual("deviceCollectionId", deviceCollectionId)
  43 + })
  44 + })
  45 + })
  46 + AfterEach(func() {
  47 + _, err := pG.DB.Exec("DELETE FROM device_collections WHERE true")
  48 + Expect(err).NotTo(HaveOccurred())
  49 + })
  50 +})