266 lines
7.0 KiB
PHP
266 lines
7.0 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Admin\Coupon;
|
|
|
|
use App\Amqp\Producer\CouponAutoDispenseProducer;
|
|
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\ServiceTrait\Admin\CouponDispenseTrait;
|
|
use Exception;
|
|
use Hyperf\Amqp\Producer;
|
|
use Hyperf\Context\ApplicationContext;
|
|
use Hyperf\DbConnection\Db;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
class DispenseAddService extends BaseService
|
|
{
|
|
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 Producer
|
|
*/
|
|
#[Inject]
|
|
protected Producer $producer;
|
|
|
|
/**
|
|
* @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;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private int $dispenseId;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
private int $itemCount;
|
|
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function handle(): array
|
|
{
|
|
try {
|
|
$this->claimRule = (int)$this->request->input('claim_rule',CouponCode::DISPENSE_CLAIM_RULE_HOME_POPUPS);
|
|
|
|
$this->userIds = $this->beneficiary();
|
|
|
|
$this->checkCouponTemplate();
|
|
|
|
$this->checkClaimRuleData();
|
|
|
|
Db::transaction(function () {
|
|
$this->dispenseId = $this->addMasterTableInformation();
|
|
|
|
$this->addSubTableInformation($this->dispenseId);
|
|
});
|
|
|
|
$this->sendAutoMq();
|
|
|
|
return $this->return->success();
|
|
}catch (Exception $e) {
|
|
throw new ErrException($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
private function sendAutoMq(): void
|
|
{
|
|
if ($this->claimRule != CouponCode::DISPENSE_CLAIM_RULE_POST_ACCOUNT) return;
|
|
|
|
if (empty($this->userIds)) return;
|
|
|
|
foreach ($this->userIds as $userId) {
|
|
//mq发放优惠券
|
|
$message = new CouponAutoDispenseProducer([
|
|
'coupon_dispense_id' => $this->dispenseId,
|
|
'user_id' => $userId,
|
|
]);
|
|
$this->producer->produce($message);
|
|
}
|
|
|
|
//todo 加一个缓存进度控制器 有没有必要
|
|
}
|
|
|
|
/**
|
|
* @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->itemCount,
|
|
'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 = $this->request->input('total_count',count($this->userIds) * $this->itemCount);
|
|
$insertModel->receive_count = 0;
|
|
$insertModel->item_count = $this->itemCount;
|
|
$insertModel->appoint_group = $this->groupType;
|
|
$insertModel->claim_rule = $this->claimRule;
|
|
$insertModel->claim_value = $this->request->input('claim_value');
|
|
$insertModel->appoint_value = !empty($this->appointValue) ? json_encode($this->appointValue) : null;
|
|
$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
|
|
{
|
|
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'));
|
|
$this->itemCount = (int)$this->request->input('item_count',1);
|
|
|
|
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 => []
|
|
};
|
|
}
|
|
|
|
} |