互动
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

package pc
import (
"fmt"
"hudongzhuanjia/controllers"
"hudongzhuanjia/models"
"hudongzhuanjia/services/pay"
"hudongzhuanjia/utils"
"hudongzhuanjia/utils/code"
"strings"
"time"
"github.com/ouxuanserver/osmanthuswine/src/core"
)
type RewardCtl struct {
controllers.AuthorCtl
}
type RewardResult struct {
models.RewardHistory `xorm:"extends"`
User *models.User `json:"user" xorm:"extends"`
}
//获取最新的打赏记录
func (t *RewardCtl) Latest() {
activityId := t.MustGetInt64("activity_id")
rehearsalId := t.MustGetInt64("rehearsal_id")
//获取打赏服务得id
server := new(models.RewardServer)
exist, err := server.GetByActivityId(activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_REWARD_NOT_EXIST, "打赏不存在")
result := new(RewardResult)
exist, err = core.GetXormAuto().Table(new(models.RewardHistory)).Alias("h").
Join("LEFT", new(models.User).Alias("u"), "h.user_id=u.id and u.is_delete=0").
Where("h.is_delete=0 and h.status=2 and h.reward_server_id=? and h.rehearsal_id=?", server.Id, rehearsalId).
OrderBy("review_time desc").Get(result)
t.CheckErr(err)
if !exist {
t.RAW(result)
}
result.User.Openid = ""
result.User.Unionid = ""
result.RewardHistory.RewardAmount = fmt.Sprintf("%.2f", result.RewardHistory.Amount)
t.CheckErr(models.Save(map[string]interface{}{
"id=": result.RewardHistory.Id,
}, &models.RewardHistory{
Status: 3,
UpdatedAt: time.Now(),
}, "status", "update_at"))
t.RAW(result)
}
//获取待审核列表
func (t *RewardCtl) WaitReview() {
activityId := t.MustGetInt64("activity_id")
rehearsalId := t.MustGetInt64("rehearsal_id")
//获取打赏服务得id
server := new(models.RewardServer)
exist, err := server.GetByActivityId(activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_REWARD_NOT_EXIST, "打赏不存在")
t.CheckErr(pay_service.BatchQueryByActivityId(activityId))
t.CheckErr(pay_service.BatchQueryRefundByActivityId(activityId))
result := make([]*RewardResult, 0)
err = core.GetXormAuto().Table(new(models.RewardHistory)).Alias("h").
Join("LEFT", new(models.User).Alias("u"), "h.user_id=u.id and u.is_delete=0").
Where("h.is_delete=0 and h.status=0 and h.reward_server_id=? and h.rehearsal_id=?",
server.Id, rehearsalId).OrderBy("h.created_at desc").Find(&result)
t.CheckErr(err)
//根据打赏服务得id获取待审核得打赏列表
t.JSON(map[string]interface{}{
"total": len(result),
"list": result,
})
}
//审核
func (t *RewardCtl) Review() {
// todo: false 代表通过, true 代表不通过
idList := t.MustGet("ids")
status := t.MustGetBool("status")
ids := strings.Split(idList, "|")
//rehearsalId := t.MustGetInt64("rehearsal_id")
result := make([]*models.RewardHistory, 0)
err := core.GetXormAuto().Where("is_delete=0").In("id", ids).Find(&result)
t.CheckErr(err)
//if rehearsalId != 0 { // 彩排不需要金额
// t.RAW("审核成功")
// return
//}
// 审核
for _, v := range result {
if v.Status != 0 {
t.ERROR("该打赏已处理", code.MSG_USER_DATA_ERROR)
}
if status { // false 通过 , true 不通过
// 回退金额给用户
// 应该微信退款
_, err := v.UpdateStatus(v.Id, 1)
t.CheckErr(err)
//if rehearsalId != 0 { // 彩排不需要金额
// continue
//}
order := new(models.UserOrder)
exist, err := models.GetById(order, v.UserOrderId)
t.CheckErr(err)
t.Assert(exist, code.MSG_USER_ORDER_NOT_EXIST, "用户支付订单不存在")
_, err = pay_service.Refund("欧轩互动-打赏拉黑", order.OutTradeNo, order.OpenId, order.GoodType, order.TotalFee, order.TotalFee, order.UserId, order.ActivityId)
t.CheckErr(err)
} else {
//增加客户的金额
_, err := v.UpdateStatus(v.Id, 2)
t.CheckErr(err)
//if rehearsalId != 0 { // 彩排不需要金额
// continue
//}
uid := t.MustGetUID()
customer := new(models.Customer)
exist, err := models.GetById(customer, uid)
t.CheckErr(err)
t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
wallet := new(models.RewardWallet)
_, err = wallet.IncrBalance(uid, v.Amount)
t.CheckErr(err)
_, err = core.GetXormAuto().InsertOne(&models.RewardWalletHistory{
CustomerId: uid,
Money: v.Amount,
Type: "打赏",
UserId: v.UserId,
RewardHistoryId: v.Id,
RewardAccount: v.RewardAmount,
Status: "交易成功",
Mark: "打赏金额",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
})
t.CheckErr(err)
}
}
// 发送审核的信息
t.RAW("审核成功")
}
//获取打赏得总金额
func (t *RewardCtl) Amount() {
activityId := t.MustGetInt64("activity_id")
rehearsalId := t.MustGetInt64("rehearsal_id")
//获取打赏服务得id
server := new(models.RewardServer)
exist, err := server.GetByActivityId(activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_REWARD_NOT_EXIST, "打赏不存在")
totalAmount, err := core.GetXormAuto().Where("reward_server_id=? and status=3 and is_delete=0 "+
" and rehearsal_id=?", server.Id, rehearsalId).Sum(new(models.RewardHistory), "amount")
t.CheckErr(err)
t.DisplayByData(map[string]interface{}{
"amount": utils.Float64CusDecimal(totalAmount, 2),
})
}
// 黑名单
func (t *RewardCtl) Blacklist() {
activityId := t.MustGetInt64("activity_id")
rehearsalId := t.MustGetInt64("rehearsal_id")
// 获取打赏的服务id
server := new(models.RewardServer)
exist, err := server.GetByActivityId(activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_REWARD_NOT_EXIST, "打赏不存在")
// 根据打赏服务id获取待审核的名单
// status=true 代表审核不通过还是通过
// 目前表示不通过,bully_screen也一样
result := make([]*RewardResult, 0)
err = core.GetXormAuto().Table(new(models.RewardHistory)).Alias("h").
Join("LEFT", new(models.User).Alias("u"), "h.user_id=u.id and u.is_delete=0").
Where("h.is_delete=0 and h.status=1 and h.reward_server_id=? and h.rehearsal_id=?",
server.Id, rehearsalId).OrderBy("review_time desc").Find(&result)
t.CheckErr(err)
t.JSON(map[string]interface{}{
"total": len(result),
"list": result,
})
}
// 开启打赏
func (t *RewardCtl) Details() {
activityId := t.MustGetInt64("activity_id")
server := new(models.RewardServer)
exist, err := server.GetByActivityId(activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_REWARD_NOT_EXIST, "打赏不存在")
t.JSON(map[string]interface{}{
"server": server,
})
}