| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- <?php
- namespace backend\controllers;
- use biz\goods\services\CategoryRelateService;
- use biz\goods\services\CategoryService;
- use biz\goods\services\UsageRelationService;
- use biz\tool\services\CouponModelService;
- use biz\tool\services\CouponRewardService;
- use biz\tool\services\CouponRightService;
- use biz\tool\services\CouponService;
- use biz\user\services\UserService;
- use common\services\xhUserService;
- use common\models\xhCoupon;
- use common\services\xhCouponService;
- use common\components\util;
- use Yii;
- use yii\web\Controller;
- use yii\filters\AccessControl;
- use yii\helpers\Json;
- use yii\data\Pagination;
- class CouponController extends BackendController
- {
-
- //列出所有优惠券 shish 2019.8.30
- public function actionAll()
- {
- $get = Yii::$app->request->get();
- $merchantId = $this->merchantId;
- $query = xhCoupon::find();
- $query->where(['merchantId' => $merchantId]);
- if (isset($get['mobile']) && !empty($get['mobile'])) {
- $query->andWhere(['mobile' => $get['mobile']]);
- }
- if (isset($get['id']) && !empty($get['id'])) {
- $query->andWhere(['id' => $get['id']]);
- }
- if (isset($get['status']) && $get['status'] != -1) {
- $query->andWhere(['status' => $get['status']]);
- }
- $countQuery = clone $query;
- $query->orderBy('id desc');
- $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]);
- $models = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
- $userInfoList = [];
- if (!empty($models)) {
- $userIdList = array_column($models, 'userId');
- $userInfoList = UserService::getByIds($userIdList, null, 'id');
- CouponService::setDeadline($models);
- }
- $now = time();
- return $this->render('all', ['models' => $models, 'pages' => $pages, 'now' => $now, 'userInfoList' => $userInfoList]);
- }
-
- public function actionList()
- {
- $get = Yii::$app->request->get();
- $merchantId = $this->merchantId;
- $query = xhCoupon::find();
- $query->where(['merchantId' => $merchantId]);
- if (isset($get['number']) && !empty($get['number'])) {
- $query->andWhere(['number' => $get['number']]);
- }
- if (isset($get['useStatus']) && $get['useStatus'] != -1) {
- $query->andWhere(['useStatus' => $get['useStatus']]);
- }
- $countQuery = clone $query;
- $query->orderBy('id desc');
- $pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]);
- $models = $query->offset($pages->offset)->limit($pages->limit)->asArray()->all();
- return $this->render('list', ['models' => $models, 'pages' => $pages, 'now' => time()]);
- }
-
- public function actionUse()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $coupon = xhCouponService::getById($id);
- if ($this->merchantId != $coupon['merchantId']) {
- util::failInfo('不是您的客户,没有操作权限');
- }
- if ($coupon['useStatus'] == 1) {
- util::failInfo('这个代金劵已经用过了');
- }
- $now = time();
- if ($now > $coupon['deadline']) {
- util::failInfo('代金劵已经过期');
- }
- if ($now < $coupon['beginTime']) {
- util::failInfo('活动还未开始,代金劵不能使用');
- }
- $id = $coupon['id'];
- $userId = $coupon['userId'];
- xhCouponService::updateById($id, $userId, ['useStatus' => 1]);
- util::successInfo('操作成功');
- }
-
- /**
- * 用户的所有代金劵
- */
- public function actionUserList()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $userInfo = xhUserService::getById($id);
- if ($this->merchantId != $userInfo['merchantId']) {
- util::stop('您没有权限操作');
- }
- $models = xhCouponService::getUserCoupon($id);
- return $this->render('userList', ['models' => $models, 'userInfo' => $userInfo]);
- }
-
- /**
- * 代金劵的明细
- */
- public function actionDetail()
- {
- $get = Yii::$app->request->get();
- $id = isset($get['id']) ? $get['id'] : 0;
- $coupon = xhCouponService::getById($id);
- if ($coupon['merchantId'] != $this->merchantId) {
- util::stop('非法访问');
- }
- $userId = $coupon['userId'];
- $user = xhUserService::getById($userId);
- return $this->render('detail', ['coupon' => $coupon, 'user' => $user]);
- }
-
-
- //添加分类
- public function actionAddModel()
- {
- $post = Yii::$app->request->post();
- $ret = CouponModelService::addModel($post);
- if ($ret == false) {
- util::sendFail($this->error->getError());
- }
- util::sendSuccess();
- }
-
- //卷管理 shish 2019.8.16
- public function actionModel()
- {
- $where['merchantId'] = $this->merchantId;
- $order = 'level desc';
- $return = CouponModelService::getBackendList('*', $where, $order);
- $pages = $return['pages'];
- $list = $return['list'];
- $categoryList = CategoryRelateService::getCategoryData();
- $usageList = UsageRelationService::getMyUsage();
- return $this->render('model', ['list' => $list, 'pages' => $pages, 'categoryList' => $categoryList, 'usageList' => $usageList]);
- }
-
- //保存卷模板 shish 2019.8.17
- public function actionSaveModel()
- {
- $post = Yii::$app->request->post();
- $data = $post['globalCoupon'];
- $ret = CouponModelService::replaceModel($data);
- if ($ret == false) {
- util::sendFail($this->error->getError());
- }
- util::sendSuccess();
- }
-
- //获取优惠卷信息 shish 2019.8.18
- public function actionGetDetail()
- {
- $id = Yii::$app->request->get('id');
- $info = CouponModelService::getCouponInfo($id);
- if ($this->error->hasError) {
- util::sendFail($this->error->getError());
- }
- util::sendJson($info);
- }
-
- //发卷人列表 shish 2019.8.18
- public function actionRight()
- {
- $where['merchantId'] = $this->merchantId;
- $order = 'addTime desc';
- $return = CouponRightService::getBackendList('*', $where, $order);
- $pages = $return['pages'];
- $list = $return['list'];
- $userInfo = [];
- if (!empty($list)) {
- $userIds = array_column($list, 'userId');
- $userIds = array_unique(array_filter($userIds));
- $userInfo = UserService::getByIds($userIds, null, 'id');
- }
- $categoryList = CategoryRelateService::getCategoryData();
- $usageList = UsageRelationService::getMyUsage();
- return $this->render('right', ['list' => $list, 'pages' => $pages, 'userInfo' => $userInfo, 'categoryList' => $categoryList, 'usageList' => $usageList]);
- }
-
- //兑奖列表 shish 2019.8.18
- public function actionReward()
- {
- $get = Yii::$app->request->get();
- $where['merchantId'] = $this->merchantId;
- if (isset($get['mobile']) && !empty($get['mobile'])) {
- $where['mobile'] = $get['mobile'];
- }
- if (isset($get['couponId']) && !empty($get['couponId'])) {
- $where['couponId'] = $get['couponId'];
- }
- if (isset($get['status']) && $get['status'] != -1) {
- $where['status'] = $get['status'];
- }
- $return = CouponRewardService::getBackendList('*', $where, 'addTime desc');
- $pages = $return['pages'];
- $list = $return['list'];
- $userInfo = [];
- if (!empty($list)) {
- $userIdList = array_column($list, 'userId');
- $invitedUserIdList = array_column($list, 'invitedUserId');
- $ids = array_merge($userIdList, $invitedUserIdList);
- $ids = array_unique(array_filter($ids));
- $userInfo = UserService::getByIds($ids, null, 'id');
- }
- return $this->render('reward', ['list' => $list, 'pages' => $pages, 'userInfo' => $userInfo]);
- }
-
- //确认发放奖品 shish 2019.8.25
- public function actionGrantReward()
- {
- $this->errorExportJson();
- $get = Yii::$app->request->get();
- CouponRewardService::grantReward($get);
- util::ok();
- }
-
- //客户到店使用优惠卷 shish 2019.8.25
- public function actionUseCoupon()
- {
- $this->errorExportJson();
- $get = Yii::$app->request->get();
- CouponService::destroyToGetReward($get);
- util::ok();
- }
-
- }
|