c4eede1114
CI / changes (push) Successful in 1m32s
CI / ad (push) Successful in 0s
CI / admin (push) Successful in 0s
CI / analysis (push) Successful in 3m45s
CI / bff (push) Successful in 31s
CI / chore (push) Successful in 0s
CI / equipment (push) Successful in 0s
CI / express (push) Successful in 1s
CI / factory (push) Successful in 0s
CI / message (push) Successful in 0s
CI / order (push) Successful in 0s
CI / payment (push) Successful in 0s
CI / percutaneous (push) Successful in 1s
CI / product (push) Successful in 0s
CI / sale (push) Successful in 0s
CI / record (push) Successful in 0s
CI / store (push) Successful in 0s
CI / support (push) Successful in 0s
CI / user (push) Successful in 1s
CI / task (push) Successful in 0s
CI / water (push) Successful in 0s
CI / wecom (push) Successful in 0s
CI / ws (push) Successful in 0s
94 lines
2.6 KiB
Go
94 lines
2.6 KiB
Go
package analysislogic
|
|
|
|
import (
|
|
"context"
|
|
|
|
"lone-services/pkg/utils"
|
|
"lone-services/pkg/validate"
|
|
analysis "lone-services/rpc/analysis/pb"
|
|
equipmentpb "lone-services/rpc/equipment/pb"
|
|
"lone-services/services/analysis/internal/logic/common"
|
|
"lone-services/services/analysis/internal/svc"
|
|
"lone-services/services/analysis/validator"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type DeviceUpdateLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewDeviceUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeviceUpdateLogic {
|
|
return &DeviceUpdateLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
type equipmentBindSnap struct {
|
|
HostCode string `json:"host_code"`
|
|
Type int32 `json:"type"`
|
|
}
|
|
|
|
func (l *DeviceUpdateLogic) DeviceUpdate(in *analysis.DeviceUpdateReq) (*analysis.Response, error) {
|
|
var req validator.DeviceUpdateValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return outParams(msg), nil
|
|
}
|
|
if _, fail := common.RequireLogin(l.ctx); fail != nil {
|
|
return fail, nil
|
|
}
|
|
if l.svcCtx.EquipmentSvcName == utils.StringEmpty {
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
cli, err := svc.GetRpcClient(l.svcCtx.EquipmentSvcName)
|
|
if err != nil {
|
|
l.Errorf("device update rpc: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
equipmentClient := equipmentpb.NewEquipmentClient(cli.Conn())
|
|
|
|
info, err := equipmentClient.InternalBindInfo(l.ctx, &equipmentpb.InternalBindInfoRequest{
|
|
Id: req.Id,
|
|
})
|
|
if err != nil {
|
|
l.Errorf("device update bind info: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
if info == nil {
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
if info.Code != utils.Ok.Code {
|
|
return &analysis.Response{Code: info.Code, Msg: info.Msg}, nil
|
|
}
|
|
|
|
var snap equipmentBindSnap
|
|
if info.Data != utils.StringEmpty && info.Data != "null" {
|
|
_ = jsoniter.UnmarshalFromString(info.Data, &snap)
|
|
}
|
|
if snap.HostCode == utils.StringEmpty || snap.Type == utils.NumberZero {
|
|
return common.FailResponse(utils.ErrorNotFund), nil
|
|
}
|
|
|
|
edited, err := equipmentClient.ApiEditName(l.ctx, &equipmentpb.ApiEditNameRequest{
|
|
HostCode: snap.HostCode,
|
|
HostNickname: req.DeviceName,
|
|
Type: snap.Type,
|
|
})
|
|
if err != nil {
|
|
l.Errorf("device update edit name: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
if edited == nil {
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
if edited.Code != utils.Ok.Code {
|
|
return &analysis.Response{Code: edited.Code, Msg: edited.Msg}, nil
|
|
}
|
|
return common.OkResponse(nil), nil
|
|
}
|