operationHisService.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package service
  2. import (
  3. "iot_manager_service/app/system/dao"
  4. "iot_manager_service/app/system/model"
  5. "iot_manager_service/util/common"
  6. "iot_manager_service/util/logger"
  7. "strconv"
  8. "time"
  9. )
  10. // 操作记录服务
  11. var OperationHisService = new(operationHisService)
  12. type operationHisService struct{}
  13. func (s *operationHisService) Save(userId int, tenantId string, handleType, handleModuleType, deviceType int,
  14. handleObject string, handleResult int) {
  15. operation := dao.OperationHistory{
  16. OperationType: handleType,
  17. ModuleType: handleModuleType,
  18. DeviceType: deviceType,
  19. Object: handleObject,
  20. Result: handleResult,
  21. HandleUserId: userId,
  22. HandleTime: time.Now(),
  23. TenantId: tenantId,
  24. }
  25. err := operation.Create()
  26. if err != nil {
  27. logger.Logger.Errorf("Operation save err =%v \n", err)
  28. }
  29. }
  30. func (s *operationHisService) List(tenantId string, handleContent, operationType, moduleType string, current,
  31. size int) ([]model.OperationHisDetail, int64, *common.Errors) {
  32. his := dao.OperationHistory{
  33. Object: handleContent,
  34. }
  35. if operationType != "" {
  36. operation, err := strconv.Atoi(operationType)
  37. if err != nil {
  38. return nil, 0, common.FailResponse(err.Error(), nil)
  39. }
  40. his.OperationType = operation
  41. }
  42. if moduleType != "" {
  43. module, err := strconv.Atoi(moduleType)
  44. if err != nil {
  45. return nil, 0, common.FailResponse(err.Error(), nil)
  46. }
  47. his.ModuleType = module
  48. }
  49. offset := (current - 1) * size
  50. limit := size
  51. list, counts, err := his.GetHistories(offset, limit)
  52. if err != nil {
  53. return nil, 0, common.FailResponse(err.Error(), nil)
  54. }
  55. var details []model.OperationHisDetail
  56. for _, his := range list {
  57. detail := model.OperationHisDetail{
  58. OperationHistory: his,
  59. }
  60. detail.HandleName = UserService.GetUserName(tenantId, his.HandleUserId)
  61. detail.OperationTypeName = DictService.GetControlType(tenantId, his.OperationType)
  62. detail.ModuleTypeName = DictService.GetModuleName(tenantId, his.ModuleType)
  63. details = append(details, detail)
  64. }
  65. return details, counts, nil
  66. }