GuaGuaController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace backend\controllers;
  3. use biz\merchant\services\AdminService;
  4. use biz\tool\models\GuaGua;
  5. use biz\tool\models\GuaGuaAttend;
  6. use biz\tool\models\GuaGuaWin;
  7. use biz\tool\services\GuaGuaAttendService;
  8. use biz\tool\services\GuaGuaPrizeService;
  9. use biz\tool\services\GuaGuaService;
  10. use biz\tool\services\GuaGuaWinService;
  11. use biz\user\services\UserService;
  12. use biz\weixin\services\WxSceneService;
  13. use Yii;
  14. use yii\data\Pagination;
  15. use common\components\util;
  16. use common\components\qrCodeUtil;
  17. use common\services\xhUserService;
  18. class GuaGuaController extends BackendController
  19. {
  20. public $tabList = [
  21. ['name' => '老带新优惠卷', 'action' => 'all', 'url' => '/coupon/all'],
  22. ['name' => '刮刮卡', 'action' => 'list', 'url' => '/gua-gua/list'],
  23. ];
  24. public function actionList()
  25. {
  26. $condition = ['merchantId' => $this->merchantId, 'delStatus' => 1];
  27. $query = GuaGua::find()->where($condition)->orderBy('createTime desc');
  28. $countQuery = clone $query;
  29. $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]);
  30. $activities = $query->offset($pages->offset)->orderBy('addTime desc')->limit($pages->limit)->asArray()->all();
  31. $now = time();
  32. foreach ($activities as $key => $activity) {
  33. if ($activity['startTime'] > $now) {
  34. $activities[$key]['status'] = '未开始';
  35. } else if ($activity['endTime'] < $now) {
  36. $activities[$key]['status'] = '已结束';
  37. } else {
  38. $activities[$key]['status'] = '进行中';
  39. }
  40. $visitNum = GuaGuaService::getCurrentVisitNum($activity);
  41. $visitUser = GuaGuaService::getCurrentVisitUserNum($activity);
  42. $activities[$key]['visitNum'] = $visitNum;
  43. $activities[$key]['visitUser'] = $visitUser;
  44. }
  45. return $this->render('list', ['activities' => $activities, 'pages' => $pages]);
  46. }
  47. public function actionAdd()
  48. {
  49. return $this->render('add');
  50. }
  51. public function actionSaveAdd()
  52. {
  53. $post = Yii::$app->request->post();
  54. $awardNames = $post['awardName'];
  55. $orders = $post['order'];
  56. $counts = $post['count'];
  57. $probabilities = $post['probability'];
  58. //表单校验
  59. if ($post['startTime'] > $post['endTime']) {
  60. util::failInfo('结束时间要大于开始时间');
  61. }
  62. $connection = Yii::$app->db;//事务处理
  63. $transaction = $connection->beginTransaction();
  64. try {
  65. $date = date('Y-m-d H:m:i', time());
  66. $time = time();
  67. $post['merchantId'] = $this->merchantId;
  68. $post['startTime'] = strtotime($post['startTime']);
  69. $post['endTime'] = strtotime($post['endTime']);
  70. $post['awardIds'] = '';
  71. $post['actName'] = $post['activityName'];
  72. $post['createTime'] = $date;
  73. $post['addTime'] = $time;
  74. if (($post['endTime'] - $post['startTime']) > 86400 * 30) {
  75. util::failInfo('活动时长不能超过30天');
  76. }
  77. $result = GuaGuaService::add($post);
  78. $cardId = $result['id'];
  79. //奖品保存
  80. foreach ($awardNames as $key => $awardName) {
  81. $data = [
  82. 'prizeName' => $awardName,
  83. 'order' => $orders[$key],
  84. 'count' => $counts[$key],
  85. 'getChance' => $probabilities[$key],
  86. 'cardId' => $cardId,
  87. 'merchantId' => $this->merchantId,
  88. 'addTime' => $time,
  89. 'createTime' => $date
  90. ];
  91. GuaGuaPrizeService::add($data);
  92. }
  93. $transaction->commit();
  94. util::successInfo('添加成功', 'A0001');
  95. } catch (Exception $e) {
  96. $transaction->rollBack();
  97. util::failInfo('操作失败');
  98. }
  99. }
  100. public function actionEdit()
  101. {
  102. $id = Yii::$app->request->get('id');
  103. $guaGua = GuaGuaService::getById($id);
  104. $awards = GuaGuaPrizeService::getAllByCondition(['cardId' => $id], 'order desc', '*');
  105. return $this->render('edit', ['activity' => $guaGua, 'awards' => $awards]);
  106. }
  107. public function actionUpdate()
  108. {
  109. $post = Yii::$app->request->post();
  110. $cardId = $post['id'];
  111. //有用户已经参与不能再修改
  112. $hasAttend = GuaGuaAttendService::getByCondition(['cardId' => $cardId]);
  113. if (!empty($hasAttend)) {
  114. util::failInfo('有客户参与了抽奖活动,不能修改!');
  115. }
  116. $awardIds = $post['awardId'];
  117. $awardNames = $post['awardName'];
  118. $orders = $post['order'];
  119. $counts = $post['count'];
  120. $probabilities = $post['probability'];
  121. $post['startTime'] = strtotime($post['startTime']);
  122. $post['endTime'] = strtotime($post['endTime']);
  123. $post['awardIds'] = '';
  124. $post['createTime'] = date('Y-m-d H:m:i', time());
  125. GuaGuaService::updateById($cardId, $post);
  126. //奖品保存
  127. foreach ($awardNames as $key => $awardName) {
  128. $id = isset($awardIds[$key]) ? $awardIds[$key] : 0;
  129. if ($id) {
  130. $data = [
  131. 'prizeName' => $awardName,
  132. 'order' => $orders[$key],
  133. 'count' => $counts[$key],
  134. 'getChance' => $probabilities[$key],
  135. 'cardId' => $cardId,
  136. ];
  137. GuaGuaPrizeService::updateById($id, $data);
  138. } else {
  139. $data = [
  140. 'prizeName' => $awardName,
  141. 'order' => $orders[$key],
  142. 'count' => $counts[$key],
  143. 'getChance' => $probabilities[$key],
  144. 'cardId' => $cardId,
  145. ];
  146. GuaGuaPrizeService::add($data);
  147. }
  148. }
  149. util::successInfo('修改成功', 'A0001');
  150. }
  151. /**
  152. * 中奖列表
  153. * @return string
  154. */
  155. public function actionWinList()
  156. {
  157. $cardId = Yii::$app->request->get('id');
  158. $condition = ['cardId' => $cardId];
  159. $query = GuaGuaWin::find()->groupBy(['userId', 'prizeId'])->where($condition)->orderBy('getTime desc');
  160. $pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => 20]);
  161. $winnerList = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
  162. $userList = [];
  163. $prizeList = [];
  164. if (!empty($winnerList)) {
  165. $userIdList = array_column($winnerList, 'userId');
  166. $userIdList = array_unique(array_filter($userIdList));
  167. $userList = UserService::getAllByCondition(['id' => ['in', $userIdList]], null, 'id,userName,avatar,mobile', 'id');
  168. $prizeIdList = array_column($winnerList, 'prizeId');
  169. $prizeIdList = array_unique(array_filter($prizeIdList));
  170. $prizeList = GuaGuaPrizeService::getAllByCondition(['id' => ['in', $prizeIdList]], null, 'id,prizeName', 'id');
  171. }
  172. return $this->render('winList', ['winnerList' => $winnerList, 'pages' => $pages, 'userList' => $userList, 'prizeList' => $prizeList]);
  173. }
  174. /**
  175. * 所有参加抽奖活动的用户列表 shish 2019.8.11
  176. */
  177. public function actionJoinList()
  178. {
  179. $cardId = Yii::$app->request->get('id');
  180. $condition = ['cardId' => $cardId];
  181. $query = GuaGuaAttend::find()->where($condition);
  182. $totalCount = $query->count();
  183. $pages = new Pagination(['totalCount' => $totalCount, 'pageSize' => 20]);
  184. $participants = $query->offset($pages->offset)->limit($pages->limit)->orderBy('addTime desc')->asArray()->all();
  185. $newParticipants = [];
  186. foreach ($participants as $participant) {
  187. $uid = $participant['userId'];
  188. $user = xhUserService::getById($uid);
  189. $participant['userName'] = $user['userName'];
  190. $participant['mobile'] = $user['mobile'];
  191. $participant['avatar'] = $user['avatar'];
  192. $newParticipants[] = $participant;
  193. }
  194. return $this->render('joinList', ['participants' => $newParticipants, 'totalCount' => $totalCount, 'pages' => $pages]);
  195. }
  196. /**
  197. * 奖品详情 shish 2019.8.11
  198. */
  199. public function actionPrizeDetail()
  200. {
  201. $cardId = Yii::$app->request->get('id');
  202. $card = GuaGuaService::getById($cardId);
  203. if (empty($card) || $card['merchantId'] != $this->merchantId) {
  204. util::failInfo('没有活动或者没有权限操作');
  205. }
  206. $prize = GuaGuaPrizeService::getPrizeList($cardId);
  207. return $this->render('awardDetail', ['activityName' => $card['actName'], 'awards' => $prize]);
  208. }
  209. /**
  210. * 设置奖品已领取
  211. */
  212. public function actionSetPrizeGet()
  213. {
  214. $id = Yii::$app->request->post('id');
  215. $ret = GuaGuaWinService::setPrizeGet($id);
  216. if ($ret == false) {
  217. util::failInfo($this->error->getError());
  218. }
  219. util::successInfo();
  220. }
  221. /**
  222. * 生成活动链接的二维码地址 shish 2019.8.11
  223. */
  224. public function actionShowActQrcode()
  225. {
  226. $get = Yii::$app->request->get();
  227. $id = isset($get['id']) ? $get['id'] : 0;
  228. $activity = GuaGuaService::getById($id);
  229. if ($activity['merchantId'] != $this->merchantId) {
  230. util::failInfo();
  231. }
  232. if (!empty($activity['urlQrCode'])) {
  233. util::successInfo('操作成功', 'A0001', ['url' => $activity['urlQrCode']]);
  234. }
  235. /**生成微信场景二维码**/
  236. $url = WxSceneService::generateGuaGuaScene($id);
  237. //$url = Yii::$app->params['frontUrl'] . '/gua/index?id=' . $id . '&account=' . $this->merchantId;
  238. $logo = Yii::getAlias("@app") . '/../images' . $this->merchant['logo'];//取商家的logo
  239. $imgUrl = qrCodeUtil::generate($url, 'backend_active_url_' . $this->merchantId . '_' . $id, $logo);
  240. GuaGuaService::updateById($id, ['urlQrCode' => $imgUrl]);
  241. util::successInfo('操作成功', 'A0001', ['url' => $imgUrl]);
  242. }
  243. //链接发送到手机上 shish 2019.8.11
  244. public function actionSendActiveUrl()
  245. {
  246. $get = Yii::$app->request->get();
  247. $id = isset($get['activeId']) ? $get['activeId'] : 0;
  248. $activity = GuaGuaService::getById($id);
  249. if ($activity['merchantId'] != $this->merchantId) {
  250. util::failInfo();
  251. }
  252. $url = Yii::$app->params['frontUrl'] . '/gua/index?id=' . $id . '&account=' . $this->merchantId;
  253. $ret = AdminService::sendUrlToMobile($url, $this->merchant);
  254. if ($ret == false) {
  255. util::failInfo($this->error->getError());
  256. }
  257. util::successInfo('已发送');
  258. }
  259. }