正在显示
27 个修改的文件
包含
777 行增加
和
41 行删除
| @@ -350,7 +350,9 @@ func (deviceService *DeviceService) SearchDevice(operateInfo *domain.OperateInfo | @@ -350,7 +350,9 @@ func (deviceService *DeviceService) SearchDevice(operateInfo *domain.OperateInfo | ||
| 350 | for i := range devices { | 350 | for i := range devices { |
| 351 | item := devices[i] | 351 | item := devices[i] |
| 352 | newJobDto := &dto.DeviceDto{} | 352 | newJobDto := &dto.DeviceDto{} |
| 353 | - newJobDto.WorkStation = workshops.FindWorkStation(item.WorkStation.WorkshopId, item.WorkStation.LineId, item.WorkStation.SectionId) | 353 | + if item.WorkStation != nil && item.WorkStation.WorkshopId > 0 { |
| 354 | + newJobDto.WorkStation = workshops.FindWorkStationOrNil(item.WorkStation.WorkshopId, item.WorkStation.LineId, item.WorkStation.SectionId) | ||
| 355 | + } | ||
| 354 | newJobDto.LoadDto(item, operateInfo.OrgId) | 356 | newJobDto.LoadDto(item, operateInfo.OrgId) |
| 355 | result = append(result, newJobDto) | 357 | result = append(result, newJobDto) |
| 356 | } | 358 | } |
| @@ -117,3 +117,19 @@ func CreateProductPlanDispatchRecordRepository(options map[string]interface{}) ( | @@ -117,3 +117,19 @@ func CreateProductPlanDispatchRecordRepository(options map[string]interface{}) ( | ||
| 117 | } | 117 | } |
| 118 | return repository.NewProductPlanDispatchRecordRepository(transactionContext) | 118 | return repository.NewProductPlanDispatchRecordRepository(transactionContext) |
| 119 | } | 119 | } |
| 120 | + | ||
| 121 | +func CreateDeviceRunningRecordRepository(options map[string]interface{}) (domain.DeviceRunningRecordRepository, error) { | ||
| 122 | + var transactionContext *pg.TransactionContext | ||
| 123 | + if value, ok := options["transactionContext"]; ok { | ||
| 124 | + transactionContext = value.(*pg.TransactionContext) | ||
| 125 | + } | ||
| 126 | + return repository.NewDeviceRunningRecordRepository(transactionContext) | ||
| 127 | +} | ||
| 128 | + | ||
| 129 | +func CreateDeviceDailyRunningRecordRepository(options map[string]interface{}) (domain.DeviceDailyRunningRecordRepository, error) { | ||
| 130 | + var transactionContext *pg.TransactionContext | ||
| 131 | + if value, ok := options["transactionContext"]; ok { | ||
| 132 | + transactionContext = value.(*pg.TransactionContext) | ||
| 133 | + } | ||
| 134 | + return repository.NewDeviceDailyRunningRecordRepository(transactionContext) | ||
| 135 | +} |
| @@ -25,7 +25,7 @@ type CreateProductGroupCommand struct { | @@ -25,7 +25,7 @@ type CreateProductGroupCommand struct { | ||
| 25 | // 班组长Id | 25 | // 班组长Id |
| 26 | GroupLeaderId int `cname:"班组长" json:"groupLeaderId,omitempty"` | 26 | GroupLeaderId int `cname:"班组长" json:"groupLeaderId,omitempty"` |
| 27 | // 帮组成员列表 | 27 | // 帮组成员列表 |
| 28 | - GroupMembers []int `cname:"帮组成员列表" json:"groupMembers" valid:"Required"` | 28 | + GroupMembers []int `cname:"帮组成员列表" json:"groupMembers"` |
| 29 | // 上班班次 1:全天 2:白班 4:中班 8:夜班 | 29 | // 上班班次 1:全天 2:白班 4:中班 8:夜班 |
| 30 | WorkOn int `cname:"上班班次 1:全天 2:白班 4:中班 8:夜班" json:"workOn" valid:"Required"` | 30 | WorkOn int `cname:"上班班次 1:全天 2:白班 4:中班 8:夜班" json:"workOn" valid:"Required"` |
| 31 | } | 31 | } |
| @@ -22,7 +22,7 @@ type UpdateProductGroupCommand struct { | @@ -22,7 +22,7 @@ type UpdateProductGroupCommand struct { | ||
| 22 | // 班组长Id | 22 | // 班组长Id |
| 23 | GroupLeaderId int `cname:"班组长" json:"groupLeaderId,omitempty"` | 23 | GroupLeaderId int `cname:"班组长" json:"groupLeaderId,omitempty"` |
| 24 | // 帮组成员列表 | 24 | // 帮组成员列表 |
| 25 | - GroupMembers []int `cname:"帮组成员列表" json:"groupMembers" valid:"Required"` | 25 | + GroupMembers []int `cname:"帮组成员列表" json:"groupMembers"` |
| 26 | // 上班班次 1:全天 2:白班 4:中班 8:夜班 | 26 | // 上班班次 1:全天 2:白班 4:中班 8:夜班 |
| 27 | WorkOn int `cname:"上班班次 1:全天 2:白班 4:中班 8:夜班" json:"workOn" valid:"Required"` | 27 | WorkOn int `cname:"上班班次 1:全天 2:白班 4:中班 8:夜班" json:"workOn" valid:"Required"` |
| 28 | } | 28 | } |
| @@ -16,9 +16,9 @@ type ProductGroupDto struct { | @@ -16,9 +16,9 @@ type ProductGroupDto struct { | ||
| 16 | // 班组名称 | 16 | // 班组名称 |
| 17 | GroupName string `json:"groupName,omitempty"` | 17 | GroupName string `json:"groupName,omitempty"` |
| 18 | // 班组长 | 18 | // 班组长 |
| 19 | - GroupLeader string `json:"groupLeader,omitempty"` | 19 | + GroupLeader string `json:"groupLeader"` |
| 20 | // 帮组成员列表 | 20 | // 帮组成员列表 |
| 21 | - GroupMembers string `json:"groupMembers,omitempty"` | 21 | + GroupMembers string `json:"groupMembers"` |
| 22 | // 上班班次 1:全天 2:白班 4:中班 8:夜班 | 22 | // 上班班次 1:全天 2:白班 4:中班 8:夜班 |
| 23 | WorkOn int `json:"workOn,omitempty"` | 23 | WorkOn int `json:"workOn,omitempty"` |
| 24 | // 工作位置 | 24 | // 工作位置 |
| @@ -32,7 +32,9 @@ type ProductGroupDto struct { | @@ -32,7 +32,9 @@ type ProductGroupDto struct { | ||
| 32 | func (d *ProductGroupDto) LoadDto(m *domain.ProductGroup, orgId int) *ProductGroupDto { | 32 | func (d *ProductGroupDto) LoadDto(m *domain.ProductGroup, orgId int) *ProductGroupDto { |
| 33 | d.ProductGroupId = m.ProductGroupId | 33 | d.ProductGroupId = m.ProductGroupId |
| 34 | d.GroupName = m.GroupName | 34 | d.GroupName = m.GroupName |
| 35 | - d.GroupLeader = m.GroupLeader.UserName | 35 | + if m.GroupLeader != nil { |
| 36 | + d.GroupLeader = m.GroupLeader.UserName | ||
| 37 | + } | ||
| 36 | var members []string | 38 | var members []string |
| 37 | for i := range m.GroupMembers { | 39 | for i := range m.GroupMembers { |
| 38 | members = append(members, m.GroupMembers[i].UserName) | 40 | members = append(members, m.GroupMembers[i].UserName) |
| @@ -45,15 +45,19 @@ func (productGroupService *ProductGroupService) CreateProductGroup(operateInfo * | @@ -45,15 +45,19 @@ func (productGroupService *ProductGroupService) CreateProductGroup(operateInfo * | ||
| 45 | } | 45 | } |
| 46 | 46 | ||
| 47 | var leader *domain.User | 47 | var leader *domain.User |
| 48 | - var members []*domain.User | 48 | + var members = make([]*domain.User, 0) |
| 49 | userService := domainService.NewUserService() | 49 | userService := domainService.NewUserService() |
| 50 | - leader, err = userService.User(cmd.GroupLeaderId) | ||
| 51 | - if err != nil { | ||
| 52 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | 50 | + if cmd.GroupLeaderId > 0 { |
| 51 | + leader, err = userService.User(cmd.GroupLeaderId) | ||
| 52 | + if err != nil { | ||
| 53 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 54 | + } | ||
| 53 | } | 55 | } |
| 54 | - members, err = userService.Users(cmd.GroupMembers) | ||
| 55 | - if err != nil { | ||
| 56 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | 56 | + if len(cmd.GroupMembers) > 0 { |
| 57 | + members, err = userService.Users(cmd.GroupMembers) | ||
| 58 | + if err != nil { | ||
| 59 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 60 | + } | ||
| 57 | } | 61 | } |
| 58 | 62 | ||
| 59 | var org *domain.Org | 63 | var org *domain.Org |
| @@ -130,6 +134,9 @@ func (productGroupService *ProductGroupService) GetProductGroup(getProductGroupQ | @@ -130,6 +134,9 @@ func (productGroupService *ProductGroupService) GetProductGroup(getProductGroupQ | ||
| 130 | if err := transactionContext.CommitTransaction(); err != nil { | 134 | if err := transactionContext.CommitTransaction(); err != nil { |
| 131 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) | 135 | return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error()) |
| 132 | } | 136 | } |
| 137 | + //if productGroup.GroupLeader==nil{ | ||
| 138 | + // productGroup.GroupLeader = &domain.User{} | ||
| 139 | + //} | ||
| 133 | return productGroup, nil | 140 | return productGroup, nil |
| 134 | } | 141 | } |
| 135 | 142 | ||
| @@ -292,15 +299,19 @@ func (productGroupService *ProductGroupService) UpdateProductGroup(cmd *command. | @@ -292,15 +299,19 @@ func (productGroupService *ProductGroupService) UpdateProductGroup(cmd *command. | ||
| 292 | } | 299 | } |
| 293 | 300 | ||
| 294 | var leader *domain.User | 301 | var leader *domain.User |
| 295 | - var members []*domain.User | 302 | + var members = make([]*domain.User, 0) |
| 296 | userService := domainService.NewUserService() | 303 | userService := domainService.NewUserService() |
| 297 | - leader, err = userService.User(cmd.GroupLeaderId) | ||
| 298 | - if err != nil { | ||
| 299 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | 304 | + if cmd.GroupLeaderId > 0 { |
| 305 | + leader, err = userService.User(cmd.GroupLeaderId) | ||
| 306 | + if err != nil { | ||
| 307 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 308 | + } | ||
| 300 | } | 309 | } |
| 301 | - members, err = userService.Users(cmd.GroupMembers) | ||
| 302 | - if err != nil { | ||
| 303 | - return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | 310 | + if len(cmd.GroupMembers) > 0 { |
| 311 | + members, err = userService.Users(cmd.GroupMembers) | ||
| 312 | + if err != nil { | ||
| 313 | + return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | ||
| 314 | + } | ||
| 304 | } | 315 | } |
| 305 | productGroup.GroupLeader = leader | 316 | productGroup.GroupLeader = leader |
| 306 | productGroup.GroupMembers = members | 317 | productGroup.GroupMembers = members |
| @@ -12,7 +12,7 @@ type SetOnlineCommand struct { | @@ -12,7 +12,7 @@ type SetOnlineCommand struct { | ||
| 12 | // 生产计划ID | 12 | // 生产计划ID |
| 13 | ProductPlanId int `cname:"生产计划ID" json:"productPlanId" valid:"Required"` | 13 | ProductPlanId int `cname:"生产计划ID" json:"productPlanId" valid:"Required"` |
| 14 | // 车间ID | 14 | // 车间ID |
| 15 | - WorkshopId int `cname:"车间ID" json:"workshopId" valid:"Required"` | 15 | + WorkshopId int `cname:"车间ID" json:"workshopId" ` |
| 16 | // 生产线ID | 16 | // 生产线ID |
| 17 | LineId int `cname:"生产线ID" json:"lineId" valid:"Required"` | 17 | LineId int `cname:"生产线ID" json:"lineId" valid:"Required"` |
| 18 | // 工段ID | 18 | // 工段ID |
| @@ -9,10 +9,10 @@ import ( | @@ -9,10 +9,10 @@ import ( | ||
| 9 | ) | 9 | ) |
| 10 | 10 | ||
| 11 | type SwitchCommand struct { | 11 | type SwitchCommand struct { |
| 12 | - // 下线计划调度ID | ||
| 13 | - FromProductPlanDispatchRecordId int `cname:"下线计划ID" json:"fromProductPlanId,omitempty"` | ||
| 14 | - // 上线计划ID | ||
| 15 | - ToProductPlanId int `cname:"上线计划ID" json:"toProductPlanId,omitempty"` | 12 | + // 已上线计划ID |
| 13 | + FromProductPlanDispatchRecordId int `cname:"已上线计划ID" json:"productPlanDispatchRecordId,omitempty" valid:"Required"` | ||
| 14 | + // 计划ID | ||
| 15 | + ToProductPlanId int `cname:"计划ID" json:"productPlanId,omitempty" valid:"Required"` | ||
| 16 | } | 16 | } |
| 17 | 17 | ||
| 18 | func (switchCommand *SwitchCommand) Valid(validation *validation.Validation) { | 18 | func (switchCommand *SwitchCommand) Valid(validation *validation.Validation) { |
| @@ -7,7 +7,7 @@ import ( | @@ -7,7 +7,7 @@ import ( | ||
| 7 | 7 | ||
| 8 | type ProductPlanDispatchRecordDto struct { | 8 | type ProductPlanDispatchRecordDto struct { |
| 9 | // 生产计划ID | 9 | // 生产计划ID |
| 10 | - ProductPlanId int `json:"productPlanId,omitempty"` | 10 | + ProductPlanId int `json:"productPlanDispatchRecordId,omitempty"` |
| 11 | // 批号 | 11 | // 批号 |
| 12 | BatchNumber string `json:"batchNumber,omitempty"` | 12 | BatchNumber string `json:"batchNumber,omitempty"` |
| 13 | // 生产日期 | 13 | // 生产日期 |
| @@ -28,6 +28,8 @@ type SearchProductPlanQuery struct { | @@ -28,6 +28,8 @@ type SearchProductPlanQuery struct { | ||
| 28 | BatchNumber string `cname:"批号" json:"batchNumber"` | 28 | BatchNumber string `cname:"批号" json:"batchNumber"` |
| 29 | // 车间名称 | 29 | // 车间名称 |
| 30 | WorkshopName string `cname:"车间名称" json:"workshopName"` | 30 | WorkshopName string `cname:"车间名称" json:"workshopName"` |
| 31 | + // 车间ID | ||
| 32 | + WorkshopId string `cname:"车间ID" json:"workshopId"` | ||
| 31 | } | 33 | } |
| 32 | 34 | ||
| 33 | func (cmd *SearchProductPlanQuery) Valid(validation *validation.Validation) { | 35 | func (cmd *SearchProductPlanQuery) Valid(validation *validation.Validation) { |
| @@ -395,6 +395,7 @@ func (productPlanService *ProductPlanService) SetOnline(cmd *command.SetOnlineCo | @@ -395,6 +395,7 @@ func (productPlanService *ProductPlanService) SetOnline(cmd *command.SetOnlineCo | ||
| 395 | return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | 395 | return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) |
| 396 | } | 396 | } |
| 397 | 397 | ||
| 398 | + cmd.WorkshopId = productPlan.Workshop.WorkshopId | ||
| 398 | var workStation *domain.WorkStation | 399 | var workStation *domain.WorkStation |
| 399 | _, workStation, err = factory.FastPgWorkstation(transactionContext, cmd.WorkshopId, cmd.LineId, cmd.SectionId) | 400 | _, workStation, err = factory.FastPgWorkstation(transactionContext, cmd.WorkshopId, cmd.LineId, cmd.SectionId) |
| 400 | if err != nil { | 401 | if err != nil { |
| @@ -575,7 +576,9 @@ func (productPlanService *ProductPlanService) SearchProductPlanOnlineDispatchRec | @@ -575,7 +576,9 @@ func (productPlanService *ProductPlanService) SearchProductPlanOnlineDispatchRec | ||
| 575 | } else { | 576 | } else { |
| 576 | productPlanRepository = value | 577 | productPlanRepository = value |
| 577 | } | 578 | } |
| 578 | - count, productPlans, err := productPlanRepository.Find(utils.ObjectToMap(cmd)) | 579 | + queryOptions := utils.ObjectToMap(cmd) |
| 580 | + //queryOptions["planDispatchStatus"] = 1 | ||
| 581 | + count, productPlans, err := productPlanRepository.Find(queryOptions) | ||
| 579 | if err != nil { | 582 | if err != nil { |
| 580 | return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) | 583 | return 0, nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error()) |
| 581 | } | 584 | } |
pkg/domain/device_daily_running_record.go
0 → 100644
| 1 | +package domain | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils" | ||
| 6 | + "time" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +const DefaultTimeWindow = 1 | ||
| 10 | + | ||
| 11 | +// 设备每日运行记录(汇总) | ||
| 12 | +type DeviceDailyRunningRecord struct { | ||
| 13 | + // 设备每日运行记录ID | ||
| 14 | + DeviceDailyRunningRecordId int `json:"deviceDailyRunningRecordId"` | ||
| 15 | + // 企业id | ||
| 16 | + CompanyId int `json:"companyId"` | ||
| 17 | + // 组织ID | ||
| 18 | + OrgId int `json:"orgId"` | ||
| 19 | + // 工作位置 | ||
| 20 | + WorkStation *WorkStation `json:"workStation"` | ||
| 21 | + // 设备Id | ||
| 22 | + DeviceId int `json:"deviceId"` | ||
| 23 | + // 设备编号 | ||
| 24 | + DeviceCode string `json:"deviceCode"` | ||
| 25 | + // 设备运行记录信息 | ||
| 26 | + DeviceRunningRecordInfo *DeviceRunningRecordInfo `json:"deviceRunningRecordInfo"` | ||
| 27 | + // 创建时间 | ||
| 28 | + CreatedAt time.Time `json:"createdAt"` | ||
| 29 | + // 更新时间 | ||
| 30 | + UpdatedAt time.Time `json:"updatedAt"` | ||
| 31 | + // 删除时间 | ||
| 32 | + DeletedAt time.Time `json:"deletedAt"` | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +type DeviceDailyRunningRecordRepository interface { | ||
| 36 | + Save(deviceDailyRunningRecord *DeviceDailyRunningRecord) (*DeviceDailyRunningRecord, error) | ||
| 37 | + Remove(deviceDailyRunningRecord *DeviceDailyRunningRecord) (*DeviceDailyRunningRecord, error) | ||
| 38 | + FindOne(queryOptions map[string]interface{}) (*DeviceDailyRunningRecord, error) | ||
| 39 | + Find(queryOptions map[string]interface{}) (int64, []*DeviceDailyRunningRecord, error) | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +func (deviceDailyRunningRecord *DeviceDailyRunningRecord) Identify() interface{} { | ||
| 43 | + if deviceDailyRunningRecord.DeviceDailyRunningRecordId == 0 { | ||
| 44 | + return nil | ||
| 45 | + } | ||
| 46 | + return deviceDailyRunningRecord.DeviceDailyRunningRecordId | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +func (deviceDailyRunningRecord *DeviceDailyRunningRecord) Update(data map[string]interface{}) error { | ||
| 50 | + return nil | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +// 设备运行记录信息 | ||
| 54 | +type DeviceRunningRecordInfo struct { | ||
| 55 | + // 当前状态 | ||
| 56 | + // bit0: 运行、停止 | ||
| 57 | + // bit1: 正常、故障 | ||
| 58 | + CurrentStatus int `json:"currentStatus"` | ||
| 59 | + // 设备OEE = tu * pu * qu | ||
| 60 | + OEE float64 `json:"oee"` | ||
| 61 | + // 时间利用率 TimeUtilization 运行时间 / (当前时间-当日零时) | ||
| 62 | + TimeUtilization float64 `json:"tu"` | ||
| 63 | + // 性能利用率 PerformanceUtilization | ||
| 64 | + // 1. 当前设备实际产出数量/理论数量(理论数量=60*60*12/标准工时) | ||
| 65 | + // 2. 没有数量100% | ||
| 66 | + PerformanceUtilization float64 `json:"pu"` | ||
| 67 | + // 合格率 QualificationUtilization | ||
| 68 | + // 1.按工段的合格率 | ||
| 69 | + // 2.默认100% | ||
| 70 | + QualificationUtilization float64 `json:"qu"` | ||
| 71 | + // 运行时长 单位:h | ||
| 72 | + UpTime float64 `json:"upTime"` | ||
| 73 | + // 生成数量 | ||
| 74 | + Count int `json:"count"` | ||
| 75 | + // 设备温度 单位:摄氏度 | ||
| 76 | + Temp float64 `json:"temp"` | ||
| 77 | + | ||
| 78 | + // 时间点 | ||
| 79 | + //TimeLine []string `json:"timeLine"` | ||
| 80 | + // 时间点对应的设备状态 按小时 1 | ||
| 81 | + TimeLineDeviceStatus map[string]*HourDeviceStatus `json:"timeLineDeviceStatus"` | ||
| 82 | +} | ||
| 83 | + | ||
| 84 | +func NewDeviceRunningRecordInfo() *DeviceRunningRecordInfo { | ||
| 85 | + return &DeviceRunningRecordInfo{ | ||
| 86 | + TimeLineDeviceStatus: make(map[string]*HourDeviceStatus), | ||
| 87 | + } | ||
| 88 | +} | ||
| 89 | +func (d *DeviceRunningRecordInfo) AddDeviceRunningData(t time.Time, data *DeviceRunningData) { | ||
| 90 | + d.CurrentStatus = data.StartupState | (1 << data.ComStatus) | ||
| 91 | + d.ResetUpTime() | ||
| 92 | + d.Count += data.Count | ||
| 93 | + //d.Temp = data.FrontTemp | ||
| 94 | + if data.Temp1 > 0 { | ||
| 95 | + d.Temp = data.Temp1 | ||
| 96 | + } else if data.FrontTemp > 0 { | ||
| 97 | + d.Temp = data.FrontTemp | ||
| 98 | + } | ||
| 99 | + d.AddTimeLineDeviceStatus(t, data) | ||
| 100 | + | ||
| 101 | + //d.OEE | ||
| 102 | + d.TimeUtilization = d.UpTime * 100 / (time.Now().Sub(utils.GetZeroTime(time.Now())).Hours()) | ||
| 103 | + //d.PerformanceUtilization | ||
| 104 | + //d.QualificationUtilization | ||
| 105 | +} | ||
| 106 | + | ||
| 107 | +// 添加新的设备状态 | ||
| 108 | +func (d *DeviceRunningRecordInfo) AddTimeLineDeviceStatus(t time.Time, data *DeviceRunningData) { | ||
| 109 | + if t.IsZero() { | ||
| 110 | + return | ||
| 111 | + } | ||
| 112 | + key := fmt.Sprintf("%v", t.Hour()) | ||
| 113 | + var v *HourDeviceStatus | ||
| 114 | + var ok bool | ||
| 115 | + if v, ok = d.TimeLineDeviceStatus[key]; !ok { | ||
| 116 | + v = NewHourDeviceStatus() | ||
| 117 | + d.TimeLineDeviceStatus[key] = v | ||
| 118 | + } | ||
| 119 | + v.UpdateUp(t, data.StartupState) | ||
| 120 | + v.UpdateCom(t, data.ComStatus) | ||
| 121 | +} | ||
| 122 | + | ||
| 123 | +// 重置运行时长 | ||
| 124 | +func (d *DeviceRunningRecordInfo) ResetUpTime() float64 { | ||
| 125 | + var upTime float64 | ||
| 126 | + for _, v := range d.TimeLineDeviceStatus { | ||
| 127 | + t := v.CountTime(v.Up) | ||
| 128 | + upTime += t.Hours() | ||
| 129 | + } | ||
| 130 | + d.UpTime = upTime | ||
| 131 | + return upTime | ||
| 132 | +} | ||
| 133 | + | ||
| 134 | +// 重置设备运行OEE | ||
| 135 | +func (d *DeviceRunningRecordInfo) ResetOEE(pu, qu float64) float64 { | ||
| 136 | + d.PerformanceUtilization = pu | ||
| 137 | + d.QualificationUtilization = qu | ||
| 138 | + d.OEE = (d.TimeUtilization + d.PerformanceUtilization + d.QualificationUtilization) / 3 | ||
| 139 | + return d.OEE | ||
| 140 | +} | ||
| 141 | + | ||
| 142 | +// 单个小时内的设备状态 | ||
| 143 | +type HourDeviceStatus struct { | ||
| 144 | + // 时间窗口 1-60 代表时间段范围 | ||
| 145 | + // 例如: w=1 则标识下面的状态按1分钟记录一次状态 | ||
| 146 | + // up 启动 bit0-bit59的位用来存启动状态 1:启动 0:关闭 | ||
| 147 | + // com通讯 bit0-bit59的位用来存通讯状态 1:正常 0:故障 | ||
| 148 | + // 如果 w=5 表示按5分钟记录一次状态 使用到 bit0-bit11 | ||
| 149 | + Window int `json:"w"` | ||
| 150 | + // 启动 | ||
| 151 | + // bit0:1 代表 | ||
| 152 | + Up int `json:"up"` | ||
| 153 | + // 通讯 | ||
| 154 | + Com int `json:"com"` | ||
| 155 | +} | ||
| 156 | + | ||
| 157 | +// 更新启动状态 | ||
| 158 | +func (d *HourDeviceStatus) UpdateUp(t time.Time, up int) { | ||
| 159 | + m := t.Minute() | ||
| 160 | + bit := 1 << (m / d.Window) | ||
| 161 | + if up&1 == 0 { | ||
| 162 | + return | ||
| 163 | + } | ||
| 164 | + if d.Up&bit > 0 { | ||
| 165 | + return | ||
| 166 | + } | ||
| 167 | + d.Up |= bit | ||
| 168 | + return | ||
| 169 | +} | ||
| 170 | + | ||
| 171 | +// 更新通讯状态 | ||
| 172 | +func (d *HourDeviceStatus) UpdateCom(t time.Time, c int) { | ||
| 173 | + m := t.Minute() | ||
| 174 | + bit := 1 << (m / d.Window) | ||
| 175 | + if c&1 == 0 { | ||
| 176 | + return | ||
| 177 | + } | ||
| 178 | + if d.Com&bit > 0 { | ||
| 179 | + return | ||
| 180 | + } | ||
| 181 | + d.Com |= bit | ||
| 182 | + return | ||
| 183 | +} | ||
| 184 | + | ||
| 185 | +// 计算状态持续的时间 | ||
| 186 | +func (d *HourDeviceStatus) CountTime(v int) time.Duration { | ||
| 187 | + l := 60 / d.Window | ||
| 188 | + count := 0 | ||
| 189 | + index := 1 | ||
| 190 | + for i := 0; i < l; i++ { | ||
| 191 | + if index&v > 0 { | ||
| 192 | + count++ | ||
| 193 | + } | ||
| 194 | + index <<= 1 | ||
| 195 | + } | ||
| 196 | + return time.Duration(d.Window*count) * time.Minute / time.Hour | ||
| 197 | +} | ||
| 198 | + | ||
| 199 | +func NewHourDeviceStatus() *HourDeviceStatus { | ||
| 200 | + return &HourDeviceStatus{ | ||
| 201 | + Window: DefaultTimeWindow, | ||
| 202 | + Up: 0, | ||
| 203 | + Com: 0, | ||
| 204 | + } | ||
| 205 | +} |
pkg/domain/device_running_data.go
0 → 100644
| 1 | +package domain | ||
| 2 | + | ||
| 3 | +// 设备运行数据 | ||
| 4 | +type DeviceRunningData struct { | ||
| 5 | + // 启动状态:1:启动,0:停止 | ||
| 6 | + StartupState int `json:"startupState"` | ||
| 7 | + // 通讯状态:1:通讯正常,0:设备未上电或与采集端通讯故障 | ||
| 8 | + ComStatus int `json:"comStatus"` | ||
| 9 | + // 匹配数目 | ||
| 10 | + Count int `json:"count,string"` | ||
| 11 | + // 炸机前段温度:炸机前段当前温度 YZJ1 油炸机 | ||
| 12 | + FrontTemp float64 `json:"frontTemp"` | ||
| 13 | + // 炸机前段温度:炸机前段当前温度 YZJ2 油炸机 | ||
| 14 | + Temp1 float64 `json:"temp1"` | ||
| 15 | + // 当前产品种类(产品编号) | ||
| 16 | + ProductType string `json:"productType"` | ||
| 17 | + // 设备编号 | ||
| 18 | + DeviceCode string `json:"deviceCode"` | ||
| 19 | +} |
pkg/domain/device_running_record.go
0 → 100644
| 1 | +package domain | ||
| 2 | + | ||
| 3 | +import "time" | ||
| 4 | + | ||
| 5 | +// 设备运行记录 | ||
| 6 | +type DeviceRunningRecord struct { | ||
| 7 | + // 设备运行记录ID | ||
| 8 | + DeviceRunningRecordId int `json:"deviceRunningRecordId"` | ||
| 9 | + // 企业id | ||
| 10 | + CompanyId int `json:"companyId"` | ||
| 11 | + // 组织ID | ||
| 12 | + OrgId int `json:"orgId"` | ||
| 13 | + // 工作位置 | ||
| 14 | + WorkStation *WorkStation `json:"workStation"` | ||
| 15 | + // 设备Id | ||
| 16 | + DeviceId int `json:"deviceId"` | ||
| 17 | + // 设备编号 | ||
| 18 | + DeviceCode string `json:"deviceCode"` | ||
| 19 | + // 设备运行记录信息 | ||
| 20 | + DeviceRunningRecordInfo string `json:"deviceRunningRecordInfo"` | ||
| 21 | + // 创建时间 | ||
| 22 | + CreatedAt time.Time `json:"createdAt"` | ||
| 23 | +} | ||
| 24 | + | ||
| 25 | +type DeviceRunningRecordRepository interface { | ||
| 26 | + Save(deviceRunningRecord *DeviceRunningRecord) (*DeviceRunningRecord, error) | ||
| 27 | + Remove(deviceRunningRecord *DeviceRunningRecord) (*DeviceRunningRecord, error) | ||
| 28 | + FindOne(queryOptions map[string]interface{}) (*DeviceRunningRecord, error) | ||
| 29 | + Find(queryOptions map[string]interface{}) (int64, []*DeviceRunningRecord, error) | ||
| 30 | +} | ||
| 31 | + | ||
| 32 | +func (deviceRunningRecord *DeviceRunningRecord) Identify() interface{} { | ||
| 33 | + if deviceRunningRecord.DeviceRunningRecordId == 0 { | ||
| 34 | + return nil | ||
| 35 | + } | ||
| 36 | + return deviceRunningRecord.DeviceRunningRecordId | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +func (deviceRunningRecord *DeviceRunningRecord) Update(data map[string]interface{}) error { | ||
| 40 | + return nil | ||
| 41 | +} |
| @@ -4,8 +4,8 @@ package domain | @@ -4,8 +4,8 @@ package domain | ||
| 4 | type ProductRecordInfo struct { | 4 | type ProductRecordInfo struct { |
| 5 | // 生产日期 | 5 | // 生产日期 |
| 6 | ProductDate string `json:"productDate"` | 6 | ProductDate string `json:"productDate"` |
| 7 | - // 原始上报数据 | ||
| 8 | - OriginalWeigh float64 `json:"originalWeigh,omitempty"` | 7 | + // 原始上报数据(kg,串) |
| 8 | + Original float64 `json:"original,omitempty"` | ||
| 9 | // 换算后的产能 | 9 | // 换算后的产能 |
| 10 | Weigh float64 `json:"weigh"` | 10 | Weigh float64 `json:"weigh"` |
| 11 | // 产能 - 审核前 | 11 | // 产能 - 审核前 |
| @@ -9,9 +9,9 @@ const ( | @@ -9,9 +9,9 @@ const ( | ||
| 9 | // 用户对象 | 9 | // 用户对象 |
| 10 | type User struct { | 10 | type User struct { |
| 11 | // 用户Id 用户唯一标识 | 11 | // 用户Id 用户唯一标识 |
| 12 | - UserId int `json:"userId,omitempty"` | 12 | + UserId int `json:"userId"` |
| 13 | // 用户姓名 | 13 | // 用户姓名 |
| 14 | - UserName string `json:"userName,omitempty"` | 14 | + UserName string `json:"userName"` |
| 15 | // 员工类型 1:固定 2:派遣 3.临时 | 15 | // 员工类型 1:固定 2:派遣 3.临时 |
| 16 | EmployeeType int `json:"employeeType,omitempty"` | 16 | EmployeeType int `json:"employeeType,omitempty"` |
| 17 | // IC卡号 | 17 | // IC卡号 |
| @@ -23,14 +23,29 @@ type WorkStation struct { | @@ -23,14 +23,29 @@ type WorkStation struct { | ||
| 23 | } | 23 | } |
| 24 | 24 | ||
| 25 | func NewWorkStation(w *Workshop, l *ProductLine, s *ProductSection) *WorkStation { | 25 | func NewWorkStation(w *Workshop, l *ProductLine, s *ProductSection) *WorkStation { |
| 26 | - return &WorkStation{ | ||
| 27 | - WorkStationId: fmt.Sprintf("%v.%v.%v", w.WorkshopId, l.LineId, s.SectionId), | ||
| 28 | - WorkshopId: w.WorkshopId, | ||
| 29 | - WorkshopName: w.WorkshopName, | ||
| 30 | - LineId: l.LineId, | ||
| 31 | - LineName: l.LineName, | ||
| 32 | - SectionId: s.SectionId, | ||
| 33 | - SectionName: s.SectionName, | 26 | + item := &WorkStation{ |
| 27 | + //WorkshopId: w.WorkshopId, | ||
| 28 | + //WorkshopName: w.WorkshopName, | ||
| 29 | + //LineId: l.LineId, | ||
| 30 | + //LineName: l.LineName, | ||
| 31 | + //SectionId: s.SectionId, | ||
| 32 | + //SectionName: s.SectionName, | ||
| 34 | //Principal: w.Principal, | 33 | //Principal: w.Principal, |
| 35 | } | 34 | } |
| 35 | + if w != nil && l != nil && s != nil { | ||
| 36 | + item.WorkStationId = fmt.Sprintf("%v.%v.%v", w.WorkshopId, l.LineId, s.SectionId) | ||
| 37 | + } | ||
| 38 | + if w != nil { | ||
| 39 | + item.WorkshopId = w.WorkshopId | ||
| 40 | + item.WorkshopName = w.WorkshopName | ||
| 41 | + } | ||
| 42 | + if l != nil { | ||
| 43 | + item.LineId = l.LineId | ||
| 44 | + item.LineName = l.LineName | ||
| 45 | + } | ||
| 46 | + if s != nil { | ||
| 47 | + item.SectionId = s.SectionId | ||
| 48 | + item.SectionName = s.SectionName | ||
| 49 | + } | ||
| 50 | + return item | ||
| 36 | } | 51 | } |
| @@ -16,6 +16,17 @@ func (m Workshops) FindWorkStation(workshopId, lineId, sectionId int) *WorkStati | @@ -16,6 +16,17 @@ func (m Workshops) FindWorkStation(workshopId, lineId, sectionId int) *WorkStati | ||
| 16 | return &WorkStation{} //返回空的对象 | 16 | return &WorkStation{} //返回空的对象 |
| 17 | } | 17 | } |
| 18 | 18 | ||
| 19 | +func (m Workshops) FindWorkStationOrNil(workshopId, lineId, sectionId int) *WorkStation { | ||
| 20 | + for i := range m { | ||
| 21 | + item := m[i] | ||
| 22 | + workstation, err := item.FindWorkStationOrNil(workshopId, lineId, sectionId) | ||
| 23 | + if err == nil && workstation != nil { | ||
| 24 | + return workstation | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | + return &WorkStation{} //返回空的对象 | ||
| 28 | +} | ||
| 29 | + | ||
| 19 | func (m Workshops) FindWorkshopsByName(workshopName string) []int { | 30 | func (m Workshops) FindWorkshopsByName(workshopName string) []int { |
| 20 | result := make([]int, 0) | 31 | result := make([]int, 0) |
| 21 | if len(workshopName) == 0 { | 32 | if len(workshopName) == 0 { |
| @@ -108,7 +108,7 @@ func (ptr *PGProductRecordService) SubmitProductRecord(productRecordType int, qu | @@ -108,7 +108,7 @@ func (ptr *PGProductRecordService) SubmitProductRecord(productRecordType int, qu | ||
| 108 | UpdatedAt: time.Now(), | 108 | UpdatedAt: time.Now(), |
| 109 | ProductRecordInfo: &domain.ProductRecordInfo{ | 109 | ProductRecordInfo: &domain.ProductRecordInfo{ |
| 110 | ProductDate: plan.ProductDate.Format("2006-01-02"), | 110 | ProductDate: plan.ProductDate.Format("2006-01-02"), |
| 111 | - OriginalWeigh: request.Weigh, | 111 | + Original: request.Weigh, |
| 112 | Weigh: weight, | 112 | Weigh: weight, |
| 113 | WeighBefore: weight, | 113 | WeighBefore: weight, |
| 114 | UnitConversionId: request.UnitConversionId, | 114 | UnitConversionId: request.UnitConversionId, |
| 1 | +package models | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
| 5 | + "time" | ||
| 6 | +) | ||
| 7 | + | ||
| 8 | +type DeviceDailyRunningRecord struct { | ||
| 9 | + tableName string `comment:"设备每日运行记录(汇总)" pg:"manufacture.device_daily_running_record"` | ||
| 10 | + // 设备每日运行记录ID | ||
| 11 | + DeviceDailyRunningRecordId int `comment:"设备每日运行记录ID" pg:"pk:device_daily_running_record_id"` | ||
| 12 | + // 企业id | ||
| 13 | + CompanyId int `comment:"企业id"` | ||
| 14 | + // 组织ID | ||
| 15 | + OrgId int `comment:"组织ID"` | ||
| 16 | + // 工作位置 | ||
| 17 | + WorkStation *domain.WorkStation `comment:"工作位置"` | ||
| 18 | + // 设备Id | ||
| 19 | + DeviceId int `comment:"设备Id"` | ||
| 20 | + // 设备编号 | ||
| 21 | + DeviceCode string `comment:"设备编号"` | ||
| 22 | + // 设备运行记录信息 | ||
| 23 | + DeviceRunningRecordInfo *domain.DeviceRunningRecordInfo `comment:"设备运行记录信息"` | ||
| 24 | + // 创建时间 | ||
| 25 | + CreatedAt time.Time `comment:"创建时间"` | ||
| 26 | + // 更新时间 | ||
| 27 | + UpdatedAt time.Time `comment:"更新时间"` | ||
| 28 | + // 删除时间 | ||
| 29 | + DeletedAt time.Time `comment:"删除时间"` | ||
| 30 | +} |
| 1 | +package models | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain" | ||
| 5 | + "time" | ||
| 6 | +) | ||
| 7 | + | ||
| 8 | +type DeviceRunningRecord struct { | ||
| 9 | + tableName string `comment:"设备运行记录" pg:"manufacture.device_running_record"` | ||
| 10 | + // 设备运行记录ID | ||
| 11 | + DeviceRunningRecordId int `comment:"设备运行记录ID" pg:"pk:device_running_record_id"` | ||
| 12 | + // 企业id | ||
| 13 | + CompanyId int `comment:"企业id"` | ||
| 14 | + // 组织ID | ||
| 15 | + OrgId int `comment:"组织ID"` | ||
| 16 | + // 工作位置 | ||
| 17 | + WorkStation *domain.WorkStation `comment:"工作位置"` | ||
| 18 | + // 设备Id | ||
| 19 | + DeviceId int `comment:"设备Id"` | ||
| 20 | + // 设备编号 | ||
| 21 | + DeviceCode string `comment:"设备编号"` | ||
| 22 | + // 设备运行记录信息 | ||
| 23 | + DeviceRunningRecordInfo string `comment:"设备运行记录信息"` | ||
| 24 | + // 创建时间 | ||
| 25 | + CreatedAt time.Time `comment:"创建时间"` | ||
| 26 | +} |
| 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 TransformToDeviceDailyRunningRecordDomainModelFromPgModels(deviceDailyRunningRecordModel *models.DeviceDailyRunningRecord) (*domain.DeviceDailyRunningRecord, error) { | ||
| 9 | + return &domain.DeviceDailyRunningRecord{ | ||
| 10 | + DeviceDailyRunningRecordId: deviceDailyRunningRecordModel.DeviceDailyRunningRecordId, | ||
| 11 | + CompanyId: deviceDailyRunningRecordModel.CompanyId, | ||
| 12 | + OrgId: deviceDailyRunningRecordModel.OrgId, | ||
| 13 | + WorkStation: deviceDailyRunningRecordModel.WorkStation, | ||
| 14 | + DeviceId: deviceDailyRunningRecordModel.DeviceId, | ||
| 15 | + DeviceCode: deviceDailyRunningRecordModel.DeviceCode, | ||
| 16 | + DeviceRunningRecordInfo: deviceDailyRunningRecordModel.DeviceRunningRecordInfo, | ||
| 17 | + CreatedAt: deviceDailyRunningRecordModel.CreatedAt, | ||
| 18 | + UpdatedAt: deviceDailyRunningRecordModel.UpdatedAt, | ||
| 19 | + DeletedAt: deviceDailyRunningRecordModel.DeletedAt, | ||
| 20 | + }, nil | ||
| 21 | +} |
| 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 TransformToDeviceRunningRecordDomainModelFromPgModels(deviceRunningRecordModel *models.DeviceRunningRecord) (*domain.DeviceRunningRecord, error) { | ||
| 9 | + return &domain.DeviceRunningRecord{ | ||
| 10 | + DeviceRunningRecordId: deviceRunningRecordModel.DeviceRunningRecordId, | ||
| 11 | + CompanyId: deviceRunningRecordModel.CompanyId, | ||
| 12 | + OrgId: deviceRunningRecordModel.OrgId, | ||
| 13 | + WorkStation: deviceRunningRecordModel.WorkStation, | ||
| 14 | + DeviceId: deviceRunningRecordModel.DeviceId, | ||
| 15 | + DeviceCode: deviceRunningRecordModel.DeviceCode, | ||
| 16 | + DeviceRunningRecordInfo: deviceRunningRecordModel.DeviceRunningRecordInfo, | ||
| 17 | + CreatedAt: deviceRunningRecordModel.CreatedAt, | ||
| 18 | + }, nil | ||
| 19 | +} |
| 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 DeviceDailyRunningRecordRepository struct { | ||
| 16 | + transactionContext *pgTransaction.TransactionContext | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +func (repository *DeviceDailyRunningRecordRepository) 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 *DeviceDailyRunningRecordRepository) Save(deviceDailyRunningRecord *domain.DeviceDailyRunningRecord) (*domain.DeviceDailyRunningRecord, error) { | ||
| 28 | + sqlBuildFields := []string{ | ||
| 29 | + "device_daily_running_record_id", | ||
| 30 | + "company_id", | ||
| 31 | + "org_id", | ||
| 32 | + "work_station", | ||
| 33 | + "device_id", | ||
| 34 | + "device_code", | ||
| 35 | + "device_running_record_info", | ||
| 36 | + "created_at", | ||
| 37 | + "updated_at", | ||
| 38 | + "deleted_at", | ||
| 39 | + } | ||
| 40 | + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields) | ||
| 41 | + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields) | ||
| 42 | + returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields) | ||
| 43 | + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "deviceDailyRunningRecord_id") | ||
| 44 | + updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields) | ||
| 45 | + tx := repository.transactionContext.PgTx | ||
| 46 | + if deviceDailyRunningRecord.Identify() == nil { | ||
| 47 | + if _, err := tx.QueryOne( | ||
| 48 | + pg.Scan( | ||
| 49 | + &deviceDailyRunningRecord.DeviceDailyRunningRecordId, | ||
| 50 | + &deviceDailyRunningRecord.CompanyId, | ||
| 51 | + &deviceDailyRunningRecord.OrgId, | ||
| 52 | + &deviceDailyRunningRecord.WorkStation, | ||
| 53 | + &deviceDailyRunningRecord.DeviceId, | ||
| 54 | + &deviceDailyRunningRecord.DeviceCode, | ||
| 55 | + &deviceDailyRunningRecord.DeviceRunningRecordInfo, | ||
| 56 | + &deviceDailyRunningRecord.CreatedAt, | ||
| 57 | + &deviceDailyRunningRecord.UpdatedAt, | ||
| 58 | + &deviceDailyRunningRecord.DeletedAt, | ||
| 59 | + ), | ||
| 60 | + fmt.Sprintf("INSERT INTO manufacture.device_daily_running_record (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet), | ||
| 61 | + deviceDailyRunningRecord.DeviceDailyRunningRecordId, | ||
| 62 | + deviceDailyRunningRecord.CompanyId, | ||
| 63 | + deviceDailyRunningRecord.OrgId, | ||
| 64 | + deviceDailyRunningRecord.WorkStation, | ||
| 65 | + deviceDailyRunningRecord.DeviceId, | ||
| 66 | + deviceDailyRunningRecord.DeviceCode, | ||
| 67 | + deviceDailyRunningRecord.DeviceRunningRecordInfo, | ||
| 68 | + deviceDailyRunningRecord.CreatedAt, | ||
| 69 | + deviceDailyRunningRecord.UpdatedAt, | ||
| 70 | + deviceDailyRunningRecord.DeletedAt, | ||
| 71 | + ); err != nil { | ||
| 72 | + return deviceDailyRunningRecord, err | ||
| 73 | + } | ||
| 74 | + } else { | ||
| 75 | + if _, err := tx.QueryOne( | ||
| 76 | + pg.Scan( | ||
| 77 | + &deviceDailyRunningRecord.DeviceDailyRunningRecordId, | ||
| 78 | + &deviceDailyRunningRecord.CompanyId, | ||
| 79 | + &deviceDailyRunningRecord.OrgId, | ||
| 80 | + &deviceDailyRunningRecord.WorkStation, | ||
| 81 | + &deviceDailyRunningRecord.DeviceId, | ||
| 82 | + &deviceDailyRunningRecord.DeviceCode, | ||
| 83 | + &deviceDailyRunningRecord.DeviceRunningRecordInfo, | ||
| 84 | + &deviceDailyRunningRecord.CreatedAt, | ||
| 85 | + &deviceDailyRunningRecord.UpdatedAt, | ||
| 86 | + &deviceDailyRunningRecord.DeletedAt, | ||
| 87 | + ), | ||
| 88 | + fmt.Sprintf("UPDATE manufacture.device_daily_running_record SET %s WHERE device_daily_running_record_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), | ||
| 89 | + deviceDailyRunningRecord.DeviceDailyRunningRecordId, | ||
| 90 | + deviceDailyRunningRecord.CompanyId, | ||
| 91 | + deviceDailyRunningRecord.OrgId, | ||
| 92 | + deviceDailyRunningRecord.WorkStation, | ||
| 93 | + deviceDailyRunningRecord.DeviceId, | ||
| 94 | + deviceDailyRunningRecord.DeviceCode, | ||
| 95 | + deviceDailyRunningRecord.DeviceRunningRecordInfo, | ||
| 96 | + deviceDailyRunningRecord.CreatedAt, | ||
| 97 | + deviceDailyRunningRecord.UpdatedAt, | ||
| 98 | + deviceDailyRunningRecord.DeletedAt, | ||
| 99 | + deviceDailyRunningRecord.Identify(), | ||
| 100 | + ); err != nil { | ||
| 101 | + return deviceDailyRunningRecord, err | ||
| 102 | + } | ||
| 103 | + } | ||
| 104 | + return deviceDailyRunningRecord, nil | ||
| 105 | +} | ||
| 106 | +func (repository *DeviceDailyRunningRecordRepository) Remove(deviceDailyRunningRecord *domain.DeviceDailyRunningRecord) (*domain.DeviceDailyRunningRecord, error) { | ||
| 107 | + tx := repository.transactionContext.PgTx | ||
| 108 | + deviceDailyRunningRecordModel := new(models.DeviceDailyRunningRecord) | ||
| 109 | + deviceDailyRunningRecordModel.DeviceDailyRunningRecordId = deviceDailyRunningRecord.Identify().(int) | ||
| 110 | + if _, err := tx.Model(deviceDailyRunningRecordModel).WherePK().Delete(); err != nil { | ||
| 111 | + return deviceDailyRunningRecord, err | ||
| 112 | + } | ||
| 113 | + return deviceDailyRunningRecord, nil | ||
| 114 | +} | ||
| 115 | +func (repository *DeviceDailyRunningRecordRepository) FindOne(queryOptions map[string]interface{}) (*domain.DeviceDailyRunningRecord, error) { | ||
| 116 | + tx := repository.transactionContext.PgTx | ||
| 117 | + deviceDailyRunningRecordModel := new(models.DeviceDailyRunningRecord) | ||
| 118 | + query := sqlbuilder.BuildQuery(tx.Model(deviceDailyRunningRecordModel), queryOptions) | ||
| 119 | + query.SetWhereByQueryOption("device_daily_running_record.device_daily_running_record_id = ?", "deviceDailyRunningRecordId") | ||
| 120 | + if err := query.First(); err != nil { | ||
| 121 | + if err.Error() == "pg: no rows in result set" { | ||
| 122 | + return nil, fmt.Errorf("没有此资源") | ||
| 123 | + } else { | ||
| 124 | + return nil, err | ||
| 125 | + } | ||
| 126 | + } | ||
| 127 | + if deviceDailyRunningRecordModel.DeviceDailyRunningRecordId == 0 { | ||
| 128 | + return nil, nil | ||
| 129 | + } else { | ||
| 130 | + return transform.TransformToDeviceDailyRunningRecordDomainModelFromPgModels(deviceDailyRunningRecordModel) | ||
| 131 | + } | ||
| 132 | +} | ||
| 133 | +func (repository *DeviceDailyRunningRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.DeviceDailyRunningRecord, error) { | ||
| 134 | + tx := repository.transactionContext.PgTx | ||
| 135 | + var deviceDailyRunningRecordModels []*models.DeviceDailyRunningRecord | ||
| 136 | + deviceDailyRunningRecords := make([]*domain.DeviceDailyRunningRecord, 0) | ||
| 137 | + query := sqlbuilder.BuildQuery(tx.Model(&deviceDailyRunningRecordModels), queryOptions) | ||
| 138 | + query.SetOffsetAndLimit(20) | ||
| 139 | + query.SetOrderDirect("device_daily_running_record_id", "DESC") | ||
| 140 | + if count, err := query.SelectAndCount(); err != nil { | ||
| 141 | + return 0, deviceDailyRunningRecords, err | ||
| 142 | + } else { | ||
| 143 | + for _, deviceDailyRunningRecordModel := range deviceDailyRunningRecordModels { | ||
| 144 | + if deviceDailyRunningRecord, err := transform.TransformToDeviceDailyRunningRecordDomainModelFromPgModels(deviceDailyRunningRecordModel); err != nil { | ||
| 145 | + return 0, deviceDailyRunningRecords, err | ||
| 146 | + } else { | ||
| 147 | + deviceDailyRunningRecords = append(deviceDailyRunningRecords, deviceDailyRunningRecord) | ||
| 148 | + } | ||
| 149 | + } | ||
| 150 | + return int64(count), deviceDailyRunningRecords, nil | ||
| 151 | + } | ||
| 152 | +} | ||
| 153 | +func NewDeviceDailyRunningRecordRepository(transactionContext *pgTransaction.TransactionContext) (*DeviceDailyRunningRecordRepository, error) { | ||
| 154 | + if transactionContext == nil { | ||
| 155 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
| 156 | + } else { | ||
| 157 | + return &DeviceDailyRunningRecordRepository{ | ||
| 158 | + transactionContext: transactionContext, | ||
| 159 | + }, nil | ||
| 160 | + } | ||
| 161 | +} |
| 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 DeviceRunningRecordRepository struct { | ||
| 16 | + transactionContext *pgTransaction.TransactionContext | ||
| 17 | +} | ||
| 18 | + | ||
| 19 | +func (repository *DeviceRunningRecordRepository) 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 *DeviceRunningRecordRepository) Save(deviceRunningRecord *domain.DeviceRunningRecord) (*domain.DeviceRunningRecord, error) { | ||
| 28 | + sqlBuildFields := []string{ | ||
| 29 | + "device_running_record_id", | ||
| 30 | + "company_id", | ||
| 31 | + "org_id", | ||
| 32 | + "work_station", | ||
| 33 | + "device_id", | ||
| 34 | + "device_code", | ||
| 35 | + "device_running_record_info", | ||
| 36 | + "created_at", | ||
| 37 | + } | ||
| 38 | + insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "device_running_record_id")) | ||
| 39 | + insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlbuilder.RemoveSqlFields(sqlBuildFields, "device_running_record_id")) | ||
| 40 | + returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields) | ||
| 41 | + updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "device_running_record_id") | ||
| 42 | + updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields) | ||
| 43 | + tx := repository.transactionContext.PgTx | ||
| 44 | + if deviceRunningRecord.Identify() == nil { | ||
| 45 | + if _, err := tx.QueryOne( | ||
| 46 | + pg.Scan( | ||
| 47 | + &deviceRunningRecord.DeviceRunningRecordId, | ||
| 48 | + &deviceRunningRecord.CompanyId, | ||
| 49 | + &deviceRunningRecord.OrgId, | ||
| 50 | + &deviceRunningRecord.WorkStation, | ||
| 51 | + &deviceRunningRecord.DeviceId, | ||
| 52 | + &deviceRunningRecord.DeviceCode, | ||
| 53 | + &deviceRunningRecord.DeviceRunningRecordInfo, | ||
| 54 | + &deviceRunningRecord.CreatedAt, | ||
| 55 | + ), | ||
| 56 | + fmt.Sprintf("INSERT INTO device_running_records (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet), | ||
| 57 | + //deviceRunningRecord.DeviceRunningRecordId, | ||
| 58 | + deviceRunningRecord.CompanyId, | ||
| 59 | + deviceRunningRecord.OrgId, | ||
| 60 | + deviceRunningRecord.WorkStation, | ||
| 61 | + deviceRunningRecord.DeviceId, | ||
| 62 | + deviceRunningRecord.DeviceCode, | ||
| 63 | + deviceRunningRecord.DeviceRunningRecordInfo, | ||
| 64 | + deviceRunningRecord.CreatedAt, | ||
| 65 | + ); err != nil { | ||
| 66 | + return deviceRunningRecord, err | ||
| 67 | + } | ||
| 68 | + } else { | ||
| 69 | + if _, err := tx.QueryOne( | ||
| 70 | + pg.Scan( | ||
| 71 | + &deviceRunningRecord.DeviceRunningRecordId, | ||
| 72 | + &deviceRunningRecord.CompanyId, | ||
| 73 | + &deviceRunningRecord.OrgId, | ||
| 74 | + &deviceRunningRecord.WorkStation, | ||
| 75 | + &deviceRunningRecord.DeviceId, | ||
| 76 | + &deviceRunningRecord.DeviceCode, | ||
| 77 | + &deviceRunningRecord.DeviceRunningRecordInfo, | ||
| 78 | + &deviceRunningRecord.CreatedAt, | ||
| 79 | + ), | ||
| 80 | + fmt.Sprintf("UPDATE device_running_records SET %s WHERE device_running_record_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet), | ||
| 81 | + //deviceRunningRecord.DeviceRunningRecordId, | ||
| 82 | + deviceRunningRecord.CompanyId, | ||
| 83 | + deviceRunningRecord.OrgId, | ||
| 84 | + deviceRunningRecord.WorkStation, | ||
| 85 | + deviceRunningRecord.DeviceId, | ||
| 86 | + deviceRunningRecord.DeviceCode, | ||
| 87 | + deviceRunningRecord.DeviceRunningRecordInfo, | ||
| 88 | + deviceRunningRecord.CreatedAt, | ||
| 89 | + deviceRunningRecord.Identify(), | ||
| 90 | + ); err != nil { | ||
| 91 | + return deviceRunningRecord, err | ||
| 92 | + } | ||
| 93 | + } | ||
| 94 | + return deviceRunningRecord, nil | ||
| 95 | +} | ||
| 96 | +func (repository *DeviceRunningRecordRepository) Remove(deviceRunningRecord *domain.DeviceRunningRecord) (*domain.DeviceRunningRecord, error) { | ||
| 97 | + tx := repository.transactionContext.PgTx | ||
| 98 | + deviceRunningRecordModel := new(models.DeviceRunningRecord) | ||
| 99 | + deviceRunningRecordModel.DeviceRunningRecordId = deviceRunningRecord.Identify().(int) | ||
| 100 | + if _, err := tx.Model(deviceRunningRecordModel).WherePK().Delete(); err != nil { | ||
| 101 | + return deviceRunningRecord, err | ||
| 102 | + } | ||
| 103 | + return deviceRunningRecord, nil | ||
| 104 | +} | ||
| 105 | +func (repository *DeviceRunningRecordRepository) FindOne(queryOptions map[string]interface{}) (*domain.DeviceRunningRecord, error) { | ||
| 106 | + tx := repository.transactionContext.PgTx | ||
| 107 | + deviceRunningRecordModel := new(models.DeviceRunningRecord) | ||
| 108 | + query := sqlbuilder.BuildQuery(tx.Model(deviceRunningRecordModel), queryOptions) | ||
| 109 | + query.SetWhereByQueryOption("device_running_record.device_running_record_id = ?", "deviceRunningRecordId") | ||
| 110 | + if err := query.First(); err != nil { | ||
| 111 | + if err.Error() == "pg: no rows in result set" { | ||
| 112 | + return nil, fmt.Errorf("没有此资源") | ||
| 113 | + } else { | ||
| 114 | + return nil, err | ||
| 115 | + } | ||
| 116 | + } | ||
| 117 | + if deviceRunningRecordModel.DeviceRunningRecordId == 0 { | ||
| 118 | + return nil, nil | ||
| 119 | + } else { | ||
| 120 | + return transform.TransformToDeviceRunningRecordDomainModelFromPgModels(deviceRunningRecordModel) | ||
| 121 | + } | ||
| 122 | +} | ||
| 123 | +func (repository *DeviceRunningRecordRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.DeviceRunningRecord, error) { | ||
| 124 | + tx := repository.transactionContext.PgTx | ||
| 125 | + var deviceRunningRecordModels []*models.DeviceRunningRecord | ||
| 126 | + deviceRunningRecords := make([]*domain.DeviceRunningRecord, 0) | ||
| 127 | + query := sqlbuilder.BuildQuery(tx.Model(&deviceRunningRecordModels), queryOptions) | ||
| 128 | + query.SetOffsetAndLimit(20) | ||
| 129 | + query.SetOrderDirect("device_running_record_id", "DESC") | ||
| 130 | + if count, err := query.SelectAndCount(); err != nil { | ||
| 131 | + return 0, deviceRunningRecords, err | ||
| 132 | + } else { | ||
| 133 | + for _, deviceRunningRecordModel := range deviceRunningRecordModels { | ||
| 134 | + if deviceRunningRecord, err := transform.TransformToDeviceRunningRecordDomainModelFromPgModels(deviceRunningRecordModel); err != nil { | ||
| 135 | + return 0, deviceRunningRecords, err | ||
| 136 | + } else { | ||
| 137 | + deviceRunningRecords = append(deviceRunningRecords, deviceRunningRecord) | ||
| 138 | + } | ||
| 139 | + } | ||
| 140 | + return int64(count), deviceRunningRecords, nil | ||
| 141 | + } | ||
| 142 | +} | ||
| 143 | +func NewDeviceRunningRecordRepository(transactionContext *pgTransaction.TransactionContext) (*DeviceRunningRecordRepository, error) { | ||
| 144 | + if transactionContext == nil { | ||
| 145 | + return nil, fmt.Errorf("transactionContext参数不能为nil") | ||
| 146 | + } else { | ||
| 147 | + return &DeviceRunningRecordRepository{ | ||
| 148 | + transactionContext: transactionContext, | ||
| 149 | + }, nil | ||
| 150 | + } | ||
| 151 | +} |
| @@ -150,6 +150,7 @@ func (repository *ProductPlanDispatchRecordRepository) Find(queryOptions map[str | @@ -150,6 +150,7 @@ func (repository *ProductPlanDispatchRecordRepository) Find(queryOptions map[str | ||
| 150 | var productPlanDispatchRecordModels []*models.ProductPlanDispatchRecord | 150 | var productPlanDispatchRecordModels []*models.ProductPlanDispatchRecord |
| 151 | productPlanDispatchRecords := make([]*domain.ProductPlanDispatchRecord, 0) | 151 | productPlanDispatchRecords := make([]*domain.ProductPlanDispatchRecord, 0) |
| 152 | query := sqlbuilder.BuildQuery(tx.Model(&productPlanDispatchRecordModels), queryOptions) | 152 | query := sqlbuilder.BuildQuery(tx.Model(&productPlanDispatchRecordModels), queryOptions) |
| 153 | + query.SetWhereByQueryOption("plan_dispatch_status=?", "planDispatchStatus") | ||
| 153 | query.SetOffsetAndLimit(domain.MaxQueryRow) | 154 | query.SetOffsetAndLimit(domain.MaxQueryRow) |
| 154 | query.SetOrderDirect("product_plan_dispatch_record_id", "DESC") | 155 | query.SetOrderDirect("product_plan_dispatch_record_id", "DESC") |
| 155 | if count, err := query.SelectAndCount(); err != nil { | 156 | if count, err := query.SelectAndCount(); err != nil { |
| @@ -174,7 +174,7 @@ func (repository *ProductPlanRepository) Find(queryOptions map[string]interface{ | @@ -174,7 +174,7 @@ func (repository *ProductPlanRepository) Find(queryOptions map[string]interface{ | ||
| 174 | query := sqlbuilder.BuildQuery(tx.Model(&productPlanModels), queryOptions) | 174 | query := sqlbuilder.BuildQuery(tx.Model(&productPlanModels), queryOptions) |
| 175 | query.SetWhereByQueryOption("company_id = ?", "companyId") | 175 | query.SetWhereByQueryOption("company_id = ?", "companyId") |
| 176 | query.SetWhereByQueryOption("org_id = ?", "orgId") | 176 | query.SetWhereByQueryOption("org_id = ?", "orgId") |
| 177 | - | 177 | + query.SetWhereByQueryOption("work_station->>'workshopId'='?'", "workshopId") |
| 178 | if v, ok := queryOptions["batchNumber"]; ok && len(v.(string)) > 0 { | 178 | if v, ok := queryOptions["batchNumber"]; ok && len(v.(string)) > 0 { |
| 179 | query.Where(fmt.Sprintf(`batch_number like '%%%v%%'`, v)) | 179 | query.Where(fmt.Sprintf(`batch_number like '%%%v%%'`, v)) |
| 180 | } | 180 | } |
-
请 注册 或 登录 后发表评论