CourseService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace addons\Course\common\services;
  3. use addons\Course\common\enums\CourseEnum;
  4. use addons\Course\common\logic\OrderRefuseLogic;
  5. use addons\Course\common\models\AddonsCourse;
  6. use addons\Course\common\models\AddonsCourseBought;
  7. use addons\Course\common\models\AddonsCourseOrders;
  8. use common\enums\StatusEnum;
  9. use common\models\common\CapitalLog;
  10. use common\models\goods\Goods;
  11. use common\models\order\Order;
  12. use common\models\user\User;
  13. use Exception;
  14. class CourseService
  15. {
  16. public $mall_id;
  17. public $user_id;
  18. public function __construct($config = [])
  19. {
  20. $this->mall_id = $config['mall_id'] ?? 0;
  21. $this->user_id = $config['user_id'] ?? 0;
  22. }
  23. //查找用户
  24. public function searchUser($params)
  25. {
  26. try {
  27. $where = [
  28. ['mall_id' => $this->mall_id],
  29. ['status' => StatusEnum::ENABLED]
  30. ];
  31. if (!empty($params['keyword'])) $where[] = ['or', ['like', 'id', $params['keyword']], ['like', 'nickname', $params['keyword']], ['like', 'mobile', $params['keyword']]];
  32. //获取用户信息
  33. $list = User::lists([
  34. 'where' => $where,
  35. 'order' => 'id desc',
  36. 'limit' => 20,
  37. 'select' => 'id,nickname,username,mall_id,level,mobile,avatar_url',
  38. ]);
  39. if (!empty($list)) {
  40. foreach ($list as &$item) {
  41. $item['user_id'] = $item['id'];
  42. unset($item['id']);
  43. }
  44. }
  45. return $list;
  46. } catch (\Exception $e) {
  47. throw new Exception($e->getMessage());
  48. }
  49. }
  50. //给用户发放课程
  51. public function giveUserCourse($params)
  52. {
  53. try {
  54. //生成订单
  55. $orderno = AddonsCourseOrders::generateOrderNo('C');
  56. //获取课程
  57. $course_info = AddonsCourse::getOne([
  58. ['mall_id' => $this->mall_id],
  59. ['id' => $params['id']],
  60. ['status' => StatusEnum::ENABLED]
  61. ], true);
  62. if (empty($course_info)) throw new Exception('查无此课程');
  63. //查找是否用户已用户该课程
  64. $user_course = AddonsCourseBought::getOne([
  65. ['mall_id' => $this->mall_id],
  66. ['user_id' => $params['user_id']],
  67. ['course_id' => $params['id']],
  68. ['is_pay' => 1],
  69. ['status' => StatusEnum::ENABLED],
  70. ]);
  71. if (!empty($user_course)) throw new Exception('该用户已拥有该课程');
  72. $params = [
  73. 'mall_id' => $this->mall_id,
  74. 'user_id' => $params['user_id'],
  75. 'course_id' => $params['id'],
  76. 'order_no' => $orderno,
  77. 'price' => 0,
  78. 'pay_money' => 0,
  79. 'remark' => '后台发放',
  80. 'pay_type' => 0,
  81. 'order_status' => 1,
  82. 'use_discount' => 0,
  83. 'discount_type' => 0,
  84. 'discount_quota' => 0,
  85. ];
  86. //记录订单
  87. $res = AddonsCourseOrders::setData($params);
  88. if (!$res) throw new \Exception(AddonsCourseOrders::$static_error);
  89. //添加课程购买记录
  90. $result = AddonsCourseBought::addBought($params, $res->id);
  91. if (!$result) throw new Exception(AddonsCourseBought::$static_error);
  92. return true;
  93. } catch (\Exception $e) {
  94. throw new Exception($e->getMessage());
  95. }
  96. }
  97. //获取课程订单分佣数据
  98. public function getCourseCommissionList($params)
  99. {
  100. try {
  101. $condition = [
  102. 'where' => [
  103. ['source_table' => [AddonsCourseOrders::tableName(), '%addons_course_orders']],
  104. ['source_table_id' => $params['order_id']],
  105. ['mall_id' => $this->mall_id],
  106. ],
  107. ];
  108. // $wallet_type = $params['wallet_type'];
  109. CapitalLog::$capitalType = $params['wallet_type'];
  110. $list = CapitalLog::getList($condition, 0);
  111. return $list;
  112. } catch (\Exception $e) {
  113. throw new Exception($e->getMessage());
  114. }
  115. }
  116. //课程订单主动退款
  117. public function doRefundSubmit($params)
  118. {
  119. try {
  120. //查看订单信息
  121. $order_info = AddonsCourseOrders::getOne([
  122. ['mall_id' => $this->mall_id],
  123. ['id' => $params['order_id']],
  124. ['refund_status' => [1, 2]],
  125. ]);
  126. if (!empty($order_info)) throw new Exception('该订单正在售后中,请勿重复操作');
  127. AddonsCourseOrders::updateAll(['refund_status' => 1, 'refund_desc' => $params['remark']], ['mall_id' => $this->mall_id, 'id' => $params['order_id']]);
  128. $params['mall_id'] = $this->mall_id;
  129. $res = (new OrderRefuseLogic())->refuseOrder($params);
  130. return true;
  131. } catch (\Exception $e) {
  132. throw new Exception($e->getMessage());
  133. }
  134. }
  135. }