mysql.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "go-common/app/service/main/point/model"
  6. "go-common/library/database/sql"
  7. xtime "go-common/library/time"
  8. "github.com/pkg/errors"
  9. )
  10. const (
  11. // point sql
  12. _pointInfoSQL = "SELECT mid,point_balance,ver FROM point_info WHERE mid=?"
  13. _pointHistorySQL = "SELECT id,mid,point,order_id,change_type,change_time,relation_id,point_balance,remark,operator FROM point_change_history WHERE mid = ? AND id < ? ORDER BY id DESC LIMIT ?"
  14. _pointHistoryCountSQL = "SELECT COUNT(1) FROM point_change_history WHERE mid = ?"
  15. _updatePointSQL = "UPDATE point_info SET point_balance = ?,ver=? WHERE mid=? AND ver=?"
  16. _insertPointSQL = "INSERT INTO point_info(mid,point_balance,ver) VALUES(?,?,?)"
  17. _InsertPointHistorySQL = "INSERT INTO point_change_history(mid,point,order_id,change_type,change_time,relation_id,point_balance,remark,operator) VALUES(?,?,?,?,?,?,?,?,?)"
  18. _pointHistoryCheckSQL = "SELECT id FROM point_change_history WHERE order_id=?"
  19. _selPointHistorySQL = "SELECT id,mid,point,order_id,change_type,change_time,relation_id,point_balance,remark,operator FROM point_change_history WHERE mid = ? AND change_time>=? AND change_time <= ? "
  20. _allPointConfig = "SELECT `id`,`app_id`,`point`,`operator`,`ctime`,`mtime` FROM `point_conf`;"
  21. //TODO compatible vip point old gateway
  22. _oldPointHistorySQL = "SELECT id,mid,point,order_id,change_type,change_time,relation_id,point_balance,remark,operator FROM point_change_history WHERE mid = ? ORDER BY id DESC LIMIT ?,?;"
  23. )
  24. // BeginTran begin transaction.
  25. func (d *Dao) BeginTran(c context.Context) (*sql.Tx, error) {
  26. return d.db.Begin(c)
  27. }
  28. // PointInfo .
  29. func (d *Dao) PointInfo(c context.Context, mid int64) (pi *model.PointInfo, err error) {
  30. row := d.db.QueryRow(c, _pointInfoSQL, mid)
  31. pi = new(model.PointInfo)
  32. if err = row.Scan(&pi.Mid, &pi.PointBalance, &pi.Ver); err != nil {
  33. if err == sql.ErrNoRows {
  34. err = nil
  35. pi = nil
  36. } else {
  37. err = errors.WithStack(err)
  38. }
  39. }
  40. return
  41. }
  42. //TxPointInfo .
  43. func (d *Dao) TxPointInfo(c context.Context, tx *sql.Tx, mid int64) (pi *model.PointInfo, err error) {
  44. row := tx.QueryRow(_pointInfoSQL, mid)
  45. pi = new(model.PointInfo)
  46. if err = row.Scan(&pi.Mid, &pi.PointBalance, &pi.Ver); err != nil {
  47. if err == sql.ErrNoRows {
  48. err = nil
  49. pi = nil
  50. } else {
  51. err = errors.WithStack(err)
  52. }
  53. }
  54. return
  55. }
  56. //PointHistory point history
  57. func (d *Dao) PointHistory(c context.Context, mid int64, cursor int, ps int) (phs []*model.PointHistory, err error) {
  58. var rows *sql.Rows
  59. if rows, err = d.db.Query(c, _pointHistorySQL, mid, cursor, ps); err != nil {
  60. err = errors.WithStack(err)
  61. return
  62. }
  63. for rows.Next() {
  64. ph := new(model.PointHistory)
  65. if err = rows.Scan(&ph.ID, &ph.Mid, &ph.Point, &ph.OrderID, &ph.ChangeType, &ph.ChangeTime, &ph.RelationID, &ph.PointBalance, &ph.Remark, &ph.Operator); err != nil {
  66. phs = nil
  67. err = errors.WithStack(err)
  68. return
  69. }
  70. phs = append(phs, ph)
  71. }
  72. return
  73. }
  74. //PointHistoryCount point history
  75. func (d *Dao) PointHistoryCount(c context.Context, mid int64) (count int, err error) {
  76. row := d.db.QueryRow(c, _pointHistoryCountSQL, mid)
  77. if err = row.Scan(&count); err != nil {
  78. err = errors.WithStack(err)
  79. }
  80. return
  81. }
  82. //UpdatePointInfo .
  83. func (d *Dao) UpdatePointInfo(c context.Context, tx *sql.Tx, pi *model.PointInfo, ver int64) (a int64, err error) {
  84. var res xsql.Result
  85. if res, err = tx.Exec(_updatePointSQL, pi.PointBalance, pi.Ver, pi.Mid, ver); err != nil {
  86. err = errors.WithStack(err)
  87. return
  88. }
  89. if a, err = res.RowsAffected(); err != nil {
  90. err = errors.WithStack(err)
  91. return
  92. }
  93. return
  94. }
  95. //InsertPoint .
  96. func (d *Dao) InsertPoint(c context.Context, tx *sql.Tx, pi *model.PointInfo) (a int64, err error) {
  97. var res xsql.Result
  98. if res, err = tx.Exec(_insertPointSQL, pi.Mid, pi.PointBalance, pi.Ver); err != nil {
  99. err = errors.WithStack(err)
  100. return
  101. }
  102. if a, err = res.RowsAffected(); err != nil {
  103. err = errors.WithStack(err)
  104. return
  105. }
  106. return
  107. }
  108. //InsertPointHistory .
  109. func (d *Dao) InsertPointHistory(c context.Context, tx *sql.Tx, ph *model.PointHistory) (a int64, err error) {
  110. var res xsql.Result
  111. if res, err = tx.Exec(_InsertPointHistorySQL, ph.Mid, ph.Point, ph.OrderID, ph.ChangeType, ph.ChangeTime, ph.RelationID, ph.PointBalance, ph.Remark, ph.Operator); err != nil {
  112. err = errors.WithStack(err)
  113. return
  114. }
  115. if a, err = res.RowsAffected(); err != nil {
  116. err = errors.WithStack(err)
  117. }
  118. return
  119. }
  120. //SelPointHistory .
  121. func (d *Dao) SelPointHistory(c context.Context, mid int64, startDate, endDate xtime.Time) (phs []*model.PointHistory, err error) {
  122. var rows *sql.Rows
  123. if rows, err = d.db.Query(c, _selPointHistorySQL, mid, startDate, endDate); err != nil {
  124. err = errors.WithStack(err)
  125. return
  126. }
  127. for rows.Next() {
  128. ph := new(model.PointHistory)
  129. if err = rows.Scan(&ph.ID, &ph.Mid, &ph.Point, &ph.OrderID, &ph.ChangeType, &ph.ChangeTime, &ph.RelationID, &ph.PointBalance, &ph.Remark, &ph.Operator); err != nil {
  130. phs = nil
  131. err = errors.WithStack(err)
  132. }
  133. phs = append(phs, ph)
  134. }
  135. return
  136. }
  137. // ExistPointOrder check orderID is uniq or not
  138. func (d *Dao) ExistPointOrder(c context.Context, orID string) (id int, err error) {
  139. row := d.db.QueryRow(c, _pointHistoryCheckSQL, orID)
  140. if err = row.Scan(&id); err != nil {
  141. if err == sql.ErrNoRows {
  142. err = nil
  143. return
  144. }
  145. err = errors.WithStack(err)
  146. return
  147. }
  148. return
  149. }
  150. // AllPointConfig all point config.
  151. func (d *Dao) AllPointConfig(c context.Context) (res []*model.VipPointConf, err error) {
  152. var rows *sql.Rows
  153. if rows, err = d.db.Query(c, _allPointConfig); err != nil {
  154. err = errors.WithStack(err)
  155. return
  156. }
  157. for rows.Next() {
  158. vf := new(model.VipPointConf)
  159. if err = rows.Scan(&vf.ID, &vf.AppID, &vf.Point, &vf.Operator, &vf.Ctime, &vf.Mtime); err != nil {
  160. res = nil
  161. err = errors.WithStack(err)
  162. }
  163. res = append(res, vf)
  164. }
  165. return
  166. }
  167. //OldPointHistory point history.
  168. func (d *Dao) OldPointHistory(c context.Context, mid int64, start int, ps int) (phs []*model.OldPointHistory, err error) {
  169. var rows *sql.Rows
  170. if rows, err = d.db.Query(c, _oldPointHistorySQL, mid, start, ps); err != nil {
  171. err = errors.WithStack(err)
  172. return
  173. }
  174. for rows.Next() {
  175. ph := new(model.PointHistory)
  176. if err = rows.Scan(&ph.ID, &ph.Mid, &ph.Point, &ph.OrderID, &ph.ChangeType, &ph.ChangeTime, &ph.RelationID, &ph.PointBalance, &ph.Remark, &ph.Operator); err != nil {
  177. phs = nil
  178. err = errors.WithStack(err)
  179. return
  180. }
  181. oph := new(model.OldPointHistory)
  182. oph.ID = ph.ID
  183. oph.Mid = ph.Mid
  184. oph.Point = ph.Point
  185. oph.OrderID = ph.OrderID
  186. oph.ChangeType = ph.ChangeType
  187. oph.ChangeTime = ph.ChangeTime.Time().Unix()
  188. oph.RelationID = ph.RelationID
  189. oph.PointBalance = ph.PointBalance
  190. oph.Remark = ph.Remark
  191. oph.Operator = ph.Operator
  192. phs = append(phs, oph)
  193. }
  194. return
  195. }