log.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package log
  2. import (
  3. "errors"
  4. "go-nc/configs/global"
  5. "go-nc/model"
  6. "go-nc/model/common"
  7. "github.com/gin-gonic/gin"
  8. )
  9. // 查看卡操作日志
  10. func LogCardOperation(c *gin.Context) {
  11. type Param struct {
  12. Iccid string `json:"iccid"`
  13. common.Pagination
  14. }
  15. var param Param
  16. param.Pagination = common.NewPagination()
  17. if err := c.ShouldBindJSON(&param); err != nil {
  18. c.Error(errors.New("缺少参数:" + err.Error()))
  19. return
  20. }
  21. // 总数
  22. total := int64(0)
  23. global.App.DB.Model(&model.Log_card_operation{}).Where("iccid LIKE ?", "%"+param.Iccid+"%").Count(&total)
  24. var logs []model.Log_card_operation
  25. global.App.DB.Limit(param.PageSize).
  26. Where("iccid LIKE ?", "%"+param.Iccid+"%").
  27. Offset((param.Current - 1) * param.PageSize).
  28. Find(&logs)
  29. // 查询用户信息
  30. var userMap = make(map[uint]model.Sys_user)
  31. var user []model.Sys_user
  32. if err := global.App.DB.Select("id", "name").Find(&user).Error; err != nil {
  33. c.Error(err)
  34. return
  35. }
  36. for _, v := range user {
  37. userMap[v.Id] = v
  38. }
  39. records := []interface{}{}
  40. for _, v := range logs {
  41. records = append(records, struct {
  42. model.Log_card_operation
  43. Username string `json:"username"`
  44. }{
  45. Log_card_operation: v,
  46. Username: userMap[v.UserId].Name,
  47. })
  48. }
  49. Pagination := common.BuildPagination(records, param.Current, param.PageSize, total)
  50. c.Set("res_data", Pagination)
  51. }