raw.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package dao
  2. import (
  3. "context"
  4. v1 "go-common/app/service/main/account/api"
  5. "go-common/app/service/main/account/model"
  6. mml "go-common/app/service/main/member/model"
  7. bml "go-common/app/service/main/member/model/block"
  8. sml "go-common/app/service/main/usersuit/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "go-common/library/sync/errgroup"
  12. "github.com/pkg/errors"
  13. )
  14. // RawInfo raw info.
  15. func (d *Dao) RawInfo(c context.Context, mid int64) (res *v1.Info, err error) {
  16. var base *mml.BaseInfo
  17. if base, err = d.mRPC.Base(c, &mml.ArgMemberMid{Mid: mid}); err != nil {
  18. // err = errors.Wrap(err, "dao raw info")
  19. // err = errors.WithStack(err)
  20. return
  21. }
  22. res = &v1.Info{
  23. Mid: base.Mid,
  24. Name: base.Name,
  25. Sex: base.SexStr(),
  26. Face: base.Face,
  27. Sign: base.Sign,
  28. Rank: int32(base.Rank),
  29. }
  30. return
  31. }
  32. // RawInfos raw infos.
  33. func (d *Dao) RawInfos(c context.Context, mids []int64) (res map[int64]*v1.Info, err error) {
  34. var bm map[int64]*mml.BaseInfo
  35. if bm, err = d.mRPC.Bases(c, &mml.ArgMemberMids{Mids: mids}); err != nil {
  36. // err = errors.Wrap(err, "dao raw info")
  37. err = errors.WithStack(err)
  38. return
  39. }
  40. res = map[int64]*v1.Info{}
  41. for _, base := range bm {
  42. i := &v1.Info{
  43. Mid: base.Mid,
  44. Name: base.Name,
  45. Sex: base.SexStr(),
  46. Face: base.Face,
  47. Sign: base.Sign,
  48. Rank: int32(base.Rank),
  49. }
  50. res[i.Mid] = i
  51. }
  52. return
  53. }
  54. // RawCard get card by mid.
  55. func (d *Dao) RawCard(c context.Context, mid int64) (res *v1.Card, err error) {
  56. eg, _ := errgroup.WithContext(c)
  57. var mb *mml.Member
  58. eg.Go(func() (e error) {
  59. if mb, e = d.mRPC.Member(c, &mml.ArgMemberMid{Mid: mid}); e != nil {
  60. log.Error("d.mRPC.Member(%d) err(%v)", mid, e)
  61. // e = ecode.Degrade
  62. }
  63. return
  64. })
  65. var medal *sml.MedalInfo
  66. eg.Go(func() (e error) {
  67. if medal, e = d.suitRPC.MedalActivated(c, &sml.ArgMid{Mid: mid}); e != nil {
  68. log.Error("s.suitRPC.MedalActivated(%d) err %v", mid, e)
  69. e = ecode.Degrade
  70. }
  71. return
  72. })
  73. var block *bml.RPCResInfo
  74. eg.Go(func() (e error) {
  75. if block, e = d.mRPC.BlockInfo(c, &bml.RPCArgInfo{MID: mid}); e != nil {
  76. log.Error("d.block.BlockInfo(%d) err %v", mid, e)
  77. e = ecode.Degrade
  78. }
  79. return
  80. })
  81. var vip *v1.VipInfo
  82. eg.Go(func() (e error) {
  83. if vip, e = d.Vip(c, mid); e != nil {
  84. log.Error("d.Vip(%d) err(%v)", mid, e)
  85. e = ecode.Degrade
  86. }
  87. return
  88. })
  89. var pendant *sml.PendantEquip
  90. eg.Go(func() (e error) {
  91. if pendant, e = d.suitRPC.Equipment(c, &sml.ArgEquipment{Mid: mid}); e != nil {
  92. log.Error("d.suitRPC.Equipment(%d) err(%v)", mid, e)
  93. e = ecode.Degrade
  94. }
  95. return
  96. })
  97. if err = eg.Wait(); err != nil && err != ecode.Degrade {
  98. return
  99. }
  100. card := &v1.Card{Mid: mid}
  101. if medal != nil {
  102. card.Nameplate.Nid = int(medal.ID)
  103. card.Nameplate.Name = medal.Name
  104. card.Nameplate.Image = medal.Image
  105. card.Nameplate.ImageSmall = medal.ImageSmall
  106. card.Nameplate.Level = medal.LevelDesc
  107. card.Nameplate.Condition = medal.Condition
  108. }
  109. if block != nil {
  110. card.Silence = blockStatusToSilence(block.BlockStatus)
  111. }
  112. if mb != nil {
  113. card.Name = mb.Name
  114. card.Sign = mb.Sign
  115. card.Sex = mb.SexStr()
  116. card.Rank = int32(mb.Rank)
  117. card.Face = mb.Face
  118. if mb.OfficialInfo != nil {
  119. // card.Official = *mb.OfficialInfo
  120. card.Official.DeepCopyFromOfficialInfo(mb.OfficialInfo)
  121. }
  122. card.Level = mb.Cur
  123. }
  124. if vip != nil {
  125. card.Vip = *vip
  126. }
  127. if pendant != nil {
  128. card.Pendant.Pid = int(pendant.Pid)
  129. card.Pendant.Expire = int(pendant.Expires)
  130. if pendant.Pendant != nil {
  131. card.Pendant.Name = pendant.Pendant.Name
  132. card.Pendant.Image = fullImage(mid, pendant.Pendant.Image)
  133. }
  134. }
  135. res = card
  136. return
  137. }
  138. // RawCards get card by mid.
  139. func (d *Dao) RawCards(c context.Context, mids []int64) (res map[int64]*v1.Card, err error) {
  140. eg, _ := errgroup.WithContext(c)
  141. var medals map[int64]*sml.MedalInfo
  142. eg.Go(func() (e error) {
  143. if medals, e = d.suitRPC.MedalActivatedMulti(c, &sml.ArgMids{Mids: mids}); e != nil {
  144. log.Error("s.suitRPC.MedalActivatedMulti(%v) err %v", mids, e)
  145. e = ecode.Degrade
  146. }
  147. return
  148. })
  149. var blocks map[int64]*bml.RPCResInfo
  150. eg.Go(func() (e error) {
  151. var bs []*bml.RPCResInfo
  152. if bs, e = d.mRPC.BlockBatchInfo(c, &bml.RPCArgBatchInfo{MIDs: mids}); e != nil {
  153. log.Error("d.block.BlockBatchInfo(%v) err %v", mids, e)
  154. e = ecode.Degrade
  155. }
  156. blocks = make(map[int64]*bml.RPCResInfo, len(bs))
  157. for _, block := range bs {
  158. blocks[block.MID] = block
  159. }
  160. return
  161. })
  162. var mbs map[int64]*mml.Member
  163. eg.Go(func() (e error) {
  164. if mbs, e = d.mRPC.Members(c, &mml.ArgMemberMids{Mids: mids}); e != nil {
  165. log.Error("d.mRPC.Members(%v) err(%v)", mids, e)
  166. // e = ecode.Degrade
  167. }
  168. return
  169. })
  170. var vipm map[int64]*v1.VipInfo
  171. eg.Go(func() (e error) {
  172. if vipm, e = d.Vips(c, mids); e != nil {
  173. log.Error("d.CpVips(%v) err(%v)", mids, e)
  174. e = ecode.Degrade
  175. }
  176. return
  177. })
  178. var pendantm map[int64]*sml.PendantEquip
  179. eg.Go(func() (e error) {
  180. if pendantm, e = d.suitRPC.Equipments(c, &sml.ArgEquipments{Mids: mids}); e != nil {
  181. log.Error("d.suitRPC.Equipments(%v) err(%v)", mids, e)
  182. e = ecode.Degrade
  183. }
  184. return
  185. })
  186. if err = eg.Wait(); err != nil && err != ecode.Degrade {
  187. return
  188. }
  189. res = map[int64]*v1.Card{}
  190. for _, mid := range mids {
  191. card := &v1.Card{Mid: mid}
  192. if mb, ok := mbs[mid]; ok && mb != nil {
  193. card.Name = mb.Name
  194. card.Sign = mb.Sign
  195. card.Sex = mb.SexStr()
  196. card.Rank = int32(mb.Rank)
  197. card.Face = mb.Face
  198. if mb.OfficialInfo != nil {
  199. // card.Official = *mb.OfficialInfo
  200. card.Official.DeepCopyFromOfficialInfo(mb.OfficialInfo)
  201. }
  202. card.Level = mb.Cur
  203. } else {
  204. continue
  205. }
  206. if block, ok := blocks[mid]; ok && block != nil {
  207. card.Silence = blockStatusToSilence(block.BlockStatus)
  208. }
  209. if medal, ok := medals[mid]; ok && medal != nil {
  210. card.Nameplate.Nid = int(medal.ID)
  211. card.Nameplate.Name = medal.Name
  212. card.Nameplate.Image = medal.Image
  213. card.Nameplate.ImageSmall = medal.ImageSmall
  214. card.Nameplate.Level = medal.LevelDesc
  215. card.Nameplate.Condition = medal.Condition
  216. }
  217. if vip, ok := vipm[mid]; ok && vip != nil {
  218. card.Vip = *vip
  219. }
  220. if pendant, ok := pendantm[mid]; ok && pendant != nil {
  221. card.Pendant.Pid = int(pendant.Pid)
  222. card.Pendant.Expire = int(pendant.Expires)
  223. if pendant.Pendant != nil {
  224. card.Pendant.Name = pendant.Pendant.Name
  225. card.Pendant.Image = fullImage(mid, pendant.Pendant.Image)
  226. }
  227. }
  228. res[mid] = card
  229. }
  230. return
  231. }
  232. // RawProfile get profile by mid.
  233. func (d *Dao) RawProfile(c context.Context, mid int64) (res *v1.Profile, err error) {
  234. eg, _ := errgroup.WithContext(c)
  235. var detail *model.PassportDetail
  236. eg.Go(func() (e error) {
  237. if detail, e = d.PassportDetail(c, mid); e != nil {
  238. log.Error("d.PassPortDetail(%d) err %v", mid, e)
  239. // e = ecode.Degrade
  240. }
  241. return
  242. })
  243. var mb *mml.Member
  244. eg.Go(func() (e error) {
  245. if mb, e = d.mRPC.Member(c, &mml.ArgMemberMid{Mid: mid}); e != nil {
  246. log.Error("d.mRPC.Member(%d) err(%v)", mid, e)
  247. // e = ecode.Degrade
  248. }
  249. return
  250. })
  251. var moral *mml.Moral
  252. eg.Go(func() (e error) {
  253. if moral, e = d.mRPC.Moral(c, &mml.ArgMemberMid{Mid: mid}); e != nil {
  254. log.Error("d.mRPC.Member(%d) err(%v)", mid, e)
  255. e = ecode.Degrade
  256. }
  257. return
  258. })
  259. var realNameStatus *mml.RealnameStatus
  260. eg.Go(func() (e error) {
  261. if realNameStatus, e = d.mRPC.RealnameStatus(c, &mml.ArgMemberMid{Mid: mid}); e != nil {
  262. log.Error("d.mRPC.RealnameStatus(%d) err(%v)", mid, e)
  263. e = ecode.Degrade
  264. }
  265. return
  266. })
  267. var medal *sml.MedalInfo
  268. eg.Go(func() (e error) {
  269. if medal, e = d.suitRPC.MedalActivated(c, &sml.ArgMid{Mid: mid}); e != nil {
  270. log.Error("s.suitRPC.MedalActivated(%d) err %v", mid, e)
  271. e = ecode.Degrade
  272. }
  273. return
  274. })
  275. var block *bml.RPCResInfo
  276. eg.Go(func() (e error) {
  277. if block, e = d.mRPC.BlockInfo(c, &bml.RPCArgInfo{MID: mid}); e != nil {
  278. log.Error("s.dao.BlockInfo(%d) err %v", mid, e)
  279. e = ecode.Degrade
  280. }
  281. return
  282. })
  283. var vip *v1.VipInfo
  284. eg.Go(func() (e error) {
  285. if vip, e = d.Vip(c, mid); e != nil {
  286. log.Error("d.Vip(%d) err(%v)", mid, e)
  287. e = ecode.Degrade
  288. }
  289. return
  290. })
  291. var pendant *sml.PendantEquip
  292. eg.Go(func() (e error) {
  293. if pendant, e = d.suitRPC.Equipment(c, &sml.ArgEquipment{Mid: mid}); e != nil {
  294. log.Error("d.suitRPC.Equipment(%d) err(%v)", mid, e)
  295. e = ecode.Degrade
  296. }
  297. return
  298. })
  299. if err = eg.Wait(); err != nil && err != ecode.Degrade {
  300. return
  301. }
  302. pfl := &v1.Profile{Mid: mid}
  303. if mb != nil {
  304. pfl.Name = mb.Name
  305. pfl.Sign = mb.Sign
  306. pfl.Sex = mb.SexStr()
  307. pfl.Rank = int32(mb.Rank)
  308. pfl.Face = mb.Face
  309. if mb.OfficialInfo != nil {
  310. // pfl.Official = *mb.OfficialInfo
  311. pfl.Official.DeepCopyFromOfficialInfo(mb.OfficialInfo)
  312. }
  313. pfl.Level = mb.Cur
  314. pfl.Birthday = mb.Birthday
  315. }
  316. if block != nil {
  317. pfl.Silence = blockStatusToSilence(block.BlockStatus)
  318. }
  319. if detail != nil {
  320. pfl.JoinTime = int32(detail.JoinTime)
  321. pfl.EmailStatus = bindEmailStatus(detail.Email, detail.Spacesta)
  322. pfl.TelStatus = bindPhoneStatus(detail.Phone)
  323. pfl.IsTourist = boolToInt32(detail.IsTourist)
  324. }
  325. if realNameStatus != nil {
  326. pfl.Identification = identificationStatus(*realNameStatus)
  327. }
  328. pfl.Moral = parseMoral(moral)
  329. if medal != nil {
  330. pfl.Nameplate.Nid = int(medal.ID)
  331. pfl.Nameplate.Name = medal.Name
  332. pfl.Nameplate.Image = medal.Image
  333. pfl.Nameplate.ImageSmall = medal.ImageSmall
  334. pfl.Nameplate.Level = medal.LevelDesc
  335. pfl.Nameplate.Condition = medal.Condition
  336. }
  337. if vip != nil {
  338. pfl.Vip = *vip
  339. }
  340. if pendant != nil {
  341. pfl.Pendant.Pid = int(pendant.Pid)
  342. pfl.Pendant.Expire = int(pendant.Expires)
  343. if pendant.Pendant != nil {
  344. pfl.Pendant.Name = pendant.Pendant.Name
  345. pfl.Pendant.Image = fullImage(mid, pendant.Pendant.Image)
  346. }
  347. }
  348. res = pfl
  349. return
  350. }
  351. func blockStatusToSilence(status bml.BlockStatus) int32 {
  352. return boolToInt32(status == bml.BlockStatusForever || status == bml.BlockStatusLimit)
  353. }
  354. func bindEmailStatus(email string, spacesta int8) int32 {
  355. return boolToInt32(spacesta > -10 && len(email) > 0)
  356. }
  357. func bindPhoneStatus(phone string) int32 {
  358. return boolToInt32(len(phone) > 0)
  359. }
  360. func parseMoral(moral *mml.Moral) int32 {
  361. m := int32(mml.DefaultMoral)
  362. if moral != nil {
  363. m = int32(moral.Moral)
  364. }
  365. return m / 100
  366. }
  367. func identificationStatus(realNameStatus mml.RealnameStatus) int32 {
  368. return boolToInt32(realNameStatus == mml.RealnameStatusTrue)
  369. }
  370. func boolToInt32(b bool) int32 {
  371. if b {
  372. return 1
  373. }
  374. return 0
  375. }