| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- <?php
- namespace addons\Coupon\backend\controllers;
- use addons\Coupon\common\models\AddonsCoupon;
- use addons\Coupon\common\models\AddonsCouponCatRelate;
- use addons\Coupon\common\models\AddonsCouponGoodsRelate;
- use addons\Coupon\common\models\AddonsCouponMemberRelate;
- use addons\Coupon\common\models\AddonsCouponStatistics;
- use addons\Coupon\common\models\AddonsCouponGoodStatistics;
- use addons\Coupon\common\models\AddonsCouponReward;
- use addons\Coupon\common\models\CouponGiveItemSetting;
- use addons\Coupon\common\models\AddonsCouponMiniCode;
- use addons\Mch\common\models\Mch;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreGoods;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\helpers\ArrayHelper;
- use common\models\common\PosterCode;
- use common\models\goods\Goods;
- use common\models\goods\GoodsCate;
- use common\models\mall\Mall;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use common\models\user\UserLevel;
- use PHPUnit\Util\Exception;
- use Yii;
- use function Clue\StreamFilter\fun;
- use function Swoole\Coroutine\Http\get;
- use common\helpers\MiniQrCode;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Coupon\backend\controllers
- */
- class CouponController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * @name:
- * @msg: 优惠券详细信息
- * @param {*}
- * @return {*}
- */
- public function actionCouponInfo()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $coupon_id = \Yii::$app->request->get('coupon_id');
- $is_copy = \Yii::$app->request->get('is_copy') ?? false;
- $store = [];
- if (empty($coupon_id)) {
- $coupon_info = [];
- $checked_categorys = $giving_rewards = $consume_rewards = $goods_id_list = $good_lists = $store_goods_lists = $member_level_limits = [];
- } else {
- $coupon_info = AddonsCoupon::find()
- ->select('id,mall_id,mch_id,store_id,name,type,discount,discount_limit,min_price,sub_price,total_count,sort,expire_type,expire_day,begin_at,end_at,appoint_type,rule,is_remind,remind_days,effect_days,is_receive_limit,receive_count,is_member,is_original_use,is_public_receive,is_giving,giving_expire,coupon_status,is_giving_reward,is_consume_reward,created_at,qrcode_url,poster_url,is_open_screen,give_time,is_superposition_use,is_not_allow_balance_pay,is_giving_use,day_limit')
- ->where(['mall_id' => $this->mall_id, 'id' => $coupon_id, 'status' => 1, 'coupon_status' => [1, 2, 3, 4]])
- ->one();
- // dd($coupon_info);
- if (empty($coupon_info)) {
- return $this->error('优惠券不存在或已失效');
- }
- if (($coupon_info->coupon_status != 1 || $coupon_info->coupon_status != 2) && $is_copy) {
- return $this->error('该优惠券不能复制');
- }
- if ($is_copy) {
- $coupon_info->id = 0;
- }
- $member_level_limits = AddonsCouponMemberRelate::find()
- ->select('member_level')
- ->where(['mall_id' => $this->mall_id, 'coupon_id' => $coupon_id])
- ->asArray()
- ->all();
- $member_level_limits = array_values(array_flip(array_flip(array_column($member_level_limits, 'member_level'))));
- $good_lists = $coupon_info->goods;
- $store_goods_lists = [];
- if (MallAddons::isInstall($this->mall_id, 'Store')) {
- $store_goods_lists = $coupon_info->storeGoods;
- }
- $coupon_rewards = AddonsCouponReward::find()
- ->select('reward_type,balance,score,red_packet')
- ->where(['mall_id' => $this->mall_id, 'coupon_id' => $coupon_id])
- ->asArray()
- ->all();
- if (!empty($coupon_rewards)) {
- foreach ($coupon_rewards as $rk => $rv) {
- if (1 == $rv['reward_type']) {
- $giving_rewards = [
- 'balance' => $rv['balance'],
- 'score' => $rv['score'],
- 'red_packet' => $rv['red_packet'],
- ];
- } elseif (2 == $rv['reward_type']) {
- $consume_rewards = [
- 'balance' => $rv['balance'],
- 'score' => $rv['score'],
- 'red_packet' => $rv['red_packet'],
- ];
- }
- }
- if (empty($giving_rewards)) {
- $giving_rewards = [];
- }
- if (empty($consume_rewards)) {
- $consume_rewards = [];
- }
- } else {
- $giving_rewards = $consume_rewards = [];
- }
- // 优惠券已经绑定的商品分类
- $checked_categorys = $coupon_info->cateRelate;
- // dd($checked_categorys);
- if (!empty($checked_categorys)) {
- $cate_ids = array_column($checked_categorys, 'cate_id');
- // dd($cate_ids);
- foreach ($cate_ids as $cate_id) {
- $cateids[] = [$cate_id];
- }
- $checked_categorys = $cateids;
- } else {
- $checked_categorys = [];
- }
- // 门店
- if ($coupon_info['appoint_type'] == 4) {
- $store = ArrayHelper::toArray($coupon_info->store);
- }
- }
- $member_levels = $member_levels = UserLevel::find()
- ->select('id,level,name')
- ->where(['mall_id' => $this->mall_id, 'status' => 1])
- ->asArray()
- ->all();
- $conditions = [['is_show' => 1, 'status' => StatusEnum::ENABLED, 'mall_id' => $this->mall_id, 'mch_id' => $this->admin->mch_id]];
- $good_categorys = GoodsCate::getCates($conditions, [], true, 'id,name,pic,parent_id');
- $good_categorys = GoodsCate::getTreeCate($good_categorys);
- $coupon_info = ArrayHelper::toArray($coupon_info);
- if (!empty($good_lists)) {
- $good_lists = ArrayHelper::toArray($good_lists);
- foreach ($good_lists as &$gv) {
- $gv['goods_id'] = $gv['id'];
- }
- $goods_id_list = array_column($good_lists, 'id');
- }
- $store_goods_id_list = [];
- if (!empty($store_goods_lists)) {
- $store_goods_lists = ArrayHelper::toArray($store_goods_lists);
- foreach ($store_goods_lists as &$gv) {
- $gv['goods_id'] = $gv['id'];
- }
- $store_goods_id_list = array_column($store_goods_lists, 'id');
- }
- if (!isset($coupon_info['is_open_screen'])) $coupon_info['is_open_screen'] = 0;
- $store_list = [];
- $mch_list = [];
- $addons_store = false;
- $addons_mch = false;
- if (MallAddons::isInstall($this->mall_id, 'Store')) {
- if (!empty($coupon_info['store_id'])) {
- // $where = [
- // ['mall_id'=>$this->mall_id],
- // ['status'=>1],
- // ['shop_status'=>1],
- // ['check_status'=>1],
- // ];
- $store_ids = explode(',', $coupon_info['store_id']);
- $coupon_info['store_id'] = $store_ids;
- $where[] = ['id' => $store_ids];
- $store_list = Store::lists([
- 'where' => $where,
- 'select' => 'id,logo_img,store_name'
- ]);
- }
- $addons_store = true;
- }
- if (MallAddons::isInstall($this->mall_id, 'Mch')) {
- if (!empty($coupon_info['mch_id'])) {
- // $where = [
- // ['mall_id'=>$this->mall_id],
- // ['status'=>1],
- // ['shop_status'=>1],
- // ['check_status'=>1],
- // ];
- $mch_ids = explode(',', $coupon_info['mch_id']);
- $coupon_info['mch_id'] = $mch_ids;
- $where[] = ['id' => $mch_ids];
- $mch_list = Mch::lists([
- 'where' => $where,
- 'select' => 'id,logo_img,store_name'
- ]);
- }
- $addons_mch = true;
- }
- return $this->success('操作成功', compact('coupon_info', 'member_levels', 'store_list', 'mch_list', 'good_categorys', 'member_level_limits', 'good_lists', 'goods_id_list', 'giving_rewards', 'consume_rewards', 'checked_categorys', 'store', 'addons_store', 'addons_mch', 'store_goods_lists', 'store_goods_id_list'));
- }
- /**
- * @name:
- * @msg: 优惠券统计数据
- * @param {*}
- * @return {*}
- */
- public function actionCouponStatistics()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $coupon_id = \Yii::$app->request->get('coupon_id');
- $page = \Yii::$app->request->get('page') ?? 1;
- $limit = \Yii::$app->request->get('page_size') ?? $this->pageSize;
- if (empty($coupon_id)) {
- return $this->error('参数错误');
- }
- $user_coupons = AddonsCouponStatistics::find()
- ->select('total_payed_amount,total_discount_amount,use_probability,receive_user_number,use_order_number,total_use_coupon_payed_amount,use_user_number')
- ->where(['mall_id' => $this->mall_id, 'coupon_id' => $coupon_id])
- ->asArray()
- ->one();
- if (!empty($user_coupons)) {
- foreach ($user_coupons as $uk => $uv) {
- $user_coupons[$uk] = floatval($uv);
- }
- $user_coupons['use_probability'] = $user_coupons['use_probability'] * 100 . '%';
- } else {
- $user_coupons = null;
- }
- $coupon_info = AddonsCoupon::find()
- ->where(['mall_id' => $this->mall_id, 'id' => $coupon_id])
- ->asArray()
- ->one();
- $coupon_info = array_merge($coupon_info, AddonsCoupon::parseCouponInfo($coupon_info));
- $where[] = ['mall_id' => $this->mall_id, 'coupon_id' => $coupon_id];
- $params = [
- 'select' => 'coupon_id,good_id,payed_number,buyed_user_number',
- 'where' => $where,
- 'limit' => $limit,
- 'page' => $page,
- ];
- $good_stat = AddonsCouponGoodStatistics::listPage($params);
- $good_statistics = [];
- if (!empty($good_stat['list'])) {
- $good_ids = array_column($good_stat['list'], 'good_id');
- if (empty($coupon_info['store_id'])) {
- $goods = Goods::find()
- ->select('id,goods_name,cover_pic')
- ->where(['id' => $good_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- } else {
- if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE)) {
- $goods = StoreGoods::find()
- ->select('id,goods_name,cover_pic')
- ->where(['id' => $good_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- }
- }
- foreach ($good_stat['list'] as $lk => $lv) {
- $good_statistics[$lk] = $lv;
- $good_statistics[$lk]['goods_name'] = $goods[$lv['good_id']]['goods_name'] ?? '';
- $good_statistics[$lk]['cover_pic'] = $goods[$lv['good_id']]['cover_pic'] ?? '';
- }
- }
- return $this->success('操作成功', compact('user_coupons', 'good_statistics', 'coupon_info'));
- }
- /**
- * @name:
- * @msg: 优惠券置失效
- * @param {*}
- * @return {*}
- * @throws \Exception
- */
- public function actionFailure()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $coupon_id = \Yii::$app->request->get('coupon_id');
- if (empty($coupon_id)) {
- return $this->error('参数错误');
- }
- $model = AddonsCoupon::findOne(['and', ['mall_id' => $this->mall_id, 'id' => $coupon_id, 'status' => 1], ['!=', 'coupon_status', 4]]);
- if (empty($model)) {
- return $this->error('优惠券不能置失效');
- }
- $transaction = \Yii::$app->db->beginTransaction();
- $model->coupon_status = 4;
- $model->updated_at = time();
- $c_res = $model->save();
- // $uc_res = UserCoupon::updateAll(['status' => 4, 'updated_at' => time()], ['mall_id' => $this->mall_id, 'id' => $coupon_id]);
- $uc_res = true;
- if ($c_res !== false && $uc_res !== false) {
- $transaction->commit();
- return $this->success('操作成功');
- } else {
- $transaction->rollBack();
- return $this->error('操作失败:' . $model->getErrorMessage());
- }
- }
- /**
- * 删除
- * @return \yii\web\Response
- */
- public function actionDelete()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $coupon_id = \Yii::$app->request->post('coupon_id');
- if (empty($coupon_id)) {
- return $this->error('参数错误');
- }
- $res = AddonsCoupon::deleteCoupon($coupon_id, $this->mall_id);
- if (!$res) {
- return $this->error(AddonsCoupon::getStaticError());
- }
- return $this->success('删除成功');
- }
- /**
- * 指定商品可用时搜索商品
- * @return mixed|void
- */
- public function actionSearchGoods()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $keyword = \Yii::$app->request->get('keyword');
- $where[] = [
- 'mall_id' => $this->mall_id,
- 'mch_id' => $this->admin->mch_id,
- 'status' => StatusEnum::ENABLED,
- 'is_on_sale' => 1,
- ];
- if (!empty($keyword)) {
- $where[] = ['like', 'goods_name', $keyword];
- }
- $params = [
- 'select' => 'id as goods_id,goods_name,cover_pic,sort,cost_price,stock,price',
- 'where' => $where,
- 'order' => 'sort asc,id desc',
- 'page' => $page,
- 'limit' => $limit,
- ];
- $list = Goods::listPage($params);
- return $this->success('操作成功', $list);
- }
- /**
- * 指定门店商品可用时搜索门店商品
- * @return mixed|void
- */
- public function actionSearchStoreGoods()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE);
- if (empty($is_install)) {
- return $this->success('操作成功', []);
- }
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $storeKeyword = \Yii::$app->request->get('store_keyword', '');
- $keyword = \Yii::$app->request->get('keyword', '');
- $where[] = [
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED,
- 'is_on_sale' => 1,
- ];
- if ($keyword !== '') {
- if (is_numeric($keyword)) {
- $where[] = [
- 'or',
- ['like', 'goods_name', $keyword],
- ['=', 'id', $keyword]
- ];
- } else {
- $where[] = ['like', 'goods_name', $keyword];
- }
- }
- if ($storeKeyword !== '') {
- $storeFind = Store::find();
- Store::paramPack([
- 'where' => [
- [
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED
- ],
- ['like', 'store_name', $storeKeyword]
- ],
- 'select' => 'id'
- ], $storeFind);
- $storeIds = $storeFind->column();
- if (is_numeric($storeKeyword)) {
- $storeIds[] = $storeKeyword;
- }
- $where[] = ['in', 'store_id', $storeIds];
- }
- $params = [
- 'select' => 'id as goods_id,goods_name,cover_pic,sort,cost_price,stock,store_id,price,store_id',
- 'where' => $where,
- 'order' => 'sort asc,id desc',
- 'page' => $page,
- 'limit' => $limit,
- 'with' => [
- 'store' => function ($query) {
- $query->select('id, store_name, shop_mobile, logo_img');
- }
- ]
- ];
- $list = StoreGoods::listPage($params);
- return $this->success('操作成功', $list);
- }
- /**
- * 选择优惠券
- * @Author: lun
- * @DateTime: 2021/12/17 0017 10:16
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|string|void
- */
- public function actionSelectCoupon()
- {
- if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $params['where'][] = ['mall_id' => $this->mall_id];
- $params['where'][] = ['status' => [StatusEnum::ENABLED]];
- $params['where'][] = [
- 'or',
- ['=', 'total_count', -1],
- ['>', 'total_count', 'receive'],
- ];
- if (!empty($params['keyword'])) $params['where'][] = ['like', 'name', $params['keyword']];
- $params['order'] = 'sort asc,id desc';
- $params['select'] = 'id,name,type,discount,min_price,sub_price,expire_type,expire_day,begin_at,end_at,status';
- $list = AddonsCoupon::listPage($params);
- //判断是否过期
- foreach ($list['list'] as $k => $v) {
- $v['end_at'] < time() && $v['expire_type'] == 2 ? $list['list'][$k]['is_failure'] = 1 : $list['list'][$k]['is_failure'] = 0;
- }
- return $this->success('操作成功', $list);
- }
- }
- /**
- * 获取商品独立设置的优惠券设置
- * @Author: lun
- * @DateTime: 2022/3/9 0009 15:53
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionGetSettingCoupon()
- {
- $params = Yii::$app->request->post();
- $data = [];
- // 获取配置
- $result = CouponGiveItemSetting::getOne([['=', 'mall_id', $this->mall_id], ['=', 'item_id', $params['goods_id']]], true, [
- 'detail' => function ($query) {
- $query->where(['status' => StatusEnum::ENABLED])->with(['coupon']);
- }]);
- if (empty($result['detail'])) return $this->success('操作成功', $data);
- // 重新组合数组
- $data['is_enable'] = $result['is_enable'];
- $data['send_type'] = $result['send_type'];
- foreach ($result['detail'] as $item) {
- $data['coupon'][] = [
- 'discount_method' => $item['coupon']['type'] == 1 ? $item['coupon']['discount'] . '折' : $item['coupon']['sub_price'] . '元',
- 'name' => $item['coupon']['name'],
- 'num' => $item['num'],
- 'relevant_id' => $item['coupon_id'],
- ];
- }
- return $this->success('操作成功', $data);
- }
- /**
- * 指定 o2o 门店可用时搜索 o2o 门店
- * @return mixed|void
- */
- public function actionSearchStores()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE);
- if (empty($is_install)) {
- return $this->success('操作成功', []);
- }
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- $keyword = \Yii::$app->request->get('keyword');
- $where[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
- if (!empty($keyword)) {
- $where[] = ['like', 'store_name', $keyword];
- }
- $params = [
- 'select' => 'id,store_name,logo_img,shop_owner,shop_mobile',
- 'where' => $where,
- 'order' => 'created_at asc,id desc',
- 'page' => $page,
- 'limit' => $limit,
- ];
- $list = Store::listPage($params);
- return $this->success('操作成功', $list);
- }
- public function actionShowMiniPromotionCode()
- {
- $request = \Yii::$app->request;
- if ($request->isAjax) {
- if ($request->isPost) {
- $params = \Yii::$app->request->post();
- // 检查提交的参数
- $res = \Yii::$app->services->validate->validate($params, [
- [['id','store_id'], 'required'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $coupon_model = AddonsCoupon::findOne(['id' => $params['id'], 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
- if (empty($coupon_model['applet_code'])) {
- $mall = $this->mall;
- $params['mall_id'] = $this->mall_id;
- $params['mall_sign'] = $mall['mall_sign'];
- $params['sign'] = $mall['mall_sign'];
- $params['coupon_id'] = $coupon_model['id'];
- $params['coupon_share_log_id'] = 0;
- $params['item_id'] = $coupon_model['id'];
- $params['store_id'] = $params['store_id'];
- // $params['user_id'] = 1;
- // $params['content'] = $content;
- $poster = PosterCode::setData($this->mall_id, 0, 0, $params);
- if ($poster == false) {
- return $this->error(AddonsCouponMiniCode::getStaticError());
- }
- $code = $poster['code'];
- $page = 'plugins/pages/coupon/type';
- $obj = new MiniQrCode();
- $applet_code = $obj->getAppletQrCode($this->mall_id, $page, $code);
- // $applet_code = 'https://shuzixiangdao.oss-cn-guangzhou.aliyuncs.com/download/qrcode/1c772ee1dc819fd3518fbdc29bab4cdc.jpg';
- $coupon_model->applet_code = $applet_code;
- $coupon_model->save();
- if ($coupon_model == false) {
- return $this->error(AddonsCoupon::getStaticError());
- }
- }
- $applet_url = $coupon_model['applet_code'];
- return $this->success('获取成功', ['applet_url' => $applet_url]);
- }
- }
- }
- }
|