| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- <?php
- namespace addons\Coupon\backend\controllers;
- use addons\Coupon\common\enums\AddonsCouponEnum;
- 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\AddonsCouponUserRelate;
- use addons\Coupon\common\models\CouponStoreGoodsRelate;
- use addons\Mch\common\models\Mch;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreCouponUserRelate;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\goods\GoodsCate;
- use common\models\mall\MallAddons;
- use common\models\mall\MallHost;
- use common\models\user\UserLevel;
- use PHPUnit\Util\Exception;
- use Yii;
- use function Clue\StreamFilter\fun;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Coupon\backend\controllers
- */
- class DefaultController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * 首页
- * @return string|\yii\web\Response
- */
- public function actionIndex()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isGet) {
- $page = \Yii::$app->request->get('page') ?? 1;
- $limit = \Yii::$app->request->get('page_size') ?? $this->pageSize;
- $name = \Yii::$app->request->get('name');
- // 优惠券状态,1:未开始,2:领取中,3:已过期,4:已失效
- $coupon_status = \Yii::$app->request->get('status');
- $appoint_type = \Yii::$app->request->get('appoint_type');
- $type = \Yii::$app->request->get('type');
- $where[] = ['mall_id' => $this->mall_id, 'status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]];
- if (!empty($name)) {
- $where[] = ['like', 'name', $name];
- }
- if (!empty($coupon_status)) {
- $where[] = ['coupon_status' => $coupon_status];
- }
- if (!empty($type)) {
- $where[] = ['type' => $type];
- }
- if (!empty($appoint_type)) {
- $where[] = ['appoint_type' => explode(",", $appoint_type)];
- }
- $params = [
- 'select' => 'id,mall_id,mch_id,store_id,name,type,coupon_status,status,discount,discount_limit,min_price,sub_price,total_count,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,coupon_status,is_giving_reward,is_consume_reward,qrcode_url,poster_url,created_at,applet_code',
- 'where' => $where,
- 'order' => 'id desc',
- 'limit' => $limit,
- 'page' => $page,
- ];
- $list = AddonsCoupon::listPage($params);
- // $list = AddonsCoupon::getStaticError();
- // dd($list);
- if (!empty($list['list'])) {
- $mall_logo = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.logo');
- if (empty($mall_logo)) \Yii::$app->request->hostInfo . '/resources/img/mall/mall_logo_default.jpg';
- $mall_logo = $this->imgBase64Encode($mall_logo);
- $mall_name = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.name');
- if (empty($mall_name)) $mall_name = '七件事商城';
- $coupon_ids = array_column($list['list'], 'id');
- $user_coupons = AddonsCouponStatistics::find()
- ->select('coupon_id,received_number,used_number')
- ->where(['mall_id' => $this->mall_id, 'coupon_id' => $coupon_ids])
- ->asArray()
- ->indexBy('coupon_id')
- ->all();
- $stores = [];
- $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE);
- if (!empty($is_install)) {
- $store_ids = array_column($list['list'], 'store_id');
- $stores = Store::find()
- ->select('id,store_name,logo_img,shop_owner,shop_mobile')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $store_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- }
- foreach ($list['list'] as &$lv) {
- if (!empty($lv['store_id'])) { // 门店优惠券
- $storeIds = explode(',', $lv['store_id']);
- $userRelate = StoreCouponUserRelate::find()
- ->where(['mall_id' => $this->mall_id, 'store_id' => $storeIds, 'coupon_id' => $lv['id']])
- ->andWhere(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]])
- ->select(['count(1) as received_number', 'sum(if(is_use = 1, 1, 0)) as used_number'])
- ->asArray()
- ->one();
- $lv['received_number'] = $userRelate['received_number'] ?? 0;
- $lv['used_number'] = $userRelate['used_number'] ?? 0;
- } else {
- $userRelate = AddonsCouponUserRelate::find()
- ->where(['mall_id' => $this->mall_id, 'coupon_id' => $lv['id']])
- ->andWhere(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]])
- ->select(['count(1) as received_number', 'sum(if(is_use = 1, 1, 0)) as used_number'])
- ->asArray()
- ->one();
- $lv['received_number'] = $userRelate['received_number'] ?? 0;
- $lv['used_number'] = $userRelate['used_number'] ?? 0;
- }
- $lv['received_number'] = $user_coupons[$lv['id']]['received_number'] ?? 0;
- // 是否有人领取(因为统计数据是定时任务处理有一定差异性不能做完条件)
- $lv['is_received'] = AddonsCouponUserRelate::find()->where(['coupon_id' => $lv['id'], 'status' => StatusEnum::ENABLED])->exists();
- $lv['used_number'] = $user_coupons[$lv['id']]['used_number'] ?? 0;
- $mall_sign = $this->mall['mall_sign'];
- $lv['page_link'] = MallHost::getHost($this->mall_id,'h5_url') . '/#/plugins/pages/coupon/type?sign=' . $mall_sign . '&coupon_id=' . $lv['id'] . '&coupon_share_log_id=0';
- // $lv['qrcode_url'] = !empty($lv['qrcode_url']) ? \Yii::$app->request->hostInfo . '/web/' . ltrim($lv['qrcode_url'], '/') : '';
- // $lv['poster_url'] = !empty($lv['poster_url']) ? \Yii::$app->request->hostInfo . '/web/' . ltrim($lv['poster_url'], '/') : '';
- $lv['poster_filename'] = substr(md5("{'poster'_{$lv['id']}_{$this->mall_id}"), 0, 16);
- $lv['qrcode_filename'] = substr(md5("{'qrcode'_{$lv['id']}_{$this->mall_id}"), 0, 16);
- $lv = array_merge($lv, AddonsCoupon::parseCouponInfo($lv));
- $lv['store'] = $stores[$lv['store_id']] ?? [];
- $lv['content'] = $lv['type'] == AddonsCouponEnum::EXCHANGE ? '全额抵扣' : $lv['content'];
- }
- $list['mall_info']['mall_logo'] = $mall_logo;
- $list['mall_info']['mall_name'] = $mall_name;
- }
- return $this->success('操作成功', $list);
- }
- }
- return $this->render($this->action->id);
- }
- public function actionEdit()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- try {
- $post = \Yii::$app->request->post();
- // dd($post);
- if (empty($post['coupon_info']['id'])) {
- $scenarios = 'create';
- $post['coupon_info']['mall_id'] = $this->mall_id;
- } else {
- $scenarios = 'update';
- }
- if(empty($post['coupon_info']['giving_expire'])) $post['coupon_info']['giving_expire'] = 0;
- if(empty($post['coupon_info']['effect_days'])) $post['coupon_info']['effect_days'] = 0;
- if(empty($post['coupon_info']['effect_expire_days'])) $post['coupon_info']['effect_expire_days'] = 0;
- if(empty($post['coupon_info']['give_time'])) $post['coupon_info']['give_time'] = 0;
- if(!empty($post['store_list'])){
- $store_ids = array_unique(array_column($post['store_list'],'id'));
- $post['coupon_info']['store_id'] = implode(',',$store_ids);
- }else{
- $post['coupon_info']['store_id'] = '';
- }
- if(!empty($post['mch_list'])){
- $store_ids = array_unique(array_column($post['mch_list'],'id'));
- $post['coupon_info']['mch_id'] = implode(',',$store_ids);
- }else{
- $post['coupon_info']['mch_id'] = '';
- }
- switch ($post['coupon_info']['expire_type']){
- case 3:
- if($post['coupon_info']['effect_days']<=0) throw new \Exception('生效的延后天数必须大于0');
- if($post['coupon_info']['effect_expire_days']<=1) throw new \Exception('领券后立即生效,有效天数不能小于1');
- break;
- }
- $res = AddonsCoupon::setData($post, $scenarios);
- if (!$res) {
- return $this->error(AddonsCoupon::getStaticError());
- }
- return $this->success('添加优惠券成功');
- } catch (\Exception $e) {
- return $this->error($e->getMessage(), []);
- }
- } else {
- $params = \Yii::$app->request->get();
- if (!empty($params['id'])) {
- $list = AddonsCoupon::getOne([['id' => $params['id']]], true, [
- 'goods' => function ($query) {
- $query->select('id,goods_name,cover_pic');
- },
- 'cat' => function ($query) {
- $query->select('id,name');
- },
- 'store' => function($query) {
- $query->select('id,store_name,logo_img,shop_owner,shop_mobile');
- },
- ]);
- }
- $condition = [];
- $condition['where'] =[['mall_id' => $this->mall_id],['coupon_id'=> $params['id']]];
- $coupon_member_relate = AddonsCouponMemberRelate::lists($condition);
- $condition = [];
- $condition['where'] =[['coupon_id'=> $params['id']]];
- $coupon_goods_relate = AddonsCouponGoodsRelate::lists($condition);
- $couponStoreGoodsRelateCondition = $condition;
- $couponStoreGoodsRelateCondition['where'][] = ['status' => StatusEnum::ENABLED];
- $coupon_store_goods_relate = CouponStoreGoodsRelate::lists($couponStoreGoodsRelateCondition);
- $coupon_cat_relate = AddonsCouponCatRelate::lists($condition);
- $list['couponMember'] = array_column($coupon_member_relate, 'member_level');
- $list['goods_id_list'] = array_column($coupon_goods_relate, 'goods_id');
- $list['store_goods_id_list'] = array_column($coupon_store_goods_relate, 'goods_id');
- $list['cat_id_list'] = array_column($coupon_cat_relate, 'cate_id');
- $list['cat_id_list'] = array_column($coupon_cat_relate, 'cate_id');
- $list['begin_at'] = empty($list['begin_at']) ? '' : date('Y-m-d H:i:s', $list['begin_at']);
- $list['end_at'] = empty($list['end_at']) ? '' : date('Y-m-d H:i:s', $list['end_at']);
- $members = UserLevel::lists(['where' => [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], 'select' => 'id,mall_id,name,level']);
- $cats = GoodsCate::getTreeCate(GoodsCate::getCates([['=','mall_id',$this->mall_id]], [], true, 'id,mall_id,mch_id,parent_id,name,status'));
- return $this->success('操作成功', compact('members', 'cats', 'list'));
- }
- } else {
- return $this->render($this->action->id);
- }
- }
- /**
- * 搜索商品
- * @return mixed|void
- */
- public function actionSearchGoods()
- {
- $params = \Yii::$app->request->get();
- $params['where'] = [['like', 'goods_name', $params['keyword']]];
- $params['select'] = 'id,goods_name,cover_pic';
- $list = Goods::listPage($params);
- return $this->success('操作成功', $list);
- }
- /**
- * 删除优惠券
- * @return mixed|void
- * @throws \yii\db\Exception
- */
- public function actionDel()
- {
- $params = \Yii::$app->request->post();
- $params['mall_id'] = $this->mall_id;
- $params['status'] = StatusEnum::DELETE;
- $params['fields'] = ['status' => StatusEnum::DELETE];
- $res = AddonsCoupon::setData($params);
- if ($res === false) return $this->error(AddonsCoupon::getStaticError());
- return $this->success('操作成功');
- }
- /**
- * 选择优惠券
- * @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'][] = ['coupon_status' => [AddonsCouponEnum::STATUS_RECEIVING]];
- $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,total_count';
- $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;
- $list['list'][$k]['from_type'] = 1;
- }
- return $this->success('操作成功', $list);
- }
- }
- /**
- * 图片base64编码
- * @param string $img
- * @param bool $imgHtmlCode
- * author 江南极客
- * @return string
- */
- function imgBase64Encode($img = '', $imgHtmlCode = true)
- {
- //如果是本地文件
- if(strpos($img,'http') === false && !file_exists($img)){
- return $img;
- }
- //获取文件内容
- $file_content = file_get_contents($img);
- if($file_content === false){
- return $img;
- }
- $imageInfo = getimagesize($img);
- $prefiex = '';
- if($imgHtmlCode){
- $prefiex = 'data:' . $imageInfo['mime'] . ';base64,';
- }
- $base64 = $prefiex.chunk_split(base64_encode($file_content));
- return $base64;
- }
- public function actionGetStoreList(){
- $params = Yii::$app->request->post();
- if(!MallAddons::isInstall($this->mall_id,'Store')){
- return $this->success('操作成功', []);
- }
- $store_id_str = $params['store_id'];
- $store_ids = explode(',',$store_id_str);
- $list = Store::lists([
- 'where'=>[['id'=>$store_ids]],
- 'select'=>'id,store_name,shop_mobile,shop_owner'
- ]);
- return $this->success('操作成功', $list);
- }
- public function actionGetMchList(){
- $params = Yii::$app->request->post();
- if(!MallAddons::isInstall($this->mall_id,'Mch')){
- return $this->success('操作成功', []);
- }
- $mch_id_str = $params['mch_id'];
- $mch_ids = explode(',',$mch_id_str);
- $list = Mch::lists([
- 'where'=>[['id'=>$mch_ids]],
- 'select'=>'id,store_name,shop_mobile,shop_owner'
- ]);
- return $this->success('操作成功', $list);
- }
- /**
- * 商户列表
- *
- * @return string
- */
- public function actionSelectMch()
- {
- if (\Yii::$app->request->isAjax && \Yii::$app->request->isGet) {
- $params = \Yii::$app->request->get();
- $params['where'][] = ['mall_id' => $this->mall_id];
- $params['where'][] = ['status' => [StatusEnum::ENABLED]];
- $params['where'][] = ['shop_status' => 1];
- if (!empty($params['keyword'])) $params['where'][] = ['like', 'store_name', $params['keyword']];
- $params['order'] = 'id desc';
- $params['select'] = 'id,store_name,shop_owner,shop_mobile,logo_img,store_desc,shop_status,status';
- $list = Mch::listPage($params);
- return $this->success('操作成功', $list);
- }
- }
- public function actionEditCouponTotalCount()
- {
- if (\Yii::$app->request->isAjax) {
- if (\Yii::$app->request->isPost) {
- $post = \Yii::$app->request->post();
- $coupon_info = AddonsCoupon::findOne([
- 'mall_id' => $this->mall_id,
- 'id' => $post['id'],
- 'status' => [StatusEnum::ENABLED],
- 'coupon_status' => [AddonsCouponEnum::STATUS_NOT_BEGIN,AddonsCouponEnum::STATUS_RECEIVING]
- ]);
- if (empty($coupon_info)) {
- return $this->error('优惠券异常');
- }
- if ($post['total_count'] < 1) {
- throw new \Exception('优惠券发放数量不能小于1');
- }
- $coupon_info->total_count = $post['total_count'];
- $res = $coupon_info->save();
- if (!$res) {
- return $this->error(AddonsCoupon::getStaticError());
- }
- return $this->success('操作成功');
- }
- }
- }
- /**
- * 是否还有未使用的优惠券
- */
- public function actionIsCoupon()
- {
- $id = Yii::$app->request->get('id');
- if (empty($id)) {
- return $this->error('参数错误');
- }
- $coupon = AddonsCoupon::findOne(['id' => $id]);
- if (empty($coupon)) {
- return $this->error('优惠券不存在');
- }
- if (!empty($coupon->store_id)) { // 门店优惠券
- $storeIds = explode(',', $coupon['store_id']);
- $count = StoreCouponUserRelate::find()
- ->where(['mall_id' => $this->mall_id, 'store_id' => $storeIds, 'coupon_id' => $coupon['id']])
- ->andWhere(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]])
- ->andWhere(['coupon_status' => 2, 'is_use' => 0])
- ->count();
- } else {
- $count = AddonsCouponUserRelate::find()
- ->where(['mall_id' => $this->mall_id, 'coupon_id' => $coupon['id']])
- ->andWhere(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]])
- ->select(['count(1) as received_number', 'sum(if(is_use = 1, 1, 0)) as used_number'])
- ->andWhere(['coupon_status' => 2, 'is_use' => 0])
- ->count();
- }
- return $this->success('操作成功', ['count' => $count]);
- }
- }
|