operationHisService.go 1.8 KB

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