order_log.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "net"
  7. time2 "time"
  8. "go-common/app/service/openplatform/ticket-sales/api/grpc/v1"
  9. "go-common/library/log"
  10. "go-common/library/net/metadata"
  11. "go-common/library/time"
  12. )
  13. const (
  14. _addOrderLog = "insert into order_log (uid,ip,order_id,op_data,remark,op_object,op_name,ctime) values(?,?,?,?,?,?,?,?)"
  15. _getOrderLogList = "select id,uid, order_id, ip,op_data,remark,op_object,op_name,ctime,mtime from order_log where order_id = %v order by %v desc limit %v,%v"
  16. _getOrderCnt = "select count(1) from order_log where order_id = ? "
  17. )
  18. //GetOrderLogList 获取订单日志列表
  19. func (d *Dao) GetOrderLogList(c context.Context, OID int64, index int64, size int64, orderBy string) (res []*v1.OrderLog, err error) {
  20. res = make([]*v1.OrderLog, 0)
  21. rows, err := d.db.Query(c, fmt.Sprintf(_getOrderLogList, OID, orderBy, index, size))
  22. if err != nil {
  23. log.Warn("d.GetOrderLogList(%v) d.db.Query() error(%v)", OID, err)
  24. return
  25. }
  26. defer rows.Close()
  27. var ip net.IP
  28. for rows.Next() {
  29. r := &v1.OrderLog{}
  30. if err = rows.Scan(&r.ID, &r.UID, &r.OID, &ip, &r.OpData, &r.Remark, &r.OpObject, &r.OpName, &r.CTime, &r.MTime); err != nil {
  31. return
  32. }
  33. r.IP = ip.String()
  34. res = append(res, r)
  35. }
  36. return
  37. }
  38. //GetOrderLogCnt 获取订单日志条数
  39. func (d *Dao) GetOrderLogCnt(c context.Context, OID int64) (cnt int64, err error) {
  40. err = d.db.QueryRow(c, _getOrderCnt, OID).Scan(&cnt)
  41. if err != nil {
  42. log.Warn("d.GetOrderLogCnt error(%v)", err)
  43. return
  44. }
  45. return
  46. }
  47. //AddOrderLog 添加订单日志
  48. func (d *Dao) AddOrderLog(c context.Context, oi *v1.OrderLog) (cnt int64, err error) {
  49. var res sql.Result
  50. if oi.CTime == 0 {
  51. oi.CTime = time.Time(time2.Now().Unix())
  52. }
  53. var ip []byte
  54. if ip = net.ParseIP(metadata.String(c, metadata.RemoteIP)); ip == nil {
  55. ip = []byte{}
  56. }
  57. if res, err = d.db.Exec(c, _addOrderLog, oi.UID, ip, oi.OID, oi.OpData, oi.Remark, oi.OpObject, oi.OpName, oi.CTime); err != nil {
  58. log.Warn("创建订单日志%d失败", oi.OID)
  59. return
  60. }
  61. return res.LastInsertId()
  62. }