diff --git a/apps/develop/api/table.go b/apps/develop/api/table.go index 4a5ee3d..8f343d5 100644 --- a/apps/develop/api/table.go +++ b/apps/develop/api/table.go @@ -1,6 +1,7 @@ package api import ( + "github.com/PandaXGO/PandaKit/biz" "github.com/PandaXGO/PandaKit/model" "github.com/PandaXGO/PandaKit/restfulx" "github.com/PandaXGO/PandaKit/utils" @@ -23,7 +24,8 @@ func (g *GenTableApi) GetDBTableList(rc *restfulx.ReqCtx) { pageSize := restfulx.QueryInt(rc, "pageSize", 10) dbt.TableName = restfulx.QueryParam(rc, "tableName") - list, total := g.GenTableApp.FindDbTablesListPage(pageNum, pageSize, dbt) + list, total, err := g.GenTableApp.FindDbTablesListPage(pageNum, pageSize, dbt) + biz.ErrIsNil(err, "查询配置分页列表信息失败") rc.ResData = model.ResultPage{ Total: total, PageNum: int64(pageNum), @@ -42,7 +44,8 @@ func (g *GenTableApi) GetTablePage(rc *restfulx.ReqCtx) { dgt.RoleId = rc.LoginAccount.RoleId dgt.Owner = rc.LoginAccount.UserName - list, total := g.GenTableApp.FindListPage(pageNum, pageSize, dgt) + list, total, err := g.GenTableApp.FindListPage(pageNum, pageSize, dgt) + biz.ErrIsNil(err, "分页获取表失败") rc.ResData = model.ResultPage{ Total: total, PageNum: int64(pageNum), @@ -56,7 +59,8 @@ func (g *GenTableApi) GetTableInfo(rc *restfulx.ReqCtx) { dgt := entity.DevGenTable{} dgt.TableId = int64(restfulx.PathParamInt(rc, "tableId")) dgt.RoleId = rc.LoginAccount.RoleId - result := g.GenTableApp.FindOne(dgt, true) + result, err := g.GenTableApp.FindOne(dgt, true) + biz.ErrIsNil(err, "分页获取表信息失败") rc.ResData = vo.TableInfoVo{ List: result.Columns, Info: *result, @@ -68,7 +72,8 @@ func (g *GenTableApi) GetTableInfoByName(rc *restfulx.ReqCtx) { dgt := entity.DevGenTable{} dgt.TableName = restfulx.QueryParam(rc, "tableName") dgt.RoleId = rc.LoginAccount.RoleId - result := g.GenTableApp.FindOne(dgt, true) + result, err := g.GenTableApp.FindOne(dgt, true) + biz.ErrIsNil(err, "分页获取表信息失败") rc.ResData = vo.TableInfoVo{ List: result.Columns, Info: *result, @@ -80,7 +85,9 @@ func (g *GenTableApi) GetTableTree(rc *restfulx.ReqCtx) { dgt := entity.DevGenTable{} dgt.RoleId = rc.LoginAccount.RoleId dgt.Owner = rc.LoginAccount.UserName - rc.ResData = g.GenTableApp.FindTree(dgt) + tree, err := g.GenTableApp.FindTree(dgt) + biz.ErrIsNil(err, "获取树表信息失败") + rc.ResData = tree } // Insert 添加表结构 @@ -92,11 +99,14 @@ func (g *GenTableApi) Insert(rc *restfulx.ReqCtx) { index := i wg.Add(1) go func(wg *sync.WaitGroup, index int) { - genTable := gen.ToolsGenTableColumn.GenTableInit(tablesList[index]) + defer wg.Done() + genTable, err := gen.ToolsGenTableColumn.GenTableInit(tablesList[index]) + if err != nil { + return + } genTable.OrgId = rc.LoginAccount.OrganizationId genTable.Owner = rc.LoginAccount.UserName g.GenTableApp.Insert(genTable) - wg.Done() }(&wg, index) } wg.Wait() @@ -106,12 +116,14 @@ func (g *GenTableApi) Insert(rc *restfulx.ReqCtx) { func (g *GenTableApi) Update(rc *restfulx.ReqCtx) { var data entity.DevGenTable restfulx.BindJsonAndValid(rc, &data) - g.GenTableApp.Update(data) + _, err := g.GenTableApp.Update(data) + biz.ErrIsNil(err, "修改表结构") } // Delete 删除表结构 func (g *GenTableApi) Delete(rc *restfulx.ReqCtx) { tableIds := restfulx.PathParam(rc, "tableId") group := utils.IdsStrToIdsIntGroup(tableIds) - g.GenTableApp.Delete(group) + err := g.GenTableApp.Delete(group) + biz.ErrIsNil(err, "删除表结构") } diff --git a/apps/develop/gen/gen.go b/apps/develop/gen/gen.go index ce16602..2a24fe9 100644 --- a/apps/develop/gen/gen.go +++ b/apps/develop/gen/gen.go @@ -169,7 +169,7 @@ func (s *toolsGenTableColumn) CheckSexColumn(columnName string) bool { return false } -func (s *toolsGenTableColumn) GenTableInit(tableName string) entity.DevGenTable { +func (s *toolsGenTableColumn) GenTableInit(tableName string) (entity.DevGenTable, error) { var data entity.DevGenTable data.TableName = tableName tableNameList := strings.Split(tableName, "_") @@ -189,7 +189,10 @@ func (s *toolsGenTableColumn) GenTableInit(tableName string) entity.DevGenTable // 中横线表名称,接口路径、前端文件夹名称和js名称使用 data.ModuleName = strings.Replace(tableName, "_", "-", -1) - dbColumn := services.DevTableColumnModelDao.FindDbTableColumnList(tableName) + dbColumn, err := services.DevTableColumnModelDao.FindDbTableColumnList(tableName) + if err != nil { + return data, err + } data.TableComment = data.ClassName data.FunctionAuthor = "panda" @@ -335,11 +338,16 @@ func (s *toolsGenTableColumn) GenTableInit(tableName string) entity.DevGenTable }(&wg, index) } wg.Wait() - return data + return data, nil } // 视图预览 func Preview(tableId int64) map[string]any { + defer func() { + if err := recover(); &err != nil { + global.Log.Error(err) + } + }() t1, err := template.ParseFiles("resource/template/go/entity.template") biz.ErrIsNil(err, "entity模版读取失败") @@ -361,7 +369,7 @@ func Preview(tableId int64) map[string]any { t7, err := template.ParseFiles("resource/template/vue/edit-vue.template") biz.ErrIsNil(err, "vue编辑模版读取失败!") - tab := services.DevGenTableModelDao.FindOne(entity.DevGenTable{TableId: tableId}, false) + tab, err := services.DevGenTableModelDao.FindOne(entity.DevGenTable{TableId: tableId}, false) var b1 bytes.Buffer err = t1.Execute(&b1, tab) @@ -391,8 +399,14 @@ func Preview(tableId int64) map[string]any { // 生成 代码 func GenCode(tableId int64) { + defer func() { + if err := recover(); &err != nil { + global.Log.Error(err) + } + }() - tab := services.DevGenTableModelDao.FindOne(entity.DevGenTable{TableId: tableId}, false) + tab, err := services.DevGenTableModelDao.FindOne(entity.DevGenTable{TableId: tableId}, false) + biz.ErrIsNil(err, "读取表信息失败!") tab.ModuleName = strings.Replace(tab.TableName, "_", "-", -1) t1, err := template.ParseFiles("resource/template/go/entity.template") @@ -449,8 +463,10 @@ func GenCode(tableId int64) { // GenConfigure 生成菜单,api func GenConfigure(tableId, parentId int) { - tab := services.DevGenTableModelDao.FindOne(entity.DevGenTable{TableId: int64(tableId)}, false) - + tab, err := services.DevGenTableModelDao.FindOne(entity.DevGenTable{TableId: int64(tableId)}, false) + if err != nil { + return + } //生成菜单 一个菜单 三个按钮 component := "Layout" if parentId != 0 { diff --git a/apps/develop/services/gen_table.go b/apps/develop/services/gen_table.go index 7d8e2a5..fd7241e 100644 --- a/apps/develop/services/gen_table.go +++ b/apps/develop/services/gen_table.go @@ -17,16 +17,16 @@ import ( type ( SysGenTableModel interface { - FindDbTablesListPage(page, pageSize int, data entity.DBTables) (*[]entity.DBTables, int64) - FindDbTableOne(tableName string) *entity.DBTables + FindDbTablesListPage(page, pageSize int, data entity.DBTables) (*[]entity.DBTables, int64, error) + FindDbTableOne(tableName string) (*entity.DBTables, error) // 导入表数据 - Insert(data entity.DevGenTable) - FindOne(data entity.DevGenTable, exclude bool) *entity.DevGenTable - FindTree(data entity.DevGenTable) *[]entity.DevGenTable - FindListPage(page, pageSize int, data entity.DevGenTable) (*[]entity.DevGenTable, int64) - Update(data entity.DevGenTable) *entity.DevGenTable - Delete(tableIds []int64) + Insert(data entity.DevGenTable) error + FindOne(data entity.DevGenTable, exclude bool) (*entity.DevGenTable, error) + FindTree(data entity.DevGenTable) (*[]entity.DevGenTable, error) + FindListPage(page, pageSize int, data entity.DevGenTable) (*[]entity.DevGenTable, int64, error) + Update(data entity.DevGenTable) (*entity.DevGenTable, error) + Delete(tableIds []int64) error } devGenTableModelImpl struct { @@ -38,7 +38,7 @@ var DevGenTableModelDao SysGenTableModel = &devGenTableModelImpl{ table: "dev_gen_tables", } -func (m *devGenTableModelImpl) FindDbTablesListPage(page, pageSize int, data entity.DBTables) (*[]entity.DBTables, int64) { +func (m *devGenTableModelImpl) FindDbTablesListPage(page, pageSize int, data entity.DBTables) (*[]entity.DBTables, int64, error) { list := make([]entity.DBTables, 0) pgdata := make([]map[string]any, 0) var total int64 = 0 @@ -60,22 +60,20 @@ func (m *devGenTableModelImpl) FindDbTablesListPage(page, pageSize int, data ent } if global.Conf.Server.DbType == "mysql" { err := db.Limit(pageSize).Offset(offset).Find(&list).Offset(-1).Limit(-1).Count(&total).Error - biz.ErrIsNil(err, "查询配置分页列表信息失败") - return &list, total + return &list, total, err } else { err := db.Limit(pageSize).Offset(offset).Find(&pgdata).Offset(-1).Limit(-1).Count(&total).Error - biz.ErrIsNil(err, "查询配置分页列表信息失败") for _, pd := range pgdata { list = append(list, entity.DBTables{TableName: utils.B2S(pd["table_name"].([]uint8))}) } - return &list, total + return &list, total, err } } -func (m *devGenTableModelImpl) FindDbTableOne(tableName string) *entity.DBTables { +func (m *devGenTableModelImpl) FindDbTableOne(tableName string) (*entity.DBTables, error) { resData := new(entity.DBTables) if global.Conf.Server.DbType != "mysql" && global.Conf.Server.DbType != "postgresql" { - biz.ErrIsNil(errors.New("只支持mysql和postgresql数据库"), "只支持mysql和postgresql数据库") + return nil, errors.New("只支持mysql和postgresql数据库") } db := global.Db.Table("information_schema.tables") if global.Conf.Server.DbType == "mysql" { @@ -86,13 +84,14 @@ func (m *devGenTableModelImpl) FindDbTableOne(tableName string) *entity.DBTables } db = db.Where("table_name = ?", tableName) err := db.First(&resData).Error - biz.ErrIsNil(err, err.Error()) - return resData + return resData, err } -func (m *devGenTableModelImpl) Insert(dgt entity.DevGenTable) { +func (m *devGenTableModelImpl) Insert(dgt entity.DevGenTable) error { err := global.Db.Table(m.table).Create(&dgt).Error - biz.ErrIsNil(err, "新增生成代码表失败") + if err != nil { + return err + } for i := 0; i < len(dgt.Columns); i++ { dgt.Columns[i].TableId = dgt.TableId columns := dgt.Columns[i] @@ -100,9 +99,10 @@ func (m *devGenTableModelImpl) Insert(dgt entity.DevGenTable) { columns.Owner = dgt.Owner DevTableColumnModelDao.Insert(columns) } + return nil } -func (m *devGenTableModelImpl) FindOne(data entity.DevGenTable, exclude bool) *entity.DevGenTable { +func (m *devGenTableModelImpl) FindOne(data entity.DevGenTable, exclude bool) (*entity.DevGenTable, error) { resData := new(entity.DevGenTable) db := global.Db.Table(m.table) if data.TableName != "" { @@ -115,13 +115,15 @@ func (m *devGenTableModelImpl) FindOne(data entity.DevGenTable, exclude bool) *e db = db.Where("table_comment = ?", data.TableComment) } err := db.First(resData).Error - biz.ErrIsNil(err, "查询配置信息失败") - list := DevTableColumnModelDao.FindList(entity.DevGenTableColumn{TableId: resData.TableId}, exclude) + if err != nil { + return resData, err + } + list, err := DevTableColumnModelDao.FindList(entity.DevGenTableColumn{TableId: resData.TableId}, exclude) resData.Columns = *list - return resData + return resData, err } -func (m *devGenTableModelImpl) FindTree(data entity.DevGenTable) *[]entity.DevGenTable { +func (m *devGenTableModelImpl) FindTree(data entity.DevGenTable) (*[]entity.DevGenTable, error) { resData := make([]entity.DevGenTable, 0) db := global.Db.Table(m.table) @@ -135,20 +137,23 @@ func (m *devGenTableModelImpl) FindTree(data entity.DevGenTable) *[]entity.DevGe db = db.Where("table_comment = ?", data.TableComment) } // 组织数据访问权限 - model.OrgAuthSet(db, data.RoleId, data.Owner) - err := db.Find(&resData).Error - biz.ErrIsNil(err, "获取TableTree失败") + if err := model.OrgAuthSet(db, data.RoleId, data.Owner); err != nil { + return nil, err + } + if err := db.Find(&resData).Error; err != nil { + return nil, err + } for i := 0; i < len(resData); i++ { var col entity.DevGenTableColumn col.TableId = resData[i].TableId col.RoleId = data.RoleId - columns := DevTableColumnModelDao.FindList(col, false) + columns, _ := DevTableColumnModelDao.FindList(col, false) resData[i].Columns = *columns } - return &resData + return &resData, nil } -func (m *devGenTableModelImpl) FindListPage(page, pageSize int, data entity.DevGenTable) (*[]entity.DevGenTable, int64) { +func (m *devGenTableModelImpl) FindListPage(page, pageSize int, data entity.DevGenTable) (*[]entity.DevGenTable, int64, error) { list := make([]entity.DevGenTable, 0) var total int64 = 0 offset := pageSize * (page - 1) @@ -162,17 +167,24 @@ func (m *devGenTableModelImpl) FindListPage(page, pageSize int, data entity.DevG db = db.Where("table_comment = ?", data.TableComment) } // 组织数据访问权限 - model.OrgAuthSet(db, data.RoleId, data.Owner) + if err := model.OrgAuthSet(db, data.RoleId, data.Owner); err != nil { + return &list, total, err + } db.Where("delete_time IS NULL") - err := db.Count(&total).Error - err = db.Limit(pageSize).Offset(offset).Find(&list).Error - biz.ErrIsNil(err, "查询生成代码列表信息失败") - return &list, total + if err := db.Count(&total).Error; err != nil { + return &list, total, err + } + if err := db.Limit(pageSize).Offset(offset).Find(&list).Error; err != nil { + return &list, total, err + } + return &list, total, nil } -func (m *devGenTableModelImpl) Update(data entity.DevGenTable) *entity.DevGenTable { +func (m *devGenTableModelImpl) Update(data entity.DevGenTable) (*entity.DevGenTable, error) { err := global.Db.Table(m.table).Model(&data).Updates(&data).Error - biz.ErrIsNil(err, "修改生成代码信息失败") + if err != nil { + return nil, err + } tableNames := make([]string, 0) for i := range data.Columns { @@ -185,7 +197,9 @@ func (m *devGenTableModelImpl) Update(data entity.DevGenTable) *entity.DevGenTab tableMap := make(map[string]*entity.DevGenTable) if len(tableNames) > 0 { err = global.Db.Table(m.table).Where("table_name in (?)", tableNames).Find(&tables).Error - biz.ErrIsNil(err, "关联表不存在") + if err != nil { + return nil, err + } for i := range tables { tableMap[tables[i].TableName] = &tables[i] } @@ -203,10 +217,10 @@ func (m *devGenTableModelImpl) Update(data entity.DevGenTable) *entity.DevGenTab } DevTableColumnModelDao.Update(data.Columns[i]) } - return &data + return &data, nil } -func (e *devGenTableModelImpl) DeleteTables(tableId int64) bool { +func (e *devGenTableModelImpl) DeleteTables(tableId int64) (bool, error) { var err error success := false tx := global.Db.Begin() @@ -218,18 +232,20 @@ func (e *devGenTableModelImpl) DeleteTables(tableId int64) bool { } }() if err = tx.Table("sys_tables").Delete(entity.DevGenTable{}, "table_id = ?", tableId).Error; err != nil { - return success + return success, err } if err = tx.Table("sys_columns").Delete(entity.DevGenTableColumn{}, "table_id = ?", tableId).Error; err != nil { - return success + return success, err } success = true - return success + return success, nil } -func (m *devGenTableModelImpl) Delete(configIds []int64) { +func (m *devGenTableModelImpl) Delete(configIds []int64) error { err := global.Db.Table(m.table).Delete(&entity.DevGenTable{}, "table_id in (?)", configIds).Error - biz.ErrIsNil(err, "删除生成代码信息失败") + if err != nil { + return errors.New("删除生成代码信息失败") + } DevTableColumnModelDao.Delete(configIds) - return + return nil } diff --git a/apps/develop/services/gen_table_column.go b/apps/develop/services/gen_table_column.go index 42ae972..5948166 100644 --- a/apps/develop/services/gen_table_column.go +++ b/apps/develop/services/gen_table_column.go @@ -2,7 +2,6 @@ package services import ( "errors" - "github.com/PandaXGO/PandaKit/biz" "pandax/apps/develop/entity" "pandax/pkg/global" ) @@ -15,13 +14,13 @@ import ( type ( SysGenTableColumnModel interface { - FindDbTablesColumnListPage(page, pageSize int, data entity.DBColumns) (*[]entity.DBColumns, int64) - FindDbTableColumnList(tableName string) []entity.DBColumns + FindDbTablesColumnListPage(page, pageSize int, data entity.DBColumns) (*[]entity.DBColumns, int64, error) + FindDbTableColumnList(tableName string) ([]entity.DBColumns, error) - Insert(data entity.DevGenTableColumn) *entity.DevGenTableColumn - FindList(data entity.DevGenTableColumn, exclude bool) *[]entity.DevGenTableColumn - Update(data entity.DevGenTableColumn) *entity.DevGenTableColumn - Delete(tableId []int64) + Insert(data entity.DevGenTableColumn) (*entity.DevGenTableColumn, error) + FindList(data entity.DevGenTableColumn, exclude bool) (*[]entity.DevGenTableColumn, error) + Update(data entity.DevGenTableColumn) (*entity.DevGenTableColumn, error) + Delete(tableId []int64) error } devTableColumnModelImpl struct { @@ -33,12 +32,12 @@ var DevTableColumnModelDao SysGenTableColumnModel = &devTableColumnModelImpl{ table: "dev_gen_table_columns", } -func (m *devTableColumnModelImpl) FindDbTablesColumnListPage(page, pageSize int, data entity.DBColumns) (*[]entity.DBColumns, int64) { +func (m *devTableColumnModelImpl) FindDbTablesColumnListPage(page, pageSize int, data entity.DBColumns) (*[]entity.DBColumns, int64, error) { list := make([]entity.DBColumns, 0) var total int64 = 0 offset := pageSize * (page - 1) if global.Conf.Server.DbType != "mysql" && global.Conf.Server.DbType != "postgresql" { - biz.ErrIsNil(errors.New("只支持mysql和postgresql数据库"), "只支持mysql和postgresql数据库") + return nil, 0, errors.New("只支持mysql和postgresql数据库") } db := global.Db.Table("information_schema.COLUMNS") if global.Conf.Server.DbType == "mysql" { @@ -54,14 +53,13 @@ func (m *devTableColumnModelImpl) FindDbTablesColumnListPage(page, pageSize int, err := db.Count(&total).Error err = db.Limit(pageSize).Offset(offset).Find(&list).Error - biz.ErrIsNil(err, "查询生成代码列表信息失败") - return &list, total + return &list, total, err } -func (m *devTableColumnModelImpl) FindDbTableColumnList(tableName string) []entity.DBColumns { +func (m *devTableColumnModelImpl) FindDbTableColumnList(tableName string) ([]entity.DBColumns, error) { if global.Conf.Server.DbType != "mysql" && global.Conf.Server.DbType != "postgresql" { - biz.ErrIsNil(errors.New("只支持mysql和postgresql数据库"), "只支持mysql和postgresql数据库") + return nil, errors.New("只支持mysql和postgresql数据库") } db := global.Db.Table("information_schema.columns") if global.Conf.Server.DbType == "mysql" { @@ -70,21 +68,26 @@ func (m *devTableColumnModelImpl) FindDbTableColumnList(tableName string) []enti if global.Conf.Server.DbType == "postgresql" { db = db.Where("table_schema = ? ", "public") } - biz.IsTrue(tableName != "", "table name cannot be empty!") + if tableName == "" { + return nil, errors.New("table name cannot be empty!") + } db = db.Where("table_name = ?", tableName) resData := make([]entity.DBColumns, 0) if global.Conf.Server.DbType == "mysql" { err := db.Find(&resData).Error - biz.ErrIsNil(err, "查询表字段失败") - return resData + return resData, err } if global.Conf.Server.DbType == "postgresql" { pr, err := getPgPR(tableName) - biz.ErrIsNil(err, "查询PG表主键字段失败") + if err != nil { + return resData, errors.New("查询PG表主键字段失败") + } resDataP := make([]entity.DBColumnsP, 0) err = db.Find(&resDataP).Error - biz.ErrIsNil(err, "查询表字段失败") + if err != nil { + return resData, errors.New("查询表字段失败") + } for _, data := range resDataP { dbc := entity.DBColumns{ TableSchema: data.TableSchema, @@ -107,9 +110,9 @@ func (m *devTableColumnModelImpl) FindDbTableColumnList(tableName string) []enti resData = append(resData, dbc) } - return resData + return resData, nil } - return resData + return resData, nil } func getPgPR(tableName string) (string, error) { @@ -127,13 +130,15 @@ WHERE return pkname, err } -func (m *devTableColumnModelImpl) Insert(dgt entity.DevGenTableColumn) *entity.DevGenTableColumn { +func (m *devTableColumnModelImpl) Insert(dgt entity.DevGenTableColumn) (*entity.DevGenTableColumn, error) { err := global.Db.Table(m.table).Create(&dgt).Error - biz.ErrIsNil(err, "新增生成代码字段表失败") - return &dgt + if err != nil { + global.Log.Error(err) + } + return &dgt, err } -func (m *devTableColumnModelImpl) FindList(data entity.DevGenTableColumn, exclude bool) *[]entity.DevGenTableColumn { +func (m *devTableColumnModelImpl) FindList(data entity.DevGenTableColumn, exclude bool) (*[]entity.DevGenTableColumn, error) { list := make([]entity.DevGenTableColumn, 0) db := global.Db.Table(m.table).Where("table_id = ?", data.TableId) if exclude { @@ -147,18 +152,15 @@ func (m *devTableColumnModelImpl) FindList(data entity.DevGenTableColumn, exclud db = db.Where("column_name not in(?)", notIn) } err := db.Find(&list).Error - biz.ErrIsNil(err, "查询生成代码字段表信息失败") - return &list + return &list, err } -func (m *devTableColumnModelImpl) Update(data entity.DevGenTableColumn) *entity.DevGenTableColumn { +func (m *devTableColumnModelImpl) Update(data entity.DevGenTableColumn) (*entity.DevGenTableColumn, error) { err := global.Db.Table(m.table).Model(&data).Updates(&data).Error - biz.ErrIsNil(err, "修改生成代码字段表失败") - return &data + return &data, err } -func (m *devTableColumnModelImpl) Delete(tableId []int64) { +func (m *devTableColumnModelImpl) Delete(tableId []int64) error { err := global.Db.Table(m.table).Delete(&entity.DevGenTableColumn{}, "table_id in (?)", tableId).Error - biz.ErrIsNil(err, "删除生成代码字段表失败") - return + return err } diff --git a/apps/device/api/device.go b/apps/device/api/device.go index bb5f102..7998994 100644 --- a/apps/device/api/device.go +++ b/apps/device/api/device.go @@ -30,11 +30,11 @@ type DeviceApi struct { func (p *DeviceApi) GetDevicePanel(rc *restfulx.ReqCtx) { var data entity.DeviceTotalOutput - data.DeviceInfo = p.DeviceApp.FindDeviceCount() - data.DeviceLinkStatusInfo = p.DeviceApp.FindDeviceCountGroupByLinkStatus() - data.DeviceCountType = p.DeviceApp.FindDeviceCountGroupByType() - data.AlarmInfo = p.DeviceAlarmApp.FindAlarmCount() - data.ProductInfo = p.ProductApp.FindProductCount() + data.DeviceInfo, _ = p.DeviceApp.FindDeviceCount() + data.DeviceLinkStatusInfo, _ = p.DeviceApp.FindDeviceCountGroupByLinkStatus() + data.DeviceCountType, _ = p.DeviceApp.FindDeviceCountGroupByType() + data.AlarmInfo, _ = p.DeviceAlarmApp.FindAlarmCount() + data.ProductInfo, _ = p.ProductApp.FindProductCount() rc.ResData = data } @@ -53,8 +53,8 @@ func (p *DeviceApi) GetDeviceList(rc *restfulx.ReqCtx) { data.RoleId = rc.LoginAccount.RoleId data.Owner = rc.LoginAccount.UserName - list, total := p.DeviceApp.FindListPage(pageNum, pageSize, data) - + list, total, err := p.DeviceApp.FindListPage(pageNum, pageSize, data) + biz.ErrIsNil(err, "查询设备列表失败") rc.ResData = model.ResultPage{ Total: total, PageNum: int64(pageNum), @@ -73,7 +73,8 @@ func (p *DeviceApi) GetDeviceListAll(rc *restfulx.ReqCtx) { data.RoleId = rc.LoginAccount.RoleId data.Owner = rc.LoginAccount.UserName - list := p.DeviceApp.FindList(data) + list, err := p.DeviceApp.FindList(data) + biz.ErrIsNil(err, "查询所有设备失败") rc.ResData = list } @@ -91,7 +92,8 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) { classify := restfulx.QueryParam(rc, "classify") device, err := p.DeviceApp.FindOne(id) biz.ErrIsNil(err, "获取设备失败") - template := p.ProductTemplateApp.FindList(entity.ProductTemplate{Classify: classify, Pid: device.Pid}) + template, err := p.ProductTemplateApp.FindList(entity.ProductTemplate{Classify: classify, Pid: device.Pid}) + biz.ErrIsNil(err, "查询设备模板失败") // 从设备影子中读取 res := make([]entity.DeviceStatusVo, 0) getDevice := shadow.InitDeviceShadow(device.Name, device.Pid) @@ -113,20 +115,6 @@ func (p *DeviceApi) GetDeviceStatus(rc *restfulx.ReqCtx) { if _, ok := rs[tel.Key]; ok { if classify == global.TslTelemetryType { value := rs[tel.Key].Value - // tsl转化 - /*var tslValue tsl.ValueType - err := tool.MapToStruct(tel.Define, &tslValue) - if err != nil { - value = rs[tel.Key].Value - } else { - tslValue.Type = tel.Type - // 此处rs[tel.Key].Value 变成字符串类型了 - value = tslValue.ConvertValue(rs[tel.Key].Value) - log.Println("value", value) - if value == nil { - value = rs[tel.Key] - } - }*/ sdv.Time = rs[tel.Key].UpdatedAt sdv.Value = value } @@ -189,36 +177,38 @@ func (p *DeviceApi) DownAttribute(rc *restfulx.ReqCtx) { func (p *DeviceApi) InsertDevice(rc *restfulx.ReqCtx) { var data entity.Device restfulx.BindJsonAndValid(rc, &data) - product := p.ProductApp.FindOne(data.Pid) + product, _ := p.ProductApp.FindOne(data.Pid) biz.NotNil(product, "未查到所属产品信息") data.Owner = rc.LoginAccount.UserName data.OrgId = rc.LoginAccount.OrganizationId - list := p.DeviceApp.FindList(entity.Device{Name: data.Name}) + list, _ := p.DeviceApp.FindList(entity.Device{Name: data.Name}) biz.IsTrue(!(list != nil && len(*list) > 0), fmt.Sprintf("名称%s已存在,设置其他命名", data.Name)) data.Id = model2.GenerateID() data.LinkStatus = global.INACTIVE data.LastAt = time.Now() data.Protocol = product.ProtocolName - p.DeviceApp.Insert(data) + _, err := p.DeviceApp.Insert(data) + biz.ErrIsNil(err, "添加设备失败") } // UpdateDevice 修改Device func (p *DeviceApi) UpdateDevice(rc *restfulx.ReqCtx) { var data entity.Device restfulx.BindQuery(rc, &data) - product := p.ProductApp.FindOne(data.Pid) + product, _ := p.ProductApp.FindOne(data.Pid) biz.NotNil(product, "未查到所属产品信息") data.Protocol = product.ProtocolName - p.DeviceApp.Update(data) + _, err := p.DeviceApp.Update(data) + biz.ErrIsNil(err, "修改失败") } // DeleteDevice 删除Device func (p *DeviceApi) DeleteDevice(rc *restfulx.ReqCtx) { id := restfulx.PathParam(rc, "id") ids := strings.Split(id, ",") - p.DeviceApp.Delete(ids) + biz.ErrIsNil(p.DeviceApp.Delete(ids), "删除失败") } func (p *DeviceApi) ScreenTwinData(rc *restfulx.ReqCtx) { @@ -227,9 +217,9 @@ func (p *DeviceApi) ScreenTwinData(rc *restfulx.ReqCtx) { classId := restfulx.QueryParam(rc, "classId") if classId == "" { vp := make([]entity.VisualClass, 0) - list := p.ProductApp.FindList(entity.Product{}) + list, _ := p.ProductApp.FindList(entity.Product{}) for _, pro := range *list { - data := p.ProductTemplateApp.FindListAttrs(entity.ProductTemplate{Pid: pro.Id}) + data, _ := p.ProductTemplateApp.FindListAttrs(entity.ProductTemplate{Pid: pro.Id}) vta := make([]entity.VisualTwinAttr, 0) for _, attr := range *data { twinAttr := entity.VisualTwinAttr{ @@ -258,7 +248,7 @@ func (p *DeviceApi) ScreenTwinData(rc *restfulx.ReqCtx) { } else { device := entity.Device{Pid: classId, RoleId: rc.LoginAccount.RoleId} device.Owner = rc.LoginAccount.UserName - findList, _ := p.DeviceApp.FindListPage(pageNum, pageSize, device) + findList, _, _ := p.DeviceApp.FindListPage(pageNum, pageSize, device) vt := make([]entity.VisualTwin, 0) for _, device := range *findList { vt = append(vt, entity.VisualTwin{ diff --git a/apps/device/api/device_alarm.go b/apps/device/api/device_alarm.go index d3b31c2..0e8741b 100644 --- a/apps/device/api/device_alarm.go +++ b/apps/device/api/device_alarm.go @@ -2,6 +2,7 @@ package api // ========================================================================== import ( + "github.com/PandaXGO/PandaKit/biz" "github.com/PandaXGO/PandaKit/model" "github.com/PandaXGO/PandaKit/restfulx" "strings" @@ -15,7 +16,7 @@ type DeviceAlarmApi struct { } func (p *DeviceAlarmApi) GetDeviceAlarmPanel(rc *restfulx.ReqCtx) { - panel := p.DeviceAlarmApp.FindAlarmPanel() + panel, _ := p.DeviceAlarmApp.FindAlarmPanel() rc.ResData = panel } @@ -34,8 +35,8 @@ func (p *DeviceAlarmApi) GetDeviceAlarmList(rc *restfulx.ReqCtx) { data.RoleId = rc.LoginAccount.RoleId data.Owner = rc.LoginAccount.UserName - list, total := p.DeviceAlarmApp.FindListPage(pageNum, pageSize, data) - + list, total, err := p.DeviceAlarmApp.FindListPage(pageNum, pageSize, data) + biz.ErrIsNil(err, "设备查询失败") rc.ResData = model.ResultPage{ Total: total, PageNum: int64(pageNum), @@ -48,12 +49,14 @@ func (p *DeviceAlarmApi) GetDeviceAlarmList(rc *restfulx.ReqCtx) { func (p *DeviceAlarmApi) UpdateDeviceAlarm(rc *restfulx.ReqCtx) { var data entity.DeviceAlarm restfulx.BindJsonAndValid(rc, &data) - p.DeviceAlarmApp.Update(data) + err := p.DeviceAlarmApp.Update(data) + biz.ErrIsNil(err, "修改告警失败") } // DeleteDeviceAlarm 删除告警 func (p *DeviceAlarmApi) DeleteDeviceAlarm(rc *restfulx.ReqCtx) { id := restfulx.PathParam(rc, "id") ids := strings.Split(id, ",") - p.DeviceAlarmApp.Delete(ids) + err := p.DeviceAlarmApp.Delete(ids) + biz.ErrIsNil(err, "删除告警失败") } diff --git a/apps/device/api/device_cmd.go b/apps/device/api/device_cmd.go index 77f270c..7bc400c 100644 --- a/apps/device/api/device_cmd.go +++ b/apps/device/api/device_cmd.go @@ -30,8 +30,8 @@ func (p *DeviceCmdLogApi) GetDeviceCmdLogList(rc *restfulx.ReqCtx) { data.State = restfulx.QueryParam(rc, "state") data.Type = restfulx.QueryParam(rc, "type") - list, total := p.DeviceCmdLogApp.FindListPage(pageNum, pageSize, data) - + list, total, err := p.DeviceCmdLogApp.FindListPage(pageNum, pageSize, data) + biz.ErrIsNil(err, "查询告警列表数据失败") rc.ResData = model.ResultPage{ Total: total, PageNum: int64(pageNum), @@ -74,5 +74,5 @@ func (p *DeviceCmdLogApi) InsertDeviceCmdLog(rc *restfulx.ReqCtx) { func (p *DeviceCmdLogApi) DeleteDeviceCmdLog(rc *restfulx.ReqCtx) { id := restfulx.PathParam(rc, "id") ids := strings.Split(id, ",") - p.DeviceCmdLogApp.Delete(ids) + biz.ErrIsNil(p.DeviceCmdLogApp.Delete(ids), "删除指令失败") } diff --git a/apps/device/api/device_group.go b/apps/device/api/device_group.go index ee85aab..7837bfb 100644 --- a/apps/device/api/device_group.go +++ b/apps/device/api/device_group.go @@ -1,6 +1,7 @@ package api import ( + "github.com/PandaXGO/PandaKit/biz" "github.com/PandaXGO/PandaKit/restfulx" "pandax/apps/device/entity" "pandax/apps/device/services" @@ -22,16 +23,18 @@ func (p *DeviceGroupApi) GetDeviceGroupTree(rc *restfulx.ReqCtx) { vsg.Id = id vsg.RoleId = rc.LoginAccount.RoleId vsg.Owner = rc.LoginAccount.UserName - - rc.ResData = p.DeviceGroupApp.SelectDeviceGroup(vsg) + group, err := p.DeviceGroupApp.SelectDeviceGroup(vsg) + biz.ErrIsNil(err, "查询设备组失败") + rc.ResData = group } func (p *DeviceGroupApi) GetDeviceGroupTreeLabel(rc *restfulx.ReqCtx) { vsg := entity.DeviceGroup{} vsg.RoleId = rc.LoginAccount.RoleId vsg.Owner = rc.LoginAccount.UserName - - rc.ResData = p.DeviceGroupApp.SelectDeviceGroupLabel(vsg) + label, err := p.DeviceGroupApp.SelectDeviceGroupLabel(vsg) + biz.ErrIsNil(err, "查询设备组失败") + rc.ResData = label } func (p *DeviceGroupApi) GetDeviceGroupList(rc *restfulx.ReqCtx) { @@ -45,9 +48,13 @@ func (p *DeviceGroupApi) GetDeviceGroupList(rc *restfulx.ReqCtx) { vsg.RoleId = rc.LoginAccount.RoleId vsg.Owner = rc.LoginAccount.UserName if vsg.Name == "" { - rc.ResData = p.DeviceGroupApp.SelectDeviceGroup(vsg) + group, err := p.DeviceGroupApp.SelectDeviceGroup(vsg) + biz.ErrIsNil(err, "查询设备组失败") + rc.ResData = group } else { - rc.ResData = p.DeviceGroupApp.FindList(vsg) + list, err := p.DeviceGroupApp.FindList(vsg) + biz.ErrIsNil(err, "查询设备组列表失败") + rc.ResData = list } } @@ -56,14 +63,17 @@ func (p *DeviceGroupApi) GetDeviceGroupAllList(rc *restfulx.ReqCtx) { var vsg entity.DeviceGroup vsg.RoleId = rc.LoginAccount.RoleId vsg.Owner = rc.LoginAccount.UserName - - rc.ResData = p.DeviceGroupApp.FindList(vsg) + list, err := p.DeviceGroupApp.FindList(vsg) + biz.ErrIsNil(err, "查询设备组列表失败") + rc.ResData = list } // GetDeviceGroup 获取DeviceGroup func (p *DeviceGroupApi) GetDeviceGroup(rc *restfulx.ReqCtx) { id := restfulx.PathParam(rc, "id") - rc.ResData = p.DeviceGroupApp.FindOne(id) + one, err := p.DeviceGroupApp.FindOne(id) + biz.ErrIsNil(err, "查询设备组失败") + rc.ResData = one } // InsertDeviceGroup 添加DeviceGroup diff --git a/apps/device/api/product.go b/apps/device/api/product.go index 07a2f24..337d40f 100644 --- a/apps/device/api/product.go +++ b/apps/device/api/product.go @@ -38,8 +38,8 @@ func (p *ProductApi) GetProductList(rc *restfulx.ReqCtx) { data.DeviceType = restfulx.QueryParam(rc, "deviceType") data.Name = restfulx.QueryParam(rc, "name") - list, total := p.ProductApp.FindListPage(pageNum, pageSize, data) - + list, total, err := p.ProductApp.FindListPage(pageNum, pageSize, data) + biz.ErrIsNil(err, "查询产品列表失败") rc.ResData = model.ResultPage{ Total: total, PageNum: int64(pageNum), @@ -55,15 +55,17 @@ func (p *ProductApi) GetProductListAll(rc *restfulx.ReqCtx) { data.ProtocolName = restfulx.QueryParam(rc, "protocolName") data.DeviceType = restfulx.QueryParam(rc, "deviceType") data.Name = restfulx.QueryParam(rc, "name") - - rc.ResData = p.ProductApp.FindList(data) + list, err := p.ProductApp.FindList(data) + biz.ErrIsNil(err, "查询产品列表失败") + rc.ResData = list } // GetProductTsl Template列表数据 func (p *ProductApi) GetProductTsl(rc *restfulx.ReqCtx) { data := entity.ProductTemplate{} data.Pid = restfulx.PathParam(rc, "id") - templates := p.TemplateApp.FindList(data) + templates, err := p.TemplateApp.FindList(data) + biz.ErrIsNil(err, "查询产品模板列表失败") attributes := make([]map[string]interface{}, 0) telemetry := make([]map[string]interface{}, 0) commands := make([]map[string]interface{}, 0) @@ -95,7 +97,9 @@ func (p *ProductApi) GetProductTsl(rc *restfulx.ReqCtx) { // GetProduct 获取Product func (p *ProductApi) GetProduct(rc *restfulx.ReqCtx) { id := restfulx.PathParam(rc, "id") - rc.ResData = p.ProductApp.FindOne(id) + one, err := p.ProductApp.FindOne(id) + biz.ErrIsNil(err, "查询失败") + rc.ResData = one } // InsertProduct 添加Product @@ -134,7 +138,7 @@ func (p *ProductApi) DeleteProduct(rc *restfulx.ReqCtx) { id := restfulx.PathParam(rc, "id") ids := strings.Split(id, ",") for _, id := range ids { - list := p.DeviceApp.FindList(entity.Device{Pid: id}) + list, _ := p.DeviceApp.FindList(entity.Device{Pid: id}) biz.IsTrue(!(list != nil && len(*list) > 0), fmt.Sprintf("产品ID%s下存在设备,不能被删除", id)) } // 删除产品 diff --git a/apps/device/api/product_category.go b/apps/device/api/product_category.go index 343a432..aea0316 100644 --- a/apps/device/api/product_category.go +++ b/apps/device/api/product_category.go @@ -1,6 +1,7 @@ package api import ( + "github.com/PandaXGO/PandaKit/biz" "github.com/PandaXGO/PandaKit/restfulx" "pandax/apps/device/entity" "pandax/apps/device/services" @@ -39,20 +40,26 @@ func (p *ProductCategoryApi) GetProductCategoryList(rc *restfulx.ReqCtx) { if sg.Name == "" { rc.ResData = p.ProductCategoryApp.SelectProductCategory(sg) } else { - rc.ResData = p.ProductCategoryApp.FindList(sg) + list, err := p.ProductCategoryApp.FindList(sg) + biz.ErrIsNil(err, "查询产品分类列表失败") + rc.ResData = list } } // GetProductCategoryAllList 查询所有 func (p *ProductCategoryApi) GetProductCategoryAllList(rc *restfulx.ReqCtx) { var vsg entity.ProductCategory - rc.ResData = p.ProductCategoryApp.FindList(vsg) + list, err := p.ProductCategoryApp.FindList(vsg) + biz.ErrIsNil(err, "查询产品分类列表失败") + rc.ResData = list } // GetProductCategory 获取ProductCategory func (p *ProductCategoryApi) GetProductCategory(rc *restfulx.ReqCtx) { id := restfulx.PathParam(rc, "id") - rc.ResData = p.ProductCategoryApp.FindOne(id) + one, err := p.ProductCategoryApp.FindOne(id) + biz.ErrIsNil(err, "查询产品分类失败") + rc.ResData = one } // InsertProductCategory 添加ProductCategory diff --git a/apps/device/api/product_ota.go b/apps/device/api/product_ota.go index 0df1f74..3f82f4d 100644 --- a/apps/device/api/product_ota.go +++ b/apps/device/api/product_ota.go @@ -28,8 +28,8 @@ func (p *ProductOtaApi) GetProductOtaList(rc *restfulx.ReqCtx) { data.Name = restfulx.QueryParam(rc, "name") data.Pid = restfulx.QueryParam(rc, "pid") - list, total := p.ProductOtaApp.FindListPage(pageNum, pageSize, data) - + list, total, err := p.ProductOtaApp.FindListPage(pageNum, pageSize, data) + biz.ErrIsNil(err, "查询OTA信息列表失败") rc.ResData = model.ResultPage{ Total: total, PageNum: int64(pageNum), @@ -41,7 +41,9 @@ func (p *ProductOtaApi) GetProductOtaList(rc *restfulx.ReqCtx) { // GetProductOta 获取Ota func (p *ProductOtaApi) GetProductOta(rc *restfulx.ReqCtx) { id := restfulx.PathParam(rc, "id") - rc.ResData = p.ProductOtaApp.FindOne(id) + one, err := p.ProductOtaApp.FindOne(id) + biz.ErrIsNil(err, "查询OTA信息失败") + rc.ResData = one } // InsertProductOta 添加Ota diff --git a/apps/device/api/product_template.go b/apps/device/api/product_template.go index 06de658..b05c065 100644 --- a/apps/device/api/product_template.go +++ b/apps/device/api/product_template.go @@ -25,8 +25,8 @@ func (p *ProductTemplateApi) GetProductTemplateList(rc *restfulx.ReqCtx) { data.Classify = restfulx.QueryParam(rc, "classify") data.Name = restfulx.QueryParam(rc, "name") - list, total := p.ProductTemplateApp.FindListPage(pageNum, pageSize, data) - + list, total, err := p.ProductTemplateApp.FindListPage(pageNum, pageSize, data) + biz.ErrIsNil(err, "查询产品模板列表失败") rc.ResData = model.ResultPage{ Total: total, PageNum: int64(pageNum), @@ -41,14 +41,17 @@ func (p *ProductTemplateApi) GetProductTemplateListAll(rc *restfulx.ReqCtx) { data.Pid = restfulx.QueryParam(rc, "pid") data.Classify = restfulx.QueryParam(rc, "classify") data.Name = restfulx.QueryParam(rc, "name") - list := p.ProductTemplateApp.FindList(data) + list, err := p.ProductTemplateApp.FindList(data) + biz.ErrIsNil(err, "查询产品模板列表失败") rc.ResData = list } // GetProductTemplate 获取Template func (p *ProductTemplateApi) GetProductTemplate(rc *restfulx.ReqCtx) { id := restfulx.PathParam(rc, "id") - rc.ResData = p.ProductTemplateApp.FindOne(id) + one, err := p.ProductTemplateApp.FindOne(id) + biz.ErrIsNil(err, "查询产品模板失败") + rc.ResData = one } // InsertProductTemplate 添加Template @@ -77,7 +80,8 @@ func (p *ProductTemplateApi) InsertProductTemplate(rc *restfulx.ReqCtx) { func (p *ProductTemplateApi) UpdateProductTemplate(rc *restfulx.ReqCtx) { var data entity.ProductTemplate restfulx.BindJsonAndValid(rc, &data) - one := p.ProductTemplateApp.FindOne(data.Id) + one, err := p.ProductTemplateApp.FindOne(data.Id) + biz.ErrIsNil(err, "查询产品模板失败") stable := "" len := 0 if data.Classify == entity.ATTRIBUTES_TSL { @@ -104,7 +108,8 @@ func (p *ProductTemplateApi) UpdateProductTemplate(rc *restfulx.ReqCtx) { // DeleteProductTemplate 删除Template func (p *ProductTemplateApi) DeleteProductTemplate(rc *restfulx.ReqCtx) { id := restfulx.PathParam(rc, "id") - data := p.ProductTemplateApp.FindOne(id) + data, err := p.ProductTemplateApp.FindOne(id) + biz.ErrIsNil(err, "查询产品模板失败") // 向超级表中删除字段 stable := "" if data.Classify == entity.ATTRIBUTES_TSL { diff --git a/apps/device/services/device.go b/apps/device/services/device.go index e698bda..f9dbe4a 100644 --- a/apps/device/services/device.go +++ b/apps/device/services/device.go @@ -1,7 +1,7 @@ package services import ( - "github.com/PandaXGO/PandaKit/biz" + "errors" "pandax/apps/device/entity" "pandax/pkg/cache" "pandax/pkg/global" @@ -11,18 +11,18 @@ import ( type ( DeviceModel interface { - Insert(data entity.Device) *entity.Device + Insert(data entity.Device) (*entity.Device, error) FindOneByToken(token string) (*entity.DeviceRes, error) FindOneByName(name string) (*entity.DeviceRes, error) FindOne(id string) (*entity.DeviceRes, error) - FindListPage(page, pageSize int, data entity.Device) (*[]entity.DeviceRes, int64) - FindList(data entity.Device) *[]entity.DeviceRes - Update(data entity.Device) *entity.Device + FindListPage(page, pageSize int, data entity.Device) (*[]entity.DeviceRes, int64, error) + FindList(data entity.Device) (*[]entity.DeviceRes, error) + Update(data entity.Device) (*entity.Device, error) UpdateStatus(id, linkStatus string) error - Delete(ids []string) - FindDeviceCount() entity.DeviceCount - FindDeviceCountGroupByLinkStatus() []entity.DeviceCountLinkStatus - FindDeviceCountGroupByType() []entity.DeviceCountType + Delete(ids []string) error + FindDeviceCount() (entity.DeviceCount, error) + FindDeviceCountGroupByLinkStatus() ([]entity.DeviceCountLinkStatus, error) + FindDeviceCountGroupByType() ([]entity.DeviceCountType, error) } deviceModelImpl struct { @@ -34,35 +34,44 @@ var DeviceModelDao DeviceModel = &deviceModelImpl{ table: `devices`, } -func (m *deviceModelImpl) Insert(data entity.Device) *entity.Device { +func (m *deviceModelImpl) Insert(data entity.Device) (*entity.Device, error) { tx := global.Db.Begin() //1 检查设备名称是否存在 - list := m.FindList(entity.Device{Name: data.Name}) - biz.IsTrue(list != nil && len(*list) == 0, "设备名称已经存在") + list, _ := m.FindList(entity.Device{Name: data.Name}) + if list != nil && len(*list) > 0 { + return nil, errors.New("设备名称已经存在") + } //2 创建认证TOKEN IOTHUB使用 token := GetDeviceToken(&data) // 子网关不需要设置token if data.DeviceType == global.GATEWAYS { err := cache.SetDeviceEtoken(data.Name, token.GetMarshal(), time.Hour*24*365) - biz.ErrIsNil(err, "设备缓存失败") + if err != nil { + return nil, err + } } else { data.Token = token.MD5ID() err := cache.SetDeviceEtoken(data.Token, token.GetMarshal(), time.Hour*24*365) - biz.ErrIsNil(err, "设备缓存失败") + if err != nil { + return nil, err + } } //3 添加设备 err := tx.Table(m.table).Create(&data).Error - biz.ErrIsNilAppendErr(err, "添加设备失败") + if err != nil { + tx.Rollback() + return nil, err + } // 创建超级表 失败就 if data.Pid != "" { err = createDeviceTable(data.Pid, data.Name) if err != nil { tx.Rollback() - biz.ErrIsNil(err, "时序数据设备表创建失败") + return nil, errors.New("时序数据设备表创建失败") } } tx.Commit() - return &data + return &data, nil } func (m *deviceModelImpl) FindOne(id string) (*entity.DeviceRes, error) { @@ -86,7 +95,7 @@ func (m *deviceModelImpl) FindOneByToken(token string) (*entity.DeviceRes, error return resData, err } -func (m *deviceModelImpl) FindListPage(page, pageSize int, data entity.Device) (*[]entity.DeviceRes, int64) { +func (m *deviceModelImpl) FindListPage(page, pageSize int, data entity.Device) (*[]entity.DeviceRes, int64, error) { list := make([]entity.DeviceRes, 0) var total int64 = 0 offset := pageSize * (page - 1) @@ -114,15 +123,17 @@ func (m *deviceModelImpl) FindListPage(page, pageSize int, data entity.Device) ( db = db.Where("parent_id = ?", data.ParentId) } // 组织数据访问权限 - model.OrgAuthSet(db, data.RoleId, data.Owner) - - err := db.Count(&total).Error - err = db.Order("create_time").Preload("Product").Preload("DeviceGroup").Limit(pageSize).Offset(offset).Find(&list).Error - biz.ErrIsNil(err, "查询设备分页列表失败") - return &list, total + if err := model.OrgAuthSet(db, data.RoleId, data.Owner); err != nil { + return &list, total, err + } + if err := db.Count(&total).Error; err != nil { + return &list, total, err + } + err := db.Order("create_time").Preload("Product").Preload("DeviceGroup").Limit(pageSize).Offset(offset).Find(&list).Error + return &list, total, err } -func (m *deviceModelImpl) FindList(data entity.Device) *[]entity.DeviceRes { +func (m *deviceModelImpl) FindList(data entity.Device) (*[]entity.DeviceRes, error) { list := make([]entity.DeviceRes, 0) db := global.Db.Table(m.table) // 此处填写 where参数判断 @@ -150,30 +161,38 @@ func (m *deviceModelImpl) FindList(data entity.Device) *[]entity.DeviceRes { if data.ParentId != "" { db = db.Where("parent_id = ?", data.ParentId) } - model.OrgAuthSet(db, data.RoleId, data.Owner) + if err := model.OrgAuthSet(db, data.RoleId, data.Owner); err != nil { + return nil, err + } db.Preload("Product").Preload("DeviceGroup") - biz.ErrIsNil(db.Order("create_time").Find(&list).Error, "查询设备列表失败") - return &list + err := db.Order("create_time").Find(&list).Error + return &list, err } -func (m *deviceModelImpl) Update(data entity.Device) *entity.Device { +func (m *deviceModelImpl) Update(data entity.Device) (*entity.Device, error) { token := GetDeviceToken(&data) if data.DeviceType == global.GATEWAYS { err := cache.SetDeviceEtoken(data.Name, token.GetMarshal(), time.Hour*24*365) - biz.ErrIsNil(err, "设备更改缓存失败") + if err != nil { + return nil, errors.New("设备更改缓存失败") + } } else { err := cache.SetDeviceEtoken(data.Token, token.GetMarshal(), time.Hour*24*365) - biz.ErrIsNil(err, "设备更改缓存失败") + if err != nil { + return nil, errors.New("设备更改缓存失败") + } } - biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改设备失败") - return &data + err := global.Db.Table(m.table).Updates(&data).Error + return &data, err } func (m *deviceModelImpl) UpdateStatus(id, linkStatus string) error { return global.Db.Table(m.table).Where("id", id).Update("link_status", linkStatus).Update("last_time", time.Now()).Error } -func (m *deviceModelImpl) Delete(ids []string) { - biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.Device{}, "id in (?)", ids).Error, "删除设备失败") +func (m *deviceModelImpl) Delete(ids []string) error { + if err := global.Db.Table(m.table).Delete(&entity.Device{}, "id in (?)", ids).Error; err != nil { + return err + } for _, id := range ids { device, err := m.FindOne(id) if err != nil { @@ -181,7 +200,6 @@ func (m *deviceModelImpl) Delete(ids []string) { } // 删除表 err = deleteDeviceTable(device.Name) - global.Log.Error("设备时序表删除失败", err) // 删除所有缓存 if device.DeviceType == global.GATEWAYS { cache.DelDeviceEtoken(device.Name) @@ -189,6 +207,7 @@ func (m *deviceModelImpl) Delete(ids []string) { cache.DelDeviceEtoken(device.Token) } } + return nil } // 创建Tdengine时序数据 @@ -235,27 +254,24 @@ func GetDeviceToken(data *entity.Device) *model.DeviceAuth { } // 获取设备数量统计 -func (m *deviceModelImpl) FindDeviceCount() (result entity.DeviceCount) { +func (m *deviceModelImpl) FindDeviceCount() (result entity.DeviceCount, err error) { sql := `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM devices WHERE DATE(create_time) = CURDATE()) AS today FROM devices` if global.Conf.Server.DbType == "postgresql" { sql = `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM devices WHERE DATE(create_time) = current_date) AS today FROM devices` } - err := global.Db.Raw(sql).Scan(&result).Error - biz.ErrIsNil(err, "获取设备统计总数失败") - return result + err = global.Db.Raw(sql).Scan(&result).Error + return } // 获取设备类型数量统计 -func (m *deviceModelImpl) FindDeviceCountGroupByLinkStatus() (count []entity.DeviceCountLinkStatus) { +func (m *deviceModelImpl) FindDeviceCountGroupByLinkStatus() (count []entity.DeviceCountLinkStatus, err error) { sql := `SELECT link_status, COUNT(*) AS total FROM devices GROUP BY link_status` - err := global.Db.Raw(sql, m.table).Scan(&count).Error - biz.ErrIsNil(err, "获取通过设备在线状态的设备统计总数失败") + err = global.Db.Raw(sql, m.table).Scan(&count).Error return } -func (m *deviceModelImpl) FindDeviceCountGroupByType() (count []entity.DeviceCountType) { +func (m *deviceModelImpl) FindDeviceCountGroupByType() (count []entity.DeviceCountType, err error) { sql := `SELECT device_type, COUNT(*) AS total FROM devices GROUP BY device_type` - err := global.Db.Raw(sql, m.table).Scan(&count).Error - biz.ErrIsNil(err, "获取通过设备类型的设备统计总数失败") + err = global.Db.Raw(sql, m.table).Scan(&count).Error return } diff --git a/apps/device/services/device_alarm.go b/apps/device/services/device_alarm.go index 9ac2e51..4850b16 100644 --- a/apps/device/services/device_alarm.go +++ b/apps/device/services/device_alarm.go @@ -1,7 +1,6 @@ package services import ( - "github.com/PandaXGO/PandaKit/biz" "pandax/apps/device/entity" "pandax/pkg/global" "pandax/pkg/global/model" @@ -10,13 +9,13 @@ import ( type ( DeviceAlarmModel interface { Insert(data entity.DeviceAlarm) error - FindOne(id string) *entity.DeviceAlarm + FindOne(id string) (*entity.DeviceAlarm, error) FindOneByType(deviceId, ty, state string) (*entity.DeviceAlarm, error) - FindListPage(page, pageSize int, data entity.DeviceAlarmForm) (*[]entity.DeviceAlarm, int64) + FindListPage(page, pageSize int, data entity.DeviceAlarmForm) (*[]entity.DeviceAlarm, int64, error) Update(data entity.DeviceAlarm) error - Delete(ids []string) - FindAlarmCount() entity.DeviceCount - FindAlarmPanel() []entity.DeviceAlarmCount + Delete(ids []string) error + FindAlarmCount() (entity.DeviceCount, error) + FindAlarmPanel() ([]entity.DeviceAlarmCount, error) } alarmModelImpl struct { @@ -33,12 +32,11 @@ func (m *alarmModelImpl) Insert(data entity.DeviceAlarm) error { return err } -func (m *alarmModelImpl) FindOne(id string) *entity.DeviceAlarm { +func (m *alarmModelImpl) FindOne(id string) (*entity.DeviceAlarm, error) { resData := new(entity.DeviceAlarm) db := global.Db.Table(m.table).Where("id = ?", id) err := db.First(resData).Error - biz.ErrIsNil(err, "查询设备告警失败") - return resData + return resData, err } func (m *alarmModelImpl) FindOneByType(deviceId, ty, state string) (*entity.DeviceAlarm, error) { @@ -51,7 +49,7 @@ func (m *alarmModelImpl) FindOneByType(deviceId, ty, state string) (*entity.Devi return resData, nil } -func (m *alarmModelImpl) FindListPage(page, pageSize int, data entity.DeviceAlarmForm) (*[]entity.DeviceAlarm, int64) { +func (m *alarmModelImpl) FindListPage(page, pageSize int, data entity.DeviceAlarmForm) (*[]entity.DeviceAlarm, int64, error) { list := make([]entity.DeviceAlarm, 0) var total int64 = 0 offset := pageSize * (page - 1) @@ -78,12 +76,14 @@ func (m *alarmModelImpl) FindListPage(page, pageSize int, data entity.DeviceAlar db = db.Where("time < ?", data.EndTime) } // 组织数据访问权限 - model.OrgAuthSet(db, data.RoleId, data.Owner) - - err := db.Count(&total).Error - err = db.Order("time").Limit(pageSize).Offset(offset).Find(&list).Error - biz.ErrIsNil(err, "查询设备告警分页列表失败") - return &list, total + if err := model.OrgAuthSet(db, data.RoleId, data.Owner); err != nil { + return nil, 0, err + } + if err := db.Count(&total).Error; err != nil { + return nil, 0, err + } + err := db.Order("time").Limit(pageSize).Offset(offset).Find(&list).Error + return &list, total, err } func (m *alarmModelImpl) Update(data entity.DeviceAlarm) error { @@ -94,26 +94,24 @@ func (m *alarmModelImpl) Update(data entity.DeviceAlarm) error { return err } -func (m *alarmModelImpl) Delete(id []string) { - biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.DeviceAlarm{}, "id in (?)", id).Error, "删除设备告警失败") +func (m *alarmModelImpl) Delete(id []string) error { + return global.Db.Table(m.table).Delete(&entity.DeviceAlarm{}, "id in (?)", id).Error } // 获取告警数量统计 -func (m *alarmModelImpl) FindAlarmCount() (count entity.DeviceCount) { +func (m *alarmModelImpl) FindAlarmCount() (count entity.DeviceCount, err error) { sql := `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM device_alarms WHERE DATE(time) = CURDATE()) AS today FROM device_alarms` if global.Conf.Server.DbType == "postgresql" { sql = `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM device_alarms WHERE DATE(time) = current_date) AS today FROM device_alarms` } - err := global.Db.Raw(sql).Scan(&count).Error - biz.ErrIsNil(err, "获取告警统计总数失败") + err = global.Db.Raw(sql).Scan(&count).Error return } -func (m *alarmModelImpl) FindAlarmPanel() (count []entity.DeviceAlarmCount) { +func (m *alarmModelImpl) FindAlarmPanel() (count []entity.DeviceAlarmCount, err error) { sql := `SELECT CAST(time AS DATE) AS date, COUNT(*) AS count FROM device_alarms WHERE time >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) GROUP BY CAST(time AS DATE)` if global.Conf.Server.DbType == "postgresql" { sql = `SELECT CAST(time AS DATE) AS date, COUNT(*) AS count FROM device_alarms WHERE time >= CURRENT_DATE - INTERVAL '30 day' GROUP BY CAST(time AS DATE);` } - err := global.Db.Raw(sql).Scan(&count).Error - biz.ErrIsNil(err, "获取告警统计列表失败") + err = global.Db.Raw(sql).Scan(&count).Error return } diff --git a/apps/device/services/device_cmd_log.go b/apps/device/services/device_cmd_log.go index 025ff39..1a5b517 100644 --- a/apps/device/services/device_cmd_log.go +++ b/apps/device/services/device_cmd_log.go @@ -1,7 +1,6 @@ package services import ( - "github.com/PandaXGO/PandaKit/biz" "pandax/apps/device/entity" "pandax/pkg/global" "time" @@ -10,11 +9,11 @@ import ( type ( DeviceCmdLogModel interface { Insert(data entity.DeviceCmdLog) error - FindOne(id string) *entity.DeviceCmdLog - FindListPage(page, pageSize int, data entity.DeviceCmdLog) (*[]entity.DeviceCmdLog, int64) + FindOne(id string) (*entity.DeviceCmdLog, error) + FindListPage(page, pageSize int, data entity.DeviceCmdLog) (*[]entity.DeviceCmdLog, int64, error) Update(data entity.DeviceCmdLog) error UpdateResp(id, responseContent, responseTime string) error - Delete(ids []string) + Delete(ids []string) error } cmdLogModelImpl struct { @@ -31,15 +30,14 @@ func (m *cmdLogModelImpl) Insert(data entity.DeviceCmdLog) error { return err } -func (m *cmdLogModelImpl) FindOne(id string) *entity.DeviceCmdLog { +func (m *cmdLogModelImpl) FindOne(id string) (*entity.DeviceCmdLog, error) { resData := new(entity.DeviceCmdLog) db := global.Db.Table(m.table).Where("id = ?", id) err := db.First(resData).Error - biz.ErrIsNil(err, "查询设备指令下发失败") - return resData + return resData, err } -func (m *cmdLogModelImpl) FindListPage(page, pageSize int, data entity.DeviceCmdLog) (*[]entity.DeviceCmdLog, int64) { +func (m *cmdLogModelImpl) FindListPage(page, pageSize int, data entity.DeviceCmdLog) (*[]entity.DeviceCmdLog, int64, error) { list := make([]entity.DeviceCmdLog, 0) var total int64 = 0 offset := pageSize * (page - 1) @@ -56,10 +54,11 @@ func (m *cmdLogModelImpl) FindListPage(page, pageSize int, data entity.DeviceCmd if data.State != "" { db = db.Where("state = ?", data.State) } - err := db.Count(&total).Error - err = db.Order("request_time").Limit(pageSize).Offset(offset).Find(&list).Error - biz.ErrIsNil(err, "查询设备指令下发分页列表失败") - return &list, total + if err := db.Count(&total).Error; err != nil { + return nil, 0, err + } + err := db.Order("request_time").Limit(pageSize).Offset(offset).Find(&list).Error + return &list, total, err } func (m *cmdLogModelImpl) Update(data entity.DeviceCmdLog) error { @@ -78,6 +77,6 @@ func (m *cmdLogModelImpl) UpdateResp(id, responseContent, state string) error { return err } -func (m *cmdLogModelImpl) Delete(id []string) { - biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.DeviceCmdLog{}, "id in (?)", id).Error, "删除设备指令下发失败") +func (m *cmdLogModelImpl) Delete(id []string) error { + return global.Db.Table(m.table).Delete(&entity.DeviceCmdLog{}, "id in (?)", id).Error } diff --git a/apps/device/services/device_group.go b/apps/device/services/device_group.go index 663ada5..ba6a6fe 100644 --- a/apps/device/services/device_group.go +++ b/apps/device/services/device_group.go @@ -2,7 +2,6 @@ package services import ( "errors" - "github.com/PandaXGO/PandaKit/biz" "pandax/apps/device/entity" "pandax/pkg/global" "pandax/pkg/global/model" @@ -10,14 +9,14 @@ import ( type ( DeviceGroupModel interface { - Insert(data entity.DeviceGroup) *entity.DeviceGroup - FindOne(id string) *entity.DeviceGroup - FindListPage(page, pageSize int, data entity.DeviceGroup) (*[]entity.DeviceGroup, int64) - FindList(data entity.DeviceGroup) *[]entity.DeviceGroup - Update(data entity.DeviceGroup) *entity.DeviceGroup - Delete(ids []string) - SelectDeviceGroup(data entity.DeviceGroup) []entity.DeviceGroup - SelectDeviceGroupLabel(data entity.DeviceGroup) []entity.DeviceGroupLabel + Insert(data entity.DeviceGroup) (*entity.DeviceGroup, error) + FindOne(id string) (*entity.DeviceGroup, error) + FindListPage(page, pageSize int, data entity.DeviceGroup) (*[]entity.DeviceGroup, int64, error) + FindList(data entity.DeviceGroup) (*[]entity.DeviceGroup, error) + Update(data entity.DeviceGroup) (*entity.DeviceGroup, error) + Delete(ids []string) error + SelectDeviceGroup(data entity.DeviceGroup) ([]entity.DeviceGroup, error) + SelectDeviceGroupLabel(data entity.DeviceGroup) ([]entity.DeviceGroupLabel, error) } deviceGroupModelImpl struct { @@ -29,31 +28,30 @@ var DeviceGroupModelDao DeviceGroupModel = &deviceGroupModelImpl{ table: `device_groups`, } -func (m *deviceGroupModelImpl) Insert(data entity.DeviceGroup) *entity.DeviceGroup { - err := global.Db.Table(m.table).Create(&data).Error - biz.ErrIsNil(err, "添加设备分组失败") - +func (m *deviceGroupModelImpl) Insert(data entity.DeviceGroup) (*entity.DeviceGroup, error) { + if err := global.Db.Table(m.table).Create(&data).Error; err != nil { + return nil, err + } path := "/" + data.Id if data.Pid != "0" { - vsg := m.FindOne(data.Pid) + vsg, _ := m.FindOne(data.Pid) path = vsg.Path + path } else { path = "/0" + path } data.Path = path - biz.ErrIsNil(global.Db.Table(m.table).Model(&data).Updates(&data).Error, "修改设备分组信息失败") - return &data + err := global.Db.Table(m.table).Model(&data).Updates(&data).Error + return &data, err } -func (m *deviceGroupModelImpl) FindOne(id string) *entity.DeviceGroup { +func (m *deviceGroupModelImpl) FindOne(id string) (*entity.DeviceGroup, error) { resData := new(entity.DeviceGroup) db := global.Db.Table(m.table).Where("id = ?", id) err := db.First(resData).Error - biz.ErrIsNil(err, "查询设备分组失败") - return resData + return resData, err } -func (m *deviceGroupModelImpl) FindListPage(page, pageSize int, data entity.DeviceGroup) (*[]entity.DeviceGroup, int64) { +func (m *deviceGroupModelImpl) FindListPage(page, pageSize int, data entity.DeviceGroup) (*[]entity.DeviceGroup, int64, error) { list := make([]entity.DeviceGroup, 0) var total int64 = 0 offset := pageSize * (page - 1) @@ -69,15 +67,17 @@ func (m *deviceGroupModelImpl) FindListPage(page, pageSize int, data entity.Devi db = db.Where("status = ?", data.Status) } // 组织数据访问权限 - model.OrgAuthSet(db, data.RoleId, data.Owner) - - err := db.Count(&total).Error - err = db.Order("sort").Limit(pageSize).Offset(offset).Find(&list).Error - biz.ErrIsNil(err, "查询设备分组分页列表失败") - return &list, total + if err := model.OrgAuthSet(db, data.RoleId, data.Owner); err != nil { + return nil, 0, err + } + if err := db.Count(&total).Error; err != nil { + return nil, 0, err + } + err := db.Order("sort").Limit(pageSize).Offset(offset).Find(&list).Error + return &list, total, err } -func (m *deviceGroupModelImpl) FindList(data entity.DeviceGroup) *[]entity.DeviceGroup { +func (m *deviceGroupModelImpl) FindList(data entity.DeviceGroup) (*[]entity.DeviceGroup, error) { list := make([]entity.DeviceGroup, 0) db := global.Db.Table(m.table) // 此处填写 where参数判断 @@ -91,17 +91,21 @@ func (m *deviceGroupModelImpl) FindList(data entity.DeviceGroup) *[]entity.Devic db = db.Where("status = ?", data.Status) } // 组织数据访问权限 - model.OrgAuthSet(db, data.RoleId, data.Owner) - biz.ErrIsNil(db.Order("sort").Find(&list).Error, "查询设备分组列表失败") - return &list + if err := model.OrgAuthSet(db, data.RoleId, data.Owner); err != nil { + return nil, err + } + err := db.Order("sort").Find(&list).Error + return &list, err } -func (m *deviceGroupModelImpl) Update(data entity.DeviceGroup) *entity.DeviceGroup { - one := m.FindOne(data.Id) - +func (m *deviceGroupModelImpl) Update(data entity.DeviceGroup) (*entity.DeviceGroup, error) { + one, err := m.FindOne(data.Id) + if err != nil { + return nil, err + } path := "/" + data.Id if data.Pid != "0" { - vsg := m.FindOne(data.Pid) + vsg, _ := m.FindOne(data.Pid) path = vsg.Path + path } else { path = "/0" + path @@ -109,19 +113,18 @@ func (m *deviceGroupModelImpl) Update(data entity.DeviceGroup) *entity.DeviceGro data.Path = path if data.Path != "" && data.Path != one.Path { - biz.ErrIsNil(errors.New("上级分组不允许修改!"), "上级分组不允许修改") + return nil, errors.New("上级分组不允许修改!") } - - biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改设备分组失败") - return &data + err = global.Db.Table(m.table).Updates(&data).Error + return &data, err } -func (m *deviceGroupModelImpl) Delete(s []string) { - biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.DeviceGroup{}, "id in (?)", s).Error, "删除设备分组失败") +func (m *deviceGroupModelImpl) Delete(s []string) error { + return global.Db.Table(m.table).Delete(&entity.DeviceGroup{}, "id in (?)", s).Error } -func (m *deviceGroupModelImpl) SelectDeviceGroup(data entity.DeviceGroup) []entity.DeviceGroup { - list := m.FindList(data) +func (m *deviceGroupModelImpl) SelectDeviceGroup(data entity.DeviceGroup) ([]entity.DeviceGroup, error) { + list, _ := m.FindList(data) sd := make([]entity.DeviceGroup, 0) li := *list for i := 0; i < len(li); i++ { @@ -131,12 +134,11 @@ func (m *deviceGroupModelImpl) SelectDeviceGroup(data entity.DeviceGroup) []enti info := DiGuiDeviceGroup(list, li[i]) sd = append(sd, info) } - return sd + return sd, nil } -func (m *deviceGroupModelImpl) SelectDeviceGroupLabel(data entity.DeviceGroup) []entity.DeviceGroupLabel { - organizationlist := m.FindList(data) - +func (m *deviceGroupModelImpl) SelectDeviceGroupLabel(data entity.DeviceGroup) ([]entity.DeviceGroupLabel, error) { + organizationlist, _ := m.FindList(data) dl := make([]entity.DeviceGroupLabel, 0) organizationl := *organizationlist for i := 0; i < len(organizationl); i++ { @@ -150,7 +152,7 @@ func (m *deviceGroupModelImpl) SelectDeviceGroupLabel(data entity.DeviceGroup) [ dl = append(dl, organizationsInfo) } - return dl + return dl, nil } func DiGuiDeviceGroup(sglist *[]entity.DeviceGroup, menu entity.DeviceGroup) entity.DeviceGroup { diff --git a/apps/device/services/product.go b/apps/device/services/product.go index 7afd2a6..c1d10d5 100644 --- a/apps/device/services/product.go +++ b/apps/device/services/product.go @@ -1,7 +1,6 @@ package services import ( - "github.com/PandaXGO/PandaKit/biz" "pandax/apps/device/entity" "pandax/pkg/cache" "pandax/pkg/global" @@ -9,13 +8,13 @@ import ( type ( ProductModel interface { - Insert(data entity.Product) *entity.Product - FindOne(id string) *entity.ProductRes - FindListPage(page, pageSize int, data entity.Product) (*[]entity.ProductRes, int64) - FindList(data entity.Product) *[]entity.ProductRes - Update(data entity.Product) *entity.Product - Delete(ids []string) - FindProductCount() entity.DeviceCount + Insert(data entity.Product) (*entity.Product, error) + FindOne(id string) (*entity.ProductRes, error) + FindListPage(page, pageSize int, data entity.Product) (*[]entity.ProductRes, int64, error) + FindList(data entity.Product) (*[]entity.ProductRes, error) + Update(data entity.Product) (*entity.Product, error) + Delete(ids []string) error + FindProductCount() (entity.DeviceCount, error) } productModelImpl struct { @@ -27,30 +26,30 @@ var ProductModelDao ProductModel = &productModelImpl{ table: `products`, } -func (m *productModelImpl) Insert(data entity.Product) *entity.Product { +func (m *productModelImpl) Insert(data entity.Product) (*entity.Product, error) { tx := global.Db.Begin() // 添加产品及规则链到redis中 - err := tx.Table(m.table).Create(&data).Error - biz.ErrIsNil(err, "添加产品失败") + if err := tx.Table(m.table).Create(&data).Error; err != nil { + return nil, err + } // 创建taos数据库超级表 - err = createDeviceStable(data.Id) + err := createDeviceStable(data.Id) if err != nil { tx.Rollback() - biz.ErrIsNil(err, "添加设备失败,超级表创建失败") + return nil, err } tx.Commit() - return &data + return &data, nil } -func (m *productModelImpl) FindOne(id string) *entity.ProductRes { +func (m *productModelImpl) FindOne(id string) (*entity.ProductRes, error) { resData := new(entity.ProductRes) db := global.Db.Table(m.table).Where("id = ?", id) err := db.Preload("ProductCategory").First(resData).Error - biz.ErrIsNil(err, "查询产品失败") - return resData + return resData, err } -func (m *productModelImpl) FindListPage(page, pageSize int, data entity.Product) (*[]entity.ProductRes, int64) { +func (m *productModelImpl) FindListPage(page, pageSize int, data entity.Product) (*[]entity.ProductRes, int64, error) { list := make([]entity.ProductRes, 0) var total int64 = 0 offset := pageSize * (page - 1) @@ -75,13 +74,15 @@ func (m *productModelImpl) FindListPage(page, pageSize int, data entity.Product) if data.RuleChainId != "" { db = db.Where("rule_chain_id = ?", data.RuleChainId) } - err := db.Count(&total).Error - err = db.Order("create_time").Preload("ProductCategory").Limit(pageSize).Offset(offset).Find(&list).Error - biz.ErrIsNil(err, "查询产品分页列表失败") - return &list, total + + if err := db.Count(&total).Error; err != nil { + return &list, total, err + } + err := db.Order("create_time").Preload("ProductCategory").Limit(pageSize).Offset(offset).Find(&list).Error + return &list, total, err } -func (m *productModelImpl) FindList(data entity.Product) *[]entity.ProductRes { +func (m *productModelImpl) FindList(data entity.Product) (*[]entity.ProductRes, error) { list := make([]entity.ProductRes, 0) db := global.Db.Table(m.table) // 此处填写 where参数判断 @@ -103,26 +104,27 @@ func (m *productModelImpl) FindList(data entity.Product) *[]entity.ProductRes { if data.RuleChainId != "" { db = db.Where("rule_chain_id = ?", data.RuleChainId) } - biz.ErrIsNil(db.Order("create_time").Preload("ProductCategory").Find(&list).Error, "查询产品列表失败") - return &list + err := db.Order("create_time").Preload("ProductCategory").Find(&list).Error + return &list, err } -func (m *productModelImpl) Update(data entity.Product) *entity.Product { +func (m *productModelImpl) Update(data entity.Product) (*entity.Product, error) { // go的一些默认值 int 0 bool false 保存失败需要先转成map err := global.Db.Table(m.table).Where("id = ?", data.Id).Updates(data).Error - biz.ErrIsNil(err, "修改产品失败") - return &data + return &data, err } -func (m *productModelImpl) Delete(ids []string) { - biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.Product{}, "id in (?)", ids).Error, "删除产品失败") +func (m *productModelImpl) Delete(ids []string) error { + if err := global.Db.Table(m.table).Delete(&entity.Product{}, "id in (?)", ids).Error; err != nil { + return err + } for _, id := range ids { // 删除超级表 - err := deleteDeviceStable(id) - global.Log.Error("时序数据库超级表删除失败", err) + deleteDeviceStable(id) // 删除所有缓存 cache.DelProductRule(id) } + return nil } func createDeviceStable(productId string) error { @@ -150,12 +152,11 @@ func deleteDeviceStable(productId string) error { } // 获取产品数量统计 -func (m *productModelImpl) FindProductCount() (count entity.DeviceCount) { +func (m *productModelImpl) FindProductCount() (count entity.DeviceCount, err error) { sql := `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM products WHERE DATE(create_time) = CURDATE()) AS today FROM products` if global.Conf.Server.DbType == "postgresql" { sql = `SELECT COUNT(*) AS total, (SELECT COUNT(*) FROM products WHERE DATE(create_time) = current_date) AS today FROM products` } - err := global.Db.Raw(sql).Scan(&count).Error - biz.ErrIsNil(err, "获取产品统计总数失败") + err = global.Db.Raw(sql).Scan(&count).Error return } diff --git a/apps/device/services/product_category.go b/apps/device/services/product_category.go index 1812574..1e6ef9f 100644 --- a/apps/device/services/product_category.go +++ b/apps/device/services/product_category.go @@ -9,12 +9,12 @@ import ( type ( ProductCategoryModel interface { - Insert(data entity.ProductCategory) *entity.ProductCategory - FindOne(id string) *entity.ProductCategory - FindListPage(page, pageSize int, data entity.ProductCategory) (*[]entity.ProductCategory, int64) - FindList(data entity.ProductCategory) *[]entity.ProductCategory - Update(data entity.ProductCategory) *entity.ProductCategory - Delete(ids []string) + Insert(data entity.ProductCategory) (*entity.ProductCategory, error) + FindOne(id string) (*entity.ProductCategory, error) + FindListPage(page, pageSize int, data entity.ProductCategory) (*[]entity.ProductCategory, int64, error) + FindList(data entity.ProductCategory) (*[]entity.ProductCategory, error) + Update(data entity.ProductCategory) (*entity.ProductCategory, error) + Delete(ids []string) error SelectProductCategory(data entity.ProductCategory) []entity.ProductCategory SelectProductCategoryLabel(data entity.ProductCategory) []entity.ProductCategoryLabel } @@ -28,31 +28,30 @@ var ProductCategoryModelDao ProductCategoryModel = &productCategoryModelImpl{ table: `product_categories`, } -func (m *productCategoryModelImpl) Insert(data entity.ProductCategory) *entity.ProductCategory { - err := global.Db.Table(m.table).Create(&data).Error - biz.ErrIsNil(err, "添加产品分组失败") - +func (m *productCategoryModelImpl) Insert(data entity.ProductCategory) (*entity.ProductCategory, error) { + if err := global.Db.Table(m.table).Create(&data).Error; err != nil { + return nil, err + } path := "/" + data.Id if data.Pid != "0" { - vsg := m.FindOne(data.Pid) + vsg, _ := m.FindOne(data.Pid) path = vsg.Path + path } else { path = "/0" + path } data.Path = path - biz.ErrIsNil(global.Db.Table(m.table).Model(&data).Updates(&data).Error, "修改产品分组信息失败") - return &data + err := global.Db.Table(m.table).Model(&data).Updates(&data).Error + return &data, err } -func (m *productCategoryModelImpl) FindOne(id string) *entity.ProductCategory { +func (m *productCategoryModelImpl) FindOne(id string) (*entity.ProductCategory, error) { resData := new(entity.ProductCategory) db := global.Db.Table(m.table).Where("id = ?", id) err := db.First(resData).Error - biz.ErrIsNil(err, "查询产品分组失败") - return resData + return resData, err } -func (m *productCategoryModelImpl) FindListPage(page, pageSize int, data entity.ProductCategory) (*[]entity.ProductCategory, int64) { +func (m *productCategoryModelImpl) FindListPage(page, pageSize int, data entity.ProductCategory) (*[]entity.ProductCategory, int64, error) { list := make([]entity.ProductCategory, 0) var total int64 = 0 offset := pageSize * (page - 1) @@ -67,13 +66,14 @@ func (m *productCategoryModelImpl) FindListPage(page, pageSize int, data entity. if data.Status != "" { db = db.Where("status = ?", data.Status) } - err := db.Count(&total).Error - err = db.Order("sort").Limit(pageSize).Offset(offset).Find(&list).Error - biz.ErrIsNil(err, "查询产品分组分页列表失败") - return &list, total + if err := db.Count(&total).Error; err != nil { + return &list, total, err + } + err := db.Order("sort").Limit(pageSize).Offset(offset).Find(&list).Error + return &list, total, err } -func (m *productCategoryModelImpl) FindList(data entity.ProductCategory) *[]entity.ProductCategory { +func (m *productCategoryModelImpl) FindList(data entity.ProductCategory) (*[]entity.ProductCategory, error) { list := make([]entity.ProductCategory, 0) db := global.Db.Table(m.table) // 此处填写 where参数判断 @@ -86,16 +86,16 @@ func (m *productCategoryModelImpl) FindList(data entity.ProductCategory) *[]enti if data.Status != "" { db = db.Where("status = ?", data.Status) } - biz.ErrIsNil(db.Order("sort").Find(&list).Error, "查询产品分组列表失败") - return &list + err := db.Order("sort").Find(&list).Error + return &list, err } -func (m *productCategoryModelImpl) Update(data entity.ProductCategory) *entity.ProductCategory { - one := m.FindOne(data.Id) +func (m *productCategoryModelImpl) Update(data entity.ProductCategory) (*entity.ProductCategory, error) { + one, _ := m.FindOne(data.Id) path := "/" + data.Id if data.Pid != "0" { - vsg := m.FindOne(data.Pid) + vsg, _ := m.FindOne(data.Pid) path = vsg.Path + path } else { path = "/0" + path @@ -104,18 +104,18 @@ func (m *productCategoryModelImpl) Update(data entity.ProductCategory) *entity.P if data.Path != "" && data.Path != one.Path { biz.ErrIsNil(errors.New("上级分组不允许修改!"), "上级分组不允许修改") + return nil, errors.New("上级分组不允许修改!") } - - biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改产品分组失败") - return &data + err := global.Db.Table(m.table).Updates(&data).Error + return &data, err } -func (m *productCategoryModelImpl) Delete(s []string) { - biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.ProductCategory{}, "id in (?)", s).Error, "删除产品分组失败") +func (m *productCategoryModelImpl) Delete(s []string) error { + return global.Db.Table(m.table).Delete(&entity.ProductCategory{}, "id in (?)", s).Error } func (m *productCategoryModelImpl) SelectProductCategory(data entity.ProductCategory) []entity.ProductCategory { - list := m.FindList(data) + list, _ := m.FindList(data) sd := make([]entity.ProductCategory, 0) li := *list for i := 0; i < len(li); i++ { @@ -129,7 +129,7 @@ func (m *productCategoryModelImpl) SelectProductCategory(data entity.ProductCate } func (m *productCategoryModelImpl) SelectProductCategoryById(data entity.ProductCategory, id string) []string { - list := m.FindList(data) + list, _ := m.FindList(data) sd := make([]string, 0) li := *list for i := 0; i < len(li); i++ { @@ -142,7 +142,7 @@ func (m *productCategoryModelImpl) SelectProductCategoryById(data entity.Product } func (m *productCategoryModelImpl) SelectProductCategoryLabel(data entity.ProductCategory) []entity.ProductCategoryLabel { - list := m.FindList(data) + list, _ := m.FindList(data) dl := make([]entity.ProductCategoryLabel, 0) organizationl := *list diff --git a/apps/device/services/product_ota.go b/apps/device/services/product_ota.go index 9519a7e..da0bff4 100644 --- a/apps/device/services/product_ota.go +++ b/apps/device/services/product_ota.go @@ -1,19 +1,18 @@ package services import ( - "github.com/PandaXGO/PandaKit/biz" "pandax/apps/device/entity" "pandax/pkg/global" ) type ( ProductOtaModel interface { - Insert(data entity.ProductOta) *entity.ProductOta - FindOne(id string) *entity.ProductOta - FindListPage(page, pageSize int, data entity.ProductOta) (*[]entity.ProductOta, int64) - FindList(data entity.ProductOta) *[]entity.ProductOta - Update(data entity.ProductOta) *entity.ProductOta - Delete(ids []string) + Insert(data entity.ProductOta) (*entity.ProductOta, error) + FindOne(id string) (*entity.ProductOta, error) + FindListPage(page, pageSize int, data entity.ProductOta) (*[]entity.ProductOta, int64, error) + FindList(data entity.ProductOta) (*[]entity.ProductOta, error) + Update(data entity.ProductOta) (*entity.ProductOta, error) + Delete(ids []string) error } otaModelImpl struct { @@ -25,31 +24,36 @@ var ProductOtaModelDao ProductOtaModel = &otaModelImpl{ table: `product_ota`, } -func (m *otaModelImpl) Insert(data entity.ProductOta) *entity.ProductOta { - m.checkLatest(data) - +func (m *otaModelImpl) Insert(data entity.ProductOta) (*entity.ProductOta, error) { + if err := m.checkLatest(data); err != nil { + return nil, err + } err := global.Db.Table(m.table).Create(&data).Error - biz.ErrIsNil(err, "添加产品固件失败") - return &data + return &data, err } -func (m *otaModelImpl) checkLatest(data entity.ProductOta) { +func (m *otaModelImpl) checkLatest(data entity.ProductOta) error { // 将原来的最新版更改为旧版本 if data.IsLatest == true { - latest := m.getLatest() - m.updateLatest(latest.Id, false) + latest, err := m.getLatest() + if err != nil { + return err + } + if err := m.updateLatest(latest.Id, false); err != nil { + return err + } } + return nil } -func (m *otaModelImpl) FindOne(id string) *entity.ProductOta { +func (m *otaModelImpl) FindOne(id string) (*entity.ProductOta, error) { resData := new(entity.ProductOta) db := global.Db.Table(m.table).Where("id = ?", id) err := db.First(resData).Error - biz.ErrIsNil(err, "查询产品固件失败") - return resData + return resData, err } -func (m *otaModelImpl) FindListPage(page, pageSize int, data entity.ProductOta) (*[]entity.ProductOta, int64) { +func (m *otaModelImpl) FindListPage(page, pageSize int, data entity.ProductOta) (*[]entity.ProductOta, int64, error) { list := make([]entity.ProductOta, 0) var total int64 = 0 offset := pageSize * (page - 1) @@ -73,13 +77,14 @@ func (m *otaModelImpl) FindListPage(page, pageSize int, data entity.ProductOta) if data.Version != "" { db = db.Where("version = ?", data.Version) } - err := db.Count(&total).Error - err = db.Order("create_time").Limit(pageSize).Offset(offset).Find(&list).Error - biz.ErrIsNil(err, "查询产品固件分页列表失败") - return &list, total + if err := db.Count(&total).Error; err != nil { + return &list, total, err + } + err := db.Order("create_time").Limit(pageSize).Offset(offset).Find(&list).Error + return &list, total, err } -func (m *otaModelImpl) FindList(data entity.ProductOta) *[]entity.ProductOta { +func (m *otaModelImpl) FindList(data entity.ProductOta) (*[]entity.ProductOta, error) { list := make([]entity.ProductOta, 0) db := global.Db.Table(m.table) // 此处填写 where参数判断 @@ -98,28 +103,28 @@ func (m *otaModelImpl) FindList(data entity.ProductOta) *[]entity.ProductOta { if data.Version != "" { db = db.Where("version = ?", data.Version) } - biz.ErrIsNil(db.Order("create_time").Find(&list).Error, "查询产品固件列表失败") - return &list + err := db.Order("create_time").Find(&list).Error + return &list, err } -func (m *otaModelImpl) getLatest() *entity.ProductOta { +func (m *otaModelImpl) getLatest() (*entity.ProductOta, error) { resData := new(entity.ProductOta) db := global.Db.Table(m.table).Where("is_latest = ?", true) err := db.First(resData).Error - biz.ErrIsNil(err, "查询产品固件失败") - return resData + return resData, err } -func (m *otaModelImpl) Update(data entity.ProductOta) *entity.ProductOta { - m.checkLatest(data) - biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改产品固件失败") - return &data +func (m *otaModelImpl) Update(data entity.ProductOta) (*entity.ProductOta, error) { + if err := m.checkLatest(data); err != nil { + return nil, err + } + err := global.Db.Table(m.table).Updates(&data).Error + return &data, err } -func (m *otaModelImpl) updateLatest(id string, IsLatest bool) { - err := global.Db.Table(m.table).Where("id = ?", id).Update("is_latest", IsLatest).Error - global.Log.Error("更新失败", err) +func (m *otaModelImpl) updateLatest(id string, IsLatest bool) error { + return global.Db.Table(m.table).Where("id = ?", id).Update("is_latest", IsLatest).Error } -func (m *otaModelImpl) Delete(ids []string) { - biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.ProductOta{}, "id in (?)", ids).Error, "删除产品固件失败") +func (m *otaModelImpl) Delete(ids []string) error { + return global.Db.Table(m.table).Delete(&entity.ProductOta{}, "id in (?)", ids).Error } diff --git a/apps/device/services/product_template.go b/apps/device/services/product_template.go index 50fbb71..06d3a2f 100644 --- a/apps/device/services/product_template.go +++ b/apps/device/services/product_template.go @@ -1,7 +1,6 @@ package services import ( - "github.com/PandaXGO/PandaKit/biz" "pandax/apps/device/entity" "pandax/pkg/global" "strings" @@ -9,13 +8,13 @@ import ( type ( ProductTemplateModel interface { - Insert(data entity.ProductTemplate) *entity.ProductTemplate - FindOne(id string) *entity.ProductTemplate - FindListPage(page, pageSize int, data entity.ProductTemplate) (*[]entity.ProductTemplate, int64) - FindListAttrs(data entity.ProductTemplate) *[]entity.ProductTemplate - FindList(data entity.ProductTemplate) *[]entity.ProductTemplate - Update(data entity.ProductTemplate) *entity.ProductTemplate - Delete(ids []string) + Insert(data entity.ProductTemplate) (*entity.ProductTemplate, error) + FindOne(id string) (*entity.ProductTemplate, error) + FindListPage(page, pageSize int, data entity.ProductTemplate) (*[]entity.ProductTemplate, int64, error) + FindListAttrs(data entity.ProductTemplate) (*[]entity.ProductTemplate, error) + FindList(data entity.ProductTemplate) (*[]entity.ProductTemplate, error) + Update(data entity.ProductTemplate) (*entity.ProductTemplate, error) + Delete(ids []string) error } templateModelImpl struct { @@ -27,21 +26,19 @@ var ProductTemplateModelDao ProductTemplateModel = &templateModelImpl{ table: `product_templates`, } -func (m *templateModelImpl) Insert(data entity.ProductTemplate) *entity.ProductTemplate { +func (m *templateModelImpl) Insert(data entity.ProductTemplate) (*entity.ProductTemplate, error) { err := global.Db.Table(m.table).Create(&data).Error - biz.ErrIsNil(err, "添加产品模型失败") - return &data + return &data, err } -func (m *templateModelImpl) FindOne(id string) *entity.ProductTemplate { +func (m *templateModelImpl) FindOne(id string) (*entity.ProductTemplate, error) { resData := new(entity.ProductTemplate) db := global.Db.Table(m.table).Where("id = ?", id) err := db.First(resData).Error - biz.ErrIsNil(err, "查询产品模型失败") - return resData + return resData, err } -func (m *templateModelImpl) FindListPage(page, pageSize int, data entity.ProductTemplate) (*[]entity.ProductTemplate, int64) { +func (m *templateModelImpl) FindListPage(page, pageSize int, data entity.ProductTemplate) (*[]entity.ProductTemplate, int64, error) { list := make([]entity.ProductTemplate, 0) var total int64 = 0 offset := pageSize * (page - 1) @@ -62,13 +59,14 @@ func (m *templateModelImpl) FindListPage(page, pageSize int, data entity.Product if data.Pid != "" { db = db.Where("pid = ?", data.Pid) } - err := db.Count(&total).Error - err = db.Order("create_time").Limit(pageSize).Offset(offset).Find(&list).Error - biz.ErrIsNil(err, "查询产品模型分页列表失败") - return &list, total + if err := db.Count(&total).Error; err != nil { + return &list, total, err + } + err := db.Order("create_time").Limit(pageSize).Offset(offset).Find(&list).Error + return &list, total, err } -func (m *templateModelImpl) FindListAttrs(data entity.ProductTemplate) *[]entity.ProductTemplate { +func (m *templateModelImpl) FindListAttrs(data entity.ProductTemplate) (*[]entity.ProductTemplate, error) { list := make([]entity.ProductTemplate, 0) db := global.Db.Table(m.table) // 此处填写 where参数判断 @@ -77,11 +75,10 @@ func (m *templateModelImpl) FindListAttrs(data entity.ProductTemplate) *[]entity } db = db.Where("classify in (?)", []string{"attributes", "telemetry"}) err := db.Order("create_time").Find(&list).Error - biz.ErrIsNil(err, "查询产品模型分页列表失败") - return &list + return &list, err } -func (m *templateModelImpl) FindList(data entity.ProductTemplate) *[]entity.ProductTemplate { +func (m *templateModelImpl) FindList(data entity.ProductTemplate) (*[]entity.ProductTemplate, error) { list := make([]entity.ProductTemplate, 0) db := global.Db.Table(m.table) // 此处填写 where参数判断 @@ -100,15 +97,15 @@ func (m *templateModelImpl) FindList(data entity.ProductTemplate) *[]entity.Prod if data.Pid != "" { db = db.Where("pid = ?", data.Pid) } - biz.ErrIsNil(db.Order("create_time").Find(&list).Error, "查询产品模型列表失败") - return &list + err := db.Order("create_time").Find(&list).Error + return &list, err } -func (m *templateModelImpl) Update(data entity.ProductTemplate) *entity.ProductTemplate { - biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改产品模型失败") - return &data +func (m *templateModelImpl) Update(data entity.ProductTemplate) (*entity.ProductTemplate, error) { + err := global.Db.Table(m.table).Updates(&data).Error + return &data, err } -func (m *templateModelImpl) Delete(ids []string) { - biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.ProductTemplate{}, "id in (?)", ids).Error, "删除产品模型失败") +func (m *templateModelImpl) Delete(ids []string) error { + return global.Db.Table(m.table).Delete(&entity.ProductTemplate{}, "id in (?)", ids).Error } diff --git a/iothub/hook_message_work/hook_message_work.go b/iothub/hook_message_work/hook_message_work.go index cc69b2e..48b67db 100644 --- a/iothub/hook_message_work/hook_message_work.go +++ b/iothub/hook_message_work/hook_message_work.go @@ -108,10 +108,13 @@ func getRuleChainInstance(etoken *model.DeviceAuth) *rule_engine.RuleChainInstan key := etoken.ProductId instance, err := cache.ComputeIfAbsentProductRule(key, func(k any) (any, error) { - one := services.ProductModelDao.FindOne(k.(string)) + one, err := services.ProductModelDao.FindOne(k.(string)) + if err != nil { + return nil, err + } rule := ruleService.RuleChainModelDao.FindOne(one.RuleChainId) var lfData ruleEntity.RuleDataJson - err := tool.StringToStruct(rule.RuleDataJson, &lfData) + err = tool.StringToStruct(rule.RuleDataJson, &lfData) if err != nil { return nil, err } diff --git a/pkg/global/model/device_auth_model.go b/pkg/global/model/device_auth_model.go index c9299b2..f1cecdd 100644 --- a/pkg/global/model/device_auth_model.go +++ b/pkg/global/model/device_auth_model.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/base64" "encoding/json" - "github.com/PandaXGO/PandaKit/biz" + "errors" "github.com/google/uuid" "gorm.io/gorm" "math/rand" @@ -63,18 +63,24 @@ func (m *DeviceAuth) UnmarshalBinary(data []byte) error { return json.Unmarshal(data, m) } -func OrgAuthSet(tx *gorm.DB, roleId int64, owner string) { +func OrgAuthSet(tx *gorm.DB, roleId int64, owner string) error { if roleId == 0 { - return + return errors.New("角色Id不能为空") } role, err := services.SysRoleModelDao.FindOrganizationsByRoleId(roleId) - biz.ErrIsNil(err, "查询角色数据权限失败") + if err != nil { + return err + } if role.DataScope != entity.SELFDATASCOPE { - biz.IsTrue(len(role.Org) > 0, "该角色下未分配组织权限") + if len(role.Org) <= 0 { + return errors.New("该角色下未分配组织权限") + } tx.Where("org_id in (?)", strings.Split(role.Org, ",")) } else { tx.Where("owner = ?", owner) } + + return nil } func GenerateID() string { rand.Seed(time.Now().UnixNano()) diff --git a/pkg/initialize/event.go b/pkg/initialize/event.go index 513725a..ed05c6f 100644 --- a/pkg/initialize/event.go +++ b/pkg/initialize/event.go @@ -17,7 +17,7 @@ func InitEvents() { // 监听规则链改变 更新所有绑定改规则链的产品 global.EventEmitter.On(events.ProductChainRuleEvent, func(ruleId, codeData string) { global.Log.Infof("规则链%s变更", ruleId) - list := services.ProductModelDao.FindList(entity.Product{ + list, _ := services.ProductModelDao.FindList(entity.Product{ RuleChainId: ruleId, }) if list != nil { diff --git a/pkg/rule_engine/instance_test.go b/pkg/rule_engine/instance_test.go index d4fc16b..46f6438 100644 --- a/pkg/rule_engine/instance_test.go +++ b/pkg/rule_engine/instance_test.go @@ -13,7 +13,7 @@ func TestNewRuleChainInstance(t *testing.T) { if err != nil { t.Error(err) } - instance, errs := NewRuleChainInstance(buf) + instance, errs := NewRuleChainInstance("11", buf) if len(errs) > 0 { t.Error(errs[0]) }