refactor: 删除 enterprise_device service 中无路由接入的死代码方法
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
ListDevicesForEnterprise、GetDeviceDetail、SuspendCard、ResumeCard、validateCardOperation 均无对应路由和 Handler 调用,且 SuspendCard/ResumeCard 仅修改本地 DB 未调 Gateway,存在错误实现风险。统一使用 iot_card stop_resume_service 处理停复机。 Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -391,214 +391,3 @@ func (s *Service) ListDevices(ctx context.Context, enterpriseID uint, req *dto.E
|
||||
Total: total,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ListDevicesForEnterprise 查询企业授权设备列表(H5企业用户)
|
||||
func (s *Service) ListDevicesForEnterprise(ctx context.Context, req *dto.EnterpriseDeviceListReq) (*dto.EnterpriseDeviceListResp, error) {
|
||||
enterpriseID := middleware.GetEnterpriseIDFromContext(ctx)
|
||||
if enterpriseID == 0 {
|
||||
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
opts := postgres.DeviceAuthListOptions{
|
||||
EnterpriseID: &enterpriseID,
|
||||
IncludeRevoked: false,
|
||||
Page: req.Page,
|
||||
PageSize: req.PageSize,
|
||||
}
|
||||
|
||||
auths, total, err := s.enterpriseDeviceAuthStore.ListByEnterprise(ctx, opts)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "查询授权记录失败")
|
||||
}
|
||||
|
||||
if len(auths) == 0 {
|
||||
return &dto.EnterpriseDeviceListResp{
|
||||
List: make([]dto.EnterpriseDeviceItem, 0),
|
||||
Total: 0,
|
||||
}, nil
|
||||
}
|
||||
|
||||
deviceIDs := make([]uint, 0, len(auths))
|
||||
authMap := make(map[uint]*model.EnterpriseDeviceAuthorization)
|
||||
for _, auth := range auths {
|
||||
deviceIDs = append(deviceIDs, auth.DeviceID)
|
||||
authMap[auth.DeviceID] = auth
|
||||
}
|
||||
|
||||
var devices []model.Device
|
||||
query := s.db.WithContext(ctx).Where("id IN ?", deviceIDs)
|
||||
if req.VirtualNo != "" {
|
||||
query = query.Where("virtual_no LIKE ?", "%"+req.VirtualNo+"%")
|
||||
}
|
||||
if err := query.Find(&devices).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "查询设备信息失败")
|
||||
}
|
||||
|
||||
var bindings []model.DeviceSimBinding
|
||||
if err := s.db.WithContext(ctx).
|
||||
Where("device_id IN ? AND bind_status = 1", deviceIDs).
|
||||
Find(&bindings).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "查询设备绑定卡失败")
|
||||
}
|
||||
|
||||
cardCountMap := make(map[uint]int)
|
||||
for _, binding := range bindings {
|
||||
cardCountMap[binding.DeviceID]++
|
||||
}
|
||||
|
||||
items := make([]dto.EnterpriseDeviceItem, 0, len(devices))
|
||||
for _, device := range devices {
|
||||
auth := authMap[device.ID]
|
||||
items = append(items, dto.EnterpriseDeviceItem{
|
||||
DeviceID: device.ID,
|
||||
VirtualNo: device.VirtualNo,
|
||||
DeviceName: device.DeviceName,
|
||||
DeviceModel: device.DeviceModel,
|
||||
CardCount: cardCountMap[device.ID],
|
||||
AuthorizedAt: auth.AuthorizedAt,
|
||||
})
|
||||
}
|
||||
|
||||
return &dto.EnterpriseDeviceListResp{
|
||||
List: items,
|
||||
Total: total,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetDeviceDetail 获取设备详情(H5企业用户)
|
||||
func (s *Service) GetDeviceDetail(ctx context.Context, deviceID uint) (*dto.EnterpriseDeviceDetailResp, error) {
|
||||
enterpriseID := middleware.GetEnterpriseIDFromContext(ctx)
|
||||
if enterpriseID == 0 {
|
||||
return nil, errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
auth, err := s.enterpriseDeviceAuthStore.GetByDeviceID(ctx, deviceID)
|
||||
if err != nil || auth.EnterpriseID != enterpriseID || auth.RevokedAt != nil {
|
||||
return nil, errors.New(errors.CodeDeviceNotAuthorized, "设备未授权给此企业")
|
||||
}
|
||||
|
||||
var device model.Device
|
||||
if err := s.db.WithContext(ctx).Where("id = ?", deviceID).First(&device).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "查询设备信息失败")
|
||||
}
|
||||
|
||||
var bindings []model.DeviceSimBinding
|
||||
if err := s.db.WithContext(ctx).
|
||||
Where("device_id = ? AND bind_status = 1", deviceID).
|
||||
Find(&bindings).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "查询设备绑定卡失败")
|
||||
}
|
||||
|
||||
cardIDs := make([]uint, 0, len(bindings))
|
||||
for _, binding := range bindings {
|
||||
cardIDs = append(cardIDs, binding.IotCardID)
|
||||
}
|
||||
|
||||
var cards []model.IotCard
|
||||
cardInfos := make([]dto.DeviceCardInfo, 0)
|
||||
if len(cardIDs) > 0 {
|
||||
if err := s.db.WithContext(ctx).Where("id IN ?", cardIDs).Find(&cards).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "查询卡信息失败")
|
||||
}
|
||||
|
||||
carrierIDs := make([]uint, 0, len(cards))
|
||||
for _, card := range cards {
|
||||
carrierIDs = append(carrierIDs, card.CarrierID)
|
||||
}
|
||||
|
||||
var carriers []model.Carrier
|
||||
carrierMap := make(map[uint]string)
|
||||
if len(carrierIDs) > 0 {
|
||||
if err := s.db.WithContext(ctx).Where("id IN ?", carrierIDs).Find(&carriers).Error; err == nil {
|
||||
for _, carrier := range carriers {
|
||||
carrierMap[carrier.ID] = carrier.CarrierName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, card := range cards {
|
||||
cardInfos = append(cardInfos, dto.DeviceCardInfo{
|
||||
CardID: card.ID,
|
||||
ICCID: card.ICCID,
|
||||
MSISDN: card.MSISDN,
|
||||
CarrierName: carrierMap[card.CarrierID],
|
||||
NetworkStatus: card.NetworkStatus,
|
||||
NetworkStatusName: constants.GetNetworkStatusName(card.NetworkStatus),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return &dto.EnterpriseDeviceDetailResp{
|
||||
Device: dto.EnterpriseDeviceInfo{
|
||||
DeviceID: device.ID,
|
||||
VirtualNo: device.VirtualNo,
|
||||
DeviceName: device.DeviceName,
|
||||
DeviceModel: device.DeviceModel,
|
||||
DeviceType: device.DeviceType,
|
||||
AuthorizedAt: auth.AuthorizedAt,
|
||||
},
|
||||
Cards: cardInfos,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) SuspendCard(ctx context.Context, deviceID, cardID uint, req *dto.DeviceCardOperationReq) (*dto.DeviceCardOperationResp, error) {
|
||||
if err := s.validateCardOperation(ctx, deviceID, cardID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := s.db.WithContext(ctx).Model(&model.IotCard{}).
|
||||
Where("id = ?", cardID).
|
||||
Update("network_status", 0).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "停机操作失败")
|
||||
}
|
||||
|
||||
return &dto.DeviceCardOperationResp{
|
||||
Success: true,
|
||||
Message: "停机成功",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) ResumeCard(ctx context.Context, deviceID, cardID uint, req *dto.DeviceCardOperationReq) (*dto.DeviceCardOperationResp, error) {
|
||||
if err := s.validateCardOperation(ctx, deviceID, cardID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := s.db.WithContext(ctx).Model(&model.IotCard{}).
|
||||
Where("id = ?", cardID).
|
||||
Update("network_status", 1).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeInternalError, err, "复机操作失败")
|
||||
}
|
||||
|
||||
return &dto.DeviceCardOperationResp{
|
||||
Success: true,
|
||||
Message: "复机成功",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) validateCardOperation(ctx context.Context, deviceID, cardID uint) error {
|
||||
enterpriseID := middleware.GetEnterpriseIDFromContext(ctx)
|
||||
if enterpriseID == 0 {
|
||||
return errors.New(errors.CodeUnauthorized, "未授权访问")
|
||||
}
|
||||
|
||||
auth, err := s.enterpriseDeviceAuthStore.GetByDeviceID(ctx, deviceID)
|
||||
if err != nil || auth.EnterpriseID != enterpriseID || auth.RevokedAt != nil {
|
||||
return errors.New(errors.CodeDeviceNotAuthorized, "设备未授权给此企业")
|
||||
}
|
||||
|
||||
var binding model.DeviceSimBinding
|
||||
if err := s.db.WithContext(ctx).
|
||||
Where("device_id = ? AND iot_card_id = ? AND bind_status = 1", deviceID, cardID).
|
||||
First(&binding).Error; err != nil {
|
||||
return errors.New(errors.CodeForbidden, "卡不属于该设备")
|
||||
}
|
||||
|
||||
var cardAuth model.EnterpriseCardAuthorization
|
||||
if err := s.db.WithContext(ctx).
|
||||
Where("enterprise_id = ? AND card_id = ? AND device_auth_id IS NOT NULL AND revoked_at IS NULL", enterpriseID, cardID).
|
||||
First(&cardAuth).Error; err != nil {
|
||||
return errors.New(errors.CodeForbidden, "无权操作此卡")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user