0
0
Files
Babyclass/app/Models/ActivityGift.php
2020-08-04 10:09:42 +08:00

106 lines
3.3 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Models;
use Auth;
use Carbon\Carbon;
use RuLong\Order\Models\Order;
class ActivityGift extends Model
{
public function activity()
{
return $this->belongsTo(Activity::class)->withDefault();
}
/**
* 是否可领取0不可领取,1已经领取,2可领取,负数相差人数
* @Date:2019-01-09T11:24:56+0800
* @return [type] [description]
*/
public function getCanGiftAttribute()
{
$user = Auth::user();
if ($user) {
if ($user->account->act_a == 0) {
//未曾满仓
return 0;
}
//今日时间范围
$toDayTime = [
Carbon::today()->startOfDay()->toDateTimeString(),
Carbon::today()->endOfDay()->toDateTimeString(),
];
$childNum = 0;
$todayLogs = $user->account->logs()->where('type', 'act_a')->count() ?: 0;
if ($user->account->act_a == 1 && $todayLogs) {
//当日第一次满仓人员份额减10
$childNum -= 10;
}
//今日已经领取的活动奖品
$orderList = Order::where('user_id', $user->id)
->where('item_type', 'ACTIVITY_GIFT')
->whereBetween('created_at', $toDayTime)
// ->where('state', 'not in', ['CLOSED', 'CANCEL'])
->whereRaw('substring(cast(status as char),1,1) = 1')
->pluck('item_id');
foreach ($orderList as $key => $item_id) {
//循环判断已经领取的奖品,等于当前奖品的返回已经领取
//不是当前商品的记录消耗数量
if ($item_id == $this->id) {
return 1;
} else {
$childNum -= self::where('id', $item_id)->value('consume_mode') ?: 0;
}
}
$toDayChild = $user->identity->childrentime($toDayTime) + $childNum; //今日总推荐数量
$childNum += $toDayChild; //消耗数量与今日数量抵充
if ($childNum >= $this->mode) {
//满足领取人数可领取
return 2;
} else {
return $childNum - $this->mode;
}
} else {
return 0;
}
}
public function goods()
{
return $this->belongsTo(Goods::class, 'goods_id', 'id');
}
public function param()
{
return $this->belongsTo(GoodsParams::class, 'param_id', 'id');
}
public function getcanCreateOrderAttribute()
{
$data = array();
$data['state'] = false;
$data['tag'] = '兑换';
if ($this->activity->status == 1) {
if ($this->can_gift == 0) {
$data['tag'] = '未满仓';
} elseif ($this->can_gift == 1) {
$data['tag'] = '已领取';
} elseif ($this->can_gift == 2) {
$data['state'] = true;
$data['tag'] = '兑换';
} else {
$data['tag'] = '差' . abs($this->can_gift) . '人';
}
} elseif ($this->activity->status == -1) {
$data['tag'] = '已结束';
} elseif ($this->activity->status == 0) {
$data['tag'] = '未开始';
}
return $data;
}
}