| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- package model
- import (
- "crawler/config"
- "crawler/service"
- "database/sql"
- "log"
- )
- type G2AOrderModel struct {
- G2AOrderParams
- G2AOrderResult
- G2AOrderDetailResult
- G2AOrderPayResult
- G2AOrderKeyResult
- G2AOrder
- }
- type G2AOrderParams struct {
- ProductID string `form:"product_id" json:"product_id" binding:"required"`
- Currency string `form:"currency" json:"currency"`
- MaxPrice float64 `form:"max_price" json:"max_price"`
- }
- // Add an Order
- type G2AOrderResult struct {
- OrderID string `json:"order_id"`
- Price float32 `json:"price"`
- Currency string `json:"currency"`
- }
- // Get Order Detail
- type G2AOrderDetailResult struct {
- Status string `json:"status"`
- Price float32 `json:"price"`
- Currency string `json:"currency"`
- }
- // Get Order Key
- type G2AOrderKeyResult struct {
- Key string `json:"key"`
- }
- // Pay for an order
- type G2AOrderPayResult struct {
- Status bool `json:"status"`
- TransactionID string `json:"transaction_id"`
- }
- type G2AOrder struct {
- RequestKey string `json:"request_key"`
- RequestOrderID int `json:"request_order_id"`
- RequestOrderDetailID int `json:"request_order_detail_id"`
- Status string `json:"status"`
- OrderID string `json:"order_id"`
- ProductID string `json:"product_id"`
- Code string `json:"code"`
- Price float32 `json:"price"`
- Currency string `json:"currency"`
- TransactionID string `json:"transaction_id"`
- }
- func (this *G2AOrderModel) IsExists(requestKey string) bool {
- var (
- db = service.DB_PLAYR
- conn = db.SQLDB
- query = "SELECT IF(COUNT(*) <= 0, 0, 1) AS `exists` FROM tb_g2a_order WHERE request_key = ?;"
- exists = false
- )
- stmt, err := conn.Prepare(query)
- if err != nil {
- log.Fatal(err)
- }
- err = stmt.QueryRow(requestKey).Scan(&exists)
- if err != nil {
- db.SetErrorLog(err, query)
- return exists
- }
- db.SetGeneralLog(config.GL_ACTION_SELECT, query, "select exists g2a order")
- return exists
- }
- func (this *G2AOrderModel) Info(requestKey string) (G2AOrder, error) {
- var (
- db = service.DB_PLAYR
- conn = db.SQLDB
- 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 = ?;`
- info G2AOrder
- )
- err := conn.QueryRow(query, requestKey).Scan(
- &info.RequestKey, &info.RequestOrderID, &info.RequestOrderDetailID, &info.Status, &info.OrderID, &info.ProductID, &info.Code, &info.Price, &info.Currency, &info.TransactionID,
- )
- if err != nil && err != sql.ErrNoRows {
- db.SetErrorLog(err, query)
- return info, err
- }
- db.SetGeneralLog(config.GL_ACTION_SELECT, query, "select g2a info")
- return info, nil
- }
- func (this *G2AOrderModel) Insert(order G2AOrder) {
- var (
- db = service.DB_PLAYR
- conn = db.SQLDB
- )
- sql := `
- INSERT INTO tb_g2a_order
- SET
- request_key = ?,
- request_order_id = ?,
- request_order_detail_id = ?,
- status = ?,
- order_id = ?,
- product_id = ?,
- code = ?,
- price = ?,
- currency = ?,
- transaction_id = ?,
- created_at = NOW();
- `
- _, err := conn.Exec(sql,
- order.RequestKey, order.RequestOrderID, order.RequestOrderDetailID, order.Status, order.OrderID, order.ProductID, order.Code, order.Price, order.Currency, order.TransactionID,
- )
- if err != nil {
- db.SetErrorLog(err, sql)
- }
- db.SetGeneralLog(config.GL_ACTION_WRITE, sql, "insert g2a order info")
- }
|