作者 yangfu

fix:采集数据修改

... ... @@ -117,4 +117,6 @@ spec:
- name: MANUFACTURE_DEFAULT_COMPANYID
value: "23"
- name: MANUFACTURE_DEFAULT_ORGID
value: "487"
\ No newline at end of file
value: "487"
- name: MANUFACTURE_PRODUCT_TYPE
value: "0502010004ST,0502010004ST"
\ No newline at end of file
... ...
... ... @@ -81,24 +81,24 @@ func (deviceCollectionService *DeviceCollectionService) CreateDeviceCollection(c
//处理设备数据
switch deviceCollection.DeviceType {
case domain.DeviceTypeBaoXianJi, domain.DeviceTypeChuanChuanJi, domain.DeviceTypeFengKouJi, domain.DeviceTypeFengXiangJi:
if v, ok := deviceCollection.Values["Count"]; ok {
if v, ok := deviceCollection.Values["count"]; ok {
curCount, errCurCount := strconv.Atoi(fmt.Sprintf("%v", v))
v, ok = lastDeviceCollectionRecord.Values["Count"]
v, ok = lastDeviceCollectionRecord.Values["count"]
if ok {
lastCount, errLastCount := strconv.Atoi(fmt.Sprintf("%v", v))
if errLastCount == nil && errCurCount == nil && lastCount <= curCount {
deviceCollection.Values["Count"] = curCount - lastCount
deviceCollection.Values["count"] = curCount - lastCount
} else {
deviceCollection.Values["Count"] = 0
deviceCollection.Values["count"] = 0
/*
设备统计的数量超过一定范围会重置为0,特殊处理0操作
*/
if lastCount > 10000000 && curCount < 1000 {
deviceCollection.Values["Count"] = curCount
deviceCollection.Values["count"] = curCount
}
}
} else {
deviceCollection.Values["Count"] = 0
deviceCollection.Values["count"] = 0
}
}
break
... ...
package dto
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
)
type EmployeeProductRecordDto struct {
// 员工产能记录ID
... ... @@ -47,10 +50,10 @@ func (d *EmployeeProductRecordDto) LoadDto(m *domain.EmployeeProductRecord, orgI
d.PlanProductName = m.ProductRecordInfo.PlanProductName
d.BatchNumber = m.ProductRecordInfo.BatchNumber
d.ParticipateType = m.ParticipateType
d.ProductWeigh = m.RealProductWeigh()
d.SecondLevelWeigh = m.SecondLevelWeigh
d.ProductWeigh = utils.Round(m.RealProductWeigh(), 1)
d.SecondLevelWeigh = utils.Round(m.SecondLevelWeigh, 1)
d.QualificationRate = m.QualificationRate()
d.CreatedAt = m.CreatedAt.Format("2006-01-02")
d.CreatedAt = m.CreatedAt.Local().Format("2006-01-02")
d.ParticipateTypeDescription = domain.ParticipateTypeDescription(m.ParticipateType)
d.EmployeeTypeDescription = domain.EmployeeTypeDescription(m.ProductWorker.EmployeeType)
d.WorkOnDescription = domain.WorkOnDescriptions(m.WorkOn)
... ...
package dto
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
import (
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
)
type WorkshopProductRecordDto struct {
// 员工产能记录ID
... ... @@ -39,9 +42,9 @@ func (d *WorkshopProductRecordDto) LoadDto(m *domain.WorkshopProductRecord, orgI
d.PlanProductName = m.ProductRecordInfo.PlanProductName
d.BatchNumber = m.ProductRecordInfo.BatchNumber
//d.ParticipateType = m.ParticipateType
d.DevotedProductWeigh = m.DevotedProductWeigh()
d.ProductWeigh = m.RealProductWeigh()
d.SecondLevelWeigh = m.SecondLevelWeigh
d.DevotedProductWeigh = utils.Round(m.DevotedProductWeigh(), 1)
d.ProductWeigh = utils.Round(m.RealProductWeigh(), 1)
d.SecondLevelWeigh = utils.Round(m.SecondLevelWeigh, 1)
d.QualificationRate = m.QualificationRate()
d.CreatedAt = m.ProductDate
d.AuthFlag = domain.CheckOrgAuth(orgId, m.OrgId)
... ...
... ... @@ -3,6 +3,7 @@ package constant
import (
"os"
"strconv"
"strings"
)
var (
... ... @@ -10,6 +11,10 @@ var (
MANUFACTURE_DEFAULT_COMPANYID = 1
// 生产制造 - 称重系统 - 默认组织
MANUFACTURE_DEFAULT_ORGID = 1
// 产品类型列表
MANUFACTURE_PRODUCT_TYPE = "0502010004ST,0502010004ST"
MapProductType = make(map[int]string)
)
func init() {
... ... @@ -19,4 +24,11 @@ func init() {
if os.Getenv("MANUFACTURE_DEFAULT_ORGID") != "" {
MANUFACTURE_DEFAULT_ORGID, _ = strconv.Atoi(os.Getenv("MANUFACTURE_DEFAULT_ORGID"))
}
if os.Getenv("MANUFACTURE_PRODUCT_TYPE") != "" {
MANUFACTURE_PRODUCT_TYPE = os.Getenv("MANUFACTURE_PRODUCT_TYPE")
}
productTypes := strings.Split(MANUFACTURE_PRODUCT_TYPE, ",")
for i, v := range productTypes {
MapProductType[i+1] = v
}
}
... ...
package domain
import "gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
// 包馅机
type DeviceBaoXianJi struct {
InterSpeed int64 `json:"InterSpeed"` // 内包材速度:内包材运行速率
... ... @@ -58,3 +60,10 @@ type DeviceFengXiangJi struct {
ProductType int `json:"ProductType"`
ProductType1 string `json:"ProductType1"` // 产品类型:当前产品种类
}
func ProductTypeToProductCode(productType int) string {
if v, ok := constant.MapProductType[productType]; ok {
return v
}
return ""
}
... ...
... ... @@ -3,6 +3,7 @@ package domain
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/log"
"time"
)
... ... @@ -142,7 +143,8 @@ func (d *DeviceRunningRecordInfo) AddTimeLineDeviceStatus(t time.Time, data *Dev
if t.IsZero() {
return
}
key := fmt.Sprintf("%v", t.Hour())
key := fmt.Sprintf("%v", t.Local().Hour())
log.Logger.Debug(fmt.Sprintf("time:%v hour:%v", t, key))
var v *HourDeviceStatus
var ok bool
if v, ok = d.TimeLineDeviceStatus[key]; !ok {
... ... @@ -251,6 +253,9 @@ func (d *DeviceRunningRecordInfo) HourDeviceStatusDetail(endTime int) map[string
begin = end + 1
end = begin
}
if end >= endTime {
break
}
index = index << 1
}
}
... ... @@ -279,7 +284,7 @@ type HourDeviceStatus struct {
// 更新启动状态
func (d *HourDeviceStatus) UpdateUp(t time.Time, up int) {
m := t.Minute()
m := t.Local().Minute()
bit := 1 << (m / d.Window)
if up&1 == 0 {
return
... ...
... ... @@ -303,7 +303,8 @@ func (ptr *PGCommonStatisticsService) DeviceRunningStatistics(queryOptions map[s
}
}
if r != nil {
m := r.UpdatedAt.Local().Hour()*60 + r.UpdatedAt.Minute()
//log.Logger.Debug(fmt.Sprintf("%v %v",r.UpdatedAt.Local().Hour(),r.UpdatedAt.Local().Minute()))
m := r.UpdatedAt.Local().Hour()*60 + r.UpdatedAt.Local().Minute()
if r.UpdatedAt.Before(utils.GetZeroTime(time.Now())) {
m = 60 * 24
}
... ...
... ... @@ -28,7 +28,7 @@ func (ptr *PGWorkshopDataConsumeService) Consume(companyId, orgId int, record *d
planId int
err error
plan *domain.ProductPlanDispatchRecord
datetime time.Time
//datetime time.Time
)
var (
deviceRepository, _ = repository.NewDeviceRepository(ptr.transactionContext)
... ... @@ -78,9 +78,9 @@ func (ptr *PGWorkshopDataConsumeService) Consume(companyId, orgId int, record *d
// 封箱机、串串机需要定位到批次
if record.DeviceType == domain.DeviceTypeFengXiangJi || record.DeviceType == domain.DeviceTypeChuanChuanJi {
datetime, _ = time.Parse("2006-01-02", deviceRunningData.Date)
if plan, err = ptr.findDeviceProductPlan(companyId, orgId, workStation.WorkStationId, datetime, deviceRunningData.ProductType); err != nil {
log.Logger.Error(err.Error())
//datetime, _ = time.Parse("2006-01-02 15:04:05", deviceRunningData.Date)
if plan, err = ptr.findDeviceProductPlan(companyId, orgId, workStation.WorkStationId, utils.GetZeroTimeWithLocal(deviceRunningData.CollectionTime, time.UTC), deviceRunningData.ProductType); err != nil {
log.Logger.Error(err.Error(), map[string]interface{}{"workstation": workStation, "product_code": deviceRunningData.ProductType})
} else {
planId = plan.PlanDispatchRecordExt.ProductPlanId
}
... ... @@ -171,7 +171,7 @@ func (ptr *PGWorkshopDataConsumeService) newDeviceRunningData(record *domain.Dev
break
}
data.Count = int(deviceChuanChuanJi.Count)
data.ProductType = deviceChuanChuanJi.ProductType1
data.ProductType = domain.ProductTypeToProductCode(deviceChuanChuanJi.ProductType)
if data.Date, err = formatDate(deviceChuanChuanJi.Year, deviceChuanChuanJi.Month, deviceChuanChuanJi.Day); err != nil {
return nil, err
}
... ... @@ -194,7 +194,8 @@ func (ptr *PGWorkshopDataConsumeService) newDeviceRunningData(record *domain.Dev
break
}
data.Count = int(deviceFengKouJi.Count)
data.ProductType = deviceFengKouJi.ProductType1
//data.ProductType = deviceFengKouJi.ProductType1
data.ProductType = domain.ProductTypeToProductCode(deviceFengKouJi.ProductType)
if data.Date, err = formatDate(deviceFengKouJi.Year, deviceFengKouJi.Month, deviceFengKouJi.Day); err != nil {
return nil, err
}
... ... @@ -207,7 +208,8 @@ func (ptr *PGWorkshopDataConsumeService) newDeviceRunningData(record *domain.Dev
break
}
data.Count = int(deviceFengXiangJi.Count)
data.ProductType = deviceFengXiangJi.ProductType1
//data.ProductType = deviceFengXiangJi.ProductType1
data.ProductType = domain.ProductTypeToProductCode(deviceFengXiangJi.ProductType)
if data.Date, err = formatDate(deviceFengXiangJi.Year, deviceFengXiangJi.Month, deviceFengXiangJi.Day); err != nil {
return nil, err
}
... ... @@ -227,7 +229,7 @@ func (ptr *PGWorkshopDataConsumeService) newDeviceRunningRecord(companyId, orgId
DeviceId: device.DeviceId,
DeviceCode: device.DeviceCode,
DeviceRunningRecordInfo: data,
CreatedAt: time.Now(),
CreatedAt: data.CollectionTime,
}, nil
}
... ... @@ -243,8 +245,8 @@ func (ptr *PGWorkshopDataConsumeService) newProductRecord(companyId int, orgId i
ProductRecordInfo: &domain.ProductRecordInfo{
ProductDate: data.CollectionTime.Local().Format("2006-01-02"),
Original: float64(data.Count),
Weigh: float64(data.Count) * DefaultCCJUnitQuantity,
WeighBefore: float64(data.Count) * DefaultCCJUnitQuantity,
Weigh: utils.Round(float64(data.Count)*DefaultCCJUnitQuantity, 1),
WeighBefore: utils.Round(float64(data.Count)*DefaultCCJUnitQuantity, 1),
ApproveStatus: domain.AttendanceNotApprove,
ProductPlanId: plan.PlanDispatchRecordExt.ProductPlanId,
PlanProductName: plan.PlanDispatchRecordExt.PlanProductName,
... ...
... ... @@ -52,6 +52,11 @@ func GetZeroTime(d time.Time) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, time.Local)
}
// GetZeroTime 获取某一天的0点时间
func GetZeroTimeWithLocal(d time.Time, loc *time.Location) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, loc)
}
// GetNextDayZeroTime 获取某一天的23点59分59秒
func GetNextDayZeroTime(d time.Time) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, 1)
... ...
package utils
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
"time"
... ... @@ -52,3 +53,51 @@ func TestComputeTimeDuration(t *testing.T) {
assert.Equal(t, input.ts, out)
}
}
const TIME_LAYOUT = "2006-01-02 03:04:05"
func TestTimeParse(t *testing.T) {
timeParse()
}
func timeParse() {
fmt.Println("0. now: ", time.Now())
str := "2018-09-10 10:00:00"
fmt.Println("1. str: ", str)
t, _ := time.Parse(TIME_LAYOUT, str)
fmt.Println("2. Parse time: ", t)
tStr := t.Format(TIME_LAYOUT)
fmt.Println("3. Format time str: ", tStr)
name, offset := t.Zone()
name2, offset2 := t.Local().Zone()
fmt.Printf("4. Zone name: %v, Zone offset: %v\n", name, offset)
fmt.Printf("5. Local Zone name: %v, Local Zone offset: %v\n", name2, offset2)
tLocal := t.Local()
tUTC := t.UTC()
fmt.Printf("6. t: %v, Local: %v, UTC: %v\n", t, tLocal, tUTC)
fmt.Printf("7. t: %v, Local: %v, UTC: %v\n", t.Format(TIME_LAYOUT), tLocal.Format(TIME_LAYOUT), tUTC.Format(TIME_LAYOUT))
fmt.Printf("8. Local.Unix: %v, UTC.Unix: %v\n", tLocal.Unix(), tUTC.Unix())
str2 := "1969-12-31 23:59:59"
t2, _ := time.Parse(TIME_LAYOUT, str2)
fmt.Printf("9. str2:%v,time: %v, Unix: %v\n", str2, t2, t2.Unix())
fmt.Printf("10. %v, %v\n", tLocal.Format(time.ANSIC), tUTC.Format(time.ANSIC))
fmt.Printf("11. %v, %v\n", tLocal.Format(time.RFC822), tUTC.Format(time.RFC822))
fmt.Printf("12. %v, %v\n", tLocal.Format(time.RFC822Z), tUTC.Format(time.RFC822Z))
//指定时区
parseWithLocation("America/Cordoba", str)
parseWithLocation("Asia/Shanghai", str)
parseWithLocation("Asia/Beijing", str)
}
func parseWithLocation(name string, timeStr string) (time.Time, error) {
locationName := name
if l, err := time.LoadLocation(locationName); err != nil {
println(err.Error())
return time.Time{}, err
} else {
lt, _ := time.ParseInLocation(TIME_LAYOUT, timeStr, l)
fmt.Println(locationName, lt)
return lt, nil
}
}
... ...
package mqtt
import (
"encoding/json"
pahomqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/linmadan/egglib-go/utils/json"
"github.com/linmadan/egglib-go/utils/tool_funs"
"github.com/tidwall/gjson"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/application/deviceCollection/command"
... ... @@ -27,6 +27,13 @@ func OnReceiveData(client pahomqtt.Client, message pahomqtt.Message) {
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)
}
if collectionTime.IsZero() {
return
}
for key, item := range payload {
if key == "WorkShop" {
continue
... ... @@ -38,7 +45,7 @@ func OnReceiveData(client pahomqtt.Client, message pahomqtt.Message) {
deviceCollection := &domain.DeviceCollection{
WorkShopName: workShop.(string),
DeviceSn: key,
CollectionTime: time.Now(),
CollectionTime: collectionTime,
StartupStatus: gjson.Get(string(message.Payload()), key+".StartupState").Int(),
ComStatus: gjson.Get(string(message.Payload()), key+".ComStatus").Int(),
}
... ... @@ -137,6 +144,7 @@ func OnReceiveData(client pahomqtt.Client, message pahomqtt.Message) {
}
}
}
//TODO:日志先注释
log.Logger.Info("MQTT", map[string]interface{}{
"Topic": message.Topic(),
"MessageId": message.MessageID(),
... ...
... ... @@ -18,7 +18,8 @@ func WorkshopDataConsumer(c context.Context, t *asynq.Task) error {
if err := json.Unmarshal(t.Payload(), cmd); err != nil {
return err
}
log.Logger.Debug(fmt.Sprintf("【车间数据消费】 消费 设备:%v 消息号:%v 时间:%v ", cmd.DeviceCollection.DeviceSn, cmd.DeviceCollectionId, cmd.CollectionTime))
log.Logger.Debug(fmt.Sprintf("【车间数据消费】 消费 设备:%v 消息号:%v 时间:%v ", cmd.DeviceCollection.DeviceSn, cmd.DeviceCollectionId, cmd.CollectionTime),
map[string]interface{}{"p-message": cmd.DeviceCollection})
cmd.CompanyId = constant.MANUFACTURE_DEFAULT_COMPANYID
cmd.OrgId = constant.MANUFACTURE_DEFAULT_ORGID
_, err := svr.WorkshopConsume(cmd)
... ...