From cec39ea72a6cdf1ddc5416b91c007fd42e9333f6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= <zoujingli@qq.com>
Date: Wed, 10 Mar 2021 11:07:55 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 app/data/controller/UserLevel.php             |  6 +-
 ...zeService.php => RebateCurrentService.php} |  8 +--
 app/data/service/RebateMonthService.php       | 45 +++++++++++++++
 app/data/service/RebateQuarterService.php     | 56 +++++++++++++++++++
 app/data/service/RebateYearService.php        | 50 +++++++++++++++++
 app/data/sys.php                              |  8 ++-
 vendor/services.php                           |  2 +-
 7 files changed, 166 insertions(+), 9 deletions(-)
 rename app/data/service/{PrizeService.php => RebateCurrentService.php} (98%)
 create mode 100644 app/data/service/RebateMonthService.php
 create mode 100644 app/data/service/RebateQuarterService.php
 create mode 100644 app/data/service/RebateYearService.php

diff --git a/app/data/controller/UserLevel.php b/app/data/controller/UserLevel.php
index 5af467c30..717a317ae 100644
--- a/app/data/controller/UserLevel.php
+++ b/app/data/controller/UserLevel.php
@@ -2,7 +2,7 @@
 
 namespace app\data\controller;
 
-use app\data\service\PrizeService;
+use app\data\service\RebateCurrentService;
 use think\admin\Controller;
 
 /**
@@ -41,7 +41,7 @@ class UserLevel extends Controller
         foreach ($data as &$vo) {
             $vo['rebate_rule'] = str2arr($vo['rebate_rule']);
             foreach ($vo['rebate_rule'] as &$v) {
-                $v = PrizeService::instance()->name($v);
+                $v = RebateCurrentService::instance()->name($v);
             }
         }
     }
@@ -77,7 +77,7 @@ class UserLevel extends Controller
     protected function _form_filter(array &$vo)
     {
         if ($this->request->isGet()) {
-            $this->prizes = PrizeService::PRIZES;
+            $this->prizes = RebateCurrentService::PRIZES;
             $vo['rebate_rule'] = str2arr($vo['rebate_rule'] ?? '');
         } else {
             $vo['utime'] = time();
diff --git a/app/data/service/PrizeService.php b/app/data/service/RebateCurrentService.php
similarity index 98%
rename from app/data/service/PrizeService.php
rename to app/data/service/RebateCurrentService.php
index a3c3fda8f..571d84d88 100644
--- a/app/data/service/PrizeService.php
+++ b/app/data/service/RebateCurrentService.php
@@ -6,11 +6,11 @@ use think\admin\Service;
 use think\admin\Exception;
 
 /**
- * 用户奖励配置
- * Class PrizeService
+ * 实时返利服务
+ * Class RebateCurrentService
  * @package app\data\service
  */
-class PrizeService extends Service
+class RebateCurrentService extends Service
 {
     const PRIZE_01 = 'prize_01';
     const PRIZE_02 = 'prize_02';
@@ -75,7 +75,7 @@ class PrizeService extends Service
         $this->order = $this->app->db->name('ShopOrder')->where($map)->find();
         if (empty($this->order)) throw new Exception('订单不存在');
         // 获取用户数据
-        $map = ['id' => $this->order['uid']];
+        $map = ['id' => $this->order['uid'], 'deleted' => 0];
         $this->user = $this->app->db->name('DataUser')->where($map)->find();
         if (empty($this->user)) throw new Exception('用户不存在');
         // 获取推荐用户
diff --git a/app/data/service/RebateMonthService.php b/app/data/service/RebateMonthService.php
new file mode 100644
index 000000000..6c5638920
--- /dev/null
+++ b/app/data/service/RebateMonthService.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace app\data\service;
+
+use think\admin\Exception;
+use think\admin\Service;
+
+/**
+ * 月度返利服务
+ * Class RebateMonthService
+ * @package app\agent\service
+ */
+class RebateMonthService extends Service
+{
+    /** @var string */
+    protected $date;
+
+    /** @var array */
+    protected $user;
+
+    /**
+     * 绑定数据表
+     * @var string
+     */
+    private $table = 'DataUserRebate';
+
+    /**
+     * 指定用户发放奖励
+     * @param mixed $uid
+     * @param array $user
+     * @throws Exception
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function execute($uid, array $user)
+    {
+        if (empty($user)) $user = $this->app->db->name('DataUser')->where(['id' => $uid])->find();
+        if (empty($user)) throw new Exception("指定用户[{$uid}]不存在");
+        $this->app->log->notice("开始处理用户[{$user['id']}]月度[{$this->date}]返利");
+        $this->user = $user;
+        // $this->_prize_02();
+    }
+
+}
\ No newline at end of file
diff --git a/app/data/service/RebateQuarterService.php b/app/data/service/RebateQuarterService.php
new file mode 100644
index 000000000..d19193ac3
--- /dev/null
+++ b/app/data/service/RebateQuarterService.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace app\data\service;
+
+use think\admin\Exception;
+use think\admin\Service;
+
+/**
+ * 季度返利服务
+ * Class RebateQuarterService
+ * @package app\data\service
+ */
+class RebateQuarterService extends Service
+{
+    /** @var string */
+    protected $quarter;
+
+    /** @var string */
+    protected $dateAfter;
+
+    /** @var string */
+    protected $dateStart;
+
+    /**
+     * 发放季度奖励
+     * @param string $year 年份
+     * @param string $month 月份
+     * @return RebateQuarterService
+     */
+    public function build(string $year = '', string $month = ''): RebateQuarterService
+    {
+        $year = $year ?: date('Y', '-3 month');
+        $month = $month ?: date('m', '-3 month');
+        $this->quarter = ceil(date('n', mktime(0, 0, 0, $month, 1, $year)) / 3);
+        $this->dateAfter = date('Y-m-t 23:59:59', mktime(0, 0, 0, ($this->quarter - 1) * 3 + 0, 1, date('Y')));
+        $this->dateStart = date('Y-m-01 00:00:00', mktime(0, 0, 0, ($this->quarter - 2) * 3 + 1, 1, date('Y')));
+        return $this;
+    }
+
+    /**
+     * 指定用户发放奖励
+     * @param mixed $uid
+     * @param array $user
+     * @throws Exception
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function execute($uid, $user = [])
+    {
+        if (empty($user)) $user = $this->app->db->name('DataUser')->where(['id' => $uid])->find();
+        if (empty($user)) throw new Exception("指定用户[{$uid}]不存在");
+        $this->app->log->notice("开始处理用户[{$user['id']}]季度[{$this->dateStart} - {$this->dateAfter}]返利");
+        // $this->_prize_05($user);
+    }
+}
\ No newline at end of file
diff --git a/app/data/service/RebateYearService.php b/app/data/service/RebateYearService.php
new file mode 100644
index 000000000..c47c31cd8
--- /dev/null
+++ b/app/data/service/RebateYearService.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace app\data\service;
+
+use think\admin\Exception;
+use think\admin\Service;
+
+/**
+ * 年度返利服务
+ * Class RebateYearService
+ * @package app\data\service
+ */
+class RebateYearService extends Service
+{
+    /** @var array */
+    protected $user;
+
+    /** @var string */
+    protected $year;
+
+    /**
+     * 发放年度奖励
+     * @param string $year 年份
+     * @return RebateYearService
+     */
+    public function build(string $year = ''): RebateYearService
+    {
+        $this->year = $year ?: date('Y', strtotime('-1 year'));
+        return $this;
+    }
+
+    /**
+     * 指定用户发放奖励
+     * @param mixed $uid
+     * @param array $user
+     * @throws Exception
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function execute($uid, $user = [])
+    {
+        if (empty($user)) $user = $this->app->db->name('DataUser')->where(['id' => $uid])->find();
+        if (empty($user)) throw new Exception("指定用户[{$uid}]不存在");
+        $this->app->log->notice("开始处理用户[{$user['id']}]年度[{$this->year}]返利");
+        $this->user = $user;
+        // $this->_prize_06();
+    }
+
+}
\ No newline at end of file
diff --git a/app/data/sys.php b/app/data/sys.php
index dd0220bc8..d1fd22ed9 100644
--- a/app/data/sys.php
+++ b/app/data/sys.php
@@ -8,11 +8,17 @@ use think\Console;
 
 if (app()->request->isCli()) {
     Console::starting(function (Console $console) {
+        $console->addCommand(UserLevel::class);
         $console->addCommand(OrderClear::class);
         $console->addCommand(UserBalance::class);
-        $console->addCommand(UserLevel::class);
         $console->addCommand(UserTransfer::class);
     });
+} else {
+    // 注册订单支付处理事件
+    app()->event->listen('ShopOrderPayment', function ($orderNo) {
+        app()->log->notice("订单支付事件,订单号:{$orderNo}");
+        \app\data\service\RebateCurrentService::instance()->execute($orderNo);
+    });
 }
 
 if (!function_exists('show_goods_spec')) {
diff --git a/vendor/services.php b/vendor/services.php
index 73a7da04b..56250ed8a 100644
--- a/vendor/services.php
+++ b/vendor/services.php
@@ -1,5 +1,5 @@
 <?php 
-// This file is automatically generated at:2021-03-09 18:14:18
+// This file is automatically generated at:2021-03-10 10:48:21
 declare (strict_types = 1);
 return array (
   0 => 'think\\admin\\Library',