作者 yangfu

refactor: 车间数据统计修改、修正批次同步批次号重复、时间蹉校正

... ... @@ -91,7 +91,7 @@ func AutoFlushDeviceDailyRunningRecordOEE(ctx context.Context) error {
log.Logger.Error(err.Error())
continue
}
if err := redis.SaveDeviceDailyRunningRecord(v); err != nil {
if err := redis.SaveDeviceDailyRunningRecord(v.ProductDate, v); err != nil {
log.Logger.Error(err.Error(), map[string]interface{}{"task": "定时刷新设备每日运行记录"})
continue
}
... ...
package syncdata
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/infrastructure/domainService"
... ... @@ -523,16 +524,25 @@ func (srv *PullDataK3CloudService) SyncDataProductPlan(ptr *pgTransaction.Transa
continue
}
// 计划信息
plan, err = productPlanRepository.FindOne(map[string]interface{}{"companyId": cid, "orgId": oid, "batchNumber": v.BillNo})
plan, err = productPlanRepository.FindOne(map[string]interface{}{"companyId": cid, "orgId": oid, "erpBillNo": v.BillNo, "productCode": v.MaterialNumber})
if err == nil && plan != nil {
plan.UpdatedAt = time.Now()
//plan.UpdatedAt = time.Now()
continue
}
count, _, errFindPlan := productPlanRepository.Find(map[string]interface{}{"companyId": cid, "orgId": oid, "erpBillNo": v.BillNo, "productCode": v.MaterialNumber})
if errFindPlan != nil {
continue
}
/* ERP系统BillNO + MaterialNumber 唯一 会重复,需要做自增格式化处理*/
batchNumber := fmt.Sprintf("%v-1", v.BillNo)
if count >= 1 {
batchNumber = fmt.Sprintf("%v-%v", v.BillNo, count)
}
if err == domain.ErrorNotFound {
plan = &domain.ProductPlan{
CompanyId: cid,
OrgId: oid,
BatchNumber: v.BillNo,
BatchNumber: batchNumber,
ProductDate: v.PlanStartDate,
Workshop: workshop.CloneSample(),
WorkOn: 0,
... ... @@ -557,6 +567,7 @@ func (srv *PullDataK3CloudService) SyncDataProductPlan(ptr *pgTransaction.Transa
ProductCode: product.ProductCode,
ProductName: product.ProductName,
DevotedUnit: "份",
ErpBillNo: v.BillNo,
})
plan.PlanProductName = product.ProductName
plan.Remark = v.Description
... ...
... ... @@ -20,16 +20,16 @@ var MQTT_TOPIC = "/MQTT"
//var MQTT_USER = ""
//var MQTT_PASSWORD = ""
var MQTT_HOST = "175.24.122.87"
var MQTT_PORT = "1883"
var MQTT_USER = "user111"
var MQTT_PASSWORD = "user111"
//var MQTT_HOST = "175.24.122.87"
//var MQTT_PORT = "1883"
//var MQTT_USER = "user111"
//var MQTT_PASSWORD = "user111"
//内网测试地址
//var MQTT_HOST = "192.168.100.222"
//var MQTT_PORT = "1883"
//var MQTT_USER = "admin"
//var MQTT_PASSWORD = "123456"
var MQTT_HOST = "47.97.5.102"
var MQTT_PORT = "6000"
var MQTT_USER = "admin"
var MQTT_PASSWORD = "123456"
func init() {
if os.Getenv("MQTT_HOST") != "" {
... ...
... ... @@ -78,6 +78,8 @@ func (deviceDailyRunningRecord *DeviceDailyRunningRecord) String() string {
// 设备运行记录信息
type DeviceRunningRecordInfo struct {
// 日期
ProductDate string `json:"productDate"`
// 设备类型
DeviceType string `json:"deviceType"`
// 当前状态
... ...
... ... @@ -12,4 +12,6 @@ type ProductPlanExt struct {
//ProductSpec *UnitQuantity `json:"productSpec,omitempty"`
// 投入量规格 默认份
DevotedUnit string `json:"devotedUnit" `
// ERP系统里面的 bill_no
ErpBillNo string `json:"erpBillNo" `
}
... ...
... ... @@ -164,7 +164,7 @@ from ts_product_list
func (dao *DeviceDailyRunningRecordDao) DeviceRunningInfo(companyId, orgId int, deviceCode string, result interface{}) error {
tx := dao.transactionContext.PgTx
sql := fmt.Sprintf(`
select * from device_running_info(?,?,?);
select * from manufacture.device_running_info(?,?,?);
`)
if _, err := tx.Query(result, sql, deviceCode, companyId, orgId); err != nil {
return err
... ...
... ... @@ -110,7 +110,7 @@ func (ptr *PGWorkshopDataConsumeService) Consume(companyId, orgId int, record *d
SendProductRecordStaticsJob(productRecord)
}
}
if err = redis.SaveDeviceDailyRunningRecord(deviceDailyRecord); err != nil {
if err = redis.SaveDeviceDailyRunningRecord(deviceRunningData.CollectionTime, deviceDailyRecord); err != nil {
return nil, err
}
return nil, nil
... ... @@ -293,6 +293,7 @@ func (ptr *PGWorkshopDataConsumeService) saveDeviceDailyRunningRecord(companyId,
recordInfo.ProductPlanId = planId
recordInfo.DeviceName = device.DeviceName
recordInfo.OrgName = device.Ext.OrgName
recordInfo.ProductDate = data.CollectionTime.Format("2006-01-02")
record = &domain.DeviceDailyRunningRecord{
CompanyId: companyId,
OrgId: orgId,
... ...
... ... @@ -32,9 +32,9 @@ func getDeviceDailyRunningRecord(client *redis.Client, key string) (*domain.Devi
}
// 保存每日设备运行数据
func SaveDeviceDailyRunningRecord(record *domain.DeviceDailyRunningRecord) error {
func SaveDeviceDailyRunningRecord(t time.Time, record *domain.DeviceDailyRunningRecord) error {
client := GetRedis()
key := DeviceDailyRunningRecordKey(record.ProductDate, record.DeviceCode)
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)
... ...
... ... @@ -151,6 +151,12 @@ func (repository *ProductPlanRepository) FindOne(queryOptions map[string]interfa
query.SetWhereByQueryOption("company_id = ?", "companyId")
query.SetWhereByQueryOption("org_id = ?", "orgId")
query.SetWhereByQueryOption("batch_number=?", "batchNumber")
if v, ok := queryOptions["erpBillNo"]; ok && len(v.(string)) > 0 {
query.Where(fmt.Sprintf(`ext#>'{productPlanExt,erpBillNo}' = '"%v"'`, v))
}
if v, ok := queryOptions["productCode"]; ok && len(v.(string)) > 0 {
query.Where(fmt.Sprintf(`ext#>'{productPlanExt,productCode}' = '"%v"'`, v))
}
if v, ok := queryOptions["includeDeleted"]; ok && v.(bool) {
query.AllWithDeleted()
}
... ... @@ -181,6 +187,12 @@ func (repository *ProductPlanRepository) Find(queryOptions map[string]interface{
if v, ok := queryOptions["workshopName"]; ok && len(v.(string)) > 0 {
query.Where(fmt.Sprintf(`workshop->>'workshopName' like '%%%v%%'`, v))
}
if v, ok := queryOptions["erpBillNo"]; ok && len(v.(string)) > 0 {
query.Where(fmt.Sprintf(`ext#>'{productPlanExt,erpBillNo}' = '"%v"'`, v))
}
if v, ok := queryOptions["productCode"]; ok && len(v.(string)) > 0 {
query.Where(fmt.Sprintf(`ext#>'{productPlanExt,productCode}' = '"%v"'`, v))
}
query.SetWhereByQueryOption("plan_status=?", "planStatus")
query.SetOffsetAndLimit(domain.MaxQueryRow)
query.SetOrderDirect("product_plan_id", "DESC")
... ...
package utils
import (
"fmt"
"github.com/linmadan/egglib-go/utils/json"
"testing"
"time"
)
type User struct {
Age time.Time
AgeZero time.Time
AgeZeroFormat string
}
func TestTimeParseInLocal(t *testing.T) {
timeStr := "2022-01-02 1:00:00"
tl, _ := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
u1 := User{
Age: tl,
AgeZero: GetZeroTime(tl),
}
u1.AgeZeroFormat = u1.AgeZero.Format("2006-01-02")
fmt.Println(json.MarshalToString(u1), u1.Age.Local())
u2 := User{
Age: tl,
AgeZero: GetZeroTimeWithLocal(tl, time.Local),
}
u2.AgeZeroFormat = u2.AgeZero.Format("2006-01-02")
fmt.Println(json.MarshalToString(u2), u2.Age.Local())
}
... ...
... ... @@ -55,7 +55,7 @@ func TestComputeTimeDuration(t *testing.T) {
}
}
const TIME_LAYOUT = "2006-01-02 03:04:05"
const TIME_LAYOUT = "2006-01-02 15:04:05"
func TestTimeParse(t *testing.T) {
fmt.Println(int(-1))
... ... @@ -64,11 +64,13 @@ func TestTimeParse(t *testing.T) {
func timeParse() {
fmt.Println("0. now: ", time.Now())
str := "2018-09-10 10:00:00"
str := "2018-09-10 16:00:00"
fmt.Println("1. str: ", str)
t, _ := time.Parse(TIME_LAYOUT, str)
fmt.Println("2. Parse time: ", t)
t, err := time.Parse(TIME_LAYOUT, str)
fmt.Println("2. Parse time: ", t, err)
fmt.Println("2.1. Parse time: ", t.Local(), t.Local().Local())
parseInLocal, _ := time.ParseInLocation(TIME_LAYOUT, str, time.Local)
fmt.Println("2.2 parse in local", parseInLocal, parseInLocal.Local())
tStr := t.Format(TIME_LAYOUT)
fmt.Println("3. Format time str: ", tStr)
name, offset := t.Zone()
... ...