feat : coupon
This commit is contained in:
@@ -59,7 +59,7 @@ class AutoDispenseService
|
||||
foreach ($partArr as $onePart) {
|
||||
$insertData = [];
|
||||
foreach ($onePart as $oneUser) {
|
||||
//todo 模拟数据
|
||||
// 模拟数据
|
||||
$oneUserData = [
|
||||
'user_id' => $oneUser,
|
||||
'coupon_template_id' => $dispenseInfo->coupon_template_id,
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
19
app/Service/Api/Coupon/ReceiveService.php
Normal file
19
app/Service/Api/Coupon/ReceiveService.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Api\Coupon;
|
||||
|
||||
class ReceiveService extends
|
||||
{
|
||||
public function handle()
|
||||
{
|
||||
//todo Write logic
|
||||
}
|
||||
}
|
||||
@@ -11,13 +11,19 @@ declare(strict_types=1);
|
||||
namespace App\Service\Api\Login;
|
||||
|
||||
use App\Cache\Redis\Api\ApiRedisKey;
|
||||
use App\Cache\Redis\Common\ConfigCache;
|
||||
use App\Cache\Redis\RedisCache;
|
||||
use App\Constants\Common\CouponCode;
|
||||
use App\Constants\Common\UserCode;
|
||||
use App\Constants\ConfigCode;
|
||||
use App\Exception\ErrException;
|
||||
use App\Extend\StringUtil;
|
||||
use App\Extend\SystemUtil;
|
||||
use App\Lib\Crypto\CryptoFactory;
|
||||
use App\Model\CouponTemplate;
|
||||
use App\Model\User;
|
||||
use App\Model\UserAccount;
|
||||
use App\Model\UserCoupon;
|
||||
use App\Model\UserThird;
|
||||
use App\Service\Api\BaseService;
|
||||
use App\Service\ServiceTrait\Api\GetUserInfoTrait;
|
||||
@@ -50,6 +56,12 @@ abstract class LoginBaseService extends BaseService
|
||||
#[Inject]
|
||||
protected UserThird $userThirdModel;
|
||||
|
||||
/**
|
||||
* @var ConfigCache
|
||||
*/
|
||||
#[Inject]
|
||||
protected ConfigCache $configCache;
|
||||
|
||||
|
||||
/**
|
||||
* 锁定注册
|
||||
@@ -163,5 +175,62 @@ abstract class LoginBaseService extends BaseService
|
||||
$this->userInfo = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加账户
|
||||
* @return void
|
||||
*/
|
||||
protected function addAccount(): void
|
||||
{
|
||||
$model = new UserAccount();
|
||||
|
||||
$model->user_id = $this->userId;
|
||||
$model->balance = 0;
|
||||
$model->integral = 0;
|
||||
|
||||
if (!$model->save()) throw new ErrException('注册失败-00002');
|
||||
}
|
||||
|
||||
/**
|
||||
* @var CouponTemplate $couponTemplateModel
|
||||
*/
|
||||
#[Inject]
|
||||
protected CouponTemplate $couponTemplateModel;
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
protected function addCoupon(): void
|
||||
{
|
||||
$couponTemplateId = $this->configCache->getConfigValue(ConfigCode::COUPONS_FOR_NEWCOMERS);
|
||||
$couponValidity = $this->configCache->getConfigValue(ConfigCode::NEWBIE_COUPON_VALIDITY);
|
||||
|
||||
// 随便一个为0代表不赠送
|
||||
if ($couponValidity == 0 || $couponTemplateId == 0) return;
|
||||
|
||||
$couponTemplateId = explode(',', $couponTemplateId);
|
||||
$couponTemplateList = $this->couponTemplateModel->getDataByIds($couponTemplateId);
|
||||
|
||||
$insertCoupon = [];
|
||||
foreach ($couponTemplateId as $one){
|
||||
if ($couponTemplateList[$one]['status'] == CouponCode::COUPON_TEMPLATE_STATUS_ENABLE) continue;
|
||||
|
||||
$insertCoupon[] = [
|
||||
'coupon_template_id' => $one,
|
||||
'coupon_dispense_id' => CouponCode::SYSTEMIC_DISTRIBUTION,
|
||||
'user_id' => $this->userId,
|
||||
'status' => CouponCode::COUPON_STATUS_UNUSED,
|
||||
'coupon_name' => $couponTemplateList[$one]['name'],
|
||||
'validity_start_time' => date('Y-m-d H:i:s'),
|
||||
'validity_end_time' => date('Y-m-d', strtotime('+'.$couponValidity.' days')),
|
||||
];
|
||||
}
|
||||
|
||||
if (empty($insertCoupon)) return;
|
||||
|
||||
if (!(new UserCoupon)->insert($insertCoupon)) throw new ErrException('注册失败-00003');
|
||||
}
|
||||
|
||||
abstract protected function register();
|
||||
}
|
||||
@@ -77,7 +77,10 @@ class WxFastLoginService extends LoginBaseService
|
||||
|
||||
$this->addAccount();
|
||||
|
||||
//todo 要不要生成邀请码 有没有注册奖励
|
||||
//todo 邀请过来的 邀请方有没有奖励 被邀请方有没有奖励
|
||||
|
||||
// 有没有注册奖励
|
||||
$this->addCoupon();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -95,19 +98,4 @@ class WxFastLoginService extends LoginBaseService
|
||||
|
||||
if (!$model->save()) throw new ErrException('注册失败-00001');
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加账户
|
||||
* @return void
|
||||
*/
|
||||
private function addAccount(): void
|
||||
{
|
||||
$model = new UserAccount();
|
||||
|
||||
$model->user_id = $this->userId;
|
||||
$model->balance = 0;
|
||||
$model->integral = 0;
|
||||
|
||||
if (!$model->save()) throw new ErrException('注册失败-00002');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user