feat : coupon
This commit is contained in:
@@ -138,32 +138,26 @@ class GoodCache
|
|||||||
|
|
||||||
$skuList = $this->skuModel->getListBySpuIds($spuIds);
|
$skuList = $this->skuModel->getListBySpuIds($spuIds);
|
||||||
|
|
||||||
if (empty($skuList)) return $list;
|
if ($skuList->isEmpty()) return $list;
|
||||||
|
|
||||||
$skuList = $skuList->toArray();
|
$skuList = $skuList->toArray();
|
||||||
|
|
||||||
$imageIdArr = array_column($skuList,'image_ids');
|
$imageIdArr = array_column($skuList,'image_ids');
|
||||||
$imageIds = array_unique(explode(',',implode(',',$imageIdArr)));
|
// $imageIds = array_unique(explode(',',implode(',',$imageIdArr)));
|
||||||
$imageList = $this->getOssObjects($imageIds);
|
$imageList = $this->getOssObjects($imageIdArr);
|
||||||
|
|
||||||
$skuListArr = [];
|
$skuListArr = [];
|
||||||
$imageArr = [];
|
$imageArr = [];
|
||||||
$stockArr = [];
|
$stockArr = [];
|
||||||
foreach ($skuList as $sku) {
|
foreach ($skuList as $sku) {
|
||||||
$imageOneArr = [];
|
|
||||||
foreach (explode(',',$sku['image_ids']) as $imageId) {
|
|
||||||
$imageOneArr[] = [
|
|
||||||
'id' => $imageId,
|
|
||||||
'url' => $imageList[$imageId]['url']
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
if (empty($skuListArr[$sku['spu_id']])) {
|
if (empty($skuListArr[$sku['spu_id']])) {
|
||||||
$skuListArr[$sku['spu_id']] = [];
|
$skuListArr[$sku['spu_id']] = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$sku['url'] = $imageList[$sku['image_ids']]['url'] ?? '';
|
||||||
|
|
||||||
$skuListArr[$sku['spu_id']][] = $sku;
|
$skuListArr[$sku['spu_id']][] = $sku;
|
||||||
$imageArr[$sku['spu_id']] = array_merge($imageArr[$sku['spu_id']] ?? [],$imageOneArr);
|
$imageArr[$sku['spu_id']] = array_merge($imageArr[$sku['spu_id']] ?? [],$sku['url']);
|
||||||
|
|
||||||
$stockArr[] = [
|
$stockArr[] = [
|
||||||
'id' => $sku['id'],
|
'id' => $sku['id'],
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use App\Controller\AbstractController;
|
|||||||
use App\Middleware\Api\JwtAuthMiddleware;
|
use App\Middleware\Api\JwtAuthMiddleware;
|
||||||
use App\Service\Api\Coupon\CouponListService;
|
use App\Service\Api\Coupon\CouponListService;
|
||||||
use App\Service\Api\Coupon\HomePopupsService;
|
use App\Service\Api\Coupon\HomePopupsService;
|
||||||
|
use App\Service\Api\Coupon\ReceiveService;
|
||||||
use Hyperf\HttpServer\Annotation\Controller;
|
use Hyperf\HttpServer\Annotation\Controller;
|
||||||
use Hyperf\HttpServer\Annotation\Middlewares;
|
use Hyperf\HttpServer\Annotation\Middlewares;
|
||||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||||
@@ -29,6 +30,11 @@ class CouponController extends AbstractController
|
|||||||
return (new HomePopupsService)->handle();
|
return (new HomePopupsService)->handle();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function receiveCoupon()
|
||||||
|
{
|
||||||
|
return (new ReceiveService)->handle();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -82,25 +82,22 @@ class CouponDispenseLog extends Model
|
|||||||
*/
|
*/
|
||||||
public function getNoReceiveCount(): array
|
public function getNoReceiveCount(): array
|
||||||
{
|
{
|
||||||
$all = $this
|
return $this
|
||||||
->where('claim_rule',CouponCode::DISPENSE_CLAIM_RULE_HOME_POPUPS)
|
->where('claim_rule',CouponCode::DISPENSE_CLAIM_RULE_HOME_POPUPS)
|
||||||
->whereColumn('total_count','!=','receive_count')
|
->whereColumn('total_count','!=','receive_count')
|
||||||
->where('appoint_group',CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE)
|
->where('appoint_group',CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE)
|
||||||
->where('status',CouponCode::DISPENSE_VALIDITY)
|
->where('status',CouponCode::DISPENSE_VALIDITY)
|
||||||
->pluck('id')
|
->pluck('id')
|
||||||
->toArray();
|
->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
$appoint = $this
|
/**
|
||||||
->where('claim_rule',CouponCode::DISPENSE_CLAIM_RULE_HOME_POPUPS)
|
* @param array $ids
|
||||||
->where('appoint_group','!=',CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE)
|
* @return Collection
|
||||||
->where('status',CouponCode::DISPENSE_VALIDITY)
|
*/
|
||||||
->pluck('id')
|
public function getInfoByIds(array $ids)
|
||||||
->toArray();
|
{
|
||||||
|
return $this->whereIn('id',$ids)->get();
|
||||||
return [
|
|
||||||
'all' => $all,
|
|
||||||
'appoint' => $appoint
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,12 +22,6 @@ use Hyperf\Di\Annotation\Inject;
|
|||||||
|
|
||||||
class HomePopupsService extends BaseService
|
class HomePopupsService extends BaseService
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var CouponDispenseUser
|
|
||||||
*/
|
|
||||||
#[Inject]
|
|
||||||
protected CouponDispenseUser $couponDispenseUserModel;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var CouponDispenseLog
|
* @var CouponDispenseLog
|
||||||
*/
|
*/
|
||||||
@@ -40,158 +34,211 @@ class HomePopupsService extends BaseService
|
|||||||
#[Inject]
|
#[Inject]
|
||||||
protected UserCoupon $userCouponModel;
|
protected UserCoupon $userCouponModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array|array[]
|
||||||
|
*/
|
||||||
|
protected array $res = [
|
||||||
|
'can_receive_ids' => [],
|
||||||
|
'coupon_info' => []
|
||||||
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function handle(): array
|
public function handle(): array
|
||||||
{
|
{
|
||||||
$dispenseIds = $this->couponDispenseUserModel->getNoReceiveCountByUserId($this->userId);
|
|
||||||
|
|
||||||
$data = $this->couponDispenseLogModel->getNoReceiveCount();
|
$data = $this->couponDispenseLogModel->getNoReceiveCount();
|
||||||
if (empty($dispenseIds) && empty($data['all']) && empty($data['appoint'])) return $this->returnNullRes();
|
|
||||||
|
|
||||||
$allDispenseIds = [];
|
if (empty($data)) return $this->return->success('success',$this->res);
|
||||||
if (!empty($data['all'])) {
|
|
||||||
$allDispenseIds = $this->userCouponModel->getReceiveCountByUserIds($this->userId,$data['all']);
|
|
||||||
}
|
|
||||||
|
|
||||||
$canReceive = array_unique(array_merge(
|
$allDispenseIds = $this->userCouponModel->getReceiveCountByUserIds($this->userId,$data);
|
||||||
array_diff($data['all'], $allDispenseIds),
|
|
||||||
array_intersect($dispenseIds,$data['appoint'])
|
|
||||||
));
|
|
||||||
|
|
||||||
if (empty($canReceive)) return $this->returnNullRes();
|
$canReceive = array_unique(array_diff($data,$allDispenseIds));
|
||||||
|
|
||||||
$res = $this->receive($canReceive);
|
if (empty($canReceive)) return $this->return->success('success',$this->res);
|
||||||
|
|
||||||
return $this->return->success('success',['coupon_data' => $res]);
|
$this->getInfo($canReceive);
|
||||||
|
|
||||||
|
return $this->return->success('success',$this->res);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param array $canReceive
|
||||||
* @return array
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function receive(array $data): array
|
private function getInfo(array $canReceive): void
|
||||||
{
|
{
|
||||||
$list = $this->couponDispenseLogModel->getListByIds($data);
|
$list = $this->couponDispenseLogModel->getListByIds($canReceive);
|
||||||
if (empty($list)) return [];
|
if (empty($list)) return;
|
||||||
|
|
||||||
$insertData = [];
|
|
||||||
$appointUpdateData = [];
|
|
||||||
$allUpdateData =[];
|
|
||||||
$destroyDispenseData = [];
|
$destroyDispenseData = [];
|
||||||
|
|
||||||
$resCouponData = [];
|
|
||||||
|
|
||||||
foreach ($list as $item) {
|
foreach ($list as $item) {
|
||||||
$validityTime = [];
|
$endTime = null;
|
||||||
|
$validityValue = json_decode($item['validity_time_value'],true);
|
||||||
switch ($item['validity_time_type']) {
|
switch ($item['validity_time_type']) {
|
||||||
case CouponCode::VALIDITY_TIME_TYPE_CYCLE:
|
case CouponCode::VALIDITY_TIME_TYPE_CYCLE:
|
||||||
$validityValue = json_decode($item['validity_time_value'],true);
|
$endTime = date('Y-m-d H:i:s', time() + ($validityValue['day_num'] * DateUtil::DAY));
|
||||||
$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;
|
break;
|
||||||
case CouponCode::VALIDITY_TIME_TYPE_FIX:
|
case CouponCode::VALIDITY_TIME_TYPE_FIX:
|
||||||
$validityTime = json_decode($item['validity_time_value'],true);
|
$endTime = $validityValue['end_time'] ?? null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
//有效期不存在或者到期销毁当前分发逻辑
|
//有效期不存在或者到期销毁当前分发逻辑
|
||||||
if (($validityTime['end_time'] ?? date('Y-m-d H:i:s')) < date('Y-m-d H:i:s')) {
|
if (($endTime ?? date('Y-m-d H:i:s')) < date('Y-m-d H:i:s')) {
|
||||||
$destroyDispenseData[] = $item['id'];
|
$destroyDispenseData[] = $item['id'];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$oneData = [
|
$oneData = [
|
||||||
'user_id' => $this->userId,
|
|
||||||
'coupon_template_id' => $item['coupon_template_id'],
|
'coupon_template_id' => $item['coupon_template_id'],
|
||||||
'coupon_name' => $item['coupon_name'],
|
'coupon_name' => $item['coupon_name'],
|
||||||
'coupon_dispense_id' => $item['id'],
|
'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) {
|
$copies = array_map(function () use ($oneData) {
|
||||||
return $oneData;
|
return $oneData;
|
||||||
}, array_fill(0, $item['item_count'], null));
|
}, array_fill(0, $item['item_count'], null));
|
||||||
$insertData = array_merge($insertData, $copies);
|
$this->res['coupon_info'] = array_merge($this->res['coupon_info'], $copies);
|
||||||
|
|
||||||
$oneData['count'] = $item['item_count'];
|
$this->res['can_receive_ids'][] = $item['id'];
|
||||||
|
|
||||||
if ($item['appoint_group'] != CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE) {
|
|
||||||
$appointUpdateData[] = [
|
|
||||||
'coupon_dispense_id' => $item['id'],
|
|
||||||
// 'user_id' => $this->userId,
|
|
||||||
'receive_count' => $item['receive_count'],
|
|
||||||
'is_receive' => CouponCode::DISPENSE_STATUS_IS_RECEIVED
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
$allUpdateData[] = [
|
|
||||||
'id' => $item['id'],
|
|
||||||
'receive_count' => $item['receive_count'] +$item['item_count'],
|
|
||||||
];
|
|
||||||
|
|
||||||
$resCouponData[] = $oneData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (empty($allUpdateData)) return [];
|
if (!empty($destroyDispenseData)) {
|
||||||
|
(new CouponDispenseLog)->whereIn('id',$destroyDispenseData)->update([
|
||||||
if (!empty($appointUpdateData)) {
|
'status' => CouponCode::DISPENSE_NULL,
|
||||||
$appointList = $this->couponDispenseUserModel->where('user_id',$this->userId)->whereIn('id',array_column($appointUpdateData,'coupon_dispense_id'))->pluck('id','coupon_dispense_id');
|
]);
|
||||||
|
|
||||||
foreach ($appointUpdateData as &$item) {
|
|
||||||
$item['id'] = $appointList[$item['coupon_dispense_id']];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Db::transaction(function () use($allUpdateData,$appointUpdateData,$insertData,$destroyDispenseData) {
|
// return $this->res;
|
||||||
$appointUpdateFlag = true;
|
|
||||||
if (!empty($appointUpdateData)) {
|
|
||||||
$userUpdateModel = new CouponDispenseUser();
|
|
||||||
foreach (array_chunk($appointUpdateData, 500) as $chunk) {
|
|
||||||
foreach ($chunk as $item) {
|
|
||||||
$appointUpdateFlag = $userUpdateModel
|
|
||||||
->where('id',$item['id'])
|
|
||||||
->update(array_diff_key($item, ['coupon_dispense_id' => null,'id' => null]));
|
|
||||||
if (!$appointUpdateFlag) break;
|
|
||||||
}
|
|
||||||
if (!$appointUpdateFlag) break;
|
|
||||||
}
|
|
||||||
// $appointUpdateFlag = (new CouponDispenseUser)->update($appointUpdateData);
|
|
||||||
}
|
|
||||||
|
|
||||||
$destroyDispenseFlag = true;
|
|
||||||
if (!empty($destroyDispenseData)) {
|
|
||||||
$destroyDispenseFlag = (new CouponDispenseLog)->whereIn('id',$destroyDispenseData)->update([
|
|
||||||
'status' => CouponCode::DISPENSE_NULL,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// $allUpdateFlag = (new CouponDispenseLog)->update($allUpdateData);
|
|
||||||
$allUpdateFlag = true;
|
|
||||||
$updateModel = new CouponDispenseLog();
|
|
||||||
foreach (array_chunk($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($insertData);
|
|
||||||
|
|
||||||
if (!$allUpdateFlag || !$appointUpdateFlag || !$insertFlag || !$destroyDispenseFlag) throw new ErrException('领取失败');
|
|
||||||
});
|
|
||||||
|
|
||||||
return $resCouponData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function returnNullRes(): array
|
// /**
|
||||||
{
|
// * @param array $data
|
||||||
return $this->return->success();
|
// * @return array
|
||||||
}
|
// */
|
||||||
|
// private function receive(array $data): array
|
||||||
|
// {
|
||||||
|
// $list = $this->couponDispenseLogModel->getListByIds($data);
|
||||||
|
// if (empty($list)) return [];
|
||||||
|
//
|
||||||
|
// $insertData = [];
|
||||||
|
// $appointUpdateData = [];
|
||||||
|
// $allUpdateData =[];
|
||||||
|
// $destroyDispenseData = [];
|
||||||
|
//
|
||||||
|
// $resCouponData = [];
|
||||||
|
//
|
||||||
|
// foreach ($list 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')) {
|
||||||
|
// $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));
|
||||||
|
// $insertData = array_merge($insertData, $copies);
|
||||||
|
//
|
||||||
|
// $oneData['count'] = $item['item_count'];
|
||||||
|
//
|
||||||
|
// if ($item['appoint_group'] != CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE) {
|
||||||
|
// $appointUpdateData[] = [
|
||||||
|
// 'coupon_dispense_id' => $item['id'],
|
||||||
|
//// 'user_id' => $this->userId,
|
||||||
|
// 'receive_count' => $item['receive_count'],
|
||||||
|
// 'is_receive' => CouponCode::DISPENSE_STATUS_IS_RECEIVED
|
||||||
|
// ];
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// $allUpdateData[] = [
|
||||||
|
// 'id' => $item['id'],
|
||||||
|
// 'receive_count' => $item['receive_count'] +$item['item_count'],
|
||||||
|
// ];
|
||||||
|
//
|
||||||
|
// $resCouponData[] = $oneData;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if (empty($allUpdateData)) return [];
|
||||||
|
//
|
||||||
|
// if (!empty($appointUpdateData)) {
|
||||||
|
// $appointList = $this->couponDispenseUserModel->where('user_id',$this->userId)->whereIn('id',array_column($appointUpdateData,'coupon_dispense_id'))->pluck('id','coupon_dispense_id');
|
||||||
|
//
|
||||||
|
// foreach ($appointUpdateData as &$item) {
|
||||||
|
// $item['id'] = $appointList[$item['coupon_dispense_id']];
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Db::transaction(function () use($allUpdateData,$appointUpdateData,$insertData,$destroyDispenseData) {
|
||||||
|
// $appointUpdateFlag = true;
|
||||||
|
// if (!empty($appointUpdateData)) {
|
||||||
|
// $userUpdateModel = new CouponDispenseUser();
|
||||||
|
// foreach (array_chunk($appointUpdateData, 500) as $chunk) {
|
||||||
|
// foreach ($chunk as $item) {
|
||||||
|
// $appointUpdateFlag = $userUpdateModel
|
||||||
|
// ->where('id',$item['id'])
|
||||||
|
// ->update(array_diff_key($item, ['coupon_dispense_id' => null,'id' => null]));
|
||||||
|
// if (!$appointUpdateFlag) break;
|
||||||
|
// }
|
||||||
|
// if (!$appointUpdateFlag) break;
|
||||||
|
// }
|
||||||
|
//// $appointUpdateFlag = (new CouponDispenseUser)->update($appointUpdateData);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// $destroyDispenseFlag = true;
|
||||||
|
// if (!empty($destroyDispenseData)) {
|
||||||
|
// $destroyDispenseFlag = (new CouponDispenseLog)->whereIn('id',$destroyDispenseData)->update([
|
||||||
|
// 'status' => CouponCode::DISPENSE_NULL,
|
||||||
|
// ]);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//// $allUpdateFlag = (new CouponDispenseLog)->update($allUpdateData);
|
||||||
|
// $allUpdateFlag = true;
|
||||||
|
// $updateModel = new CouponDispenseLog();
|
||||||
|
// foreach (array_chunk($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($insertData);
|
||||||
|
//
|
||||||
|
// if (!$allUpdateFlag || !$appointUpdateFlag || !$insertFlag || !$destroyDispenseFlag) throw new ErrException('领取失败');
|
||||||
|
// });
|
||||||
|
//
|
||||||
|
// return $resCouponData;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// private function returnNullRes(): array
|
||||||
|
// {
|
||||||
|
// return $this->return->success();
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
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