zhangqian
2023-11-30 ba442a966e3a0a79182f32110e6849d20c79fb28
重构生产进度存储和同步
6个文件已修改
708 ■■■■ 已修改文件
api/v1/task.go 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
crontask/cron_task.go 13 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/procedures.go 25 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/production_progress.go 567 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/task_status_sync.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/progress.go 97 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/task.go
@@ -234,6 +234,7 @@
            ProductProcedureID: procedure.ProductProcedureID,
            IsProcessing:       false,
            IsFinish:           true,
            FinishedQuantity:   procedure.FinishedQuantity,
        }
        return service.NewTaskService().SaveTaskStatusSync(db, &record)
    })
@@ -324,10 +325,6 @@
            return err
        }
        err = taskService.UpdateOrderStatus(db, order.ID, model.OrderStatusProcessing)
        if err != nil {
            return err
        }
        err = service.NewProgressService().Add(db, procedure, order)
        if err != nil {
            return err
        }
crontask/cron_task.go
@@ -43,10 +43,10 @@
                    logx.Infof("plc read finish number err: %v", err)
                    continue
                }
                finishNumber := cast.ToInt64(value)
                finishNumber := cast.ToInt(value)
                if finishNumber != 0 {
                    service.PlcCacheSet(conf.Conf.CurrentDeviceID, addressItem.Channel, constvar.PlcCacheKeyFinishNumber, finishNumber)
                    _ = service.NewProgressService().UpdateProgress(conf.Conf.CurrentDeviceID, addressItem.Channel, cast.ToInt64(finishNumber))
                    _ = service.NewProgressService().UpdateProgress(conf.Conf.CurrentDeviceID, addressItem.Channel, finishNumber)
                }
                logx.Infof("plc read finish number: %v", finishNumber)
            }
@@ -115,20 +115,13 @@
    }
    syncOkIds := make([]uint, 0, len(records))
    for _, record := range records {
        var finishAmount int
        if record.IsFinish {
            progress, err := model.NewProductionProgressSearch(nil).SetWorkOrderId(record.WorkOrderId).SetProductProcedureId(record.ProductProcedureID).First()
            if err == nil {
                finishAmount = int(progress.FinishedQuantity)
            }
        }
        msg := &common.MsgTaskStatusUpdate{
            WorkOrderId:        record.WorkOrderId,
            ProcedureID:        record.ProcedureID,
            DeviceId:           record.DeviceId,
            IsProcessing:       record.IsProcessing,
            IsFinish:           record.IsFinish,
            FinishAmount:       finishAmount,
            FinishAmount:       record.FinishedQuantity,
            ProductProcedureID: record.ProductProcedureID,
            StartTs:            record.StartTs,
            FinishTs:           record.FinishTs,
model/procedures.go
@@ -7,6 +7,7 @@
    "encoding/json"
    "fmt"
    "github.com/jinzhu/gorm"
    "strings"
)
type (
@@ -23,6 +24,7 @@
        EndTime            int64  `json:"endTime"`
        RealStartTime      int64  `json:"realStartTime"`
        RealEndTime        int64  `json:"realEndTime"`
        FinishedQuantity   int    `gorm:"type:int;" json:"finishedQuantity"`
        Status             ProcedureStatus
        ProcedureData      string                  `json:"-"`                  //common.ProductProcedure  json串
        ProceduresInfo     common.ProductProcedure `json:"procedure" gorm:"-"` //common.ProductProcedure  对象
@@ -44,6 +46,7 @@
        Channels     []int32
        Offset       int
        Limit        int
        Fields       []string
    }
)
@@ -59,13 +62,15 @@
    return "procedures"
}
func (slf *Procedures) AfterFind(db *gorm.DB) error {
    var proceduresInfo common.ProductProcedure
    err := json.Unmarshal([]byte(slf.ProcedureData), &proceduresInfo)
    if err != nil {
        logx.Errorf("AfterFind Unmarshal err: %v", err.Error())
        return err
    if slf.ProcedureData != "" {
        var proceduresInfo common.ProductProcedure
        err := json.Unmarshal([]byte(slf.ProcedureData), &proceduresInfo)
        if err != nil {
            logx.Errorf("AfterFind Unmarshal err: %v", err.Error())
            return err
        }
        slf.ProceduresInfo = proceduresInfo
    }
    slf.ProceduresInfo = proceduresInfo
    return nil
}
@@ -161,8 +166,8 @@
    return slf
}
func (slf *ProceduresSearch) SetChannel(channel int32) *ProceduresSearch {
    slf.Channel = channel
func (slf *ProceduresSearch) SetFields(fields []string) *ProceduresSearch {
    slf.Fields = fields
    return slf
}
@@ -225,6 +230,10 @@
        db = db.Where("channel IN (?)", slf.Channels)
    }
    if len(slf.Fields) > 0 {
        db = db.Select(strings.Join(slf.Fields, ","))
    }
    return db
}
model/production_progress.go
@@ -1,285 +1,286 @@
package model
import (
    "apsClient/pkg/sqlitex"
    "fmt"
    "github.com/jinzhu/gorm"
)
type (
    ProductionProgress struct {
        gorm.Model
        ProceduresID       uint   `gorm:"index;type:varchar(191)" json:"proceduresId"` //procedures表的id
        WorkOrderID        string `gorm:"index;type:varchar(191);not null" json:"workOrderID"`
        OrderID            string `gorm:"index;type:varchar(191);not null" json:"orderID"`
        ProcedureID        string `gorm:"type:varchar(191)" json:"procedureId"`
        ProductProcedureID string `gorm:"type:varchar(191);" json:"productProcedureID"` //产品工序id
        DeviceID           string `gorm:"type:varchar(191);not null" json:"deviceId"`
        FinishedQuantity   int64  `gorm:"type:int;not null" json:"finishedQuantity"`
        Channel            int32  `gorm:"type:int" json:"channel"` //通道
        TotalQuantity      int64  `gorm:"type:int;not null" json:"totalQuantity"`
    }
    ProductionProgressSearch struct {
        ProductionProgress
        Order      string
        PageNum    int
        PageSize   int
        Orm        *gorm.DB
        UnFinished bool
    }
)
func (slf *ProductionProgress) TableName() string {
    return "production_progress"
}
func NewProductionProgressSearch(db *gorm.DB) *ProductionProgressSearch {
    if db == nil {
        db = sqlitex.GetDB()
    }
    return &ProductionProgressSearch{Orm: db}
}
func (slf *ProductionProgressSearch) SetOrm(tx *gorm.DB) *ProductionProgressSearch {
    slf.Orm = tx
    return slf
}
func (slf *ProductionProgressSearch) SetPage(page, size int) *ProductionProgressSearch {
    slf.PageNum, slf.PageSize = page, size
    return slf
}
func (slf *ProductionProgressSearch) SetOrder(order string) *ProductionProgressSearch {
    slf.Order = order
    return slf
}
func (slf *ProductionProgressSearch) SetWorkOrderId(orderId string) *ProductionProgressSearch {
    slf.WorkOrderID = orderId
    return slf
}
func (slf *ProductionProgressSearch) SetProcedureId(procedureId string) *ProductionProgressSearch {
    slf.ProcedureID = procedureId
    return slf
}
func (slf *ProductionProgressSearch) SetProceduresId(proceduresId uint) *ProductionProgressSearch {
    slf.ProceduresID = proceduresId
    return slf
}
func (slf *ProductionProgressSearch) SetDeviceId(id string) *ProductionProgressSearch {
    slf.DeviceID = id
    return slf
}
func (slf *ProductionProgressSearch) SetProductProcedureId(productProcedureId string) *ProductionProgressSearch {
    slf.ProductProcedureID = productProcedureId
    return slf
}
func (slf *ProductionProgressSearch) SetId(id uint) *ProductionProgressSearch {
    slf.ID = id
    return slf
}
func (slf *ProductionProgressSearch) SetChannel(channel int32) *ProductionProgressSearch {
    slf.Channel = channel
    return slf
}
func (slf *ProductionProgressSearch) SetUnFinished() *ProductionProgressSearch {
    slf.UnFinished = true
    return slf
}
func (slf *ProductionProgressSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&ProductionProgress{})
    if slf.Order != "" {
        db = db.Order(slf.Order)
    }
    if slf.ID != 0 {
        db = db.Where("id = ?", slf.ID)
    }
    if slf.WorkOrderID != "" {
        db = db.Where("work_order_id = ?", slf.WorkOrderID)
    }
    if slf.OrderID != "" {
        db = db.Where("order_id = ?", slf.OrderID)
    }
    if slf.ProcedureID != "" {
        db = db.Where("procedure_id = ?", slf.ProcedureID)
    }
    if slf.ProceduresID != 0 {
        db = db.Where("procedures_id = ?", slf.ProceduresID)
    }
    if slf.DeviceID != "" {
        db = db.Where("device_id = ?", slf.DeviceID)
    }
    if slf.Channel != 0 {
        db = db.Where("channel = ?", slf.Channel)
    }
    if slf.UnFinished {
        db = db.Where("finished_quantity <  total_quantity")
    }
    if slf.ProductProcedureID != "" {
        db = db.Where("product_procedure_id = ?", slf.ProductProcedureID)
    }
    return db
}
// Create 单条插入
func (slf *ProductionProgressSearch) Create(record *ProductionProgress) error {
    var db = slf.build()
    if err := db.Create(record).Error; err != nil {
        return fmt.Errorf("create err: %v, record: %+v", err, record)
    }
    return nil
}
func (slf *ProductionProgressSearch) Save(record *ProductionProgress) error {
    var db = slf.build()
    if err := db.Save(record).Error; err != nil {
        return fmt.Errorf("save err: %v, record: %+v", err, record)
    }
    return nil
}
func (slf *ProductionProgressSearch) UpdateByMap(upMap map[string]interface{}) error {
    var (
        db = slf.build()
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
    }
    return nil
}
func (slf *ProductionProgressSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
    var (
        db = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Updates(upMap).Error; err != nil {
        return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap)
    }
    return nil
}
func (slf *ProductionProgressSearch) Delete() error {
    var db = slf.build()
    if err := db.Unscoped().Delete(&ProductionProgress{}).Error; err != nil {
        return err
    }
    return nil
}
func (slf *ProductionProgressSearch) First() (*ProductionProgress, error) {
    var (
        record = new(ProductionProgress)
        db     = slf.build()
    )
    if err := db.First(record).Error; err != nil {
        return record, err
    }
    return record, nil
}
func (slf *ProductionProgressSearch) Find() ([]*ProductionProgress, int64, error) {
    var (
        records = make([]*ProductionProgress, 0)
        total   int64
        db      = slf.build()
    )
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find records err: %v", err)
    }
    return records, total, nil
}
func (slf *ProductionProgressSearch) FindNotTotal() ([]*ProductionProgress, error) {
    var (
        records = make([]*ProductionProgress, 0)
        db      = slf.build()
    )
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
    return records, nil
}
// FindByQuery 指定条件查询.
func (slf *ProductionProgressSearch) FindByQuery(query string, args []interface{}) ([]*ProductionProgress, int64, error) {
    var (
        records = make([]*ProductionProgress, 0)
        total   int64
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if err := db.Count(&total).Error; err != nil {
        return records, total, fmt.Errorf("find by query count err: %v", err)
    }
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, total, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, total, nil
}
// FindByQueryNotTotal 指定条件查询&不查询总条数.
func (slf *ProductionProgressSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*ProductionProgress, error) {
    var (
        records = make([]*ProductionProgress, 0)
        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
    )
    if slf.PageNum*slf.PageSize > 0 {
        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
    }
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
    }
    return records, nil
}
//
//import (
//    "apsClient/pkg/sqlitex"
//    "fmt"
//    "github.com/jinzhu/gorm"
//)
//
//type (
//    ProductionProgress struct {
//        gorm.Model
//        ProceduresID       uint   `gorm:"index;type:varchar(191)" json:"proceduresId"` //procedures表的id
//        WorkOrderID        string `gorm:"index;type:varchar(191);not null" json:"workOrderID"`
//        OrderID            string `gorm:"index;type:varchar(191);not null" json:"orderID"`
//        ProcedureID        string `gorm:"type:varchar(191)" json:"procedureId"`
//        ProductProcedureID string `gorm:"type:varchar(191);" json:"productProcedureID"` //产品工序id
//        DeviceID           string `gorm:"type:varchar(191);not null" json:"deviceId"`
//        FinishedQuantity   int64  `gorm:"type:int;not null" json:"finishedQuantity"`
//        Channel            int32  `gorm:"type:int" json:"channel"` //通道
//        TotalQuantity      int64  `gorm:"type:int;not null" json:"totalQuantity"`
//    }
//
//    ProductionProgressSearch struct {
//        ProductionProgress
//        Order      string
//        PageNum    int
//        PageSize   int
//        Orm        *gorm.DB
//        UnFinished bool
//    }
//)
//
//func (slf *ProductionProgress) TableName() string {
//    return "production_progress"
//}
//
//func NewProductionProgressSearch(db *gorm.DB) *ProductionProgressSearch {
//    if db == nil {
//        db = sqlitex.GetDB()
//    }
//    return &ProductionProgressSearch{Orm: db}
//}
//
//func (slf *ProductionProgressSearch) SetOrm(tx *gorm.DB) *ProductionProgressSearch {
//    slf.Orm = tx
//    return slf
//}
//
//func (slf *ProductionProgressSearch) SetPage(page, size int) *ProductionProgressSearch {
//    slf.PageNum, slf.PageSize = page, size
//    return slf
//}
//
//func (slf *ProductionProgressSearch) SetOrder(order string) *ProductionProgressSearch {
//    slf.Order = order
//    return slf
//}
//
//func (slf *ProductionProgressSearch) SetWorkOrderId(orderId string) *ProductionProgressSearch {
//    slf.WorkOrderID = orderId
//    return slf
//}
//
//func (slf *ProductionProgressSearch) SetProcedureId(procedureId string) *ProductionProgressSearch {
//    slf.ProcedureID = procedureId
//    return slf
//}
//
//func (slf *ProductionProgressSearch) SetProceduresId(proceduresId uint) *ProductionProgressSearch {
//    slf.ProceduresID = proceduresId
//    return slf
//}
//
//func (slf *ProductionProgressSearch) SetDeviceId(id string) *ProductionProgressSearch {
//    slf.DeviceID = id
//    return slf
//}
//
//func (slf *ProductionProgressSearch) SetProductProcedureId(productProcedureId string) *ProductionProgressSearch {
//    slf.ProductProcedureID = productProcedureId
//    return slf
//}
//
//func (slf *ProductionProgressSearch) SetId(id uint) *ProductionProgressSearch {
//    slf.ID = id
//    return slf
//}
//
//func (slf *ProductionProgressSearch) SetChannel(channel int32) *ProductionProgressSearch {
//    slf.Channel = channel
//    return slf
//}
//
//func (slf *ProductionProgressSearch) SetUnFinished() *ProductionProgressSearch {
//    slf.UnFinished = true
//    return slf
//}
//
//func (slf *ProductionProgressSearch) build() *gorm.DB {
//    var db = slf.Orm.Model(&ProductionProgress{})
//
//    if slf.Order != "" {
//        db = db.Order(slf.Order)
//    }
//
//    if slf.ID != 0 {
//        db = db.Where("id = ?", slf.ID)
//    }
//
//    if slf.WorkOrderID != "" {
//        db = db.Where("work_order_id = ?", slf.WorkOrderID)
//    }
//
//    if slf.OrderID != "" {
//        db = db.Where("order_id = ?", slf.OrderID)
//    }
//
//    if slf.ProcedureID != "" {
//        db = db.Where("procedure_id = ?", slf.ProcedureID)
//    }
//
//    if slf.ProceduresID != 0 {
//        db = db.Where("procedures_id = ?", slf.ProceduresID)
//    }
//
//    if slf.DeviceID != "" {
//        db = db.Where("device_id = ?", slf.DeviceID)
//    }
//
//    if slf.Channel != 0 {
//        db = db.Where("channel = ?", slf.Channel)
//    }
//
//    if slf.UnFinished {
//        db = db.Where("finished_quantity <  total_quantity")
//    }
//
//    if slf.ProductProcedureID != "" {
//        db = db.Where("product_procedure_id = ?", slf.ProductProcedureID)
//    }
//
//    return db
//}
//
//// Create 单条插入
//func (slf *ProductionProgressSearch) Create(record *ProductionProgress) error {
//    var db = slf.build()
//
//    if err := db.Create(record).Error; err != nil {
//        return fmt.Errorf("create err: %v, record: %+v", err, record)
//    }
//
//    return nil
//}
//
//func (slf *ProductionProgressSearch) Save(record *ProductionProgress) error {
//    var db = slf.build()
//
//    if err := db.Save(record).Error; err != nil {
//        return fmt.Errorf("save err: %v, record: %+v", err, record)
//    }
//
//    return nil
//}
//
//func (slf *ProductionProgressSearch) UpdateByMap(upMap map[string]interface{}) error {
//    var (
//        db = slf.build()
//    )
//
//    if err := db.Updates(upMap).Error; err != nil {
//        return fmt.Errorf("update by map err: %v, upMap: %+v", err, upMap)
//    }
//
//    return nil
//}
//
//func (slf *ProductionProgressSearch) UpdateByQuery(query string, args []interface{}, upMap map[string]interface{}) error {
//    var (
//        db = slf.Orm.Table(slf.TableName()).Where(query, args...)
//    )
//
//    if err := db.Updates(upMap).Error; err != nil {
//        return fmt.Errorf("update by query err: %v, query: %s, args: %+v, upMap: %+v", err, query, args, upMap)
//    }
//
//    return nil
//}
//
//func (slf *ProductionProgressSearch) Delete() error {
//    var db = slf.build()
//
//    if err := db.Unscoped().Delete(&ProductionProgress{}).Error; err != nil {
//        return err
//    }
//
//    return nil
//}
//
//func (slf *ProductionProgressSearch) First() (*ProductionProgress, error) {
//    var (
//        record = new(ProductionProgress)
//        db     = slf.build()
//    )
//
//    if err := db.First(record).Error; err != nil {
//        return record, err
//    }
//
//    return record, nil
//}
//
//func (slf *ProductionProgressSearch) Find() ([]*ProductionProgress, int64, error) {
//    var (
//        records = make([]*ProductionProgress, 0)
//        total   int64
//        db      = slf.build()
//    )
//
//    if err := db.Count(&total).Error; err != nil {
//        return records, total, fmt.Errorf("find count err: %v", err)
//    }
//    if slf.PageNum*slf.PageSize > 0 {
//        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
//    }
//    if err := db.Find(&records).Error; err != nil {
//        return records, total, fmt.Errorf("find records err: %v", err)
//    }
//
//    return records, total, nil
//}
//
//func (slf *ProductionProgressSearch) FindNotTotal() ([]*ProductionProgress, error) {
//    var (
//        records = make([]*ProductionProgress, 0)
//        db      = slf.build()
//    )
//
//    if slf.PageNum*slf.PageSize > 0 {
//        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
//    }
//    if err := db.Find(&records).Error; err != nil {
//        return records, fmt.Errorf("find records err: %v", err)
//    }
//
//    return records, nil
//}
//
//// FindByQuery 指定条件查询.
//func (slf *ProductionProgressSearch) FindByQuery(query string, args []interface{}) ([]*ProductionProgress, int64, error) {
//    var (
//        records = make([]*ProductionProgress, 0)
//        total   int64
//        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
//    )
//
//    if err := db.Count(&total).Error; err != nil {
//        return records, total, fmt.Errorf("find by query count err: %v", err)
//    }
//    if slf.PageNum*slf.PageSize > 0 {
//        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
//    }
//    if err := db.Find(&records).Error; err != nil {
//        return records, total, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
//    }
//
//    return records, total, nil
//}
//
//// FindByQueryNotTotal 指定条件查询&不查询总条数.
//func (slf *ProductionProgressSearch) FindByQueryNotTotal(query string, args []interface{}) ([]*ProductionProgress, error) {
//    var (
//        records = make([]*ProductionProgress, 0)
//        db      = slf.Orm.Table(slf.TableName()).Where(query, args...)
//    )
//
//    if slf.PageNum*slf.PageSize > 0 {
//        db = db.Offset((slf.PageNum - 1) * slf.PageSize).Limit(slf.PageSize)
//    }
//    if err := db.Find(&records).Error; err != nil {
//        return records, fmt.Errorf("find by query records err: %v, query: %s, args: %+v", err, query, args)
//    }
//
//    return records, nil
//}
model/task_status_sync.go
@@ -18,6 +18,7 @@
        IsFinish           bool   //是否完成
        StartTs            int64  `json:"start_ts"`  //开始时间
        FinishTs           int64  `json:"finish_ts"` //结束时间
        FinishedQuantity   int    `json:"finishedQuantity"`
    }
    TaskStatusSyncSearch struct {
service/progress.go
@@ -3,7 +3,7 @@
import (
    "apsClient/model"
    "errors"
    "github.com/jinzhu/gorm"
    "github.com/mitchellh/mapstructure"
)
type ProgressService struct {
@@ -13,71 +13,32 @@
    return &ProgressService{}
}
func (slf ProgressService) Add(db *gorm.DB, procedure *model.Procedures, order *model.Order) error {
    _, err := model.NewProductionProgressSearch(db).SetProceduresId(procedure.ID).First()
    if err == gorm.ErrRecordNotFound {
        progress := &model.ProductionProgress{
            ProceduresID:       procedure.ID,
            WorkOrderID:        procedure.WorkOrderID,
            OrderID:            procedure.OrderID,
            ProcedureID:        procedure.ProceduresInfo.ProcedureID,
            ProductProcedureID: procedure.ProductProcedureID,
            DeviceID:           procedure.DeviceID,
            TotalQuantity:      order.Amount.IntPart(),
            Channel:            procedure.Channel,
        }
        err := model.NewProductionProgressSearch(db).Create(progress)
        if err != nil {
            return err
        }
        ProgressCacheSet(procedure.DeviceID, procedure.Channel, progress)
    }
    return nil
}
// UpdateProgress 仅限plc数据采集定时任务用(缺少工序id参数)
func (slf ProgressService) UpdateProgress(deviceID string, channel int32, finishedQuantity int64) (err error) {
    progressCache, err := slf.GetCurrentProgress(deviceID, channel)
func (slf ProgressService) UpdateProgress(deviceID string, channel int32, finishedQuantity int) (err error) {
    progress, err := slf.GetCurrentProgress(deviceID, channel)
    if err != nil {
        return err
    }
    if progressCache == nil {
    if progress == nil {
        return errors.New("progress cache not found")
    }
    if finishedQuantity > progressCache.FinishedQuantity { //当有变化时才更新
        progressCache.FinishedQuantity = finishedQuantity
        ProgressCacheSet(deviceID, channel, progressCache)
        return model.NewProductionProgressSearch(nil).SetId(progressCache.ID).Save(progressCache)
    if finishedQuantity > progress.FinishedQuantity { //当有变化时才更新
        return model.NewProceduresSearch(nil).SetId(progress.ID).UpdateByMap(map[string]interface{}{"finished_quantity": finishedQuantity})
    }
    return nil
}
// GetCurrentProgress 仅限plc数据采集定时任务用(缺少工序id参数)
func (slf ProgressService) GetCurrentProgress(deviceID string, channel int32) (progressCache *model.ProductionProgress, err error) {
    var ok bool
    progressCache, ok = ProgressCacheGet(deviceID, channel)
    if !ok {
        progressCache, err = model.NewProductionProgressSearch(nil).SetDeviceId(deviceID).SetChannel(channel).SetOrder("id asc").First()
        if err == gorm.ErrRecordNotFound {
            return nil, errors.New("progress not found")
        }
        if err != nil {
            return nil, err
        }
        if progressCache.FinishedQuantity >= progressCache.TotalQuantity { //如果完成量大于等于总量就说明是上一个已完成的任务,不是当前进行中的任务。
            progressCache = nil
        }
        if progressCache != nil {
            ProgressCacheSet(deviceID, channel, progressCache)
        }
func (slf ProgressService) GetCurrentProgress(deviceID string, channel int32) (progress *model.Procedures, err error) {
    progress, err = model.NewProceduresSearch(nil).SetDeviceId(deviceID).SetChannels([]int32{channel}).SetStatus(model.ProcedureStatusProcessing).SetOrder("id desc").First()
    if err != nil {
        return nil, err
    }
    return
}
func (slf ProgressService) UpdateProgressByProceduresId(proceduresId uint, finishedQuantity int64) (err error) {
    progress, err := slf.GetCurrentProgressByProceduresId(proceduresId)
func (slf ProgressService) UpdateProgressByProceduresId(proceduresId uint, finishedQuantity int) (err error) {
    progress, err := slf.GetProcedureByProceduresId(proceduresId)
    if err != nil {
        return err
    }
@@ -85,22 +46,44 @@
        return errors.New("progress not exists")
    }
    if finishedQuantity > progress.FinishedQuantity { //当有变化时才更新
        progress.FinishedQuantity = finishedQuantity
        return model.NewProductionProgressSearch(nil).SetId(progress.ID).Save(progress)
        return model.NewProceduresSearch(nil).SetId(progress.ID).UpdateByMap(map[string]interface{}{"finished_quantity": finishedQuantity})
    }
    return nil
}
func (slf ProgressService) GetCurrentProgressByProceduresId(proceduresId uint) (progress *model.ProductionProgress, err error) {
    progress, err = model.NewProductionProgressSearch(nil).SetProceduresId(proceduresId).First()
func (slf ProgressService) GetProcedureByProceduresId(proceduresId uint) (progress *model.Procedures, err error) {
    progress, err = model.NewProceduresSearch(nil).SetId(proceduresId).First()
    if err != nil {
        return nil, err
    }
    return
}
type ProductionProgress struct {
    WorkOrderID        string `json:"workOrderID"`
    OrderID            string `json:"orderID"`
    ProcedureID        string `json:"procedureId"`
    ProductProcedureID string `json:"productProcedureID"` //产品工序id
    DeviceID           string `json:"deviceId"`
    FinishedQuantity   int64  `json:"finishedQuantity"`
}
// GetProgressList 获取待同步进度工序
func (slf ProgressService) GetProgressList() (progressList []*model.ProductionProgress, err error) {
    progressList, err = model.NewProductionProgressSearch(nil).SetUnFinished().SetOrder("id desc").SetPage(1, 100).FindNotTotal()
func (slf ProgressService) GetProgressList() (progressList []*ProductionProgress, err error) {
    var procedureList []*model.Procedures
    procedureList, err = model.NewProceduresSearch(nil).
        SetFields([]string{"id",
            "finished_quantity",
            "work_order_id",
            "order_id",
            "procedure_id",
            "product_procedure_id",
            "device_id",
        }).SetStatus(model.ProcedureStatusProcessing).SetOrder("id desc").FindNotTotal()
    if err != nil {
        return
    }
    err = mapstructure.Decode(procedureList, procedureList)
    return
}