mysql_mobile_machine_log.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package dao
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "strconv"
  6. "go-common/app/admin/ep/merlin/model"
  7. pkgerr "github.com/pkg/errors"
  8. )
  9. const (
  10. _mobileRightJoinLogSQL = "SELECT m.id,m.serial,ml.username,ml.operation_type,ml.operation_result,ml.ctime FROM mobile_machines AS m INNER JOIN mobile_machine_logs AS ml ON m.id = ml.machine_id"
  11. _mobileRightCountSQL = "SELECT count(m.id) FROM mobile_machines AS m INNER JOIN mobile_machine_logs AS ml ON m.id = ml.machine_id"
  12. )
  13. // InsertMobileMachineLog Insert Mobile Machine Log.
  14. func (d *Dao) InsertMobileMachineLog(mobileMachineLog *model.MobileMachineLog) (err error) {
  15. return pkgerr.WithStack(d.db.Create(mobileMachineLog).Error)
  16. }
  17. // FindMobileMachineLogs Find Mobile Machine Logs.
  18. func (d *Dao) FindMobileMachineLogs(queryRequest *model.QueryMobileMachineLogRequest) (total int64, mobileMachineLogs []*model.AboundMobileMachineLog, err error) {
  19. var (
  20. qSQL = _mobileRightJoinLogSQL
  21. cSQL = _mobileRightCountSQL
  22. rows *sql.Rows
  23. )
  24. if queryRequest.MachineID > 0 || queryRequest.OperateType != "" || queryRequest.OperateUser != "" || queryRequest.Serial != "" {
  25. var (
  26. strSQL = ""
  27. logicalWord = _where
  28. )
  29. if queryRequest.MachineID > 0 {
  30. strSQL = fmt.Sprintf("%s %s ml.machine_id = %s", strSQL, logicalWord, strconv.FormatInt(queryRequest.MachineID, 10))
  31. logicalWord = _and
  32. }
  33. if queryRequest.OperateType != "" {
  34. strSQL = fmt.Sprintf("%s %s ml.operation_type like '%s'", strSQL, logicalWord, _wildcards+queryRequest.OperateType+_wildcards)
  35. logicalWord = _and
  36. }
  37. if queryRequest.Serial != "" {
  38. strSQL = fmt.Sprintf("%s %s m.serial like '%s'", strSQL, logicalWord, _wildcards+queryRequest.Serial+_wildcards)
  39. logicalWord = _and
  40. }
  41. if queryRequest.OperateUser != "" {
  42. strSQL = fmt.Sprintf("%s %s ml.username like '%s'", strSQL, logicalWord, _wildcards+queryRequest.OperateUser+_wildcards)
  43. logicalWord = _and
  44. }
  45. qSQL = _mobileRightJoinLogSQL + " " + strSQL
  46. cSQL = _mobileRightCountSQL + " " + strSQL
  47. }
  48. cDB := d.db.Raw(cSQL)
  49. if err = pkgerr.WithStack(cDB.Count(&total).Error); err != nil {
  50. return
  51. }
  52. gDB := d.db.Raw(qSQL)
  53. if rows, err = gDB.Order("ml.ctime DESC").Offset((queryRequest.PageNum - 1) * queryRequest.PageSize).Limit(queryRequest.PageSize).Rows(); err != nil {
  54. return
  55. }
  56. defer rows.Close()
  57. for rows.Next() {
  58. ml := &model.AboundMobileMachineLog{}
  59. if err = rows.Scan(&ml.MachineID, &ml.Serial, &ml.Username, &ml.OperateType, &ml.OperateResult, &ml.OperateTime); err != nil {
  60. return
  61. }
  62. mobileMachineLogs = append(mobileMachineLogs, ml)
  63. }
  64. return
  65. }