feat : coupon
This commit is contained in:
149
app/Service/Api/Coupon/ReceiveService.php
Normal file
149
app/Service/Api/Coupon/ReceiveService.php
Normal file
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Api\Coupon;
|
||||
|
||||
use App\Constants\Common\CouponCode;
|
||||
use App\Exception\ErrException;
|
||||
use App\Extend\DateUtil;
|
||||
use App\Model\CouponDispenseLog;
|
||||
use App\Model\UserCoupon;
|
||||
use App\Service\Api\BaseService;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class ReceiveService extends BaseService
|
||||
{
|
||||
/**
|
||||
* @var CouponDispenseLog
|
||||
*/
|
||||
#[Inject]
|
||||
protected CouponDispenseLog $couponDispenseLogModel;
|
||||
|
||||
/**
|
||||
* @var UserCoupon
|
||||
*/
|
||||
#[Inject]
|
||||
protected UserCoupon $userCouponModel;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
$idArr = $this->request->input('id_arr');
|
||||
|
||||
if (empty($idArr)) throw new ErrException('参数错误');
|
||||
|
||||
$couponArr = $this->couponDispenseLogModel->getInfoByIds($idArr);
|
||||
if ($couponArr->isEmpty()) throw new ErrException('领取的优惠券中有不存在的优惠券,请刷新后领取');
|
||||
$couponArr = $couponArr->toArray();
|
||||
|
||||
$userBag = $this->userCouponModel->getReceiveCountByUserIds($this->userId, $idArr);
|
||||
|
||||
if (count(array_diff(array_column($couponArr, 'id'), $userBag)) != $couponArr) throw new ErrException('优惠券已领完');
|
||||
|
||||
$this->receive($couponArr);
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $insertData = [];
|
||||
private array $allUpdateData =[];
|
||||
private array $destroyDispenseData = [];
|
||||
|
||||
/**
|
||||
* @param array $couponArr
|
||||
* @return void
|
||||
*/
|
||||
private function receive(array $couponArr): void
|
||||
{
|
||||
if (empty($couponArr)) return;
|
||||
|
||||
foreach ($couponArr as $item) {
|
||||
$validityTime = [];
|
||||
switch ($item['validity_time_type']) {
|
||||
case CouponCode::VALIDITY_TIME_TYPE_CYCLE:
|
||||
$validityValue = json_decode($item['validity_time_value'],true);
|
||||
$validityTime = [
|
||||
'start_time' => date('Y-m-d H:i:s'),
|
||||
'end_time' => date('Y-m-d H:i:s', time() + ($validityValue['day_num'] * DateUtil::DAY))
|
||||
];
|
||||
break;
|
||||
case CouponCode::VALIDITY_TIME_TYPE_FIX:
|
||||
$validityTime = json_decode($item['validity_time_value'],true);
|
||||
break;
|
||||
}
|
||||
|
||||
//有效期不存在或者到期销毁当前分发逻辑
|
||||
if (($validityTime['end_time'] ?? date('Y-m-d H:i:s')) < date('Y-m-d H:i:s')) {
|
||||
$this->destroyDispenseData[] = $item['id'];
|
||||
continue;
|
||||
}
|
||||
|
||||
$oneData = [
|
||||
'user_id' => $this->userId,
|
||||
'coupon_template_id' => $item['coupon_template_id'],
|
||||
'coupon_name' => $item['coupon_name'],
|
||||
'coupon_dispense_id' => $item['id'],
|
||||
'status' => CouponCode::COUPON_STATUS_UNUSED,
|
||||
'validity_start_time' => $validityTime['start_time'] ?? date('Y-m-d H:i:s', time()),
|
||||
'validity_end_time' => $validityTime['end_time'] ?? date('Y-m-d H:i:s', time()),
|
||||
];
|
||||
|
||||
$copies = array_map(function () use ($oneData) {
|
||||
return $oneData;
|
||||
}, array_fill(0, $item['item_count'], null));
|
||||
$this->insertData = array_merge($this->insertData, $copies);
|
||||
|
||||
$this->allUpdateData[] = [
|
||||
'id' => $item['id'],
|
||||
'receive_count' => $item['receive_count'] +$item['item_count'],
|
||||
];
|
||||
}
|
||||
|
||||
if (empty($allUpdateData)) return;
|
||||
|
||||
$this->matters();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
private function matters(): void
|
||||
{
|
||||
Db::transaction(function () {
|
||||
$destroyDispenseFlag = true;
|
||||
if (!empty($this->destroyDispenseData)) {
|
||||
$destroyDispenseFlag = (new CouponDispenseLog)->whereIn('id',$this->destroyDispenseData)->update([
|
||||
'status' => CouponCode::DISPENSE_NULL,
|
||||
]);
|
||||
}
|
||||
|
||||
// $allUpdateFlag = (new CouponDispenseLog)->update($allUpdateData);
|
||||
$allUpdateFlag = true;
|
||||
$updateModel = new CouponDispenseLog();
|
||||
foreach (array_chunk($this->allUpdateData, 500) as $chunk) {
|
||||
foreach ($chunk as $item) {
|
||||
$allUpdateFlag = $updateModel->where('id',$item['id'])->update(array_diff_key($item, ['id' => null]));
|
||||
if (!$allUpdateFlag) break;
|
||||
}
|
||||
if (!$allUpdateFlag) break;
|
||||
}
|
||||
|
||||
$insertFlag = (new UserCoupon)->insert($this->insertData);
|
||||
|
||||
if (!$allUpdateFlag || !$insertFlag || !$destroyDispenseFlag) throw new ErrException('领取失败');
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user