distrib.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "github.com/pkg/errors"
  7. "go-common/app/service/openplatform/ticket-sales/model"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _hasDisOrder = "SELECT order_id,status FROM dist_order where order_id=? and status=?"
  12. _getOrder = "SELECT order_id,cm_amount,cm_method,cm_price,dist_user,status,pid,count,sid,type,payment_amount,serial_num,ctime,mtime FROM dist_order where order_id = ?"
  13. _addDisOrder = "INSERT INTO dist_order(order_id,cm_amount,cm_method,cm_price,dist_user,status,pid,count,sid,type,payment_amount,serial_num) VALUES(?,?,?,?,?,?,?,?,?,?,?,?)"
  14. )
  15. // InsertOrder 同步订单到分销表方法
  16. func (d *Dao) InsertOrder(c context.Context, oi *model.DistOrderArg) (lastID int64, err error) {
  17. res, err := d.db.Exec(c, _addDisOrder, &oi.Oid, &oi.CmAmount, &oi.CmMethod, &oi.CmPrice, &oi.Duid, &oi.Stat, &oi.Pid, &oi.Count, &oi.Sid, &oi.Type, &oi.PayAmount, &oi.Serial)
  18. if err != nil {
  19. errors.Wrap(err, fmt.Sprintf("db.Exec(%s) err ", _addDisOrder))
  20. return
  21. }
  22. lastID, err = res.LastInsertId()
  23. return
  24. }
  25. // HasOrder 检查订单是否存在
  26. func (d *Dao) HasOrder(c context.Context, oi *model.DistOrderArg) (has bool, err error) {
  27. has = false
  28. row := d.db.QueryRow(c, _hasDisOrder, &oi.Oid, &oi.Stat)
  29. err = row.Scan(&oi.Oid, &oi.Stat)
  30. if err == nil {
  31. has = true
  32. return
  33. }
  34. if err != nil && err != sql.ErrNoRows {
  35. errors.Wrap(err, "dao Hasorder err")
  36. return
  37. }
  38. return
  39. }
  40. // GetOrder 检查订单是否存在
  41. func (d *Dao) GetOrder(c context.Context, oid uint64) (res []*model.OrderInfo, err error) {
  42. rows, err := d.db.Query(c, _getOrder, oid)
  43. if err != nil {
  44. log.Error("[dao.distrib|GetOrder] d.db.Query err: %v", err)
  45. return
  46. }
  47. defer rows.Close()
  48. for rows.Next() {
  49. oi := &model.OrderInfo{}
  50. if err = rows.Scan(&oi.Oid, &oi.CmAmount, &oi.CmMethod, &oi.CmPrice, &oi.Duid, &oi.Stat, &oi.Pid, &oi.Count, &oi.Sid, &oi.Type, &oi.PayAmount, &oi.Serial, &oi.Ctime, &oi.Mtime); err != nil {
  51. log.Error("[dao.distrib|GetOrder] rows.Scan err: %v", err)
  52. return
  53. }
  54. res = append(res, oi)
  55. }
  56. return
  57. }