156 lines
6.0 KiB
PHP
156 lines
6.0 KiB
PHP
<?php
|
|
namespace Yunshop\CulturalSpace\models;
|
|
|
|
|
|
use app\common\facades\Setting;
|
|
use app\common\models\BaseModel;
|
|
use app\common\models\Member;
|
|
use app\common\models\member\MemberParent;
|
|
use app\common\models\OrderGoods;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class CulturalSpace extends BaseModel{
|
|
|
|
public $table = 'yz_cultural_space';
|
|
public $timestamps = false;
|
|
protected $fillable = [
|
|
'uniacid',
|
|
'uid',
|
|
'contribution'
|
|
];
|
|
|
|
|
|
/**
|
|
* Common: 购买商品奖励贡献值
|
|
* Author: wu-hui
|
|
* Time: 2023/11/02 17:59
|
|
* @param $uid
|
|
* @param $orderId
|
|
*/
|
|
public function buyGoodsGiveContribution($uid,$orderId){
|
|
\Log::debug('文创空间 - 购买商品奖励贡献值',['uid' => $uid,'order_id' => $orderId]);
|
|
DB::beginTransaction();
|
|
try{
|
|
$set = Setting::get('plugin.cultural_space_set');
|
|
if($set['is_give_contribution'] != 1 || (float)$set['contribution_ratio'] <= 0) throw new \Exception('文创空间 - 购买商品奖励贡献值 - 未开启贡献值奖励或者奖励比例为0');
|
|
// 获取直推上级id && 获取订单商品信息
|
|
$parentUid = (int)MemberParent::getParentId($uid);
|
|
$orderGoodsList = (array)$this->getGoodsModel($orderId)
|
|
->where('yz_goods_cultural_space.is_give_contribution',1)
|
|
->get()
|
|
->makeHidden(['buttons','after_sales','order'])
|
|
->toArray();
|
|
if(count($orderGoodsList) <= 0) throw new \Exception('文创空间 - 购买商品奖励贡献值 - 无奖励商品信息');
|
|
// 获取文创空间用户信息
|
|
$memberList = $parentUid <= 0 ? $this->getCulturalSpace([$uid]) : $this->getCulturalSpace([$uid,$parentUid]);
|
|
$userName = Member::getMemberById($uid,['realname','nickname'])->username ?? '';
|
|
// 循环商品处理奖励贡献值
|
|
$changeList = [];
|
|
foreach($orderGoodsList as $goodsInfo){
|
|
// 计算奖励贡献值
|
|
$contribution = (float)sprintf('%.2f',$goodsInfo['payment_amount'] * (float)$set['contribution_ratio'] / 100);
|
|
if($contribution > 0){
|
|
foreach($memberList as $memberInfo){
|
|
// 用户当前持有数量
|
|
$changeFront = (float)$memberList[$memberInfo['uid']]['contribution'];
|
|
// 变更后的数量
|
|
$changeAfter = (float)sprintf("%.2f",$changeFront + $contribution);
|
|
// 记录变更信息
|
|
$changeList[] = [
|
|
'uniacid' => $goodsInfo['uniacid'],
|
|
'uid' => $memberInfo['uid'],
|
|
'goods_id' => $goodsInfo['goods_id'],
|
|
'order_id' => $orderId,
|
|
'order_goods_id' => $goodsInfo['id'],
|
|
'change_type' => 1,
|
|
'change_quantity' => $contribution,
|
|
'change_front' => $changeFront,
|
|
'change_after' => $changeAfter,
|
|
'remark' => ($memberInfo['uid'] == $uid ? '' : "【{$userName}】") . "购买商品【{$goodsInfo['title']}】赠送",
|
|
'created_at' => time(),
|
|
];
|
|
// 刷新持有处理
|
|
$memberList[$memberInfo['uid']]['contribution'] = $changeAfter;
|
|
}
|
|
}
|
|
}
|
|
// 数据操作
|
|
if(count($changeList) > 0) {
|
|
$this->batchUpdate($memberList,'uid','uid');
|
|
ContributionLog::insert($changeList);
|
|
}
|
|
|
|
DB::commit();
|
|
}catch(\Exception $e){
|
|
\Log::debug('文创空间 - 购买商品奖励贡献值 - 错误抛出',$e->getMessage());
|
|
DB::rollBack();
|
|
}
|
|
|
|
return;
|
|
}
|
|
/**
|
|
* Common: 获取订单商品查询model
|
|
* Author: wu-hui
|
|
* Time: 2023/11/02 16:26
|
|
* @param $orderId
|
|
* @return mixed
|
|
*/
|
|
private function getGoodsModel($orderId){
|
|
return OrderGoods::uniacid()
|
|
->select([
|
|
'yz_order_goods.id',
|
|
'yz_order_goods.uniacid',
|
|
'yz_order_goods.goods_id',
|
|
'yz_order_goods.total',
|
|
'yz_order_goods.title',
|
|
'yz_order_goods.payment_amount',
|
|
])
|
|
->leftJoin('yz_goods_cultural_space', 'yz_goods_cultural_space.goods_id', 'yz_order_goods.goods_id')
|
|
->where('yz_order_goods.order_id',$orderId)
|
|
->where('yz_goods_cultural_space.is_open',1);
|
|
}
|
|
/**
|
|
* Common: 根据用户ids获取用户文创空间相关信息(如果不存在则添加默认信息并且返回默认信息)
|
|
* Author: wu-hui
|
|
* Time: 2023/11/02 17:36
|
|
* @param $ids
|
|
* @return array
|
|
*/
|
|
private function getCulturalSpace($ids){
|
|
// 获取已经存在的信息
|
|
$list = self::uniacid()
|
|
->select(['id','uid','contribution'])
|
|
->whereIn('uid',$ids)
|
|
->get()
|
|
->keyBy('uid')
|
|
->toArray();
|
|
// 循环处理:不存在则添加,并且赋值默认值;存在则使用已经存在的信息
|
|
$memberList = [];
|
|
$insertData = [];
|
|
foreach($ids as $userId){
|
|
if($list[$userId]){
|
|
// 存在
|
|
$memberList[$userId] = $list[$userId];
|
|
}else{
|
|
// 不存在
|
|
$insertData[] = [
|
|
'uniacid' => \YunShop::app()->uniacid,
|
|
'uid' => $userId,
|
|
];
|
|
$memberList[$userId] = [
|
|
'uniacid' => \YunShop::app()->uniacid,
|
|
'uid' => $userId,
|
|
'contribution' => 0
|
|
];
|
|
}
|
|
}
|
|
|
|
if($insertData) self::insert($insertData);
|
|
|
|
return $memberList;
|
|
}
|
|
|
|
|
|
|
|
}
|