添加:商品支持设置是否赠送兑换额度

添加:购买商品赠送兑换额度
This commit is contained in:
wuhui_zzw 2024-01-10 18:15:30 +08:00
parent 8e8f25ebc8
commit 42b4e8e7bb
10 changed files with 297 additions and 36 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace app\common\dao\user;
use app\common\dao\BaseDao;
use app\common\model\user\ExchangeQuota;
class ExchangeQuotaDao extends BaseDao{
protected function getModel(): string{
return ExchangeQuota::class;
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace app\common\dao\user;
use app\common\dao\BaseDao;
use app\common\model\user\ExchangeQuotaRecord;
class ExchangeQuotaRecordDao extends BaseDao{
protected function getModel(): string{
return ExchangeQuotaRecord::class;
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace app\common\model\user;
use app\common\model\BaseModel;
/**
* Common: 兑换额度总数
* Author: wu-hui
* Time: 2024/01/10 16:45
* Class ExchangeQuota
* @package app\common\model\user
*/
class ExchangeQuota extends BaseModel
{
/**
* @return string
* @author xaboy
* @day 2020-03-30
*/
public static function tablePk(): string
{
return 'id';
}
/**
* @return string
* @author xaboy
* @day 2020-03-30
*/
public static function tableName(): string
{
return 'exchange_quota';
}
public function user()
{
return $this->hasOne(User::class, 'uid', 'uid');
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace app\common\model\user;
use app\common\model\BaseModel;
/**
* Common: 兑换额度变更记录
* Author: wu-hui
* Time: 2024/01/10 16:45
* Class ExchangeQuotaRecord
* @package app\common\model\user
*/
class ExchangeQuotaRecord extends BaseModel
{
/**
* @return string
* @author xaboy
* @day 2020-03-30
*/
public static function tablePk(): string
{
return 'id';
}
/**
* @return string
* @author xaboy
* @day 2020-03-30
*/
public static function tableName(): string
{
return 'exchange_quota_record';
}
public function user()
{
return $this->hasOne(User::class, 'uid', 'uid');
}
}

View File

@ -1377,6 +1377,8 @@ class StoreOrderRepository extends BaseRepository
if (isset($temp_code)) Queue::push(SendSmsJob::class, ['tempId' => $temp_code, 'id' => $order->order_id]);
event('order.delivery', compact('order', 'data'));
if($data['status'] == 2) event('order.take', compact('order'));
return $data;
}

View File

@ -75,6 +75,8 @@ class ProductRepository extends BaseRepository
// 积分抵扣
['integral_deduction_type',0],
['integral_deduction_money',0],
// 是否赠送兑换额度
['is_give_quota',0],
];
protected $admin_filed = 'Product.product_id,Product.mer_id,brand_id,spec_type,unit_name,mer_status,rate,reply_count,store_info,cate_id,Product.image,slider_image,Product.store_name,Product.keyword,Product.sort,U.rank,Product.is_show,Product.sales,Product.price,extension_type,refusal,cost,Product.ot_price,stock,is_gift_bag,Product.care_count,Product.status,is_used,Product.create_time,Product.product_type,old_product_id,star,ficti,integral_total,integral_price_total,sys_labels,param_temp_id,mer_svip_status,svip_price,svip_price_type';
protected $filed = 'Product.product_id,Product.mer_id,brand_id,unit_name,spec_type,mer_status,rate,reply_count,store_info,cate_id,Product.image,slider_image,Product.store_name,Product.keyword,Product.sort,Product.is_show,Product.sales,Product.price,extension_type,refusal,cost,Product.ot_price,stock,is_gift_bag,Product.care_count,Product.status,is_used,Product.create_time,Product.product_type,old_product_id,integral_total,integral_price_total,mer_labels,Product.is_good,Product.is_del,type,param_temp_id,mer_svip_status,svip_price,svip_price_type';
@ -448,44 +450,46 @@ class ProductRepository extends BaseRepository
if($data['integral_rate'] > 100) $integral_rate = 100;
}
$result = [
'store_name' => $data['store_name'],
'image' => $data['image'],
'slider_image' => is_array($data['slider_image']) ? implode(',',$data['slider_image']) : '',
'store_info' => $data['store_info'] ?? '',
'keyword' => $data['keyword'] ?? '',
'brand_id' => $data['brand_id'] ?? 0,
'cate_id' => $data['cate_id'] ?? 0,
'unit_name' => $data['unit_name'] ?? '件',
'sort' => $data['sort'] ?? 0,
'is_show' => $data['is_show'] ?? 0,
'is_used' => (isset($data['status']) && $data['status'] == 1) ? 1 : 0,
'is_good' => $data['is_good'] ?? 0,
'video_link' => $data['video_link'] ?? '',
'temp_id' => $data['delivery_free'] ? 0 : ($data['temp_id'] ?? 0),
'extension_type' => $data['extension_type'] ?? 0,
'spec_type' => $data['spec_type'] ?? 0,
'status' => $data['status'] ?? 0,
'give_coupon_ids' => $give_coupon_ids,
'mer_status' => $data['mer_status'],
'guarantee_template_id' => $data['guarantee_template_id'] ?? 0,
'is_gift_bag' => $data['is_gift_bag'] ?? 0,
'integral_rate' => $integral_rate ?? 0,
'delivery_way' => implode(',',$data['delivery_way']),
'delivery_free' => $data['delivery_free'] ?? 0,
'once_min_count' => $data['once_min_count'] ?? 0,
'once_max_count' => $data['once_max_count'] ?? 0,
'pay_limit' => $data['pay_limit'] ?? 0,
'svip_price_type' => $data['svip_price_type'] ?? 0,
'refund_switch' => $data['refund_switch'] ?? 0,
'store_name' => $data['store_name'],
'image' => $data['image'],
'slider_image' => is_array($data['slider_image']) ? implode(',',$data['slider_image']) : '',
'store_info' => $data['store_info'] ?? '',
'keyword' => $data['keyword'] ?? '',
'brand_id' => $data['brand_id'] ?? 0,
'cate_id' => $data['cate_id'] ?? 0,
'unit_name' => $data['unit_name'] ?? '件',
'sort' => $data['sort'] ?? 0,
'is_show' => $data['is_show'] ?? 0,
'is_used' => (isset($data['status']) && $data['status'] == 1) ? 1 : 0,
'is_good' => $data['is_good'] ?? 0,
'video_link' => $data['video_link'] ?? '',
'temp_id' => $data['delivery_free'] ? 0 : ($data['temp_id'] ?? 0),
'extension_type' => $data['extension_type'] ?? 0,
'spec_type' => $data['spec_type'] ?? 0,
'status' => $data['status'] ?? 0,
'give_coupon_ids' => $give_coupon_ids,
'mer_status' => $data['mer_status'],
'guarantee_template_id' => $data['guarantee_template_id'] ?? 0,
'is_gift_bag' => $data['is_gift_bag'] ?? 0,
'integral_rate' => $integral_rate ?? 0,
'delivery_way' => implode(',',$data['delivery_way']),
'delivery_free' => $data['delivery_free'] ?? 0,
'once_min_count' => $data['once_min_count'] ?? 0,
'once_max_count' => $data['once_max_count'] ?? 0,
'pay_limit' => $data['pay_limit'] ?? 0,
'svip_price_type' => $data['svip_price_type'] ?? 0,
'refund_switch' => $data['refund_switch'] ?? 0,
// 赠送积分
'integral_give_switch' => $data['integral_give_switch'] ?? 0,
'integral_give_set' => $data['integral_give_set'] ?? 0,
'integral_give_type' => $data['integral_give_type'] ?? 0,
'integral_give_rate' => $data['integral_give_rate'] ?? 0,
'integral_give_money' => $data['integral_give_money'] ?? 0,
'integral_give_switch' => $data['integral_give_switch'] ?? 0,
'integral_give_set' => $data['integral_give_set'] ?? 0,
'integral_give_type' => $data['integral_give_type'] ?? 0,
'integral_give_rate' => $data['integral_give_rate'] ?? 0,
'integral_give_money' => $data['integral_give_money'] ?? 0,
// 积分抵扣
'integral_deduction_type' => $data['integral_deduction_type'] ?? 0,
'integral_deduction_money' => $data['integral_deduction_money'] ?? 0,
'integral_deduction_type' => $data['integral_deduction_type'] ?? 0,
'integral_deduction_money' => $data['integral_deduction_money'] ?? 0,
// 赠送兑换额度
'is_give_quota' => $data['is_give_quota'] ?? 0,
];
if (isset($data['extend']))
$result['extend'] = $data['extend'] ? json_encode($data['extend'], JSON_UNESCAPED_UNICODE) : '';

View File

@ -0,0 +1,19 @@
<?php
namespace app\common\repositories\user;
use app\common\dao\user\ExchangeQuotaRecordDao;
use app\common\repositories\BaseRepository;
class ExchangeQuotaRecordRepository extends BaseRepository{
protected $dao;
public function __construct(ExchangeQuotaRecordDao $dao){
$this->dao = $dao;
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace app\common\repositories\user;
use app\common\dao\user\ExchangeQuotaDao;
use app\common\repositories\BaseRepository;
class ExchangeQuotaRepository extends BaseRepository{
protected $dao;
public function __construct(ExchangeQuotaDao $dao){
$this->dao = $dao;
}
}

View File

@ -53,6 +53,15 @@ return [
'pay_success_order' => [\crmeb\listens\pay\OrderPaySuccessListen::class],
'pay_success_presell' => [\crmeb\listens\pay\PresellPaySuccessListen::class],
'pay_success_meal' => [\crmeb\listens\pay\MealSuccessListen::class],
// 订单支付成功事件触发
'order.paySuccess' => [
// 支付成功 赠送兑换额度
'app\listener\exchangeQuota\OrderPaySuccessEvent'
],
// 订单完成事件触发(进入待评价)
'order.take' => [],
// 订单退款事件
'refund.agree' => [],
],
'subscribe' => [],

View File

@ -0,0 +1,68 @@
<?php
namespace app\listener\exchangeQuota;
use app\common\model\user\ExchangeQuota;
use app\common\model\user\ExchangeQuotaRecord;
use think\facade\Log;
class OrderPaySuccessEvent{
public $groupOrder;
public function handle($groupOrder){
$groupOrder = $groupOrder['groupOrder'];
try{
// Log::info('支付成功 - 赠送兑换额度 - 开始: '.var_export(['uid'=>$groupOrder->uid,'group_order_id'=>$groupOrder->group_order_id],1));
$this->orderPaySuccessHandle($groupOrder);
}catch(\Exception $e){
$data = [
'uid' => $groupOrder->uid,
'group_order_id' => $groupOrder->group_order_id,
'msg' => $e->getMessage()
];
Log::info('支付成功 - 赠送兑换额度 - 错误: '.var_export($data,1));
}
}
// 支付成功 - 赠送兑换额度
public function orderPaySuccessHandle($groupOrder):bool{
// 获取用户当前持有
$userHoldInfo = ExchangeQuota::where('uid',$groupOrder->uid)->findOrEmpty();
if((int)$userHoldInfo->uid <= 0) $userHoldInfo->uid = $groupOrder->uid;
// 循环处理单个商品
$insertData = [];
foreach($groupOrder->orderList as $orderInfo){
foreach($orderInfo->orderProduct as $orderProductInfo){
// 判断:商品是否开启赠送兑换额度 当前商品实际支付的金额=赠送的兑换额度
if($orderProductInfo->product->is_give_quota == 1 && $orderProductInfo->product_price > 0){
// 赠送
$changeFront = (float)$userHoldInfo->surplus_quota;
$userHoldInfo->total_quota += (float)$orderProductInfo->product_price;// 总额度
$userHoldInfo->surplus_quota += (float)$orderProductInfo->product_price;// 剩余额度
$userHoldInfo->freeze_quota += (float)$orderProductInfo->product_price;// 冻结额度
// 记录
$insertData[] = [
'uid' => $orderProductInfo->uid,
'product_id' => $orderProductInfo->product_id,
'order_id' => $orderProductInfo->order_id,
'order_product_id' => $orderProductInfo->order_product_id,
'change_type' => 1,
'change_quantity' => (float)$orderProductInfo->product_price,
'change_front' => $changeFront,
'change_after' => (float)$userHoldInfo->surplus_quota,
'remark' => "购买商品赠送兑换额度",
];
}
}
}
// 修改用户持有
$userHoldInfo->save();
// 记录兑换额度信息
if(count($insertData) > 0) ExchangeQuotaRecord::insertAll($insertData);
return true;
}
}