IndexController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace addons\RandomRedPacket\backend\controllers;
  3. use addons\RandomRedPacket\common\models\RandomRedPacket;
  4. use addons\RandomRedPacket\common\models\RandomRedPacketRelation;
  5. use addons\Rebate\backend\controllers\BaseController;
  6. use common\enums\StatusEnum;
  7. use common\models\goods\Goods;
  8. use Yii;
  9. use common\controllers\AddonsController;
  10. use yii\helpers\Json;
  11. class IndexController extends BaseController
  12. {
  13. public $enableCsrfValidation = false;
  14. /**
  15. * 活动列表
  16. * @Author: wu
  17. * @DateTime: 2023/2/14 0015 18:06
  18. * @Copyright: copyright (c) 2023 广东七件事集团
  19. * @return string|\yii\web\Response
  20. */
  21. public function actionIndex()
  22. {
  23. if (Yii::$app->request->isAjax) {
  24. if (Yii::$app->request->isPost) {
  25. $params = Yii::$app->request->post();
  26. // 检查提交的参数
  27. $res = Yii::$app->services->validate->validate($get,[
  28. [['activities_number', 'reward_type'], 'integer'],
  29. [['activities_name'], 'string'],
  30. ]);
  31. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  32. $time = time();
  33. $where = [];
  34. $where[] = ['=', 'mall_id', $this->mall_id];
  35. $where[] = ['<>', 'status', StatusEnum::DELETE];
  36. !empty($params['activities_number']) && $where[] = ['=', 'activities_number', $params['activities_number']];
  37. !empty($params['activities_name']) && $where[] = ['like', 'activities_name', $params['activities_name']];
  38. !empty($params['reward_type']) && $where[] = ['=', 'reward_type', $params['reward_type']];
  39. if(!empty($params['status'])) {// 状态条件筛选
  40. switch($params['status']) {
  41. case 1:// 进行中
  42. $where[] = ['<', 'start_at', $time];
  43. $where[] = ['>', 'end_at', $time];
  44. $where[] = ['=', 'status', StatusEnum::ENABLED];
  45. break;
  46. case 2:// 已过期
  47. $where[] = ['<', 'end_at', $time];
  48. break;
  49. case 3:// 已失效
  50. $where[] = ['=', 'status', StatusEnum::DISABLED];
  51. break;
  52. case 4:// 未开始
  53. $where[] = ['>', 'start_at', $time];
  54. break;
  55. }
  56. }
  57. // $with = ['statistics' => function($query){
  58. // $query->select("r_id,join_num,pay_num,pay_money");
  59. // }];
  60. $order = 'created_at desc';
  61. $select = "id,activities_name,activities_number,reward_type,get_number,created_at,status,start_at,end_at";
  62. $params = compact('where','order','select');
  63. $list = RandomRedPacket::listPage($params, false, true);
  64. $this->success('获取成功', ['list' => $list, 'now_time' => $time]);
  65. }
  66. } else {
  67. return $this->render($this->action->id);
  68. }
  69. }
  70. public function actionEdit()
  71. {
  72. if(\Yii::$app->request->isAjax){
  73. if(\Yii::$app->request->isPost){
  74. $params = \Yii::$app->request->post();
  75. $res = RandomRedPacket::setData($this->mall_id, $params);
  76. if($res == false) return $this->error("保存失败");
  77. return $this->success("保存成功");
  78. }else{
  79. $params = Yii::$app->request->get();
  80. //检查提交的参数
  81. $res = Yii::$app->services->validate->validate($get,[[['id'], 'integer']]);
  82. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  83. if(!empty($params['id'])){
  84. $detail = RandomRedPacket::getOne([['id' => $params['id']]],true);
  85. $detail['goods'] = [];
  86. if($detail){
  87. $sort = 'id DESC';
  88. $select = "id, goods_name, cost_price, stock, cover_pic";
  89. $relation_list = RandomRedPacketRelation::find()->where(['packet_id'=>$params['id']])->all();
  90. $goods_ids = array_column($relation_list,'goods_id');
  91. $where = [['id' => $goods_ids]];
  92. $params = array('alias' => 'g', 'where' => $where, 'select'=>$select,'limit' => 10, 'order' => $sort );
  93. $page_data = Goods::listPage($params, false, true);
  94. $goods = Goods::find()->where(['id'=>$goods_ids])->select($select)->asArray()->all();
  95. $detail['goods'] = $goods;
  96. if ($page_data === false) return $this->error(Goods::getStaticError());
  97. }
  98. $data['detail'] = $detail;
  99. // echo '<pre>';print_r($detail);die;
  100. return $this->success('操作成功', $data);
  101. }
  102. }
  103. }
  104. return $this->render($this->action->id,['has_coupon' => []]);
  105. }
  106. /**
  107. * 操作处理
  108. *
  109. * @return void
  110. */
  111. public function actionHandle()
  112. {
  113. $request = Yii::$app->request;
  114. if($request->isAjax && $request->isPost){
  115. $params = Yii::$app->request->post();
  116. // echo '<pre>';print_r($params);die;
  117. //检查提交的参数
  118. $res = Yii::$app->services->validate->validate($get,[
  119. [['id', 'status'], 'integer']
  120. ]);
  121. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  122. // 修改数据
  123. $res = RandomRedPacket::setData($this->mall_id, $params);
  124. if($res == false) return $this->error("修改失败" . RandomRedPacket::getStaticError());
  125. return $this->success("修改成功");
  126. }
  127. }
  128. /**
  129. * @desc:获取商品列表,添加活动使用,不用验证权限
  130. * @Author: hua
  131. * @Date: 2021/12/13
  132. * @Time: 18:51
  133. * @Copyright: copyright (c) 2021 广东七件事集团
  134. * @return mixed|string|void
  135. */
  136. public function actionGoodsList()
  137. {
  138. $request = Yii::$app->request;
  139. if ($request->isAjax) {
  140. //商品列表筛选
  141. $get = Yii::$app->request->get();
  142. $where = [['mall_id' => $this->mall['id'], 'status' => [StatusEnum::ENABLED]], ['is_on_sale' => 1]];
  143. // 筛选
  144. if (isset($get['keyword'])) {
  145. isset($get['keyword']) && $where[] = ['or', ['like', 'goods_name', $get['keyword']], ['=', 'id', $get['keyword']]];
  146. // isset($get['keyword']) && $where[] = ['like', 'goods_name', trim($get['keyword']) . '%', false];
  147. }
  148. // 排序
  149. $packet_active_list = RandomRedPacket::find()->where(['mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED])->andWhere(['>','end_at',time()])->all();
  150. $packet_id_arr = array_column($packet_active_list, 'id');
  151. $relation_list = RandomRedPacketRelation::find()->where(['packet_id'=>$packet_id_arr])->asArray()->all();
  152. $ids_arr = array_column($relation_list, 'goods_id');
  153. $ids_arr = array_unique($ids_arr);
  154. !empty($ids_arr) && $where[] = ['not in', 'id', $ids_arr];
  155. // $where[] = ['not in', 'id', $ids_arr];
  156. $sort = 'sort ASC,id DESC';
  157. $params = ['where' => $where, 'limit' => 20, 'order' => $sort];
  158. $page_data = Goods::listPage($params, false, true);
  159. if ($page_data === false) return $this->error(Goods::getStaticError());
  160. return $this->success('操作成功', $page_data);
  161. }
  162. return $this->render($this->action->id);
  163. }
  164. }