Skip to content

Commit

Permalink
修改 自动扣款逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
liufei-ereach committed Jul 4, 2024
1 parent 41d66b5 commit 5f37fc0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 32 deletions.
4 changes: 4 additions & 0 deletions locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ mattoid-store:
buy-goods-fail: Purchase failed [{title}]
file-exist: file already exist
automatic-renewal: Automatic renewal not enabled
invalid-product: Invalid product
automatic-renewal-fail: Automatic renewal failed [{message}]
goods-invalid-fail: Product failure logic execution failed [{message}]
goods-invalid-event-fail: Product failure event push failed [{message}]

lib:
item-status-all: All product statuses
Expand Down
4 changes: 4 additions & 0 deletions locale/zh-Hans.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ mattoid-store:
buy-goods-fail: 购买【{title}】失败
file-exist: 文件已存在
automatic-renewal: 未开启自动续费
invalid-product: 无效商品
automatic-renewal-fail: 自动续费失败【{message}】
goods-invalid-fail: 商品失效逻辑执行失败【{message}】
goods-invalid-event-fail: 商品失效事件推送失败【{message}】

lib:
item-status-all: 全部商品状态
Expand Down
32 changes: 17 additions & 15 deletions src/Console/Command/GoodsInvalidCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Carbon\Carbon;
use Flarum\Console\AbstractCommand;
use Flarum\Foundation\ValidationException;
use Flarum\Locale\Translator;
use Symfony\Contracts\Translation\TranslatorInterface;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
Expand All @@ -20,31 +20,30 @@ class GoodsInvalidCommand extends AbstractCommand
protected $events;
protected $settings;

public function __construct() {
public function __construct(SettingsRepositoryInterface $settings, TranslatorInterface $translator, Repository $cache, Dispatcher $events) {
parent::__construct();
$this->cache = resolve(Repository::class);
$this->events = resolve(Dispatcher::class);
$this->translator = resolve(Translator::class);
$this->settings = resolve(SettingsRepositoryInterface::class);
$this->cache = $cache;
$this->events = $events;
$this->settings = $settings;
$this->translator = $translator;
}

protected function configure()
{
$this->setName('mattoid:store:check:date')->setDescription('Check the expiration time of the goods');
$this->setName('mattoid:store:check:date')->setDescription('Check the expiration time of the product');
}

protected function fire()
{
$storeMap = [];
$dateTime = Carbon::now()->tz($this->settings->get($this->settings->get('mattoid-store.storeTimezone', 'Asia/Shanghai')));
$dateTime = Carbon::now()->tz($this->settings->get('mattoid-store.storeTimezone', 'Asia/Shanghai'));
$invalidList = StoreCartModel::query()->where('outtime', '<=', $dateTime)->where('type', 'limit')->where('status', 1)->get();

if (!$invalidList) {
// 未发现失效商品,跳过处理
return;
}

$storeIdList = array_column(json_decode($invalidList, true), 'id');
$storeIdList = array_column(json_decode($invalidList, true), 'store_id');
$storeList = StoreModel::query()->whereIn('id', $storeIdList)->get();
foreach ($storeList as $store) {
$storeMap[$store->id] = $store;
Expand All @@ -59,7 +58,7 @@ protected function fire()
$this->autoDeduction($store, $cart);
} catch (\Exception $e) {
$buyStatus = false;
$this->error("[{$cart->code}]-{$cart->id}-{$cart->store_id}: Automatic fee deduction failed【{$e->getMessage()}");
$this->error("[{$cart->code}] {$cart->store_id}:{$cart->id}: {$this->translator->trans('mattoid-store.forum.error.automatic-renewal-fail', ['message' => $e->getMessage()])}");
try {
// 超期商品自动失效
$invalid = StoreExtend::getInvalid($cart->code);
Expand All @@ -70,22 +69,22 @@ protected function fire()
$cart->status = 2;
$cart->save();
} catch (\Exception $e) {
$this->error("[{$cart->code}]-{$cart->id}-{$cart->store_id}: Failed to execute product invalidation logic【{$e->getMessage()}");
$this->error("[{$cart->code}] {$cart->store_id}:{$cart->id}: {$this->translator->trans('mattoid-store.forum.error.goods-invalid-fail', ['message' => $e->getMessage()])}");
}
}

try {
$this->events->dispatch(new StoreInvalidEvent($store, $cart, $buyStatus));
} catch (\Exception $e) {
$this->error("[{$cart->code}]-{$cart->id}-{$cart->store_id}: Failed to notify product expiration event【{$e->getMessage()}");
$this->error("[{$cart->code}] {$cart->store_id}:{$cart->id}: {$this->translator->trans('mattoid-store.forum.error.goods-invalid-event-fail', ['message' => $e->getMessage()])}");
}
}
}

private function autoDeduction(StoreModel $store, StoreCartModel $cart)
{
// 商品未开启自动扣费
if ($cart->auto_deduction) {
if (!$cart->auto_deduction) {
throw new ValidationException(['message' => $this->translator->trans('mattoid-store.forum.error.automatic-renewal')]);
}

Expand All @@ -94,7 +93,7 @@ private function autoDeduction(StoreModel $store, StoreCartModel $cart)
throw new ValidationException(['message' => $this->translator->trans('mattoid-store.forum.error.invalid-product')]);
}

$key = md5("{$store->store_id}-{$cart->user_id}");
$key = md5("{$cart->store_id}-{$cart->user_id}");
if (!$this->cache->add($key, time(), 5)) {
throw new ValidationException(['message' => $this->translator->trans('mattoid-store.forum.error.validate-fail')]);
}
Expand All @@ -114,6 +113,7 @@ private function autoDeduction(StoreModel $store, StoreCartModel $cart)
}

// 刷新过期时间
$cart->pay_amt = $cart->price;
$cart->outtime = Carbon::now()->tz($this->settings->get('mattoid-store.storeTimezone', 'Asia/Shanghai'))->addDays($store->outtime);
$cart->created_at = Carbon::now()->tz($this->settings->get('mattoid-store.storeTimezone', 'Asia/Shanghai'));
$cart->save();
Expand All @@ -122,5 +122,7 @@ private function autoDeduction(StoreModel $store, StoreCartModel $cart)
if (class_exists('Mattoid\MoneyHistory\Event\MoneyHistoryEvent')) {
$this->events->dispatch(new \Mattoid\MoneyHistory\Event\MoneyHistoryEvent($user, -$cart->price, 'AUTODEDUCTION', $this->translator->trans("mattoid-store.forum.auto-deduction", ['title' => $store->title]), ''));
}

$this->cache->delete($key);
}
}
34 changes: 17 additions & 17 deletions src/Extend/StoreExtend.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class StoreExtend implements ExtenderInterface, LifecycleInterface
private static $goodList = [];
private static $afterList = [];
private static $validateList = [];
private static $invalidList = [];
public static $invalidList = [];


public function __construct(string $key = '')
Expand Down Expand Up @@ -55,38 +55,38 @@ public function addInvalid($callback): self

public static function getStoreGoods(String $key)
{
$class = StoreExtend::$goodList[$key];
if (!class_exists($class)) {
return null;
if (array_key_exists($key, StoreExtend::$goodList)) {
$class = StoreExtend::$goodList[$key];
return new $class;
}
return new $class;
return null;
}

public static function getValidate(String $key)
{
$class = StoreExtend::$validateList[$key];
if (!class_exists($class)) {
return null;
if (array_key_exists($key, StoreExtend::$validateList)) {
$class = StoreExtend::$validateList[$key];
return new $class;
}
return new $class;
return null;
}

public static function getAfter(String $key)
{
$class = StoreExtend::$afterList[$key];
if (!class_exists($class)) {
return null;
if (array_key_exists($key, StoreExtend::$afterList)) {
$class = StoreExtend::$afterList[$key];
return new $class;
}
return new $class;
return null;
}

public static function getInvalid(string $key)
{
$class = StoreExtend::$invalidList[$key];
if (!class_exists($class)) {
return null;
if (array_key_exists($key, StoreExtend::$invalidList)) {
$class = StoreExtend::$invalidList[$key];
return new $class;
}
return new $class;
return null;
}

public function extend(Container $container, Extension $extension = null)
Expand Down

0 comments on commit 5f37fc0

Please sign in to comment.