pendant.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package usersuit
  2. import (
  3. "context"
  4. "math"
  5. "go-common/app/interface/main/account/model"
  6. cmdl "go-common/app/service/main/coin/model"
  7. memmdl "go-common/app/service/main/member/model"
  8. usmdl "go-common/app/service/main/usersuit/model"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "go-common/library/net/metadata"
  12. )
  13. // Equip user pendant equip.
  14. func (s *Service) Equip(c context.Context, mid, pid int64, status int8, source int64) (err error) {
  15. return s.usRPC.Equip(c, &usmdl.ArgEquip{Mid: mid, Pid: pid, Status: status, Source: source})
  16. }
  17. // Equipment get pendant current equipment
  18. func (s *Service) Equipment(c context.Context, mid int64) (equipPHP *model.EquipPHP, err error) {
  19. var equip *usmdl.PendantEquip
  20. ip := metadata.String(c, metadata.RemoteIP)
  21. if equip, err = s.usRPC.Equipment(c, &usmdl.ArgEquipment{Mid: mid, IP: ip}); err != nil {
  22. log.Error("s.usRPC.Equipment(%d) error(%v)", mid, err)
  23. return
  24. }
  25. var coin float64
  26. if coin, err = s.coinRPC.UserCoins(c, &cmdl.ArgCoinInfo{Mid: mid}); err != nil {
  27. log.Error("s.coinRPC.UserCoins(%d) error(%v)", mid, err)
  28. return
  29. }
  30. var base *memmdl.BaseInfo
  31. if base, err = s.memRPC.Base(c, &memmdl.ArgMemberMid{Mid: mid, RemoteIP: ip}); err != nil {
  32. log.Error("s.memRPC.Base(%d) error(%v)", mid, err)
  33. return
  34. }
  35. equipPHP = &model.EquipPHP{
  36. Coins: coin,
  37. FaceURL: base.Face,
  38. }
  39. if equip == nil || equip.Pendant == nil {
  40. log.Info("s.Equipment(%d) usequip(%+v) or usequip.Pendant(%+v) is nil", equip, equip.Pendant)
  41. return
  42. }
  43. equipPHP.Pid = equip.Pid
  44. equipPHP.Image = model.FormatImgURL(mid, equip.Pendant.Image)
  45. equipPHP.ImageModel = model.FormatImgURL(mid, equip.Pendant.ImageModel)
  46. return
  47. }
  48. // Pendant return pendant info.
  49. func (s *Service) Pendant(c context.Context, pid int64) (pendantPHP *model.PendantPHP, err error) {
  50. var pendant *usmdl.Pendant
  51. ip := metadata.String(c, metadata.RemoteIP)
  52. if pendant, err = s.dao.Pendant(c, pid, ip); err != nil {
  53. log.Error("s.dao.Group(%d) error(%v)", pid, err)
  54. return
  55. }
  56. pendantPHP = &model.PendantPHP{}
  57. pendantPHP.Name = pendant.Name
  58. pendantPHP.Pid = pendant.ID
  59. pendantPHP.Image = model.FormatImgURL(pid, pendant.Image)
  60. pendantPHP.ImageModel = model.FormatImgURL(pid, pendant.ImageModel)
  61. return
  62. }
  63. // Group return pendant group info.
  64. func (s *Service) Group(c context.Context, mid int64) (groupPHP []*model.GroupPHP, err error) {
  65. var groups []*usmdl.PendantGroupInfo
  66. ip := metadata.String(c, metadata.RemoteIP)
  67. if groups, err = s.dao.Group(c, ip); err != nil {
  68. log.Error("s.dao.Group(%d) error(%v)", mid, err)
  69. return
  70. }
  71. for _, g := range groups {
  72. if g.ID == 30 || g.ID == 31 {
  73. continue
  74. }
  75. for _, p := range g.SubPendant {
  76. p.BCoin = p.BCoin / 100
  77. p.Image = model.FormatImgURL(mid, p.Image)
  78. p.ImageModel = model.FormatImgURL(mid, p.ImageModel)
  79. }
  80. gh := &model.GroupPHP{}
  81. gh.Name = g.Name
  82. gh.Count = g.Number
  83. gh.Pendant = g.SubPendant
  84. groupPHP = append(groupPHP, gh)
  85. }
  86. return
  87. }
  88. // GroupEntry return vip pendant.
  89. func (s *Service) GroupEntry(c context.Context, mid int64) (entryPHP []*model.GroupEntryPHP, err error) {
  90. var group *usmdl.PendantGroupInfo
  91. ip := metadata.String(c, metadata.RemoteIP)
  92. if group, err = s.dao.GroupEntry(c, ip); err != nil {
  93. log.Error("s.dao.GroupEntry(%d) error(%v)", mid, ip)
  94. return
  95. }
  96. if group == nil {
  97. log.Info("s.dao.GroupEntry(%d) result value is nil", mid)
  98. return
  99. }
  100. for _, p := range group.SubPendant {
  101. entry := &model.GroupEntryPHP{}
  102. entry.Pid = p.ID
  103. entry.Name = p.Name
  104. entry.Money = p.Point
  105. entry.Image = model.FormatImgURL(mid, p.Image)
  106. entry.ImageModel = model.FormatImgURL(mid, p.ImageModel)
  107. entryPHP = append(entryPHP, entry)
  108. }
  109. return
  110. }
  111. // GroupVIP return vip pendant.
  112. func (s *Service) GroupVIP(c context.Context, mid int64) (vipPHP []*model.GroupVipPHP, err error) {
  113. var group *usmdl.PendantGroupInfo
  114. ip := metadata.String(c, metadata.RemoteIP)
  115. if group, err = s.dao.GroupVip(c, ip); err != nil {
  116. log.Error("s.dao.GroupVip(%d) error(%v)", mid, ip)
  117. return
  118. }
  119. if group == nil {
  120. log.Info("s.dao.GroupEntry(%d) result value is nil", mid)
  121. return
  122. }
  123. for _, p := range group.SubPendant {
  124. vip := &model.GroupVipPHP{}
  125. vip.Pid = p.ID
  126. vip.Name = p.Name
  127. vip.Money = 0
  128. vip.MoneyType = 3
  129. vip.Expire = 2678400
  130. vip.Image = model.FormatImgURL(mid, p.Image)
  131. vip.ImageModel = model.FormatImgURL(mid, p.ImageModel)
  132. vipPHP = append(vipPHP, vip)
  133. }
  134. return
  135. }
  136. // VipGet pc vip install pendant.
  137. func (s *Service) VipGet(c context.Context, mid, pid int64, activated int8) (err error) {
  138. err = s.Equip(c, mid, pid, int8(activated), usmdl.EquipFromVIP)
  139. return
  140. }
  141. // CheckOrder check order by oid.
  142. func (s *Service) CheckOrder(c context.Context, mid int64, orderID string) (err error) {
  143. var hs []*usmdl.PendantOrderInfo
  144. ip := metadata.String(c, metadata.RemoteIP)
  145. if hs, _, err = s.dao.OrderHistory(c, mid, 1, 0, orderID, ip); err != nil {
  146. log.Error("s.dao.OrderHistory(%d) error(%v)", mid, err)
  147. return
  148. }
  149. if len(hs) == 0 {
  150. err = ecode.PendantOrderNotFound
  151. log.Info("s.dao.OrderHistory(%d) orderID(%d) error(%v)", mid, orderID, err)
  152. return
  153. }
  154. if hs[0].Stauts != 1 {
  155. err = ecode.PendantOrderNotFound
  156. log.Info("s.dao.OrderHistory(%d) orderID(%d) order not complete", mid, orderID, err)
  157. return
  158. }
  159. return
  160. }
  161. // Order pay pandent by coin/bcoin/point.
  162. func (s *Service) Order(c context.Context, mid, pid, timeLength int64, moneyType int8) (res interface{}, err error) {
  163. var payInfo *usmdl.PayInfo
  164. ip := metadata.String(c, metadata.RemoteIP)
  165. if payInfo, err = s.dao.Order(c, mid, pid, timeLength, moneyType, ip); err != nil {
  166. log.Error("s.dao.Order(%d) error(%v)", mid, err)
  167. return
  168. }
  169. if payInfo != nil {
  170. payInfo.PayURL = "https://pay.bilibili.com" + payInfo.PayURL
  171. res = payInfo
  172. return
  173. }
  174. if moneyType == 2 {
  175. log.Info("s.dao.Order(%d) pid(%d) buy type with point", mid, pid)
  176. s.Equip(c, mid, pid, 2, usmdl.EquipFromPackage)
  177. }
  178. var pkgs []*usmdl.PendantPackage
  179. if pkgs, err = s.dao.Packages(c, mid, ip); err != nil {
  180. log.Error("s.dao.Packages(%d) error(%v)", mid, err)
  181. return
  182. }
  183. var pendant *usmdl.PendantPackage
  184. for _, pkg := range pkgs {
  185. if pkg.Pid == pid {
  186. pendant = pkg
  187. }
  188. }
  189. if pendant != nil {
  190. res = &struct {
  191. Msg string `json:"msg"`
  192. Expire int64 `json:"expire"`
  193. }{
  194. Msg: "您已成功购买" + pendant.Pendant.Name,
  195. Expire: pendant.Expires,
  196. }
  197. }
  198. return
  199. }
  200. // My get my pandent
  201. func (s *Service) My(c context.Context, mid int64) (my []*model.MyPHP, err error) {
  202. var equip *usmdl.PendantEquip
  203. ip := metadata.String(c, metadata.RemoteIP)
  204. if equip, err = s.usRPC.Equipment(c, &usmdl.ArgEquipment{Mid: mid, IP: ip}); err != nil {
  205. log.Error("s.usRPC.Equipment(%d) error(%v)", mid, err)
  206. return
  207. }
  208. var pkgs []*usmdl.PendantPackage
  209. if pkgs, err = s.dao.Packages(c, mid, ip); err != nil {
  210. log.Error("s.dao.Packages(%d) error(%v)", mid, err)
  211. return
  212. }
  213. for _, pkg := range pkgs {
  214. m := &model.MyPHP{}
  215. m.Pid = pkg.Pid
  216. m.Name = pkg.Pendant.Name
  217. m.MoneyType = int8(pkg.Type)
  218. m.Image = model.FormatImgURL(mid, pkg.Pendant.Image)
  219. m.ImageModel = model.FormatImgURL(mid, pkg.Pendant.ImageModel)
  220. m.Expire = pkg.Expires
  221. m.IsOnline = 1
  222. if equip != nil && equip.Pid == pkg.Pid {
  223. m.IsActivated = 1
  224. }
  225. my = append(my, m)
  226. }
  227. return
  228. }
  229. // MyHistory get my pandent buy history.
  230. func (s *Service) MyHistory(c context.Context, mid, page int64) (res map[string]interface{}, err error) {
  231. var (
  232. hs []*usmdl.PendantOrderInfo
  233. myhs []*model.MyHistoryPHP
  234. count map[string]int64
  235. ip = metadata.String(c, metadata.RemoteIP)
  236. )
  237. if hs, count, err = s.dao.OrderHistory(c, mid, page, 0, "", ip); err != nil {
  238. log.Error("s.dao.OrderHistory(%d) error(%v)", mid, err)
  239. return
  240. }
  241. if len(hs) == 0 {
  242. log.Info("s.dao.OrderHistory(%d) result len eq(0)", mid)
  243. return
  244. }
  245. for _, h := range hs {
  246. my := &model.MyHistoryPHP{}
  247. my.Pid = h.Pid
  248. my.Image = model.FormatImgURL(mid, h.Image)
  249. my.Name = h.Name
  250. my.BuyTime = h.BuyTime
  251. my.PayID = h.PayID
  252. my.Cost = h.Cost
  253. my.TimeLength = h.TimeLength
  254. myhs = append(myhs, my)
  255. }
  256. res = make(map[string]interface{})
  257. count["page_count"] = int64(math.Ceil(float64(count["result_count"]) / float64(count["page_size"])))
  258. res["page"] = count
  259. res["result"] = myhs
  260. return
  261. }