feat : coupon
This commit is contained in:
@@ -51,4 +51,8 @@ class CouponCode
|
||||
CONST INT DISPENSE_STATUS_IS_NO_RECEIVED = 1;
|
||||
CONST INT DISPENSE_STATUS_IS_RECEIVED = 2;
|
||||
|
||||
/**
|
||||
* @var int 优惠券发放方式 0 系统发放
|
||||
*/
|
||||
const int SYSTEMIC_DISTRIBUTION = 0;
|
||||
}
|
||||
@@ -4,15 +4,36 @@ namespace App\Constants;
|
||||
|
||||
class ConfigCode
|
||||
{
|
||||
/**
|
||||
* @var string BasicConfiguration|基础配置
|
||||
*/
|
||||
const string TODAY_CUT_OFF_TIME_KEY = 'TodayCutOffTime'; // 当日下单截止时间
|
||||
const string ORDER_CANCEL_TIME_KEY = 'OrderCancelTime'; // 订单取消时间(下单后自动取消)
|
||||
const string SUNDRY_UNIT_PRICE = 'SundryUnitPrice'; // 附加费单价 (服务费)
|
||||
const string SUNDRY_PRICE_COMPUTE_TYPE = 'SundryPriceComputeType'; // 附加费计算方式 1 仅自选计算(默认值) 2 仅套餐计算 3 套餐+自选计算
|
||||
|
||||
/**
|
||||
* @var string CouponConfiguration|优惠券配置
|
||||
*/
|
||||
const string COUPONS_FOR_NEWCOMERS = 'CouponsForNewcomers'; // 新人赠送的优惠券(送多少张直接用模板 id 加英文字符隔开 0代表不赠送)
|
||||
const string NEWBIE_COUPON_VALIDITY = 'NewbieCouponValidity'; // 新人优惠券有效期(ps:从领取时间开始的第 x 天 0表示不赠送)
|
||||
const string INVITING_PARTY_COUPON = 'InvitingPartyCoupon'; // 邀请方优惠券 邀请一个人所送的优惠券
|
||||
const string INVITING_PARTY_COUPON_VALIDITY = 'InvitingPartyCouponValidity'; // 邀请方优惠券有效期(ps:从领取时间开始的第 x 天)
|
||||
const string INVITED_PARTY_COUPON = 'InvitedPartyCoupon'; // 被邀请方优惠券 被邀请一个人所送的优惠券
|
||||
const string INVITEE_COUPON_VALIDITY = 'InviteeCouponValidity'; // 被邀请方优惠券有效期(ps:从领取时间开始的第 x 天)
|
||||
|
||||
const array DEFAULT_VALUE = [
|
||||
//BasicConfiguration
|
||||
self::TODAY_CUT_OFF_TIME_KEY => '15:00:00',
|
||||
self::ORDER_CANCEL_TIME_KEY => 5,
|
||||
self::SUNDRY_UNIT_PRICE => '3',
|
||||
self::SUNDRY_PRICE_COMPUTE_TYPE => 1,
|
||||
//CouponConfiguration
|
||||
self::COUPONS_FOR_NEWCOMERS => '0',
|
||||
self::NEWBIE_COUPON_VALIDITY => '0',
|
||||
self::INVITING_PARTY_COUPON => '0',
|
||||
self::INVITING_PARTY_COUPON_VALIDITY => '0',
|
||||
self::INVITED_PARTY_COUPON => '0',
|
||||
self::INVITEE_COUPON_VALIDITY => '0',
|
||||
];
|
||||
}
|
||||
22
app/Controller/Api/CouponController.php
Normal file
22
app/Controller/Api/CouponController.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\Controller\AbstractController;
|
||||
use App\Service\Api\Coupon\HomePopupsService;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
use Hyperf\Validation\Annotation\Scene;
|
||||
|
||||
#[Controller(prefix: 'api/coupon')]
|
||||
class CouponController extends AbstractController
|
||||
{
|
||||
#[RequestMapping(path: "home_popups", methods: "GET")]
|
||||
#[Scene(scene: "home_popups")]
|
||||
public function homePopups()
|
||||
{
|
||||
return (new HomePopupsService)->handle();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use App\Constants\Common\CouponCode;
|
||||
use Hyperf\Collection\Collection;
|
||||
use Hyperf\DbConnection\Model\Model;
|
||||
|
||||
/**
|
||||
@@ -55,6 +57,15 @@ class CouponDispenseLog extends Model
|
||||
return $this->find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $ids
|
||||
* @return array
|
||||
*/
|
||||
public function getListByIds(array $ids): array
|
||||
{
|
||||
return $this->whereIn('id',$ids)->get()->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @param int $receiveCount
|
||||
@@ -64,6 +75,30 @@ class CouponDispenseLog extends Model
|
||||
{
|
||||
return $this->where('id', $id)->increment('receive_count' , $receiveCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getNoReceiveCount(): array
|
||||
{
|
||||
$all = $this
|
||||
->where('claim_rule',CouponCode::DISPENSE_CLAIM_RULE_HOME_POPUPS)
|
||||
->whereColumn('total_count','!=','receive_count')
|
||||
->where('appoint_group',CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE)
|
||||
->pluck('id')
|
||||
->toArray();
|
||||
|
||||
$appoint = $this
|
||||
->where('claim_rule',CouponCode::DISPENSE_CLAIM_RULE_HOME_POPUPS)
|
||||
->where('appoint_group','!=',CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE)
|
||||
->pluck('id')
|
||||
->toArray();
|
||||
|
||||
return [
|
||||
'all' => $all,
|
||||
'appoint' => $appoint
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -72,4 +72,17 @@ class CouponDispenseUser extends Model
|
||||
'is_receive' => CouponCode::DISPENSE_STATUS_IS_RECEIVED
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $userIds
|
||||
* @return array
|
||||
*/
|
||||
public function getNoReceiveCountByUserIds(array $userIds): array
|
||||
{
|
||||
return $this
|
||||
->whereIn('user_id', $userIds)
|
||||
->where('receive_count',CouponCode::DISPENSE_STATUS_IS_NO_RECEIVED)
|
||||
->pluck('coupon_dispense_id')
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,4 +39,13 @@ class UserCoupon extends Model
|
||||
|
||||
const string CREATED_AT = 'created_time';
|
||||
const string UPDATED_AT = 'updated_time';
|
||||
|
||||
public function getReceiveCountByUserIds(int $user_id,array $couponIdArr): array
|
||||
{
|
||||
return $this
|
||||
->where('user_id', $user_id)
|
||||
->whereIn('coupon_id', $couponIdArr)
|
||||
->pluck('coupon_id')
|
||||
->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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