VirtualOrderController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace addons\Rebate\backend\controllers;
  3. use addons\Rebate\common\logic\RewardLogic;
  4. use addons\Rebate\common\models\Rebate;
  5. use addons\Rebate\common\models\RebateBuy;
  6. use addons\Rebate\common\models\RebateVirtualOrderLog;
  7. use common\models\base\User;
  8. use Yii;
  9. /**
  10. * 默认控制器
  11. *
  12. * Class DefaultController
  13. *
  14. * @package addons\Rebate\backend\controllers
  15. */
  16. class VirtualOrderController extends BaseController
  17. {
  18. public $enableCsrfValidation = false;
  19. /**
  20. * 首页
  21. *
  22. * @return string
  23. */
  24. public function actionIndex()
  25. {
  26. if (Yii::$app->request->isAjax) {
  27. if (Yii::$app->request->isGet) {
  28. $params = Yii::$app->request->get();
  29. $time = time();
  30. $list = $where = [];
  31. $where[] = ['=', 'mall_id', $this->mall_id];
  32. if (isset($params['p_id']) && $params['p_id'] !== '') {
  33. $where[] = ['=', 'user_parent_id', $params['p_id']];
  34. }
  35. if (isset($params['id']) && $params['id'] !== '') {
  36. $where[] = ['=', 'user_id', $params['id']];
  37. }
  38. if (isset($params['mobile']) && $params['mobile'] !== '') {
  39. $userId = User::find()
  40. ->where([
  41. 'mobile' => $params['mobile'],
  42. 'mall_id' => $this->mall_id
  43. ])
  44. ->select('id')
  45. ->scalar();
  46. if (empty($userId)) {
  47. $this->success('获取成功', ['list' => $list, 'now_time' => $time]);
  48. }
  49. $where[] = ['=', 'user_id', $userId];
  50. }
  51. if (isset($params['p_mobile']) && $params['p_mobile'] !== '') {
  52. $userId = User::find()
  53. ->where([
  54. 'mobile' => $params['p_mobile'],
  55. 'mall_id' => $this->mall_id
  56. ])
  57. ->select('id')
  58. ->scalar();
  59. if (empty($userId)) {
  60. $this->success('获取成功', ['list' => $list, 'now_time' => $time]);
  61. }
  62. $where[] = ['=', 'user_parent_id', $userId];
  63. }
  64. if (isset($params['rebate_title']) && $params['rebate_title'] !== '') {
  65. $rebateId = Rebate::find()
  66. ->where([
  67. 'title' => $params['rebate_title'],
  68. 'mall_id' => $this->mall_id
  69. ])
  70. ->select('id')
  71. ->column();
  72. if (empty($rebateId)) {
  73. $this->success('获取成功', ['list' => $list, 'now_time' => $time]);
  74. }
  75. $where[] = ['in', 'rebate_id', $rebateId];
  76. }
  77. if (isset($params['order_no']) && $params['order_no'] !== '') {
  78. $rebateBuyId = RebateBuy::find()
  79. ->where([
  80. 'order_no' => $params['order_no'],
  81. 'mall_id' => $this->mall_id
  82. ])
  83. ->select('id')
  84. ->scalar();
  85. if (empty($rebateBuyId)) {
  86. $this->success('获取成功', ['list' => $list, 'now_time' => $time]);
  87. }
  88. $where[] = ['=', 'rebate_buy_id', $rebateBuyId];
  89. }
  90. $order = 'id desc';
  91. $select = 'id, user_id, user_parent_id, mall_id, rebate_id, rebate_buy_id, created_at';
  92. $with = [
  93. 'user' => function ($query) {
  94. $query->select('id, nickname, avatar_url, mobile');
  95. },
  96. 'parentUser' => function ($query) {
  97. $query->select('id, nickname, avatar_url, mobile');
  98. },
  99. 'rebate' => function ($query) {
  100. $query->select('id, title');
  101. },
  102. 'rebateBuy' => function ($query) {
  103. $query->select('id, order_no');
  104. }
  105. ];
  106. $params = compact('where', 'order', 'select', 'with');
  107. $list = RebateVirtualOrderLog::listPage($params, false, true);
  108. $this->success('获取成功', ['list' => $list, 'now_time' => $time]);
  109. }
  110. } else {
  111. return $this->render($this->action->id);
  112. }
  113. }
  114. /**
  115. * 创建虚拟订单
  116. *
  117. * @return mixed|string|null
  118. */
  119. public function actionAdd()
  120. {
  121. if (Yii::$app->request->isAjax) {
  122. if (Yii::$app->request->isPost) {
  123. $params = Yii::$app->request->post();
  124. $res = Yii::$app->services->validate->validate($params, [
  125. [['user_id', 'rebate_id'], 'required'],
  126. [['user_id', 'rebate_id'], 'integer'],
  127. ]);
  128. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  129. if (!RewardLogic::createVirtualOrder($this->mall_id, $params['user_id'], $params['rebate_id'])) {
  130. return $this->error(RewardLogic::getStaticError());
  131. }
  132. return $this->success("保存成功");
  133. }
  134. }
  135. return $this->render($this->action->id);
  136. }
  137. }