调整补贴发放

This commit is contained in:
luofei 2024-03-16 15:31:05 +08:00
parent e95efacd95
commit 29060c709a
5 changed files with 48 additions and 20 deletions

13
app/common/Enum.php Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace app\common;
class Enum
{
/** 消费金状态 */
const CONSUMPTION_STATUS_REPEAL = -1; //未激活
const CONSUMPTION_STATUS_AUDIT = 0; //审核中
const CONSUMPTION_STATUS_CONFIRM = 1; //待确认
const CONSUMPTION_STATUS_FINISHED = 2; //已完成
}

View File

@ -281,34 +281,36 @@ class StoreActivityUserDao extends BaseDao
/** /**
* 红包余额统计 * 红包余额统计
* @param int $userId * @param int $userId
* @return array * @return array|array[]
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/ */
public function total(int $userId) public function total(int $userId)
{ {
$totalAmount = StoreConsumptionUser::where('uid', $userId) $totalAmount = StoreConsumptionUser::where('uid', $userId)
->whereIn('type', [StoreConsumptionUser::TYPE_ONE, StoreConsumptionUser::TYPE_TWO]) ->whereIn('type', [StoreConsumptionUser::TYPE_ONE, StoreConsumptionUser::TYPE_TWO])
->where('status', StoreConsumptionUser::STATUS_UNUSED) ->whereIn('status', [StoreConsumptionUser::STATUS_UNUSED, StoreConsumptionUser::STATUS_REPEAL])
->field('SUM(balance) as total_amount,type') ->field('balance,type')
->group('type')
->select()->toArray(); ->select()->toArray();
$totalAmount = reset_index($totalAmount, 'type');
$result = [ $result = [
[ '1' => [
'type' => 1, 'type' => 1,
'total_amount' => 0.00 'total_amount' => 0.00,
'type_cn' => StoreConsumptionUser::TYPE_MAP[1],
], ],
[ '2' => [
'type' => 2, 'type' => 2,
'total_amount' => 0.00 'total_amount' => 0.00,
'type_cn' => StoreConsumptionUser::TYPE_MAP[2],
] ]
]; ];
foreach ($result as &$item) { foreach ($totalAmount as $item) {
if (isset($totalAmount[$item['type']])) { if (isset($result[$item['type']])) {
$item['total_amount'] = $totalAmount[$item['type']]['total_amount']; $result[$item['type']]['total_amount']= bcadd($result[$item['type']]['total_amount'], $item['balance'], 2);
} }
$item['type_cn'] = StoreConsumptionUser::TYPE_MAP[$item['type']];
} }
return $result; return array_values($result);
} }
} }

View File

@ -52,6 +52,7 @@ class StoreConsumptionUserDao extends BaseDao
public $startTime; public $startTime;
public $endTime; public $endTime;
public $billExtra; public $billExtra;
public $onlyBill = false; // 是否只写入用户账单
protected function getModel(): string protected function getModel(): string
{ {
@ -241,10 +242,12 @@ class StoreConsumptionUserDao extends BaseDao
$model = StoreConsumptionUser::where('uid', $userId)->where('type', StoreConsumptionUser::TYPE_TWO)->find(); $model = StoreConsumptionUser::where('uid', $userId)->where('type', StoreConsumptionUser::TYPE_TWO)->find();
$couponPrice = bcmul($amount, $rate, 2); $couponPrice = bcmul($amount, $rate, 2);
if (!empty($model) && $model['type'] == $type) { if (!empty($model) && $model['type'] == $type) {
$model->coupon_price = bcadd($model->coupon_price, $couponPrice, 2); if (!$this->onlyBill) {
$model->balance = bcadd($model->balance, $couponPrice, 2); $model->coupon_price = bcadd($model->coupon_price, $couponPrice, 2);
if ($model->status != StoreConsumptionUser::STATUS_UNUSED) { $model->balance = bcadd($model->balance, $couponPrice, 2);
$model->status = StoreConsumptionUser::STATUS_UNUSED; if ($model->status != StoreConsumptionUser::STATUS_UNUSED) {
$model->status = StoreConsumptionUser::STATUS_UNUSED;
}
} }
} else { } else {
$model = new StoreConsumptionUser(); $model = new StoreConsumptionUser();
@ -260,6 +263,9 @@ class StoreConsumptionUserDao extends BaseDao
$model->end_time = $this->endTime ?: '2026-01-15 23:59:59'; $model->end_time = $this->endTime ?: '2026-01-15 23:59:59';
$model->type = $type; $model->type = $type;
$model->status = $status; $model->status = $status;
if ($this->onlyBill === true) {
$model->status = StoreConsumptionUser::STATUS_REPEAL;
}
} }
if (!$model->save()) { if (!$model->save()) {
throw new \Exception('发放失败'); throw new \Exception('发放失败');

View File

@ -844,10 +844,11 @@ class StoreOrderRepository extends BaseRepository
$money = bcsub($money, $refundPrice, 2); $money = bcsub($money, $refundPrice, 2);
//订单确认收货,增加商户销售金额 //订单确认收货,增加商户销售金额
Merchant::where('mer_id', $order->mer_id)->update(['sale_amount' => Db::raw('sale_amount+' . $money)]); Merchant::where('mer_id', $order->mer_id)->update(['sale_amount' => Db::raw('sale_amount+' . $money)]);
$field = $order->merchant['type_id'] == 22 ? 'official_purchase_amount' : 'purchase_amount';
//订单确认收货,增加商户采购金额 //订单确认收货,增加商户采购金额
$merId = Merchant::where('uid', $order->uid)->value('mer_id'); $merId = Merchant::where('uid', $order->uid)->value('mer_id');
if (!empty($merId)) { if (!empty($merId)) {
Merchant::where('mer_id', $merId)->update(['purchase_amount' => Db::raw('purchase_amount+' . $money)]); Merchant::where('mer_id', $merId)->update([$field => Db::raw("{$field}+" . $money)]);
} }
} }
}); });

View File

@ -3,6 +3,7 @@
namespace crmeb\listens; namespace crmeb\listens;
use app\common\dao\store\consumption\StoreConsumptionUserDao; use app\common\dao\store\consumption\StoreConsumptionUserDao;
use app\common\Enum;
use app\common\model\store\consumption\StoreConsumption; use app\common\model\store\consumption\StoreConsumption;
use app\common\model\store\consumption\StoreConsumptionUser; use app\common\model\store\consumption\StoreConsumptionUser;
use app\common\model\system\merchant\Merchant; use app\common\model\system\merchant\Merchant;
@ -23,10 +24,12 @@ class SendSubsidyCouponListen extends TimerService implements ListenerInterface
$count = 0; $count = 0;
if ($consumption) { if ($consumption) {
foreach ($consumption['config'] as $item) { foreach ($consumption['config'] as $item) {
$purchaseAmount = $item['amount'] * 0.5; $purchaseAmount = $item['amount'] * 0.2; //普通店铺采购金额
$officialPurchaseAmount = $item['amount'] * 0.3; //官方店铺采购金额
$merchants = Merchant::whereIn('type_id', $item['type_id']) $merchants = Merchant::whereIn('type_id', $item['type_id'])
->where('sale_amount', '>=', $item['amount']) ->where('sale_amount', '>=', $item['amount'])
->where('purchase_amount', '>=', $purchaseAmount) ->where('purchase_amount', '>=', $purchaseAmount)
->where('official_purchase_amount', '>=', $officialPurchaseAmount)
->select(); ->select();
foreach ($merchants as $merchant) { foreach ($merchants as $merchant) {
//商户已获得的补贴金额 //商户已获得的补贴金额
@ -43,7 +46,10 @@ class SendSubsidyCouponListen extends TimerService implements ListenerInterface
'coupon_id' => $consumption['coupon_id'], 'coupon_id' => $consumption['coupon_id'],
'sale_amount' => $item['amount'], 'sale_amount' => $item['amount'],
'purchase_amount' => $purchaseAmount, 'purchase_amount' => $purchaseAmount,
'official_purchase_amount' => $officialPurchaseAmount,
'status' => Enum::CONSUMPTION_STATUS_REPEAL,
]; ];
$consumptionRepo->onlyBill = true;
$consumptionRepo->send($consumption, 1, $merchant->uid, 0, $amount, StoreConsumptionUser::STATUS_UNUSED, StoreConsumptionUser::TYPE_TWO); $consumptionRepo->send($consumption, 1, $merchant->uid, 0, $amount, StoreConsumptionUser::STATUS_UNUSED, StoreConsumptionUser::TYPE_TWO);
$count++; $count++;
} }