change_history.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "go-common/app/service/main/tv/internal/model"
  6. xsql "go-common/library/database/sql"
  7. "go-common/library/log"
  8. xtime "go-common/library/time"
  9. "github.com/pkg/errors"
  10. )
  11. const (
  12. _getUserChangeHistoryByID = "SELECT `id`, `mid`, `change_type`, `change_time`, `order_no`, `days`, `operator_id`, `remark`, `ctime`, `mtime` FROM `tv_user_change_history` WHERE `id`=?"
  13. _getUserChangeHistorysByMid = "SELECT `id`, `mid`, `change_type`, `change_time`, `order_no`, `days`, `operator_id`, `remark`, `ctime`, `mtime` FROM `tv_user_change_history` WHERE `mid`=? ORDER BY `ctime` DESC LIMIT ?,?"
  14. _countUserChangeHistoryByMid = "SELECT count(*) FROM `tv_user_change_history` WHERE `mid`=?"
  15. _getUserChangeHistorysByMidAndCtime = "SELECT `id`, `mid`, `change_type`, `change_time`, `order_no`, `days`, `operator_id`, `remark`, `ctime`, `mtime` FROM `tv_user_change_history` WHERE `mid`=? AND `ctime`>=? AND `ctime`<? ORDER BY `ctime` DESC LIMIT ?,?"
  16. _countUserChangeHistoryByMidAndCtime = "SELECT count(*) FROM `tv_user_change_history` WHERE `mid`=? AND `ctime`>=? AND `ctime`<?"
  17. _insertUserChangeHistory = "INSERT INTO tv_user_change_history (`mid`, `change_type`, `change_time`, `order_no`, `days`, `operator_id`, `remark`) VALUES (?,?,?,?,?,?,?)"
  18. )
  19. // UserChangeHistoryByID quires one row from tv_user_change_history.
  20. func (d *Dao) UserChangeHistoryByID(c context.Context, id int32) (uch *model.UserChangeHistory, err error) {
  21. row := d.db.QueryRow(c, _getUserChangeHistoryByID, id)
  22. uch = &model.UserChangeHistory{}
  23. err = row.Scan(&uch.ID, &uch.Mid, &uch.ChangeType, &uch.ChangeTime, &uch.OrderNo, &uch.Days, &uch.OperatorId, &uch.Remark, &uch.Ctime, &uch.Mtime)
  24. if err != nil {
  25. log.Error("rows.Scan(%s) error(%v)", _getUserChangeHistoryByID, err)
  26. err = errors.WithStack(err)
  27. return nil, err
  28. }
  29. return uch, nil
  30. }
  31. // UserChangeHistorysByMid quires rows from tv_user_change_history.
  32. func (d *Dao) UserChangeHistorysByMid(c context.Context, mid int64, pn, ps int32) (res []*model.UserChangeHistory, total int, err error) {
  33. res = make([]*model.UserChangeHistory, 0)
  34. totalRow := d.db.QueryRow(c, _countUserChangeHistoryByMid, mid)
  35. if err = totalRow.Scan(&total); err != nil {
  36. log.Error("row.ScanCount error(%v)", err)
  37. err = errors.WithStack(err)
  38. return
  39. }
  40. rows, err := d.db.Query(c, _getUserChangeHistorysByMid, mid, (pn-1)*ps, ps)
  41. if err != nil {
  42. log.Error("db.Query(%s) error(%v)", _getUserChangeHistorysByMid, err)
  43. err = errors.WithStack(err)
  44. return
  45. }
  46. defer rows.Close()
  47. for rows.Next() {
  48. uch := &model.UserChangeHistory{}
  49. if err = rows.Scan(&uch.ID, &uch.Mid, &uch.ChangeType, &uch.ChangeTime, &uch.OrderNo, &uch.Days, &uch.OperatorId, &uch.Remark, &uch.Ctime, &uch.Mtime); err != nil {
  50. log.Error("rows.Scan() error(%v)", err)
  51. err = errors.WithStack(err)
  52. return
  53. }
  54. res = append(res, uch)
  55. }
  56. return
  57. }
  58. // UserChangeHistorysByMidAndCtime quires rows from tv_user_change_history.
  59. func (d *Dao) UserChangeHistorysByMidAndCtime(c context.Context, mid int64, from, to xtime.Time, pn, ps int32) (res []*model.UserChangeHistory, total int, err error) {
  60. res = make([]*model.UserChangeHistory, 0)
  61. totalRow := d.db.QueryRow(c, _countUserChangeHistoryByMidAndCtime, mid, from, to)
  62. if err = totalRow.Scan(&total); err != nil {
  63. log.Error("row.ScanCount error(%v)", err)
  64. err = errors.WithStack(err)
  65. return
  66. }
  67. rows, err := d.db.Query(c, _getUserChangeHistorysByMidAndCtime, mid, from, to, (pn-1)*ps, ps)
  68. if err != nil {
  69. log.Error("db.Query(%s) error(%v)", _getUserChangeHistorysByMidAndCtime, err)
  70. err = errors.WithStack(err)
  71. return
  72. }
  73. defer rows.Close()
  74. for rows.Next() {
  75. uch := &model.UserChangeHistory{}
  76. if err = rows.Scan(&uch.ID, &uch.Mid, &uch.ChangeType, &uch.ChangeTime, &uch.OrderNo, &uch.Days, &uch.OperatorId, &uch.Remark, &uch.Ctime, &uch.Mtime); err != nil {
  77. log.Error("rows.Scan() error(%v)", err)
  78. err = errors.WithStack(err)
  79. return
  80. }
  81. res = append(res, uch)
  82. }
  83. return
  84. }
  85. // TxInsertUserChangeHistory insert one row into tv_user_change_history.
  86. func (d *Dao) TxInsertUserChangeHistory(ctx context.Context, tx *xsql.Tx, uch *model.UserChangeHistory) (id int64, err error) {
  87. var (
  88. res sql.Result
  89. )
  90. if res, err = tx.Exec(_insertUserChangeHistory, uch.Mid, uch.ChangeType, uch.ChangeTime, uch.OrderNo, uch.Days, uch.OperatorId, uch.Remark); err != nil {
  91. log.Error("tx.Exec(%s) error(%v)", _insertUserChangeHistory, err)
  92. err = errors.WithStack(err)
  93. return
  94. }
  95. if id, err = res.LastInsertId(); err != nil {
  96. log.Error("res.LastInsertId(%s) error(%v)", _insertUserChangeHistory, err)
  97. err = errors.WithStack(err)
  98. return
  99. }
  100. return
  101. }