G2AOrder.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package model
  2. import (
  3. "crawler/config"
  4. "crawler/service"
  5. "database/sql"
  6. "log"
  7. )
  8. type G2AOrderModel struct {
  9. G2AOrderParams
  10. G2AOrderResult
  11. G2AOrderDetailResult
  12. G2AOrderPayResult
  13. G2AOrderKeyResult
  14. G2AOrder
  15. }
  16. type G2AOrderParams struct {
  17. ProductID string `form:"product_id" json:"product_id" binding:"required"`
  18. Currency string `form:"currency" json:"currency"`
  19. MaxPrice float64 `form:"max_price" json:"max_price"`
  20. }
  21. // Add an Order
  22. type G2AOrderResult struct {
  23. OrderID string `json:"order_id"`
  24. Price float32 `json:"price"`
  25. Currency string `json:"currency"`
  26. }
  27. // Get Order Detail
  28. type G2AOrderDetailResult struct {
  29. Status string `json:"status"`
  30. Price float32 `json:"price"`
  31. Currency string `json:"currency"`
  32. }
  33. // Get Order Key
  34. type G2AOrderKeyResult struct {
  35. Key string `json:"key"`
  36. }
  37. // Pay for an order
  38. type G2AOrderPayResult struct {
  39. Status bool `json:"status"`
  40. TransactionID string `json:"transaction_id"`
  41. }
  42. type G2AOrder struct {
  43. RequestKey string `json:"request_key"`
  44. RequestOrderID int `json:"request_order_id"`
  45. RequestOrderDetailID int `json:"request_order_detail_id"`
  46. Status string `json:"status"`
  47. OrderID string `json:"order_id"`
  48. ProductID string `json:"product_id"`
  49. Code string `json:"code"`
  50. Price float32 `json:"price"`
  51. Currency string `json:"currency"`
  52. TransactionID string `json:"transaction_id"`
  53. }
  54. func (this *G2AOrderModel) IsExists(requestKey string) bool {
  55. var (
  56. db = service.DB_PLAYR
  57. conn = db.SQLDB
  58. query = "SELECT IF(COUNT(*) <= 0, 0, 1) AS `exists` FROM tb_g2a_order WHERE request_key = ?;"
  59. exists = false
  60. )
  61. stmt, err := conn.Prepare(query)
  62. if err != nil {
  63. log.Fatal(err)
  64. }
  65. err = stmt.QueryRow(requestKey).Scan(&exists)
  66. if err != nil {
  67. db.SetErrorLog(err, query)
  68. return exists
  69. }
  70. db.SetGeneralLog(config.GL_ACTION_SELECT, query, "select exists g2a order")
  71. return exists
  72. }
  73. func (this *G2AOrderModel) Info(requestKey string) (G2AOrder, error) {
  74. var (
  75. db = service.DB_PLAYR
  76. conn = db.SQLDB
  77. query = `SELECT request_key, request_order_id, request_order_detail_id, status, order_id, product_id, code, price, currency, transaction_id FROM tb_g2a_order WHERE request_key = ?;`
  78. info G2AOrder
  79. )
  80. err := conn.QueryRow(query, requestKey).Scan(
  81. &info.RequestKey, &info.RequestOrderID, &info.RequestOrderDetailID, &info.Status, &info.OrderID, &info.ProductID, &info.Code, &info.Price, &info.Currency, &info.TransactionID,
  82. )
  83. if err != nil && err != sql.ErrNoRows {
  84. db.SetErrorLog(err, query)
  85. return info, err
  86. }
  87. db.SetGeneralLog(config.GL_ACTION_SELECT, query, "select g2a info")
  88. return info, nil
  89. }
  90. func (this *G2AOrderModel) Insert(order G2AOrder) {
  91. var (
  92. db = service.DB_PLAYR
  93. conn = db.SQLDB
  94. )
  95. sql := `
  96. INSERT INTO tb_g2a_order
  97. SET
  98. request_key = ?,
  99. request_order_id = ?,
  100. request_order_detail_id = ?,
  101. status = ?,
  102. order_id = ?,
  103. product_id = ?,
  104. code = ?,
  105. price = ?,
  106. currency = ?,
  107. transaction_id = ?,
  108. created_at = NOW();
  109. `
  110. _, err := conn.Exec(sql,
  111. order.RequestKey, order.RequestOrderID, order.RequestOrderDetailID, order.Status, order.OrderID, order.ProductID, order.Code, order.Price, order.Currency, order.TransactionID,
  112. )
  113. if err != nil {
  114. db.SetErrorLog(err, sql)
  115. }
  116. db.SetGeneralLog(config.GL_ACTION_WRITE, sql, "insert g2a order info")
  117. }