175 lines
7.4 KiB
PHP
175 lines
7.4 KiB
PHP
<?php
|
|
|
|
|
|
namespace Yunshop\Redpack\services;
|
|
|
|
use app\common\services\finance\PointService;
|
|
use app\framework\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Yunshop\Redpack\model\Activity;
|
|
use Yunshop\Redpack\model\Challenge;
|
|
use Yunshop\Redpack\model\RedpackOrder;
|
|
use Yunshop\Redpack\model\PointExchangeLog;
|
|
use Yunshop\Redpack\services\ActivityService;
|
|
|
|
|
|
|
|
class RewardService
|
|
{
|
|
protected $uid;
|
|
protected $key;
|
|
protected $times;
|
|
protected $allows;
|
|
protected $activity_data;
|
|
protected $challenge;
|
|
|
|
public function __construct($request)
|
|
{
|
|
$this->key = $request->exchange_key;
|
|
$this->times = $request->times > 0 ? $request->times : 1;
|
|
$this->uid = $request->uid ? : \YunShop::app()->getMemberId();
|
|
$this->allows = ['credit','consume','invite','goods'];
|
|
$activity_data = ActivityService::getIssetActivity([$this->key],$request->activity_id);
|
|
$this->activity_data = empty($activity_data[0]) ? [] : $activity_data[0];
|
|
}
|
|
|
|
//兑换奖励次数主函数,根据前端传的兑换方式调用方法
|
|
public function reward(){
|
|
if(!method_exists($this,$this->key) || !in_array($this->key,$this->allows)) return $this->returnArr(0,'兑换方式错误');
|
|
|
|
if (!$this->activity_data) return $this->returnArr(0,'当前活动无法通过该方式兑换');
|
|
$this->activity_data['rule_data'] = array_column($this->activity_data['rule_data'],null,'key');
|
|
|
|
$this->challenge = ActivityService::getChallengeLog($this->activity_data,$this->uid);
|
|
$key = $this->key;
|
|
$res = $this->$key();
|
|
if (!$res || $res['code'] != 1){
|
|
return $this->returnArr(0,$res['msg']);
|
|
}
|
|
if(!$this->decreaseChallenge()){
|
|
return $this->returnArr(0,'兑换失败');
|
|
}
|
|
return $this->returnArr(1,'兑换成功');
|
|
}
|
|
|
|
//修改挑战数据表数据统一方法
|
|
public function decreaseChallenge()
|
|
{
|
|
$update_data = [];
|
|
switch ($this->key) {
|
|
case 'goods':
|
|
$update_data['challenge_number'] = DB::raw("challenge_number + ( (sum_goods - settle_goods) * {$this->activity_data['rule_data']['goods']['reward_num']} )");
|
|
$update_data['goods_reward_number'] = DB::raw("goods_reward_number + ( (sum_goods - settle_goods) * {$this->activity_data['rule_data']['goods']['reward_num']} )");
|
|
$update_data['settle_goods'] = DB::raw("sum_goods");
|
|
break;
|
|
case 'invite':
|
|
$update_data['challenge_number'] = DB::raw("challenge_number + ( ( (sum_recommend - settle_recommend) DIV {$this->activity_data['rule_data']['invite']['exchange_num']}) * {$this->activity_data['rule_data']['invite']['reward_num']} )");
|
|
$update_data['recommend_reward_number'] = DB::raw("recommend_reward_number + ( ( (sum_recommend - settle_recommend) DIV {$this->activity_data['rule_data']['invite']['exchange_num']}) * {$this->activity_data['rule_data']['invite']['reward_num']} )");
|
|
$update_data['settle_recommend'] = DB::raw("settle_recommend + ( ( (sum_recommend - settle_recommend) DIV {$this->activity_data['rule_data']['invite']['exchange_num']}) * {$this->activity_data['rule_data']['invite']['exchange_num']} )");
|
|
break;
|
|
case 'consume':
|
|
$update_data['challenge_number'] = DB::raw("challenge_number + ( ( (sum_consume - settle_consume) DIV {$this->activity_data['rule_data']['consume']['exchange_num']}) * {$this->activity_data['rule_data']['consume']['reward_num']} )");
|
|
$update_data['consume_reward_number'] = DB::raw("consume_reward_number + ( ( (sum_consume - settle_consume) DIV {$this->activity_data['rule_data']['consume']['exchange_num']}) * {$this->activity_data['rule_data']['consume']['reward_num']} )");
|
|
$update_data['settle_consume'] = DB::raw("settle_consume + ( ( (sum_consume - settle_consume) DIV {$this->activity_data['rule_data']['consume']['exchange_num']}) * {$this->activity_data['rule_data']['consume']['exchange_num']} )");
|
|
break;
|
|
case 'credit':
|
|
$update_data['challenge_number'] = DB::raw("challenge_number + {$this->times}");
|
|
$update_data['point_reward_number'] = DB::raw("point_reward_number + {$this->times}");
|
|
break;
|
|
}
|
|
|
|
if ($update_data) {
|
|
return Challenge::where('id', $this->challenge->id)->update($update_data);
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
//购买指定商品兑换
|
|
public function goods(){
|
|
|
|
$this->times = $this->challenge->sum_goods - $this->challenge->settle_goods;
|
|
if ($this->times <= 0){
|
|
return $this->returnArr('0','未满足兑换条件');
|
|
}
|
|
|
|
return $this->returnArr(1,'满足兑换条件');
|
|
}
|
|
|
|
|
|
//直推兑换
|
|
public function invite(){
|
|
$this->times = bcdiv($this->challenge->sum_recommend - $this->challenge->settle_recommend,$this->activity_data['rule_data']['invite']['exchange_num'],0);
|
|
if ($this->times <= 0){
|
|
return $this->returnArr('0','未满足兑换条件');
|
|
}
|
|
|
|
return $this->returnArr(1,'满足兑换条件');
|
|
}
|
|
|
|
//积分兑换
|
|
public function credit(){
|
|
$need_point = bcmul($this->activity_data['rule_data']['credit']['exchange_num'],$this->times,2);
|
|
$member_data = \app\common\models\Member::find($this->uid);
|
|
if (bccomp($need_point,$member_data->credit1,2) == 1){
|
|
return $this->returnArr('0','积分不足');
|
|
}
|
|
|
|
$point_log_id = PointExchangeLog::insertGetId([
|
|
'uniacid'=>$this->activity_data['uniacid'],
|
|
'uid'=>$this->uid,
|
|
'activity_id'=>$this->activity_data['id'],
|
|
'exchange_times'=>$this->times,
|
|
'created_at'=>time(),
|
|
'updated_at'=>time(),
|
|
]);
|
|
if ($point_log_id) {
|
|
$data = [
|
|
'point_income_type' => -1,
|
|
'member_id' => $this->challenge->uid,
|
|
'point_mode' => 69,
|
|
'point' => bcsub(0,$need_point,2),
|
|
'remark' => "口令红包兑换挑战次数,记录id为{$point_log_id}"
|
|
];
|
|
try {
|
|
$pointService = new PointService($data);
|
|
$pointService->changePoint($point_log_id);
|
|
} catch (\Exception $e) {
|
|
return $this->returnArr(0,'口令红包兑换次数出错');
|
|
}
|
|
$where = [
|
|
'point_mode' => 69,
|
|
'relation_id' => $point_log_id
|
|
];
|
|
$status = 2;
|
|
if (\app\common\models\finance\PointLog::where($where)->first()) {
|
|
$status = 1;
|
|
}
|
|
PointExchangeLog::where('id', $point_log_id)->update(['status' => $status]);
|
|
}
|
|
|
|
if (!$point_log_id || $status == 2){
|
|
return $this->returnArr(0,'积分兑换失败');
|
|
}
|
|
return $this->returnArr(1,'兑换成功');
|
|
}
|
|
|
|
|
|
//累计消费兑换
|
|
public function consume(){
|
|
|
|
$this->times = bcdiv($this->challenge->sum_consume - $this->challenge->settle_consume,$this->activity_data['rule_data']['consume']['exchange_num'],0);
|
|
if ($this->times <= 0){
|
|
return $this->returnArr('0','未满足兑换条件');
|
|
}
|
|
|
|
return $this->returnArr(1,'满足兑换条件');
|
|
|
|
}
|
|
|
|
|
|
public function returnArr($code,$msg='',$data=[]){
|
|
return ['code'=>$code,'msg'=>$msg,'data'=>$data];
|
|
}
|
|
|
|
} |