feat : coupon
This commit is contained in:
141
app/Service/Api/Coupon/HomePopupsService.php
Normal file
141
app/Service/Api/Coupon/HomePopupsService.php
Normal file
@@ -0,0 +1,141 @@
|
||||
<?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\Model\CouponDispenseLog;
|
||||
use App\Model\CouponDispenseUser;
|
||||
use App\Model\UserCoupon;
|
||||
use App\Service\Api\BaseService;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class HomePopupsService extends BaseService
|
||||
{
|
||||
/**
|
||||
* @var CouponDispenseUser
|
||||
*/
|
||||
#[Inject]
|
||||
protected CouponDispenseUser $couponDispenseUserModel;
|
||||
|
||||
/**
|
||||
* @var CouponDispenseLog
|
||||
*/
|
||||
#[Inject]
|
||||
protected CouponDispenseLog $couponDispenseLogModel;
|
||||
|
||||
/**
|
||||
* @var UserCoupon
|
||||
*/
|
||||
#[Inject]
|
||||
protected UserCoupon $userCouponModel;
|
||||
|
||||
|
||||
public function handle(): array
|
||||
{
|
||||
$dispenseIds = $this->couponDispenseUserModel->getNoReceiveCountByUserIds($this->userId);
|
||||
|
||||
$data = $this->couponDispenseLogModel->getNoReceiveCount();
|
||||
if (empty($dispenseIds) && empty($data['all']) && empty($data['appoint'])) return $this->returnNullRes();
|
||||
|
||||
$allDispenseIds = [];
|
||||
if (!empty($all['all'])) {
|
||||
$allDispenseIds = $this->userCouponModel->getReceiveCountByUserIds($this->userId,$data['all']);
|
||||
}
|
||||
|
||||
$canReceive = array_unique(array_merge(
|
||||
array_diff($data['all'], $allDispenseIds),
|
||||
array_intersect($dispenseIds,$data['appoint'])
|
||||
));
|
||||
|
||||
if (empty($canReceive)) return $this->returnNullRes();
|
||||
|
||||
$this->receive($canReceive);
|
||||
|
||||
return $this->return->success('success',['canReceive'=>$canReceive]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
private function receive(array $data): void
|
||||
{
|
||||
$list = $this->couponDispenseLogModel->getListByIds($data);
|
||||
if (empty($list)) return;
|
||||
|
||||
$insertData = [];
|
||||
$appointUpdateData = [];
|
||||
$allUpdateData =[];
|
||||
foreach ($list as $item) {
|
||||
$oneData = [
|
||||
'user_id' => $this->userId,
|
||||
'coupon_template_id' => $item['coupon_template_id'],
|
||||
'coupon_name' => $item['coupon_name']->coupon_name,
|
||||
'coupon_dispense_id' => $item['id'],
|
||||
'status' => CouponCode::COUPON_STATUS_UNUSED,
|
||||
'validity_start_time' => $item['validity_start_time'],
|
||||
'validity_end_time' => $item['validity_end_time'],
|
||||
];
|
||||
|
||||
$copies = array_map(function () use ($oneData) {
|
||||
return $oneData;
|
||||
}, array_fill(0, $item['item_count'], null));
|
||||
|
||||
$insertData = array_merge($insertData, $copies);
|
||||
|
||||
if ($item['appoint_group'] == CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE) {
|
||||
$allUpdateData[] = [
|
||||
'id' => $item['id'],
|
||||
'receive_count' => $item['receive_count'] +$item['item_count'],
|
||||
];
|
||||
} else {
|
||||
$appointUpdateData[] = [
|
||||
'coupon_dispense_id' => $item['id'],
|
||||
'user_id' => $this->userId,
|
||||
];
|
||||
|
||||
$allUpdateData[] = [
|
||||
'id' => $item['id'],
|
||||
'receive_count' => $item['receive_count'] +$item['item_count'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
$appointUpdateFlag = true;
|
||||
|
||||
if (!empty($appointUpdateData)) {
|
||||
$appointUpdateFlag = (new CouponDispenseUser())->update($appointUpdateData);
|
||||
}
|
||||
|
||||
$allUpdateFlag = (new CouponDispenseLog)->update($allUpdateData);
|
||||
|
||||
if (!$allUpdateFlag || !$appointUpdateFlag) throw new ErrException('领取失败');
|
||||
});
|
||||
}
|
||||
|
||||
private function returnNullRes(): array
|
||||
{
|
||||
return $this->return->success();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user