调整补贴发放

This commit is contained in:
luofei 2024-03-19 15:17:39 +08:00
parent 62bd597ac5
commit 7c7a806dfe
9 changed files with 159 additions and 131 deletions

View File

@ -188,7 +188,8 @@ class StoreCouponUserDao extends BaseDao
if (!$storeCouponUser->save()) {
throw new ValidateException('优惠券余额更新出错');
}
StoreCouponDetail::income($order, $storeCouponUser['coupon_user_id'], $amount, $mark);
$coupon = ['coupon_id' => $storeCouponUser['coupon_id'], 'coupon_price' => $amount];
StoreCouponDetail::income($order, $storeCouponUser['coupon_user_id'], $coupon, $mark, StoreCouponDetail::STATUS_VALID, StoreCouponDetail::SEND_FINISHED);
}
/**

View File

@ -87,26 +87,29 @@ class StoreCouponDetail extends BaseModel
/**
* 获得优惠券
* @param $order
* @param $id
* @param $amount
* @param $couponUserId
* @param $coupon
* @param $mark
* @param $status
* @param $sendStatus
* @param $extra
* @return void
*/
public static function income($order, $id, $amount, $mark = '', $status = 0, $sendStatus = -1)
public static function income($order, $couponUserId, $coupon, $mark = '', $status = 0, $sendStatus = -1, $extra = [])
{
$detailModel = new self();
$detailModel->uid = $order['uid'];
$detailModel->coupon_user_id = $id;
$detailModel->group_order_id = $order['group_order_id'];
$detailModel->coupon_id = $coupon['coupon_id'];
$detailModel->coupon_user_id = $couponUserId;
$detailModel->group_order_id = $order['group_order_id'] ?? 0;
$detailModel->order_id = $order['order_id'] ?? 0;
$detailModel->type = StoreCouponDetail::TYPE_INCOME;
$detailModel->amount = $amount;
$detailModel->pay_price = $order['pay_price'];
$detailModel->amount = $coupon['coupon_price'];
$detailModel->pay_price = $order['pay_price'] ?? 0;
$detailModel->status = $status;
$detailModel->send_status = $sendStatus;
$detailModel->mark = $mark;
$detailModel->extra = $extra;
$detailModel->create_time = time();
$detailModel->save();
}

View File

@ -17,7 +17,6 @@ namespace app\common\repositories\store\coupon;
use app\common\dao\store\coupon\StoreCouponDao;
use app\common\dao\store\coupon\StoreCouponProductDao;
use app\common\model\store\coupon\StoreCoupon;
use app\common\model\store\coupon\StoreCouponUser;
use app\common\repositories\BaseRepository;
use app\common\repositories\store\product\ProductRepository;
use app\common\repositories\store\StoreCategoryRepository;
@ -339,61 +338,6 @@ class StoreCouponRepository extends BaseRepository
public function preSendCoupon(StoreCoupon $coupon, $uid, $type = 'send')
{
$data = $this->createData($coupon, $uid, $type);
// $title = StoreCouponRepository::TYPE_MAP[$type];
// $model = StoreCouponUser::where('uid', $uid)->where('type', StoreCouponRepository::TYPE_STORE_COUPON)->find();
// $couponPrice = bcmul($amount, $rate, 2);
// if (!empty($model) && $model['type'] == $type) {
// if (!$this->onlyBill) {
// $model->coupon_price = bcadd($model->coupon_price, $couponPrice, 2);
// $model->balance = bcadd($model->balance, $couponPrice, 2);
// if ($model->status != StoreConsumptionUser::STATUS_UNUSED) {
// $model->status = StoreConsumptionUser::STATUS_UNUSED;
// }
// }
// } else {
// $model = new StoreConsumptionUser();
// $model->coupon_id = $consumption['coupon_id'];
// $model->uid = $userId;
// $model->coupon_title = $title;
// $model->order_id_set = $groupOrderIds;
// $model->coupon_price = $couponPrice;
// $model->balance = $model->coupon_price;
// $model->order_amount = $amount;
// $model->create_time = date('Y-m-d H:i:s');
// $model->start_time = $this->startTime ?: date('Y-m-d H:i:s', time() + 7 * 86400);
// $model->end_time = $this->endTime ?: '2026-01-15 23:59:59';
// $model->type = $type;
// $model->status = $status;
// if ($this->onlyBill === true) {
// $model->status = StoreConsumptionUser::STATUS_REPEAL;
// }
// }
// if (!$model->save()) {
// throw new \Exception('发放失败');
// }
// // 写入红包日志
// /** @var $userBillRepository UserBillRepository */
// $userBillRepository = app()->make(UserBillRepository::class);
// $extra = ['order_amount' => $amount, 'coupon_user_id' => $model['coupon_user_id']];
// if ($this->orderType == 2) {
// $extra['order_id'] = $groupOrderIds;
// } else {
// $extra['group_order_id'] = $groupOrderIds;
// }
// if (!empty($this->billExtra)) {
// $extra = array_merge($extra, $this->billExtra);
// }
// $userBillRepository->incBill($userId, 'red_pack', "red_pack_{$type}", [
// 'link_id' => $model['coupon_user_id'],
// 'status' => $status == -1 ?: 1,
// 'title' => '获得' . $title,
// 'number' => $couponPrice,
// 'mark' => '获得' . $title . $couponPrice . ",订单金额:{$amount}",
// 'extra' => json_encode($extra, JSON_UNESCAPED_UNICODE),
// 'balance' => 0
// ]);
return app()->make(StoreCouponUserRepository::class)->create($data);
}

View File

@ -14,6 +14,8 @@ namespace app\common\repositories\store\coupon;
use app\common\dao\store\coupon\StoreCouponUserDao;
use app\common\Enum;
use app\common\model\store\coupon\StoreCouponDetail;
use app\common\model\store\coupon\StoreCouponUser;
use app\common\model\store\order\StoreOrder;
use app\common\model\store\product\ProductCate;
use app\common\model\system\merchant\Merchant;
@ -22,6 +24,7 @@ use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
use think\facade\Db;
/**
* Class StoreCouponUserRepository
@ -48,6 +51,18 @@ class StoreCouponUserRepository extends BaseRepository
* @var StoreCouponUserDao
*/
protected $dao;
public $uid;
public $status = StoreCouponUser::STATUS_UNUSED; //优惠券状态
public $detailStatus; //优惠券详情状态
public $sendStatus; //优惠券发放状态
public $coupon; //优惠券model
public $order; //订单model
public $orderIds = ''; //优惠券关联的订单ids
public $sendType; //优惠券发放类型
public $onlyDetail = false; //仅记录优惠券详情,不增加优惠券余额
public $startTime; //优惠券使用开始时间
public $endTime; //优惠券使用结束时间
public $extra = []; //优惠券额外的信息
/**
* StoreCouponUserRepository constructor.
@ -158,4 +173,54 @@ class StoreCouponUserRepository extends BaseRepository
return $hasPlatformCard;
}
public function send()
{
Db::startTrans();
try {
if ($this->coupon['type'] == StoreCouponRepository::TYPE_SALE_SUBSIDY) {
$this->coupon['type'] = StoreCouponRepository::TYPE_STORE_COUPON;
}
$title = StoreCouponRepository::TYPE_MAP[$this->coupon['type']];
$model = StoreCouponUser::where('uid', $this->uid)->where('coupon_type', StoreCouponRepository::TYPE_STORE_COUPON)->find();
if (!empty($model) && $model['coupon_type'] == $this->coupon['type']) {
if (!$this->onlyDetail) {
$model->coupon_price = bcadd($model->coupon_price, $this->coupon->coupon_price, 2);
$model->balance = bcadd($model->balance, $model->coupon_price, 2);
if ($model->status != StoreCouponUser::STATUS_UNUSED) {
$model->status = StoreCouponUser::STATUS_UNUSED;
}
}
} else {
$model = new StoreCouponUser();
$model->coupon_id = $this->coupon['coupon_id'];
$model->uid = $this->uid;
$model->coupon_title = $title;
$model->order_id_set = $this->orderIds;
$model->coupon_price = $this->coupon->coupon_price;
$model->origin_price = $model->coupon_price;
$model->order_amount = $this->order['total_price'] ?? 0;
$model->create_time = date('Y-m-d H:i:s');
$model->start_time = $this->startTime ?: date('Y-m-d H:i:s', time() + 7 * 86400);
$model->end_time = $this->endTime ?: '2026-01-15 23:59:59';
$model->coupon_type = $this->coupon['type'];
$model->type = $this->sendType;
$model->status = $this->status;
if ($this->onlyDetail === true) {
$model->coupon_price = 0;
$model->origin_price = 0;
$model->status = StoreCouponUser::STATUS_REPEAL;
}
}
if (!$model->save()) {
throw new \Exception('发放失败');
}
StoreCouponDetail::income($this->order, $model['coupon_user_id'], $this->coupon, $title, $this->status, $this->sendStatus, $this->extra);
app()->make(StoreCouponIssueUserRepository::class)->issue($this->coupon['coupon_id'], $this->uid);
Db::commit();
return $model;
} catch (\Exception $e) {
Db::rollback();
}
}
}

View File

@ -32,10 +32,10 @@ class Subsidy extends BaseController
if (empty($data)) {
return app('json')->fail('数据不存在');
}
if ($data['status'] != StoreCouponDetail::SEND_AUDIT) {
if ($data['send_status'] != StoreCouponDetail::SEND_AUDIT) {
return app('json')->fail('当前状态不支持操作');
}
$data->status = StoreCouponDetail::SEND_CONFIRM;
$data->send_status = StoreCouponDetail::SEND_CONFIRM;
$data->save();
return app('json')->success('审核完成');
}
@ -43,11 +43,14 @@ class Subsidy extends BaseController
public function update($id)
{
$amount = $this->request->post('amount');
if (is_null($amount)) {
return app('json')->fail('请输入金额');
}
$data = StoreCouponDetail::find($id);
if (empty($data)) {
return app('json')->fail('数据不存在');
}
if ($data['status'] != StoreCouponDetail::SEND_AUDIT) {
if ($data['send_status'] != StoreCouponDetail::SEND_AUDIT) {
return app('json')->fail('当前状态不支持操作');
}
$data->amount = max($amount, 0);

View File

@ -84,54 +84,58 @@ class Store extends BaseController
$purchaseTarget = '0.00';
$officialPurchaseTarget = '0.00';
$userCouponStatus = false;
$userCouponStatusName = '';
if ($type == 1) {
//春耕采购补贴
//春耕采购余额
$couponId = StoreCoupon::where('type', StoreCouponRepository::TYPE_PLATFORM_CARD)->value('coupon_id');
$userCoupon = StoreCouponUser::where('uid', $uid)
->where('coupon_id', $couponId)
->field('coupon_price,origin_price,status')
->find();
if (!empty($userCoupon)) {
//春耕采购补贴
$couponId = StoreCoupon::where('type', StoreCouponRepository::TYPE_STORE_COUPON)->value('coupon_id');
$subsidyAmount = StoreCouponUser::where('uid', $uid)
->where('coupon_id', $couponId)
->value('origin_price');
$couponDetail = StoreCouponDetail::where('coupon_id', $couponId)
->where('uid', $uid)
->order('id', 'desc')
->find();
$subsidyAmount = $couponDetail['amount'];
$saleTarget = $userCoupon['origin_price'];
$purchaseTarget = bcmul($userCoupon['coupon_price'], 0.4, 2);
$purchaseTarget = bcmul($saleTarget, 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]);
$userCouponStatus = $couponDetail['send_status'];
$userCouponStatusName = StoreCouponDetail::SEND_STATUS_MAP[$userCouponStatus];
}
} else {
//增收销售补贴
$coupon = StoreCoupon::where('type', StoreCouponRepository::TYPE_SALE_SUBSIDY)->find();
if ($coupon) {
$userCoupon = StoreCouponUser::where('uid', $uid)
->where('coupon_id', $coupon['coupon_id'])
->field('coupon_price,origin_price,status')
->order('coupon_user_id', 'desc')
$couponDetail = StoreCouponDetail::where('coupon_id', $coupon['coupon_id'])
->where('uid', $uid)
->order('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';
$merchant['balance'] = '0.00';
$userCouponStatus = $couponDetail['send_status'] ?? StoreCouponDetail::SEND_REPEAL;
$userCouponStatusName = StoreCouponDetail::SEND_STATUS_MAP[$userCouponStatus];
$target = [];
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.2, 2);
$officialPurchaseTarget = bcmul($saleTarget, 0.3, 2);
break;
if (!in_array($merchant['type_id'], $item['type_id'])) {
continue;
}
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.2, 2);
$officialPurchaseTarget = bcmul($saleTarget, 0.3, 2);
if (empty($couponDetail['extra']['sale_target'])) {
$target = $item;
break;
} elseif ($couponDetail['extra']['sale_target'] == $item['amount']) {
$target = $coupon['config'][$k + 1] ?? $item;
break;
}
}
$saleTarget = $target['amount'];
$subsidyAmount = $target['subsidy'];
$purchaseTarget = bcmul($saleTarget, 0.2, 2);
$officialPurchaseTarget = bcmul($saleTarget, 0.3, 2);
}
}
$saleFinishRate = $saleTarget <= 0 ? 0 : bcdiv($merchant['sale_amount'], $saleTarget, 2);
@ -142,14 +146,10 @@ class Store extends BaseController
$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' => $status && $userCouponStatus,
'subsidy_status' => $userCouponStatus,
'subsidy_status_name' => $userCouponStatusName,
'sale_target' => $saleTarget,
'sale_amount' => $merchant['sale_amount'],
'sale_finish_rate' => $saleFinishRate,
@ -186,20 +186,16 @@ class Store extends BaseController
}
$couponId = StoreCoupon::where('type', $couponType)->value('coupon_id');
$totalAmount = StoreCouponUser::where('uid', $userId)
->where('coupon_id', $couponId)
->where('coupon_type', StoreCouponRepository::TYPE_STORE_COUPON)
->whereIn('status', [StoreCouponUser::STATUS_UNUSED, StoreCouponUser::STATUS_REPEAL])
->sum('coupon_price');
$couponUserId = StoreCouponUser::where('uid', $userId)
->value('origin_price');
$query = StoreCouponDetail::field('amount coupon_price,create_time,status,send_status,extra')
->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,status,send_status')
->whereIn('coupon_user_id', $couponUserId)
->where('type', StoreCouponDetail::TYPE_INCOME);
$count = $query->count();
$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']];
}

View File

@ -3,7 +3,6 @@
namespace crmeb\listens;
use app\common\model\store\coupon\StoreCouponDetail;
use app\common\model\store\coupon\StoreCouponUser;
use app\common\repositories\store\coupon\StoreCouponRepository;
use app\common\repositories\store\coupon\StoreCouponUserRepository;
use crmeb\interfaces\ListenerInterface;
@ -26,29 +25,39 @@ class OrderDeliveryListen implements ListenerInterface
if ($couponTotal > 0) {
//发放大礼包
/** @var StoreCouponRepository $repo */
$repo = app()->make(StoreCouponRepository::class);
$coupon = $repo->validCouponQuery(StoreCouponRepository::TYPE_PLATFORM_CARD, StoreCouponRepository::GET_COUPON_TYPE_BUY)->find();
/** @var StoreCouponRepository $couponRepo */
$couponRepo = app()->make(StoreCouponRepository::class);
$coupon = $couponRepo->validCouponQuery(StoreCouponRepository::TYPE_PLATFORM_CARD, StoreCouponRepository::GET_COUPON_TYPE_BUY)->find();
if (empty($coupon)) {
return;
}
/** @var StoreCouponUserRepository $userCouponRepo */
$userCouponRepo = app()->make(StoreCouponUserRepository::class);
$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::STATUS_VALID, StoreCouponDetail::SEND_FINISHED);
$userCouponRepo->order = $order;
$userCouponRepo->coupon = $coupon;
$userCouponRepo->uid = $order['uid'];
$userCouponRepo->startTime = date('Y-m-d H:i:s');
$userCouponRepo->endTime = '2025-07-01';
$userCouponRepo->sendType = StoreCouponUserRepository::SEND_TYPE_BUY;
$userCouponRepo->detailStatus = StoreCouponDetail::STATUS_VALID;
$userCouponRepo->sendStatus = StoreCouponDetail::SEND_FINISHED;
$userCouponRepo->orderIds = $order['order_id'];
$userCouponRepo->send();
//发放春耕补贴优惠券
if ($couponTotal >= 1000) {
$giveCoupon = $repo->validCouponQuery(StoreCouponRepository::TYPE_STORE_COUPON, StoreCouponRepository::GET_COUPON_TYPE_PAY)->find();
$giveCoupon = $couponRepo->validCouponQuery(StoreCouponRepository::TYPE_STORE_COUPON, StoreCouponRepository::GET_COUPON_TYPE_PAY)->find();
if (empty($giveCoupon)) {
return;
}
$rate = $this->getRate($couponTotal);
$giveCoupon->coupon_price = bcmul($couponTotal, $rate, 2);
$repo->startTime = date('Y-m-d H:i:s');
$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']);
$userCouponRepo->coupon = $giveCoupon;
$userCouponRepo->onlyDetail = true;
$userCouponRepo->detailStatus = StoreCouponDetail::STATUS_INVALID;
$userCouponRepo->sendStatus = StoreCouponDetail::SEND_REPEAL;
$userCouponRepo->send();
}
}
}

View File

@ -11,6 +11,7 @@ use app\common\repositories\store\coupon\StoreCouponUserRepository;
use crmeb\interfaces\ListenerInterface;
use crmeb\services\TimerService;
use think\facade\Log;
class SendSubsidyCouponListen extends TimerService implements ListenerInterface
{
@ -41,18 +42,22 @@ class SendSubsidyCouponListen extends TimerService implements ListenerInterface
}
//补贴金额为当前补贴减去已获得的补贴
$amount = bcsub($item['subsidy'], $gotSubsidy, 2);
/** @var StoreCouponRepository $repo */
$repo = app()->make(StoreCouponRepository::class);
/** @var StoreCouponUserRepository $userCouponRepo */
$userCouponRepo = app()->make(StoreCouponUserRepository::class);
$coupon->coupon_price = $amount;
$repo->status = StoreCouponUser::STATUS_REPEAL;
$couponUser = $repo->sendCoupon($coupon, $merchant->uid, StoreCouponUserRepository::SEND_TYPE_SEND);
$order = [
'uid' => $merchant['uid'],
'order_id' => 0,
'pay_price' => 0,
'group_order_id' => 0,
$userCouponRepo->status = StoreCouponUser::STATUS_REPEAL;
$userCouponRepo->coupon = $coupon;
$userCouponRepo->order = ['uid' => $merchant['uid'],];
$userCouponRepo->extra = [
'sale_target' => $item['amount'],
'purchase_target' => $purchaseAmount,
'official_purchase_target' => $officialPurchaseAmount,
];
StoreCouponDetail::income($order, $couponUser['coupon_user_id'], $coupon->coupon_price, $coupon['title']);
$userCouponRepo->orderIds = 0;
$userCouponRepo->onlyDetail = true;
$userCouponRepo->detailStatus = StoreCouponDetail::STATUS_INVALID;
$userCouponRepo->sendStatus = StoreCouponDetail::SEND_REPEAL;
$userCouponRepo->send();
$count++;
}
}

View File

@ -7,15 +7,17 @@ use app\common\middleware\AllowOriginMiddleware;
use app\common\middleware\LogMiddleware;
Route::group(function () {
//文章分类
Route::group('system/financial/subsidy', function () {
Route::resource('', 'SubsidyController');
Route::get('index', '/index')->name('userSubsidy')->option([
'_alias' => '补贴列表',
'_auth' => false,
'_form' => 'userSubsidyList',
]);
Route::post('status', '/status')->name('userSubsidyStatus')->option([
'_alias' => '补贴审核',
'_auth' => false,
'_form' => 'userSubsidyStatus',
]);
})->prefix('admin.system.financial.Subsidy')->option([
'_path' => '/cms/userSubsidy',
'_auth' => true,