CouponController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace backend\controllers;
  3. use biz\goods\services\CategoryRelateService;
  4. use biz\goods\services\CategoryService;
  5. use biz\goods\services\UsageRelationService;
  6. use biz\tool\services\CouponModelService;
  7. use biz\tool\services\CouponRewardService;
  8. use biz\tool\services\CouponRightService;
  9. use biz\tool\services\CouponService;
  10. use biz\user\services\UserService;
  11. use common\services\xhUserService;
  12. use common\models\xhCoupon;
  13. use common\services\xhCouponService;
  14. use common\components\util;
  15. use Yii;
  16. use yii\web\Controller;
  17. use yii\filters\AccessControl;
  18. use yii\helpers\Json;
  19. use yii\data\Pagination;
  20. class CouponController extends BackendController
  21. {
  22. //列出所有优惠券 shish 2019.8.30
  23. public function actionAll()
  24. {
  25. $get = Yii::$app->request->get();
  26. $merchantId = $this->merchantId;
  27. $query = xhCoupon::find();
  28. $query->where(['merchantId' => $merchantId]);
  29. if (isset($get['mobile']) && !empty($get['mobile'])) {
  30. $query->andWhere(['mobile' => $get['mobile']]);
  31. }
  32. if (isset($get['id']) && !empty($get['id'])) {
  33. $query->andWhere(['id' => $get['id']]);
  34. }
  35. if (isset($get['status']) && $get['status'] != -1) {
  36. $query->andWhere(['status' => $get['status']]);
  37. }
  38. $countQuery = clone $query;
  39. $query->orderBy('id desc');
  40. $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]);
  41. $models = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
  42. $userInfoList = [];
  43. if (!empty($models)) {
  44. $userIdList = array_column($models, 'userId');
  45. $userInfoList = UserService::getByIds($userIdList, null, 'id');
  46. CouponService::setDeadline($models);
  47. }
  48. $now = time();
  49. return $this->render('all', ['models' => $models, 'pages' => $pages, 'now' => $now, 'userInfoList' => $userInfoList]);
  50. }
  51. public function actionList()
  52. {
  53. $get = Yii::$app->request->get();
  54. $merchantId = $this->merchantId;
  55. $query = xhCoupon::find();
  56. $query->where(['merchantId' => $merchantId]);
  57. if (isset($get['number']) && !empty($get['number'])) {
  58. $query->andWhere(['number' => $get['number']]);
  59. }
  60. if (isset($get['useStatus']) && $get['useStatus'] != -1) {
  61. $query->andWhere(['useStatus' => $get['useStatus']]);
  62. }
  63. $countQuery = clone $query;
  64. $query->orderBy('id desc');
  65. $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]);
  66. $models = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
  67. return $this->render('list', ['models' => $models, 'pages' => $pages, 'now' => time()]);
  68. }
  69. public function actionUse()
  70. {
  71. $get = Yii::$app->request->get();
  72. $id = isset($get['id']) ? $get['id'] : 0;
  73. $coupon = xhCouponService::getById($id);
  74. if ($this->merchantId != $coupon['merchantId']) {
  75. util::failInfo('不是您的客户,没有操作权限');
  76. }
  77. if ($coupon['useStatus'] == 1) {
  78. util::failInfo('这个代金劵已经用过了');
  79. }
  80. $now = time();
  81. if ($now > $coupon['deadline']) {
  82. util::failInfo('代金劵已经过期');
  83. }
  84. if ($now < $coupon['beginTime']) {
  85. util::failInfo('活动还未开始,代金劵不能使用');
  86. }
  87. $id = $coupon['id'];
  88. $userId = $coupon['userId'];
  89. xhCouponService::updateById($id, $userId, ['useStatus' => 1]);
  90. util::successInfo('操作成功');
  91. }
  92. /**
  93. * 用户的所有代金劵
  94. */
  95. public function actionUserList()
  96. {
  97. $get = Yii::$app->request->get();
  98. $id = isset($get['id']) ? $get['id'] : 0;
  99. $userInfo = xhUserService::getById($id);
  100. if ($this->merchantId != $userInfo['merchantId']) {
  101. util::stop('您没有权限操作');
  102. }
  103. $models = xhCouponService::getUserCoupon($id);
  104. return $this->render('userList', ['models' => $models, 'userInfo' => $userInfo]);
  105. }
  106. /**
  107. * 代金劵的明细
  108. */
  109. public function actionDetail()
  110. {
  111. $get = Yii::$app->request->get();
  112. $id = isset($get['id']) ? $get['id'] : 0;
  113. $coupon = xhCouponService::getById($id);
  114. if ($coupon['merchantId'] != $this->merchantId) {
  115. util::stop('非法访问');
  116. }
  117. $userId = $coupon['userId'];
  118. $user = xhUserService::getById($userId);
  119. return $this->render('detail', ['coupon' => $coupon, 'user' => $user]);
  120. }
  121. //添加分类
  122. public function actionAddModel()
  123. {
  124. $post = Yii::$app->request->post();
  125. $ret = CouponModelService::addModel($post);
  126. if ($ret == false) {
  127. util::sendFail($this->error->getError());
  128. }
  129. util::sendSuccess();
  130. }
  131. //卷管理 shish 2019.8.16
  132. public function actionModel()
  133. {
  134. $where['merchantId'] = $this->merchantId;
  135. $order = 'level desc';
  136. $return = CouponModelService::getBackendList('*', $where, $order);
  137. $pages = $return['pages'];
  138. $list = $return['list'];
  139. $categoryList = CategoryRelateService::getCategoryData();
  140. $usageList = UsageRelationService::getMyUsage();
  141. return $this->render('model', ['list' => $list, 'pages' => $pages, 'categoryList' => $categoryList, 'usageList' => $usageList]);
  142. }
  143. //保存卷模板 shish 2019.8.17
  144. public function actionSaveModel()
  145. {
  146. $post = Yii::$app->request->post();
  147. $data = $post['globalCoupon'];
  148. $ret = CouponModelService::replaceModel($data);
  149. if ($ret == false) {
  150. util::sendFail($this->error->getError());
  151. }
  152. util::sendSuccess();
  153. }
  154. //获取优惠卷信息 shish 2019.8.18
  155. public function actionGetDetail()
  156. {
  157. $id = Yii::$app->request->get('id');
  158. $info = CouponModelService::getCouponInfo($id);
  159. if ($this->error->hasError) {
  160. util::sendFail($this->error->getError());
  161. }
  162. util::sendJson($info);
  163. }
  164. //发卷人列表 shish 2019.8.18
  165. public function actionRight()
  166. {
  167. $where['merchantId'] = $this->merchantId;
  168. $order = 'addTime desc';
  169. $return = CouponRightService::getBackendList('*', $where, $order);
  170. $pages = $return['pages'];
  171. $list = $return['list'];
  172. $userInfo = [];
  173. if (!empty($list)) {
  174. $userIds = array_column($list, 'userId');
  175. $userIds = array_unique(array_filter($userIds));
  176. $userInfo = UserService::getByIds($userIds, null, 'id');
  177. }
  178. $categoryList = CategoryRelateService::getCategoryData();
  179. $usageList = UsageRelationService::getMyUsage();
  180. return $this->render('right', ['list' => $list, 'pages' => $pages, 'userInfo' => $userInfo, 'categoryList' => $categoryList, 'usageList' => $usageList]);
  181. }
  182. //兑奖列表 shish 2019.8.18
  183. public function actionReward()
  184. {
  185. $get = Yii::$app->request->get();
  186. $where['merchantId'] = $this->merchantId;
  187. if (isset($get['mobile']) && !empty($get['mobile'])) {
  188. $where['mobile'] = $get['mobile'];
  189. }
  190. if (isset($get['couponId']) && !empty($get['couponId'])) {
  191. $where['couponId'] = $get['couponId'];
  192. }
  193. if (isset($get['status']) && $get['status'] != -1) {
  194. $where['status'] = $get['status'];
  195. }
  196. $return = CouponRewardService::getBackendList('*', $where, 'addTime desc');
  197. $pages = $return['pages'];
  198. $list = $return['list'];
  199. $userInfo = [];
  200. if (!empty($list)) {
  201. $userIdList = array_column($list, 'userId');
  202. $invitedUserIdList = array_column($list, 'invitedUserId');
  203. $ids = array_merge($userIdList, $invitedUserIdList);
  204. $ids = array_unique(array_filter($ids));
  205. $userInfo = UserService::getByIds($ids, null, 'id');
  206. }
  207. return $this->render('reward', ['list' => $list, 'pages' => $pages, 'userInfo' => $userInfo]);
  208. }
  209. //确认发放奖品 shish 2019.8.25
  210. public function actionGrantReward()
  211. {
  212. $this->errorExportJson();
  213. $get = Yii::$app->request->get();
  214. CouponRewardService::grantReward($get);
  215. util::ok();
  216. }
  217. //客户到店使用优惠卷 shish 2019.8.25
  218. public function actionUseCoupon()
  219. {
  220. $this->errorExportJson();
  221. $get = Yii::$app->request->get();
  222. CouponService::destroyToGetReward($get);
  223. util::ok();
  224. }
  225. }