feat : auto coupon
This commit is contained in:
60
app/Amqp/Consumer/CouponAutoDispenseConsumer.php
Normal file
60
app/Amqp/Consumer/CouponAutoDispenseConsumer.php
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Amqp\Consumer;
|
||||||
|
|
||||||
|
use App\Lib\Log;
|
||||||
|
use App\Service\Amqp\Coupon\AutoDispenseService;
|
||||||
|
use Exception;
|
||||||
|
use Hyperf\Amqp\Message\Type;
|
||||||
|
use Hyperf\Amqp\Result;
|
||||||
|
use Hyperf\Amqp\Annotation\Consumer;
|
||||||
|
use Hyperf\Amqp\Message\ConsumerMessage;
|
||||||
|
use Hyperf\Di\Annotation\Inject;
|
||||||
|
use PhpAmqpLib\Message\AMQPMessage;
|
||||||
|
use Psr\Container\ContainerExceptionInterface;
|
||||||
|
use Psr\Container\NotFoundExceptionInterface;
|
||||||
|
|
||||||
|
#[Consumer(exchange: 'CouponAutoDispense', routingKey: 'CouponAutoDispense', queue: 'CouponAutoDispense.sender', name: "CouponAutoDispenseConsumer", nums: 1)]
|
||||||
|
class CouponAutoDispenseConsumer extends ConsumerMessage
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Type|string 消息类型
|
||||||
|
*/
|
||||||
|
protected Type|string $type = Type::DIRECT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Log $log
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected Log $log;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $data
|
||||||
|
* @param AMQPMessage $message
|
||||||
|
* @return Result
|
||||||
|
* @throws ContainerExceptionInterface
|
||||||
|
* @throws NotFoundExceptionInterface
|
||||||
|
*/
|
||||||
|
public function consumeMessage($data, AMQPMessage $message): Result
|
||||||
|
{
|
||||||
|
if (!$data['coupon_dispense_id']) {
|
||||||
|
$this->log->error('CouponAutoDispenseConsumer:error:NoData:'.json_encode($data));
|
||||||
|
return Result::ACK;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$service = new AutoDispenseService();
|
||||||
|
|
||||||
|
$service->couponDispenseId = $data['coupon_dispense_id'];
|
||||||
|
|
||||||
|
$service->handle();
|
||||||
|
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$this->log->error('CouponAutoDispenseConsumer:error:'.$e->getMessage().':data:'.json_encode($data));
|
||||||
|
}
|
||||||
|
|
||||||
|
return Result::ACK;
|
||||||
|
}
|
||||||
|
}
|
||||||
26
app/Amqp/Producer/CouponAutoDispenseProducer.php
Normal file
26
app/Amqp/Producer/CouponAutoDispenseProducer.php
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Amqp\Producer;
|
||||||
|
|
||||||
|
use Hyperf\Amqp\Annotation\Producer;
|
||||||
|
use Hyperf\Amqp\Message\ProducerMessage;
|
||||||
|
use Hyperf\Amqp\Message\Type;
|
||||||
|
|
||||||
|
#[Producer(exchange: 'CouponAutoDispense', routingKey: 'CouponAutoDispense')]
|
||||||
|
class CouponAutoDispenseProducer extends ProducerMessage
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Type|string 消息类型
|
||||||
|
*/
|
||||||
|
protected Type|string $type = Type::DIRECT;
|
||||||
|
|
||||||
|
public function __construct($data)
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* $data string array => {"coupon_dispense_id":"dispenseId"}
|
||||||
|
*/
|
||||||
|
$this->payload = $data;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,9 +32,16 @@ class CouponCode
|
|||||||
CONST INT DISPENSE_APPOINT_GROUP_DESIGNATED_SITES_AND_GOODS = 5;
|
CONST INT DISPENSE_APPOINT_GROUP_DESIGNATED_SITES_AND_GOODS = 5;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int 优惠券发放规则 1 首页弹窗 2 下单账户 3 单页
|
* @var int 优惠券发放规则 1 首页弹窗 2 直发账户 3 单页
|
||||||
*/
|
*/
|
||||||
CONST INT DISPENSE_CLAIM_RULE_HOME_POPUPS = 1;
|
CONST INT DISPENSE_CLAIM_RULE_HOME_POPUPS = 1;
|
||||||
CONST INT DISPENSE_CLAIM_RULE_POST_ACCOUNT = 2;
|
CONST INT DISPENSE_CLAIM_RULE_POST_ACCOUNT = 2;
|
||||||
CONST INT DISPENSE_CLAIM_RULE_SINGLE_PAGE = 3;
|
CONST INT DISPENSE_CLAIM_RULE_SINGLE_PAGE = 3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int 优惠券状态 1 未使用 2 已使用 3 未过期
|
||||||
|
*/
|
||||||
|
CONST INT COUPON_STATUS_UNUSED = 1;
|
||||||
|
CONST INT COUPON_STATUS_USED = 2;
|
||||||
|
CONST INT COUPON_STATUS_NOT_EXPIRE = 3;
|
||||||
}
|
}
|
||||||
@@ -11,16 +11,14 @@ use Hyperf\DbConnection\Model\Model;
|
|||||||
* @property string $title
|
* @property string $title
|
||||||
* @property string $coupon_name
|
* @property string $coupon_name
|
||||||
* @property int $coupon_template_id
|
* @property int $coupon_template_id
|
||||||
* @property int $send_count
|
* @property int $total_count
|
||||||
* @property int $receive_count
|
* @property int $receive_count
|
||||||
* @property int $left_count
|
|
||||||
* @property int $item_count
|
* @property int $item_count
|
||||||
* @property int $appoint_group
|
* @property int $appoint_group
|
||||||
* @property int $claim_rule
|
* @property int $claim_rule
|
||||||
* @property string $claim_value
|
* @property string $claim_value
|
||||||
* @property int $appoint_city_id
|
|
||||||
* @property string $appoint_value
|
* @property string $appoint_value
|
||||||
* @property string $use_scen_ids
|
* @property string $use_scene_ids
|
||||||
* @property int $validity_time_type
|
* @property int $validity_time_type
|
||||||
* @property string $validity_time_value
|
* @property string $validity_time_value
|
||||||
* @property string $remark
|
* @property string $remark
|
||||||
@@ -38,9 +36,34 @@ class CouponDispenseLog extends Model
|
|||||||
* The attributes that are mass assignable.
|
* The attributes that are mass assignable.
|
||||||
*/
|
*/
|
||||||
protected array $fillable = [];
|
protected array $fillable = [];
|
||||||
|
protected array $guarded = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes that should be cast to native types.
|
* The attributes that should be cast to native types.
|
||||||
*/
|
*/
|
||||||
protected array $casts = ['id' => 'integer', 'coupon_template_id' => 'integer', 'send_count' => 'integer', 'receive_count' => 'integer', 'left_count' => 'integer', 'item_count' => 'integer', 'appoint_group' => 'integer', 'claim_rule' => 'integer', 'appoint_city_id' => 'integer', 'validity_time_type' => 'integer'];
|
protected array $casts = ['id' => 'integer', 'coupon_template_id' => 'integer', 'send_count' => 'integer', 'receive_count' => 'integer', 'left_count' => 'integer', 'item_count' => 'integer', 'appoint_group' => 'integer', 'claim_rule' => 'integer', 'appoint_city_id' => 'integer', 'validity_time_type' => 'integer'];
|
||||||
|
|
||||||
|
const string CREATED_AT = 'create_time';
|
||||||
|
const string UPDATED_AT = 'update_time';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @return CouponDispenseLog|CouponDispenseLog[]|\Hyperf\Database\Model\Model|null
|
||||||
|
*/
|
||||||
|
public function getInfoById(int $id): \Hyperf\Database\Model\Model|CouponDispenseLog|array|null
|
||||||
|
{
|
||||||
|
return $this->find($id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @param int $receiveCount
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function updateReceiveCountById(int $id,int $receiveCount): int
|
||||||
|
{
|
||||||
|
return $this->where('id', $id)->increment('receive_count' , $receiveCount);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
71
app/Model/CouponDispenseUser.php
Normal file
71
app/Model/CouponDispenseUser.php
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Model;
|
||||||
|
|
||||||
|
use Hyperf\DbConnection\Model\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property int $id
|
||||||
|
* @property int $coupon_dispense_id
|
||||||
|
* @property int $user_id
|
||||||
|
* @property int $total_count
|
||||||
|
* @property int $receive_count
|
||||||
|
* @property string $create_time
|
||||||
|
* @property string $update_time
|
||||||
|
*/
|
||||||
|
class CouponDispenseUser extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The table associated with the model.
|
||||||
|
*/
|
||||||
|
protected ?string $table = 'coupon_dispense_user';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*/
|
||||||
|
protected array $fillable = [];
|
||||||
|
protected array $guarded = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that should be cast to native types.
|
||||||
|
*/
|
||||||
|
protected array $casts = ['id' => 'integer', 'coupon_dispense_id' => 'integer', 'user_id' => 'integer', 'total_count' => 'integer', 'receive_count' => 'integer'];
|
||||||
|
|
||||||
|
const string CREATED_AT = 'create_time';
|
||||||
|
const string UPDATED_AT = 'update_time';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $dispenseId
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function getNoSendCountByDispenseId(int $dispenseId): int
|
||||||
|
{
|
||||||
|
return $this
|
||||||
|
->where('dispense_id', $dispenseId)
|
||||||
|
->where('receive_count',0)
|
||||||
|
->count() ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getNoSendUserListByDispenseId(int $dispenseId): array
|
||||||
|
{
|
||||||
|
return $this
|
||||||
|
->where('dispense_id', $dispenseId)
|
||||||
|
->where('receive_count',0)
|
||||||
|
->pluck('user_id')
|
||||||
|
->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $userIds
|
||||||
|
* @param int $count
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function updateReceiveCountByUserIds(array $userIds,int $count): int
|
||||||
|
{
|
||||||
|
return $this
|
||||||
|
->whereIn('user_id', $userIds)
|
||||||
|
->increment('receive_count', $count);
|
||||||
|
}
|
||||||
|
}
|
||||||
42
app/Model/UserCoupon.php
Normal file
42
app/Model/UserCoupon.php
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Model;
|
||||||
|
|
||||||
|
use Hyperf\DbConnection\Model\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property int $id
|
||||||
|
* @property int $coupon_template_id
|
||||||
|
* @property int $coupon_dispense_id
|
||||||
|
* @property int $user_id
|
||||||
|
* @property int $status
|
||||||
|
* @property string $coupon_name
|
||||||
|
* @property string $use_time
|
||||||
|
* @property string $validity_start_time
|
||||||
|
* @property string $validity_end_time
|
||||||
|
* @property string $create_time
|
||||||
|
* @property string $update_time
|
||||||
|
*/
|
||||||
|
class UserCoupon extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The table associated with the model.
|
||||||
|
*/
|
||||||
|
protected ?string $table = 'user_coupon';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*/
|
||||||
|
protected array $fillable = [];
|
||||||
|
protected array $guarded = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that should be cast to native types.
|
||||||
|
*/
|
||||||
|
protected array $casts = ['id' => 'integer', 'coupon_template_id' => 'integer', 'coupon_dispense_id' => 'integer', 'user_id' => 'integer', 'status' => 'integer'];
|
||||||
|
|
||||||
|
const string CREATED_AT = 'created_time';
|
||||||
|
const string UPDATED_AT = 'updated_time';
|
||||||
|
}
|
||||||
@@ -10,12 +10,215 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Service\Admin\Coupon;
|
namespace App\Service\Admin\Coupon;
|
||||||
|
|
||||||
|
use App\Constants\Common\CouponCode;
|
||||||
|
use App\Exception\ErrException;
|
||||||
|
use App\Model\CouponDispenseLog;
|
||||||
|
use App\Model\CouponDispenseUser;
|
||||||
|
use App\Model\CouponTemplate;
|
||||||
|
use App\Model\Cycle;
|
||||||
|
use App\Model\Order;
|
||||||
|
use App\Model\OrderGood;
|
||||||
|
use App\Model\Site;
|
||||||
|
use App\Model\Sku;
|
||||||
use App\Service\Admin\BaseService;
|
use App\Service\Admin\BaseService;
|
||||||
|
use App\Service\ServiceTrait\Admin\CouponDispenseTrait;
|
||||||
|
use Exception;
|
||||||
|
use Hyperf\DbConnection\Db;
|
||||||
|
use Hyperf\Di\Annotation\Inject;
|
||||||
|
|
||||||
class DispenseAddService extends BaseService
|
class DispenseAddService extends BaseService
|
||||||
{
|
{
|
||||||
public function handle()
|
use CouponDispenseTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Order
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected Order $orderModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Sku
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected Sku $skuModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var OrderGood
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected OrderGood $orderGoodModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Site
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected Site $siteModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Cycle
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected Cycle $cycleModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var CouponTemplate
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected CouponTemplate $couponTemplateModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private int $cycleId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private array $appointValue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private int $groupType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private array $userIds;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var mixed
|
||||||
|
*/
|
||||||
|
private mixed $couponTemplateInfo;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private int $claimRule;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function handle(): array
|
||||||
{
|
{
|
||||||
//todo Write logic
|
try {
|
||||||
|
$this->userIds = $this->beneficiary();
|
||||||
|
|
||||||
|
$this->checkCouponTemplate();
|
||||||
|
|
||||||
|
$this->checkClaimRuleData();
|
||||||
|
|
||||||
|
Db::transaction(function () {
|
||||||
|
$dispenseId = $this->addMasterTableInformation();
|
||||||
|
|
||||||
|
$this->addSubTableInformation($dispenseId);
|
||||||
|
});
|
||||||
|
|
||||||
|
if ($this->claimRule == CouponCode::DISPENSE_CLAIM_RULE_POST_ACCOUNT) {
|
||||||
|
//todo mq发放优惠券
|
||||||
|
echo 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->return->success();
|
||||||
|
}catch (Exception $e) {
|
||||||
|
throw new ErrException($e->getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $dispenseId
|
||||||
|
* @return void
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function addSubTableInformation(int $dispenseId): void
|
||||||
|
{
|
||||||
|
if ($this->groupType == CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE) return;
|
||||||
|
|
||||||
|
if ($this->claimRule == CouponCode::DISPENSE_CLAIM_RULE_SINGLE_PAGE) return;
|
||||||
|
|
||||||
|
if (empty($this->userIds)) return;
|
||||||
|
|
||||||
|
$insertData = [];
|
||||||
|
foreach ($this->userIds as $userId) {
|
||||||
|
$insertData[] = [
|
||||||
|
'coupon_dispense_id' => $dispenseId,
|
||||||
|
'user_id' => $userId,
|
||||||
|
'total_count' => $this->request->input('item_count'),
|
||||||
|
'create_time' => date('Y-m-d H:i:s'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($insertData)) throw new Exception('添加失败');
|
||||||
|
|
||||||
|
if (!((new CouponDispenseUser)->insert($insertData))) throw new Exception('添加失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function addMasterTableInformation(): int
|
||||||
|
{
|
||||||
|
$insertModel = new CouponDispenseLog();
|
||||||
|
|
||||||
|
$insertModel->title = $this->request->input('title',date('Ymd').'分发优惠券');
|
||||||
|
$insertModel->coupon_name = $this->request->input('coupon_name', $this->couponTemplateInfo->name);
|
||||||
|
$insertModel->coupon_template_id = $this->couponTemplateInfo->id;
|
||||||
|
$insertModel->total_count = 0;
|
||||||
|
$insertModel->receive_count = 0;
|
||||||
|
$insertModel->item_count = 0;
|
||||||
|
$insertModel->appoint_group = $this->groupType;
|
||||||
|
$insertModel->claim_rule = $this->claimRule;
|
||||||
|
$insertModel->claim_value = $this->request->input('claim_value');
|
||||||
|
$insertModel->appoint_value = json_encode($this->appointValue);
|
||||||
|
$insertModel->validity_time_type = $this->request->input('validity_time_type');
|
||||||
|
$insertModel->validity_time_value = json_encode($this->request->input('validity_time_value'));
|
||||||
|
$insertModel->remark = $this->request->input('remark');
|
||||||
|
|
||||||
|
if (!$insertModel->save()) throw new Exception('添加分发记录失败');
|
||||||
|
|
||||||
|
return $insertModel->id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function checkClaimRuleData(): void
|
||||||
|
{
|
||||||
|
$this->claimRule = (int)$this->request->input('claim_rule',CouponCode::DISPENSE_CLAIM_RULE_HOME_POPUPS);
|
||||||
|
if ($this->claimRule == CouponCode::DISPENSE_CLAIM_RULE_SINGLE_PAGE && empty($this->request->input('claim_value'))) {
|
||||||
|
throw new Exception('单页请输入路径');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function checkCouponTemplate(): void
|
||||||
|
{
|
||||||
|
$this->couponTemplateInfo = $this->couponTemplateModel->getInfoById((int)$this->request->input('coupon_template_id'));
|
||||||
|
|
||||||
|
if (empty($this->couponTemplateInfo)) throw new Exception('未找到该优惠券模板');
|
||||||
|
|
||||||
|
if ($this->couponTemplateInfo->status == CouponCode::COUPON_TEMPLATE_STATUS_ENABLE) throw new Exception('该优惠券模板已被禁用');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function beneficiary(): array
|
||||||
|
{
|
||||||
|
return match ($this->groupType = (int)$this->request->input('appoint_group',CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE))
|
||||||
|
{
|
||||||
|
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_USERS => $this->handleDesignatedUsers(),
|
||||||
|
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_SITES => $this->handleDesignatedSites(),
|
||||||
|
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_GOODS => $this->handleDesignatedGoods(),
|
||||||
|
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_SITES_AND_GOODS => $this->handleDesignatedSitesAndGoods(),
|
||||||
|
default => []
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -52,29 +52,12 @@ class DispenseConfirmService extends BaseService
|
|||||||
#[Inject]
|
#[Inject]
|
||||||
protected Site $siteModel;
|
protected Site $siteModel;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var User
|
|
||||||
*/
|
|
||||||
#[Inject]
|
|
||||||
protected User $userModel;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var CouponDispenseLog
|
|
||||||
*/
|
|
||||||
#[Inject]
|
|
||||||
protected CouponDispenseLog $couponDispenseLogModel;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Cycle
|
* @var Cycle
|
||||||
*/
|
*/
|
||||||
#[Inject]
|
#[Inject]
|
||||||
protected Cycle $cycleModel;
|
protected Cycle $cycleModel;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private array $res;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var int
|
* @var int
|
||||||
*/
|
*/
|
||||||
@@ -97,11 +80,7 @@ class DispenseConfirmService extends BaseService
|
|||||||
public function handle(): array
|
public function handle(): array
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$this->res = [];
|
$userIdList = match ($this->groupType = (int)$this->request->input('appoint_group',CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE))
|
||||||
|
|
||||||
if (empty($this->request->input('appoint_value'))) throw new Exception('请选择指定值');
|
|
||||||
|
|
||||||
match ($this->groupType = (int)$this->request->input('appoint_group',CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE))
|
|
||||||
{
|
{
|
||||||
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_USERS => $this->handleDesignatedUsers(),
|
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_USERS => $this->handleDesignatedUsers(),
|
||||||
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_SITES => $this->handleDesignatedSites(),
|
CouponCode::DISPENSE_APPOINT_GROUP_DESIGNATED_SITES => $this->handleDesignatedSites(),
|
||||||
@@ -110,59 +89,11 @@ class DispenseConfirmService extends BaseService
|
|||||||
default => throw new Exception('不需要渲染用户数据')
|
default => throw new Exception('不需要渲染用户数据')
|
||||||
};
|
};
|
||||||
|
|
||||||
return $this->return->success('success',['list' => $this->res]);
|
return $this->return->success('success',['list' => $userIdList]);
|
||||||
}catch (Exception $e) {
|
}catch (Exception $e) {
|
||||||
throw new ErrException($e->getMessage());
|
throw new ErrException($e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
private function handleDesignatedUsers(): void
|
|
||||||
{
|
|
||||||
$this->res = explode(',',$this->request->input('appoint_value'));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function handleDesignatedSitesAndGoods(): void
|
|
||||||
{
|
|
||||||
$this->getValueAndCheckDate();
|
|
||||||
|
|
||||||
$this->checkSite();
|
|
||||||
|
|
||||||
$this->checkSku();
|
|
||||||
|
|
||||||
$this->getUserData();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function handleDesignatedSites(): void
|
|
||||||
{
|
|
||||||
$this->getValueAndCheckDate();
|
|
||||||
|
|
||||||
$this->checkSite();
|
|
||||||
|
|
||||||
$this->getUserData();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return void
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function handleDesignatedGoods(): void
|
|
||||||
{
|
|
||||||
$this->getValueAndCheckDate();
|
|
||||||
|
|
||||||
$this->checkSku();
|
|
||||||
|
|
||||||
$this->getUserData();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
94
app/Service/Amqp/Coupon/AutoDispenseService.php
Normal file
94
app/Service/Amqp/Coupon/AutoDispenseService.php
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* This service file is part of item.
|
||||||
|
*
|
||||||
|
* @author ctexthuang
|
||||||
|
* @contact ctexthuang@qq.com
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Service\Amqp\Coupon;
|
||||||
|
|
||||||
|
use App\Constants\Common\CouponCode;
|
||||||
|
use App\Model\CouponDispenseLog;
|
||||||
|
use App\Model\CouponDispenseUser;
|
||||||
|
use App\Model\UserCoupon;
|
||||||
|
use Exception;
|
||||||
|
use Hyperf\Coroutine\Coroutine;
|
||||||
|
use Hyperf\DbConnection\Db;
|
||||||
|
use Hyperf\Di\Annotation\Inject;
|
||||||
|
|
||||||
|
class AutoDispenseService
|
||||||
|
{
|
||||||
|
|
||||||
|
const int SINGLE_MAX = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var CouponDispenseLog
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected CouponDispenseLog $couponDispenseLogModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var CouponDispenseUser
|
||||||
|
*/
|
||||||
|
#[Inject]
|
||||||
|
protected CouponDispenseUser $couponDispenseUserModel;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
public int $couponDispenseId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function handle(): void
|
||||||
|
{
|
||||||
|
$dispenseInfo = $this->couponDispenseLogModel->getInfoById($this->couponDispenseId);
|
||||||
|
|
||||||
|
if (empty($dispenseInfo)) throw new Exception('分发记录不存在');
|
||||||
|
|
||||||
|
$userIds = $this->couponDispenseUserModel->getNoSendUserListByDispenseId($this->couponDispenseId);
|
||||||
|
if (empty($userIds)) throw new Exception('该分发已发送完毕');
|
||||||
|
|
||||||
|
$partArr = array_chunk($userIds, self::SINGLE_MAX);
|
||||||
|
|
||||||
|
foreach ($partArr as $onePart) {
|
||||||
|
$insertData = [];
|
||||||
|
foreach ($onePart as $oneUser) {
|
||||||
|
//todo 模拟数据
|
||||||
|
$oneUserData = [
|
||||||
|
'user_id' => $oneUser,
|
||||||
|
'coupon_template_id' => $dispenseInfo->coupon_template_id,
|
||||||
|
'coupon_name' => $dispenseInfo->coupon_name,
|
||||||
|
'coupon_dispense_id' => $dispenseInfo->id,
|
||||||
|
'status' => CouponCode::COUPON_STATUS_UNUSED,
|
||||||
|
'validity_start_time' => $dispenseInfo->validity_start_time,
|
||||||
|
'validity_end_time' => $dispenseInfo->validity_end_time,
|
||||||
|
];
|
||||||
|
|
||||||
|
$copies = array_map(function () use ($oneUserData) {
|
||||||
|
return $oneUserData;
|
||||||
|
}, array_fill(0, $dispenseInfo->item_count, null));
|
||||||
|
|
||||||
|
$insertData = array_merge($insertData, $copies);
|
||||||
|
}
|
||||||
|
|
||||||
|
Db::transaction(function () use ($insertData,$dispenseInfo,$onePart) {
|
||||||
|
//添加优惠券信息
|
||||||
|
if (!(new UserCoupon())->insert($insertData)) throw new Exception('写入数据失败');
|
||||||
|
//修改分发用户信息
|
||||||
|
if (!(new CouponDispenseUser())->updateReceiveCountByUserIds($onePart,$dispenseInfo->item_count)) throw new Exception('修改分发用户信息失败');
|
||||||
|
|
||||||
|
//修改分发 log 信息 (修改receive_count = origin_value + count($insertData) ps : 查询还没发的用户 * item_count / total_count = 完成百分比)
|
||||||
|
if (!(new CouponDispenseLog())->updateReceiveCountById($dispenseInfo->id,count($insertData))) throw new Exception('修改分发log信息失败');
|
||||||
|
});
|
||||||
|
|
||||||
|
// 休息1秒
|
||||||
|
Coroutine::sleep(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,8 +31,10 @@ trait CouponDispenseTrait
|
|||||||
'site' => explode(',', $appointValue['site']),
|
'site' => explode(',', $appointValue['site']),
|
||||||
'sku' => explode(',', $appointValue['goods'])
|
'sku' => explode(',', $appointValue['goods'])
|
||||||
];
|
];
|
||||||
|
} else {
|
||||||
|
$this->appointValue = explode(',', $this->request->input('appoint_value'));
|
||||||
}
|
}
|
||||||
$this->appointValue = explode(',', $this->request->input('appoint_value'));
|
|
||||||
unset($cycleInfo);
|
unset($cycleInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,10 +66,10 @@ trait CouponDispenseTrait
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return array
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
protected function getUserData(): void
|
protected function getUserData(): array
|
||||||
{
|
{
|
||||||
$userIds = [];
|
$userIds = [];
|
||||||
|
|
||||||
@@ -128,14 +130,74 @@ trait CouponDispenseTrait
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->res = $userIds;
|
return $userIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected function handleDesignatedUsers(): array
|
||||||
|
{
|
||||||
|
$this->checkAppointValue();
|
||||||
|
|
||||||
|
return explode(',',$this->request->input('appoint_value'));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected function handleDesignatedSitesAndGoods(): array
|
||||||
|
{
|
||||||
|
$this->checkAppointValue();
|
||||||
|
|
||||||
|
$this->getValueAndCheckDate();
|
||||||
|
|
||||||
|
$this->checkSite();
|
||||||
|
|
||||||
|
$this->checkSku();
|
||||||
|
|
||||||
|
return $this->getUserData();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected function handleDesignatedSites(): array
|
||||||
|
{
|
||||||
|
$this->checkAppointValue();
|
||||||
|
|
||||||
|
$this->getValueAndCheckDate();
|
||||||
|
|
||||||
|
$this->checkSite();
|
||||||
|
|
||||||
|
return $this->getUserData();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
protected function handleDesignatedGoods(): array
|
||||||
|
{
|
||||||
|
$this->checkAppointValue();
|
||||||
|
|
||||||
|
$this->getValueAndCheckDate();
|
||||||
|
|
||||||
|
$this->checkSku();
|
||||||
|
|
||||||
|
return $this->getUserData();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
protected function getUserInfoByUserIds(array $userIds)
|
protected function checkAppointValue(): void
|
||||||
{
|
{
|
||||||
|
if (empty($this->request->input('appoint_value'))) throw new Exception('请选择指定值');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user