mysql.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "fmt"
  6. "strconv"
  7. "time"
  8. "go-common/app/admin/main/vip/model"
  9. "go-common/library/database/sql"
  10. "go-common/library/log"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _SelPoolQueryCount = "SELECT COUNT(1) count from vip_resource_pool WHERE 1=1"
  15. _SelBusinessByID = "SELECT id,business_name,business_type,status,app_key,secret,contacts,contacts_number,ctime,mtime FROM vip_business_info WHERE id=?"
  16. _SelBusinessByQuery = "SELECT id,business_name,business_type,status,app_key,secret,contacts,contacts_number,ctime,mtime FROM vip_business_info WHERE 1=1 "
  17. _SelPoolByQuery = "SELECT id,pool_name,business_id,reason,code_expire_time,start_time,end_time,contacts,contacts_number FROM vip_resource_pool WHERE 1=1"
  18. _SelPoolByName = "SELECT id,pool_name,business_id,reason,code_expire_time,start_time,end_time,contacts,contacts_number FROM vip_resource_pool WHERE pool_name = ?"
  19. _SelPoolByID = "SELECT id,pool_name,business_id,reason,code_expire_time,start_time,end_time,contacts,contacts_number FROM vip_resource_pool WHERE id=?"
  20. _AddPool = "INSERT INTO vip_resource_pool(pool_name,business_id,reason,code_expire_time,start_time,end_time,contacts,contacts_number) VALUES(?,?,?,?,?,?,?,?)"
  21. _UpdatePool = "UPDATE vip_resource_pool SET pool_name=?,business_id=?,reason=?,code_expire_time=?,start_time=?,end_time=?,contacts=?,contacts_number=? WHERE id = ?"
  22. _SelBatchByPoolID = "SELECT id,pool_id,unit,count,ver,start_time,end_time,surplus_count,code_use_count,direct_use_count FROM vip_resource_batch WHERE pool_id=?"
  23. _SelBatchByID = "SELECT id,pool_id,unit,count,ver,start_time,end_time,surplus_count,code_use_count,direct_use_count FROM vip_resource_batch WHERE id=?"
  24. _AddBatch = "INSERT INTO vip_resource_batch(pool_id,unit,count,ver,start_time,end_time,surplus_count,code_use_count,direct_use_count) VALUES(?,?,?,?,?,?,?,?,?)"
  25. _UpdateBatch = "UPDATE vip_resource_batch SET count = ?,ver=?,start_time=?,end_time=?,surplus_count=? WHERE id = ? AND ver=?"
  26. _UseBatch = "UPDATE vip_resource_batch SET ver=?,surplus_count = ?,direct_use_count=? WHERE id =? AND ver=?"
  27. _allVersionSQL = "SELECT `id`,`platform_id`,`version`,`tip`,`operator`,`link` FROM `vip_app_version`;"
  28. _updateVersionSQL = "UPDATE `vip_app_version` SET %s WHERE `id` = ?;"
  29. _businessInfosSQL = "SELECT id,business_name,business_type,status,app_key,contacts,contacts_number,ctime,mtime FROM vip_business_info WHERE 1=1 "
  30. _businessCountSQL = "SELECT COUNT(1) FROM vip_business_info WHERE 1=1"
  31. _addBusinessSQL = "INSERT INTO `vip_business_info` (`business_name`,`business_type`,`status`,`app_key`,`secret`,`contacts`,`contacts_number`) VALUES (?,?,?,?,?,?,?);"
  32. _updateBusinessSQL = "UPDATE `vip_business_info` SET `business_name` = ?,`business_type` = ?,`status` = ?,`app_key` = ?,`secret` = ?,`contacts` = ?,`contacts_number` = ? WHERE `id` = ?;"
  33. _allMonth = "SELECT id,month,month_type,operator,status,mtime FROM vip_month WHERE deleted = 0"
  34. _getMonth = "SELECT id,month,month_type,operator,status,mtime FROM vip_month WHERE id=?"
  35. _updateMonthStatus = "UPDATE vip_month SET status=?,operator=? WHERE id=?"
  36. _allMonthPrice = "SELECT id,month_id,month_type,money,selected,first_discount_money,discount_money,start_time,end_time,remark,operator FROM vip_month_price WHERE month_id=?"
  37. _monthPriceSQL = "SELECT id,month_id,month_type,money,selected,first_discount_money,discount_money,start_time,end_time,remark,operator FROM vip_month_price WHERE id=?"
  38. _addMonthPrice = "INSERT INTO vip_month_price (month_id,month_type,money,first_discount_money,discount_money,start_time,end_time,remark,operator) VALUES (?,?,?,?,?,?,?,?,?)"
  39. _editMonthPrice = "UPDATE vip_month_price SET month_type=?,money=?,first_discount_money=?,discount_money=?,start_time=?,end_time=?,remark=?,operator=? WHERE id=?"
  40. //resouce SQL
  41. _addBatchCodeSQL = "INSERT INTO vip_resource_batch_code(business_id,pool_id,status,batch_name,reason,unit,count,surplus_count,price,start_time,end_time,contacts,contacts_number,type,limit_day,max_count,operator) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
  42. _updateBatchCodeSQL = "UPDATE vip_resource_batch_code SET status=?,batch_name=?,reason=?,price=?,contacts=?,contacts_number=?,type=?,limit_day=?,max_count=?,operator=? WHERE id=?"
  43. _selBatchCodeIDSQL = "SELECT id FROM vip_resource_batch_code WHERE 1=1"
  44. _selBatchCodeSQL = "SELECT id,business_id,pool_id,status,type,limit_day,max_count,batch_name,reason,unit,count,surplus_count,price,start_time,end_time,contacts,contacts_number,ctime FROM vip_resource_batch_code WHERE 1=1 "
  45. _selBatchCodeByIDSQL = "SELECT id,business_id,pool_id,status,type,limit_day,max_count,batch_name,reason,unit,count,surplus_count,price,start_time,end_time,contacts,contacts_number,ctime FROM vip_resource_batch_code WHERE id = ?"
  46. _selBatchCodeCountSQL = "SELECT COUNT(1) FROM vip_resource_batch_code WHERE 1=1 "
  47. _selBatchCodeByNameSQL = "SELECT id,business_id,pool_id,status,type,limit_day,max_count,batch_name,reason,unit,count,surplus_count,price,start_time,end_time,contacts,contacts_number,ctime FROM vip_resource_batch_code WHERE batch_name = ?"
  48. _selBatchCodesSQL = "SELECT id,business_id,pool_id,status,type,limit_day,max_count,batch_name,reason,unit,count,surplus_count,price,start_time,end_time,contacts,contacts_number,ctime FROM vip_resource_batch_code WHERE 1=1 AND id in (%v)"
  49. _batchAddCodeSQL = "INSERT INTO vip_resource_code(batch_code_id,status,code) VALUES"
  50. _updateCodeSQL = "UPDATE vip_resource_code SET status=? WHERE id = ?"
  51. _selCodeSQL = "SELECT id,batch_code_id,status,code,mid,use_time,ctime FROM vip_resource_code WHERE 1=1"
  52. _selCodeByIDSQL = "SELECT id,batch_code_id,status,code,mid,use_time,ctime FROM vip_resource_code WHERE id = ?"
  53. //pushData
  54. _selPushDataCountSQL = "SELECT COUNT(1) FROM vip_push_data WHERE 1=1 "
  55. _selPushDataSQL = "SELECT id,disable_type,`group_name`,title,content,push_total_count,pushed_count,progress_status,`status`,platform,link_type,link_url,error_code,expired_day_start,expired_day_end,effect_start_date,effect_end_date,push_start_time,push_end_time,operator FROM vip_push_data WHERE 1=1 "
  56. _selPushDataByIDSQL = "SELECT id,disable_type,`group_name`,title,content,push_total_count,pushed_count,progress_status,`status`,platform,link_type,link_url,error_code,expired_day_start,expired_day_end,effect_start_date,effect_end_date,push_start_time,push_end_time,operator FROM vip_push_data WHERE id=?"
  57. _addPushDataSQL = "INSERT INTO vip_push_data(`group_name`,title,content,push_total_count,pushed_count,progress_status,`status`,platform,link_type,link_url,expired_day_start,expired_day_end,effect_start_date,effect_end_date,push_start_time,push_end_time,operator) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
  58. _updatePushDataSQL = "UPDATE vip_push_data SET group_name=?,title=?,content=?,push_total_count=?,progress_status=?,platform=?,link_type=?,link_url=?,expired_day_start=?,expired_day_end=?,effect_start_date=?,effect_end_date=?,push_start_time=?,push_end_time=?,operator=? WHERE id=?"
  59. _delPushDataSQL = "DELETE FROM vip_push_data WHERE id = ?"
  60. _disablePushDataSQL = "UPDATE vip_push_data SET disable_type=1,progress_status=?,push_total_count=?,effect_end_date=? WHERE id=?"
  61. //order
  62. _vipOrderListSQL = "SELECT id,order_no,app_id,platform,order_type,mid,to_mid,buy_months,money,refund_amount,status,pay_type,recharge_bp,third_trade_no,ver,payment_time,ctime,mtime,app_sub_id FROM vip_pay_order WHERE 1=1"
  63. _vipOrderCountSQL = "SELECT COUNT(1) FROM vip_pay_order WHERE 1=1"
  64. _vipOrderSQL = "SELECT id,order_no,app_id,platform,order_type,mid,to_mid,buy_months,money,refund_amount,status,pay_type,recharge_bp,third_trade_no,ver,payment_time,ctime,mtime,app_sub_id FROM vip_pay_order WHERE order_no = ? "
  65. _addOrderLogSQL = "INSERT INTO vip_pay_order_log(order_no,refund_id,refund_amount,operator,mid,status) VALUES(?,?,?,?,?,?)"
  66. _getUserChangeHistorySQL = "SELECT id,mid,change_type,change_time,days,operator_id,relation_id,batch_id,remark,ctime,mtime FROM vip_user_change_history WHERE 1=1 "
  67. _getUserChangeHistoryCountSQL = "SELECT COUNT(1) FROM `vip_user_change_history` WHERE 1=1 "
  68. _defpn = 1
  69. _defps = 20
  70. _maxps = 100
  71. )
  72. func (d *Dao) joinPoolCondition(sql string, q *model.ResoucePoolBo, pn, ps int) string {
  73. if len(q.PoolName) > 0 {
  74. sql += " and pool_name like '%" + q.PoolName + "%'"
  75. }
  76. if q.BusinessID > 0 {
  77. sql += " and business_id = " + strconv.Itoa(q.BusinessID)
  78. }
  79. if q.StartTime > 0 {
  80. sql += " and start_time >= '" + q.StartTime.Time().Format("2006-01-02 15:04:05") + "'"
  81. }
  82. if q.EndTime > 0 {
  83. sql += " and end_time <='" + q.EndTime.Time().Format("2006-01-02 15:04:05") + "'"
  84. }
  85. if q.ID > 0 || q.ID == -1 {
  86. sql += " and id = " + strconv.Itoa(q.ID)
  87. }
  88. if pn > 0 {
  89. if ps <= 0 {
  90. ps = 20
  91. }
  92. offer := (q.PN - 1) * q.PS
  93. sql += " limit " + strconv.Itoa(offer) + ", " + strconv.Itoa(q.PS)
  94. }
  95. return sql
  96. }
  97. func (d *Dao) joinHistoryCondition(sql string, u *model.UserChangeHistoryReq, iscount bool) string {
  98. if u.Mid > 0 {
  99. sql += " AND mid = " + fmt.Sprintf("%d", u.Mid)
  100. }
  101. if u.ChangeType > 0 {
  102. sql += " AND change_type = " + fmt.Sprintf("%d", u.ChangeType)
  103. }
  104. if u.StartChangeTime > 0 {
  105. stime := time.Unix(u.StartChangeTime, 0).Format("2006-01-02 15:04:05")
  106. sql += " AND change_time >= '" + stime + "'"
  107. }
  108. if u.EndChangeTime > 0 {
  109. etime := time.Unix(u.EndChangeTime, 0).Format("2006-01-02 15:04:05")
  110. sql += " AND change_time <= '" + etime + "'"
  111. }
  112. if u.BatchID > 0 {
  113. sql += " AND batch_id = " + fmt.Sprintf("%d", u.BatchID)
  114. }
  115. if len(u.RelationID) > 0 {
  116. sql += " AND relation_id = " + fmt.Sprintf("'%v'", u.RelationID)
  117. }
  118. if iscount {
  119. return sql
  120. }
  121. if u.Pn == 0 {
  122. u.Pn = _defpn
  123. }
  124. if u.Ps == 0 {
  125. u.Ps = _defps
  126. }
  127. offer := (u.Pn - 1) * u.Ps
  128. sql += " ORDER BY id DESC LIMIT " + strconv.Itoa(offer) + ", " + strconv.Itoa(u.Ps)
  129. return sql
  130. }
  131. // SelPoolByName sel pool by name
  132. func (d *Dao) SelPoolByName(c context.Context, name string) (r *model.VipResourcePool, err error) {
  133. var row *sql.Row
  134. if row = d.db.QueryRow(c, _SelPoolByName, name); err != nil {
  135. log.Error("SelPoolByName db.query() error(%v)", err)
  136. return
  137. }
  138. r = new(model.VipResourcePool)
  139. if err = row.Scan(&r.ID, &r.PoolName, &r.BusinessID, &r.Reason, &r.CodeExpireTime, &r.StartTime, &r.EndTime, &r.Contacts, &r.ContactsNumber); err != nil {
  140. if err == sql.ErrNoRows {
  141. err = nil
  142. r = nil
  143. } else {
  144. log.Error("row.Scan() error(%v)", err)
  145. }
  146. return
  147. }
  148. return
  149. }
  150. // SelCountPool sel count Pool
  151. func (d *Dao) SelCountPool(c context.Context, r *model.ResoucePoolBo) (count int, err error) {
  152. var row *sql.Row
  153. if row = d.db.QueryRow(c, d.joinPoolCondition(_SelPoolQueryCount, r, 0, 0)); err != nil {
  154. log.Error("SelCountPool db.query() error(%v)", err)
  155. return
  156. }
  157. if err = row.Scan(&count); err != nil {
  158. log.Error("row.scan() error(%v)", err)
  159. return
  160. }
  161. return
  162. }
  163. // SelPool sel pool by query condition
  164. func (d *Dao) SelPool(c context.Context, r *model.ResoucePoolBo, pn, ps int) (res []*model.VipResourcePool, err error) {
  165. var rows *sql.Rows
  166. if rows, err = d.db.Query(c, d.joinPoolCondition(_SelPoolByQuery, r, pn, ps)); err != nil {
  167. log.Error("SelPool db.query() error(%v)", err)
  168. return
  169. }
  170. defer rows.Close()
  171. for rows.Next() {
  172. r := new(model.VipResourcePool)
  173. if err = rows.Scan(&r.ID, &r.PoolName, &r.BusinessID, &r.Reason, &r.CodeExpireTime, &r.StartTime, &r.EndTime, &r.Contacts, &r.ContactsNumber); err != nil {
  174. log.Error("row.scan() error(%v)", err)
  175. res = nil
  176. return
  177. }
  178. res = append(res, r)
  179. }
  180. return
  181. }
  182. // SelPoolRow sel pool by id
  183. func (d *Dao) SelPoolRow(c context.Context, id int) (r *model.VipResourcePool, err error) {
  184. var row *sql.Row
  185. if row = d.db.QueryRow(c, _SelPoolByID, id); err != nil {
  186. log.Error("SelPoolRow db.query() error(%v)", err)
  187. return
  188. }
  189. r = new(model.VipResourcePool)
  190. if err = row.Scan(&r.ID, &r.PoolName, &r.BusinessID, &r.Reason, &r.CodeExpireTime, &r.StartTime, &r.EndTime, &r.Contacts, &r.ContactsNumber); err != nil {
  191. if err == sql.ErrNoRows {
  192. err = nil
  193. r = nil
  194. } else {
  195. log.Error("row.Scan() error(%v)", err)
  196. }
  197. return
  198. }
  199. return
  200. }
  201. // AddPool add pool
  202. func (d *Dao) AddPool(c context.Context, r *model.ResoucePoolBo) (a int64, err error) {
  203. var res xsql.Result
  204. if res, err = d.db.Exec(c, _AddPool, r.PoolName, r.BusinessID, r.Reason, r.CodeExpireTime, r.StartTime, r.EndTime, r.Contacts, r.ContactsNumber); err != nil {
  205. log.Error("AddPool d.db.exec(%v) error(%v)", r, err)
  206. return
  207. }
  208. if a, err = res.RowsAffected(); err != nil {
  209. log.Error("AddPool RowsAffected() error(%v)", err)
  210. return
  211. }
  212. return
  213. }
  214. // UpdatePool update pool
  215. func (d *Dao) UpdatePool(c context.Context, r *model.ResoucePoolBo) (a int64, err error) {
  216. var res xsql.Result
  217. if res, err = d.db.Exec(c, _UpdatePool, r.PoolName, r.BusinessID, r.Reason, r.CodeExpireTime, r.StartTime, r.EndTime, r.Contacts, r.ContactsNumber, r.ID); err != nil {
  218. log.Error("UpdatePool d.db.exec(%v) error(%v)", r, err)
  219. return
  220. }
  221. if a, err = res.RowsAffected(); err != nil {
  222. log.Error("UpdatePool RowsAffected() error(%v)", err)
  223. return
  224. }
  225. return
  226. }
  227. // SelBatchRow sel batch by id
  228. func (d *Dao) SelBatchRow(c context.Context, id int) (r *model.VipResourceBatch, err error) {
  229. var row *sql.Row
  230. if row = d.db.QueryRow(c, _SelBatchByID, id); err != nil {
  231. log.Error("SelBatchRow db.query() error(%v)", err)
  232. return
  233. }
  234. r = new(model.VipResourceBatch)
  235. if err = row.Scan(&r.ID, &r.PoolID, &r.Unit, &r.Count, &r.Ver, &r.StartTime, &r.EndTime, &r.SurplusCount, &r.CodeUseCount, &r.DirectUseCount); err != nil {
  236. if err == sql.ErrNoRows {
  237. err = nil
  238. r = nil
  239. } else {
  240. log.Error("row.Scan() error(%v)", err)
  241. }
  242. return
  243. }
  244. return
  245. }
  246. // SelBatchRows sel batch by poolID
  247. func (d *Dao) SelBatchRows(c context.Context, poolID int) (res []*model.VipResourceBatch, err error) {
  248. var rows *sql.Rows
  249. if rows, err = d.db.Query(c, _SelBatchByPoolID, poolID); err != nil {
  250. log.Error("SelBatchRows db.query() error(%v)", err)
  251. return
  252. }
  253. defer rows.Close()
  254. for rows.Next() {
  255. r := new(model.VipResourceBatch)
  256. if err = rows.Scan(&r.ID, &r.PoolID, &r.Unit, &r.Count, &r.Ver, &r.StartTime, &r.EndTime, &r.SurplusCount, &r.CodeUseCount, &r.DirectUseCount); err != nil {
  257. log.Error("row.scan() error(%v)", err)
  258. res = nil
  259. return
  260. }
  261. res = append(res, r)
  262. }
  263. return
  264. }
  265. // AddBatch add batch
  266. func (d *Dao) AddBatch(c context.Context, r *model.ResouceBatchBo) (a int64, err error) {
  267. var res xsql.Result
  268. if res, err = d.db.Exec(c, _AddBatch, r.PoolID, r.Unit, r.Count, 0, r.StartTime, r.EndTime, r.SurplusCount, r.CodeUseCount, r.DirectUseCount); err != nil {
  269. log.Error("AddBatch d.db.exec(%v) error(%v)", r, err)
  270. return
  271. }
  272. if a, err = res.RowsAffected(); err != nil {
  273. log.Error("AddBatch RowsAffected() error(%v)", err)
  274. return
  275. }
  276. return
  277. }
  278. // UpdateBatch update batch data
  279. func (d *Dao) UpdateBatch(c context.Context, r *model.VipResourceBatch, ver int) (a int64, err error) {
  280. var res xsql.Result
  281. if res, err = d.db.Exec(c, _UpdateBatch, r.Count, r.Ver, r.StartTime, r.EndTime, r.SurplusCount, r.ID, ver); err != nil {
  282. log.Error("UpdateBatch d.db.exec(%v) error(%v)", r, err)
  283. return
  284. }
  285. if a, err = res.RowsAffected(); err != nil {
  286. log.Error("UpdateBatch RowsAffected() error(%v)", err)
  287. return
  288. }
  289. return
  290. }
  291. // UseBatch use batch resouce
  292. func (d *Dao) UseBatch(tx *sql.Tx, r *model.VipResourceBatch, ver int) (a int64, err error) {
  293. var res xsql.Result
  294. if res, err = tx.Exec(_UseBatch, r.Ver, r.SurplusCount, r.DirectUseCount, r.ID, ver); err != nil {
  295. log.Error("UseBatch d.db.exec(%v) error(%v)", r, err)
  296. return
  297. }
  298. if a, err = res.RowsAffected(); err != nil {
  299. log.Error("UseBatch RowsAffected() error(%v)", err)
  300. return
  301. }
  302. return
  303. }
  304. // SelBusiness select businessInfo by id
  305. func (d *Dao) SelBusiness(c context.Context, id int) (r *model.VipBusinessInfo, err error) {
  306. var row = d.db.QueryRow(c, _SelBusinessByID, id)
  307. r = new(model.VipBusinessInfo)
  308. 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 {
  309. if err == sql.ErrNoRows {
  310. err = nil
  311. r = nil
  312. } else {
  313. log.Error("row.Scan() error(%v)", err)
  314. }
  315. }
  316. return
  317. }
  318. // SelBusinessByQuery .
  319. func (d *Dao) SelBusinessByQuery(c context.Context, arg *model.QueryBusinessInfo) (r *model.VipBusinessInfo, err error) {
  320. queryStr := ""
  321. if len(arg.Name) > 0 {
  322. queryStr += fmt.Sprintf(" AND business_name = '%v' ", arg.Name)
  323. }
  324. if len(arg.Appkey) > 0 {
  325. queryStr += fmt.Sprintf(" AND app_key = '%v' ", arg.Appkey)
  326. }
  327. var row = d.db.QueryRow(c, _SelBusinessByQuery+queryStr)
  328. r = new(model.VipBusinessInfo)
  329. 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 {
  330. if err == sql.ErrNoRows {
  331. err = nil
  332. r = nil
  333. } else {
  334. log.Error("row.Scan() error(%v)", err)
  335. }
  336. }
  337. return
  338. }
  339. // AllVersion all version.
  340. func (d *Dao) AllVersion(c context.Context) (res []*model.VipAppVersion, err error) {
  341. var rows *sql.Rows
  342. if rows, err = d.db.Query(c, _allVersionSQL); err != nil {
  343. err = errors.WithStack(err)
  344. return
  345. }
  346. defer rows.Close()
  347. for rows.Next() {
  348. r := new(model.VipAppVersion)
  349. if err = rows.Scan(&r.ID, &r.PlatformID, &r.Version, &r.Tip, &r.Operator, &r.Link); err != nil {
  350. err = errors.WithStack(err)
  351. res = nil
  352. return
  353. }
  354. res = append(res, r)
  355. }
  356. return
  357. }
  358. // UpdateVersion update version.
  359. func (d *Dao) UpdateVersion(c context.Context, v *model.VipAppVersion) (ret int64, err error) {
  360. var (
  361. sql string
  362. res xsql.Result
  363. )
  364. if len(v.Version) > 0 {
  365. sql += "`version` = '" + v.Version + "',"
  366. }
  367. if len(v.Tip) > 0 {
  368. sql += "`tip` = '" + v.Tip + "',"
  369. }
  370. if len(v.Link) > 0 {
  371. sql += "`link` = '" + v.Link + "',"
  372. }
  373. sql += "`operator` = '" + v.Operator + "'"
  374. if res, err = d.db.Exec(c, fmt.Sprintf(_updateVersionSQL, sql), v.ID); err != nil {
  375. err = errors.WithStack(err)
  376. return
  377. }
  378. if ret, err = res.RowsAffected(); err != nil {
  379. err = errors.WithStack(err)
  380. return
  381. }
  382. return
  383. }
  384. // BussinessList business infos.
  385. func (d *Dao) BussinessList(c context.Context, pn, ps, status int) (res []*model.VipBusinessInfo, err error) {
  386. var rows *sql.Rows
  387. sqlStr := _businessInfosSQL
  388. if status >= 0 {
  389. sqlStr += fmt.Sprintf(" AND status=%v", status)
  390. }
  391. if pn <= 0 {
  392. pn = _defpn
  393. }
  394. if pn <= 0 || pn > _maxps {
  395. ps = _defps
  396. }
  397. sqlStr += fmt.Sprintf(" ORDER BY id DESC limit %v,%v", (pn-1)*ps, ps)
  398. if rows, err = d.db.Query(c, sqlStr); err != nil {
  399. err = errors.WithStack(err)
  400. return
  401. }
  402. defer rows.Close()
  403. for rows.Next() {
  404. r := new(model.VipBusinessInfo)
  405. if err = rows.Scan(&r.ID, &r.BusinessName, &r.BusinessType, &r.Status, &r.AppKey,
  406. &r.Contacts, &r.ContactsNumber, &r.Ctime, &r.Mtime); err != nil {
  407. err = errors.WithStack(err)
  408. res = nil
  409. return
  410. }
  411. res = append(res, r)
  412. }
  413. return
  414. }
  415. // BussinessCount bussiness info count.
  416. func (d *Dao) BussinessCount(c context.Context, status int) (count int64, err error) {
  417. var row *sql.Row
  418. sqlstr := _businessCountSQL
  419. if status >= 0 {
  420. sqlstr += fmt.Sprintf(" AND status=%v", status)
  421. }
  422. if row = d.db.QueryRow(c, sqlstr); err != nil {
  423. err = errors.WithStack(err)
  424. d.errProm.Incr("query_db")
  425. return
  426. }
  427. if err = row.Scan(&count); err != nil {
  428. err = errors.WithStack(err)
  429. d.errProm.Incr("row_scan_db")
  430. return
  431. }
  432. return
  433. }
  434. // UpdateBusiness update business info.
  435. func (d *Dao) UpdateBusiness(c context.Context, r *model.VipBusinessInfo) (a int64, err error) {
  436. var res xsql.Result
  437. if res, err = d.db.Exec(c, _updateBusinessSQL, r.BusinessName, r.BusinessType, r.Status, r.AppKey, r.Secret, r.Contacts, r.ContactsNumber, r.ID); err != nil {
  438. err = errors.WithStack(err)
  439. d.errProm.Incr("exec_db")
  440. return
  441. }
  442. if a, err = res.RowsAffected(); err != nil {
  443. err = errors.WithStack(err)
  444. d.errProm.Incr("rows_affected_db")
  445. return
  446. }
  447. return
  448. }
  449. // AddBusiness add business info.
  450. func (d *Dao) AddBusiness(c context.Context, r *model.VipBusinessInfo) (a int64, err error) {
  451. var res xsql.Result
  452. if res, err = d.db.Exec(c, _addBusinessSQL, r.BusinessName, r.BusinessType, r.Status, r.AppKey, r.Secret, r.Contacts, r.ContactsNumber); err != nil {
  453. err = errors.WithStack(err)
  454. d.errProm.Incr("exec_db")
  455. return
  456. }
  457. if a, err = res.RowsAffected(); err != nil {
  458. err = errors.WithStack(err)
  459. d.errProm.Incr("rows_affected_db")
  460. return
  461. }
  462. return
  463. }
  464. // HistoryCount user change history.
  465. func (d *Dao) HistoryCount(c context.Context, u *model.UserChangeHistoryReq) (count int, err error) {
  466. var row *sql.Row
  467. if row = d.db.QueryRow(c, d.joinHistoryCondition(_getUserChangeHistoryCountSQL, u, true)); err != nil {
  468. err = errors.WithStack(err)
  469. d.errProm.Incr("query_db")
  470. return
  471. }
  472. if err = row.Scan(&count); err != nil {
  473. err = errors.WithStack(err)
  474. d.errProm.Incr("row_scan_db")
  475. return
  476. }
  477. return
  478. }
  479. // HistoryList history list.
  480. func (d *Dao) HistoryList(c context.Context, u *model.UserChangeHistoryReq) (res []*model.VipChangeHistory, err error) {
  481. var rows *sql.Rows
  482. if rows, err = d.db.Query(c, d.joinHistoryCondition(_getUserChangeHistorySQL, u, false)); err != nil {
  483. err = errors.WithStack(err)
  484. d.errProm.Incr("query_db")
  485. return
  486. }
  487. defer rows.Close()
  488. for rows.Next() {
  489. r := new(model.VipChangeHistory)
  490. if err = rows.Scan(&r.ID, &r.Mid, &r.ChangeType, &r.ChangeTime, &r.Days, &r.OperatorID, &r.RelationID, &r.BatchID, &r.Remark, &r.Ctime, &r.Mtime); err != nil {
  491. err = errors.WithStack(err)
  492. d.errProm.Incr("rows_scan_db")
  493. res = nil
  494. return
  495. }
  496. res = append(res, r)
  497. }
  498. return
  499. }