作者 yangfu

fix :统计修改

fix :统计修改

fix :统计修改

fix :统计修改

fix :统计修改

fix :统计修改

fix :统计修改
... ... @@ -91,7 +91,7 @@ func AutoFlushDeviceDailyRunningRecordOEE(ctx context.Context) error {
log.Logger.Error(err.Error())
continue
}
if err := redis.SaveDeviceDailyRunningRecord(v.ProductDate, v); err != nil {
if err := redis.SaveDeviceDailyRunningRecordByKey(v.RedisKey, v); err != nil {
log.Logger.Error(err.Error(), map[string]interface{}{"task": "定时刷新设备每日运行记录"})
continue
}
... ...
... ... @@ -33,6 +33,8 @@ type DeviceDailyRunningRecord struct {
UpdatedAt time.Time `json:"updatedAt"`
// 删除时间
DeletedAt time.Time `json:"deletedAt"`
RedisKey string `json:"-"`
}
type DeviceDailyRunningRecordRepository interface {
... ... @@ -69,10 +71,12 @@ func (deviceDailyRunningRecord *DeviceDailyRunningRecord) AddDeviceRunningData(d
}
func (deviceDailyRunningRecord *DeviceDailyRunningRecord) String() string {
return fmt.Sprintf("记录ID:%v 工段:%v 设备:%v",
return fmt.Sprintf("记录ID:%v 工段:%v 设备:%v 数量:%v(%v)",
deviceDailyRunningRecord.DeviceDailyRunningRecordId,
deviceDailyRunningRecord.WorkStation.SectionName,
deviceDailyRunningRecord.DeviceCode,
deviceDailyRunningRecord.DeviceRunningRecordInfo.Count,
deviceDailyRunningRecord.DeviceRunningRecordInfo.ProductDate,
)
}
... ...
... ... @@ -98,7 +98,7 @@ func (ptr *PGWorkshopDataConsumeService) Consume(companyId, orgId int, record *d
if record.DeviceType == domain.DeviceTypeChuanChuanJi && plan != nil && deviceRunningData.Count > 0 {
log.Logger.Debug(fmt.Sprintf("设备统计 设备:%v(%v) 当前数量:%v 增加数量:%v",
deviceDailyRecord.DeviceCode,
deviceDailyRecord.ProductDate.Local().Format("2006-01-02"),
deviceDailyRecord.DeviceRunningRecordInfo.ProductDate,
deviceRunningRecord.DeviceRunningRecordInfo.TodayTotal,
deviceRunningRecord.DeviceRunningRecordInfo.Count))
productRecord, _ := ptr.newProductRecord(companyId, orgId, workStation, device, deviceRunningData, plan)
... ... @@ -110,7 +110,7 @@ func (ptr *PGWorkshopDataConsumeService) Consume(companyId, orgId int, record *d
SendProductRecordStaticsJob(productRecord)
}
}
if err = redis.SaveDeviceDailyRunningRecord(deviceRunningData.CollectionTime, deviceDailyRecord); err != nil {
if err = redis.SaveDeviceDailyRunningRecordByKey(deviceDailyRecord.RedisKey, deviceDailyRecord); err != nil {
return nil, err
}
return nil, nil
... ... @@ -282,21 +282,21 @@ func (ptr *PGWorkshopDataConsumeService) saveDeviceDailyRunningRecord(companyId,
if record, err = deviceDailyRunningRecordRepository.FindOne(map[string]interface{}{
"workStationId": workStation.WorkStationId,
"deviceCode": data.DeviceCode,
"productDate": utils.GetZeroTime(data.CollectionTime),
"productDate": utils.GetZeroTime(data.CollectionTime.Local()),
}); err != nil {
if err != domain.ErrorNotFound {
return nil, err
}
}
if record != nil {
record.RedisKey = redis.DeviceDailyRunningRecordKeyByTimeStr(record.DeviceRunningRecordInfo.ProductDate, record.DeviceCode)
return record, nil
}
recordInfo := domain.NewDeviceRunningRecordInfo()
recordInfo.ProductPlanId = planId
recordInfo.DeviceName = device.DeviceName
recordInfo.OrgName = device.Ext.OrgName
recordInfo.ProductDate = data.CollectionTime.Format("2006-01-02")
recordInfo.ProductDate = data.CollectionTime.Local().Format("2006-01-02")
record = &domain.DeviceDailyRunningRecord{
CompanyId: companyId,
OrgId: orgId,
... ... @@ -308,6 +308,7 @@ func (ptr *PGWorkshopDataConsumeService) saveDeviceDailyRunningRecord(companyId,
CreatedAt: data.CollectionTime,
UpdatedAt: time.Now(),
}
record.RedisKey = redis.DeviceDailyRunningRecordKeyByTimeStr(record.DeviceRunningRecordInfo.ProductDate, record.DeviceCode)
if record, err = deviceDailyRunningRecordRepository.Save(record); err != nil {
return nil, err
}
... ...
... ... @@ -14,8 +14,9 @@ import (
func GetDeviceDailyRunningRecord(t time.Time, deviceCode string) (*domain.DeviceDailyRunningRecord, error) {
client := GetRedis()
key := DeviceDailyRunningRecordKey(t, deviceCode)
log.Logger.Debug(fmt.Sprintf("Redis Device:%v GET Key:%v", deviceCode, key))
return getDeviceDailyRunningRecord(client, key)
record, err := getDeviceDailyRunningRecord(client, key)
log.Logger.Debug(fmt.Sprintf("Redis Device:%v GET Key:%v Value:%v", deviceCode, key, record))
return record, err
}
func getDeviceDailyRunningRecord(client *redis.Client, key string) (*domain.DeviceDailyRunningRecord, error) {
... ... @@ -25,6 +26,7 @@ func getDeviceDailyRunningRecord(client *redis.Client, key string) (*domain.Devi
return nil, domain.ErrorNotFound
}
var record = &domain.DeviceDailyRunningRecord{}
record.RedisKey = key
if err = json.Unmarshal(data, record); err != nil {
return nil, err
}
... ... @@ -35,13 +37,25 @@ func getDeviceDailyRunningRecord(client *redis.Client, key string) (*domain.Devi
func SaveDeviceDailyRunningRecord(t time.Time, record *domain.DeviceDailyRunningRecord) error {
client := GetRedis()
key := DeviceDailyRunningRecordKey(t, record.DeviceCode)
log.Logger.Debug(fmt.Sprintf("Redis Device:%v SET Key:%v Count:%v", record.DeviceCode, key, record.DeviceRunningRecordInfo.Count))
recordData, err := json.Marshal(record)
result := client.Set(key, recordData, time.Hour*24*5)
_, err = result.Result()
return err
}
// 保存每日设备运行数据 - 按键值
func SaveDeviceDailyRunningRecordByKey(key string, record *domain.DeviceDailyRunningRecord) error {
client := GetRedis()
recordData, err := json.Marshal(record)
if err != nil {
return err
}
log.Logger.Debug(fmt.Sprintf("Redis Device:%v SET Key:%v Value:%v", record.DeviceCode, key, record))
result := client.Set(key, recordData, time.Hour*24*5)
_, err = result.Result()
return err
}
// 保存每日设备运行数据
func RemoveDeviceDailyRunningRecord(t time.Time, deviceCode string) error {
client := GetRedis()
... ... @@ -51,11 +65,25 @@ func RemoveDeviceDailyRunningRecord(t time.Time, deviceCode string) error {
return err
}
// 保存每日设备运行数据
func RemoveDeviceDailyRunningRecordByKey(key string) error {
client := GetRedis()
//key := DeviceDailyRunningRecordKey(t, deviceCode)
result := client.Del(key)
_, err := result.Result()
return err
}
func DeviceDailyRunningRecordKey(t time.Time, deviceCode string) string {
str := fmt.Sprintf("%v:device-daily-record:%v-%v:%v:%v", constant.CACHE_PREFIX, constant.MANUFACTURE_DEFAULT_COMPANYID, constant.MANUFACTURE_DEFAULT_ORGID, t.Local().Format("2006-01-02"), deviceCode)
return str
}
func DeviceDailyRunningRecordKeyByTimeStr(timeStr string, deviceCode string) string {
str := fmt.Sprintf("%v:device-daily-record:%v-%v:%v:%v", constant.CACHE_PREFIX, constant.MANUFACTURE_DEFAULT_COMPANYID, constant.MANUFACTURE_DEFAULT_ORGID, timeStr, deviceCode)
return str
}
// 获取设备每日所有数据记录
func GetDeviceDailyAllRecord(t time.Time) ([]*domain.DeviceDailyRunningRecord, error) {
client := GetRedis()
... ... @@ -70,6 +98,7 @@ func GetDeviceDailyAllRecord(t time.Time) ([]*domain.DeviceDailyRunningRecord, e
if err != nil {
return nil, err
}
record.RedisKey = v
records = append(records, record)
}
return records, nil
... ...
... ... @@ -23,12 +23,13 @@ func TestTimeParseInLocal(t *testing.T) {
u1.AgeZeroFormat = u1.AgeZero.Format("2006-01-02")
fmt.Println(json.MarshalToString(u1), u1.Age.Local())
timeMarsh(&u1)
}
u2 := User{
Age: tl,
AgeZero: GetZeroTimeWithLocal(tl, time.Local),
}
u2.AgeZeroFormat = u2.AgeZero.Format("2006-01-02")
func timeMarsh(u *User) {
var u2 = &User{}
u1Data, _ := json.Marshal(u)
json.Unmarshal(u1Data, u2)
fmt.Println(json.MarshalToString(u2), u2.Age.Local())
fmt.Println("时间:", u2.Age, u2.Age.Local(), u2.AgeZero)
}
... ...
... ... @@ -119,6 +119,31 @@ func (controller *StatisticsController) TaskHandler() func(ctx *context.Context)
}
Response(ctx, response, nil)
break
case "10":
t := ctx.Input.Query("t")
c, _ := strconv.Atoi(ctx.Input.Query("c"))
if len(t) != 0 {
if v, err := time.ParseInLocation("2006-01-02 - 15:04:05", t, time.UTC); err == nil {
domainService.SendWorkshopDeviceData(&domain.DeviceCollection{
DeviceCollectionId: time.Now().Unix(),
WorkShopName: "NG",
CollectionTime: v,
DeviceSn: "CCJ1",
DeviceType: "CCJ",
StartupStatus: 1,
ComStatus: 1,
ProductCount: c,
Values: map[string]interface{}{
"Count": c,
"Year": 2022,
"Month": 3,
"Day": 8,
"ProductType": 2,
},
LatestUpdateTime: time.Now(),
})
}
}
}
Response(ctx, nil, nil)
}
... ...
... ... @@ -22,13 +22,14 @@ func Start(log logimp.Logger) {
func OnReceiveData(client pahomqtt.Client, message pahomqtt.Message) {
payload := make(map[string]interface{})
var collectionTime time.Time
err := json.Unmarshal(message.Payload(), &payload)
if err != nil {
log.Logger.Error("车间数据json解析失败:" + err.Error())
return
}
if workShop, ok := payload["WorkShop"]; ok {
var collectionTime time.Time
if t, ok := payload["UpTime"]; ok {
collectionTime, _ = time.ParseInLocation("2006-01-02 - 15:04:05", t.(string), time.Local)
}
... ... @@ -153,7 +154,7 @@ func OnReceiveData(client pahomqtt.Client, message pahomqtt.Message) {
}
//TODO:日志先注释
if err == nil {
log.Logger.Info("MQTT", map[string]interface{}{
log.Logger.Info(fmt.Sprintf("MQTT:%v", collectionTime), map[string]interface{}{
"Topic": message.Topic(),
"MessageId": message.MessageID(),
"Message": payload,
... ...
... ... @@ -19,7 +19,7 @@ func WorkshopDataConsumer(c context.Context, t *asynq.Task) error {
return err
}
log.Logger.Debug(fmt.Sprintf("【车间数据消费】 消费 设备:%v 消息号:%v 时间:%v ", cmd.DeviceCollection.DeviceSn, cmd.DeviceCollectionId, cmd.CollectionTime),
map[string]interface{}{"p-message": cmd.DeviceCollection})
map[string]interface{}{"p-message": string(t.Payload())})
cmd.CompanyId = constant.MANUFACTURE_DEFAULT_COMPANYID
cmd.OrgId = constant.MANUFACTURE_DEFAULT_ORGID
_, err := svr.WorkshopConsume(cmd)
... ...