调整补贴优惠券发放
This commit is contained in:
parent
c872994d00
commit
e5581d2dc9
@ -16,6 +16,7 @@ namespace app\common\dao\store\coupon;
|
||||
|
||||
use app\common\dao\BaseDao;
|
||||
use app\common\model\BaseModel;
|
||||
use app\common\model\store\consumption\StoreConsumptionUser;
|
||||
use app\common\model\store\coupon\StoreCouponDetail;
|
||||
use app\common\model\store\coupon\StoreCouponUser;
|
||||
use app\common\repositories\store\coupon\StoreCouponRepository;
|
||||
@ -299,28 +300,27 @@ class StoreCouponUserDao extends BaseDao
|
||||
{
|
||||
$totalAmount = StoreCouponUser::where('uid', $userId)
|
||||
->whereIn('coupon_type', [StoreCouponRepository::TYPE_STORE_COUPON, StoreCouponRepository::TYPE_PLATFORM_CARD])
|
||||
->where('status', StoreCouponUserRepository::STATUS_UNUSED)
|
||||
->field('SUM(coupon_price) as total_amount,coupon_type')
|
||||
->group('coupon_type')
|
||||
->whereIn('status', [StoreCouponUserRepository::STATUS_UNUSED, StoreCouponUserRepository::STATUS_REPEAL])
|
||||
->field('coupon_price,coupon_type')
|
||||
->select()->toArray();
|
||||
$totalAmount = reset_index($totalAmount, 'coupon_type');
|
||||
$result = [
|
||||
[
|
||||
'coupon_type' => StoreCouponRepository::TYPE_STORE_COUPON,
|
||||
'total_amount' => 0.00
|
||||
'total_amount' => 0.00,
|
||||
'type' => StoreCouponRepository::TYPE_STORE_COUPON,
|
||||
'type_cn' => StoreCouponRepository::TYPE_MAP[StoreCouponRepository::TYPE_STORE_COUPON],
|
||||
],
|
||||
[
|
||||
'coupon_type' => StoreCouponRepository::TYPE_PLATFORM_CARD,
|
||||
'total_amount' => 0.00
|
||||
'total_amount' => 0.00,
|
||||
'type' => StoreCouponRepository::TYPE_PLATFORM_CARD,
|
||||
'type_cn' => StoreCouponRepository::TYPE_MAP[StoreCouponRepository::TYPE_PLATFORM_CARD],
|
||||
]
|
||||
];
|
||||
foreach ($result as &$item) {
|
||||
if (isset($totalAmount[$item['coupon_type']])) {
|
||||
$item['total_amount'] = $totalAmount[$item['coupon_type']]['total_amount'];
|
||||
foreach ($totalAmount as $item) {
|
||||
if (isset($result[$item['type']])) {
|
||||
$result[$item['type']]['total_amount']= bcadd($result[$item['type']]['total_amount'], $item['coupon_price'], 2);
|
||||
}
|
||||
$item['type_cn'] = StoreCouponRepository::TYPE_MAP[$item['coupon_type']];
|
||||
}
|
||||
return $result;
|
||||
return array_values($result);
|
||||
}
|
||||
|
||||
public function record($userId, $type, $page, $limit)
|
||||
|
@ -15,6 +15,22 @@ class StoreCouponDetail extends BaseModel
|
||||
const TYPE_INCOME = 2; //收入
|
||||
const STATUS_VALID = 1; //有效的
|
||||
const STATUS_INVALID = 0; //无效的
|
||||
const SEND_REPEAL = -1; //未激活
|
||||
const SEND_AUDIT = 0; //审核中
|
||||
const SEND_CONFIRM = 1; //待确认
|
||||
const SEND_FINISHED = 2; //已完成
|
||||
|
||||
const STATUS_MAP = [
|
||||
self::STATUS_VALID => '有效',
|
||||
self::STATUS_INVALID => '无效'
|
||||
];
|
||||
|
||||
const SEND_STATUS_MAP = [
|
||||
self::SEND_REPEAL => '未激活',
|
||||
self::SEND_AUDIT => '审核中',
|
||||
self::SEND_CONFIRM => '待确认',
|
||||
self::SEND_FINISHED => '已到账',
|
||||
];
|
||||
|
||||
/**
|
||||
* @return string
|
||||
@ -72,9 +88,10 @@ class StoreCouponDetail extends BaseModel
|
||||
* @param $amount
|
||||
* @param $mark
|
||||
* @param $status
|
||||
* @param $sendStatus
|
||||
* @return void
|
||||
*/
|
||||
public static function income($order, $id, $amount, $mark = '', $status = 1)
|
||||
public static function income($order, $id, $amount, $mark = '', $status = 0, $sendStatus = -1)
|
||||
{
|
||||
$detailModel = new self();
|
||||
$detailModel->uid = $order['uid'];
|
||||
@ -85,6 +102,7 @@ class StoreCouponDetail extends BaseModel
|
||||
$detailModel->amount = $amount;
|
||||
$detailModel->pay_price = $order['pay_price'];
|
||||
$detailModel->status = $status;
|
||||
$detailModel->send_status = $sendStatus;
|
||||
$detailModel->mark = $mark;
|
||||
$detailModel->create_time = time();
|
||||
$detailModel->save();
|
||||
|
@ -795,9 +795,10 @@ class StoreOrderRepository extends BaseRepository
|
||||
//订单确认收货,增加商户销售金额
|
||||
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');
|
||||
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)]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -79,16 +79,17 @@ class Store extends BaseController
|
||||
{
|
||||
$type = $this->request->get('type', 1);
|
||||
$uid = $this->request->uid();
|
||||
$merchant = Merchant::where('mer_id', $this->merId)->field('sale_amount,purchase_amount,type_id')->find()->toArray();
|
||||
$merchant = Merchant::where('mer_id', $this->merId)->field('sale_amount,purchase_amount,official_purchase_amount,type_id')->find()->toArray();
|
||||
$saleTarget = '0.00';
|
||||
$purchaseTarget = '0.00';
|
||||
$officialPurchaseTarget = '0.00';
|
||||
$userCouponStatus = false;
|
||||
if ($type == 1) {
|
||||
//春耕采购补贴
|
||||
$couponId = StoreCoupon::where('type', StoreCouponRepository::TYPE_PLATFORM_CARD)->value('coupon_id');
|
||||
$userCoupon = StoreCouponUser::where('uid', $uid)
|
||||
->where('coupon_id', $couponId)
|
||||
->where('status', '<>', StoreCouponUser::STATUS_REPEAL)
|
||||
->field('coupon_price,origin_price')
|
||||
->field('coupon_price,origin_price,status')
|
||||
->find();
|
||||
if (!empty($userCoupon)) {
|
||||
$couponId = StoreCoupon::where('type', StoreCouponRepository::TYPE_STORE_COUPON)->value('coupon_id');
|
||||
@ -96,9 +97,11 @@ class Store extends BaseController
|
||||
->where('coupon_id', $couponId)
|
||||
->value('origin_price');
|
||||
$saleTarget = $userCoupon['origin_price'];
|
||||
$purchaseTarget = $userCoupon['origin_price'];
|
||||
$purchaseTarget = bcmul($userCoupon['coupon_price'], 0.4, 2);
|
||||
$officialPurchaseTarget = bcsub($saleTarget, $purchaseTarget, 2);
|
||||
$merchant['purchase_amount'] = bcsub($userCoupon['origin_price'], $userCoupon['coupon_price'], 2);
|
||||
$merchant['balance'] = $userCoupon['coupon_price'];
|
||||
$userCouponStatus = in_array($userCoupon['status'], [StoreCouponUser::STATUS_UNUSED, StoreCouponUser::STATUS_USED]);
|
||||
}
|
||||
} else {
|
||||
//增收销售补贴
|
||||
@ -106,23 +109,26 @@ class Store extends BaseController
|
||||
if ($coupon) {
|
||||
$userCoupon = StoreCouponUser::where('uid', $uid)
|
||||
->where('coupon_id', $coupon['coupon_id'])
|
||||
->where('status', '<>', StoreCouponUser::STATUS_REPEAL)
|
||||
->field('coupon_price,origin_price,status')
|
||||
->order('coupon_user_id', 'desc')
|
||||
->find();
|
||||
$userCouponStatus = in_array($userCoupon['status'] ?? -1, [StoreCouponUser::STATUS_UNUSED, StoreCouponUser::STATUS_USED]);
|
||||
$subsidyAmount = $userCoupon['origin_price'] ?? '0.00';
|
||||
$merchant['balance'] = $userCoupon['coupon_price'] ?? '0.00';
|
||||
foreach ($coupon['config'] as $k => $item) {
|
||||
if (empty($userCoupon) && in_array($merchant['type_id'], $item['type_id'])) {
|
||||
$saleTarget = $item['amount'];
|
||||
$subsidyAmount = $item['subsidy'];
|
||||
$purchaseTarget = bcmul($saleTarget, 0.5, 2);
|
||||
$purchaseTarget = bcmul($saleTarget, 0.2, 2);
|
||||
$officialPurchaseTarget = bcmul($saleTarget, 0.3, 2);
|
||||
break;
|
||||
}
|
||||
if (!empty($userCoupon) && in_array($merchant['type_id'], $item['type_id'])) {
|
||||
$nextItem = $coupon['config'][$k + 1] ?? $item;
|
||||
$saleTarget = $nextItem['amount'];
|
||||
$subsidyAmount = $nextItem['subsidy'];
|
||||
$purchaseTarget = bcmul($saleTarget, 0.5, 2);
|
||||
$purchaseTarget = bcmul($saleTarget, 0.2, 2);
|
||||
$officialPurchaseTarget = bcmul($saleTarget, 0.3, 2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -132,17 +138,27 @@ class Store extends BaseController
|
||||
$saleFinishRate = bcmul(min($saleFinishRate, 1), 100, 0);
|
||||
$purchaseFinishRate = $purchaseTarget <= 0 ? 0 : bcdiv($merchant['purchase_amount'], $purchaseTarget, 2);
|
||||
$purchaseFinishRate = bcmul(min($purchaseFinishRate, 1), 100, 0);
|
||||
$springSubsidyRate = $purchaseTarget <= 0 ? 0 : bcdiv($merchant['purchase_amount'], $purchaseTarget, 2);
|
||||
$officialPurchaseFinishRate = $officialPurchaseTarget <= 0 ? 0 : bcdiv($merchant['official_purchase_amount'], $officialPurchaseTarget, 2);
|
||||
$officialPurchaseFinishRate = bcmul(min($officialPurchaseFinishRate, 1), 100, 0);
|
||||
$springSubsidyRate = $saleTarget <= 0 ? 0 : bcdiv(bcadd($merchant['purchase_amount'], $merchant['official_purchase_amount'], 2), $saleTarget, 2);
|
||||
$springSubsidyRate = bcmul(min($springSubsidyRate, 1), 100, 0);
|
||||
if ($type == 1) {
|
||||
$status = $saleFinishRate >= 100 && $purchaseFinishRate >= 100 && $merchant['balance'] == 0;
|
||||
} else {
|
||||
$status = $saleFinishRate >= 100 && $purchaseFinishRate >= 100;
|
||||
}
|
||||
return app('json')->success([
|
||||
'subsidy_amount' => empty($subsidyAmount) ? '0.00' : $subsidyAmount,
|
||||
'subsidy_status' => $saleFinishRate >= 100 && $purchaseFinishRate >= 100,
|
||||
'subsidy_status' => $status && $userCouponStatus,
|
||||
'sale_target' => $saleTarget,
|
||||
'sale_amount' => $merchant['sale_amount'],
|
||||
'sale_finish_rate' => $saleFinishRate,
|
||||
'purchase_target' => $purchaseTarget,
|
||||
'purchase_amount' => $merchant['purchase_amount'],
|
||||
'purchase_finish_rate' => $purchaseFinishRate,
|
||||
'official_purchase_target' => $officialPurchaseTarget,
|
||||
'official_purchase_amount' => $merchant['official_purchase_amount'],
|
||||
'official_purchase_finish_rate' => $officialPurchaseFinishRate,
|
||||
'spring_subsidy' => $saleTarget,
|
||||
'spring_subsidy_amount' => $merchant['purchase_amount'],
|
||||
'spring_subsidy_rate' => $springSubsidyRate,
|
||||
@ -171,19 +187,21 @@ class Store extends BaseController
|
||||
$couponId = StoreCoupon::where('type', $couponType)->value('coupon_id');
|
||||
$totalAmount = StoreCouponUser::where('uid', $userId)
|
||||
->where('coupon_id', $couponId)
|
||||
->where('status', StoreCouponUser::STATUS_UNUSED)
|
||||
->whereIn('status', [StoreCouponUser::STATUS_UNUSED, StoreCouponUser::STATUS_REPEAL])
|
||||
->sum('coupon_price');
|
||||
$couponUserId = StoreCouponUser::where('uid', $userId)
|
||||
->where('coupon_id', $couponId)
|
||||
->where('status', '<>', StoreCouponUser::STATUS_REPEAL)
|
||||
->column('coupon_user_id');
|
||||
$query = StoreCouponDetail::field('amount coupon_price,create_time,pay_price')
|
||||
$query = StoreCouponDetail::field('amount coupon_price,create_time,pay_price,status,send_status')
|
||||
->whereIn('coupon_user_id', $couponUserId)
|
||||
->where('type', StoreCouponDetail::TYPE_INCOME);
|
||||
$count = $query->count();
|
||||
$record = $query->page($page)->limit($limit)->select()->toArray();
|
||||
$record = $query->page($page)->limit($limit)->order('id', 'desc')->select()->toArray();
|
||||
foreach ($record as &$item) {
|
||||
$item['order_amount'] = bcadd($item['coupon_price'], $item['pay_price'], 2);
|
||||
$item['status_cn'] = StoreCouponDetail::STATUS_MAP[$item['status']];
|
||||
$item['send_status_cn'] = StoreCouponDetail::SEND_STATUS_MAP[$item['send_status']];
|
||||
}
|
||||
$result = ['total_amount' => $totalAmount, 'count' => $count, 'record' => $record];
|
||||
return app('json')->success($result);
|
||||
|
@ -34,7 +34,7 @@ class OrderDeliveryListen implements ListenerInterface
|
||||
}
|
||||
$coupon->coupon_price = $couponTotal;
|
||||
$couponUser = $repo->sendCoupon($coupon, $order['uid'], StoreCouponUserRepository::SEND_TYPE_BUY);
|
||||
StoreCouponDetail::income($order, $couponUser['coupon_user_id'], $coupon->coupon_price, $coupon['title']);
|
||||
StoreCouponDetail::income($order, $couponUser['coupon_user_id'], $coupon->coupon_price, $coupon['title'], StoreCouponDetail::STATUS_VALID, StoreCouponDetail::SEND_FINISHED);
|
||||
|
||||
//发放春耕补贴优惠券
|
||||
if ($couponTotal >= 1000) {
|
||||
@ -48,7 +48,7 @@ class OrderDeliveryListen implements ListenerInterface
|
||||
$repo->endTime = '2025-07-01';
|
||||
$repo->status = StoreCouponUser::STATUS_REPEAL;
|
||||
$couponUser = $repo->sendCoupon($giveCoupon, $order['uid'], StoreCouponUserRepository::SEND_TYPE_BUY);
|
||||
StoreCouponDetail::income($order, $couponUser['coupon_user_id'], $giveCoupon->coupon_price, $giveCoupon['title'], 0);
|
||||
StoreCouponDetail::income($order, $couponUser['coupon_user_id'], $giveCoupon->coupon_price, $giveCoupon['title']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,10 +24,12 @@ class SendSubsidyCouponListen extends TimerService implements ListenerInterface
|
||||
$count = 0;
|
||||
if ($coupon) {
|
||||
foreach ($coupon['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'])
|
||||
->where('sale_amount', '>=', $item['amount'])
|
||||
->where('purchase_amount', '>=', $purchaseAmount)
|
||||
->where('official_purchase_amount', '>=', $officialPurchaseAmount)
|
||||
->select();
|
||||
foreach ($merchants as $merchant) {
|
||||
//商户已获得的补贴金额
|
||||
@ -43,7 +45,13 @@ class SendSubsidyCouponListen extends TimerService implements ListenerInterface
|
||||
$repo = app()->make(StoreCouponRepository::class);
|
||||
$coupon->coupon_price = $amount;
|
||||
$couponUser = $repo->sendCoupon($coupon, $merchant->uid, StoreCouponUserRepository::SEND_TYPE_SEND);
|
||||
StoreCouponDetail::income([], $couponUser['coupon_user_id'], $coupon->coupon_price, $coupon['title']);
|
||||
$order = [
|
||||
'uid' => $merchant['uid'],
|
||||
'order_id' => 0,
|
||||
'pay_price' => 0,
|
||||
'group_order_id' => 0,
|
||||
];
|
||||
StoreCouponDetail::income($order, $couponUser['coupon_user_id'], $coupon->coupon_price, $coupon['title']);
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user