vip.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "fmt"
  6. "strings"
  7. "go-common/app/job/main/vip/model"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/time"
  11. "go-common/library/xstr"
  12. "github.com/pkg/errors"
  13. )
  14. const (
  15. _getVipStatus = "SELECT id,mid,vip_status FROM vip_user_info WHERE mid=?"
  16. _dupUserDiscountHistory = "INSERT INTO vip_user_discount_history(mid,discount_id,order_no,status) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE order_no= VALUES(order_no) ,status = VALUES(status)"
  17. _insertUserInfo = "INSERT ignore INTO vip_user_info(mid,vip_type,vip_pay_type,vip_status,vip_start_time,vip_recent_time,vip_overdue_time,annual_vip_overdue_time,pay_channel_id,ios_overdue_time,ver) VALUES(?,?,?,?,?,?,?,?,?,?,?) "
  18. _updateUserInfo = "UPDATE vip_user_info SET vip_type=?,vip_pay_type=?,vip_status=?,vip_start_time=?,vip_recent_time=?,vip_overdue_time=?,annual_vip_overdue_time=?,pay_channel_id=?,ios_overdue_time=?,ver=? WHERE mid=? AND ver=? "
  19. _SelUserDiscountHistorys = "SELECT mid,discount_id FROM vip_user_discount_history WHERE id>? AND id<=? AND discount_id=?"
  20. _SelVipUserInfos = "SELECT mid,vip_type,vip_pay_type,vip_status,vip_start_time,vip_recent_time,vip_overdue_time,annual_vip_overdue_time FROM vip_user_info WHERE id>? and id<=? "
  21. _SelOldVipUserInfos = "SELECT mid,vip_type,is_auto_renew,auto_renewed,IFNULL(vip_status,0),vip_recent_time,TIMESTAMP(IFNULL(vip_start_time,'2016-01-01 00:00:00')),TIMESTAMP(IFNULL(vip_overdue_time,'2016-01-01 00:00:00')),TIMESTAMP(IFNULL(annual_vip_overdue_time,'2016-01-01 00:00:00')),ver,ios_overdue_time,pay_channel_id,ctime,mtime FROM vip_user_info WHERE id>? and id<=?"
  22. _SelVipList = "SELECT id,mid,vip_type,vip_status,vip_overdue_time,annual_vip_overdue_time,is_auto_renew,pay_channel_id FROM vip_user_info WHERE id>? AND id <=? AND vip_status != 0 AND annual_vip_overdue_time<=?"
  23. _selVipUsersSQL = "SELECT id,mid,vip_type,vip_status,vip_overdue_time,annual_vip_overdue_time FROM vip_user_info WHERE id>? AND id <=? AND vip_pay_type = 1 AND vip_overdue_time>=? AND vip_overdue_time<?"
  24. _selVipUserInfoSQL = "SELECT mid FROM vip_user_info WHERE id>? AND id <=? AND vip_status = ? AND vip_overdue_time>=? AND vip_overdue_time<?"
  25. _UpdateVipUserInfoByID = "UPDATE vip_user_info SET pay_channel_id=?,vip_pay_type=?,vip_recent_time=?,vip_overdue_time=?,annual_vip_overdue_time=?, vip_type=?, vip_status=? WHERE mid=?"
  26. _updateVipStatus = "UPDATE vip_user_info SET vip_status=? WHERE mid=?"
  27. _updateVipUserSQL = "UPDATE vip_user_info SET vip_status=?,vip_type=?,is_auto_renew=? WHERE mid=?"
  28. _SelMaxID = "SELECT IFNULL(MAX(id),0) id FROM vip_user_info"
  29. _SelEffectiveScopeVipList = "SELECT `id`,`mid`,`ver`,`vip_type`,`vip_pay_type`,`pay_channel_id`,`vip_status`,`vip_start_time`,`vip_recent_time`,`vip_overdue_time`,`annual_vip_overdue_time`,`ios_overdue_time`,`ctime` FROM `vip_user_info` WHERE id>? AND id <=?;"
  30. _selDiscountMaxID = "SELECT IFNULL(MAX(id),0) id FROM vip_user_discount_history"
  31. _selUserInfoMaxID = "SELECT IFNULL(MAX(id),0) id FROM vip_user_info"
  32. //Vip change history
  33. _selOldMaxIDSQL = "SELECT IFNULL(MAX(id),0) FROM vip_change_history"
  34. _selMaxIDSQL = "SELECT IFNULL(MAX(id),0) FROM vip_user_change_history"
  35. _selOldChangeHistorySQL = "SELECT id,mid,change_type,change_time,days,month,operator_id,relation_id,batch_id,IFNULL(remark,''),batch_code_id FROM vip_change_history WHERE id>? AND id<=?"
  36. _selOldChangeHistoryByMidsSQL = "SELECT id,mid,change_type,change_time,days,month,operator_id,relation_id,batch_id,IFNULL(remark,''),batch_code_id FROM vip_change_history WHERE mid IN (%v)"
  37. _selChangeHistorySQL = "SELECT id,mid,change_type,change_time,days,operator_id,relation_id,batch_id,remark,batch_code_id FROM vip_user_change_history WHERE id>? AND id<=?"
  38. _selChangeHistoryByMidsSQL = "SELECT id,mid,change_type,change_time,days,operator_id,relation_id,batch_id,remark,batch_code_id FROM vip_user_change_history WHERE mid IN (%v)"
  39. _addChangeHistoryBatchSQL = "INSERT INTO vip_user_change_history(mid,change_type,change_time,days,operator_id,relation_id,batch_id,remark,batch_code_id) VALUES"
  40. // old vip db
  41. _selOldVipUserInfo = "SELECT mid,vip_type,is_auto_renew,auto_renewed,IFNULL(vip_status,0),vip_recent_time,TIMESTAMP(IFNULL(vip_start_time,'2016-01-01 00:00:00')),TIMESTAMP(IFNULL(vip_overdue_time,'2016-01-01 00:00:00')),annual_vip_overdue_time,ios_overdue_time,ver,pay_channel_id FROM vip_user_info WHERE mid=?"
  42. _selVipByMidsSQL = "SELECT id,mid,vip_type,vip_status,vip_recent_time,vip_start_time,vip_overdue_time,annual_vip_overdue_time,vip_pay_type,pay_channel_id,ios_overdue_time,ctime,mtime,ver FROM vip_user_info WHERE mid IN (%s)"
  43. _selVipUserInfoByMid = "SELECT id,mid,ver,vip_type,vip_pay_type,vip_status,vip_start_time,vip_overdue_time,annual_vip_overdue_time,vip_recent_time FROM vip_user_info WHERE mid = ?"
  44. )
  45. //AddChangeHistoryBatch batch add change history
  46. func (d *Dao) AddChangeHistoryBatch(res []*model.VipChangeHistory) (err error) {
  47. var values []string
  48. if len(res) <= 0 {
  49. return
  50. }
  51. for _, v := range res {
  52. value := fmt.Sprintf("('%v','%v','%v','%v','%v','%v','%v','%v','%v')", v.Mid, v.ChangeType, v.ChangeTime.Time().Format("2006-01-02 15:04:05"), v.Days, v.OperatorID, v.RelationID, v.BatchID, v.Remark, v.BatchCodeID)
  53. values = append(values, value)
  54. }
  55. valuesStr := strings.Join(values, ",")
  56. if _, err = d.db.Exec(context.TODO(), _addChangeHistoryBatchSQL+valuesStr); err != nil {
  57. log.Error("AddChangeHistoryBatch d.db.exec(%v),error(%v)", valuesStr, err)
  58. return
  59. }
  60. return
  61. }
  62. //SelChangeHistory .
  63. func (d *Dao) SelChangeHistory(c context.Context, startID, endID int64) (res []*model.VipChangeHistory, err error) {
  64. var rows *sql.Rows
  65. if rows, err = d.db.Query(c, _selChangeHistorySQL, startID, endID); err != nil {
  66. err = errors.WithStack(err)
  67. return
  68. }
  69. defer rows.Close()
  70. for rows.Next() {
  71. r := new(model.VipChangeHistory)
  72. if err = rows.Scan(&r.ID, &r.Mid, &r.ChangeType, &r.ChangeTime, &r.Days, &r.OperatorID, &r.RelationID, &r.BatchID, &r.Remark, &r.BatchCodeID); err != nil {
  73. err = errors.WithStack(err)
  74. res = nil
  75. return
  76. }
  77. res = append(res, r)
  78. }
  79. err = rows.Err()
  80. return
  81. }
  82. //SelOldChangeHistory .
  83. func (d *Dao) SelOldChangeHistory(c context.Context, startID, endID int64) (res []*model.VipChangeHistory, err error) {
  84. var rows *sql.Rows
  85. if rows, err = d.oldDb.Query(c, _selOldChangeHistorySQL, startID, endID); err != nil {
  86. err = errors.WithStack(err)
  87. return
  88. }
  89. defer rows.Close()
  90. for rows.Next() {
  91. r := new(model.VipChangeHistory)
  92. if err = rows.Scan(&r.ID, &r.Mid, &r.ChangeType, &r.ChangeTime, &r.Days, &r.Month, &r.OperatorID, &r.RelationID, &r.BatchID, &r.Remark, &r.BatchCodeID); err != nil {
  93. err = errors.WithStack(err)
  94. res = nil
  95. return
  96. }
  97. res = append(res, r)
  98. }
  99. err = rows.Err()
  100. return
  101. }
  102. //SelChangeHistoryMaps .
  103. func (d *Dao) SelChangeHistoryMaps(c context.Context, mids []int64) (res map[int64][]*model.VipChangeHistory, err error) {
  104. var rows *sql.Rows
  105. if rows, err = d.db.Query(c, fmt.Sprintf(_selChangeHistoryByMidsSQL, xstr.JoinInts(mids))); err != nil {
  106. err = errors.WithStack(err)
  107. return
  108. }
  109. res = make(map[int64][]*model.VipChangeHistory)
  110. defer rows.Close()
  111. for rows.Next() {
  112. r := new(model.VipChangeHistory)
  113. if err = rows.Scan(&r.ID, &r.Mid, &r.ChangeType, &r.ChangeTime, &r.Days, &r.OperatorID, &r.RelationID, &r.BatchID, &r.Remark, &r.BatchCodeID); err != nil {
  114. err = errors.WithStack(err)
  115. res = nil
  116. return
  117. }
  118. histories := res[r.Mid]
  119. histories = append(histories, r)
  120. res[r.Mid] = histories
  121. }
  122. err = rows.Err()
  123. return
  124. }
  125. //SelOldChangeHistoryMaps .
  126. func (d *Dao) SelOldChangeHistoryMaps(c context.Context, mids []int64) (res map[int64][]*model.VipChangeHistory, err error) {
  127. var rows *sql.Rows
  128. if rows, err = d.oldDb.Query(c, fmt.Sprintf(_selOldChangeHistoryByMidsSQL, xstr.JoinInts(mids))); err != nil {
  129. err = errors.WithStack(err)
  130. return
  131. }
  132. res = make(map[int64][]*model.VipChangeHistory)
  133. defer rows.Close()
  134. for rows.Next() {
  135. r := new(model.VipChangeHistory)
  136. if err = rows.Scan(&r.ID, &r.Mid, &r.ChangeType, &r.ChangeTime, &r.Days, &r.Month, &r.OperatorID, &r.RelationID, &r.BatchID, &r.Remark, &r.BatchCodeID); err != nil {
  137. err = errors.WithStack(err)
  138. res = nil
  139. return
  140. }
  141. histories := res[r.Mid]
  142. histories = append(histories, r)
  143. res[r.Mid] = histories
  144. }
  145. err = rows.Err()
  146. return
  147. }
  148. //SelOldChangeHistoryMaxID .
  149. func (d *Dao) SelOldChangeHistoryMaxID(c context.Context) (id int64, err error) {
  150. var row = d.oldDb.QueryRow(c, _selOldMaxIDSQL)
  151. if err = row.Scan(&id); err != nil {
  152. log.Error("SelMaxID row.Scan() error(%v)", err)
  153. return
  154. }
  155. return
  156. }
  157. //SelChangeHistoryMaxID .
  158. func (d *Dao) SelChangeHistoryMaxID(c context.Context) (id int64, err error) {
  159. var row = d.db.QueryRow(c, _selMaxIDSQL)
  160. if err = row.Scan(&id); err != nil {
  161. log.Error("SelMaxID row.Scan() error(%v)", err)
  162. return
  163. }
  164. return
  165. }
  166. // VipStatus get user vip status.
  167. func (d *Dao) VipStatus(c context.Context, mid int64) (res *model.VipUserInfo, err error) {
  168. row := d.db.QueryRow(c, _getVipStatus, mid)
  169. res = new(model.VipUserInfo)
  170. if err = row.Scan(&res.ID, &res.Mid, &res.Status); err != nil {
  171. if err == sql.ErrNoRows {
  172. err = nil
  173. res = nil
  174. } else {
  175. err = errors.Wrapf(err, "d.VipStatus(%d)", mid)
  176. }
  177. }
  178. return
  179. }
  180. //SelMaxID get max id
  181. func (d *Dao) SelMaxID(c context.Context) (mID int, err error) {
  182. var row = d.db.QueryRow(c, _SelMaxID)
  183. if err = row.Scan(&mID); err != nil {
  184. log.Error("SelMaxID row.Scan() error(%v)", err)
  185. return
  186. }
  187. return
  188. }
  189. //UpdateVipUserInfo update vip user info by id
  190. func (d *Dao) UpdateVipUserInfo(tx *sql.Tx, r *model.VipUserInfo) (a int64, err error) {
  191. var res xsql.Result
  192. if res, err = tx.Exec(_UpdateVipUserInfoByID, r.PayChannelID, r.PayType, r.RecentTime, r.OverdueTime, r.AnnualVipOverdueTime, r.Type, r.Status, r.Mid); err != nil {
  193. log.Error("UpdateVipUserInfo: db.Exec(%v) error(%v)", r, err)
  194. return
  195. }
  196. if a, err = res.RowsAffected(); err != nil {
  197. log.Error("UpdateVipUserInfo: res.RowsAffected error(%v)", err)
  198. }
  199. return
  200. }
  201. // UpdateVipUser update vip user info
  202. func (d *Dao) UpdateVipUser(c context.Context, mid int64, status, vipType int8, payType int8) (eff int64, err error) {
  203. var res xsql.Result
  204. if res, err = d.oldDb.Exec(c, _updateVipUserSQL, status, vipType, payType, mid); err != nil {
  205. err = errors.Wrapf(err, "d.UpdateVipStatus(%d)", mid)
  206. return
  207. }
  208. if eff, err = res.RowsAffected(); err != nil {
  209. err = errors.Wrapf(err, "d.UpdateVipStatus(%d) res.RowsAffected", mid)
  210. }
  211. return
  212. }
  213. //UpdateVipStatus update vip status info by mid
  214. func (d *Dao) UpdateVipStatus(c context.Context, mid int64, status int) (eff int64, err error) {
  215. var res xsql.Result
  216. if res, err = d.db.Exec(c, _updateVipStatus, status, mid); err != nil {
  217. err = errors.Wrapf(err, "d.UpdateVipStatus(%d)", mid)
  218. return
  219. }
  220. if eff, err = res.RowsAffected(); err != nil {
  221. err = errors.Wrapf(err, "d.UpdateVipStatus(%d) res.RowsAffected", mid)
  222. }
  223. return
  224. }
  225. // DupUserDiscountHistory add user discount history.
  226. func (d *Dao) DupUserDiscountHistory(tx *sql.Tx, r *model.VipUserDiscountHistory) (a int64, err error) {
  227. var res xsql.Result
  228. if res, err = tx.Exec(_dupUserDiscountHistory, r.Mid, r.DiscountID, r.OrderNo, r.Status); err != nil {
  229. err = errors.WithStack(err)
  230. return
  231. }
  232. if a, err = res.RowsAffected(); err != nil {
  233. err = errors.WithStack(err)
  234. }
  235. return
  236. }
  237. //AddUserInfo add user info.
  238. func (d *Dao) AddUserInfo(tx *sql.Tx, r *model.VipUserInfo) (eff int64, err error) {
  239. var res xsql.Result
  240. if res, err = tx.Exec(_insertUserInfo, r.Mid, r.Type, r.PayType, r.Status, r.StartTime, r.RecentTime, r.OverdueTime, r.AnnualVipOverdueTime, r.PayChannelID, r.IosOverdueTime, r.Ver); err != nil {
  241. log.Error("AddUserDiscountHistory d.db.exec(%v) error(%v)", r, err)
  242. return
  243. }
  244. if eff, err = res.RowsAffected(); err != nil {
  245. log.Error("AddUserDiscountHistory RowsAffected(%v)", err)
  246. }
  247. return
  248. }
  249. //SelVipUserInfo sel vipuser info
  250. func (d *Dao) SelVipUserInfo(c context.Context, mid int64) (r *model.VipUserInfo, err error) {
  251. var row = d.db.QueryRow(c, _selVipUserInfoByMid, mid)
  252. r = new(model.VipUserInfo)
  253. if err = row.Scan(&r.ID, &r.Mid, &r.Ver, &r.Type, &r.PayType, &r.Status, &r.StartTime, &r.OverdueTime, &r.AnnualVipOverdueTime, &r.RecentTime); err != nil {
  254. if err == sql.ErrNoRows {
  255. r = nil
  256. err = nil
  257. } else {
  258. log.Error("row.Scan() error(%v)", err)
  259. }
  260. }
  261. return
  262. }
  263. //UpdateUserInfo add user info
  264. func (d *Dao) UpdateUserInfo(tx *sql.Tx, r *model.VipUserInfo) (eff int64, err error) {
  265. var res xsql.Result
  266. if res, err = tx.Exec(_updateUserInfo, r.Type, r.PayType, r.Status, r.StartTime, r.RecentTime, r.OverdueTime, r.AnnualVipOverdueTime, r.PayChannelID, r.IosOverdueTime, r.Ver, r.Mid, r.OldVer); err != nil {
  267. err = errors.WithStack(err)
  268. return
  269. }
  270. if eff, err = res.RowsAffected(); err != nil {
  271. err = errors.WithStack(err)
  272. }
  273. return
  274. }
  275. //SelUserDiscountMaxID sel discount maxID
  276. func (d *Dao) SelUserDiscountMaxID(c context.Context) (maxID int, err error) {
  277. var row = d.db.QueryRow(c, _selDiscountMaxID)
  278. if err = row.Scan(&maxID); err != nil {
  279. err = errors.WithStack(err)
  280. d.errProm.Incr("db_scan")
  281. return
  282. }
  283. return
  284. }
  285. //SelUserDiscountHistorys sel user discount hsitorys
  286. func (d *Dao) SelUserDiscountHistorys(c context.Context, sID, eID, discountID int) (res []*model.VipUserDiscountHistory, err error) {
  287. var rows *sql.Rows
  288. if rows, err = d.db.Query(c, _SelUserDiscountHistorys, sID, eID, discountID); err != nil {
  289. log.Error("SelUserDiscountHistorys d.db.query(sID:%d,eID:%d,discountID:%d) error(%+v)", sID, eID, discountID, err)
  290. return
  291. }
  292. defer rows.Close()
  293. for rows.Next() {
  294. r := new(model.VipUserDiscountHistory)
  295. if err = rows.Scan(&r.Mid, &r.DiscountID); err != nil {
  296. log.Error("SelUserDiscountHistorys rows.scan() error(%v)", err)
  297. res = nil
  298. return
  299. }
  300. res = append(res, r)
  301. }
  302. return
  303. }
  304. //SelOldUserInfoMaxID sel old userinfo maxID
  305. func (d *Dao) SelOldUserInfoMaxID(c context.Context) (maxID int, err error) {
  306. var row = d.oldDb.QueryRow(c, _selUserInfoMaxID)
  307. if err = row.Scan(&maxID); err != nil {
  308. err = errors.WithStack(err)
  309. d.errProm.Incr("db_scan")
  310. return
  311. }
  312. return
  313. }
  314. // SelUserInfoMaxID select userinfo maxID.
  315. func (d *Dao) SelUserInfoMaxID(c context.Context) (maxID int, err error) {
  316. var row = d.db.QueryRow(c, _selUserInfoMaxID)
  317. if err = row.Scan(&maxID); err != nil {
  318. err = errors.WithStack(err)
  319. d.errProm.Incr("db_scan")
  320. return
  321. }
  322. return
  323. }
  324. //SelUserInfos sel user infos
  325. func (d *Dao) SelUserInfos(c context.Context, sID, eID int) (res []*model.VipUserInfo, err error) {
  326. var rows *sql.Rows
  327. if rows, err = d.db.Query(c, _SelVipUserInfos, sID, eID); err != nil {
  328. log.Error("SelUserDiscountHistorys d.db.query(sID:%v,eID:%v) error(%v)", sID, eID, err)
  329. return
  330. }
  331. for rows.Next() {
  332. r := new(model.VipUserInfo)
  333. if err = rows.Scan(&r.Mid, &r.Type, &r.PayType, &r.Status, &r.StartTime, &r.RecentTime, &r.OverdueTime, &r.AnnualVipOverdueTime); err != nil {
  334. log.Error("SelUserDiscountHistorys rows.scan() error(%v)", err)
  335. res = nil
  336. return
  337. }
  338. res = append(res, r)
  339. }
  340. return
  341. }
  342. //SelVipList sel vipuserinfo list
  343. func (d *Dao) SelVipList(c context.Context, id, endID int, ot string) (res []*model.VipUserInfo, err error) {
  344. var rows *sql.Rows
  345. if rows, err = d.oldDb.Query(c, _SelVipList, id, endID, ot); err != nil {
  346. log.Error("SelVipList db.query(id:%v,ot:%v,endID:%v) error(%v)", id, ot, endID, err)
  347. return
  348. }
  349. defer rows.Close()
  350. for rows.Next() {
  351. r := new(model.VipUserInfo)
  352. if err = rows.Scan(&r.ID, &r.Mid, &r.Type, &r.Status, &r.OverdueTime, &r.AnnualVipOverdueTime, &r.PayType, &r.PayChannelID); err != nil {
  353. log.Error("row.Scan() error(%v)", err)
  354. res = nil
  355. return
  356. }
  357. res = append(res, r)
  358. }
  359. return
  360. }
  361. //SelVipUsers .
  362. func (d *Dao) SelVipUsers(c context.Context, id, endID int, startTime, endTime time.Time) (res []*model.VipUserInfo, err error) {
  363. var rows *sql.Rows
  364. if rows, err = d.db.Query(c, _selVipUsersSQL, id, endID, startTime, endTime); err != nil {
  365. log.Error("SelVipList db.query(id:%v,endID:%v,st:%v,et:%v) error(%v)", id, endID, startTime, endTime, err)
  366. return
  367. }
  368. defer rows.Close()
  369. for rows.Next() {
  370. r := new(model.VipUserInfo)
  371. if err = rows.Scan(&r.ID, &r.Mid, &r.Type, &r.Status, &r.OverdueTime, &r.AnnualVipOverdueTime); err != nil {
  372. log.Error("row.Scan() error(%v)", err)
  373. res = nil
  374. return
  375. }
  376. res = append(res, r)
  377. }
  378. return
  379. }
  380. //SelVipUserInfos sel vipuser info datas
  381. func (d *Dao) SelVipUserInfos(c context.Context, id, endID int, startTime, endTime time.Time, status int) (res []int64, err error) {
  382. var rows *sql.Rows
  383. if rows, err = d.db.Query(c, _selVipUserInfoSQL, id, endID, status, startTime.Time().Format("2006-01-02"), endTime.Time().Format("2006-01-02")); err != nil {
  384. log.Error("SelVipList db.query(id:%v,endID:%v,st:%v,et:%v) error(%v)", id, endID, startTime, endTime, err)
  385. return
  386. }
  387. defer rows.Close()
  388. for rows.Next() {
  389. var r int64
  390. if err = rows.Scan(&r); err != nil {
  391. log.Error("row.Scan() error(%v)", err)
  392. res = nil
  393. return
  394. }
  395. res = append(res, r)
  396. }
  397. return
  398. }
  399. //OldVipInfo get old user by mid.
  400. func (d *Dao) OldVipInfo(c context.Context, mid int64) (r *model.VipUserInfoOld, err error) {
  401. var row = d.oldDb.QueryRow(c, _selOldVipUserInfo, mid)
  402. r = new(model.VipUserInfoOld)
  403. if err = row.Scan(&r.Mid, &r.Type, &r.IsAutoRenew, &r.AutoRenewed, &r.Status, &r.RecentTime, &r.StartTime, &r.OverdueTime,
  404. &r.AnnualVipOverdueTime, &r.IosOverdueTime, &r.Ver, &r.PayChannelID); err != nil {
  405. if err == sql.ErrNoRows {
  406. r = nil
  407. err = nil
  408. } else {
  409. err = errors.WithStack(err)
  410. d.errProm.Incr("row_scan_db")
  411. }
  412. }
  413. return
  414. }
  415. // SelEffectiveScopeVipList get vip list by id scope.
  416. func (d *Dao) SelEffectiveScopeVipList(c context.Context, id, endID int) (res []*model.VipInfoDB, err error) {
  417. var rows *sql.Rows
  418. if rows, err = d.db.Query(c, _SelEffectiveScopeVipList, id, endID); err != nil {
  419. err = errors.WithStack(err)
  420. return
  421. }
  422. defer rows.Close()
  423. for rows.Next() {
  424. r := new(model.VipInfoDB)
  425. if err = rows.Scan(&r.ID, &r.Mid, &r.Ver, &r.Type, &r.PayType, &r.PayChannelID, &r.Status, &r.StartTime, &r.RecentTime, &r.OverdueTime, &r.AnnualVipOverdueTime,
  426. &r.IosOverdueTime, &r.Ctime); err != nil {
  427. err = errors.WithStack(err)
  428. res = nil
  429. return
  430. }
  431. res = append(res, r)
  432. }
  433. err = rows.Err()
  434. return
  435. }
  436. // SelVipByIds select vip by ids .
  437. func (d *Dao) SelVipByIds(c context.Context, ids []int64) (res map[int64]*model.VipUserInfo, err error) {
  438. var (
  439. rows *sql.Rows
  440. idStr = xstr.JoinInts(ids)
  441. )
  442. res = make(map[int64]*model.VipUserInfo, len(ids))
  443. if rows, err = d.db.Query(c, fmt.Sprintf(_selVipByMidsSQL, idStr)); err != nil {
  444. err = errors.WithStack(err)
  445. return
  446. }
  447. defer rows.Close()
  448. for rows.Next() {
  449. r := new(model.VipUserInfo)
  450. if err = rows.Scan(&r.ID, &r.Mid, &r.Type, &r.Status, &r.RecentTime, &r.StartTime, &r.OverdueTime, &r.AnnualVipOverdueTime, &r.PayType, &r.PayChannelID, &r.IosOverdueTime, &r.Ctime, &r.Mtime, &r.Ver); err != nil {
  451. err = errors.WithStack(err)
  452. res = nil
  453. return
  454. }
  455. res[r.Mid] = r
  456. }
  457. return
  458. }
  459. //SelOldUserInfoMaps sel old user info map.
  460. func (d *Dao) SelOldUserInfoMaps(c context.Context, sID, eID int) (res map[int64]*model.VipUserInfoOld, err error) {
  461. var (
  462. rows *sql.Rows
  463. )
  464. res = make(map[int64]*model.VipUserInfoOld, eID-sID)
  465. if rows, err = d.oldDb.Query(c, _SelOldVipUserInfos, sID, eID); err != nil {
  466. err = errors.WithStack(err)
  467. return
  468. }
  469. for rows.Next() {
  470. r := new(model.VipUserInfoOld)
  471. if err = rows.Scan(&r.Mid, &r.Type, &r.IsAutoRenew, &r.AutoRenewed, &r.Status, &r.RecentTime, &r.StartTime, &r.OverdueTime, &r.AnnualVipOverdueTime, &r.Ver, &r.IosOverdueTime, &r.PayChannelID, &r.Ctime, &r.Mtime); err != nil {
  472. err = errors.WithStack(err)
  473. res = nil
  474. return
  475. }
  476. res[r.Mid] = r
  477. }
  478. return
  479. }