| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace addons\Course\common\services;
- use addons\Course\common\enums\CourseEnum;
- use addons\Course\common\logic\OrderRefuseLogic;
- use addons\Course\common\models\AddonsCourse;
- use addons\Course\common\models\AddonsCourseBought;
- use addons\Course\common\models\AddonsCourseOrders;
- use common\enums\StatusEnum;
- use common\models\common\CapitalLog;
- use common\models\goods\Goods;
- use common\models\order\Order;
- use common\models\user\User;
- use Exception;
- class CourseService
- {
- public $mall_id;
- public $user_id;
- public function __construct($config = [])
- {
- $this->mall_id = $config['mall_id'] ?? 0;
- $this->user_id = $config['user_id'] ?? 0;
- }
- //查找用户
- public function searchUser($params)
- {
- try {
- $where = [
- ['mall_id' => $this->mall_id],
- ['status' => StatusEnum::ENABLED]
- ];
- if (!empty($params['keyword'])) $where[] = ['or', ['like', 'id', $params['keyword']], ['like', 'nickname', $params['keyword']], ['like', 'mobile', $params['keyword']]];
- //获取用户信息
- $list = User::lists([
- 'where' => $where,
- 'order' => 'id desc',
- 'limit' => 20,
- 'select' => 'id,nickname,username,mall_id,level,mobile,avatar_url',
- ]);
- if (!empty($list)) {
- foreach ($list as &$item) {
- $item['user_id'] = $item['id'];
- unset($item['id']);
- }
- }
- return $list;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- //给用户发放课程
- public function giveUserCourse($params)
- {
- try {
- //生成订单
- $orderno = AddonsCourseOrders::generateOrderNo('C');
- //获取课程
- $course_info = AddonsCourse::getOne([
- ['mall_id' => $this->mall_id],
- ['id' => $params['id']],
- ['status' => StatusEnum::ENABLED]
- ], true);
- if (empty($course_info)) throw new Exception('查无此课程');
- //查找是否用户已用户该课程
- $user_course = AddonsCourseBought::getOne([
- ['mall_id' => $this->mall_id],
- ['user_id' => $params['user_id']],
- ['course_id' => $params['id']],
- ['is_pay' => 1],
- ['status' => StatusEnum::ENABLED],
- ]);
- if (!empty($user_course)) throw new Exception('该用户已拥有该课程');
- $params = [
- 'mall_id' => $this->mall_id,
- 'user_id' => $params['user_id'],
- 'course_id' => $params['id'],
- 'order_no' => $orderno,
- 'price' => 0,
- 'pay_money' => 0,
- 'remark' => '后台发放',
- 'pay_type' => 0,
- 'order_status' => 1,
- 'use_discount' => 0,
- 'discount_type' => 0,
- 'discount_quota' => 0,
- ];
- //记录订单
- $res = AddonsCourseOrders::setData($params);
- if (!$res) throw new \Exception(AddonsCourseOrders::$static_error);
- //添加课程购买记录
- $result = AddonsCourseBought::addBought($params, $res->id);
- if (!$result) throw new Exception(AddonsCourseBought::$static_error);
- return true;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- //获取课程订单分佣数据
- public function getCourseCommissionList($params)
- {
- try {
- $condition = [
- 'where' => [
- ['source_table' => [AddonsCourseOrders::tableName(), '%addons_course_orders']],
- ['source_table_id' => $params['order_id']],
- ['mall_id' => $this->mall_id],
- ],
- ];
- // $wallet_type = $params['wallet_type'];
- CapitalLog::$capitalType = $params['wallet_type'];
- $list = CapitalLog::getList($condition, 0);
- return $list;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- //课程订单主动退款
- public function doRefundSubmit($params)
- {
- try {
- //查看订单信息
- $order_info = AddonsCourseOrders::getOne([
- ['mall_id' => $this->mall_id],
- ['id' => $params['order_id']],
- ['refund_status' => [1, 2]],
- ]);
- if (!empty($order_info)) throw new Exception('该订单正在售后中,请勿重复操作');
- AddonsCourseOrders::updateAll(['refund_status' => 1, 'refund_desc' => $params['remark']], ['mall_id' => $this->mall_id, 'id' => $params['order_id']]);
- $params['mall_id'] = $this->mall_id;
- $res = (new OrderRefuseLogic())->refuseOrder($params);
- return true;
- } catch (\Exception $e) {
- throw new Exception($e->getMessage());
- }
- }
- }
|