diff --git a/conf/config.go b/conf/config.go index 6ca5226..f3201eb 100644 --- a/conf/config.go +++ b/conf/config.go @@ -8,7 +8,6 @@ import ( var DbConfig *EnvConfig type EnvConfig struct { - Project string `yaml:"project"` Finr int `yaml:"finr"` Mongdbname string `yaml:"mongdbname"` Mongdbip string `yaml:"mongdbip"` diff --git a/meta/base/Plant.meta.go b/meta/base/Plant.meta.go index ef72edd..9346380 100644 --- a/meta/base/Plant.meta.go +++ b/meta/base/Plant.meta.go @@ -36,6 +36,7 @@ var Plant_TaxId = grmi.NewField("TaxId", "[Plant].TaxId", "taxid", grmi.TypeStri var Plant_AccountBank = grmi.NewField("AccountBank", "[Plant].AccountBank", "accountbank", grmi.TypeString) var Plant_AccountNr = grmi.NewField("AccountNr", "[Plant].AccountNr", "accountnr", grmi.TypeInt) var Plant_Currency = grmi.NewField("Currency", "[Plant].Currency", "currency", grmi.TypeString) +var Plant_PlantProject = grmi.NewField("PlantProject", "[Plant].PlantProject", "plantproject", grmi.TypeString) var Plant_LastModify = grmi.NewField("LastModify", "[Plant].LastModify", "lastmodify", grmi.TypeDateTime) var Plant_LastUser = grmi.NewField("LastUser", "[Plant].LastUser", "lastuser", grmi.TypeString) var Plant_CreateTime = grmi.NewField("CreateTime", "[Plant].CreateTime", "createtime", grmi.TypeDateTime) @@ -73,9 +74,10 @@ var Plant = grmi.NewEntity( Plant_AccountBank.Name: Plant_AccountBank, Plant_AccountNr.Name: Plant_AccountNr, Plant_Currency.Name: Plant_Currency, + Plant_PlantProject.Name: Plant_PlantProject, Plant_LastModify.Name: Plant_LastModify, Plant_LastUser.Name: Plant_LastUser, Plant_CreateTime.Name: Plant_CreateTime, }, []string{}, - []string{"Status", "Name1", "Name2", "Logo", "Abrevname", "Country", "Street", "Address", "PostCode", "Phone", "Fax", "StartYear", "EndYear", "CompanyTitle", "CompanyAddress", "TaxId", "AccountBank", "AccountNr", "Currency", "LastUser"}) + []string{"Status", "Name1", "Name2", "Logo", "Abrevname", "Country", "Street", "Address", "PostCode", "Phone", "Fax", "StartYear", "EndYear", "CompanyTitle", "CompanyAddress", "TaxId", "AccountBank", "AccountNr", "Currency", "PlantProject", "LastUser"}) diff --git a/meta/jit/PackOrderPlcError.meta.go b/meta/jit/PackOrderPlcError.meta.go index 05c7c2b..d470608 100644 --- a/meta/jit/PackOrderPlcError.meta.go +++ b/meta/jit/PackOrderPlcError.meta.go @@ -24,6 +24,13 @@ var PackOrderPlcError_ErrorInfo = grmi.NewField("ErrorInfo", "[JIT_PackOrderPlcE var PackOrderPlcError_ErrorType = grmi.NewField("ErrorType", "[JIT_PackOrderPlcError].ErrorType", "errortype", grmi.TypeString) var PackOrderPlcError_Location = grmi.NewField("Location", "[JIT_PackOrderPlcError].Location", "location", grmi.TypeString) var PackOrderPlcError_SeatData = grmi.NewField("SeatData", "[JIT_PackOrderPlcError].SeatData", "seatdata", grmi.TypeString) +var PackOrderPlcError_Source = grmi.NewField("Source", "[JIT_PackOrderPlcError].Source", "source", grmi.TypeString) +var PackOrderPlcError_StrCtrl1 = grmi.NewField("StrCtrl1", "[JIT_PackOrderPlcError].StrCtrl1", "strctrl1", grmi.TypeString) +var PackOrderPlcError_StrCtrl2 = grmi.NewField("StrCtrl2", "[JIT_PackOrderPlcError].StrCtrl2", "strctrl2", grmi.TypeString) +var PackOrderPlcError_StrCtrl3 = grmi.NewField("StrCtrl3", "[JIT_PackOrderPlcError].StrCtrl3", "strctrl3", grmi.TypeString) +var PackOrderPlcError_IntCtrl1 = grmi.NewField("IntCtrl1", "[JIT_PackOrderPlcError].IntCtrl1", "intctrl1", grmi.TypeInt) +var PackOrderPlcError_IntCtrl2 = grmi.NewField("IntCtrl2", "[JIT_PackOrderPlcError].IntCtrl2", "intctrl2", grmi.TypeInt) +var PackOrderPlcError_IntCtrl3 = grmi.NewField("IntCtrl3", "[JIT_PackOrderPlcError].IntCtrl3", "intctrl3", grmi.TypeInt) var PackOrderPlcError_LastModify = grmi.NewField("LastModify", "[JIT_PackOrderPlcError].LastModify", "lastmodify", grmi.TypeDateTime) var PackOrderPlcError_LastUser = grmi.NewField("LastUser", "[JIT_PackOrderPlcError].LastUser", "lastuser", grmi.TypeString) var PackOrderPlcError_CreateTime = grmi.NewField("CreateTime", "[JIT_PackOrderPlcError].CreateTime", "createtime", grmi.TypeDateTime) @@ -49,9 +56,16 @@ var PackOrderPlcError = grmi.NewEntity( PackOrderPlcError_ErrorType.Name: PackOrderPlcError_ErrorType, PackOrderPlcError_Location.Name: PackOrderPlcError_Location, PackOrderPlcError_SeatData.Name: PackOrderPlcError_SeatData, + PackOrderPlcError_Source.Name: PackOrderPlcError_Source, + PackOrderPlcError_StrCtrl1.Name: PackOrderPlcError_StrCtrl1, + PackOrderPlcError_StrCtrl2.Name: PackOrderPlcError_StrCtrl2, + PackOrderPlcError_StrCtrl3.Name: PackOrderPlcError_StrCtrl3, + PackOrderPlcError_IntCtrl1.Name: PackOrderPlcError_IntCtrl1, + PackOrderPlcError_IntCtrl2.Name: PackOrderPlcError_IntCtrl2, + PackOrderPlcError_IntCtrl3.Name: PackOrderPlcError_IntCtrl3, PackOrderPlcError_LastModify.Name: PackOrderPlcError_LastModify, PackOrderPlcError_LastUser.Name: PackOrderPlcError_LastUser, PackOrderPlcError_CreateTime.Name: PackOrderPlcError_CreateTime, }, []string{"PlantNr"}, - []string{"Status", "Step", "ErrorInfo", "ErrorType", "Location", "SeatData", "LastUser"}) + []string{"Status", "Step", "ErrorInfo", "ErrorType", "Location", "SeatData", "Source", "StrCtrl1", "StrCtrl2", "StrCtrl3", "IntCtrl1", "IntCtrl2", "IntCtrl3", "LastUser"}) diff --git a/models/base/Const.go b/models/base/Const.go index 11782c9..ad92ef2 100644 --- a/models/base/Const.go +++ b/models/base/Const.go @@ -6,6 +6,9 @@ const ( PROJECT_ACURA = "ACURA" PROJECT_NANSHA = "nansha" + WORKPLACE_CONIFG_WIRTE_SEAT = "SEAT" + WORKPLACE_CONFIG_WRITE_SIGN = "SIGN" + // 工单绑定来源类型 ORDER_IDENTIFY_SOURCE_MONITOR = "MONITOR" ORDER_IDENTIFY_SOURCE_PRODUCE = "PRODUCE" diff --git a/models/base/Plant.model.go b/models/base/Plant.model.go index e6e91bb..fbfe38a 100644 --- a/models/base/Plant.model.go +++ b/models/base/Plant.model.go @@ -40,6 +40,7 @@ type Plant struct { AccountBank string `xorm:"nvarchar(100) 'AccountBank' not null" json:"Plant-AccountBank"` AccountNr int `xorm:"int 'AccountNr' not null" json:"Plant-AccountNr"` Currency string `xorm:"nvarchar(20) 'Currency' not null" json:"Plant-Currency"` + PlantProject string `xorm:"nvarchar(20) 'PlantProject' not null" json:"Plant-PlantProject"` LastModify grmi.DateTime `xorm:"datetime 'LastModify' not null updated" json:"Plant-LastModify"` LastUser string `xorm:"nvarchar(20) 'LastUser' not null" json:"Plant-LastUser"` CreateTime grmi.DateTime `xorm:"datetime 'CreateTime' not null created" json:"Plant-CreateTime"` diff --git a/models/jit/PackOrderPlcError.model.go b/models/jit/PackOrderPlcError.model.go index e526209..8ce2669 100644 --- a/models/jit/PackOrderPlcError.model.go +++ b/models/jit/PackOrderPlcError.model.go @@ -25,9 +25,16 @@ type PackOrderPlcError struct { Status int `xorm:"int 'Status' not null" json:"JIT_PackOrderPlcError-Status"` Step int `xorm:"int 'Step' not null" json:"JIT_PackOrderPlcError-Step"` ErrorInfo string `xorm:"nvarchar(500) 'ErrorInfo' not null" json:"JIT_PackOrderPlcError-ErrorInfo"` - ErrorType string `xorm:"nvarchar(20) 'ErrorType' not null" json:"PLCErrorInfo-ErrorType"` + ErrorType string `xorm:"nvarchar(20) 'ErrorType' not null" json:"PLCErrorInfo-ErrorType"` Location string `xorm:"nvarchar(40) 'Location' not null" json:"JIT_PackOrderPlcError-Location"` SeatData string `xorm:"nvarchar(100) 'SeatData' not null" json:"JIT_PackOrderPlcError-SeatData"` + Source string `xorm:"nvarchar(100) 'Source' not null" json:"JIT_PackOrderPlcError-Source"` + StrCtrl1 string `xorm:"nvarchar(100) 'StrCtrl1' null" json:"JIT_PackOrderPlcError-StrCtrl1"` + StrCtrl2 string `xorm:"nvarchar(100) 'StrCtrl2' null" json:"JIT_PackOrderPlcError-StrCtrl2"` + StrCtrl3 string `xorm:"nvarchar(100) 'StrCtrl3' null" json:"JIT_PackOrderPlcError-StrCtrl3"` + IntCtrl1 int `xorm:"int 'IntCtrl1' null" json:"JIT_PackOrderPlcError-IntCtrl1"` + IntCtrl2 int `xorm:"int 'IntCtrl2' null" json:"JIT_PackOrderPlcError-IntCtrl2"` + IntCtrl3 int `xorm:"int 'IntCtrl3' null" json:"JIT_PackOrderPlcError-IntCtrl3"` LastModify grmi.DateTime `xorm:"datetime 'LastModify' not null updated" json:"JIT_PackOrderPlcError-LastModify"` LastUser string `xorm:"nvarchar(20) 'LastUser' not null" json:"JIT_PackOrderPlcError-LastUser"` CreateTime grmi.DateTime `xorm:"datetime 'CreateTime' not null created" json:"JIT_PackOrderPlcError-CreateTime"` diff --git a/models/jit/const.go b/models/jit/const.go index 51c3833..9ccd3b3 100644 --- a/models/jit/const.go +++ b/models/jit/const.go @@ -21,6 +21,10 @@ const ( PACK_TYPE_STD = "STD" PACK_TYPE_INT = "INT" + // 合格、不合格包装 + PACK_QUALITY_OK = "OK" + PACK_QUALITY_NG = "NG" + // 发运状态 SHIP_STATUS_UNPLANED = 10 // 未计划 SHIP_STATUS_PLANNED = 20 // 已计划 diff --git a/plc/plc.go b/plc/plc.go new file mode 100644 index 0000000..e299052 --- /dev/null +++ b/plc/plc.go @@ -0,0 +1,137 @@ +package plc + +import ( + "LAPP_ACURA_MOM_BACKEND/conf" + "LAPP_ACURA_MOM_BACKEND/grmi" + "LAPP_ACURA_MOM_BACKEND/infra/logger" + baseModel "LAPP_ACURA_MOM_BACKEND/models/base" + "LAPP_ACURA_MOM_BACKEND/utils" + json "github.com/json-iterator/go" + "github.com/kataras/iris/v12" +) + +// ReadDataFromPLC 从PLC读取数据 +func ReadDataFromPLC(data baseModel.PLC, log logger.Log, dataType string) (interface{}, error) { + postData, err := json.Marshal(data) + if err != nil { + log.Error("序列化读数据请求数据失败, error:" + err.Error()) + return "", grmi.NewBusinessError("序列化读取数据请求数据失败, error:" + err.Error()) + } + log.Debug("读取数据请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCReadBackend) + boday, err := utils.Post(conf.DbConfig.PLCReadBackend, postData) + if err != nil { + log.Error("读取数据调用失败, error:" + err.Error()) + return "", grmi.NewBusinessError("读取数据调用失败, error:" + err.Error()) + } + log.Debug("读取数据请求响应:" + string(boday)) + + var response baseModel.Response + err = json.Unmarshal(boday, &response) + if err != nil { + log.Error("解析读取数据后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("解析读取数据后台数据失败, error:" + err.Error()) + } + if response.Code != iris.StatusOK { + log.Error("读取数据调用后台失败, 后台返回错误, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("读取数据调用后台失败, 后台返回错误, 错误:" + response.Msg) + } + plcData, ok := response.Data.(map[string]interface{}) + if !ok { + log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + successInterface, exist := plcData["IsSuccess"] + if !exist { + log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + success, ok := successInterface.(bool) + if !ok { + log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + if !success { + log.Error("读取数据后台plc执行失败, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("读取数据后台plc执行失败, 后台数据:" + string(boday)) + } + content, exist := plcData["Content"] + if !exist { + log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + if dataType == "String" { + stringContent, ok := content.(string) + if !ok { + log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + if !ok { + log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + length := len(stringContent) + for index, item := range stringContent { + if item == 0 { + length = index + break + } + } + return stringContent[:length], nil + } else if dataType == "Boolean" { + boolContent, ok := content.(bool) + if !ok { + log.Error("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + return false, grmi.NewBusinessError("读取数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + return boolContent, nil + } else { + return nil, grmi.NewBusinessError("不支持的数据类型") + } +} + +// WriteDataToPLC 向plc中写入数据 +func WriteDataToPLC(data baseModel.PLC, log logger.Log) error { + postData, err := json.Marshal(data) + if err != nil { + log.Error("序列化数据请求数据失败, error:" + err.Error()) + return grmi.NewBusinessError("序列化数据请求数据失败, error:" + err.Error()) + } + log.Debug("写入数据请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCWriteBackend) + boday, err := utils.Post(conf.DbConfig.PLCWriteBackend, postData) + if err != nil { + log.Error("写入数据调用失败, error:" + err.Error()) + return grmi.NewBusinessError("写入数据调用失败, error:" + err.Error()) + } + log.Debug("写入数据请求响应:" + string(boday)) + + var response baseModel.Response + err = json.Unmarshal(boday, &response) + if err != nil { + log.Error("解析写入数据后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) + return grmi.NewBusinessError("解析写入数据后台数据失败, error:" + err.Error()) + } + if response.Code != iris.StatusOK { + log.Error("写入数据调用后台失败, 后台返回错误, 后台数据:" + string(boday)) + return grmi.NewBusinessError("写入数据调用后台失败, 后台返回错误, 错误:" + response.Msg) + } + plcData, ok := response.Data.(map[string]interface{}) + if !ok { + log.Error("写入数据后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("写入数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + successInterface, exist := plcData["IsSuccess"] + if !exist { + log.Error("写入数据后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("写入数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + success, ok := successInterface.(bool) + if !ok { + log.Error("写入数据后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("写入数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + if !success { + log.Error("写入数据后台plc执行失败, 后台数据:" + string(boday)) + return grmi.NewBusinessError("写入数据后台plc执行失败, 后台数据:" + string(boday)) + } + return nil +} \ No newline at end of file diff --git a/services/base/implments/Plant.service.impl.go b/services/base/implments/Plant.service.impl.go index 8bae8b9..e489e3c 100644 --- a/services/base/implments/Plant.service.impl.go +++ b/services/base/implments/Plant.service.impl.go @@ -45,6 +45,7 @@ var DefaultConditionOfPlant = grmi.NewCondition( meta.Plant_AccountBank.UrlParameterName: grmi.NewConditionItem(meta.Plant_AccountBank, grmi.Equal, false), meta.Plant_AccountNr.UrlParameterName: grmi.NewConditionItem(meta.Plant_AccountNr, grmi.Equal, false), meta.Plant_Currency.UrlParameterName: grmi.NewConditionItem(meta.Plant_Currency, grmi.Equal, false), + meta.Plant_PlantProject.UrlParameterName: grmi.NewConditionItem(meta.Plant_PlantProject, grmi.Equal, false), meta.Plant_LastModify.UrlParameterName: grmi.NewConditionItem(meta.Plant_LastModify, grmi.Approximate, false), meta.Plant_LastUser.UrlParameterName: grmi.NewConditionItem(meta.Plant_LastUser, grmi.Equal, false), meta.Plant_CreateTime.UrlParameterName: grmi.NewConditionItem(meta.Plant_CreateTime, grmi.Approximate, false), @@ -396,7 +397,7 @@ func (impl *PlantServiceImplement) GetAllProjectsOfPlant(user *global.User) ([]m projectDataLi := make([]model.PlantDataTree, 0, len(projectLi)) for _, project := range projectLi { projectData := model.PlantDataTree{ - ItemId: project.ProjectId, + ItemId: project.ProjectId, ItemDesc: project.Descr, } productFamilyLi, err := productFamilyDao.Select([]grmi.Predicate{meMeta.ProductFamily_ProjectId.NewPredicate(grmi.Equal, project.ProjectId)}, []grmi.Field{meMeta.ProductFamily_ProductFamilyId}) @@ -406,7 +407,7 @@ func (impl *PlantServiceImplement) GetAllProjectsOfPlant(user *global.User) ([]m productFamilyDataLi := make([]model.PlantDataTree, 0, len(productFamilyLi)) for _, productFamily := range productFamilyLi { productFamilyData := model.PlantDataTree{ - ItemId: productFamily.ProductFamilyId, + ItemId: productFamily.ProductFamilyId, ItemDesc: productFamily.Descr, } productLi, err := productFamilyRelateDao.SelectProductByFamilyId(project.ProjectId, productFamily.ProductFamilyId) @@ -416,9 +417,9 @@ func (impl *PlantServiceImplement) GetAllProjectsOfPlant(user *global.User) ([]m productDataLi := make([]model.PlantDataTree, 0, len(productLi)) for _, product := range productLi { productData := model.PlantDataTree{ - ItemId: product.ProductId, + ItemId: product.ProductId, ItemDesc: product.Descr, - IsEnd: true, + IsEnd: true, } productDataLi = append(productDataLi, productData) } @@ -435,7 +436,6 @@ func (impl *PlantServiceImplement) GetAllProjectsOfPlant(user *global.User) ([]m } - /****************************************************************************** * * @Reference LAPP_ACURA_MOM_BACKEND/services/base/PlantService.GetAllWorkLineOfPlant @@ -454,7 +454,7 @@ func (impl *PlantServiceImplement) GetAllWorkLineOfPlant(user *global.User) ([]m result := make([]model.WorkLineOfPlant, 0, 1) for _, plant := range plantLi { plantData := model.WorkLineOfPlant{ - ItemId: plant.PlantNr, + ItemId: plant.PlantNr, ItemDesc: plant.Name1, Children: make([]model.WorkLineOfPlant, 0), } @@ -465,13 +465,13 @@ func (impl *PlantServiceImplement) GetAllWorkLineOfPlant(user *global.User) ([]m } for _, workLine := range workLineLi { data := model.WorkLineOfPlant{ - ItemId: workLine.WorkLineid, + ItemId: workLine.WorkLineid, ItemDesc: workLine.Descr, - IsEnd: true, + IsEnd: true, } plantData.Children = append(plantData.Children, data) } result = append(result, plantData) } return result, nil -} \ No newline at end of file +} diff --git a/services/jit/PackOrder.service.go b/services/jit/PackOrder.service.go index bba996b..5e33e7d 100644 --- a/services/jit/PackOrder.service.go +++ b/services/jit/PackOrder.service.go @@ -215,7 +215,7 @@ type PackOrderService interface { * @Date : 2021-10-22 * ******************************************************************************/ - ScanBarcode(user *global.User, barcode string) (*model.PackOrderInfo, error) + ScanBarcode(user *global.User, barcode string, workplaceNr int) (*model.PackOrderInfo, error) /****************************************************************************** * * @Function Name : ClosePackOrder diff --git a/services/jit/implments/PackOrder.service.impl.go b/services/jit/implments/PackOrder.service.impl.go index 1e5b1b3..3452d3b 100644 --- a/services/jit/implments/PackOrder.service.impl.go +++ b/services/jit/implments/PackOrder.service.impl.go @@ -11,21 +11,14 @@ import ( "LAPP_ACURA_MOM_BACKEND/db" "LAPP_ACURA_MOM_BACKEND/global" "LAPP_ACURA_MOM_BACKEND/grmi" - "LAPP_ACURA_MOM_BACKEND/infra/logger" baseMeta "LAPP_ACURA_MOM_BACKEND/meta/base" meta "LAPP_ACURA_MOM_BACKEND/meta/jit" omMeta "LAPP_ACURA_MOM_BACKEND/meta/om" baseModel "LAPP_ACURA_MOM_BACKEND/models/base" model "LAPP_ACURA_MOM_BACKEND/models/jit" - meModel "LAPP_ACURA_MOM_BACKEND/models/me" omModel "LAPP_ACURA_MOM_BACKEND/models/om" - plnModel "LAPP_ACURA_MOM_BACKEND/models/pln" - "LAPP_ACURA_MOM_BACKEND/utils" "encoding/hex" - "github.com/go-xorm/xorm" - uuid "github.com/iris-contrib/go.uuid" - json "github.com/json-iterator/go" - "github.com/kataras/iris/v12" + "strconv" "strings" "time" ) @@ -406,13 +399,23 @@ func (impl *PackOrderServiceImplement) Update(user *global.User, entities *[]mod } // ScanBarcode 包装扫描条码 -func (impl *PackOrderServiceImplement) ScanBarcode(user *global.User, barcode string) (*model.PackOrderInfo, error) { +func (impl *PackOrderServiceImplement) ScanBarcode(user *global.User, barcode string, workplaceNr int) (*model.PackOrderInfo, error) { grmi.Log(user, "/services/jit/implments/PackOrder.service.impl.go", "ScanBarcode", "扫描零件条码") result := &model.PackOrderInfo{} result.PLCResult = true engine := db.Eloquent.Master() session := engine.NewSession() defer session.Close() + var ( + // 打包工位 + packWorkplace *baseModel.WorkPlace + // 打包工位配置 + packWorkplaceConfig *baseModel.WorkPlaceConfig + // 工厂归属项目 + plantProject string + // 当前工单的合格状态 + currentSerialOrderStatus = true + ) snrDao := baseDal.NewSnrDAO(session, user.PlantNr, user.UserId) packLstDAO := dal.NewPackOrderItemLstDAO(session, user.PlantNr, user.UserId) packOrderDao := dal.NewPackOrderDAO(session, user.PlantNr, user.UserId) @@ -420,7 +423,20 @@ func (impl *PackOrderServiceImplement) ScanBarcode(user *global.User, barcode st packOrderStatusHistoryDao := dal.NewPackOrderStatusHistoryDAO(session, user.PlantNr, user.UserId) projectDao := meDal.NewProjectDAO(session, user.PlantNr, user.UserId) packOrderPlcErrDao := dal.NewPackOrderPlcErrorDAO(session, user.PlantNr, user.UserId) - if conf.DbConfig.Project == baseModel.PROJECT_NANSHA { + plantDao := baseDal.NewPlantDAO(session, user.UserId) + workplaceDao := baseDal.NewWorkPlaceDAO(session, user.PlantNr, user.UserId) + workplaceConfigDao := baseDal.NewWorkPlaceConfigDAO(session, user.PlantNr, user.UserId) + plant, err := plantDao.SelectOne(user.PlantNr) + if err != nil { + return nil, grmi.NewBusinessError("查询工厂数据失败, 错误:" + err.Error()) + } + if plant == nil { + return nil, grmi.NewBusinessError("用户归属工厂不存在, 工厂ID:" + strconv.Itoa(user.PlantNr)) + } + plantProject = plant.PlantProject + // 根据不同的项目做不同的预处理 + if plantProject == baseModel.PROJECT_NANSHA { + // 南沙下线打包都需要写plc 先查询plc错误 有错误不能打包 errorLi, err := packOrderPlcErrDao.Select([]grmi.Predicate{meta.PackOrderPlcError_Location.NewPredicate(grmi.Equal, model.PACK_LOCATION_ON_LINE), meta.PackOrderPlcError_Status.NewPredicate(grmi.LessThen, model.PACK_STATUS_CLOSED)}, nil) if err != nil { @@ -429,6 +445,36 @@ func (impl *PackOrderServiceImplement) ScanBarcode(user *global.User, barcode st if len(errorLi) != 0 { return nil, grmi.NewBusinessError("当前存在未处理的座椅下线写入PLC错误") } + } else if plantProject == baseModel.PROJECT_ACURA { + // 讴歌下线打包需要选择工位 查询工位配置 如果需要记录plc错误 查询plc错误 有错误不能打包 + packWorkplace, err = workplaceDao.SelectOne(workplaceNr) + if err != nil { + return nil, grmi.NewBusinessError("查询工位失败, 错误:" + err.Error()) + } + if packWorkplace == nil { + return nil, grmi.NewBusinessError("打包工位不存在") + } + workplaceConfigLi, err := workplaceConfigDao.Select([]grmi.Predicate{ + baseMeta.WorkPlaceConfig_WorkPlaceNr.NewPredicate(grmi.Equal, workplaceNr), + }, nil) + if err != nil { + return nil, grmi.NewBusinessError("查询工位配置失败, 错误:" + err.Error()) + } + if len(workplaceConfigLi) == 0 { + return nil, grmi.NewBusinessError("缺少打包工位配置") + } + packWorkplaceConfig = &workplaceConfigLi[0] + // 需要写入plc 座椅数据 + if packWorkplaceConfig.StrValue1 == baseModel.WORKPLACE_CONIFG_WIRTE_SEAT { + errorLi, err := packOrderPlcErrDao.Select([]grmi.Predicate{meta.PackOrderPlcError_Location.NewPredicate(grmi.Equal, model.PACK_LOCATION_ON_LINE), + meta.PackOrderPlcError_Status.NewPredicate(grmi.LessThen, model.PACK_STATUS_CLOSED)}, nil) + if err != nil { + return nil, grmi.NewBusinessError("查询座椅下线写入PLC数据错误失败, error:" + err.Error()) + } + if len(errorLi) != 0 { + return nil, grmi.NewBusinessError("当前存在未处理的座椅下线写入PLC错误") + } + } } // 初始化全局的包装模板数据 if model.AllPackTemplate.Data == nil { @@ -473,6 +519,10 @@ func (impl *PackOrderServiceImplement) ScanBarcode(user *global.User, barcode st if serialOrder.QualityCheck != baseModel.QualityCheck_STATUS_OK && serialOrder.QualityCheck != baseModel.QualityCheck_STATUS_FAIL { return nil, grmi.NewBusinessError("工单未进行目检") } + if serialOrderStatus != baseModel.WO_STATUS_FINISHED || serialOrder.QualityCheck != baseModel.QualityCheck_STATUS_OK { + currentSerialOrderStatus = false + } + // 获取用户之前是否存在未关包的包装单 如果存在则使用之前的包装单ID 如果不存在则创建包装单 如果超过一个则报错 var packOrderId string packOrderLi, err := packOrderDao.SelectUserRunningPackOrder(user.UserId, model.PACK_LOCATION_ON_LINE) @@ -498,14 +548,13 @@ func (impl *PackOrderServiceImplement) ScanBarcode(user *global.User, barcode st } // 台套Key syncKey := serialOrderLi[0].SyncKey - if err := session.Begin(); err != nil { return nil, grmi.NewBusinessError("事务开启失败, error:" + err.Error()) } currentPackOrder := &model.PackOrderWithStatus{} // 如果当前用户不存在操作中未关闭的包装单, 则创建包装单数据 if packOrderId == "" { - var packTypeId string = model.PACK_TYPE_STD + var packTypeId = model.PACK_TYPE_STD //if serialOrder.OrderType == baseModel.ODER_TYPE_SEQ { // packTypeId = model.PACK_TYPE_STD //} else { @@ -526,6 +575,11 @@ func (impl *PackOrderServiceImplement) ScanBarcode(user *global.User, barcode st Operator: user.UserId, PackLocation: model.PACK_LOCATION_ON_LINE, } + if currentSerialOrderStatus { + packOrder.CtrlStr1 = model.PACK_QUALITY_OK + } else { + packOrder.CtrlStr1 = model.PACK_QUALITY_NG + } err = packOrderDao.InsertOne(&packOrder) if err != nil { _ = session.Rollback() @@ -584,40 +638,40 @@ func (impl *PackOrderServiceImplement) ScanBarcode(user *global.User, barcode st // _ = session.Rollback() // return nil, grmi.NewBusinessError("当前工单是序列工单,但是包装单是内部包装单。") // } - //currentPackOrder.PackOrder.ActQty += 1 - //currentPackOrder.PackOrder.PlanQty += 1 - //currentPackOrder.PackOrder.Operator = user.UserId - //err = packOrderDao.UpdateOne(¤tPackOrder.PackOrder) - //if err != nil { - // _ = session.Rollback() - // return nil, grmi.NewBusinessError("更新包装单数据失败, error:" + err.Error()) - //} - //maxPos, err := packLstDAO.GetMaxPost(packOrderId) - //if err != nil { - // _ = session.Rollback() - // return nil, grmi.NewBusinessError("查询包装单子项位置数据失败, error:" + err.Error()) - //} - //// 写入当前扫描的包装单子项 - //packOrderLst := model.PackOrderItemLst{ - // PackOrderId: packOrderId, - // Pos: maxPos + 1, - // PartId: artId, - // SerialOrderId: barcode, - // Status: model.PACK_STATUS_CLOSED, - // PlanQty: 1, - // ActQty: 1, - //} - //err = packLstDAO.InsertOne(&packOrderLst) - //if err != nil { - // _ = session.Rollback() - // return nil, grmi.NewBusinessError("插入包装单细则失败, error:" + err.Error()) - //} - //_ = session.Commit() - //result.IsClosed = false - //result.Message = "扫描完成, 包装单ID:" + currentPackOrder.PackOrder.PackOrderId - //result.PackOrderId = currentPackOrder.PackOrder.PackOrderId - //result.MatchTemplateLi = make([]model.PackTemplate, 0) - //return result, nil + //currentPackOrder.PackOrder.ActQty += 1 + //currentPackOrder.PackOrder.PlanQty += 1 + //currentPackOrder.PackOrder.Operator = user.UserId + //err = packOrderDao.UpdateOne(¤tPackOrder.PackOrder) + //if err != nil { + // _ = session.Rollback() + // return nil, grmi.NewBusinessError("更新包装单数据失败, error:" + err.Error()) + //} + //maxPos, err := packLstDAO.GetMaxPost(packOrderId) + //if err != nil { + // _ = session.Rollback() + // return nil, grmi.NewBusinessError("查询包装单子项位置数据失败, error:" + err.Error()) + //} + //// 写入当前扫描的包装单子项 + //packOrderLst := model.PackOrderItemLst{ + // PackOrderId: packOrderId, + // Pos: maxPos + 1, + // PartId: artId, + // SerialOrderId: barcode, + // Status: model.PACK_STATUS_CLOSED, + // PlanQty: 1, + // ActQty: 1, + //} + //err = packLstDAO.InsertOne(&packOrderLst) + //if err != nil { + // _ = session.Rollback() + // return nil, grmi.NewBusinessError("插入包装单细则失败, error:" + err.Error()) + //} + //_ = session.Commit() + //result.IsClosed = false + //result.Message = "扫描完成, 包装单ID:" + currentPackOrder.PackOrder.PackOrderId + //result.PackOrderId = currentPackOrder.PackOrder.PackOrderId + //result.MatchTemplateLi = make([]model.PackTemplate, 0) + //return result, nil //} else { // if currentPackOrder.PackOrder.PackTypeId != model.PACK_TYPE_INT { // _ = session.Rollback() @@ -708,6 +762,12 @@ func (impl *PackOrderServiceImplement) ScanBarcode(user *global.User, barcode st return nil, grmi.NewBusinessError("更新包装单状态失败, error:" + err.Error()) } } + // 如果当前包装为合格包装 判断当前工单 如果工单不合格 则修改包装为不合格 + if currentPackOrder.PackOrder.CtrlStr1 == model.PACK_QUALITY_OK { + if !currentSerialOrderStatus { + currentPackOrder.PackOrder.CtrlStr1 = model.PACK_QUALITY_NG + } + } err = packOrderDao.UpdateOne(¤tPackOrder.PackOrder) if err != nil { _ = session.Rollback() @@ -721,20 +781,30 @@ func (impl *PackOrderServiceImplement) ScanBarcode(user *global.User, barcode st result.IsClosed = false result.Message = "扫描完成, 包装单ID:" + currentPackOrder.PackOrder.PackOrderId } - if conf.DbConfig.Project == baseModel.PROJECT_NANSHA { + // 如果是南沙项目 已关包写入座椅数据 + if plantProject == baseModel.PROJECT_NANSHA { if result.IsClosed { - err = impl.SeatOffline(user, packOrderId, projectId, model.PACK_LOCATION_ON_LINE, ¤tPackOrder.PackOrder) + err = impl.NSPackPLC(user, packOrderId, projectId, model.PACK_LOCATION_ON_LINE, ¤tPackOrder.PackOrder) if err != nil { result.PLCResult = false result.PLCMessage = err.Error() } } } else { + // 如果是讴歌项目 如果是讴歌线关包 写plc放行 如果是缤智线 写座椅数据 if result.IsClosed { - err = impl.PLCPermitThrough() - if err != nil { - result.PLCResult = false - result.PLCMessage = err.Error() + if packWorkplaceConfig.StrValue1 == baseModel.WORKPLACE_CONFIG_WRITE_SIGN { + err = impl.AUCRAPackPLC() + if err != nil { + result.PLCResult = false + result.PLCMessage = err.Error() + } + } else if packWorkplaceConfig.StrValue1 == baseModel.WORKPLACE_CONIFG_WIRTE_SEAT { + err = impl.BZPackPLC(user, packWorkplaceConfig, packOrderId, currentPackOrder.PackOrder.CtrlStr1, true, 0) + if err != nil { + result.PLCResult = false + result.PLCMessage = err.Error() + } } } } @@ -759,9 +829,8 @@ func (impl *PackOrderServiceImplement) ScanBarcode(user *global.User, barcode st return nil, grmi.NewBusinessError("插入包装单细则失败, error:" + err.Error()) } } - } - + // 查询当前包装单下的所有子项 用于做模板匹配 lstLi, err := packLstDAO.Select([]grmi.Predicate{meta.PackOrderItemLst_PackOrderId.NewPredicate(grmi.Equal, packOrderId)}, []grmi.Field{meta.PackOrderItemLst_Pos}) if err != nil { _ = session.Rollback() @@ -863,18 +932,31 @@ func (impl *PackOrderServiceImplement) ScanBarcode(user *global.User, barcode st result.Message = "扫描完成, 包装单ID:" + currentPackOrder.PackOrder.PackOrderId + ", 并完成包装单" result.PackOrderId = currentPackOrder.PackOrder.PackOrderId result.MatchTemplateLi = matchLi - if conf.DbConfig.Project == baseModel.PROJECT_NANSHA { - err = impl.SeatOffline(user, packOrderId, projectId, model.PACK_LOCATION_ON_LINE, ¤tPackOrder.PackOrder) - if err != nil { - result.PLCResult = false - result.PLCMessage = err.Error() + // 如果是南沙项目 已关包写入座椅数据 + if plantProject == baseModel.PROJECT_NANSHA { + if result.IsClosed { + err = impl.NSPackPLC(user, packOrderId, projectId, model.PACK_LOCATION_ON_LINE, ¤tPackOrder.PackOrder) + if err != nil { + result.PLCResult = false + result.PLCMessage = err.Error() + } } - } else { - err = impl.PLCPermitThrough() - if err != nil { - result.PLCResult = false - result.PLCMessage = err.Error() + // 如果是讴歌项目 如果是讴歌线关包 写plc放行 如果是缤智线 写座椅数据 + if result.IsClosed { + if packWorkplaceConfig.StrValue1 == baseModel.WORKPLACE_CONFIG_WRITE_SIGN { + err = impl.AUCRAPackPLC() + if err != nil { + result.PLCResult = false + result.PLCMessage = err.Error() + } + } else if packWorkplaceConfig.StrValue1 == baseModel.WORKPLACE_CONIFG_WIRTE_SEAT { + err = impl.BZPackPLC(user, packWorkplaceConfig, packOrderId, currentPackOrder.PackOrder.CtrlStr1, true, 0) + if err != nil { + result.PLCResult = false + result.PLCMessage = err.Error() + } + } } } return result, nil @@ -916,20 +998,30 @@ func (impl *PackOrderServiceImplement) ScanBarcode(user *global.User, barcode st result.Message = "扫描完成, 包装单ID:" + currentPackOrder.PackOrder.PackOrderId + ", 并完成包装单" result.PackOrderId = currentPackOrder.PackOrder.PackOrderId result.MatchTemplateLi = matchLi - if conf.DbConfig.Project == baseModel.PROJECT_NANSHA { + // 如果是南沙项目 已关包写入座椅数据 + if plantProject == baseModel.PROJECT_NANSHA { if result.IsClosed { - err = impl.SeatOffline(user, packOrderId, projectId, model.PACK_LOCATION_ON_LINE, ¤tPackOrder.PackOrder) + err = impl.NSPackPLC(user, packOrderId, projectId, model.PACK_LOCATION_ON_LINE, ¤tPackOrder.PackOrder) if err != nil { result.PLCResult = false result.PLCMessage = err.Error() } } } else { + // 如果是讴歌项目 如果是讴歌线关包 写plc放行 如果是缤智线 写座椅数据 if result.IsClosed { - err = impl.PLCPermitThrough() - if err != nil { - result.PLCResult = false - result.PLCMessage = err.Error() + if packWorkplaceConfig.StrValue1 == baseModel.WORKPLACE_CONFIG_WRITE_SIGN { + err = impl.AUCRAPackPLC() + if err != nil { + result.PLCResult = false + result.PLCMessage = err.Error() + } + } else if packWorkplaceConfig.StrValue1 == baseModel.WORKPLACE_CONIFG_WIRTE_SEAT { + err = impl.BZPackPLC(user, packWorkplaceConfig, packOrderId, currentPackOrder.PackOrder.CtrlStr1, true, 0) + if err != nil { + result.PLCResult = false + result.PLCMessage = err.Error() + } } } } @@ -1391,7 +1483,16 @@ func (impl *PackOrderServiceImplement) ScanBarcodeOutLine(user *global.User, bar packOrderStatusHistoryDao := dal.NewPackOrderStatusHistoryDAO(session, user.PlantNr, user.UserId) projectDao := meDal.NewProjectDAO(session, user.PlantNr, user.UserId) packOrderPlcErrDao := dal.NewPackOrderPlcErrorDAO(session, user.PlantNr, user.UserId) - if conf.DbConfig.Project == baseModel.PROJECT_NANSHA { + plantDao := baseDal.NewPlantDAO(session, user.UserId) + plant, err := plantDao.SelectOne(user.PlantNr) + if err != nil { + return nil, grmi.NewBusinessError("查询工厂数据失败, 错误:" + err.Error()) + } + if plant == nil { + return nil, grmi.NewBusinessError("用户归属工厂不存在, 工厂ID:" + strconv.Itoa(user.PlantNr)) + } + plantProject := plant.PlantProject + if plantProject == baseModel.PROJECT_NANSHA { errorLi, err := packOrderPlcErrDao.Select([]grmi.Predicate{meta.PackOrderPlcError_Location.NewPredicate(grmi.Equal, model.PACK_LOCATION_ON_LINE), meta.PackOrderPlcError_Status.NewPredicate(grmi.LessThen, model.PACK_STATUS_CLOSED)}, nil) if err != nil { @@ -1610,9 +1711,9 @@ func (impl *PackOrderServiceImplement) ScanBarcodeOutLine(user *global.User, bar result.IsClosed = false result.Message = "扫描完成, 包装单ID:" + currentPackOrder.PackOrder.PackOrderId } - if conf.DbConfig.Project == baseModel.PROJECT_NANSHA { + if plantProject == baseModel.PROJECT_NANSHA { if result.IsClosed { - err = impl.SeatOffline(user, packOrderId, projectId, model.PACK_LOCATION_ON_LINE, ¤tPackOrder.PackOrder) + err = impl.NSPackPLC(user, packOrderId, projectId, model.PACK_LOCATION_ON_LINE, ¤tPackOrder.PackOrder) if err != nil { result.PLCResult = false result.PLCMessage = err.Error() @@ -1745,9 +1846,9 @@ func (impl *PackOrderServiceImplement) ScanBarcodeOutLine(user *global.User, bar result.Message = "扫描完成, 包装单ID:" + currentPackOrder.PackOrder.PackOrderId + ", 并完成包装单" result.PackOrderId = currentPackOrder.PackOrder.PackOrderId result.MatchTemplateLi = matchLi - if conf.DbConfig.Project == baseModel.PROJECT_NANSHA { + if plantProject == baseModel.PROJECT_NANSHA { if result.IsClosed { - err = impl.SeatOffline(user, packOrderId, projectId, model.PACK_LOCATION_ON_LINE, ¤tPackOrder.PackOrder) + err = impl.NSPackPLC(user, packOrderId, projectId, model.PACK_LOCATION_ON_LINE, ¤tPackOrder.PackOrder) if err != nil { result.PLCResult = false result.PLCMessage = err.Error() @@ -1793,9 +1894,9 @@ func (impl *PackOrderServiceImplement) ScanBarcodeOutLine(user *global.User, bar result.Message = "扫描完成, 包装单ID:" + currentPackOrder.PackOrder.PackOrderId + ", 并完成包装单" result.PackOrderId = currentPackOrder.PackOrder.PackOrderId result.MatchTemplateLi = matchLi - if conf.DbConfig.Project == baseModel.PROJECT_NANSHA { + if plantProject == baseModel.PROJECT_NANSHA { if result.IsClosed { - err = impl.SeatOffline(user, packOrderId, projectId, model.PACK_LOCATION_ON_LINE, ¤tPackOrder.PackOrder) + err = impl.NSPackPLC(user, packOrderId, projectId, model.PACK_LOCATION_ON_LINE, ¤tPackOrder.PackOrder) if err != nil { result.PLCResult = false result.PLCMessage = err.Error() @@ -1879,414 +1980,6 @@ func (impl *PackOrderServiceImplement) ScanBarcodeOutLine(user *global.User, bar } } -// SeatOffline 座椅入库 plc交互逻辑 -func (impl *PackOrderServiceImplement) SeatOffline(user *global.User, packOrderId string, projectId string, location string, packOrder *model.PackOrder) error { - // 写入plc数据格式:第一位质量,第二位安道拓还是鹰泰生产,第三到二十七位为5种座椅四位机种代码+一位颜色代码合起来 - // 第一位:1为合格,0为不合格 - // 第二位:1为安道拓,0为樱泰 - engine := db.Eloquent.Master() - session := engine.NewSession() - defer session.Close() - packOrderDao := dal.NewPackOrderDAO(session, user.PlantNr, user.UserId) - seatData, err := impl.MakeUpSeatData(session, user, packOrderId, location, projectId) - if err != nil { - innerErr := impl.SaveOfflineError(session, user, packOrderId, 0, err.Error(), location, "") - if innerErr != nil { - return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) - } - return err - } - if seatData == "" { - return nil - } - packOrder.RFID = seatData - err = packOrderDao.UpdateOne(packOrder) - if err != nil { - errorInfo := "保存座椅数据至包装单失败, 错误:" + err.Error() - innerErr := impl.SaveOfflineError(session, user, packOrderId, 1, err.Error(), location, seatData) - if innerErr != nil { - return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + errorInfo) - } - return grmi.NewBusinessError(errorInfo) - } - err, step := impl.WriteSeatDataToPLC(seatData, location) - if err != nil { - innerErr := impl.SaveOfflineError(session, user, packOrderId, step, err.Error(), location, seatData) - if innerErr != nil { - return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) - } - return err - } - return nil -} - -// SaveOfflineError 将座椅下线写入plc阶段的错误保存到数据表 -func (impl *PackOrderServiceImplement) SaveOfflineError(session *xorm.Session, user *global.User, packOrderId string, step int, errInfo string, location string, seatData string) error { - packOrderPlcErrDao := dal.NewPackOrderPlcErrorDAO(session, user.PlantNr, user.UserId) - errData := model.PackOrderPlcError{ - PackOrderId: packOrderId, - Status: model.PACK_STATUS_PLANNED, - Step: step, - ErrorType: model.ERROR_TYTPE_PLC, - ErrorInfo: errInfo, - Location: location, - SeatData: seatData, - } - return packOrderPlcErrDao.InsertOne(&errData) -} - -// WriteSeatDataToPLC 将座椅下线写入plc -func (impl *PackOrderServiceImplement) WriteSeatDataToPLC(seatData string, location string) (error, int) { - // 1. 读取地址位 检验是否可以写入 - var plcAddress, DBAddress, plcType string - var dbLen int - if location == model.PACK_LOCATION_ON_LINE { - plcAddress = conf.DbConfig.OfflinePLCAddress - DBAddress = conf.DbConfig.OfflineDBSign - plcType = conf.DbConfig.OfflinePLCType - } else { - plcAddress = conf.DbConfig.RepairPlcAddress - DBAddress = conf.DbConfig.RepairPlcReadSign - plcType = conf.DbConfig.RepairPlcType - } - valueType := "Boolean" - retryCount := conf.DbConfig.OfflinePLCRetry - data := baseModel.PLC{ - PLCType: plcType, - PLCAddress: plcAddress, - ValueAddress: DBAddress, - ValueType: valueType, - RetryCount: retryCount, - } - err := impl.SeatOfflineReadSign(data) - if err != nil { - return err, 2 - } - // 2. 写入座椅数据 - if location == model.PACK_LOCATION_ON_LINE { - DBAddress = conf.DbConfig.OfflineDBSave - dbLen = conf.DbConfig.OfflineDBSaveLen - } else { - DBAddress = conf.DbConfig.RepairPlcWrite - dbLen = conf.DbConfig.RepairPlcWriteLen - } - valueType = "Bytes" - data = baseModel.PLC{ - PLCType: plcType, - PLCAddress: plcAddress, - ValueAddress: DBAddress, - ValueType: valueType, - RetryCount: retryCount, - ValueText: seatData, - ByteCount: dbLen, - } - err = impl.SeatOfflineWriteSeatData(data) - if err != nil { - return err, 3 - } - - // 3. 写入保存完成 - if location == model.PACK_LOCATION_ON_LINE { - DBAddress = conf.DbConfig.OfflineDBSign - } else { - DBAddress = conf.DbConfig.RepairPlcReadSign - } - valueType = "Boolean" - data = baseModel.PLC{ - PLCType: plcType, - PLCAddress: plcAddress, - ValueAddress: DBAddress, - ValueType: valueType, - RetryCount: retryCount, - ValueText: true, - } - err = impl.SeatOfflineWriteSign(data) - if err != nil { - return err, 4 - } - return nil, 0 -} - -// SeatOfflineReadSign 座椅入库 读取标识位 -func (impl *PackOrderServiceImplement) SeatOfflineReadSign(data baseModel.PLC) error { - log, _ := logger.NewLogger("plc", "JIT") - var retryNum int - for { - postData, err := json.Marshal(data) - if err != nil { - log.Error("座椅下线plc写入, 序列化读取标识位请求数据失败, error:" + err.Error()) - return grmi.NewBusinessError("座椅下线plc写入, 序列化读取标识位请求数据失败, error:" + err.Error()) - } - log.Debug("座椅下线plc写入, 读取标识位请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCReadBackend) - boday, err := utils.Post(conf.DbConfig.PLCReadBackend, postData) - if err != nil { - log.Error("座椅下线plc写入, 读取标识位调用失败, error:" + err.Error()) - return grmi.NewBusinessError("座椅下线plc写入, 读取标识位调用失败, error:" + err.Error()) - } - log.Debug("座椅下线plc写入, 读取标识位请求响应:" + string(boday)) - - var response baseModel.Response - err = json.Unmarshal(boday, &response) - if err != nil { - log.Error("座椅下线plc写入, 解析读取标识位后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 解析读取标识位后台数据失败, error:" + err.Error()) - } - if response.Code != iris.StatusOK { - log.Error("座椅下线plc写入,读取标识位调用后台失败, 后台返回错误, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入,读取标识位调用后台失败, 后台返回错误, 错误:" + response.Msg) - } - plcData, ok := response.Data.(map[string]interface{}) - if !ok { - log.Error("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - } - successInterface, exist := plcData["IsSuccess"] - if !exist { - log.Error("座椅下线plc写入, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) - } - success, ok := successInterface.(bool) - if !ok { - log.Error("座椅下线plc写入, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) - } - if !success { - log.Error("座椅下线plc写入, 读取写入标识后台plc执行失败, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 读取写入标识后台plc执行失败, 后台数据:" + string(boday)) - } - content, exist := plcData["Content"] - if !exist { - log.Error("座椅下线plc写入, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) - } - sign, ok := content.(bool) - if !ok { - log.Error("座椅下线plc写入, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) - } - if sign { - if retryNum >= 3 { - log.Error("座椅下线plc写入, 读取标识位不允许写入且达到最大次数, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 读取标识位不允许写入且达到最大次数, 后台数据:" + string(boday)) - } - log.Error("座椅下线plc写入, 读取标识位不允许写入, 后台数据:" + string(boday)) - retryNum++ - time.Sleep(time.Millisecond * 500) - continue - } else { - break - } - } - return nil -} - -// SeatOfflineWriteSeatData 座椅入库写入座椅数据 -func (impl *PackOrderServiceImplement) SeatOfflineWriteSeatData(data baseModel.PLC) error { - log, _ := logger.NewLogger("plc", "JIT") - data.ValueText = hex.EncodeToString([]byte(data.ValueText.(string))) - postData, err := json.Marshal(data) - if err != nil { - log.Error("座椅下线plc写入, 序列化座椅数据请求数据失败, error:" + err.Error()) - return grmi.NewBusinessError("座椅下线plc写入, 序列化座椅数据请求数据失败, error:" + err.Error()) - } - log.Debug("座椅下线plc写入, 写入座椅数据请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCWriteBackend) - boday, err := utils.Post(conf.DbConfig.PLCWriteBackend, postData) - if err != nil { - log.Error("座椅下线plc写入,写入座椅数据调用失败, error:" + err.Error()) - return grmi.NewBusinessError("座椅下线plc写入, 写入座椅数据调用失败, error:" + err.Error()) - } - log.Debug("座椅下线plc写入, 写入座椅数据请求响应:" + string(boday)) - - var response baseModel.Response - err = json.Unmarshal(boday, &response) - if err != nil { - log.Error("座椅下线plc写入, 解析写入座椅数据后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 解析写入座椅数据后台数据失败, error:" + err.Error()) - } - if response.Code != iris.StatusOK { - log.Error("座椅下线plc写入,写入座椅数据调用后台失败, 后台返回错误, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入,写入座椅数据调用后台失败, 后台返回错误, 错误:" + response.Msg) - } - plcData, ok := response.Data.(map[string]interface{}) - if !ok { - log.Error("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - } - successInterface, exist := plcData["IsSuccess"] - if !exist { - log.Error("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - } - success, ok := successInterface.(bool) - if !ok { - log.Error("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - } - if !success { - log.Error("座椅下线plc写入, 写入座椅数据后台plc执行失败, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 写入座椅数据后台plc执行失败, 后台数据:" + string(boday)) - } - return nil -} - -// SeatOfflineWriteSign 座椅入库写入标识位 -func (impl *PackOrderServiceImplement) SeatOfflineWriteSign(data baseModel.PLC) error { - log, _ := logger.NewLogger("plc", "JIT") - postData, err := json.Marshal(data) - if err != nil { - log.Error("座椅下线plc写入, 序列化保存完成请求数据失败, error:" + err.Error()) - return grmi.NewBusinessError("座椅下线plc写入, 序列化保存完成请求数据失败, error:" + err.Error()) - } - log.Debug("座椅下线plc写入, 写入保存完成请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCWriteBackend) - boday, err := utils.Post(conf.DbConfig.PLCWriteBackend, postData) - if err != nil { - log.Error("座椅下线plc写入,写入保存完成调用失败, error:" + err.Error()) - return grmi.NewBusinessError("座椅下线plc写入, 写入保存完成调用失败, error:" + err.Error()) - } - log.Debug("座椅下线plc写入, 写入保存完成请求响应:" + string(boday)) - var response baseModel.Response - err = json.Unmarshal(boday, &response) - if err != nil { - log.Error("座椅下线plc写入, 解析写入保存完成后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 解析写入保存完成后台数据失败, error:" + err.Error()) - } - if response.Code != iris.StatusOK { - log.Error("座椅下线plc写入,写入保存完成调用后台失败, 后台返回错误, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入,写入保存完成调用后台失败, 后台返回错误, 错误:" + response.Msg) - } - plcData, ok := response.Data.(map[string]interface{}) - if !ok { - log.Error("座椅下线plc写入, 写入保存完成后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 写入保存完成后台返回data格式不正确, 后台数据:" + string(boday)) - } - successInterface, exist := plcData["IsSuccess"] - if !exist { - log.Error("座椅下线plc写入, 写入保存完成后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 写入保存完成后台返回data格式不正确, 后台数据:" + string(boday)) - } - success, ok := successInterface.(bool) - if !ok { - log.Error("座椅下线plc写入, 写入保存完成后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 写入保存完成后台返回data格式不正确, 后台数据:" + string(boday)) - } - if !success { - log.Error("座椅下线plc写入, 写入保存完成后台plc执行失败, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅下线plc写入, 写入保存完成后台plc执行失败, 后台数据:" + string(boday)) - } - return nil -} - -// MakeUpSeatData 拼接座椅数据 -func (impl *PackOrderServiceImplement) MakeUpSeatData(session *xorm.Session, user *global.User, packOrderId string, location string, projectId string) (string, error) { - packLstDao := dal.NewPackOrderItemLstDAO(session, user.PlantNr, user.UserId) - productDao := meDal.NewProductDAO(session, user.PlantNr, user.UserId) - projectDao := meDal.NewProjectDAO(session, user.PlantNr, user.UserId) - serialOrderDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId) - packOrderItemLi, err := packLstDao.Select([]grmi.Predicate{meta.PackOrderItemLst_PackOrderId.NewPredicate(grmi.Equal, packOrderId), - meta.PackOrderItemLst_Status.NewPredicate(grmi.Equal, model.PACK_STATUS_CLOSED)}, []grmi.Field{meta.PackOrderItemLst_Pos}) - if len(packOrderItemLi) == 0 { - return "", nil - } - if err != nil { - errInfo := "查询包装单子项数据失败, error:" + err.Error() - return "", grmi.NewBusinessError(errInfo) - } - project, err := projectDao.SelectOne(projectId) - if err != nil { - errorInfo := "查询项目数据失败, 错误:" + err.Error() - return "", grmi.NewBusinessError(errorInfo) - } - serialOrderIdLi := make([]interface{}, 0, len(packOrderItemLi)) - var seatData string - seatMap := make(map[string]*meModel.Product) - for _, packOrderItem := range packOrderItemLi { - serialOrderIdLi = append(serialOrderIdLi, packOrderItem.SerialOrderId) - product, err := productDao.SelectOne(packOrderItem.PartId, projectId) - if err != nil { - errInfo := "查询包装单子项总成数据失败, error:" + err.Error() - innerErr := impl.SaveOfflineError(session, user, packOrderId, 0, errInfo, location, "") - if innerErr != nil { - return "", grmi.NewBusinessError(errInfo + ", 保存错误失败, error:" + err.Error()) - } - return "", err - } - if product == nil { - errInfo := "查询包装单子项总成不存在, 总成ID:" + packOrderItem.PartId - return "", grmi.NewBusinessError(errInfo) - } - seatMap[product.CtrlStr1] = product - } - product, exist := seatMap[meModel.PRODUCT_TYPE_MS] - if !exist { - errInfo := "包装单子项中不存在主驾" - return "", grmi.NewBusinessError(errInfo) - } - seatData += product.MachineCode - seatData += product.ColorValue - product, exist = seatMap[meModel.PRODUCT_TYPE_PS] - if !exist { - errInfo := "包装单子项中不存在副驾" - return "", grmi.NewBusinessError(errInfo) - } - seatData += product.MachineCode - seatData += product.ColorValue - product, exist = seatMap[meModel.PRODUCT_TYPE_40] - if !exist { - errInfo := "包装单子项中不存在40%" - return "", grmi.NewBusinessError(errInfo) - } - seatData += product.MachineCode - seatData += product.ColorValue - product, exist = seatMap[meModel.PRODUCT_TYPE_60] - if !exist { - errInfo := "包装单子项中不存在60%" - return "", grmi.NewBusinessError(errInfo) - } - seatData += product.MachineCode - seatData += product.ColorValue - product, exist = seatMap[meModel.PRODUCT_TYPE_CUSHION] - if !exist { - errInfo := "包装单子项中不存在坐垫" - return "", grmi.NewBusinessError(errInfo) - } - seatData += product.MachineCode - seatData += product.ColorValue - serialOrderLi, err := serialOrderDao.Select([]grmi.Predicate{omMeta.SerialOrder_SerialOrderId.NewPredicate(grmi.Include, serialOrderIdLi...)}, nil) - if err != nil { - errInfo := "查询包装单子项工单数据失败, error:" + err.Error() - return "", grmi.NewBusinessError(errInfo) - } - if len(serialOrderIdLi) != len(packOrderItemLi) { - errInfo := "查询包装单子项工单数据缺失" - return "", grmi.NewBusinessError(errInfo) - } - var status = `1` - for _, serialOrder := range serialOrderLi { - if serialOrder.SerialOrderStatus.Status != baseModel.WO_STATUS_FINISHED || serialOrder.QualityCheck != baseModel.QualityCheck_STATUS_OK { - status = `0` - } - } - seatData = status + "1" + seatData - if err = session.Begin(); err != nil { - return "", grmi.NewBusinessError("开启事务失败, 错误:" + err.Error()) - } - snrDao := baseDal.NewSnrDAO(session, user.PlantNr, user.UserId) - var num string - if location == model.PACK_LOCATION_ON_LINE { - num, err = snrDao.GetNextSnr(project.PackOrderRFIDSnr) - } else { - num, err = snrDao.GetNextSnr(project.PackOrderOutLineRFIDSnr) - } - if err != nil { - _ = session.Rollback() - return "", grmi.NewBusinessError("获取RFID流水号失败, 错误:" + err.Error()) - } - _ = session.Commit() - timeStr := time.Now().Format("060102") - seatData = seatData + timeStr + num - return seatData, nil -} - // RepairPlcErr 修复包装单和plc相互的错误 func (impl *PackOrderServiceImplement) RepairPlcErr(user *global.User, packOrderId string) error { engine := db.Eloquent.Master() @@ -2295,6 +1988,7 @@ func (impl *PackOrderServiceImplement) RepairPlcErr(user *global.User, packOrder packOrderDao := dal.NewPackOrderDAO(session, user.PlantNr, user.UserId) packOrderStatusDao := dal.NewPackOrderStatusDAO(session, user.PlantNr, user.UserId) packOrderPlcErrDao := dal.NewPackOrderPlcErrorDAO(session, user.PlantNr, user.UserId) + workplaceConfigDao := baseDal.NewWorkPlaceConfigDAO(session, user.PlantNr, user.UserId) packOrderError, err := packOrderPlcErrDao.SelectOne(packOrderId) if err != nil { return grmi.NewBusinessError("查询错误项失败, error:" + err.Error()) @@ -2305,789 +1999,196 @@ func (impl *PackOrderServiceImplement) RepairPlcErr(user *global.User, packOrder if packOrderError.Status == model.PACK_STATUS_CLOSED { return grmi.NewBusinessError("错误项已修复") } - // 座椅数据回写错误处理 - if packOrderError.Location == model.PACK_LOCATION_BACKFLUSH { - seatData := packOrderError.SeatData - var seatStr string - switch packOrderError.Step { - case -1: // plc数据解析成字符串 - seatBytes, err := hex.DecodeString(seatData) - if err != nil { - return grmi.NewBusinessError("解析座椅数据失败, 错误:" + err.Error()) - } - seatStr = string(seatBytes) - fallthrough - case -2: // 座椅数据绑定包装单 更新包装单状态 - packOrderLi, err := packOrderDao.Select([]grmi.Predicate{meta.PackOrder_RFID.NewPredicate(grmi.Equal, seatStr)}, nil) - if err != nil { - return grmi.NewBusinessError("查询包装单数据失败, 错误:" + err.Error()) - } - if len(packOrderLi) == 0 { - return grmi.NewBusinessError("不存RFID对应的包装单, RFID:" + seatStr) - } - if len(packOrderLi) > 1 { - return grmi.NewBusinessError("该RFID匹配到多个包装单, RFID:" + seatStr) - } - packOrder := packOrderLi[0] - packOrderStatus, err := packOrderStatusDao.SelectOne(packOrder.PackOrderId) - if err != nil { - return grmi.NewBusinessError("查询包装单状态数据失败, 错误:" + err.Error()) - } - packOrderStatus.ShipStatus = model.SHIP_STATUS_CLOSED - err = packOrderStatusDao.UpdateOne(packOrderStatus) - if err != nil { - return grmi.NewBusinessError("更新包装单状态失败, 错误:" + err.Error()) - } - packOrder.CtrlTime1 = grmi.DateTime(time.Now()) - err = packOrderDao.UpdateOne(&packOrder) - if err != nil { - return grmi.NewBusinessError("更新包装单回写时间失败, 错误:" + err.Error()) - } - } - return nil - } else { - // 座椅下线和线外打包plc错误处理 - plcType := conf.DbConfig.OfflinePLCType - retryCount := conf.DbConfig.OfflinePLCRetry - var plcAddress string - var DBAddress string - if packOrderError.Location == model.PACK_LOCATION_ON_LINE { - plcAddress = conf.DbConfig.OfflinePLCAddress - } - var seatData string - if packOrderError.Step != 0 { - seatData = packOrderError.SeatData - } - switch packOrderError.Step { - case 0, 1: // 组装座椅数据 - packOrder, err := packOrderDao.SelectOne(packOrderId) - if err != nil { - return grmi.NewBusinessError("查询包装单数据失败, error:" + err.Error()) - } - if packOrder == nil { - return grmi.NewBusinessError("包装单不存在") - } - seatData, err = impl.MakeUpSeatData(session, user, packOrderId, packOrderError.Location, packOrder.ProjectId) - if err != nil { - packOrderError.ErrorInfo = err.Error() - innerErr := packOrderPlcErrDao.UpdateOne(packOrderError) - if innerErr != nil { - return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) + switch packOrderError.Source { + case "NS": + // 座椅数据回写错误处理 + if packOrderError.Location == model.PACK_LOCATION_BACKFLUSH { + seatData := packOrderError.SeatData + var seatStr string + switch packOrderError.Step { + case -1: // plc数据解析成字符串 + seatBytes, err := hex.DecodeString(seatData) + if err != nil { + return grmi.NewBusinessError("解析座椅数据失败, 错误:" + err.Error()) } - return err - } - if seatData == "" { - return grmi.NewBusinessError("待写入座椅数据为空") - } - packOrder.RFID = seatData - err = packOrderDao.UpdateOne(packOrder) - if err != nil { - errorInfo := "保存座椅数据到包装单上失败, 错误:" + err.Error() - packOrderError.ErrorInfo = err.Error() - innerErr := packOrderPlcErrDao.UpdateOne(packOrderError) - if innerErr != nil { - return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) + seatStr = string(seatBytes) + fallthrough + case -2: // 座椅数据绑定包装单 更新包装单状态 + packOrderLi, err := packOrderDao.Select([]grmi.Predicate{meta.PackOrder_RFID.NewPredicate(grmi.Equal, seatStr)}, nil) + if err != nil { + return grmi.NewBusinessError("查询包装单数据失败, 错误:" + err.Error()) } - return grmi.NewBusinessError(errorInfo) - } - fallthrough - case 2, 3: // 读取标识位 写入座椅数据 - if packOrderError.Location == model.PACK_LOCATION_ON_LINE { - DBAddress = conf.DbConfig.OfflineDBSign - } - valueType := "Boolean" - data := baseModel.PLC{ - PLCType: plcType, - PLCAddress: plcAddress, - ValueAddress: DBAddress, - ValueType: valueType, - RetryCount: retryCount, - } - err = impl.SeatOfflineReadSign(data) - if err != nil { - packOrderError.Step = 1 - packOrderError.ErrorInfo = err.Error() - packOrderError.SeatData = seatData - innerErr := packOrderPlcErrDao.UpdateOne(packOrderError) - if innerErr != nil { - return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) + if len(packOrderLi) == 0 { + return grmi.NewBusinessError("不存RFID对应的包装单, RFID:" + seatStr) } - return err - } - if packOrderError.Location == model.PACK_LOCATION_ON_LINE { - DBAddress = conf.DbConfig.OfflineDBSave - } - valueType = "Bytes" - data = baseModel.PLC{ - PLCType: plcType, - PLCAddress: plcAddress, - ValueAddress: DBAddress, - ValueType: valueType, - RetryCount: retryCount, - ValueText: packOrderError.SeatData, - ByteCount: conf.DbConfig.OfflineDBSaveLen, - } - err = impl.SeatOfflineWriteSeatData(data) - if err != nil { - packOrderError.Step = 2 - packOrderError.ErrorInfo = err.Error() - packOrderError.SeatData = seatData - innerErr := packOrderPlcErrDao.UpdateOne(packOrderError) - if innerErr != nil { - return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) + if len(packOrderLi) > 1 { + return grmi.NewBusinessError("该RFID匹配到多个包装单, RFID:" + seatStr) } - return err - } - fallthrough - case 4: // 写入标识位数据 - if packOrderError.Location == model.PACK_LOCATION_ON_LINE { - DBAddress = conf.DbConfig.OfflineDBSign - } - valueType := "Boolean" - data := baseModel.PLC{ - PLCType: plcType, - PLCAddress: plcAddress, - ValueAddress: DBAddress, - ValueType: valueType, - RetryCount: retryCount, - ValueText: true, - } - err = impl.SeatOfflineWriteSign(data) - if err != nil { - packOrderError.Step = 3 - packOrderError.ErrorInfo = err.Error() - packOrderError.SeatData = seatData - innerErr := packOrderPlcErrDao.UpdateOne(packOrderError) - if innerErr != nil { - return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) + packOrder := packOrderLi[0] + packOrderStatus, err := packOrderStatusDao.SelectOne(packOrder.PackOrderId) + if err != nil { + return grmi.NewBusinessError("查询包装单状态数据失败, 错误:" + err.Error()) + } + packOrderStatus.ShipStatus = model.SHIP_STATUS_CLOSED + err = packOrderStatusDao.UpdateOne(packOrderStatus) + if err != nil { + return grmi.NewBusinessError("更新包装单状态失败, 错误:" + err.Error()) + } + packOrder.CtrlTime1 = grmi.DateTime(time.Now()) + err = packOrderDao.UpdateOne(&packOrder) + if err != nil { + return grmi.NewBusinessError("更新包装单回写时间失败, 错误:" + err.Error()) } - return err } - } - packOrderError.Status = model.PACK_STATUS_CLOSED - packOrderError.SeatData = seatData - err = packOrderPlcErrDao.UpdateOne(packOrderError) - if err != nil { - return grmi.NewBusinessError("问题已修复, 但是更新错误项数据状态失败请删除, error:" + err.Error()) - } - return nil - } -} - -// BackFlushReadSign 座椅回写 读取标识位 -func (impl *PackOrderServiceImplement) BackFlushReadSign() (bool, error) { - data := baseModel.PLC{ - PLCType: conf.DbConfig.BackFlushPlcType, - PLCAddress: conf.DbConfig.BackFlushPlcAddress, - ValueAddress: conf.DbConfig.BackFlushPlcReadSign, - ValueType: "Boolean", - RetryCount: conf.DbConfig.OfflinePLCRetry, - } - log, _ := logger.NewLogger("plc", "JIT") - var retryNum int - for { - postData, err := json.Marshal(data) - if err != nil { - log.Error("座椅数据回写, 序列化读取标识位请求数据失败, error:" + err.Error()) - return false, grmi.NewBusinessError("座椅数据回写, 序列化读取标识位请求数据失败, error:" + err.Error()) - } - log.Debug("座椅数据回写, 读取标识位请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCReadBackend) - boday, err := utils.Post(conf.DbConfig.PLCReadBackend, postData) - if err != nil { - log.Error("座椅数据回写, 读取标识位调用失败, error:" + err.Error()) - return false, grmi.NewBusinessError("座椅数据回写, 读取标识位调用失败, error:" + err.Error()) - } - log.Debug("座椅数据回写, 读取标识位请求响应:" + string(boday)) - - var response baseModel.Response - err = json.Unmarshal(boday, &response) - if err != nil { - log.Error("座椅数据回写, 解析读取标识位后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) - return false, grmi.NewBusinessError("座椅数据回写, 解析读取标识位后台数据失败, error:" + err.Error()) - } - if response.Code != iris.StatusOK { - log.Error("座椅数据回写,读取标识位调用后台失败, 后台返回错误, 后台数据:" + string(boday)) - return false, grmi.NewBusinessError("座椅数据回写,读取标识位调用后台失败, 后台返回错误, 错误:" + response.Msg) - } - plcData, ok := response.Data.(map[string]interface{}) - if !ok { - log.Error("座椅数据回写, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - return false, grmi.NewBusinessError("座椅数据回写, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) - } - successInterface, exist := plcData["IsSuccess"] - if !exist { - log.Error("座椅数据回写, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) - return false, grmi.NewBusinessError("座椅数据回写, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) - } - success, ok := successInterface.(bool) - if !ok { - log.Error("座椅数据回写, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) - return false, grmi.NewBusinessError("座椅数据回写, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) - } - if !success { - log.Error("座椅数据回写, 读取写入标识后台plc执行失败, 后台数据:" + string(boday)) - return false, grmi.NewBusinessError("座椅数据回写, 读取标识位后台plc执行失败, 后台数据:" + string(boday)) - } - content, exist := plcData["Content"] - if !exist { - log.Error("座椅数据回写, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) - return false, grmi.NewBusinessError("座椅数据回写, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) - } - sign, ok := content.(bool) - if !ok { - log.Error("座椅数据回写, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) - return false, grmi.NewBusinessError("座椅数据回写, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) - } - if sign { - return true, nil + return nil } else { - if retryNum >= 3 { - return false, nil - } - log.Error("座椅数据回写, 读取标识位不允许写入, 后台数据:" + string(boday)) - retryNum++ - time.Sleep(time.Millisecond * 500) - continue - } - } -} - -// BackFlushReadSeatData 座椅回写 读取座椅数据 -func (impl *PackOrderServiceImplement) BackFlushReadSeatData() (string, error) { - log, _ := logger.NewLogger("plc", "JIT") - data := baseModel.PLC{ - PLCType: conf.DbConfig.BackFlushPlcType, - PLCAddress: conf.DbConfig.BackFlushPlcAddress, - ValueAddress: conf.DbConfig.BackFlushPlcReadData, - ValueType: "Bytes", - RetryCount: conf.DbConfig.OfflinePLCRetry, - ByteCount: conf.DbConfig.BackFlushPlcReadLen, - } - postData, err := json.Marshal(data) - if err != nil { - log.Error("座椅数据回写, 序列化读取座椅数据请求数据失败, error:" + err.Error()) - return "", grmi.NewBusinessError("座椅数据回写, 序列化读取座椅数据请求数据失败, error:" + err.Error()) - } - log.Debug("座椅数据回写, 读取座椅数据请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCReadBackend) - boday, err := utils.Post(conf.DbConfig.PLCReadBackend, postData) - if err != nil { - log.Error("座椅数据回写, 读取座椅数据调用失败, error:" + err.Error()) - return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据调用失败, error:" + err.Error()) - } - log.Debug("座椅数据回写, 读取座椅数据请求响应:" + string(boday)) - - var response baseModel.Response - err = json.Unmarshal(boday, &response) - if err != nil { - log.Error("座椅数据回写, 解析读取座椅数据后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) - return "", grmi.NewBusinessError("座椅数据回写, 解析读取座椅数据后台数据失败, error:" + err.Error()) - } - if response.Code != iris.StatusOK { - log.Error("座椅数据回写,读取座椅数据调用后台失败, 后台返回错误, 后台数据:" + string(boday)) - return "", grmi.NewBusinessError("座椅数据回写,读取座椅数据调用后台失败, 后台返回错误, 错误:" + response.Msg) - } - plcData, ok := response.Data.(map[string]interface{}) - if !ok { - log.Error("座椅数据回写, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - } - successInterface, exist := plcData["IsSuccess"] - if !exist { - log.Error("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - } - success, ok := successInterface.(bool) - if !ok { - log.Error("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - } - if !success { - log.Error("座椅数据回写, 读取座椅数据后台plc执行失败, 后台数据:" + string(boday)) - return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据后台plc执行失败, 后台数据:" + string(boday)) - } - content, exist := plcData["Content"] - if !exist { - log.Error("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - } - seatContent, ok := content.(string) - if !ok { - log.Error("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - } - length := len(seatContent) - for index, item := range seatContent { - if item == 0 { - length = index - break - } - } - return seatContent[:length], nil -} - -// BackFlushClearSeatData 座椅回写 清空座椅数据 -func (impl *PackOrderServiceImplement) BackFlushClearSeatData() error { - log, _ := logger.NewLogger("plc", "JIT") - data := baseModel.PLC{ - PLCType: conf.DbConfig.BackFlushPlcType, - PLCAddress: conf.DbConfig.BackFlushPlcAddress, - ValueAddress: conf.DbConfig.BackFlushPlcReadData, - ValueType: "Bytes", - RetryCount: conf.DbConfig.OfflinePLCRetry, - ByteCount: conf.DbConfig.BackFlushPlcReadLen, - ValueText: strings.Repeat("0", 96), - } - postData, err := json.Marshal(data) - if err != nil { - log.Error("座椅数据回写, 序列化清空座椅数据请求数据失败, error:" + err.Error()) - return grmi.NewBusinessError("座椅数据回写, 序列化清空椅数据请求数据失败, error:" + err.Error()) - } - log.Debug("座椅数据回写, 清空座椅数据请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCWriteBackend) - boday, err := utils.Post(conf.DbConfig.PLCWriteBackend, postData) - if err != nil { - log.Error("座椅数据回写, 清空座椅数据调用失败, error:" + err.Error()) - return grmi.NewBusinessError("座椅数据回写, 清空座椅数据调用失败, error:" + err.Error()) - } - log.Debug("座椅数据回写, 清空座椅数据请求响应:" + string(boday)) - - var response baseModel.Response - err = json.Unmarshal(boday, &response) - if err != nil { - log.Error("座椅数据回写, 清空读取座椅数据后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅数据回写, 清空读取座椅数据后台数据失败, error:" + err.Error()) - } - if response.Code != iris.StatusOK { - log.Error("座椅数据回写,清空座椅数据调用后台失败, 后台返回错误, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅数据回写,清空座椅数据调用后台失败, 后台返回错误, 错误:" + response.Msg) - } - plcData, ok := response.Data.(map[string]interface{}) - if !ok { - log.Error("座椅数据回写, 清空座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅数据回写, 清空座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - } - successInterface, exist := plcData["IsSuccess"] - if !exist { - log.Error("座椅数据回写, 清空座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅数据回写, 清空座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - } - success, ok := successInterface.(bool) - if !ok { - log.Error("座椅数据回写, 清空座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅数据回写, 清空座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) - } - if !success { - log.Error("座椅数据回写, 清空座椅数据后台plc执行失败, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅数据回写, 清空座椅数据后台plc执行失败, 后台数据:" + string(boday)) - } - return nil -} - -// BackFlushWriteSign 座椅回写 写入完成标标识 -func (impl *PackOrderServiceImplement) BackFlushWriteSign() error { - data := baseModel.PLC{ - PLCType: conf.DbConfig.BackFlushPlcType, - PLCAddress: conf.DbConfig.BackFlushPlcAddress, - ValueAddress: conf.DbConfig.BackFlushPlcReadSign, - ValueType: "Boolean", - RetryCount: conf.DbConfig.OfflinePLCRetry, - ValueText: false, - } - log, _ := logger.NewLogger("座椅数据回写", "JIT") - postData, err := json.Marshal(data) - if err != nil { - log.Error("座椅数据回写, 序列化读取完成请求数据失败, error:" + err.Error()) - return grmi.NewBusinessError("座椅数据回写, 序列化读取完成请求数据失败, error:" + err.Error()) - } - log.Debug("座椅数据回写, 写入读取完成请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCWriteBackend) - boday, err := utils.Post(conf.DbConfig.PLCWriteBackend, postData) - if err != nil { - log.Error("座椅数据回写,写入读取完成调用失败, error:" + err.Error()) - return grmi.NewBusinessError("座椅数据回写, 写入读取完成调用失败, error:" + err.Error()) - } - log.Debug("座椅数据回写, 写入读取完成请求响应:" + string(boday)) - var response baseModel.Response - err = json.Unmarshal(boday, &response) - if err != nil { - log.Error("座椅数据回写, 解析写入读取完成后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅数据回写, 解析写入读取完成后台数据失败, error:" + err.Error()) - } - if response.Code != iris.StatusOK { - log.Error("座椅数据回写,写入读取完成调用后台失败, 后台返回错误, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅数据回写,写入读取完成调用后台失败, 后台返回错误, 错误:" + response.Msg) - } - plcData, ok := response.Data.(map[string]interface{}) - if !ok { - log.Error("座椅数据回写, 写入读取完成后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅数据回写, 写入读取完成后台返回data格式不正确, 后台数据:" + string(boday)) - } - successInterface, exist := plcData["IsSuccess"] - if !exist { - log.Error("座椅数据回写, 写入读取完成后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅数据回写, 写入读取完成后台返回data格式不正确, 后台数据:" + string(boday)) - } - success, ok := successInterface.(bool) - if !ok { - log.Error("座椅数据回写, 写入读取完成后台返回data格式不正确, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅数据回写, 写入读取完成后台返回data格式不正确, 后台数据:" + string(boday)) - } - if !success { - log.Error("座椅数据回写, 写入读取完成后台plc执行失败, 后台数据:" + string(boday)) - return grmi.NewBusinessError("座椅数据回写, 写入读取完成后台plc执行失败, 后台数据:" + string(boday)) - } - return nil -} - -func (impl *PackOrderServiceImplement) ReadPLCBackFlushDataTask() { - - log, _ := logger.NewLogger("座椅数据回写", "JIT") - userId := "Scheduler" - engine := db.Eloquent.Master() - session := engine.NewSession() - defer session.Close() - plantDao := baseDal.NewPlantDAO(session, userId) - plantLi, err := plantDao.Select(nil, nil) - if err != nil { - log.Error("查询工厂数据失败, 错误:" + err.Error()) - return - } - for _, plant := range plantLi { - go impl.ReadPLCBackFlushData(&global.User{ - UserId: userId, - PlantNr: plant.PlantNr, - }) - } -} - - -// ReadPLCBackFlushData 座椅回写 业务逻辑 -func (impl *PackOrderServiceImplement) ReadPLCBackFlushData(user *global.User) { - log, _ := logger.NewLogger("座椅数据回写", "JIT") - uid, err := uuid.NewV1() - if err != nil { - log.Error("生成任务ID失败, 错误:" + err.Error()) - return - } - taskId := uid.String() - engine := db.Eloquent.Master() - session := engine.NewSession() - defer session.Close() - plcErrorDao := dal.NewPackOrderPlcErrorDAO(session, user.PlantNr, user.UserId) - // 1. 先查看有没有数据方面的错误没有处理 如果有错误则不进行任务 - //dataErrorInfoLi, err := plcErrorDao.Select([]grmi.Predicate{ - // meta.PackOrderPlcError_Location.NewPredicate(grmi.Equal, model.PACK_LOCATION_BACKFLUSH), - // meta.PackOrderPlcError_Status.NewPredicate(grmi.Equal, model.PACK_STATUS_PLANNED), - // meta.PackOrderPlcError_ErrorType.NewPredicate(grmi.Equal, plnModel.ERROR_TYPE_DATA), - //}, nil) - //if err != nil { - // log.Error("查询PLC错误项失败, 错误:"+err.Error(), "任务ID:"+taskId) - // return - //} - //if len(dataErrorInfoLi) != 0 { - // log.Info("存在尚未处理的数据项错误", "任务ID:"+taskId) - // return - //} - // 2. 先从PLC错误数据表中查看是否有没处理的错误 - errorInfoLi, err := plcErrorDao.Select([]grmi.Predicate{ - meta.PackOrderPlcError_Location.NewPredicate(grmi.Equal, model.PACK_LOCATION_BACKFLUSH), - meta.PackOrderPlcError_Status.NewPredicate(grmi.Equal, model.PACK_STATUS_PLANNED), - meta.PackOrderPlcError_ErrorType.NewPredicate(grmi.Equal, plnModel.ERROR_TYTPE_PLC), - }, nil) - if err != nil { - log.Error("查询PLC错误项失败, 错误:"+err.Error(), "任务ID:"+taskId) - return - } - // 如果有plc交互的错误 直接写入标识位 完成此次任务 - if len(errorInfoLi) != 0 { - errorInfo := errorInfoLi[0] - if err = session.Begin(); err != nil { - log.Error("开启事务失败", "任务ID:"+taskId) - return - } - errorInfo.Status = model.PACK_STATUS_CLOSED - err = plcErrorDao.UpdateOne(&errorInfo) - if err != nil { - _ = session.Rollback() - log.Error("更新PLC错误项状态失败, 错误:"+err.Error(), "任务ID:"+taskId) - return - } - switch errorInfo.Step { - case 3: - err = impl.BackFlushClearSeatData() - if err != nil { - _ = session.Rollback() - log.Error("根据PLC错误项清空座椅数据失败", "任务ID:"+taskId) - return - } - fallthrough - case 4: - err = impl.BackFlushWriteSign() - if err != nil { - _ = session.Rollback() - log.Error("根据PLC错误项写入完成标识失败", "任务ID:"+taskId) - return - } - } - _ = session.Commit() - log.Info("完成PLC错误项修复, 错误ID:"+errorInfo.PackOrderId, "任务ID:"+taskId) - return - } - // 如果没有PLC错误 从PLC读取数据回写PackOrderStatus的ShipStatus - packOrderDao := dal.NewPackOrderDAO(session, user.PlantNr, user.UserId) - packOrderStatusDao := dal.NewPackOrderStatusDAO(session, user.PlantNr, user.UserId) - sign, err := impl.BackFlushReadSign() - if err != nil { - log.Error("读取标识位失败, 错误:"+err.Error(), "任务ID:"+taskId) - return - } - if !sign { - log.Info("暂无需要读取的数据", "任务ID:"+taskId) - return - } - seatData, err := impl.BackFlushReadSeatData() - if err != nil { - log.Info("读取座椅数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - return - } - seatBytes, err := hex.DecodeString(seatData) - seatStr := string(seatBytes) - if err != nil { - log.Error("解码座椅数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - plcErrorInfo := model.PackOrderPlcError{ - PackOrderId: taskId, - Location: model.PACK_LOCATION_BACKFLUSH, - Status: model.PACK_STATUS_PLANNED, - Step: -1, - ErrorInfo: "解码座椅数据失败, 错误:" + err.Error(), - SeatData: seatData, - ErrorType: plnModel.ERROR_TYPE_DATA, - } - err = plcErrorDao.InsertOne(&plcErrorInfo) - if err != nil { - log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - return - } - err = impl.BackFlushClearSeatData() - if err != nil { - log.Error("清空座椅数据失败, 错误:" + err.Error()) - plcErrorInfo = model.PackOrderPlcError{ - PackOrderId: taskId, - Location: model.PACK_LOCATION_BACKFLUSH, - Status: model.PACK_STATUS_PLANNED, - Step: 3, - ErrorInfo: "清空座椅数据失败, 错误:" + err.Error(), - SeatData: seatStr, - ErrorType: plnModel.ERROR_TYTPE_PLC, - } - err = plcErrorDao.InsertOne(&plcErrorInfo) - if err != nil { - log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - return - } - return - } - err = impl.BackFlushWriteSign() - if err != nil { - log.Error("写入标识位失败, 错误:" + err.Error()) - plcErrorInfo = model.PackOrderPlcError{ - PackOrderId: taskId, - Location: model.PACK_LOCATION_BACKFLUSH, - Status: model.PACK_STATUS_PLANNED, - Step: 4, - ErrorInfo: "写入标识位失败, 错误:" + err.Error(), - SeatData: seatStr, - ErrorType: plnModel.ERROR_TYTPE_PLC, - } - err = plcErrorDao.InsertOne(&plcErrorInfo) - if err != nil { - log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - return - } - return - } - } - packOrderLi, err := packOrderDao.Select([]grmi.Predicate{meta.PackOrder_RFID.NewPredicate(grmi.Equal, seatStr)}, nil) - if err != nil { - log.Error("查询包装单数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - return - } - if len(packOrderLi) == 0 { - log.Error("不存RFID对应的包装单, RFID:"+seatStr, "任务ID:"+taskId) - plcErrorInfo := model.PackOrderPlcError{ - PackOrderId: taskId, - Location: model.PACK_LOCATION_BACKFLUSH, - Status: model.PACK_STATUS_PLANNED, - Step: -2, - ErrorInfo: "不存RFID对应的包装单, RFID:" + seatStr, - SeatData: seatStr, - ErrorType: plnModel.ERROR_TYPE_DATA, - } - err = plcErrorDao.InsertOne(&plcErrorInfo) - if err != nil { - log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - return - } - err = impl.BackFlushClearSeatData() - if err != nil { - log.Error("清空座椅数据失败, 错误:" + err.Error()) - plcErrorInfo = model.PackOrderPlcError{ - PackOrderId: taskId, - Location: model.PACK_LOCATION_BACKFLUSH, - Status: model.PACK_STATUS_PLANNED, - Step: 3, - ErrorInfo: "清空座椅数据失败, 错误:" + err.Error(), - SeatData: seatStr, - ErrorType: plnModel.ERROR_TYTPE_PLC, + // 座椅下线和线外打包plc错误处理 + plcType := conf.DbConfig.OfflinePLCType + retryCount := conf.DbConfig.OfflinePLCRetry + var plcAddress string + var DBAddress string + if packOrderError.Location == model.PACK_LOCATION_ON_LINE { + plcAddress = conf.DbConfig.OfflinePLCAddress } - err = plcErrorDao.InsertOne(&plcErrorInfo) - if err != nil { - log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - return + var seatData string + if packOrderError.Step != 0 { + seatData = packOrderError.SeatData } - return - } - err = impl.BackFlushWriteSign() - if err != nil { - log.Error("写入标识位失败, 错误:" + err.Error()) - plcErrorInfo = model.PackOrderPlcError{ - PackOrderId: taskId, - Location: model.PACK_LOCATION_BACKFLUSH, - Status: model.PACK_STATUS_PLANNED, - Step: 4, - ErrorInfo: "写入标识位失败, 错误:" + err.Error(), - SeatData: seatStr, - ErrorType: plnModel.ERROR_TYTPE_PLC, + switch packOrderError.Step { + case 0, 1: // 组装座椅数据 + packOrder, err := packOrderDao.SelectOne(packOrderId) + if err != nil { + return grmi.NewBusinessError("查询包装单数据失败, error:" + err.Error()) + } + if packOrder == nil { + return grmi.NewBusinessError("包装单不存在") + } + seatData, err = impl.MakeUpSeatData(session, user, packOrderId, packOrderError.Location, packOrder.ProjectId) + if err != nil { + packOrderError.ErrorInfo = err.Error() + innerErr := packOrderPlcErrDao.UpdateOne(packOrderError) + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) + } + return err + } + if seatData == "" { + return grmi.NewBusinessError("待写入座椅数据为空") + } + packOrder.RFID = seatData + err = packOrderDao.UpdateOne(packOrder) + if err != nil { + errorInfo := "保存座椅数据到包装单上失败, 错误:" + err.Error() + packOrderError.ErrorInfo = err.Error() + innerErr := packOrderPlcErrDao.UpdateOne(packOrderError) + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) + } + return grmi.NewBusinessError(errorInfo) + } + fallthrough + case 2, 3: // 读取标识位 写入座椅数据 + if packOrderError.Location == model.PACK_LOCATION_ON_LINE { + DBAddress = conf.DbConfig.OfflineDBSign + } + valueType := "Boolean" + data := baseModel.PLC{ + PLCType: plcType, + PLCAddress: plcAddress, + ValueAddress: DBAddress, + ValueType: valueType, + RetryCount: retryCount, + } + err = impl.SeatOfflineReadSign(data) + if err != nil { + packOrderError.Step = 1 + packOrderError.ErrorInfo = err.Error() + packOrderError.SeatData = seatData + innerErr := packOrderPlcErrDao.UpdateOne(packOrderError) + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) + } + return err + } + if packOrderError.Location == model.PACK_LOCATION_ON_LINE { + DBAddress = conf.DbConfig.OfflineDBSave + } + valueType = "Bytes" + data = baseModel.PLC{ + PLCType: plcType, + PLCAddress: plcAddress, + ValueAddress: DBAddress, + ValueType: valueType, + RetryCount: retryCount, + ValueText: packOrderError.SeatData, + ByteCount: conf.DbConfig.OfflineDBSaveLen, + } + err = impl.SeatOfflineWriteSeatData(data) + if err != nil { + packOrderError.Step = 2 + packOrderError.ErrorInfo = err.Error() + packOrderError.SeatData = seatData + innerErr := packOrderPlcErrDao.UpdateOne(packOrderError) + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) + } + return err + } + fallthrough + case 4: // 写入标识位数据 + if packOrderError.Location == model.PACK_LOCATION_ON_LINE { + DBAddress = conf.DbConfig.OfflineDBSign + } + valueType := "Boolean" + data := baseModel.PLC{ + PLCType: plcType, + PLCAddress: plcAddress, + ValueAddress: DBAddress, + ValueType: valueType, + RetryCount: retryCount, + ValueText: true, + } + err = impl.SeatOfflineWriteSign(data) + if err != nil { + packOrderError.Step = 3 + packOrderError.ErrorInfo = err.Error() + packOrderError.SeatData = seatData + innerErr := packOrderPlcErrDao.UpdateOne(packOrderError) + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) + } + return err + } } - err = plcErrorDao.InsertOne(&plcErrorInfo) + packOrderError.Status = model.PACK_STATUS_CLOSED + packOrderError.SeatData = seatData + err = packOrderPlcErrDao.UpdateOne(packOrderError) if err != nil { - log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - return + return grmi.NewBusinessError("问题已修复, 但是更新错误项数据状态失败请删除, error:" + err.Error()) } - return - } - } - if len(packOrderLi) > 1 { - log.Error("该RFID绑定多个包装单, RFID:"+seatStr, "任务ID:"+taskId) - plcErrorInfo := model.PackOrderPlcError{ - PackOrderId: taskId, - Location: model.PACK_LOCATION_BACKFLUSH, - Status: model.PACK_STATUS_PLANNED, - Step: -2, - ErrorInfo: "该RFID绑定多个包装单, RFID:" + seatStr, - SeatData: seatStr, - ErrorType: plnModel.ERROR_TYPE_DATA, + return nil } - err = plcErrorDao.InsertOne(&plcErrorInfo) + case "BZ-OK", "BZ-NG": + workplaceConfig, err := workplaceConfigDao.SelectOne(packOrderError.IntCtrl2) if err != nil { - log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - return + return grmi.NewBusinessError("查询工位配置失败, 错误:" + err.Error()) } - err = impl.BackFlushClearSeatData() - if err != nil { - log.Error("清空座椅数据失败, 错误:" + err.Error()) - plcErrorInfo = model.PackOrderPlcError{ - PackOrderId: taskId, - Location: model.PACK_LOCATION_BACKFLUSH, - Status: model.PACK_STATUS_PLANNED, - Step: 3, - ErrorInfo: "清空座椅数据失败, 错误:" + err.Error(), - SeatData: seatStr, - ErrorType: plnModel.ERROR_TYTPE_PLC, - } - err = plcErrorDao.InsertOne(&plcErrorInfo) - if err != nil { - log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - return - } - return + if workplaceConfig == nil { + return grmi.NewBusinessError("查询工位配置不存在") } - err = impl.BackFlushWriteSign() + quality := strings.Split(packOrderError.Source, "-")[1] + err = impl.BZPackPLC(user, workplaceConfig, packOrderId, quality, false, packOrderError.Step) if err != nil { - log.Error("写入标识位失败, 错误:" + err.Error()) - plcErrorInfo = model.PackOrderPlcError{ - PackOrderId: taskId, - Location: model.PACK_LOCATION_BACKFLUSH, - Status: model.PACK_STATUS_PLANNED, - Step: 4, - ErrorInfo: "写入标识位失败, 错误:" + err.Error(), - SeatData: seatStr, - ErrorType: plnModel.ERROR_TYTPE_PLC, - } - err = plcErrorDao.InsertOne(&plcErrorInfo) - if err != nil { - log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - return - } - return - } - } - packOrder := packOrderLi[0] - packOrderStatus, err := packOrderStatusDao.SelectOne(packOrder.PackOrderId) - if err != nil { - log.Error("查询包装单状态数据失败, 错误:" + err.Error()) - return - } - packOrderStatus.ShipStatus = model.SHIP_STATUS_CLOSED - err = packOrderStatusDao.UpdateOne(packOrderStatus) - if err != nil { - log.Error("更新包装单状态失败, 错误:" + err.Error()) - return - } - packOrder.CtrlTime1 = grmi.DateTime(time.Now()) - err = packOrderDao.UpdateOne(&packOrder) - if err != nil { - log.Error("更新包装单回写时间失败, 错误:" + err.Error()) - return - } - err = impl.BackFlushClearSeatData() - if err != nil { - log.Error("清空座椅数据失败, 错误:" + err.Error()) - plcErrorInfo := model.PackOrderPlcError{ - PackOrderId: taskId, - Location: model.PACK_LOCATION_BACKFLUSH, - Status: model.PACK_STATUS_PLANNED, - Step: 3, - ErrorInfo: "清空座椅数据失败, 错误:" + err.Error(), - SeatData: seatStr, - ErrorType: plnModel.ERROR_TYTPE_PLC, - } - err = plcErrorDao.InsertOne(&plcErrorInfo) - if err != nil { - log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - return + return err } - return - } - err = impl.BackFlushWriteSign() - if err != nil { - log.Error("写入标识位失败, 错误:" + err.Error()) - plcErrorInfo := model.PackOrderPlcError{ - PackOrderId: taskId, - Location: model.PACK_LOCATION_BACKFLUSH, - Status: model.PACK_STATUS_PLANNED, - Step: 4, - ErrorInfo: "写入标识位失败, 错误:" + err.Error(), - SeatData: seatStr, - ErrorType: plnModel.ERROR_TYTPE_PLC, - } - err = plcErrorDao.InsertOne(&plcErrorInfo) + packOrderError.Status = model.PACK_STATUS_CLOSED + err = packOrderPlcErrDao.UpdateOne(packOrderError) if err != nil { - log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) - return + return grmi.NewBusinessError("问题已修复, 但是更新错误项数据状态失败请删除, error:" + err.Error()) } - return + return nil + default: + return grmi.NewBusinessError("不支持该来源的错误修复") } -} -// PLCPermitThrough 讴歌下线打包 PLC放行 -func (impl *PackOrderServiceImplement) PLCPermitThrough() error { - data := baseModel.PLC{ - PLCType: conf.DbConfig.PermitThroughPlcType, - PLCAddress: conf.DbConfig.PermitThroughPlcAddress, - ValueAddress: conf.DbConfig.PermitThroughPlcDB, - ValueType: "Boolean", - RetryCount: 2, - ValueText: true, - } - err := impl.SeatOfflineWriteSign(data) - return err } diff --git a/services/jit/implments/PackOrderToPLC.service.impl.go b/services/jit/implments/PackOrderToPLC.service.impl.go new file mode 100644 index 0000000..4e9cc4d --- /dev/null +++ b/services/jit/implments/PackOrderToPLC.service.impl.go @@ -0,0 +1,1245 @@ +package implments + +import ( + "LAPP_ACURA_MOM_BACKEND/conf" + baseDal "LAPP_ACURA_MOM_BACKEND/dao/base" + dal "LAPP_ACURA_MOM_BACKEND/dao/jit" + meDal "LAPP_ACURA_MOM_BACKEND/dao/me" + omDal "LAPP_ACURA_MOM_BACKEND/dao/om" + "LAPP_ACURA_MOM_BACKEND/db" + "LAPP_ACURA_MOM_BACKEND/global" + "LAPP_ACURA_MOM_BACKEND/grmi" + "LAPP_ACURA_MOM_BACKEND/infra/logger" + meta "LAPP_ACURA_MOM_BACKEND/meta/jit" + omMeta "LAPP_ACURA_MOM_BACKEND/meta/om" + baseModel "LAPP_ACURA_MOM_BACKEND/models/base" + model "LAPP_ACURA_MOM_BACKEND/models/jit" + meModel "LAPP_ACURA_MOM_BACKEND/models/me" + plnModel "LAPP_ACURA_MOM_BACKEND/models/pln" + "LAPP_ACURA_MOM_BACKEND/plc" + "LAPP_ACURA_MOM_BACKEND/utils" + "encoding/hex" + "github.com/go-xorm/xorm" + uuid "github.com/iris-contrib/go.uuid" + json "github.com/json-iterator/go" + "github.com/kataras/iris/v12" + "strings" + "time" +) + +// 南沙 + +// NSPackPLC 南沙项目 座椅入库 plc交互逻辑 +func (impl *PackOrderServiceImplement) NSPackPLC(user *global.User, packOrderId string, projectId string, location string, packOrder *model.PackOrder) error { + // 写入plc数据格式:第一位质量,第二位安道拓还是鹰泰生产,第三到二十七位为5种座椅四位机种代码+一位颜色代码合起来 + // 第一位:1为合格,0为不合格 + // 第二位:1为安道拓,0为樱泰 + engine := db.Eloquent.Master() + session := engine.NewSession() + defer session.Close() + packOrderDao := dal.NewPackOrderDAO(session, user.PlantNr, user.UserId) + seatData, err := impl.MakeUpSeatData(session, user, packOrderId, location, projectId) + if err != nil { + innerErr := impl.SaveOfflineError(session, user, packOrderId, 0, err.Error(), location, "") + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) + } + return err + } + if seatData == "" { + return nil + } + packOrder.RFID = seatData + err = packOrderDao.UpdateOne(packOrder) + if err != nil { + errorInfo := "保存座椅数据至包装单失败, 错误:" + err.Error() + innerErr := impl.SaveOfflineError(session, user, packOrderId, 1, err.Error(), location, seatData) + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + errorInfo) + } + return grmi.NewBusinessError(errorInfo) + } + err, step := impl.WriteSeatDataToPLC(seatData, location) + if err != nil { + innerErr := impl.SaveOfflineError(session, user, packOrderId, step, err.Error(), location, seatData) + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + err.Error()) + } + return err + } + return nil +} + +// SaveOfflineError 将座椅下线写入plc阶段的错误保存到数据表 +func (impl *PackOrderServiceImplement) SaveOfflineError(session *xorm.Session, user *global.User, packOrderId string, step int, errInfo string, location string, seatData string) error { + packOrderPlcErrDao := dal.NewPackOrderPlcErrorDAO(session, user.PlantNr, user.UserId) + errData := model.PackOrderPlcError{ + PackOrderId: packOrderId, + Status: model.PACK_STATUS_PLANNED, + Step: step, + ErrorType: model.ERROR_TYTPE_PLC, + ErrorInfo: errInfo, + Location: location, + SeatData: seatData, + Source: "NS", + } + return packOrderPlcErrDao.InsertOne(&errData) +} + +// WriteSeatDataToPLC 将座椅下线写入plc +func (impl *PackOrderServiceImplement) WriteSeatDataToPLC(seatData string, location string) (error, int) { + // 1. 读取地址位 检验是否可以写入 + var plcAddress, DBAddress, plcType string + var dbLen int + if location == model.PACK_LOCATION_ON_LINE { + plcAddress = conf.DbConfig.OfflinePLCAddress + DBAddress = conf.DbConfig.OfflineDBSign + plcType = conf.DbConfig.OfflinePLCType + } else { + plcAddress = conf.DbConfig.RepairPlcAddress + DBAddress = conf.DbConfig.RepairPlcReadSign + plcType = conf.DbConfig.RepairPlcType + } + valueType := "Boolean" + retryCount := conf.DbConfig.OfflinePLCRetry + data := baseModel.PLC{ + PLCType: plcType, + PLCAddress: plcAddress, + ValueAddress: DBAddress, + ValueType: valueType, + RetryCount: retryCount, + } + err := impl.SeatOfflineReadSign(data) + if err != nil { + return err, 2 + } + // 2. 写入座椅数据 + if location == model.PACK_LOCATION_ON_LINE { + DBAddress = conf.DbConfig.OfflineDBSave + dbLen = conf.DbConfig.OfflineDBSaveLen + } else { + DBAddress = conf.DbConfig.RepairPlcWrite + dbLen = conf.DbConfig.RepairPlcWriteLen + } + valueType = "Bytes" + data = baseModel.PLC{ + PLCType: plcType, + PLCAddress: plcAddress, + ValueAddress: DBAddress, + ValueType: valueType, + RetryCount: retryCount, + ValueText: seatData, + ByteCount: dbLen, + } + err = impl.SeatOfflineWriteSeatData(data) + if err != nil { + return err, 3 + } + + // 3. 写入保存完成 + if location == model.PACK_LOCATION_ON_LINE { + DBAddress = conf.DbConfig.OfflineDBSign + } else { + DBAddress = conf.DbConfig.RepairPlcReadSign + } + valueType = "Boolean" + data = baseModel.PLC{ + PLCType: plcType, + PLCAddress: plcAddress, + ValueAddress: DBAddress, + ValueType: valueType, + RetryCount: retryCount, + ValueText: true, + } + err = impl.SeatOfflineWriteSign(data) + if err != nil { + return err, 4 + } + return nil, 0 +} + +// SeatOfflineReadSign 座椅入库 读取标识位 +func (impl *PackOrderServiceImplement) SeatOfflineReadSign(data baseModel.PLC) error { + log, _ := logger.NewLogger("plc", "JIT") + var retryNum int + for { + postData, err := json.Marshal(data) + if err != nil { + log.Error("座椅下线plc写入, 序列化读取标识位请求数据失败, error:" + err.Error()) + return grmi.NewBusinessError("座椅下线plc写入, 序列化读取标识位请求数据失败, error:" + err.Error()) + } + log.Debug("座椅下线plc写入, 读取标识位请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCReadBackend) + boday, err := utils.Post(conf.DbConfig.PLCReadBackend, postData) + if err != nil { + log.Error("座椅下线plc写入, 读取标识位调用失败, error:" + err.Error()) + return grmi.NewBusinessError("座椅下线plc写入, 读取标识位调用失败, error:" + err.Error()) + } + log.Debug("座椅下线plc写入, 读取标识位请求响应:" + string(boday)) + + var response baseModel.Response + err = json.Unmarshal(boday, &response) + if err != nil { + log.Error("座椅下线plc写入, 解析读取标识位后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 解析读取标识位后台数据失败, error:" + err.Error()) + } + if response.Code != iris.StatusOK { + log.Error("座椅下线plc写入,读取标识位调用后台失败, 后台返回错误, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入,读取标识位调用后台失败, 后台返回错误, 错误:" + response.Msg) + } + plcData, ok := response.Data.(map[string]interface{}) + if !ok { + log.Error("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + successInterface, exist := plcData["IsSuccess"] + if !exist { + log.Error("座椅下线plc写入, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) + } + success, ok := successInterface.(bool) + if !ok { + log.Error("座椅下线plc写入, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) + } + if !success { + log.Error("座椅下线plc写入, 读取写入标识后台plc执行失败, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 读取写入标识后台plc执行失败, 后台数据:" + string(boday)) + } + content, exist := plcData["Content"] + if !exist { + log.Error("座椅下线plc写入, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) + } + sign, ok := content.(bool) + if !ok { + log.Error("座椅下线plc写入, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) + } + if sign { + if retryNum >= 3 { + log.Error("座椅下线plc写入, 读取标识位不允许写入且达到最大次数, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 读取标识位不允许写入且达到最大次数, 后台数据:" + string(boday)) + } + log.Error("座椅下线plc写入, 读取标识位不允许写入, 后台数据:" + string(boday)) + retryNum++ + time.Sleep(time.Millisecond * 500) + continue + } else { + break + } + } + return nil +} + +// SeatOfflineWriteSeatData 座椅入库写入座椅数据 +func (impl *PackOrderServiceImplement) SeatOfflineWriteSeatData(data baseModel.PLC) error { + log, _ := logger.NewLogger("plc", "JIT") + data.ValueText = hex.EncodeToString([]byte(data.ValueText.(string))) + postData, err := json.Marshal(data) + if err != nil { + log.Error("座椅下线plc写入, 序列化座椅数据请求数据失败, error:" + err.Error()) + return grmi.NewBusinessError("座椅下线plc写入, 序列化座椅数据请求数据失败, error:" + err.Error()) + } + log.Debug("座椅下线plc写入, 写入座椅数据请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCWriteBackend) + boday, err := utils.Post(conf.DbConfig.PLCWriteBackend, postData) + if err != nil { + log.Error("座椅下线plc写入,写入座椅数据调用失败, error:" + err.Error()) + return grmi.NewBusinessError("座椅下线plc写入, 写入座椅数据调用失败, error:" + err.Error()) + } + log.Debug("座椅下线plc写入, 写入座椅数据请求响应:" + string(boday)) + + var response baseModel.Response + err = json.Unmarshal(boday, &response) + if err != nil { + log.Error("座椅下线plc写入, 解析写入座椅数据后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 解析写入座椅数据后台数据失败, error:" + err.Error()) + } + if response.Code != iris.StatusOK { + log.Error("座椅下线plc写入,写入座椅数据调用后台失败, 后台返回错误, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入,写入座椅数据调用后台失败, 后台返回错误, 错误:" + response.Msg) + } + plcData, ok := response.Data.(map[string]interface{}) + if !ok { + log.Error("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + successInterface, exist := plcData["IsSuccess"] + if !exist { + log.Error("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + success, ok := successInterface.(bool) + if !ok { + log.Error("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + if !success { + log.Error("座椅下线plc写入, 写入座椅数据后台plc执行失败, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 写入座椅数据后台plc执行失败, 后台数据:" + string(boday)) + } + return nil +} + +// SeatOfflineWriteSign 座椅入库写入标识位 +func (impl *PackOrderServiceImplement) SeatOfflineWriteSign(data baseModel.PLC) error { + log, _ := logger.NewLogger("plc", "JIT") + postData, err := json.Marshal(data) + if err != nil { + log.Error("座椅下线plc写入, 序列化保存完成请求数据失败, error:" + err.Error()) + return grmi.NewBusinessError("座椅下线plc写入, 序列化保存完成请求数据失败, error:" + err.Error()) + } + log.Debug("座椅下线plc写入, 写入保存完成请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCWriteBackend) + boday, err := utils.Post(conf.DbConfig.PLCWriteBackend, postData) + if err != nil { + log.Error("座椅下线plc写入,写入保存完成调用失败, error:" + err.Error()) + return grmi.NewBusinessError("座椅下线plc写入, 写入保存完成调用失败, error:" + err.Error()) + } + log.Debug("座椅下线plc写入, 写入保存完成请求响应:" + string(boday)) + var response baseModel.Response + err = json.Unmarshal(boday, &response) + if err != nil { + log.Error("座椅下线plc写入, 解析写入保存完成后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 解析写入保存完成后台数据失败, error:" + err.Error()) + } + if response.Code != iris.StatusOK { + log.Error("座椅下线plc写入,写入保存完成调用后台失败, 后台返回错误, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入,写入保存完成调用后台失败, 后台返回错误, 错误:" + response.Msg) + } + plcData, ok := response.Data.(map[string]interface{}) + if !ok { + log.Error("座椅下线plc写入, 写入保存完成后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 写入保存完成后台返回data格式不正确, 后台数据:" + string(boday)) + } + successInterface, exist := plcData["IsSuccess"] + if !exist { + log.Error("座椅下线plc写入, 写入保存完成后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 写入保存完成后台返回data格式不正确, 后台数据:" + string(boday)) + } + success, ok := successInterface.(bool) + if !ok { + log.Error("座椅下线plc写入, 写入保存完成后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 写入保存完成后台返回data格式不正确, 后台数据:" + string(boday)) + } + if !success { + log.Error("座椅下线plc写入, 写入保存完成后台plc执行失败, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅下线plc写入, 写入保存完成后台plc执行失败, 后台数据:" + string(boday)) + } + return nil +} + +// MakeUpSeatData 拼接座椅数据 +func (impl *PackOrderServiceImplement) MakeUpSeatData(session *xorm.Session, user *global.User, packOrderId string, location string, projectId string) (string, error) { + packLstDao := dal.NewPackOrderItemLstDAO(session, user.PlantNr, user.UserId) + productDao := meDal.NewProductDAO(session, user.PlantNr, user.UserId) + projectDao := meDal.NewProjectDAO(session, user.PlantNr, user.UserId) + serialOrderDao := omDal.NewSerialOrderDAO(session, user.PlantNr, user.UserId) + packOrderItemLi, err := packLstDao.Select([]grmi.Predicate{meta.PackOrderItemLst_PackOrderId.NewPredicate(grmi.Equal, packOrderId), + meta.PackOrderItemLst_Status.NewPredicate(grmi.Equal, model.PACK_STATUS_CLOSED)}, []grmi.Field{meta.PackOrderItemLst_Pos}) + if len(packOrderItemLi) == 0 { + return "", nil + } + if err != nil { + errInfo := "查询包装单子项数据失败, error:" + err.Error() + return "", grmi.NewBusinessError(errInfo) + } + project, err := projectDao.SelectOne(projectId) + if err != nil { + errorInfo := "查询项目数据失败, 错误:" + err.Error() + return "", grmi.NewBusinessError(errorInfo) + } + serialOrderIdLi := make([]interface{}, 0, len(packOrderItemLi)) + var seatData string + seatMap := make(map[string]*meModel.Product) + for _, packOrderItem := range packOrderItemLi { + serialOrderIdLi = append(serialOrderIdLi, packOrderItem.SerialOrderId) + product, err := productDao.SelectOne(packOrderItem.PartId, projectId) + if err != nil { + errInfo := "查询包装单子项总成数据失败, error:" + err.Error() + innerErr := impl.SaveOfflineError(session, user, packOrderId, 0, errInfo, location, "") + if innerErr != nil { + return "", grmi.NewBusinessError(errInfo + ", 保存错误失败, error:" + err.Error()) + } + return "", err + } + if product == nil { + errInfo := "查询包装单子项总成不存在, 总成ID:" + packOrderItem.PartId + return "", grmi.NewBusinessError(errInfo) + } + seatMap[product.CtrlStr1] = product + } + product, exist := seatMap[meModel.PRODUCT_TYPE_MS] + if !exist { + errInfo := "包装单子项中不存在主驾" + return "", grmi.NewBusinessError(errInfo) + } + seatData += product.MachineCode + seatData += product.ColorValue + product, exist = seatMap[meModel.PRODUCT_TYPE_PS] + if !exist { + errInfo := "包装单子项中不存在副驾" + return "", grmi.NewBusinessError(errInfo) + } + seatData += product.MachineCode + seatData += product.ColorValue + product, exist = seatMap[meModel.PRODUCT_TYPE_40] + if !exist { + errInfo := "包装单子项中不存在40%" + return "", grmi.NewBusinessError(errInfo) + } + seatData += product.MachineCode + seatData += product.ColorValue + product, exist = seatMap[meModel.PRODUCT_TYPE_60] + if !exist { + errInfo := "包装单子项中不存在60%" + return "", grmi.NewBusinessError(errInfo) + } + seatData += product.MachineCode + seatData += product.ColorValue + product, exist = seatMap[meModel.PRODUCT_TYPE_CUSHION] + if !exist { + errInfo := "包装单子项中不存在坐垫" + return "", grmi.NewBusinessError(errInfo) + } + seatData += product.MachineCode + seatData += product.ColorValue + serialOrderLi, err := serialOrderDao.Select([]grmi.Predicate{omMeta.SerialOrder_SerialOrderId.NewPredicate(grmi.Include, serialOrderIdLi...)}, nil) + if err != nil { + errInfo := "查询包装单子项工单数据失败, error:" + err.Error() + return "", grmi.NewBusinessError(errInfo) + } + if len(serialOrderIdLi) != len(packOrderItemLi) { + errInfo := "查询包装单子项工单数据缺失" + return "", grmi.NewBusinessError(errInfo) + } + var status = `1` + for _, serialOrder := range serialOrderLi { + if serialOrder.SerialOrderStatus.Status != baseModel.WO_STATUS_FINISHED || serialOrder.QualityCheck != baseModel.QualityCheck_STATUS_OK { + status = `0` + } + } + seatData = status + "1" + seatData + if err = session.Begin(); err != nil { + return "", grmi.NewBusinessError("开启事务失败, 错误:" + err.Error()) + } + snrDao := baseDal.NewSnrDAO(session, user.PlantNr, user.UserId) + var num string + if location == model.PACK_LOCATION_ON_LINE { + num, err = snrDao.GetNextSnr(project.PackOrderRFIDSnr) + } else { + num, err = snrDao.GetNextSnr(project.PackOrderOutLineRFIDSnr) + } + if err != nil { + _ = session.Rollback() + return "", grmi.NewBusinessError("获取RFID流水号失败, 错误:" + err.Error()) + } + _ = session.Commit() + timeStr := time.Now().Format("060102") + seatData = seatData + timeStr + num + return seatData, nil +} + +// BackFlushReadSign 座椅回写 读取标识位 +func (impl *PackOrderServiceImplement) BackFlushReadSign() (bool, error) { + data := baseModel.PLC{ + PLCType: conf.DbConfig.BackFlushPlcType, + PLCAddress: conf.DbConfig.BackFlushPlcAddress, + ValueAddress: conf.DbConfig.BackFlushPlcReadSign, + ValueType: "Boolean", + RetryCount: conf.DbConfig.OfflinePLCRetry, + } + log, _ := logger.NewLogger("plc", "JIT") + var retryNum int + for { + postData, err := json.Marshal(data) + if err != nil { + log.Error("座椅数据回写, 序列化读取标识位请求数据失败, error:" + err.Error()) + return false, grmi.NewBusinessError("座椅数据回写, 序列化读取标识位请求数据失败, error:" + err.Error()) + } + log.Debug("座椅数据回写, 读取标识位请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCReadBackend) + boday, err := utils.Post(conf.DbConfig.PLCReadBackend, postData) + if err != nil { + log.Error("座椅数据回写, 读取标识位调用失败, error:" + err.Error()) + return false, grmi.NewBusinessError("座椅数据回写, 读取标识位调用失败, error:" + err.Error()) + } + log.Debug("座椅数据回写, 读取标识位请求响应:" + string(boday)) + + var response baseModel.Response + err = json.Unmarshal(boday, &response) + if err != nil { + log.Error("座椅数据回写, 解析读取标识位后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) + return false, grmi.NewBusinessError("座椅数据回写, 解析读取标识位后台数据失败, error:" + err.Error()) + } + if response.Code != iris.StatusOK { + log.Error("座椅数据回写,读取标识位调用后台失败, 后台返回错误, 后台数据:" + string(boday)) + return false, grmi.NewBusinessError("座椅数据回写,读取标识位调用后台失败, 后台返回错误, 错误:" + response.Msg) + } + plcData, ok := response.Data.(map[string]interface{}) + if !ok { + log.Error("座椅数据回写, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + return false, grmi.NewBusinessError("座椅数据回写, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) + } + successInterface, exist := plcData["IsSuccess"] + if !exist { + log.Error("座椅数据回写, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) + return false, grmi.NewBusinessError("座椅数据回写, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) + } + success, ok := successInterface.(bool) + if !ok { + log.Error("座椅数据回写, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) + return false, grmi.NewBusinessError("座椅数据回写, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) + } + if !success { + log.Error("座椅数据回写, 读取写入标识后台plc执行失败, 后台数据:" + string(boday)) + return false, grmi.NewBusinessError("座椅数据回写, 读取标识位后台plc执行失败, 后台数据:" + string(boday)) + } + content, exist := plcData["Content"] + if !exist { + log.Error("座椅数据回写, 读取写入标识后台返回data格式不正确, 后台数据:" + string(boday)) + return false, grmi.NewBusinessError("座椅数据回写, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) + } + sign, ok := content.(bool) + if !ok { + log.Error("座椅数据回写, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) + return false, grmi.NewBusinessError("座椅数据回写, 读取标识位后台返回data格式不正确, 后台数据:" + string(boday)) + } + if sign { + return true, nil + } else { + if retryNum >= 3 { + return false, nil + } + log.Error("座椅数据回写, 读取标识位不允许写入, 后台数据:" + string(boday)) + retryNum++ + time.Sleep(time.Millisecond * 500) + continue + } + } +} + +// BackFlushReadSeatData 座椅回写 读取座椅数据 +func (impl *PackOrderServiceImplement) BackFlushReadSeatData() (string, error) { + log, _ := logger.NewLogger("plc", "JIT") + data := baseModel.PLC{ + PLCType: conf.DbConfig.BackFlushPlcType, + PLCAddress: conf.DbConfig.BackFlushPlcAddress, + ValueAddress: conf.DbConfig.BackFlushPlcReadData, + ValueType: "Bytes", + RetryCount: conf.DbConfig.OfflinePLCRetry, + ByteCount: conf.DbConfig.BackFlushPlcReadLen, + } + postData, err := json.Marshal(data) + if err != nil { + log.Error("座椅数据回写, 序列化读取座椅数据请求数据失败, error:" + err.Error()) + return "", grmi.NewBusinessError("座椅数据回写, 序列化读取座椅数据请求数据失败, error:" + err.Error()) + } + log.Debug("座椅数据回写, 读取座椅数据请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCReadBackend) + boday, err := utils.Post(conf.DbConfig.PLCReadBackend, postData) + if err != nil { + log.Error("座椅数据回写, 读取座椅数据调用失败, error:" + err.Error()) + return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据调用失败, error:" + err.Error()) + } + log.Debug("座椅数据回写, 读取座椅数据请求响应:" + string(boday)) + + var response baseModel.Response + err = json.Unmarshal(boday, &response) + if err != nil { + log.Error("座椅数据回写, 解析读取座椅数据后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("座椅数据回写, 解析读取座椅数据后台数据失败, error:" + err.Error()) + } + if response.Code != iris.StatusOK { + log.Error("座椅数据回写,读取座椅数据调用后台失败, 后台返回错误, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("座椅数据回写,读取座椅数据调用后台失败, 后台返回错误, 错误:" + response.Msg) + } + plcData, ok := response.Data.(map[string]interface{}) + if !ok { + log.Error("座椅数据回写, 写入座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + successInterface, exist := plcData["IsSuccess"] + if !exist { + log.Error("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + success, ok := successInterface.(bool) + if !ok { + log.Error("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + if !success { + log.Error("座椅数据回写, 读取座椅数据后台plc执行失败, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据后台plc执行失败, 后台数据:" + string(boday)) + } + content, exist := plcData["Content"] + if !exist { + log.Error("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + seatContent, ok := content.(string) + if !ok { + log.Error("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + return "", grmi.NewBusinessError("座椅数据回写, 读取座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + length := len(seatContent) + for index, item := range seatContent { + if item == 0 { + length = index + break + } + } + return seatContent[:length], nil +} + +// BackFlushClearSeatData 座椅回写 清空座椅数据 +func (impl *PackOrderServiceImplement) BackFlushClearSeatData() error { + log, _ := logger.NewLogger("plc", "JIT") + data := baseModel.PLC{ + PLCType: conf.DbConfig.BackFlushPlcType, + PLCAddress: conf.DbConfig.BackFlushPlcAddress, + ValueAddress: conf.DbConfig.BackFlushPlcReadData, + ValueType: "Bytes", + RetryCount: conf.DbConfig.OfflinePLCRetry, + ByteCount: conf.DbConfig.BackFlushPlcReadLen, + ValueText: strings.Repeat("0", 96), + } + postData, err := json.Marshal(data) + if err != nil { + log.Error("座椅数据回写, 序列化清空座椅数据请求数据失败, error:" + err.Error()) + return grmi.NewBusinessError("座椅数据回写, 序列化清空椅数据请求数据失败, error:" + err.Error()) + } + log.Debug("座椅数据回写, 清空座椅数据请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCWriteBackend) + boday, err := utils.Post(conf.DbConfig.PLCWriteBackend, postData) + if err != nil { + log.Error("座椅数据回写, 清空座椅数据调用失败, error:" + err.Error()) + return grmi.NewBusinessError("座椅数据回写, 清空座椅数据调用失败, error:" + err.Error()) + } + log.Debug("座椅数据回写, 清空座椅数据请求响应:" + string(boday)) + + var response baseModel.Response + err = json.Unmarshal(boday, &response) + if err != nil { + log.Error("座椅数据回写, 清空读取座椅数据后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅数据回写, 清空读取座椅数据后台数据失败, error:" + err.Error()) + } + if response.Code != iris.StatusOK { + log.Error("座椅数据回写,清空座椅数据调用后台失败, 后台返回错误, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅数据回写,清空座椅数据调用后台失败, 后台返回错误, 错误:" + response.Msg) + } + plcData, ok := response.Data.(map[string]interface{}) + if !ok { + log.Error("座椅数据回写, 清空座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅数据回写, 清空座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + successInterface, exist := plcData["IsSuccess"] + if !exist { + log.Error("座椅数据回写, 清空座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅数据回写, 清空座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + success, ok := successInterface.(bool) + if !ok { + log.Error("座椅数据回写, 清空座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅数据回写, 清空座椅数据后台返回data格式不正确, 后台数据:" + string(boday)) + } + if !success { + log.Error("座椅数据回写, 清空座椅数据后台plc执行失败, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅数据回写, 清空座椅数据后台plc执行失败, 后台数据:" + string(boday)) + } + return nil +} + +// BackFlushWriteSign 座椅回写 写入完成标标识 +func (impl *PackOrderServiceImplement) BackFlushWriteSign() error { + data := baseModel.PLC{ + PLCType: conf.DbConfig.BackFlushPlcType, + PLCAddress: conf.DbConfig.BackFlushPlcAddress, + ValueAddress: conf.DbConfig.BackFlushPlcReadSign, + ValueType: "Boolean", + RetryCount: conf.DbConfig.OfflinePLCRetry, + ValueText: false, + } + log, _ := logger.NewLogger("座椅数据回写", "JIT") + postData, err := json.Marshal(data) + if err != nil { + log.Error("座椅数据回写, 序列化读取完成请求数据失败, error:" + err.Error()) + return grmi.NewBusinessError("座椅数据回写, 序列化读取完成请求数据失败, error:" + err.Error()) + } + log.Debug("座椅数据回写, 写入读取完成请求参数:" + string(postData) + ", url:" + conf.DbConfig.PLCWriteBackend) + boday, err := utils.Post(conf.DbConfig.PLCWriteBackend, postData) + if err != nil { + log.Error("座椅数据回写,写入读取完成调用失败, error:" + err.Error()) + return grmi.NewBusinessError("座椅数据回写, 写入读取完成调用失败, error:" + err.Error()) + } + log.Debug("座椅数据回写, 写入读取完成请求响应:" + string(boday)) + var response baseModel.Response + err = json.Unmarshal(boday, &response) + if err != nil { + log.Error("座椅数据回写, 解析写入读取完成后台数据失败, error:" + err.Error() + ", 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅数据回写, 解析写入读取完成后台数据失败, error:" + err.Error()) + } + if response.Code != iris.StatusOK { + log.Error("座椅数据回写,写入读取完成调用后台失败, 后台返回错误, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅数据回写,写入读取完成调用后台失败, 后台返回错误, 错误:" + response.Msg) + } + plcData, ok := response.Data.(map[string]interface{}) + if !ok { + log.Error("座椅数据回写, 写入读取完成后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅数据回写, 写入读取完成后台返回data格式不正确, 后台数据:" + string(boday)) + } + successInterface, exist := plcData["IsSuccess"] + if !exist { + log.Error("座椅数据回写, 写入读取完成后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅数据回写, 写入读取完成后台返回data格式不正确, 后台数据:" + string(boday)) + } + success, ok := successInterface.(bool) + if !ok { + log.Error("座椅数据回写, 写入读取完成后台返回data格式不正确, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅数据回写, 写入读取完成后台返回data格式不正确, 后台数据:" + string(boday)) + } + if !success { + log.Error("座椅数据回写, 写入读取完成后台plc执行失败, 后台数据:" + string(boday)) + return grmi.NewBusinessError("座椅数据回写, 写入读取完成后台plc执行失败, 后台数据:" + string(boday)) + } + return nil +} + +func (impl *PackOrderServiceImplement) ReadPLCBackFlushDataTask() { + + log, _ := logger.NewLogger("座椅数据回写", "JIT") + userId := "Scheduler" + engine := db.Eloquent.Master() + session := engine.NewSession() + defer session.Close() + plantDao := baseDal.NewPlantDAO(session, userId) + plantLi, err := plantDao.Select(nil, nil) + if err != nil { + log.Error("查询工厂数据失败, 错误:" + err.Error()) + return + } + for _, plant := range plantLi { + if plant.PlantProject != baseModel.PROJECT_NANSHA { + continue + } + go impl.ReadPLCBackFlushData(&global.User{ + UserId: userId, + PlantNr: plant.PlantNr, + }) + } +} + +// ReadPLCBackFlushData 座椅回写 业务逻辑 +func (impl *PackOrderServiceImplement) ReadPLCBackFlushData(user *global.User) { + log, _ := logger.NewLogger("座椅数据回写", "JIT") + uid, err := uuid.NewV1() + if err != nil { + log.Error("生成任务ID失败, 错误:" + err.Error()) + return + } + taskId := uid.String() + engine := db.Eloquent.Master() + session := engine.NewSession() + defer session.Close() + plcErrorDao := dal.NewPackOrderPlcErrorDAO(session, user.PlantNr, user.UserId) + // 1. 先查看有没有数据方面的错误没有处理 如果有错误则不进行任务 + //dataErrorInfoLi, err := plcErrorDao.Select([]grmi.Predicate{ + // meta.PackOrderPlcError_Location.NewPredicate(grmi.Equal, model.PACK_LOCATION_BACKFLUSH), + // meta.PackOrderPlcError_Status.NewPredicate(grmi.Equal, model.PACK_STATUS_PLANNED), + // meta.PackOrderPlcError_ErrorType.NewPredicate(grmi.Equal, plnModel.ERROR_TYPE_DATA), + //}, nil) + //if err != nil { + // log.Error("查询PLC错误项失败, 错误:"+err.Error(), "任务ID:"+taskId) + // return + //} + //if len(dataErrorInfoLi) != 0 { + // log.Info("存在尚未处理的数据项错误", "任务ID:"+taskId) + // return + //} + // 2. 先从PLC错误数据表中查看是否有没处理的错误 + errorInfoLi, err := plcErrorDao.Select([]grmi.Predicate{ + meta.PackOrderPlcError_Location.NewPredicate(grmi.Equal, model.PACK_LOCATION_BACKFLUSH), + meta.PackOrderPlcError_Status.NewPredicate(grmi.Equal, model.PACK_STATUS_PLANNED), + meta.PackOrderPlcError_ErrorType.NewPredicate(grmi.Equal, plnModel.ERROR_TYTPE_PLC), + }, nil) + if err != nil { + log.Error("查询PLC错误项失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + // 如果有plc交互的错误 直接写入标识位 完成此次任务 + if len(errorInfoLi) != 0 { + errorInfo := errorInfoLi[0] + if err = session.Begin(); err != nil { + log.Error("开启事务失败", "任务ID:"+taskId) + return + } + errorInfo.Status = model.PACK_STATUS_CLOSED + err = plcErrorDao.UpdateOne(&errorInfo) + if err != nil { + _ = session.Rollback() + log.Error("更新PLC错误项状态失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + switch errorInfo.Step { + case 3: + err = impl.BackFlushClearSeatData() + if err != nil { + _ = session.Rollback() + log.Error("根据PLC错误项清空座椅数据失败", "任务ID:"+taskId) + return + } + fallthrough + case 4: + err = impl.BackFlushWriteSign() + if err != nil { + _ = session.Rollback() + log.Error("根据PLC错误项写入完成标识失败", "任务ID:"+taskId) + return + } + } + _ = session.Commit() + log.Info("完成PLC错误项修复, 错误ID:"+errorInfo.PackOrderId, "任务ID:"+taskId) + return + } + // 如果没有PLC错误 从PLC读取数据回写PackOrderStatus的ShipStatus + packOrderDao := dal.NewPackOrderDAO(session, user.PlantNr, user.UserId) + packOrderStatusDao := dal.NewPackOrderStatusDAO(session, user.PlantNr, user.UserId) + sign, err := impl.BackFlushReadSign() + if err != nil { + log.Error("读取标识位失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + if !sign { + log.Info("暂无需要读取的数据", "任务ID:"+taskId) + return + } + seatData, err := impl.BackFlushReadSeatData() + if err != nil { + log.Info("读取座椅数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + seatBytes, err := hex.DecodeString(seatData) + seatStr := string(seatBytes) + if err != nil { + log.Error("解码座椅数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + plcErrorInfo := model.PackOrderPlcError{ + PackOrderId: taskId, + Location: model.PACK_LOCATION_BACKFLUSH, + Status: model.PACK_STATUS_PLANNED, + Step: -1, + ErrorInfo: "解码座椅数据失败, 错误:" + err.Error(), + SeatData: seatData, + ErrorType: plnModel.ERROR_TYPE_DATA, + } + err = plcErrorDao.InsertOne(&plcErrorInfo) + if err != nil { + log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + err = impl.BackFlushClearSeatData() + if err != nil { + log.Error("清空座椅数据失败, 错误:" + err.Error()) + plcErrorInfo = model.PackOrderPlcError{ + PackOrderId: taskId, + Location: model.PACK_LOCATION_BACKFLUSH, + Status: model.PACK_STATUS_PLANNED, + Step: 3, + ErrorInfo: "清空座椅数据失败, 错误:" + err.Error(), + SeatData: seatStr, + ErrorType: plnModel.ERROR_TYTPE_PLC, + } + err = plcErrorDao.InsertOne(&plcErrorInfo) + if err != nil { + log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + return + } + err = impl.BackFlushWriteSign() + if err != nil { + log.Error("写入标识位失败, 错误:" + err.Error()) + plcErrorInfo = model.PackOrderPlcError{ + PackOrderId: taskId, + Location: model.PACK_LOCATION_BACKFLUSH, + Status: model.PACK_STATUS_PLANNED, + Step: 4, + ErrorInfo: "写入标识位失败, 错误:" + err.Error(), + SeatData: seatStr, + ErrorType: plnModel.ERROR_TYTPE_PLC, + } + err = plcErrorDao.InsertOne(&plcErrorInfo) + if err != nil { + log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + return + } + } + packOrderLi, err := packOrderDao.Select([]grmi.Predicate{meta.PackOrder_RFID.NewPredicate(grmi.Equal, seatStr)}, nil) + if err != nil { + log.Error("查询包装单数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + if len(packOrderLi) == 0 { + log.Error("不存RFID对应的包装单, RFID:"+seatStr, "任务ID:"+taskId) + plcErrorInfo := model.PackOrderPlcError{ + PackOrderId: taskId, + Location: model.PACK_LOCATION_BACKFLUSH, + Status: model.PACK_STATUS_PLANNED, + Step: -2, + ErrorInfo: "不存RFID对应的包装单, RFID:" + seatStr, + SeatData: seatStr, + ErrorType: plnModel.ERROR_TYPE_DATA, + } + err = plcErrorDao.InsertOne(&plcErrorInfo) + if err != nil { + log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + err = impl.BackFlushClearSeatData() + if err != nil { + log.Error("清空座椅数据失败, 错误:" + err.Error()) + plcErrorInfo = model.PackOrderPlcError{ + PackOrderId: taskId, + Location: model.PACK_LOCATION_BACKFLUSH, + Status: model.PACK_STATUS_PLANNED, + Step: 3, + ErrorInfo: "清空座椅数据失败, 错误:" + err.Error(), + SeatData: seatStr, + ErrorType: plnModel.ERROR_TYTPE_PLC, + } + err = plcErrorDao.InsertOne(&plcErrorInfo) + if err != nil { + log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + return + } + err = impl.BackFlushWriteSign() + if err != nil { + log.Error("写入标识位失败, 错误:" + err.Error()) + plcErrorInfo = model.PackOrderPlcError{ + PackOrderId: taskId, + Location: model.PACK_LOCATION_BACKFLUSH, + Status: model.PACK_STATUS_PLANNED, + Step: 4, + ErrorInfo: "写入标识位失败, 错误:" + err.Error(), + SeatData: seatStr, + ErrorType: plnModel.ERROR_TYTPE_PLC, + } + err = plcErrorDao.InsertOne(&plcErrorInfo) + if err != nil { + log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + return + } + } + if len(packOrderLi) > 1 { + log.Error("该RFID绑定多个包装单, RFID:"+seatStr, "任务ID:"+taskId) + plcErrorInfo := model.PackOrderPlcError{ + PackOrderId: taskId, + Location: model.PACK_LOCATION_BACKFLUSH, + Status: model.PACK_STATUS_PLANNED, + Step: -2, + ErrorInfo: "该RFID绑定多个包装单, RFID:" + seatStr, + SeatData: seatStr, + ErrorType: plnModel.ERROR_TYPE_DATA, + } + err = plcErrorDao.InsertOne(&plcErrorInfo) + if err != nil { + log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + err = impl.BackFlushClearSeatData() + if err != nil { + log.Error("清空座椅数据失败, 错误:" + err.Error()) + plcErrorInfo = model.PackOrderPlcError{ + PackOrderId: taskId, + Location: model.PACK_LOCATION_BACKFLUSH, + Status: model.PACK_STATUS_PLANNED, + Step: 3, + ErrorInfo: "清空座椅数据失败, 错误:" + err.Error(), + SeatData: seatStr, + ErrorType: plnModel.ERROR_TYTPE_PLC, + } + err = plcErrorDao.InsertOne(&plcErrorInfo) + if err != nil { + log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + return + } + err = impl.BackFlushWriteSign() + if err != nil { + log.Error("写入标识位失败, 错误:" + err.Error()) + plcErrorInfo = model.PackOrderPlcError{ + PackOrderId: taskId, + Location: model.PACK_LOCATION_BACKFLUSH, + Status: model.PACK_STATUS_PLANNED, + Step: 4, + ErrorInfo: "写入标识位失败, 错误:" + err.Error(), + SeatData: seatStr, + ErrorType: plnModel.ERROR_TYTPE_PLC, + } + err = plcErrorDao.InsertOne(&plcErrorInfo) + if err != nil { + log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + return + } + } + packOrder := packOrderLi[0] + packOrderStatus, err := packOrderStatusDao.SelectOne(packOrder.PackOrderId) + if err != nil { + log.Error("查询包装单状态数据失败, 错误:" + err.Error()) + return + } + packOrderStatus.ShipStatus = model.SHIP_STATUS_CLOSED + err = packOrderStatusDao.UpdateOne(packOrderStatus) + if err != nil { + log.Error("更新包装单状态失败, 错误:" + err.Error()) + return + } + packOrder.CtrlTime1 = grmi.DateTime(time.Now()) + err = packOrderDao.UpdateOne(&packOrder) + if err != nil { + log.Error("更新包装单回写时间失败, 错误:" + err.Error()) + return + } + err = impl.BackFlushClearSeatData() + if err != nil { + log.Error("清空座椅数据失败, 错误:" + err.Error()) + plcErrorInfo := model.PackOrderPlcError{ + PackOrderId: taskId, + Location: model.PACK_LOCATION_BACKFLUSH, + Status: model.PACK_STATUS_PLANNED, + Step: 3, + ErrorInfo: "清空座椅数据失败, 错误:" + err.Error(), + SeatData: seatStr, + ErrorType: plnModel.ERROR_TYTPE_PLC, + } + err = plcErrorDao.InsertOne(&plcErrorInfo) + if err != nil { + log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + return + } + err = impl.BackFlushWriteSign() + if err != nil { + log.Error("写入标识位失败, 错误:" + err.Error()) + plcErrorInfo := model.PackOrderPlcError{ + PackOrderId: taskId, + Location: model.PACK_LOCATION_BACKFLUSH, + Status: model.PACK_STATUS_PLANNED, + Step: 4, + ErrorInfo: "写入标识位失败, 错误:" + err.Error(), + SeatData: seatStr, + ErrorType: plnModel.ERROR_TYTPE_PLC, + } + err = plcErrorDao.InsertOne(&plcErrorInfo) + if err != nil { + log.Error("写入PLC错误项数据失败, 错误:"+err.Error(), "任务ID:"+taskId) + return + } + return + } +} + +// 讴歌 + +// AUCRAPackPLC 讴歌下线打包 PLC放行 -- 讴歌线 +func (impl *PackOrderServiceImplement) AUCRAPackPLC() error { + data := baseModel.PLC{ + PLCType: conf.DbConfig.PermitThroughPlcType, + PLCAddress: conf.DbConfig.PermitThroughPlcAddress, + ValueAddress: conf.DbConfig.PermitThroughPlcDB, + ValueType: "Boolean", + RetryCount: 2, + ValueText: true, + } + err := impl.SeatOfflineWriteSign(data) + return err +} + +// BZPackPLC 讴歌下线打包 写包装数据到plc --缤智线 +func (impl *PackOrderServiceImplement) BZPackPLC(user *global.User, workplaceConfig *baseModel.WorkPlaceConfig, packOrderId string, quality string, recordErr bool, step int) error { + engine := db.Eloquent.Master() + session := engine.NewSession() + defer session.Close() + plcErrorDao := dal.NewPackOrderPlcErrorDAO(session, user.PlantNr, user.UserId) + log, _ := logger.NewLogger("缤智线下线打包", "JIT") + var ( + data baseModel.PLC + ) + switch step { + case 0, 1: + data = baseModel.PLC{ + PLCType: workplaceConfig.StrValue2, + PLCAddress: workplaceConfig.StrValue3, + ValueAddress: workplaceConfig.StrValue4, + ValueType: "Bytes", + RetryCount: workplaceConfig.IntValue2, + ByteCount: workplaceConfig.IntValue1, + } + existDataInterface, err := plc.ReadDataFromPLC(data, log, "String") + if err != nil { + errorInfo := "读取plc数据失败, " + err.Error() + if recordErr { + errorData := model.PackOrderPlcError{ + PackOrderId: packOrderId, + Status: model.PACK_STATUS_PLANNED, + Step: 1, + ErrorInfo: errorInfo, + ErrorType: model.ERROR_TYTPE_PLC, + Location: model.PACK_LOCATION_ON_LINE, + Source: "BZ-" + quality, + IntCtrl1: workplaceConfig.WorkPlaceNr, + IntCtrl2: workplaceConfig.ConfigNr, + } + innerErr := plcErrorDao.InsertOne(&errorData) + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + errorInfo) + } + } + return grmi.NewBusinessError(errorInfo) + } + existData := existDataInterface.(string) + if existData != "" { + errorInfo := "地址位数据未清空" + if recordErr { + errorData := model.PackOrderPlcError{ + PackOrderId: packOrderId, + Status: model.PACK_STATUS_PLANNED, + Step: 1, + ErrorInfo: errorInfo, + ErrorType: model.ERROR_TYTPE_PLC, + Location: model.PACK_LOCATION_ON_LINE, + Source: "BZ-" + quality, + IntCtrl1: workplaceConfig.WorkPlaceNr, + IntCtrl2: workplaceConfig.ConfigNr, + } + innerErr := plcErrorDao.InsertOne(&errorData) + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + errorInfo) + } + } + return grmi.NewBusinessError(errorInfo) + } + data = baseModel.PLC{ + PLCType: workplaceConfig.StrValue2, + PLCAddress: workplaceConfig.StrValue3, + ValueAddress: workplaceConfig.StrValue4, + ValueType: "Bytes", + ValueText: packOrderId, + RetryCount: workplaceConfig.IntValue2, + ByteCount: workplaceConfig.IntValue1, + } + err = plc.WriteDataToPLC(data, log) + if err != nil { + errorInfo := "写入包装单条码到plc失败, " + err.Error() + if recordErr { + errorData := model.PackOrderPlcError{ + PackOrderId: packOrderId, + Status: model.PACK_STATUS_PLANNED, + Step: 1, + ErrorInfo: errorInfo, + ErrorType: model.ERROR_TYTPE_PLC, + Location: model.PACK_LOCATION_ON_LINE, + Source: "BZ-" + quality, + IntCtrl1: workplaceConfig.WorkPlaceNr, + IntCtrl2: workplaceConfig.ConfigNr, + } + innerErr := plcErrorDao.InsertOne(&errorData) + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + errorInfo) + } + } + return grmi.NewBusinessError(errorInfo) + } + fallthrough + case 2: + if quality == model.PACK_QUALITY_NG { + data = baseModel.PLC{ + PLCType: workplaceConfig.StrValue2, + PLCAddress: workplaceConfig.StrValue3, + ValueAddress: workplaceConfig.StrValue5, + ValueType: "Boolean", + RetryCount: workplaceConfig.IntValue2, + } + existDataInterface, err := plc.ReadDataFromPLC(data, log, "String") + if err != nil { + errorInfo := "读取plc数据失败, " + err.Error() + if recordErr { + errorData := model.PackOrderPlcError{ + PackOrderId: packOrderId, + Status: model.PACK_STATUS_PLANNED, + Step: 1, + ErrorInfo: errorInfo, + ErrorType: model.ERROR_TYTPE_PLC, + Location: model.PACK_LOCATION_ON_LINE, + Source: "BZ-" + quality, + IntCtrl1: workplaceConfig.WorkPlaceNr, + IntCtrl2: workplaceConfig.ConfigNr, + } + innerErr := plcErrorDao.InsertOne(&errorData) + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + errorInfo) + } + } + return grmi.NewBusinessError(errorInfo) + } + existBool := existDataInterface.(bool) + if existBool { + errorInfo := "地址位数据非0" + if recordErr { + errorData := model.PackOrderPlcError{ + PackOrderId: packOrderId, + Status: model.PACK_STATUS_PLANNED, + Step: 1, + ErrorInfo: errorInfo, + ErrorType: model.ERROR_TYTPE_PLC, + Location: model.PACK_LOCATION_ON_LINE, + Source: "BZ-" + quality, + IntCtrl1: workplaceConfig.WorkPlaceNr, + IntCtrl2: workplaceConfig.ConfigNr, + } + innerErr := plcErrorDao.InsertOne(&errorData) + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + errorInfo) + } + } + return grmi.NewBusinessError(errorInfo) + } + data = baseModel.PLC{ + PLCType: workplaceConfig.StrValue2, + PLCAddress: workplaceConfig.StrValue3, + ValueAddress: workplaceConfig.StrValue5, + ValueType: "Boolean", + ValueText: true, + RetryCount: workplaceConfig.IntValue2, + } + err = plc.WriteDataToPLC(data, log) + if err != nil { + errorInfo := "写入标识位到plc失败, " + err.Error() + if recordErr { + errorData := model.PackOrderPlcError{ + PackOrderId: packOrderId, + Status: model.PACK_STATUS_PLANNED, + Step: 1, + ErrorInfo: errorInfo, + ErrorType: model.ERROR_TYTPE_PLC, + Location: model.PACK_LOCATION_ON_LINE, + Source: "BZ-" + quality, + IntCtrl1: workplaceConfig.WorkPlaceNr, + IntCtrl2: workplaceConfig.ConfigNr, + } + innerErr := plcErrorDao.InsertOne(&errorData) + if innerErr != nil { + return grmi.NewBusinessError(err.Error() + ", 保存错误失败, error:" + errorInfo) + } + } + return grmi.NewBusinessError(errorInfo) + } + } + } + return nil +} diff --git a/services/jit/implments/ShipOrder.service.impl.go b/services/jit/implments/ShipOrder.service.impl.go index 32bc076..d4310a9 100644 --- a/services/jit/implments/ShipOrder.service.impl.go +++ b/services/jit/implments/ShipOrder.service.impl.go @@ -762,8 +762,17 @@ func (impl *ShipOrderServiceImplement) ScanBarCode(user *global.User, barCode st shipOrderStatusDao := dal.NewShipOrderStatusDAO(session, user.PlantNr, user.UserId) shipOrderStatusHistoryDao := dal.NewShipOrderStatusHistoryDAO(session, user.PlantNr, user.UserId) workPlaceConfigDao := baseDal.NewWorkPlaceConfigDAO(session, user.PlantNr, user.UserId) + plantDao := baseDal.NewPlantDAO(session, user.UserId) + plant, err := plantDao.SelectOne(user.PlantNr) + if err != nil { + return nil, grmi.NewBusinessError("查询工厂数据失败, 错误:" + err.Error()) + } + if plant == nil { + return nil, grmi.NewBusinessError("用户归属工厂不存在, 工厂ID:" + strconv.Itoa(user.PlantNr)) + } + plantProject := plant.PlantProject var workPlaceConfig *baseModel.WorkPlaceConfig - if conf.DbConfig.Project == baseModel.PROJECT_ACURA { + if plantProject == baseModel.PROJECT_ACURA { // 获取工位配置 workPlaceConfigLi, err := workPlaceConfigDao.Select([]grmi.Predicate{baseMeta.WorkPlaceConfig_WorkPlaceNr.NewPredicate(grmi.Equal, workPlaceNr)}, nil) if err != nil { @@ -1071,7 +1080,7 @@ func (impl *ShipOrderServiceImplement) ScanBarCode(user *global.User, barCode st _ = session.Commit() result.Message = "扫描完成,扫描包装单ID:" + packOrder.PackOrder.PackOrderId + ", 当前发运单ID:" + shipOrderId result.IsClosed = isClosed - if conf.DbConfig.Project == baseModel.PROJECT_ACURA { + if plantProject == baseModel.PROJECT_ACURA { err = impl.PermitTrough(workPlaceConfig) if err != nil { result.PLCResult = false @@ -1198,7 +1207,7 @@ func (impl *ShipOrderServiceImplement) ScanBarCode(user *global.User, barCode st return nil, grmi.NewBusinessError("更新发运单数据失败, error:" + err.Error()) } _ = session.Commit() - if conf.DbConfig.Project == baseModel.PROJECT_ACURA { + if plantProject == baseModel.PROJECT_ACURA { err = impl.PermitTrough(workPlaceConfig) if err != nil { result.PLCResult = false @@ -1253,7 +1262,7 @@ func (impl *ShipOrderServiceImplement) ScanBarCode(user *global.User, barCode st _ = session.Commit() result.Message = "扫描完成,扫描包装单ID:" + packOrder.PackOrder.PackOrderId + ", 当前发运单ID:" + shipOrderId result.IsClosed = false - if conf.DbConfig.Project == baseModel.PROJECT_ACURA { + if plantProject == baseModel.PROJECT_ACURA { err = impl.PermitTrough(workPlaceConfig) if err != nil { result.PLCResult = false diff --git a/services/om/implments/SerialOrder.service.impl.go b/services/om/implments/SerialOrder.service.impl.go index 0b72322..5552351 100644 --- a/services/om/implments/SerialOrder.service.impl.go +++ b/services/om/implments/SerialOrder.service.impl.go @@ -2162,7 +2162,7 @@ func (impl *SerialOrderServiceImplement) GetOrderPOLi(poData *baseModel.Process, prePoData, exist := allPoMap[poId] if exist { prePoDataLi = append(prePoDataLi, prePoData) - if prePoData.NextPO != "" { + if prePoData.PrePO != "" { innerPrePoDataLi := impl.GetOrderPOLi(prePoData, allPoMap) prePoDataLi = append(prePoDataLi, innerPrePoDataLi...) } diff --git a/services/pln/implments/HondaCalloff.service.impl.go b/services/pln/implments/HondaCalloff.service.impl.go index 39057a7..c7ab107 100644 --- a/services/pln/implments/HondaCalloff.service.impl.go +++ b/services/pln/implments/HondaCalloff.service.impl.go @@ -1646,6 +1646,9 @@ func (impl *HondaCalloffServiceImplement) AnalysisTask() { return } for _, plant := range plantLi { + if plant.PlantProject != baseModel.PROJECT_ACURA { + continue + } go func(plant baseModel.Plant) { dao := dal.NewHondaCalloffDAO(session, plant.PlantNr, userId) callOffLi, err := dao.Select([]grmi.Predicate{meta.HondaCalloff_Parsed.NewPredicate(grmi.Equal, model.CALL_OFF_LOADED)}, []grmi.Field{meta.HondaCalloff_CalloffId}) diff --git a/services/pln/implments/ToyotaCalloff.service.impl.go b/services/pln/implments/ToyotaCalloff.service.impl.go index 960edda..2cd9b5f 100644 --- a/services/pln/implments/ToyotaCalloff.service.impl.go +++ b/services/pln/implments/ToyotaCalloff.service.impl.go @@ -370,6 +370,9 @@ func (impl *ToyotaCalloffServiceImplement) ParseToyotaCallOffFile() { return } for _, plant := range plantLi { + if plant.PlantProject != baseModel.PROJECT_NANSHA { + continue + } go func(plant baseModel.Plant) { user := &global.User{ UserId: userId, @@ -543,6 +546,9 @@ func (impl *ToyotaCalloffServiceImplement) ParseToyotaCallOffCache() { return } for _, plant := range plantLi { + if plant.PlantProject != baseModel.PROJECT_NANSHA { + continue + } go func(plant baseModel.Plant) { user := &global.User{ UserId: userId, diff --git a/services/pln/implments/ToyotaDeliveryOrder.service.impl.go b/services/pln/implments/ToyotaDeliveryOrder.service.impl.go index 5c2dfb2..b543e30 100644 --- a/services/pln/implments/ToyotaDeliveryOrder.service.impl.go +++ b/services/pln/implments/ToyotaDeliveryOrder.service.impl.go @@ -376,6 +376,9 @@ func (impl *ToyotaDeliveryOrderServiceImplement) WriteDeliveryDataToPLCTask() { return } for _, plant := range plantLi { + if plant.PlantProject != baseModel.PROJECT_NANSHA { + continue + } go impl.WriteDeliveryDataToPLC(&global.User{ UserId: userId, PlantNr: plant.PlantNr, diff --git a/services/qm/implments/ReclinerReclst.service.impl.go b/services/qm/implments/ReclinerReclst.service.impl.go index cd0ebef..073f39d 100644 --- a/services/qm/implments/ReclinerReclst.service.impl.go +++ b/services/qm/implments/ReclinerReclst.service.impl.go @@ -3,7 +3,6 @@ package implments import ( - "LAPP_ACURA_MOM_BACKEND/conf" baseDal "LAPP_ACURA_MOM_BACKEND/dao/base" jitDal "LAPP_ACURA_MOM_BACKEND/dao/jit" meDal "LAPP_ACURA_MOM_BACKEND/dao/me" @@ -23,6 +22,7 @@ import ( model "LAPP_ACURA_MOM_BACKEND/models/qm" svr "LAPP_ACURA_MOM_BACKEND/services/om" "LAPP_ACURA_MOM_BACKEND/utils" + "strconv" "time" ) @@ -728,6 +728,15 @@ func (impl *ReclinerReclstServiceImplement) CreateRecPrintInfo(entity model.Recl engine := db.Eloquent.Master() session := engine.NewSession() defer session.Close() + plantDao := baseDal.NewPlantDAO(session, "sys") + plant, err := plantDao.SelectOne(entity.PlantNr) + if err != nil { + return grmi.NewBusinessError("查询工厂数据失败, 错误:" + err.Error()) + } + if plant == nil { + return grmi.NewBusinessError("用户归属工厂不存在, 工厂ID:" + strconv.Itoa(entity.PlantNr)) + } + plantProject := plant.PlantProject seriDao := omDal.NewSerialOrderDAO(session, entity.PlantNr, "sys") seriInfo, err := seriDao.SelectBySerialOrder(entity.SerialOrderId) if err != nil { @@ -796,7 +805,7 @@ func (impl *ReclinerReclstServiceImplement) CreateRecPrintInfo(entity model.Recl head.Status = 0 head.LabelTemplateId = result.LabelTemplateId head.PrinterId = result.PrinterId - if conf.DbConfig.Project == baseModel.PROJECT_NANSHA { + if plantProject == baseModel.PROJECT_NANSHA { head.PrintQty = 1 } else { head.PrintQty = 2 diff --git a/task/scheduler.go b/task/scheduler.go index f98ff3d..3fb8782 100644 --- a/task/scheduler.go +++ b/task/scheduler.go @@ -1,8 +1,6 @@ package task import ( - "LAPP_ACURA_MOM_BACKEND/conf" - baseModel "LAPP_ACURA_MOM_BACKEND/models/base" "github.com/robfig/cron" ) @@ -21,31 +19,28 @@ import ( func Start() error { c := cron.New() var err error - if conf.DbConfig.Project == baseModel.PROJECT_ACURA { - // 解析HondaCallOff - err = c.AddFunc("*/5 * * * * ?", StartParseCallOff) - if err != nil { - return err - } - } else { - err = c.AddFunc("*/5 * * * * ?", ParsedToyotaCallOffFile) - if err != nil { - return err - } - err = c.AddFunc("*/5 * * * * ?", ParsedToyotaCallOff) - if err != nil { - return err - } - err = c.AddFunc("*/5 * * * * ?", WriteDeliveryOrderToPLC) - if err != nil { - return err - } - err = c.AddFunc("*/5 * * * * ?", ReadDeliveryOrderDataFromPLC) - if err != nil { - return err - } + // 解析HondaCallOff + err = c.AddFunc("*/5 * * * * ?", StartParseCallOff) + if err != nil { + return err } + err = c.AddFunc("*/5 * * * * ?", ParsedToyotaCallOffFile) + if err != nil { + return err + } + err = c.AddFunc("*/5 * * * * ?", ParsedToyotaCallOff) + if err != nil { + return err + } + err = c.AddFunc("*/5 * * * * ?", WriteDeliveryOrderToPLC) + if err != nil { + return err + } + err = c.AddFunc("*/5 * * * * ?", ReadDeliveryOrderDataFromPLC) + if err != nil { + return err + } c.Start() select {} } diff --git a/web/controllers/jit/PackOrder.rest.go b/web/controllers/jit/PackOrder.rest.go index 3ae4653..025a25d 100644 --- a/web/controllers/jit/PackOrder.rest.go +++ b/web/controllers/jit/PackOrder.rest.go @@ -10,6 +10,7 @@ import ( "LAPP_ACURA_MOM_BACKEND/web/supports" "github.com/kataras/iris/v12" "github.com/kataras/iris/v12/core/router" + "strconv" ) /****************************************************************************** @@ -413,7 +414,7 @@ func RegisterUpdatePackOrder(party router.Party, path string, method func(*globa * @Date : 2021-10-22 * ******************************************************************************/ -func RegisterScanBarcode(party router.Party, path string, method func(user *global.User, barcode string) (*model.PackOrderInfo, error)) { +func RegisterScanBarcode(party router.Party, path string, method func(user *global.User, barcode string, workplaceNr int) (*model.PackOrderInfo, error)) { party.Get(path, func(ctx iris.Context) { user, ok := jwts.ParseToken(ctx) @@ -425,7 +426,21 @@ func RegisterScanBarcode(party router.Party, path string, method func(user *glob supports.Error(ctx, iris.StatusBadRequest, "未获取到工单条码", nil) return } - result, err := method(user, barcode) + workplaceNrStr := ctx.URLParam("workPlaceNr") + var ( + workplaceNr int + err error + ) + if workplaceNrStr == "" { + workplaceNr = 0 + } else { + workplaceNr, err = strconv.Atoi(workplaceNrStr) + if err != nil { + supports.Error(ctx, iris.StatusBadRequest, "工位数据格式错误", nil) + return + } + } + result, err := method(user, barcode, workplaceNr) if err != nil { supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil) return