diff --git a/app/Console/Tasks/PointGiftDeliverTask.php b/app/Console/Tasks/PointGiftDeliverTask.php
index ec93e357..b1553501 100644
--- a/app/Console/Tasks/PointGiftDeliverTask.php
+++ b/app/Console/Tasks/PointGiftDeliverTask.php
@@ -7,17 +7,16 @@
namespace App\Console\Tasks;
-use App\Models\CourseUser as CourseUserModel;
-use App\Models\ImGroupUser as ImGroupUserModel;
use App\Models\PointGift as PointGiftModel;
use App\Models\PointGiftRedeem as PointGiftRedeemModel;
use App\Models\Task as TaskModel;
use App\Repos\Course as CourseRepo;
-use App\Repos\CourseUser as CourseUserRepo;
-use App\Repos\ImGroup as ImGroupRepo;
-use App\Repos\ImGroupUser as ImGroupUserRepo;
use App\Repos\PointGift as PointGiftRepo;
use App\Repos\PointGiftRedeem as PointGiftRedeemRepo;
+use App\Repos\User as UserRepo;
+use App\Repos\Vip as VipRepo;
+use App\Services\Logic\Deliver\CourseDeliver as CourseDeliverService;
+use App\Services\Logic\Deliver\VipDeliver as VipDeliverService;
use App\Services\Logic\Notice\DingTalk\PointGiftRedeem as PointGiftRedeemNotice;
use App\Services\Logic\Point\History\PointGiftRefund as PointGiftRefundPointHistory;
use Phalcon\Mvc\Model\Resultset;
@@ -52,6 +51,9 @@ class PointGiftDeliverTask extends Task
case PointGiftModel::TYPE_COURSE:
$this->handleCourseRedeem($redeem);
break;
+ case PointGiftModel::TYPE_VIP:
+ $this->handleVipRedeem($redeem);
+ break;
case PointGiftModel::TYPE_GOODS:
$this->handleGoodsRedeem($redeem);
break;
@@ -112,54 +114,52 @@ class PointGiftDeliverTask extends Task
throw new \RuntimeException('Course Not Found');
}
- $groupRepo = new ImGroupRepo();
+ $redeem->status = PointGiftRedeemModel::STATUS_FINISHED;
- $group = $groupRepo->findByCourseId($course->id);
-
- if (!$group) {
- throw new \RuntimeException('Im Group Not Found');
+ if ($redeem->update() === false) {
+ throw new \RuntimeException('Update Point Redeem Status Failed');
}
- $courseUserRepo = new CourseUserRepo();
+ $userRepo = new UserRepo();
- $courseUser = $courseUserRepo->findCourseUser($course->id, $redeem->user_id);
+ $user = $userRepo->findById($redeem->user_id);
- if (!$courseUser) {
+ $deliverService = new CourseDeliverService();
- $courseUser = new CourseUserModel();
+ $deliverService->handle($course, $user);
+ }
- $courseUser->user_id = $redeem->user_id;
- $courseUser->course_id = $course->id;
- $courseUser->expiry_time = strtotime("+{$course->study_expiry} months");
- $courseUser->role_type = CourseUserModel::ROLE_STUDENT;
- $courseUser->source_type = CourseUserModel::SOURCE_POINT_REDEEM;
+ protected function handleVipRedeem(PointGiftRedeemModel $redeem)
+ {
+ $giftRepo = new PointGiftRepo();
- if ($courseUser->create() === false) {
- throw new \RuntimeException('Create Course User Failed');
- }
+ $gift = $giftRepo->findById($redeem->gift_id);
+
+ if (!$gift) {
+ throw new \RuntimeException('Gift Not Found');
}
- $groupUserRepo = new ImGroupUserRepo();
+ $vipRepo = new VipRepo();
- $groupUser = $groupUserRepo->findGroupUser($group->id, $redeem->user_id);
+ $vip = $vipRepo->findById($gift->attrs['id']);
- if (!$groupUser) {
-
- $groupUser = new ImGroupUserModel();
-
- $groupUser->group_id = $group->id;
- $groupUser->user_id = $redeem->user_id;
-
- if ($groupUser->create() === false) {
- throw new \RuntimeException('Create Group User Failed');
- }
+ if (!$vip) {
+ throw new \RuntimeException('Vip Not Found');
}
$redeem->status = PointGiftRedeemModel::STATUS_FINISHED;
if ($redeem->update() === false) {
- throw new \RuntimeException('Update Redeem Status Failed');
+ throw new \RuntimeException('Update Point Redeem Status Failed');
}
+
+ $userRepo = new UserRepo();
+
+ $user = $userRepo->findById($redeem->user_id);
+
+ $deliverService = new VipDeliverService();
+
+ $deliverService->handle($vip, $user);
}
protected function handleGoodsRedeem(PointGiftRedeemModel $redeem)
diff --git a/app/Http/Admin/Controllers/PointGiftController.php b/app/Http/Admin/Controllers/PointGiftController.php
index 433308a5..057834db 100644
--- a/app/Http/Admin/Controllers/PointGiftController.php
+++ b/app/Http/Admin/Controllers/PointGiftController.php
@@ -47,9 +47,11 @@ class PointGiftController extends Controller
$service = new PointGiftService();
$xmCourses = $service->getXmCourses();
+ $xmVips = $service->getXmVips();
$types = $service->getTypes();
$this->view->setVar('xm_courses', $xmCourses);
+ $this->view->setVar('xm_vips', $xmVips);
$this->view->setVar('types', $types);
}
diff --git a/app/Http/Admin/Services/PointGift.php b/app/Http/Admin/Services/PointGift.php
index 4ce0d39e..56487edf 100644
--- a/app/Http/Admin/Services/PointGift.php
+++ b/app/Http/Admin/Services/PointGift.php
@@ -11,6 +11,7 @@ use App\Library\Paginator\Query as PagerQuery;
use App\Models\PointGift as PointGiftModel;
use App\Repos\Course as CourseRepo;
use App\Repos\PointGift as PointGiftRepo;
+use App\Repos\Vip as VipRepo;
use App\Validators\PointGift as PointGiftValidator;
class PointGift extends Service
@@ -47,6 +48,29 @@ class PointGift extends Service
return $result;
}
+ public function getXmVips()
+ {
+ $vipRepo = new VipRepo();
+
+ $items = $vipRepo->findAll([
+ 'published' => 1,
+ 'deleted' => 0,
+ ]);
+
+ if ($items->count() == 0) return [];
+
+ $result = [];
+
+ foreach ($items as $item) {
+ $result[] = [
+ 'name' => sprintf('%s(¥%0.2f)', $item->title, $item->price),
+ 'value' => $item->id,
+ ];
+ }
+
+ return $result;
+ }
+
public function getPointGifts()
{
$pagerQuery = new PagerQuery();
@@ -83,6 +107,9 @@ class PointGift extends Service
case PointGiftModel::TYPE_COURSE:
$gift = $this->createCoursePointGift($post);
break;
+ case PointGiftModel::TYPE_VIP:
+ $gift = $this->createVipPointGift($post);
+ break;
case PointGiftModel::TYPE_GOODS:
$gift = $this->createGoodsPointGift($post);
break;
@@ -168,7 +195,7 @@ class PointGift extends Service
$giftRepo = new PointGiftRepo();
- $gift = $giftRepo->findByCourseId($course->id);
+ $gift = $giftRepo->findItemGift($course->id, PointGiftModel::TYPE_COURSE);
if ($gift) return $gift;
@@ -188,6 +215,35 @@ class PointGift extends Service
return $gift;
}
+
+ protected function createVipPointGift($post)
+ {
+ $validator = new PointGiftValidator();
+
+ $vip = $validator->checkVip($post['xm_vip_id']);
+
+ $giftRepo = new PointGiftRepo();
+
+ $gift = $giftRepo->findItemGift($vip->id, PointGiftModel::TYPE_VIP);
+
+ if ($gift) return $gift;
+
+ $gift = new PointGiftModel();
+
+ $gift->type = PointGiftModel::TYPE_VIP;
+ $gift->name = sprintf('会员服务(%s个月)', $vip->expiry);
+ $gift->cover = $vip->cover;
+ $gift->attrs = [
+ 'id' => $vip->id,
+ 'title' => $vip->title,
+ 'price' => $vip->price,
+ ];
+
+ $gift->create();
+
+ return $gift;
+ }
+
protected function createGoodsPointGift($post)
{
$validator = new PointGiftValidator();
diff --git a/app/Http/Admin/Views/macros/point_gift.volt b/app/Http/Admin/Views/macros/point_gift.volt
index 3b9216ec..9f6e695a 100644
--- a/app/Http/Admin/Views/macros/point_gift.volt
+++ b/app/Http/Admin/Views/macros/point_gift.volt
@@ -4,7 +4,7 @@
{% elseif value == 2 %}
商品
{% elseif value == 3 %}
- 现金
+ 会员
{% endif %}
{%- endmacro %}
diff --git a/app/Http/Admin/Views/point_gift/add.volt b/app/Http/Admin/Views/point_gift/add.volt
index bcd4d198..298f822d 100644
--- a/app/Http/Admin/Views/point_gift/add.volt
+++ b/app/Http/Admin/Views/point_gift/add.volt
@@ -22,6 +22,14 @@
+
+
+
{% if contact_info.enabled == 1 %}
diff --git a/app/Models/PointGift.php b/app/Models/PointGift.php
index 7efcce29..ae424730 100644
--- a/app/Models/PointGift.php
+++ b/app/Models/PointGift.php
@@ -19,6 +19,7 @@ class PointGift extends Model
*/
const TYPE_COURSE = 1; // 课程
const TYPE_GOODS = 2; // 商品
+ const TYPE_VIP = 3; // 会员
/**
* 课程扩展属性
@@ -31,6 +32,17 @@ class PointGift extends Model
'price' => 0,
];
+ /**
+ * 会员扩展属性
+ *
+ * @var array
+ */
+ protected $_vip_attrs = [
+ 'id' => 0,
+ 'title' => '',
+ 'price' => 0,
+ ];
+
/**
* 商品扩展属性
*
@@ -162,6 +174,8 @@ class PointGift extends Model
if (empty($this->attrs)) {
if ($this->type == self::TYPE_COURSE) {
$this->attrs = $this->_course_attrs;
+ } elseif ($this->type == self::TYPE_VIP) {
+ $this->attrs = $this->_vip_attrs;
} elseif ($this->type == self::TYPE_GOODS) {
$this->attrs = $this->_goods_attrs;
}
@@ -224,6 +238,7 @@ class PointGift extends Model
return [
self::TYPE_COURSE => '课程',
self::TYPE_GOODS => '商品',
+ self::TYPE_VIP => '会员',
];
}
diff --git a/app/Repos/CourseUser.php b/app/Repos/CourseUser.php
index 36b1c38f..3afa19bf 100644
--- a/app/Repos/CourseUser.php
+++ b/app/Repos/CourseUser.php
@@ -163,4 +163,17 @@ class CourseUser extends Repository
->execute();
}
+ /**
+ * @param int $courseId
+ * @param int $userId
+ * @return ResultsetInterface|Resultset|CourseUserModel[]
+ */
+ public function findByCourseAndUserId($courseId, $userId)
+ {
+ return CourseUserModel::query()
+ ->where('course_id = :course_id:', ['course_id' => $courseId])
+ ->andWhere('user_id = :user_id:', ['user_id' => $userId])
+ ->execute();
+ }
+
}
diff --git a/app/Repos/PointGift.php b/app/Repos/PointGift.php
index ba078a52..91669d01 100644
--- a/app/Repos/PointGift.php
+++ b/app/Repos/PointGift.php
@@ -77,10 +77,11 @@ class PointGift extends Repository
}
/**
- * @param int $courseId
+ * @param int $itemId
+ * @param int $itemType
* @return PointGiftModel|Model|bool
*/
- public function findByCourseId($courseId)
+ public function findItemGift($itemId, $itemType)
{
/**
* @todo 重新设计表结构
@@ -88,13 +89,13 @@ class PointGift extends Repository
* 没有预留独立的条目编号,先这么将就实现吧
*/
$records = PointGiftModel::query()
- ->where('type = :type:', ['type' => PointGiftModel::TYPE_COURSE])
+ ->where('type = :type:', ['type' => $itemType])
->execute();
if ($records->count() == 0) return false;
foreach ($records as $record) {
- if ($record->attrs['id'] == $courseId) {
+ if ($record->attrs['id'] == $itemId) {
return $record;
}
}
diff --git a/app/Services/Logic/Deliver/CourseDeliver.php b/app/Services/Logic/Deliver/CourseDeliver.php
index 4645a5c7..4259aba4 100644
--- a/app/Services/Logic/Deliver/CourseDeliver.php
+++ b/app/Services/Logic/Deliver/CourseDeliver.php
@@ -11,6 +11,7 @@ use App\Models\Course as CourseModel;
use App\Models\CourseUser as CourseUserModel;
use App\Models\ImGroupUser as ImGroupUserModel;
use App\Models\User as UserModel;
+use App\Repos\CourseUser as CourseUserRepo;
use App\Repos\ImGroup as ImGroupRepo;
use App\Repos\ImGroupUser as ImGroupUserRepo;
use App\Repos\ImUser as ImUserRepo;
@@ -20,6 +21,13 @@ class CourseDeliver extends LogicService
{
public function handle(CourseModel $course, UserModel $user)
+ {
+ $this->revokeCourseUser($course, $user);
+ $this->handleCourseUser($course, $user);
+ $this->handleImGroupUser($course, $user);
+ }
+
+ protected function handleCourseUser(CourseModel $course, UserModel $user)
{
if ($course->model == CourseModel::MODEL_OFFLINE) {
$expiryTime = strtotime($course->attrs['end_date']);
@@ -28,7 +36,6 @@ class CourseDeliver extends LogicService
}
$courseUser = new CourseUserModel();
-
$courseUser->user_id = $user->id;
$courseUser->course_id = $course->id;
$courseUser->expiry_time = $expiryTime;
@@ -39,6 +46,12 @@ class CourseDeliver extends LogicService
$course->user_count += 1;
$course->update();
+ $user->course_count += 1;
+ $user->update();
+ }
+
+ protected function handleImGroupUser(CourseModel $course, UserModel $user)
+ {
$groupRepo = new ImGroupRepo();
$group = $groupRepo->findByCourseId($course->id);
@@ -52,9 +65,7 @@ class CourseDeliver extends LogicService
$groupUser = $groupUserRepo->findGroupUser($group->id, $user->id);
if (!$groupUser) {
-
$groupUser = new ImGroupUserModel();
-
$groupUser->group_id = $group->id;
$groupUser->user_id = $user->id;
$groupUser->create();
@@ -67,4 +78,20 @@ class CourseDeliver extends LogicService
}
}
+ protected function revokeCourseUser(CourseModel $course, UserModel $user)
+ {
+ $courseUserRepo = new CourseUserRepo();
+
+ $relations = $courseUserRepo->findByCourseAndUserId($course->id, $user->id);
+
+ if ($relations->count() == 0) return;
+
+ foreach ($relations as $relation) {
+ if ($relation->deleted == 0) {
+ $relation->deleted = 1;
+ $relation->update();
+ }
+ }
+ }
+
}
diff --git a/app/Services/Logic/Deliver/PackageDeliver.php b/app/Services/Logic/Deliver/PackageDeliver.php
index 36586f7a..709fdc83 100644
--- a/app/Services/Logic/Deliver/PackageDeliver.php
+++ b/app/Services/Logic/Deliver/PackageDeliver.php
@@ -7,13 +7,8 @@
namespace App\Services\Logic\Deliver;
-use App\Models\CourseUser as CourseUserModel;
-use App\Models\ImGroupUser as ImGroupUserModel;
use App\Models\Package as PackageModel;
use App\Models\User as UserModel;
-use App\Repos\ImGroup as ImGroupRepo;
-use App\Repos\ImGroupUser as ImGroupUserRepo;
-use App\Repos\ImUser as ImUserRepo;
use App\Repos\Package as PackageRepo;
use App\Services\Logic\Service as LogicService;
@@ -27,43 +22,8 @@ class PackageDeliver extends LogicService
$courses = $packageRepo->findCourses($package->id);
foreach ($courses as $course) {
-
- $courseUser = new CourseUserModel();
-
- $courseUser->user_id = $user->id;
- $courseUser->course_id = $course->id;
- $courseUser->expiry_time = strtotime("+{$course->study_expiry} months");
- $courseUser->role_type = CourseUserModel::ROLE_STUDENT;
- $courseUser->source_type = CourseUserModel::SOURCE_CHARGE;
- $courseUser->create();
-
- $course->user_count += 1;
- $course->update();
-
- $imUserRepo = new ImUserRepo();
-
- $imUser = $imUserRepo->findById($user->id);
-
- $groupRepo = new ImGroupRepo();
-
- $group = $groupRepo->findByCourseId($course->id);
-
- $groupUserRepo = new ImGroupUserRepo();
-
- $groupUser = $groupUserRepo->findGroupUser($group->id, $user->id);
-
- if (!$groupUser) {
- $groupUser = new ImGroupUserModel();
- $groupUser->group_id = $group->id;
- $groupUser->user_id = $user->id;
- $groupUser->create();
-
- $imUser->group_count += 1;
- $imUser->update();
-
- $group->user_count += 1;
- $group->update();
- }
+ $deliver = new CourseDeliver();
+ $deliver->handle($course, $user);
}
}
diff --git a/app/Validators/PointGift.php b/app/Validators/PointGift.php
index 2a296437..5c1460ca 100644
--- a/app/Validators/PointGift.php
+++ b/app/Validators/PointGift.php
@@ -177,4 +177,11 @@ class PointGift extends Validator
return $validator->checkCourse($id);
}
+ public function checkVip($id)
+ {
+ $validator = new Vip();
+
+ return $validator->checkVip($id);
+ }
+
}
diff --git a/db/migrations/20220607014823.php b/db/migrations/20220607014823.php
new file mode 100644
index 00000000..a1f74762
--- /dev/null
+++ b/db/migrations/20220607014823.php
@@ -0,0 +1,42 @@
+handleSiteSettings();
+ }
+
+ protected function handleSiteSettings()
+ {
+ $row =
+ [
+ [
+ 'section' => 'site',
+ 'item_key' => 'isp_sn',
+ 'item_value' => '',
+ ],
+ [
+ 'section' => 'site',
+ 'item_key' => 'isp_link',
+ 'item_value' => 'https://dxzhgl.miit.gov.cn',
+ ],
+ [
+ 'section' => 'site',
+ 'item_key' => 'company_sn',
+ 'item_value' => '',
+ ],
+ [
+ 'section' => 'site',
+ 'item_key' => 'company_sn_link',
+ 'item_value' => '',
+ ],
+ ];
+
+ $this->table('kg_setting')->insert($row)->save();
+ }
+
+}
\ No newline at end of file