operationHisService.go 1.8 KB

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