互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
6.4 KiB

5 years ago
  1. package pc
  2. import (
  3. "fmt"
  4. "hudongzhuanjia/controllers"
  5. "hudongzhuanjia/models"
  6. "hudongzhuanjia/services/pay"
  7. "hudongzhuanjia/utils"
  8. "hudongzhuanjia/utils/code"
  9. "strings"
  10. "time"
  11. "github.com/ouxuanserver/osmanthuswine/src/core"
  12. )
  13. type RewardCtl struct {
  14. controllers.AuthorCtl
  15. }
  16. type RewardResult struct {
  17. models.RewardHistory `xorm:"extends"`
  18. User *models.User `json:"user" xorm:"extends"`
  19. }
  20. //获取最新的打赏记录
  21. func (t *RewardCtl) Latest() {
  22. activityId := t.MustGetInt64("activity_id")
  23. rehearsalId := t.MustGetInt64("rehearsal_id")
  24. //获取打赏服务得id
  25. server := new(models.RewardServer)
  26. exist, err := server.GetByActivityId(activityId)
  27. t.CheckErr(err)
  28. t.Assert(exist, code.MSG_REWARD_NOT_EXIST, "打赏不存在")
  29. result := new(RewardResult)
  30. exist, err = core.GetXormAuto().Table(new(models.RewardHistory)).Alias("h").
  31. Join("LEFT", new(models.User).Alias("u"), "h.user_id=u.id and u.is_delete=0").
  32. Where("h.is_delete=0 and h.status=2 and h.reward_server_id=? and h.rehearsal_id=?", server.Id, rehearsalId).
  33. OrderBy("review_time desc").Get(result)
  34. t.CheckErr(err)
  35. if !exist {
  36. t.RAW(result)
  37. }
  38. result.User.Openid = ""
  39. result.User.Unionid = ""
  40. result.RewardHistory.RewardAmount = fmt.Sprintf("%.2f", result.RewardHistory.Amount)
  41. t.CheckErr(models.Save(map[string]interface{}{
  42. "id=": result.RewardHistory.Id,
  43. }, &models.RewardHistory{
  44. Status: 3,
  45. UpdatedAt: time.Now(),
  46. }, "status", "update_at"))
  47. t.RAW(result)
  48. }
  49. //获取待审核列表
  50. func (t *RewardCtl) WaitReview() {
  51. activityId := t.MustGetInt64("activity_id")
  52. rehearsalId := t.MustGetInt64("rehearsal_id")
  53. //获取打赏服务得id
  54. server := new(models.RewardServer)
  55. exist, err := server.GetByActivityId(activityId)
  56. t.CheckErr(err)
  57. t.Assert(exist, code.MSG_REWARD_NOT_EXIST, "打赏不存在")
  58. t.CheckErr(pay_service.BatchQueryByActivityId(activityId))
  59. t.CheckErr(pay_service.BatchQueryRefundByActivityId(activityId))
  60. result := make([]*RewardResult, 0)
  61. err = core.GetXormAuto().Table(new(models.RewardHistory)).Alias("h").
  62. Join("LEFT", new(models.User).Alias("u"), "h.user_id=u.id and u.is_delete=0").
  63. Where("h.is_delete=0 and h.status=0 and h.reward_server_id=? and h.rehearsal_id=?",
  64. server.Id, rehearsalId).OrderBy("h.created_at desc").Find(&result)
  65. t.CheckErr(err)
  66. //根据打赏服务得id获取待审核得打赏列表
  67. t.JSON(map[string]interface{}{
  68. "total": len(result),
  69. "list": result,
  70. })
  71. }
  72. //审核
  73. func (t *RewardCtl) Review() {
  74. // todo: false 代表通过, true 代表不通过
  75. idList := t.MustGet("ids")
  76. status := t.MustGetBool("status")
  77. ids := strings.Split(idList, "|")
  78. //rehearsalId := t.MustGetInt64("rehearsal_id")
  79. result := make([]*models.RewardHistory, 0)
  80. err := core.GetXormAuto().Where("is_delete=0").In("id", ids).Find(&result)
  81. t.CheckErr(err)
  82. //if rehearsalId != 0 { // 彩排不需要金额
  83. // t.RAW("审核成功")
  84. // return
  85. //}
  86. // 审核
  87. for _, v := range result {
  88. if v.Status != 0 {
  89. t.ERROR("该打赏已处理", code.MSG_USER_DATA_ERROR)
  90. }
  91. if status { // false 通过 , true 不通过
  92. // 回退金额给用户
  93. // 应该微信退款
  94. _, err := v.UpdateStatus(v.Id, 1)
  95. t.CheckErr(err)
  96. //if rehearsalId != 0 { // 彩排不需要金额
  97. // continue
  98. //}
  99. order := new(models.UserOrder)
  100. exist, err := models.GetById(order, v.UserOrderId)
  101. t.CheckErr(err)
  102. t.Assert(exist, code.MSG_USER_ORDER_NOT_EXIST, "用户支付订单不存在")
  103. _, err = pay_service.Refund("欧轩互动-打赏拉黑", order.OutTradeNo, order.OpenId, order.GoodType, order.TotalFee, order.TotalFee, order.UserId, order.ActivityId)
  104. t.CheckErr(err)
  105. } else {
  106. //增加客户的金额
  107. _, err := v.UpdateStatus(v.Id, 2)
  108. t.CheckErr(err)
  109. //if rehearsalId != 0 { // 彩排不需要金额
  110. // continue
  111. //}
  112. uid := t.MustGetUID()
  113. customer := new(models.Customer)
  114. exist, err := models.GetById(customer, uid)
  115. t.CheckErr(err)
  116. t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
  117. wallet := new(models.RewardWallet)
  118. _, err = wallet.IncrBalance(uid, v.Amount)
  119. t.CheckErr(err)
  120. _, err = core.GetXormAuto().InsertOne(&models.RewardWalletHistory{
  121. CustomerId: uid,
  122. Money: v.Amount,
  123. Type: "打赏",
  124. UserId: v.UserId,
  125. RewardHistoryId: v.Id,
  126. RewardAccount: v.RewardAmount,
  127. Status: "交易成功",
  128. Mark: "打赏金额",
  129. CreatedAt: time.Now(),
  130. UpdatedAt: time.Now(),
  131. })
  132. t.CheckErr(err)
  133. }
  134. }
  135. // 发送审核的信息
  136. t.RAW("审核成功")
  137. }
  138. //获取打赏得总金额
  139. func (t *RewardCtl) Amount() {
  140. activityId := t.MustGetInt64("activity_id")
  141. rehearsalId := t.MustGetInt64("rehearsal_id")
  142. //获取打赏服务得id
  143. server := new(models.RewardServer)
  144. exist, err := server.GetByActivityId(activityId)
  145. t.CheckErr(err)
  146. t.Assert(exist, code.MSG_REWARD_NOT_EXIST, "打赏不存在")
  147. totalAmount, err := core.GetXormAuto().Where("reward_server_id=? and status=3 and is_delete=0 "+
  148. " and rehearsal_id=?", server.Id, rehearsalId).Sum(new(models.RewardHistory), "amount")
  149. t.CheckErr(err)
  150. t.DisplayByData(map[string]interface{}{
  151. "amount": utils.Float64CusDecimal(totalAmount, 2),
  152. })
  153. }
  154. // 黑名单
  155. func (t *RewardCtl) Blacklist() {
  156. activityId := t.MustGetInt64("activity_id")
  157. rehearsalId := t.MustGetInt64("rehearsal_id")
  158. // 获取打赏的服务id
  159. server := new(models.RewardServer)
  160. exist, err := server.GetByActivityId(activityId)
  161. t.CheckErr(err)
  162. t.Assert(exist, code.MSG_REWARD_NOT_EXIST, "打赏不存在")
  163. // 根据打赏服务id获取待审核的名单
  164. // status=true 代表审核不通过还是通过
  165. // 目前表示不通过,bully_screen也一样
  166. result := make([]*RewardResult, 0)
  167. err = core.GetXormAuto().Table(new(models.RewardHistory)).Alias("h").
  168. Join("LEFT", new(models.User).Alias("u"), "h.user_id=u.id and u.is_delete=0").
  169. Where("h.is_delete=0 and h.status=1 and h.reward_server_id=? and h.rehearsal_id=?",
  170. server.Id, rehearsalId).OrderBy("review_time desc").Find(&result)
  171. t.CheckErr(err)
  172. t.JSON(map[string]interface{}{
  173. "total": len(result),
  174. "list": result,
  175. })
  176. }
  177. // 开启打赏
  178. func (t *RewardCtl) Details() {
  179. activityId := t.MustGetInt64("activity_id")
  180. server := new(models.RewardServer)
  181. exist, err := server.GetByActivityId(activityId)
  182. t.CheckErr(err)
  183. t.Assert(exist, code.MSG_REWARD_NOT_EXIST, "打赏不存在")
  184. t.JSON(map[string]interface{}{
  185. "server": server,
  186. })
  187. }