resource.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "fmt"
  6. "strings"
  7. "go-common/app/service/main/vip/model"
  8. "go-common/library/database/sql"
  9. "go-common/library/time"
  10. "go-common/library/xstr"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _defaultsize = 10000
  15. )
  16. //SelNewResourcePool select new resource pool by id
  17. func (d *Dao) SelNewResourcePool(c context.Context, id int64) (r *model.VipResourcePool, err error) {
  18. var row = d.db.QueryRow(c, _selResourcePoolByIDSQL, id)
  19. r = new(model.VipResourcePool)
  20. if err = row.Scan(&r.ID, &r.PoolName, &r.BusinessID, &r.Reason, &r.CodeExpireTime, &r.StartTime, &r.EndTime, &r.Contacts, &r.ContactsNumber, &r.Ctime, &r.Mtime); err != nil {
  21. if err == sql.ErrNoRows {
  22. r = nil
  23. err = nil
  24. } else {
  25. err = errors.WithStack(err)
  26. d.errProm.Incr("row_scan_db")
  27. return
  28. }
  29. }
  30. return
  31. }
  32. //SelNewBusiness select newdb businessInfo by id.
  33. func (d *Dao) SelNewBusiness(c context.Context, id int64) (r *model.VipBusinessInfo, err error) {
  34. var row = d.db.QueryRow(c, _selBusinessByIDSQL, id)
  35. r = new(model.VipBusinessInfo)
  36. if err = row.Scan(&r.ID, &r.BusinessName, &r.BusinessType, &r.Status, &r.AppKey, &r.Secret, &r.Contacts, &r.ContactsNumber, &r.Ctime, &r.Mtime); err != nil {
  37. if err == sql.ErrNoRows {
  38. err = nil
  39. r = nil
  40. } else {
  41. err = errors.WithStack(err)
  42. d.errProm.Incr("row_scan_db")
  43. }
  44. }
  45. return
  46. }
  47. //SelNewBusinessByAppkey select newdb businessInfo by id.
  48. func (d *Dao) SelNewBusinessByAppkey(c context.Context, appkey string) (r *model.VipBusinessInfo, err error) {
  49. var row = d.db.QueryRow(c, _selBusinessByAppkeySQL, appkey)
  50. r = new(model.VipBusinessInfo)
  51. if err = row.Scan(&r.ID, &r.BusinessName, &r.BusinessType, &r.Status, &r.AppKey, &r.Secret, &r.Contacts, &r.ContactsNumber, &r.Ctime, &r.Mtime); err != nil {
  52. if err == sql.ErrNoRows {
  53. err = nil
  54. r = nil
  55. } else {
  56. err = errors.WithStack(err)
  57. d.errProm.Incr("row_scan_db")
  58. }
  59. }
  60. return
  61. }
  62. //SelCode sel code.
  63. func (d *Dao) SelCode(c context.Context, codeStr string) (code *model.VipResourceCode, err error) {
  64. row := d.db.QueryRow(c, _selCodeSQL, codeStr)
  65. code = new(model.VipResourceCode)
  66. if err = row.Scan(&code.ID, &code.BatchCodeID, &code.Status, &code.Code, &code.Mid, &code.UseTime, &code.RelationID); err != nil {
  67. if sql.ErrNoRows == err {
  68. c = nil
  69. err = nil
  70. return
  71. }
  72. err = errors.WithStack(err)
  73. d.errProm.Incr("db_scan")
  74. return
  75. }
  76. return
  77. }
  78. //SelCodes sel codes
  79. func (d *Dao) SelCodes(c context.Context, codes []string) (cs []*model.VipResourceCode, err error) {
  80. var (
  81. rows *sql.Rows
  82. )
  83. if len(codes) <= 0 {
  84. return
  85. }
  86. if rows, err = d.db.Query(c, fmt.Sprintf(_selCodesSQL, strings.Join(codes, "','"))); err != nil {
  87. err = errors.WithStack(err)
  88. return
  89. }
  90. defer rows.Close()
  91. for rows.Next() {
  92. code := new(model.VipResourceCode)
  93. if err = rows.Scan(&code.ID, &code.BatchCodeID, &code.Status, &code.Code, &code.Mid, &code.UseTime, &code.RelationID); err != nil {
  94. if sql.ErrNoRows == err {
  95. c = nil
  96. err = nil
  97. return
  98. }
  99. err = errors.WithStack(err)
  100. d.errProm.Incr("db_scan")
  101. return
  102. }
  103. cs = append(cs, code)
  104. }
  105. err = rows.Err()
  106. return
  107. }
  108. // SelBatchCode set batch code.
  109. func (d *Dao) SelBatchCode(c context.Context, id int64) (bc *model.VipResourceBatchCode, err error) {
  110. row := d.db.QueryRow(c, _selBatchCodeSQL, id)
  111. bc = new(model.VipResourceBatchCode)
  112. if err = row.Scan(&bc.ID, &bc.BusinessID, &bc.PoolID, &bc.Status, &bc.BatchName, &bc.Reason, &bc.Unit, &bc.Count, &bc.SurplusCount, &bc.Price, &bc.StartTime, &bc.EndTime, &bc.Type, &bc.LimitDay, &bc.MaxCount); err != nil {
  113. if sql.ErrNoRows == err {
  114. bc = nil
  115. err = nil
  116. return
  117. }
  118. err = errors.WithStack(err)
  119. d.errProm.Incr("db_scan")
  120. return
  121. }
  122. return
  123. }
  124. //SelBatchCodes sel batchcodes
  125. func (d *Dao) SelBatchCodes(c context.Context, ids []int64) (bcs []*model.VipResourceBatchCode, err error) {
  126. var (
  127. rows *sql.Rows
  128. )
  129. if len(ids) <= 0 {
  130. return
  131. }
  132. if rows, err = d.db.Query(c, fmt.Sprintf(_selBatchCodesSQL, xstr.JoinInts(ids))); err != nil {
  133. err = errors.WithStack(err)
  134. return
  135. }
  136. defer rows.Close()
  137. for rows.Next() {
  138. bc := new(model.VipResourceBatchCode)
  139. if err = rows.Scan(&bc.ID, &bc.BusinessID, &bc.PoolID, &bc.Status, &bc.BatchName, &bc.Reason, &bc.Unit, &bc.Count, &bc.SurplusCount, &bc.Price, &bc.StartTime, &bc.EndTime, &bc.Type, &bc.LimitDay, &bc.MaxCount); err != nil {
  140. if sql.ErrNoRows == err {
  141. bc = nil
  142. err = nil
  143. return
  144. }
  145. err = errors.WithStack(err)
  146. d.errProm.Incr("db_scan")
  147. return
  148. }
  149. bcs = append(bcs, bc)
  150. }
  151. err = rows.Err()
  152. return
  153. }
  154. // SelBatchCodesByBisID set batch codes by business id.
  155. func (d *Dao) SelBatchCodesByBisID(c context.Context, bisID int64) (bcs []*model.VipResourceBatchCode, err error) {
  156. var rows *sql.Rows
  157. if rows, err = d.db.Query(c, _selBatchCodeByBisSQL, bisID); err != nil {
  158. err = errors.WithStack(err)
  159. return
  160. }
  161. defer rows.Close()
  162. for rows.Next() {
  163. bc := new(model.VipResourceBatchCode)
  164. if err = rows.Scan(&bc.ID, &bc.BusinessID, &bc.PoolID, &bc.Status, &bc.BatchName, &bc.Reason, &bc.Unit, &bc.Count, &bc.SurplusCount, &bc.Price, &bc.StartTime, &bc.EndTime, &bc.Type, &bc.LimitDay, &bc.MaxCount); err != nil {
  165. if sql.ErrNoRows == err {
  166. bc = nil
  167. err = nil
  168. return
  169. }
  170. err = errors.WithStack(err)
  171. d.errProm.Incr("db_scan")
  172. return
  173. }
  174. bcs = append(bcs, bc)
  175. }
  176. err = rows.Err()
  177. return
  178. }
  179. // SelCodeOpened set code open.
  180. func (d *Dao) SelCodeOpened(c context.Context, bisIDs []int64, arg *model.ArgCodeOpened) (cs []*model.CodeInfoResp, err error) {
  181. var rows *sql.Rows
  182. size := d.c.Property.CodeOpenedSearchSize
  183. if size == 0 {
  184. size = _defaultsize
  185. }
  186. if rows, err = d.db.Query(c, fmt.Sprintf(_selCodeOpenedSQL, xstr.JoinInts(bisIDs), arg.Cursor, arg.StartTime.Time().Format("2006-01-02 15:04:05"), arg.EndTime.Time().Format("2006-01-02 15:04:05"), size)); err != nil {
  187. err = errors.WithStack(err)
  188. return
  189. }
  190. defer rows.Close()
  191. for rows.Next() {
  192. code := new(model.CodeInfoResp)
  193. if err = rows.Scan(&code.ID, &code.Code, &code.UserTime); err != nil {
  194. if sql.ErrNoRows == err {
  195. c = nil
  196. err = nil
  197. return
  198. }
  199. err = errors.WithStack(err)
  200. d.errProm.Incr("db_scan")
  201. return
  202. }
  203. cs = append(cs, code)
  204. }
  205. err = rows.Err()
  206. return
  207. }
  208. //TxUpdateCode tx update code.
  209. func (d *Dao) TxUpdateCode(tx *sql.Tx, id int64, mid int64, useTime time.Time) (eff int64, err error) {
  210. var result xsql.Result
  211. if result, err = tx.Exec(_updateCodeSQL, mid, useTime, id); err != nil {
  212. err = errors.WithStack(err)
  213. d.errProm.Incr("db_update")
  214. return
  215. }
  216. if eff, err = result.RowsAffected(); err != nil {
  217. err = errors.WithStack(err)
  218. d.errProm.Incr("db_row_affected")
  219. return
  220. }
  221. return
  222. }
  223. //TxUpdateCodeStatus tx update code status.
  224. func (d *Dao) TxUpdateCodeStatus(tx *sql.Tx, id int64, status int8) (eff int64, err error) {
  225. var result xsql.Result
  226. if result, err = tx.Exec(_updateCodeStatusSQL, status, id); err != nil {
  227. err = errors.WithStack(err)
  228. d.errProm.Incr("db_update")
  229. return
  230. }
  231. if eff, err = result.RowsAffected(); err != nil {
  232. err = errors.WithStack(err)
  233. d.errProm.Incr("db_row_affected")
  234. return
  235. }
  236. return
  237. }
  238. //TxUpdateBatchCode tx update batch code.
  239. func (d *Dao) TxUpdateBatchCode(tx *sql.Tx, id int64, sc int32) (eff int64, err error) {
  240. var result xsql.Result
  241. if result, err = tx.Exec(_updateBatchCodeSQL, sc, id); err != nil {
  242. err = errors.WithStack(err)
  243. d.errProm.Incr("db_batch_code")
  244. return
  245. }
  246. if eff, err = result.RowsAffected(); err != nil {
  247. err = errors.WithStack(err)
  248. d.errProm.Incr("db_row_affected")
  249. return
  250. }
  251. return
  252. }
  253. //SelCodesByBMid sel codes by bmid
  254. func (d *Dao) SelCodesByBMid(c context.Context, mid int64) (cs []string, err error) {
  255. var rows *sql.Rows
  256. if rows, err = d.db.Query(c, _selCodesByBmidSQL, mid); err != nil {
  257. err = errors.WithStack(err)
  258. d.errProm.Incr("db_query")
  259. return
  260. }
  261. defer rows.Close()
  262. for rows.Next() {
  263. var r string
  264. if err = rows.Scan(&r); err != nil {
  265. if err == sql.ErrNoRows {
  266. err = nil
  267. return
  268. }
  269. err = errors.WithStack(err)
  270. d.errProm.Incr("db_rows_scan")
  271. return
  272. }
  273. cs = append(cs, r)
  274. }
  275. err = rows.Err()
  276. return
  277. }
  278. //SelActives sel active data.
  279. func (d *Dao) SelActives(c context.Context, relations []string) (rep []*model.VipActiveShow, err error) {
  280. if len(relations) <= 0 {
  281. return
  282. }
  283. var rows *sql.Rows
  284. if rows, err = d.db.Query(c, fmt.Sprintf(_selActiveSQL, strings.Join(relations, "','"))); err != nil {
  285. err = errors.WithStack(err)
  286. return
  287. }
  288. defer rows.Close()
  289. for rows.Next() {
  290. r := new(model.VipActiveShow)
  291. if err = rows.Scan(&r.ID, &r.Type, &r.ProductName, &r.ProductPic, &r.RelationID, &r.BusID, &r.ProductDetail, &r.UseType); err != nil {
  292. err = errors.WithStack(err)
  293. return
  294. }
  295. rep = append(rep, r)
  296. }
  297. err = rows.Err()
  298. return
  299. }
  300. //SelBatchCount sel batch count
  301. func (d *Dao) SelBatchCount(c context.Context, batchCodeID, mid int64) (count int64, err error) {
  302. row := d.db.QueryRow(c, _selBatchCountSQL, mid, batchCodeID)
  303. if err = row.Scan(&count); err != nil {
  304. err = errors.WithStack(err)
  305. }
  306. return
  307. }