realname.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. package dao
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "go-common/app/admin/main/member/model"
  9. "go-common/library/cache/memcache"
  10. "go-common/library/log"
  11. "github.com/pkg/errors"
  12. )
  13. var (
  14. _reasonKey = "reason"
  15. )
  16. func realnameImageKey(IMGData string) string {
  17. return fmt.Sprintf("realname_image_%s", IMGData)
  18. }
  19. // RealnameInfo is.
  20. func (d *Dao) RealnameInfo(ctx context.Context, mid int64) (info *model.DBRealnameInfo, err error) {
  21. info = &model.DBRealnameInfo{}
  22. if err = d.memberRead.Table(info.TableName()).Where("mid = ?", mid).First(&info).Error; err != nil {
  23. info = nil
  24. err = errors.WithStack(err)
  25. }
  26. return
  27. }
  28. // BatchRealnameInfo is
  29. func (d *Dao) BatchRealnameInfo(ctx context.Context, mids []int64) (map[int64]*model.DBRealnameInfo, error) {
  30. find := make([]*model.DBRealnameInfo, 0, len(mids))
  31. db := d.memberRead.Table("realname_info").Where("mid IN (?)", mids).Where("status=?", model.RealnameApplyStatePassed.DBStatus()).Find(&find)
  32. if err := db.Error; err != nil {
  33. return nil, err
  34. }
  35. result := make(map[int64]*model.DBRealnameInfo, len(find))
  36. for _, ri := range find {
  37. result[ri.MID] = ri
  38. }
  39. return result, nil
  40. }
  41. // RealnameInfoByCardMD5 is.
  42. func (d *Dao) RealnameInfoByCardMD5(ctx context.Context, cardMD5 string, state int, channel uint8) (infos []*model.DBRealnameInfo, err error) {
  43. infos = make([]*model.DBRealnameInfo, 0)
  44. var info *model.DBRealnameInfo
  45. db := d.memberRead.Table(info.TableName()).Where("card_md5 = ?", cardMD5).Where("channel = ?", channel)
  46. if state >= 0 {
  47. db = db.Where("status = ?", state)
  48. }
  49. if err = db.Find(&infos).Error; err != nil {
  50. if !db.RecordNotFound() {
  51. err = errors.Wrapf(err, "RealnameInfoByCardMD5")
  52. return
  53. }
  54. err = nil
  55. return
  56. }
  57. return
  58. }
  59. // UpdateRealnameAlipayApply is.
  60. func (d *Dao) UpdateRealnameAlipayApply(ctx context.Context, id int64, adminID int64, adminName string, state int, reason string) (err error) {
  61. var (
  62. mld *model.DBRealnameAlipayApply
  63. ups = map[string]interface{}{
  64. "operator": adminName,
  65. "operator_id": adminID,
  66. "operator_time": time.Now(),
  67. "status": state,
  68. "reason": reason,
  69. }
  70. )
  71. if err = d.member.Table(mld.TableName()).Where("id = ?", id).Updates(ups).Error; err != nil {
  72. err = errors.Wrapf(err, "UpdateRealnameAlipayApply")
  73. }
  74. return
  75. }
  76. // UpdateRealnameInfo is.
  77. func (d *Dao) UpdateRealnameInfo(ctx context.Context, mid int64, state int, reason string) (err error) {
  78. var (
  79. mld *model.DBRealnameInfo
  80. ups = map[string]interface{}{
  81. "status": state,
  82. "reason": reason,
  83. }
  84. )
  85. if err = d.member.Table(mld.TableName()).Where("mid = ?", mid).Updates(ups).Error; err != nil {
  86. err = errors.Wrapf(err, "UpdateRealnameInfo")
  87. }
  88. return
  89. }
  90. // RealnameMainApply is.
  91. func (d *Dao) RealnameMainApply(ctx context.Context, id int64) (apply *model.DBRealnameApply, err error) {
  92. apply = &model.DBRealnameApply{
  93. Status: model.RealnameApplyStateNone.DBStatus(),
  94. }
  95. if err = d.memberRead.Table(apply.TableName()).Where("id = ?", id).First(&apply).Error; err != nil {
  96. err = errors.WithStack(err)
  97. }
  98. return
  99. }
  100. // RealnameAlipayApply is.
  101. func (d *Dao) RealnameAlipayApply(ctx context.Context, id int64) (apply *model.DBRealnameAlipayApply, err error) {
  102. apply = &model.DBRealnameAlipayApply{
  103. Status: model.RealnameApplyStateNone.DBStatus(),
  104. }
  105. if err = d.memberRead.Table(apply.TableName()).Where("id = ?", id).First(&apply).Error; err != nil {
  106. err = errors.WithStack(err)
  107. }
  108. return
  109. }
  110. // RealnameMainList is.
  111. func (d *Dao) RealnameMainList(ctx context.Context, mids []int64, cardType int, country int, opName string, tsFrom, tsTo int64, state int, pn, ps int, isDesc bool) (list []*model.DBRealnameApply, total int, err error) {
  112. var (
  113. mdl *model.DBRealnameApply
  114. )
  115. db := d.memberRead.Table(mdl.TableName())
  116. if state >= 0 {
  117. db = db.Where("status = ?", state)
  118. }
  119. if len(mids) > 0 {
  120. db = db.Where("mid in (?)", mids)
  121. }
  122. if cardType >= 0 {
  123. db = db.Where("card_type = ?", cardType)
  124. }
  125. if country >= 0 {
  126. db = db.Where("country = ?", country)
  127. }
  128. if opName != "" {
  129. db = db.Where("operator = ?", opName)
  130. }
  131. if tsTo > 0 {
  132. timeTo := time.Unix(tsTo, 0)
  133. db = db.Where("mtime <= ?", timeTo)
  134. }
  135. if tsFrom > 0 {
  136. timeFrom := time.Unix(tsFrom, 0)
  137. db = db.Where("mtime >= ?", timeFrom)
  138. }
  139. if err = db.Count(&total).Error; err != nil {
  140. err = errors.Wrapf(err, "realname apply list count")
  141. return
  142. }
  143. mtimeSort := "mtime ASC"
  144. if isDesc {
  145. mtimeSort = "mtime DESC"
  146. }
  147. db = db.Order(mtimeSort).Offset((pn - 1) * ps).Limit(ps)
  148. if err = db.Find(&list).Error; err != nil {
  149. if !db.RecordNotFound() {
  150. err = errors.Wrapf(err, "realname apply list")
  151. return
  152. }
  153. err = nil
  154. return
  155. }
  156. return
  157. }
  158. // RealnameAlipayList is.
  159. func (d *Dao) RealnameAlipayList(ctx context.Context, mids []int64, tsFrom, tsTo int64, state int, pn, ps int, isDesc bool) (list []*model.DBRealnameAlipayApply, total int, err error) {
  160. var (
  161. mdl *model.DBRealnameAlipayApply
  162. )
  163. db := d.memberRead.Table(mdl.TableName())
  164. if state >= 0 {
  165. db = db.Where("status = ?", state)
  166. }
  167. if len(mids) > 0 {
  168. db = db.Where("mid in (?)", mids)
  169. }
  170. if tsTo > 0 {
  171. timeTo := time.Unix(tsTo, 0)
  172. db = db.Where("mtime <= ?", timeTo)
  173. }
  174. if tsFrom > 0 {
  175. timeFrom := time.Unix(tsFrom, 0)
  176. db = db.Where("mtime >= ?", timeFrom)
  177. }
  178. if err = db.Count(&total).Error; err != nil {
  179. err = errors.Wrapf(err, "realname apply list count")
  180. return
  181. }
  182. mtimeSort := "mtime ASC"
  183. if isDesc {
  184. mtimeSort = "mtime DESC"
  185. }
  186. db = db.Order(mtimeSort).Offset((pn - 1) * ps).Limit(ps)
  187. if err = db.Find(&list).Error; err != nil {
  188. if !db.RecordNotFound() {
  189. err = errors.Wrapf(err, "realname apply list")
  190. return
  191. }
  192. err = nil
  193. return
  194. }
  195. return
  196. }
  197. // RealnameSearchCards is.
  198. func (d *Dao) RealnameSearchCards(ctx context.Context, cardMD5s []string) (list []*model.DBRealnameInfo, err error) {
  199. var (
  200. mdl *model.DBRealnameInfo
  201. )
  202. db := d.memberRead.Table(mdl.TableName()).Where("card_md5 in (?)", cardMD5s)
  203. if err = db.Find(&list).Error; err != nil {
  204. if !db.RecordNotFound() {
  205. err = errors.Wrapf(err, "realname apply list")
  206. return
  207. }
  208. err = nil
  209. return
  210. }
  211. return
  212. }
  213. // UpdateRealnameMainApply .
  214. func (d *Dao) UpdateRealnameMainApply(ctx context.Context, id int, state int, opname string, opid int64, optime time.Time, remark string) (err error) {
  215. var (
  216. mld *model.DBRealnameApply
  217. ups = map[string]interface{}{
  218. "operator": opname,
  219. "operator_id": opid,
  220. "operator_time": optime,
  221. "remark": remark,
  222. "remark_status": 1,
  223. "status": state,
  224. }
  225. )
  226. if err = d.member.Table(mld.TableName()).Where("id = ?", id).Updates(ups).Error; err != nil {
  227. err = errors.Wrapf(err, "UpdateRealnameApply")
  228. }
  229. return
  230. }
  231. // UpdateOldRealnameApply .
  232. func (d *Dao) UpdateOldRealnameApply(ctx context.Context, id int64, state int, opname string, opid int64, optime time.Time, remark string) (err error) {
  233. var (
  234. ups = map[string]interface{}{
  235. "operater": opname,
  236. // "operator_id": opid,
  237. "operater_time": optime.Unix(),
  238. "remark": remark,
  239. "remark_status": 1,
  240. "status": state,
  241. }
  242. )
  243. if err = d.account.Table("dede_identification_card_apply").Where("id = ?", id).Updates(ups).Error; err != nil {
  244. err = errors.Wrapf(err, "UpdateOldRealnameApply")
  245. }
  246. return
  247. }
  248. // RealnameApplyIMG .
  249. func (d *Dao) RealnameApplyIMG(ctx context.Context, ids []int64) (imgMap map[int64]*model.DBRealnameApplyIMG, err error) {
  250. var (
  251. db = d.memberRead.Where("id in (?)", ids)
  252. list []*model.DBRealnameApplyIMG
  253. )
  254. imgMap = make(map[int64]*model.DBRealnameApplyIMG)
  255. if err = db.Find(&list).Error; err != nil {
  256. if !db.RecordNotFound() {
  257. err = errors.Wrapf(err, "RealnameApplyIMG")
  258. return
  259. }
  260. err = nil
  261. }
  262. for _, l := range list {
  263. imgMap[l.ID] = l
  264. }
  265. return
  266. }
  267. // RealnameApplyCount .
  268. func (d *Dao) RealnameApplyCount(ctx context.Context, mid int64) (count int, err error) {
  269. var (
  270. ml *model.DBRealnameApply
  271. aml *model.DBRealnameAlipayApply
  272. tempCount int
  273. db = d.memberRead.Table(ml.TableName()).Where("mid = ?", mid)
  274. db2 = d.memberRead.Table(aml.TableName()).Where("mid = ?", mid)
  275. )
  276. if err = db.Count(&tempCount).Error; err != nil {
  277. err = errors.WithStack(err)
  278. return
  279. }
  280. count += tempCount
  281. if err = db2.Count(&tempCount).Error; err != nil {
  282. err = errors.WithStack(err)
  283. return
  284. }
  285. count += tempCount
  286. return
  287. }
  288. // RealnameReasonList .
  289. func (d *Dao) RealnameReasonList(ctx context.Context) (list []string, total int, err error) {
  290. var (
  291. conf = &model.DBRealnameConfig{}
  292. db = d.memberRead.Where("`key` = ?", _reasonKey).First(&conf)
  293. )
  294. if err = db.Error; err != nil {
  295. if !db.RecordNotFound() {
  296. err = errors.Wrapf(err, "RealnameReasonList")
  297. return
  298. }
  299. err = nil
  300. }
  301. list = decodeReason(conf.Data)
  302. total = len(list)
  303. return
  304. }
  305. // UpdateRealnameReason .
  306. func (d *Dao) UpdateRealnameReason(ctx context.Context, list []string) (err error) {
  307. var (
  308. conf *model.DBRealnameConfig
  309. ups = map[string]interface{}{
  310. "data": encodeReason(list),
  311. }
  312. )
  313. if err = d.member.Table(conf.TableName()).Where("`key` = ?", _reasonKey).Updates(ups).Error; err != nil {
  314. err = errors.Wrapf(err, "UpdateRealnameReason")
  315. }
  316. return
  317. }
  318. func encodeReason(list []string) (data string) {
  319. raw := []byte(strings.Join(list, "(#=_=#)"))
  320. return base64.StdEncoding.EncodeToString(raw)
  321. }
  322. func decodeReason(data string) (list []string) {
  323. var (
  324. raw []byte
  325. err error
  326. )
  327. if raw, err = base64.StdEncoding.DecodeString(data); err != nil {
  328. err = errors.WithStack(err)
  329. log.Error("%+v", err)
  330. return
  331. }
  332. list = strings.Split(string(raw), "(#=_=#)")
  333. return
  334. }
  335. // AddRealnameIMG is
  336. func (d *Dao) AddRealnameIMG(ctx context.Context, img *model.DBRealnameApplyIMG) error {
  337. if err := d.member.Create(img).Error; err != nil {
  338. return errors.WithStack(err)
  339. }
  340. return nil
  341. }
  342. // AddRealnameApply is
  343. func (d *Dao) AddRealnameApply(ctx context.Context, apply *model.DBRealnameApply) error {
  344. if err := d.member.Create(apply).Error; err != nil {
  345. return errors.WithStack(err)
  346. }
  347. return nil
  348. }
  349. // SubmitRealnameInfo is
  350. func (d *Dao) SubmitRealnameInfo(ctx context.Context, info *model.DBRealnameInfo) error {
  351. ups := map[string]interface{}{
  352. "channel": info.Channel,
  353. "realname": info.Realname,
  354. "country": info.Country,
  355. "card_type": info.CardType,
  356. "card": info.Card,
  357. "card_md5": info.CardMD5,
  358. "status": info.Status,
  359. "reason": info.Reason,
  360. }
  361. if err := d.member.Table(info.TableName()).Where("mid=?", info.MID).Assign(ups).FirstOrCreate(info).Error; err != nil {
  362. return errors.WithStack(err)
  363. }
  364. return nil
  365. }
  366. // GetRealnameImageCache is
  367. func (d *Dao) GetRealnameImageCache(ctx context.Context, IMGData string) ([]byte, error) {
  368. key := realnameImageKey(IMGData)
  369. conn := d.memcache.Get(ctx)
  370. defer conn.Close()
  371. item, err := conn.Get(key)
  372. if err != nil {
  373. return nil, err
  374. }
  375. out := []byte{}
  376. if err := conn.Scan(item, &out); err != nil {
  377. return nil, err
  378. }
  379. return out, nil
  380. }
  381. // SetRealnameImageCache is
  382. func (d *Dao) SetRealnameImageCache(ctx context.Context, IMGData string, data []byte) error {
  383. key := realnameImageKey(IMGData)
  384. conn := d.memcache.Get(ctx)
  385. defer conn.Close()
  386. return conn.Set(&memcache.Item{
  387. Key: key,
  388. Value: data,
  389. Flags: memcache.FlagRAW | memcache.FlagGzip,
  390. Expiration: 6 * 3600, // cache for 6 hours
  391. })
  392. }
  393. // RecentRealnameApplyImg is
  394. func (d *Dao) RecentRealnameApplyImg(ctx context.Context, duration time.Duration) ([]*model.DBRealnameApplyIMG, error) {
  395. from := time.Now().Add(-duration)
  396. result := []*model.DBRealnameApplyIMG{}
  397. db := d.memberRead.Where("mtime>=?", from).Order("id desc").Find(&result)
  398. if err := db.Error; err != nil {
  399. return nil, err
  400. }
  401. return result, nil
  402. }
  403. // RejectRealnameMainApply is
  404. func (d *Dao) RejectRealnameMainApply(ctx context.Context, mid int64, opname string, opid int64, remark string) (err error) {
  405. var (
  406. mld *model.DBRealnameApply
  407. ups = map[string]interface{}{
  408. "operator": opname,
  409. "operator_id": opid,
  410. "operator_time": time.Now(),
  411. "remark": remark,
  412. "remark_status": 1,
  413. "status": model.RealnameApplyStateRejective.DBStatus(),
  414. }
  415. )
  416. if err = d.member.Table(mld.TableName()).
  417. Where("mid = ?", mid).
  418. Where("status = ?", model.RealnameApplyStatePassed.DBStatus()).
  419. Updates(ups).Error; err != nil {
  420. err = errors.Wrapf(err, "RejectRealnameMainApply")
  421. }
  422. return
  423. }
  424. // RejectRealnameAlipayApply is
  425. func (d *Dao) RejectRealnameAlipayApply(ctx context.Context, mid int64, opname string, opid int64, reason string) (err error) {
  426. var (
  427. mld *model.DBRealnameAlipayApply
  428. ups = map[string]interface{}{
  429. "operator": opname,
  430. "operator_id": opid,
  431. "operator_time": time.Now(),
  432. "status": model.RealnameApplyStateRejective.DBStatus(),
  433. "reason": reason,
  434. }
  435. )
  436. if err = d.member.Table(mld.TableName()).
  437. Where("mid = ?", mid).
  438. Where("status = ?", model.RealnameApplyStatePassed.DBStatus()).
  439. Updates(ups).Error; err != nil {
  440. err = errors.Wrapf(err, "UpdateRealnameAlipayApply")
  441. }
  442. return
  443. }
  444. // LastPassedRealnameMainApply is
  445. func (d *Dao) LastPassedRealnameMainApply(ctx context.Context, mid int64) (*model.DBRealnameApply, error) {
  446. apply := &model.DBRealnameApply{}
  447. if err := d.member.Table("realname_apply").Where("mid=?", mid).
  448. Where("status=?", model.RealnameApplyStatePassed.DBStatus()).
  449. Order("id DESC").Limit(1).Last(apply).Error; err != nil {
  450. return nil, err
  451. }
  452. return apply, nil
  453. }
  454. // LastPassedRealnameAlipayApply is
  455. func (d *Dao) LastPassedRealnameAlipayApply(ctx context.Context, mid int64) (*model.DBRealnameAlipayApply, error) {
  456. apply := &model.DBRealnameAlipayApply{}
  457. if err := d.member.Table("realname_alipay_apply").Where("mid=?", mid).
  458. Where("status=?", model.RealnameApplyStatePassed.DBStatus()).
  459. Order("id DESC").Limit(1).Last(apply).Error; err != nil {
  460. return nil, err
  461. }
  462. return apply, nil
  463. }