mysql.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "go-common/app/service/main/member/model"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/time"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _shard = 100
  15. // base
  16. _selBaseInfo = "SELECT mid,name,sex,face,sign,rank,birthday FROM user_base_%02d WHERE mid=? "
  17. _setSex = "INSERT INTO user_base_%02d(mid,sex) VALUES (?,?) ON DUPLICATE KEY UPDATE sex=?"
  18. _setName = "INSERT INTO user_base_%02d(mid,name) VALUES (?,?) ON DUPLICATE KEY UPDATE name=?"
  19. _setRank = "INSERT INTO user_base_%02d(mid,rank) VALUES (?,?) ON DUPLICATE KEY UPDATE rank=?"
  20. _setSign = "INSERT INTO user_base_%02d(mid,sign) VALUES (?,?) ON DUPLICATE KEY UPDATE sign=?"
  21. _setBirthday = "INSERT INTO user_base_%02d(mid,birthday) VALUES (?,?) ON DUPLICATE KEY UPDATE birthday=?"
  22. _setFace = "INSERT INTO user_base_%02d(mid,face) VALUES (?,?) ON DUPLICATE KEY UPDATE face=?"
  23. _setBase = `INSERT INTO user_base_%02d(mid,name,sex,face,sign,rank,birthday) VALUES (?,?,?,?,?,?,?)
  24. ON DUPLICATE KEY UPDATE name=?,sex=?,face=?,sign=?,rank=?,birthday=?`
  25. // _exp
  26. _setExp = "INSERT INTO user_exp_%02d (mid,exp) VALUES(?,?) ON DUPLICATE KEY UPDATE exp=VALUES(exp)"
  27. _updateExp = "UPDATE user_exp_%02d SET exp=exp+? WHERE mid=?"
  28. _selExp = "SELECT exp FROM user_exp_%02d where mid=?"
  29. //user_flag
  30. _selUserAttr = "SELECT flag FROM user_flag where mid =? and flag & ?= ?"
  31. _updateUserAttr = "INSERT INTO user_flag(mid,flag) VALUES(?,?) ON DUPLICATE KEY UPDATE flag=flag|?"
  32. // user_official
  33. _selOfficials = "SELECT mid,role,title,description FROM user_official WHERE role>0"
  34. // moral
  35. _selMoral = "SELECT moral,added,deducted,last_recover_date from user_moral where mid=?"
  36. _initMoral = `INSERT IGNORE INTO user_moral (mid,moral,added,deducted,last_recover_date) VALUES(?,?,?,?,?)`
  37. _updateMoral = `update user_moral set moral=moral+?,added=added+?, deducted=deducted + ? where mid = ?`
  38. _updateMoralRecoverDate = `update user_moral set last_recover_date = ? where mid = ?`
  39. // official
  40. _setOfficialDoc = `INSERT INTO user_official_doc (mid,name,state,role,title,description,extra,submit_source,submit_time) VALUES (?,?,?,?,?,?,?,?,?)
  41. ON DUPLICATE KEY UPDATE name=VALUES(name), state=VALUES(state), role=VALUES(role), title=VALUES(title), description=VALUES(description), extra=VALUES(extra), submit_source=VALUES(submit_source), submit_time=VALUES(submit_time)`
  42. _selOfficialDoc = "SELECT mid,name,state,role,title,description,reject_reason,extra FROM user_official_doc WHERE mid=?"
  43. _selOfficial = "SELECT role,title,description FROM user_official WHERE mid=? AND role>0"
  44. //official addit
  45. _setOfficialDocAddit = `INSERT INTO user_official_doc_addit (mid,property,vstring) VALUES (?,?,?) ON DUPLICATE KEY UPDATE vstring=VALUES(vstring)`
  46. // realname
  47. _selRealnameInfo = `SELECT id,mid,channel,realname,country,card_type,card,card_md5,status,reason,ctime,mtime FROM realname_info WHERE mid = ? LIMIT 1`
  48. _selRealnameInfoByCard = `SELECT id,mid,channel,realname,country,card_type,card,card_md5,status,reason,ctime,mtime FROM realname_info WHERE card_md5=? AND status in (0,1) LIMIT 1`
  49. _selRealnameInfoMidByCards = `SELECT mid,card_md5 FROM realname_info WHERE card_md5 IN (%s) AND status in (0,1)`
  50. _upsertRealnameInfo = `INSERT INTO realname_info (mid,channel,realname,country,card_type,card,card_md5,status,reason) VALUES (?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE channel=?,realname=?,country=?,card_type=?,card=?,card_md5=?,status=?,reason=?`
  51. _updateRealnameInfo = `UPDATE realname_info SET status=?,reason=? WHERE mid=?`
  52. _selRealnameApply = "SELECT id,mid,realname,country,card_type,card_num,card_md5,hand_img,front_img,back_img,status,operator,operator_id,operator_time,remark,remark_status,ctime,mtime FROM realname_apply WHERE mid = ? ORDER BY ID DESC LIMIT 1"
  53. _insertRealnameApply = `INSERT INTO realname_apply (mid,realname,country,card_type,card_num,card_md5,hand_img,front_img,back_img,status) VALUES (?,?,?,?,?,?,?,?,?,?)`
  54. _selRealnameApplyImg = "SELECT id,img_data,ctime,mtime FROM realname_apply_img WHERE id = ? LIMIT 1"
  55. _insertRealnameApplyImg = `INSERT INTO realname_apply_img (img_data) VALUES (?)`
  56. // alipay
  57. _selRealnameAlipayApply = "SELECT id,mid,realname,card,img,status,reason,bizno,ctime,mtime FROM realname_alipay_apply WHERE mid = ? ORDER BY ID DESC LIMIT 1"
  58. _insertRealnameAlipayApply = `INSERT INTO realname_alipay_apply (mid,realname,card,img,status,reason,bizno) VALUES (?,?,?,?,?,?,?)`
  59. _updateRealnameAlipayApply = `UPDATE realname_alipay_apply SET status=?,reason=? WHERE id=?`
  60. // realname old
  61. _insertOldRealnameApply = `INSERT INTO dede_identification_card_apply (mid,realname,type,card_data,card_for_search,front_img,front_img2,back_img,apply_time,status) VALUES (?,?,?,?,?,?,?,?,?,?)`
  62. _insertOldRealnameApplyImg = `INSERT INTO dede_identification_card_apply_img (img_data,add_time) VALUES (?,?)`
  63. )
  64. func hit(id int64) int64 {
  65. return id % _shard
  66. }
  67. // BeginTran begin transcation.
  68. func (d *Dao) BeginTran(c context.Context) (tx *xsql.Tx, err error) {
  69. return d.db.Begin(c)
  70. }
  71. // BaseInfo base info of user.
  72. func (d *Dao) BaseInfo(c context.Context, mid int64) (r *model.BaseInfo, err error) {
  73. r = &model.BaseInfo{}
  74. row := d.db.Master().QueryRow(c, fmt.Sprintf(_selBaseInfo, hit(mid)), mid)
  75. if err = row.Scan(&r.Mid, &r.Name, &r.Sex, &r.Face, &r.Sign, &r.Rank, &r.Birthday); err != nil {
  76. if err == xsql.ErrNoRows {
  77. r = nil
  78. err = nil
  79. return
  80. }
  81. err = errors.Wrapf(err, "dao base mid(%d)", mid)
  82. }
  83. r.RandFaceURL()
  84. return
  85. }
  86. // SetBase set base.
  87. func (d *Dao) SetBase(c context.Context, base *model.BaseInfo) (err error) {
  88. if _, err = d.db.Exec(c, fmt.Sprintf(_setBase, hit(base.Mid)), base.Mid, base.Name, base.Sex, base.Face, base.Sign,
  89. base.Rank, base.Birthday, base.Name, base.Sex, base.Face, base.Sign, base.Rank, base.Birthday); err != nil {
  90. err = errors.Wrapf(err, "dao set base(%v)", base)
  91. }
  92. return
  93. }
  94. // SetSign set user sign.
  95. func (d *Dao) SetSign(c context.Context, mid int64, sign string) (err error) {
  96. if _, err = d.db.Exec(c, fmt.Sprintf(_setSign, hit(mid)), mid, sign, sign); err != nil {
  97. err = errors.Wrapf(err, "dao set sign mid(%d)", mid)
  98. }
  99. return
  100. }
  101. // SetName set user name.
  102. func (d *Dao) SetName(c context.Context, mid int64, name string) (err error) {
  103. if _, err = d.db.Exec(c, fmt.Sprintf(_setName, hit(mid)), mid, name, name); err != nil {
  104. err = errors.Wrapf(err, "dao set name mid(%d) name(%s)", mid, name)
  105. }
  106. return
  107. }
  108. // SetRank set user rank.
  109. func (d *Dao) SetRank(c context.Context, mid, rank int64) (err error) {
  110. if _, err = d.db.Exec(c, fmt.Sprintf(_setRank, hit(mid)), mid, rank, rank); err != nil {
  111. err = errors.Wrapf(err, "dao set rank mid(%d) rank(%d)", mid, rank)
  112. }
  113. return
  114. }
  115. // SetSex set sex.
  116. func (d *Dao) SetSex(c context.Context, mid, sex int64) (err error) {
  117. if _, err = d.db.Exec(c, fmt.Sprintf(_setSex, hit(mid)), mid, sex, sex); err != nil {
  118. err = errors.Wrapf(err, "dao set sex mid(%d) sex(%d)", mid, sex)
  119. }
  120. return
  121. }
  122. // SetBirthday set birthday.
  123. func (d *Dao) SetBirthday(c context.Context, mid int64, birthday time.Time) (err error) {
  124. if _, err = d.db.Exec(c, fmt.Sprintf(_setBirthday, hit(mid)), mid, birthday, birthday); err != nil {
  125. err = errors.Wrapf(err, "dao set birthday mid(%d) birthday(%d)", mid, birthday)
  126. }
  127. return
  128. }
  129. // SetFace set face.
  130. func (d *Dao) SetFace(c context.Context, mid int64, face string) (err error) {
  131. if _, err = d.db.Exec(c, fmt.Sprintf(_setFace, hit(mid)), mid, face, face); err != nil {
  132. err = errors.Wrapf(err, "dao set face mid(%d) face(%v)", mid, face)
  133. }
  134. return
  135. }
  136. // ExpDB get user exp from db.
  137. func (d *Dao) ExpDB(c context.Context, mid int64) (count int64, err error) {
  138. row := d.db.QueryRow(c, fmt.Sprintf(_selExp, hit(mid)), mid)
  139. if err = row.Scan(&count); err != nil {
  140. if err == xsql.ErrNoRows {
  141. err = nil
  142. return
  143. }
  144. err = errors.Wrapf(err, "dao exp mid(%d)", mid)
  145. }
  146. return
  147. }
  148. // SetExp set user exp to count.
  149. func (d *Dao) SetExp(c context.Context, mid, count int64) (affect int64, err error) {
  150. row, err := d.db.Exec(c, fmt.Sprintf(_setExp, hit(mid)), mid, count)
  151. if err != nil {
  152. err = errors.Wrapf(err, "dao set exp mid(%d) exp(%d)", mid, count)
  153. return
  154. }
  155. return row.RowsAffected()
  156. }
  157. // UpdateExp incr user exp by delta.
  158. func (d *Dao) UpdateExp(c context.Context, mid, delta int64) (affect int64, err error) {
  159. row, err := d.db.Exec(c, fmt.Sprintf(_updateExp, hit(mid)), delta, mid)
  160. if err != nil {
  161. err = errors.Wrapf(err, "dao update exp mid(%d) exp(%d)", mid, delta)
  162. return
  163. }
  164. return row.RowsAffected()
  165. }
  166. // ------------- exp ---------------- //
  167. // UserAttrDB get attr.
  168. func (d *Dao) UserAttrDB(c context.Context, mid int64, attr uint) (hasAttr bool, err error) {
  169. var flag int8
  170. row := d.db.QueryRow(c, _selUserAttr, mid, attr, attr)
  171. if err = row.Scan(&flag); err != nil {
  172. if err == xsql.ErrNoRows {
  173. err = nil
  174. return
  175. }
  176. err = errors.Wrapf(err, "dao attr mid(%d) attr(%d)", mid, attr)
  177. }
  178. hasAttr = true
  179. return
  180. }
  181. // SetUserAttr update attr .
  182. func (d *Dao) SetUserAttr(c context.Context, mid int64, attr uint) (err error) {
  183. if _, err = d.db.Exec(c, _updateUserAttr, mid, attr, attr); err != nil {
  184. err = errors.Wrapf(err, "dao attr mid(%d) attr(%d)", mid, attr)
  185. return
  186. }
  187. return
  188. }
  189. // Officials all officials info of user.
  190. func (d *Dao) Officials(c context.Context) (om map[int64]*model.OfficialInfo, err error) {
  191. om = make(map[int64]*model.OfficialInfo)
  192. rows, err := d.db.Query(c, _selOfficials)
  193. if err != nil {
  194. err = errors.Wrap(err, "dao officials")
  195. return
  196. }
  197. defer rows.Close()
  198. for rows.Next() {
  199. var mid int64
  200. o := &model.OfficialInfo{}
  201. if err = rows.Scan(&mid, &o.Role, &o.Title, &o.Desc); err != nil {
  202. err = errors.Wrap(err, "dao officials scan")
  203. return
  204. }
  205. om[mid] = o
  206. }
  207. return
  208. }
  209. // Official is.
  210. func (d *Dao) Official(c context.Context, mid int64) (*model.OfficialInfo, error) {
  211. row := d.db.QueryRow(c, _selOfficial, mid)
  212. o := &model.OfficialInfo{}
  213. if err := row.Scan(&o.Role, &o.Title, &o.Desc); err != nil {
  214. if err == sql.ErrNoRows {
  215. return nil, nil
  216. }
  217. return nil, errors.Wrap(err, "dao official scan")
  218. }
  219. return o, nil
  220. }
  221. // MoralDB get user moral from db.
  222. func (d *Dao) MoralDB(c context.Context, mid int64) (moral *model.Moral, err error) {
  223. moral = &model.Moral{}
  224. row := d.db.QueryRow(c, _selMoral, mid)
  225. if err = row.Scan(&moral.Moral, &moral.Added, &moral.Deducted, &moral.LastRecoverDate); err != nil {
  226. if err == sql.ErrNoRows {
  227. moral = nil
  228. err = nil
  229. return
  230. }
  231. log.Error(" SelMoral row.Scan() error(%v) mid(%v)", err, mid)
  232. err = errors.Wrapf(err, "dao moral mid(%d)", mid)
  233. return
  234. }
  235. return
  236. }
  237. // TxMoralDB get user moral from db.
  238. func (d *Dao) TxMoralDB(tx *xsql.Tx, mid int64) (moral *model.Moral, err error) {
  239. moral = &model.Moral{}
  240. row := tx.QueryRow(_selMoral, mid)
  241. if err = row.Scan(&moral.Moral, &moral.Added, &moral.Deducted, &moral.LastRecoverDate); err != nil {
  242. if err == sql.ErrNoRows {
  243. moral = nil
  244. err = nil
  245. return
  246. }
  247. log.Error(" SelMoral row.Scan() error(%v) mid(%v)", err, mid)
  248. err = errors.Wrapf(err, "dao moral mid(%d)", mid)
  249. return
  250. }
  251. return
  252. }
  253. // TxUpdateMoral set user moral.
  254. func (d *Dao) TxUpdateMoral(tx *xsql.Tx, mid, moral, added, deducted int64) (err error) {
  255. if _, err = tx.Exec(_updateMoral, moral, added, deducted, mid); err != nil {
  256. err = errors.Wrapf(err, "TxUpdateMoral mid(%d) moralAdded(%v)", mid, moral)
  257. return
  258. }
  259. return
  260. }
  261. // TxUpdateMoralRecoverDate update moral recover date.
  262. func (d *Dao) TxUpdateMoralRecoverDate(tx *xsql.Tx, mid int64, recoverDate time.Time) (err error) {
  263. if _, err = tx.Exec(_updateMoralRecoverDate, recoverDate, mid); err != nil {
  264. err = errors.Wrapf(err, "TxUpdateMoralRecoverDate mid(%d) recoverDate(%v)", mid, recoverDate)
  265. return
  266. }
  267. return
  268. }
  269. // TxInitMoral set user moral.
  270. func (d *Dao) TxInitMoral(tx *xsql.Tx, mid, moral, added, deducted int64, lastRecoverDate time.Time) (err error) {
  271. if _, err = tx.Exec(_initMoral, mid, moral, added, deducted, lastRecoverDate); err != nil {
  272. err = errors.Wrapf(err, "TxInitMoral mid(%d) moral(%v)", mid, moral)
  273. return
  274. }
  275. return
  276. }
  277. // SetOfficialDoc add official doc.
  278. func (d *Dao) SetOfficialDoc(c context.Context, od *model.OfficialDoc) (err error) {
  279. _, err = d.db.Exec(c, _setOfficialDoc, od.Mid, od.Name, model.OfficialStateWait, od.Role,
  280. od.Title, od.Desc, od.OfficialExtra.String(), od.SubmitSource, od.SubmitTime)
  281. if err != nil {
  282. err = errors.Wrapf(err, "dao add official doc")
  283. return
  284. }
  285. return
  286. }
  287. // SetOfficialDocAddit add official doc addit.
  288. func (d *Dao) SetOfficialDocAddit(c context.Context, mid int64, property, vstring string) error {
  289. _, err := d.db.Exec(c, _setOfficialDocAddit, mid, property, vstring)
  290. if err != nil {
  291. err = errors.Wrapf(err, "dao add official doc addit")
  292. return err
  293. }
  294. return nil
  295. }
  296. // OfficialDoc get official doc.
  297. func (d *Dao) OfficialDoc(c context.Context, mid int64) (*model.OfficialDoc, error) {
  298. od := new(model.OfficialDoc)
  299. row := d.db.QueryRow(c, _selOfficialDoc, mid)
  300. if err := row.Scan(&od.Mid, &od.Name, &od.State, &od.Role, &od.Title, &od.Desc, &od.RejectReason, &od.Extra); err != nil {
  301. err = errors.Wrapf(err, "official doc")
  302. return nil, err
  303. }
  304. od.ParseExtra()
  305. return od, nil
  306. }
  307. // Realname
  308. // RealnameInfo is.
  309. func (d *Dao) RealnameInfo(c context.Context, mid int64) (info *model.RealnameInfo, err error) {
  310. row := d.db.QueryRow(c, _selRealnameInfo, mid)
  311. info = &model.RealnameInfo{}
  312. if err = row.Scan(&info.ID, &info.MID, &info.Channel, &info.Realname, &info.Country, &info.CardType, &info.Card, &info.CardMD5, &info.Status, &info.Reason, &info.CTime, &info.MTime); err != nil {
  313. if err == xsql.ErrNoRows {
  314. err = nil
  315. info = nil
  316. return
  317. }
  318. err = errors.Wrapf(err, "dao RealnameInfo mid(%d)", mid)
  319. return
  320. }
  321. return
  322. }
  323. // RealnameInfoByCard is.
  324. func (d *Dao) RealnameInfoByCard(c context.Context, cardMD5 string) (info *model.RealnameInfo, err error) {
  325. row := d.db.QueryRow(c, _selRealnameInfoByCard, cardMD5)
  326. info = &model.RealnameInfo{}
  327. if err = row.Scan(&info.ID, &info.MID, &info.Channel, &info.Realname, &info.Country, &info.CardType, &info.Card, &info.CardMD5, &info.Status, &info.Reason, &info.CTime, &info.MTime); err != nil {
  328. if err == xsql.ErrNoRows {
  329. err = nil
  330. info = nil
  331. return
  332. }
  333. err = errors.Wrapf(err, "dao RealnameInfo cardMD5(%s)", cardMD5)
  334. return
  335. }
  336. return
  337. }
  338. // UpsertTxRealnameInfo is.
  339. func (d *Dao) UpsertTxRealnameInfo(c context.Context, tx *xsql.Tx, info *model.RealnameInfo) (err error) {
  340. if _, err = tx.Exec(_upsertRealnameInfo, info.MID, info.Channel, info.Realname, info.Country, info.CardType, info.Card, info.CardMD5, info.Status, info.Reason, info.Channel, info.Realname, info.Country, info.CardType, info.Card, info.CardMD5, info.Status, info.Reason); err != nil {
  341. err = errors.WithStack(err)
  342. return
  343. }
  344. return
  345. }
  346. // UpdateTxRealnameInfo is.
  347. func (d *Dao) UpdateTxRealnameInfo(c context.Context, tx *xsql.Tx, mid int64, status model.RealnameApplyStatus, reason string) (err error) {
  348. if _, err = tx.Exec(_updateRealnameInfo, status, reason, mid); err != nil {
  349. err = errors.WithStack(err)
  350. return
  351. }
  352. return
  353. }
  354. // RealnameApply realname
  355. func (d *Dao) RealnameApply(c context.Context, mid int64) (apply *model.RealnameApply, err error) {
  356. row := d.db.Master().QueryRow(c, _selRealnameApply, mid)
  357. apply = &model.RealnameApply{}
  358. if err = row.Scan(&apply.ID, &apply.MID, &apply.Realname, &apply.Country, &apply.CardType, &apply.CardNum, &apply.CardMD5, &apply.HandIMG, &apply.FrontIMG, &apply.BackIMG, &apply.Status, &apply.Operator, &apply.OperatorID, &apply.OperatorTime, &apply.Remark, &apply.RemarkStatus, &apply.CTime, &apply.MTime); err != nil {
  359. if err == xsql.ErrNoRows {
  360. err = nil
  361. apply = nil
  362. return
  363. }
  364. err = errors.Wrapf(err, "dao RealnameApply mid(%d)", mid)
  365. return
  366. }
  367. return
  368. }
  369. // InsertRealnameApply is
  370. func (d *Dao) InsertRealnameApply(c context.Context, data *model.RealnameApply) (err error) {
  371. if _, err = d.db.Exec(c, _insertRealnameApply, data.MID, data.Realname, data.Country, data.CardType, data.CardNum, data.CardMD5, data.HandIMG, data.FrontIMG, data.BackIMG, data.Status); err != nil {
  372. err = errors.WithStack(err)
  373. return
  374. }
  375. return
  376. }
  377. // RealnameApplyIMG is
  378. func (d *Dao) RealnameApplyIMG(c context.Context, id int) (img *model.RealnameApplyImage, err error) {
  379. row := d.db.Master().QueryRow(c, _selRealnameApplyImg, id)
  380. img = &model.RealnameApplyImage{}
  381. if err = row.Scan(&img.ID, &img.IMGData, &img.CTime, &img.MTime); err != nil {
  382. if err == xsql.ErrNoRows {
  383. err = nil
  384. img = nil
  385. return
  386. }
  387. err = errors.Wrapf(err, "dao RealnameApplyIMG mid(%d)", id)
  388. return
  389. }
  390. return
  391. }
  392. // InsertRealnameApplyImg is
  393. func (d *Dao) InsertRealnameApplyImg(c context.Context, data *model.RealnameApplyImage) (id int64, err error) {
  394. var res sql.Result
  395. if res, err = d.db.Exec(c, _insertRealnameApplyImg, data.IMGData); err != nil {
  396. err = errors.WithStack(err)
  397. return
  398. }
  399. if id, err = res.LastInsertId(); err != nil {
  400. err = errors.WithStack(err)
  401. return
  402. }
  403. return
  404. }
  405. // InsertOldRealnameApply is
  406. func (d *Dao) InsertOldRealnameApply(c context.Context, data *model.RealnameApply) (id int64, err error) {
  407. var (
  408. res sql.Result
  409. cardType = int16(data.CardType)
  410. )
  411. if data.Country > 0 {
  412. cardType = data.Country
  413. }
  414. if res, err = d.accdb.Exec(c, _insertOldRealnameApply, data.MID, data.Realname, cardType, data.CardNum, data.CardMD5, data.FrontIMG, data.HandIMG, data.BackIMG, data.CTime.Unix(), data.Status); err != nil {
  415. err = errors.WithStack(err)
  416. return
  417. }
  418. if id, err = res.LastInsertId(); err != nil {
  419. err = errors.WithStack(err)
  420. return
  421. }
  422. return
  423. }
  424. // InsertOldRealnameApplyImg is
  425. func (d *Dao) InsertOldRealnameApplyImg(c context.Context, data *model.RealnameApplyImage) (id int64, err error) {
  426. var res sql.Result
  427. if res, err = d.accdb.Exec(c, _insertOldRealnameApplyImg, data.IMGData, data.CTime); err != nil {
  428. err = errors.WithStack(err)
  429. return
  430. }
  431. if id, err = res.LastInsertId(); err != nil {
  432. err = errors.WithStack(err)
  433. return
  434. }
  435. return
  436. }
  437. // InsertTxRealnameAlipayApply .
  438. func (d *Dao) InsertTxRealnameAlipayApply(c context.Context, tx *xsql.Tx, data *model.RealnameAlipayApply) (err error) {
  439. if _, err = tx.Exec(_insertRealnameAlipayApply, data.MID, data.Realname, data.Card, data.IMG, data.Status, data.Reason, data.Bizno); err != nil {
  440. err = errors.WithStack(err)
  441. return
  442. }
  443. return
  444. }
  445. // UpdateTxRealnameAlipayApply is.
  446. func (d *Dao) UpdateTxRealnameAlipayApply(c context.Context, tx *xsql.Tx, id int64, status model.RealnameApplyStatus, reason string) (err error) {
  447. if _, err = tx.Exec(_updateRealnameAlipayApply, status, reason, id); err != nil {
  448. err = errors.WithStack(err)
  449. return
  450. }
  451. return
  452. }
  453. // RealnameAlipayApply .
  454. func (d *Dao) RealnameAlipayApply(c context.Context, mid int64) (apply *model.RealnameAlipayApply, err error) {
  455. row := d.db.QueryRow(c, _selRealnameAlipayApply, mid)
  456. apply = &model.RealnameAlipayApply{}
  457. if err = row.Scan(&apply.ID, &apply.MID, &apply.Realname, &apply.Card, &apply.IMG, &apply.Status, &apply.Reason, &apply.Bizno, &apply.CTime, &apply.MTime); err != nil {
  458. if err == xsql.ErrNoRows {
  459. err = nil
  460. apply = nil
  461. return
  462. }
  463. err = errors.Wrapf(err, "dao RealnameAlipayApply mid(%d)", mid)
  464. return
  465. }
  466. return
  467. }
  468. func prepareStringArray(in []string) string {
  469. surrounded := make([]string, 0, len(in))
  470. for _, s := range in {
  471. surrounded = append(surrounded, fmt.Sprintf(`'%s'`, s))
  472. }
  473. return strings.Join(surrounded, ",")
  474. }
  475. // MidByRealnameCards is
  476. func (d *Dao) MidByRealnameCards(ctx context.Context, cardMD5s []string) (map[string]int64, error) {
  477. rows, err := d.db.Query(ctx, fmt.Sprintf(_selRealnameInfoMidByCards, prepareStringArray(cardMD5s)))
  478. if err != nil {
  479. return nil, err
  480. }
  481. defer rows.Close()
  482. result := make(map[string]int64, len(cardMD5s))
  483. for rows.Next() {
  484. mid, cardMD5 := int64(0), ""
  485. if err := rows.Scan(&mid, &cardMD5); err != nil {
  486. log.Warn("Failed to scan realname info: %+v", err)
  487. continue
  488. }
  489. result[cardMD5] = mid
  490. }
  491. return result, nil
  492. }