添加拉新红包活动发放消费金
This commit is contained in:
parent
887ada2f81
commit
faa74c6b37
71
app/common/dao/store/StoreActivityOrderDao.php
Normal file
71
app/common/dao/store/StoreActivityOrderDao.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\dao\store;
|
||||
|
||||
use app\common\dao\BaseDao;
|
||||
use app\common\model\store\StoreActivityOrder;
|
||||
use think\facade\Db;
|
||||
|
||||
class StoreActivityOrderDao extends BaseDao
|
||||
{
|
||||
|
||||
protected function getModel(): string
|
||||
{
|
||||
return StoreActivityOrder::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存活动订单数据
|
||||
* @param array $groupOrder 订单
|
||||
* @param int $spreadId 推荐人id
|
||||
* @param int $isFirstOrder 是否首单
|
||||
* @param int $activityId 活动id
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function save(array $groupOrder, int $spreadId, int $isFirstOrder, int $activityId = 1)
|
||||
{
|
||||
$model = new StoreActivityOrder();
|
||||
$model->activity_id = $activityId;
|
||||
$model->user_id = $groupOrder['uid'];
|
||||
$model->group_order_id = $groupOrder['group_order_id'];
|
||||
$model->spread_id = $spreadId;
|
||||
$model->total_amount = $groupOrder['pay_price'];
|
||||
$model->is_first_order = $isFirstOrder;
|
||||
if (!$model->save()) {
|
||||
throw new \Exception('活动订单保存失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 活动订单撤销
|
||||
* @param int $groupOrderId
|
||||
* @return void
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function repeal(int $groupOrderId)
|
||||
{
|
||||
$model = StoreActivityOrder::where('group_order_id', $groupOrderId)->find();
|
||||
$model->status = StoreActivityOrder::STATUS_INVALID;
|
||||
if (!$model->save()) {
|
||||
throw new \Exception('活动订单保存失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 活动订单批量撤销
|
||||
* @param array $groupOrderIds
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function batchRepeal(array $groupOrderIds)
|
||||
{
|
||||
$count = StoreActivityOrder::whereIn('group_order_id', $groupOrderIds)->update(['status' => StoreActivityOrder::STATUS_INVALID]);
|
||||
if ($count < count($groupOrderIds)) {
|
||||
throw new \Exception('活动订单批量作废失败');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
159
app/common/dao/store/consumption/StoreConsumptionUserDao.php
Normal file
159
app/common/dao/store/consumption/StoreConsumptionUserDao.php
Normal file
@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\dao\store\consumption;
|
||||
|
||||
use app\common\dao\BaseDao;
|
||||
use app\common\dao\store\StoreActivityOrderDao;
|
||||
use app\common\model\store\consumption\StoreConsumption;
|
||||
use app\common\model\store\consumption\StoreConsumptionUser;
|
||||
use app\common\model\store\order\StoreGroupOrder;
|
||||
use app\common\model\store\StoreActivityOrder;
|
||||
use app\common\model\user\User;
|
||||
use think\facade\Db;
|
||||
|
||||
class StoreConsumptionUserDao extends BaseDao
|
||||
{
|
||||
|
||||
public $consumption;
|
||||
|
||||
protected function getModel(): string
|
||||
{
|
||||
return StoreConsumptionUser::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验用户是否满足红包拉新活动条件
|
||||
* @param int $groupOrderId 订单id
|
||||
* @return false|void
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function check($groupOrderId)
|
||||
{
|
||||
$groupOrder = StoreGroupOrder::where('group_order_id', $groupOrderId)->where('paid', 1)->where('pay_time', '>=', $this->consumption['start_time'])->find()->toArray();
|
||||
if (!empty($this->consumption['config'])) {
|
||||
$rate = 0;
|
||||
$scope = [];
|
||||
// 根据当前订单金额,计算是否满足消费金发放条件
|
||||
foreach ($this->consumption['config'] as $item) {
|
||||
if ($item['start'] <= $groupOrder['pay_price'] && $item['end'] >= $groupOrder['pay_price']) {
|
||||
$rate = bcdiv($item['rate'], 100, 2);
|
||||
$scope = $item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($rate <= 0) {
|
||||
return false;
|
||||
}
|
||||
Db::startTrans();
|
||||
try {
|
||||
$isFirstOrder = $this->isFirstOrder($groupOrder['uid'], $groupOrderId, $this->consumption['start_time']);
|
||||
$storeActivityOrderDao = new StoreActivityOrderDao();
|
||||
$spreadUserId = User::where('uid', $groupOrder['uid'])->value('spread_uid');
|
||||
$spreadUserId = empty($spreadUserId) ? 0 : $spreadUserId;
|
||||
$storeActivityOrderDao->save($groupOrder, $spreadUserId, $isFirstOrder);
|
||||
if ($this->consumption['type'] == StoreConsumption::TYPE_PULL_CONSUMPTION) {
|
||||
// 拉新返消费金
|
||||
// 用户作为推荐人,是否满足任务条件
|
||||
$mineOrderIds = $this->isFinished($groupOrder['uid'], $scope);
|
||||
if ($mineOrderIds !== false) {
|
||||
$mineOrderIds = "{$groupOrder['group_order_id']}," . $mineOrderIds;
|
||||
$this->send($rate, $groupOrder['uid'], $mineOrderIds, $groupOrder['pay_price']);
|
||||
$storeActivityOrderDao->batchRepeal(explode(',', $mineOrderIds));
|
||||
}
|
||||
// 用户作为受邀人且当前订单是首单
|
||||
if ($spreadUserId && $isFirstOrder) {
|
||||
// 查询推荐人满足条件的订单
|
||||
$spreadGroupOrder = StoreActivityOrder::where('user_id', $spreadUserId)->where('status', StoreActivityOrder::STATUS_VALID)->whereBetween('total_amount', $scope['start'], $scope['end'])->find();
|
||||
if (empty($spreadGroupOrder)) {
|
||||
return false;
|
||||
}
|
||||
$spreadOrderIds = $this->isFinished($spreadUserId, $scope);
|
||||
if ($spreadOrderIds !== false) {
|
||||
$spreadOrderIds = "{$spreadGroupOrder['group_order_id']}," . $spreadOrderIds;
|
||||
$this->send($rate, $spreadUserId, $spreadOrderIds, $spreadGroupOrder['total_amount']);
|
||||
$storeActivityOrderDao->batchRepeal(explode(',', $spreadOrderIds));
|
||||
}
|
||||
}
|
||||
} elseif ($this->consumption['type'] == StoreConsumption::TYPE_OWNER_CONSUMPTION) {
|
||||
$this->send($rate, $groupOrder['uid'], $groupOrder['group_order_id'], $groupOrder['pay_price']);
|
||||
$storeActivityOrderDao->repeal($groupOrder['group_order_id']);
|
||||
}
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断用户是否满足条件
|
||||
* @param int $userId
|
||||
* @param array $scope
|
||||
* @return false|string
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
*/
|
||||
public function isFinished(int $userId, array $scope)
|
||||
{
|
||||
$groupOrders = StoreActivityOrder::where('spread_id', $userId)->where('is_first_order', StoreActivityOrder::IS_FIRST_ORDER)->where('total_amount', '>=', $scope['start'])->where('status', StoreActivityOrder::STATUS_VALID)->field('group_order_id,user_id,total_amount')->select()->toArray();
|
||||
$userOrders = [];
|
||||
foreach ($groupOrders as $groupOrder) {
|
||||
if (!isset($userOrders[$groupOrder['user_id']])) {
|
||||
$userOrders[$groupOrder['user_id']] = $groupOrder;
|
||||
}
|
||||
}
|
||||
if (count($userOrders) < 4) {
|
||||
return false;
|
||||
}
|
||||
return implode(',', array_column($userOrders, 'group_order_id'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 发放消费金
|
||||
* @param float $rate
|
||||
* @param int $userId
|
||||
* @param string $groupOrderIds
|
||||
* @param float $amount
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function send(float $rate, int $userId, string $groupOrderIds, float $amount)
|
||||
{
|
||||
$model = new StoreConsumptionUser();
|
||||
$model->coupon_id = $this->consumption['coupon_id'];
|
||||
$model->uid = $userId;
|
||||
$model->coupon_title = $this->consumption['title'];
|
||||
$model->order_id_set = $groupOrderIds;
|
||||
$model->coupon_price = bcmul($amount, $rate, 2);
|
||||
$model->balance = $model->coupon_price;
|
||||
$model->create_time = date('Y-m-d H:i:s');
|
||||
$model->start_time = date('Y-m-d H:i:s', time() + 7 * 86400);
|
||||
$model->end_time = date('Y-m-d H:i:s', time() + 7 * 86400 + 365 * 86400);
|
||||
$model->type = 'send';
|
||||
if (!$model->save()) {
|
||||
throw new \Exception('发放失败');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否是首单
|
||||
* @param int $userId 用户id
|
||||
* @param int $groupOrderId 订单id
|
||||
* @param string $startTime 活动开始时间
|
||||
* @return int
|
||||
* @throws \think\db\exception\DbException
|
||||
*/
|
||||
public function isFirstOrder(int $userId, int $groupOrderId, string $startTime)
|
||||
{
|
||||
$isNewUser = User::where('uid', $userId)->where('create_time', '>=', $startTime)->count();
|
||||
if ($isNewUser == 0) {
|
||||
return 0;
|
||||
}
|
||||
$count = StoreGroupOrder::where('group_order_id', '<>', $groupOrderId)->where('paid', 1)->where('pay_time', '>=', $startTime)->count();
|
||||
return intval($count == 0); // 如果是0 则表示是首单
|
||||
}
|
||||
|
||||
}
|
28
app/common/model/store/StoreActivityOrder.php
Normal file
28
app/common/model/store/StoreActivityOrder.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\store;
|
||||
|
||||
use app\common\model\BaseModel;
|
||||
|
||||
/**
|
||||
* 活动订单表
|
||||
* 目前用于拉新红包活动
|
||||
*/
|
||||
class StoreActivityOrder extends BaseModel
|
||||
{
|
||||
|
||||
const STATUS_VALID = 1; //有效
|
||||
const STATUS_INVALID = 0; //无效
|
||||
const IS_FIRST_ORDER = 1; //首单
|
||||
|
||||
public static function tablePk(): string
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
|
||||
public static function tableName(): string
|
||||
{
|
||||
return 'store_activity_order';
|
||||
}
|
||||
|
||||
}
|
24
app/common/model/store/consumption/StoreConsumptionUser.php
Normal file
24
app/common/model/store/consumption/StoreConsumptionUser.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\store\consumption;
|
||||
|
||||
use app\common\model\BaseModel;
|
||||
|
||||
class StoreConsumptionUser extends BaseModel
|
||||
{
|
||||
|
||||
const STATUS_UNUSED = 0; //未使用
|
||||
const STATUS_USED = 1; //已使用
|
||||
const STATUS_OVERDUE = 2; //过期的
|
||||
|
||||
public static function tablePk(): string
|
||||
{
|
||||
return 'coupon_user_id';
|
||||
}
|
||||
|
||||
public static function tableName(): string
|
||||
{
|
||||
return 'store_consumption_user';
|
||||
}
|
||||
|
||||
}
|
@ -4,7 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace app\listener;
|
||||
|
||||
use app\common\dao\store\consumption\StoreConsumptionDao;
|
||||
use app\common\dao\store\consumption\StoreConsumptionUserDao;
|
||||
use app\common\dao\store\order\StoreCartDao;
|
||||
use app\common\dao\store\StoreActivityUserDao;
|
||||
use app\common\dao\system\merchant\MerchantDao;
|
||||
use app\common\model\system\merchant\Merchant;
|
||||
use app\common\repositories\store\order\StoreOrderRepository;
|
||||
@ -32,6 +35,13 @@ class paySuccess
|
||||
{
|
||||
try {
|
||||
$orderList = $event['groupOrder']['orderList'];
|
||||
$consumptionId = (new StoreActivityUserDao())->getValue($event['groupOrder']['uid']);
|
||||
$consumption = (new StoreConsumptionDao())->getOne($consumptionId);
|
||||
if (!empty($consumption)) {
|
||||
$storeConsumptionUserDao = new StoreConsumptionUserDao();
|
||||
$storeConsumptionUserDao->consumption = $consumption;
|
||||
$storeConsumptionUserDao->check($event['groupOrder']['group_order_id']);
|
||||
}
|
||||
foreach ($orderList as $k => $order) {
|
||||
// $merchant = Merchant::find($order['mer_id']);
|
||||
//添加到代发订单表里
|
||||
|
Loading…
x
Reference in New Issue
Block a user