DefaultController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. namespace addons\Coupon\backend\controllers;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use addons\Coupon\common\models\AddonsCoupon;
  5. use addons\Coupon\common\models\AddonsCouponCatRelate;
  6. use addons\Coupon\common\models\AddonsCouponGoodsRelate;
  7. use addons\Coupon\common\models\AddonsCouponMemberRelate;
  8. use addons\Coupon\common\models\AddonsCouponStatistics;
  9. use addons\Coupon\common\models\AddonsCouponUserRelate;
  10. use addons\Coupon\common\models\CouponStoreGoodsRelate;
  11. use addons\Mch\common\models\Mch;
  12. use addons\Store\common\models\Store;
  13. use addons\Store\common\models\StoreCouponUserRelate;
  14. use common\enums\AddonsEnum;
  15. use common\enums\StatusEnum;
  16. use common\models\goods\Goods;
  17. use common\models\goods\GoodsCate;
  18. use common\models\mall\MallAddons;
  19. use common\models\mall\MallHost;
  20. use common\models\user\UserLevel;
  21. use PHPUnit\Util\Exception;
  22. use Yii;
  23. use function Clue\StreamFilter\fun;
  24. /**
  25. * 默认控制器
  26. *
  27. * Class DefaultController
  28. * @package addons\Coupon\backend\controllers
  29. */
  30. class DefaultController extends BaseController
  31. {
  32. public $enableCsrfValidation = false;
  33. /**
  34. * 首页
  35. * @return string|\yii\web\Response
  36. */
  37. public function actionIndex()
  38. {
  39. if (\Yii::$app->request->isAjax) {
  40. if (\Yii::$app->request->isGet) {
  41. $page = \Yii::$app->request->get('page') ?? 1;
  42. $limit = \Yii::$app->request->get('page_size') ?? $this->pageSize;
  43. $name = \Yii::$app->request->get('name');
  44. // 优惠券状态,1:未开始,2:领取中,3:已过期,4:已失效
  45. $coupon_status = \Yii::$app->request->get('status');
  46. $appoint_type = \Yii::$app->request->get('appoint_type');
  47. $type = \Yii::$app->request->get('type');
  48. $where[] = ['mall_id' => $this->mall_id, 'status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  49. if (!empty($name)) {
  50. $where[] = ['like', 'name', $name];
  51. }
  52. if (!empty($coupon_status)) {
  53. $where[] = ['coupon_status' => $coupon_status];
  54. }
  55. if (!empty($type)) {
  56. $where[] = ['type' => $type];
  57. }
  58. if (!empty($appoint_type)) {
  59. $where[] = ['appoint_type' => explode(",", $appoint_type)];
  60. }
  61. $params = [
  62. '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',
  63. 'where' => $where,
  64. 'order' => 'id desc',
  65. 'limit' => $limit,
  66. 'page' => $page,
  67. ];
  68. $list = AddonsCoupon::listPage($params);
  69. // $list = AddonsCoupon::getStaticError();
  70. // dd($list);
  71. if (!empty($list['list'])) {
  72. $mall_logo = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.logo');
  73. if (empty($mall_logo)) \Yii::$app->request->hostInfo . '/resources/img/mall/mall_logo_default.jpg';
  74. $mall_logo = $this->imgBase64Encode($mall_logo);
  75. $mall_name = \Yii::$app->services->setting->mall->get($this->mall_id, 'basic.name');
  76. if (empty($mall_name)) $mall_name = '七件事商城';
  77. $coupon_ids = array_column($list['list'], 'id');
  78. $user_coupons = AddonsCouponStatistics::find()
  79. ->select('coupon_id,received_number,used_number')
  80. ->where(['mall_id' => $this->mall_id, 'coupon_id' => $coupon_ids])
  81. ->asArray()
  82. ->indexBy('coupon_id')
  83. ->all();
  84. $stores = [];
  85. $is_install = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_STORE);
  86. if (!empty($is_install)) {
  87. $store_ids = array_column($list['list'], 'store_id');
  88. $stores = Store::find()
  89. ->select('id,store_name,logo_img,shop_owner,shop_mobile')
  90. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $store_ids])
  91. ->asArray()
  92. ->indexBy('id')
  93. ->all();
  94. }
  95. foreach ($list['list'] as &$lv) {
  96. if (!empty($lv['store_id'])) { // 门店优惠券
  97. $storeIds = explode(',', $lv['store_id']);
  98. $userRelate = StoreCouponUserRelate::find()
  99. ->where(['mall_id' => $this->mall_id, 'store_id' => $storeIds, 'coupon_id' => $lv['id']])
  100. ->andWhere(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]])
  101. ->select(['count(1) as received_number', 'sum(if(is_use = 1, 1, 0)) as used_number'])
  102. ->asArray()
  103. ->one();
  104. $lv['received_number'] = $userRelate['received_number'] ?? 0;
  105. $lv['used_number'] = $userRelate['used_number'] ?? 0;
  106. } else {
  107. $userRelate = AddonsCouponUserRelate::find()
  108. ->where(['mall_id' => $this->mall_id, 'coupon_id' => $lv['id']])
  109. ->andWhere(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]])
  110. ->select(['count(1) as received_number', 'sum(if(is_use = 1, 1, 0)) as used_number'])
  111. ->asArray()
  112. ->one();
  113. $lv['received_number'] = $userRelate['received_number'] ?? 0;
  114. $lv['used_number'] = $userRelate['used_number'] ?? 0;
  115. }
  116. $lv['received_number'] = $user_coupons[$lv['id']]['received_number'] ?? 0;
  117. // 是否有人领取(因为统计数据是定时任务处理有一定差异性不能做完条件)
  118. $lv['is_received'] = AddonsCouponUserRelate::find()->where(['coupon_id' => $lv['id'], 'status' => StatusEnum::ENABLED])->exists();
  119. $lv['used_number'] = $user_coupons[$lv['id']]['used_number'] ?? 0;
  120. $mall_sign = $this->mall['mall_sign'];
  121. $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';
  122. // $lv['qrcode_url'] = !empty($lv['qrcode_url']) ? \Yii::$app->request->hostInfo . '/web/' . ltrim($lv['qrcode_url'], '/') : '';
  123. // $lv['poster_url'] = !empty($lv['poster_url']) ? \Yii::$app->request->hostInfo . '/web/' . ltrim($lv['poster_url'], '/') : '';
  124. $lv['poster_filename'] = substr(md5("{'poster'_{$lv['id']}_{$this->mall_id}"), 0, 16);
  125. $lv['qrcode_filename'] = substr(md5("{'qrcode'_{$lv['id']}_{$this->mall_id}"), 0, 16);
  126. $lv = array_merge($lv, AddonsCoupon::parseCouponInfo($lv));
  127. $lv['store'] = $stores[$lv['store_id']] ?? [];
  128. $lv['content'] = $lv['type'] == AddonsCouponEnum::EXCHANGE ? '全额抵扣' : $lv['content'];
  129. }
  130. $list['mall_info']['mall_logo'] = $mall_logo;
  131. $list['mall_info']['mall_name'] = $mall_name;
  132. }
  133. return $this->success('操作成功', $list);
  134. }
  135. }
  136. return $this->render($this->action->id);
  137. }
  138. public function actionEdit()
  139. {
  140. if (\Yii::$app->request->isAjax) {
  141. if (\Yii::$app->request->isPost) {
  142. try {
  143. $post = \Yii::$app->request->post();
  144. // dd($post);
  145. if (empty($post['coupon_info']['id'])) {
  146. $scenarios = 'create';
  147. $post['coupon_info']['mall_id'] = $this->mall_id;
  148. } else {
  149. $scenarios = 'update';
  150. }
  151. if(empty($post['coupon_info']['giving_expire'])) $post['coupon_info']['giving_expire'] = 0;
  152. if(empty($post['coupon_info']['effect_days'])) $post['coupon_info']['effect_days'] = 0;
  153. if(empty($post['coupon_info']['effect_expire_days'])) $post['coupon_info']['effect_expire_days'] = 0;
  154. if(empty($post['coupon_info']['give_time'])) $post['coupon_info']['give_time'] = 0;
  155. if(!empty($post['store_list'])){
  156. $store_ids = array_unique(array_column($post['store_list'],'id'));
  157. $post['coupon_info']['store_id'] = implode(',',$store_ids);
  158. }else{
  159. $post['coupon_info']['store_id'] = '';
  160. }
  161. if(!empty($post['mch_list'])){
  162. $store_ids = array_unique(array_column($post['mch_list'],'id'));
  163. $post['coupon_info']['mch_id'] = implode(',',$store_ids);
  164. }else{
  165. $post['coupon_info']['mch_id'] = '';
  166. }
  167. switch ($post['coupon_info']['expire_type']){
  168. case 3:
  169. if($post['coupon_info']['effect_days']<=0) throw new \Exception('生效的延后天数必须大于0');
  170. if($post['coupon_info']['effect_expire_days']<=1) throw new \Exception('领券后立即生效,有效天数不能小于1');
  171. break;
  172. }
  173. $res = AddonsCoupon::setData($post, $scenarios);
  174. if (!$res) {
  175. return $this->error(AddonsCoupon::getStaticError());
  176. }
  177. return $this->success('添加优惠券成功');
  178. } catch (\Exception $e) {
  179. return $this->error($e->getMessage(), []);
  180. }
  181. } else {
  182. $params = \Yii::$app->request->get();
  183. if (!empty($params['id'])) {
  184. $list = AddonsCoupon::getOne([['id' => $params['id']]], true, [
  185. 'goods' => function ($query) {
  186. $query->select('id,goods_name,cover_pic');
  187. },
  188. 'cat' => function ($query) {
  189. $query->select('id,name');
  190. },
  191. 'store' => function($query) {
  192. $query->select('id,store_name,logo_img,shop_owner,shop_mobile');
  193. },
  194. ]);
  195. }
  196. $condition = [];
  197. $condition['where'] =[['mall_id' => $this->mall_id],['coupon_id'=> $params['id']]];
  198. $coupon_member_relate = AddonsCouponMemberRelate::lists($condition);
  199. $condition = [];
  200. $condition['where'] =[['coupon_id'=> $params['id']]];
  201. $coupon_goods_relate = AddonsCouponGoodsRelate::lists($condition);
  202. $couponStoreGoodsRelateCondition = $condition;
  203. $couponStoreGoodsRelateCondition['where'][] = ['status' => StatusEnum::ENABLED];
  204. $coupon_store_goods_relate = CouponStoreGoodsRelate::lists($couponStoreGoodsRelateCondition);
  205. $coupon_cat_relate = AddonsCouponCatRelate::lists($condition);
  206. $list['couponMember'] = array_column($coupon_member_relate, 'member_level');
  207. $list['goods_id_list'] = array_column($coupon_goods_relate, 'goods_id');
  208. $list['store_goods_id_list'] = array_column($coupon_store_goods_relate, 'goods_id');
  209. $list['cat_id_list'] = array_column($coupon_cat_relate, 'cate_id');
  210. $list['cat_id_list'] = array_column($coupon_cat_relate, 'cate_id');
  211. $list['begin_at'] = empty($list['begin_at']) ? '' : date('Y-m-d H:i:s', $list['begin_at']);
  212. $list['end_at'] = empty($list['end_at']) ? '' : date('Y-m-d H:i:s', $list['end_at']);
  213. $members = UserLevel::lists(['where' => [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], 'select' => 'id,mall_id,name,level']);
  214. $cats = GoodsCate::getTreeCate(GoodsCate::getCates([['=','mall_id',$this->mall_id]], [], true, 'id,mall_id,mch_id,parent_id,name,status'));
  215. return $this->success('操作成功', compact('members', 'cats', 'list'));
  216. }
  217. } else {
  218. return $this->render($this->action->id);
  219. }
  220. }
  221. /**
  222. * 搜索商品
  223. * @return mixed|void
  224. */
  225. public function actionSearchGoods()
  226. {
  227. $params = \Yii::$app->request->get();
  228. $params['where'] = [['like', 'goods_name', $params['keyword']]];
  229. $params['select'] = 'id,goods_name,cover_pic';
  230. $list = Goods::listPage($params);
  231. return $this->success('操作成功', $list);
  232. }
  233. /**
  234. * 删除优惠券
  235. * @return mixed|void
  236. * @throws \yii\db\Exception
  237. */
  238. public function actionDel()
  239. {
  240. $params = \Yii::$app->request->post();
  241. $params['mall_id'] = $this->mall_id;
  242. $params['status'] = StatusEnum::DELETE;
  243. $params['fields'] = ['status' => StatusEnum::DELETE];
  244. $res = AddonsCoupon::setData($params);
  245. if ($res === false) return $this->error(AddonsCoupon::getStaticError());
  246. return $this->success('操作成功');
  247. }
  248. /**
  249. * 选择优惠券
  250. * @Author: lun
  251. * @DateTime: 2021/12/17 0017 10:16
  252. * @Copyright: copyright (c) 2021 广东七件事集团
  253. * @return mixed|string|void
  254. */
  255. public function actionSelectCoupon()
  256. {
  257. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  258. $params = Yii::$app->request->post();
  259. $params['where'][] = ['mall_id' => $this->mall_id];
  260. $params['where'][] = ['status' => [StatusEnum::ENABLED]];
  261. $params['where'][] = ['coupon_status' => [AddonsCouponEnum::STATUS_RECEIVING]];
  262. $params['where'][] = ['or',
  263. ['=', 'total_count', -1],
  264. ['>', 'total_count', 'receive'],
  265. ];
  266. if (!empty($params['keyword'])) $params['where'][] = ['like', 'name', $params['keyword']];
  267. $params['order'] = 'sort asc,id desc';
  268. $params['select'] = 'id,name,type,discount,min_price,sub_price,expire_type,expire_day,begin_at,end_at,status,total_count';
  269. $list = AddonsCoupon::listPage($params);
  270. //判断是否过期
  271. foreach ($list['list'] as $k=>$v){
  272. $v['end_at']<time()&&$v['expire_type']==2?$list['list'][$k]['is_failure'] = 1:$list['list'][$k]['is_failure'] = 0;
  273. $list['list'][$k]['from_type'] = 1;
  274. }
  275. return $this->success('操作成功', $list);
  276. }
  277. }
  278. /**
  279. * 图片base64编码
  280. * @param string $img
  281. * @param bool $imgHtmlCode
  282. * author 江南极客
  283. * @return string
  284. */
  285. function imgBase64Encode($img = '', $imgHtmlCode = true)
  286. {
  287. //如果是本地文件
  288. if(strpos($img,'http') === false && !file_exists($img)){
  289. return $img;
  290. }
  291. //获取文件内容
  292. $file_content = file_get_contents($img);
  293. if($file_content === false){
  294. return $img;
  295. }
  296. $imageInfo = getimagesize($img);
  297. $prefiex = '';
  298. if($imgHtmlCode){
  299. $prefiex = 'data:' . $imageInfo['mime'] . ';base64,';
  300. }
  301. $base64 = $prefiex.chunk_split(base64_encode($file_content));
  302. return $base64;
  303. }
  304. public function actionGetStoreList(){
  305. $params = Yii::$app->request->post();
  306. if(!MallAddons::isInstall($this->mall_id,'Store')){
  307. return $this->success('操作成功', []);
  308. }
  309. $store_id_str = $params['store_id'];
  310. $store_ids = explode(',',$store_id_str);
  311. $list = Store::lists([
  312. 'where'=>[['id'=>$store_ids]],
  313. 'select'=>'id,store_name,shop_mobile,shop_owner'
  314. ]);
  315. return $this->success('操作成功', $list);
  316. }
  317. public function actionGetMchList(){
  318. $params = Yii::$app->request->post();
  319. if(!MallAddons::isInstall($this->mall_id,'Mch')){
  320. return $this->success('操作成功', []);
  321. }
  322. $mch_id_str = $params['mch_id'];
  323. $mch_ids = explode(',',$mch_id_str);
  324. $list = Mch::lists([
  325. 'where'=>[['id'=>$mch_ids]],
  326. 'select'=>'id,store_name,shop_mobile,shop_owner'
  327. ]);
  328. return $this->success('操作成功', $list);
  329. }
  330. /**
  331. * 商户列表
  332. *
  333. * @return string
  334. */
  335. public function actionSelectMch()
  336. {
  337. if (\Yii::$app->request->isAjax && \Yii::$app->request->isGet) {
  338. $params = \Yii::$app->request->get();
  339. $params['where'][] = ['mall_id' => $this->mall_id];
  340. $params['where'][] = ['status' => [StatusEnum::ENABLED]];
  341. $params['where'][] = ['shop_status' => 1];
  342. if (!empty($params['keyword'])) $params['where'][] = ['like', 'store_name', $params['keyword']];
  343. $params['order'] = 'id desc';
  344. $params['select'] = 'id,store_name,shop_owner,shop_mobile,logo_img,store_desc,shop_status,status';
  345. $list = Mch::listPage($params);
  346. return $this->success('操作成功', $list);
  347. }
  348. }
  349. public function actionEditCouponTotalCount()
  350. {
  351. if (\Yii::$app->request->isAjax) {
  352. if (\Yii::$app->request->isPost) {
  353. $post = \Yii::$app->request->post();
  354. $coupon_info = AddonsCoupon::findOne([
  355. 'mall_id' => $this->mall_id,
  356. 'id' => $post['id'],
  357. 'status' => [StatusEnum::ENABLED],
  358. 'coupon_status' => [AddonsCouponEnum::STATUS_NOT_BEGIN,AddonsCouponEnum::STATUS_RECEIVING]
  359. ]);
  360. if (empty($coupon_info)) {
  361. return $this->error('优惠券异常');
  362. }
  363. if ($post['total_count'] < 1) {
  364. throw new \Exception('优惠券发放数量不能小于1');
  365. }
  366. $coupon_info->total_count = $post['total_count'];
  367. $res = $coupon_info->save();
  368. if (!$res) {
  369. return $this->error(AddonsCoupon::getStaticError());
  370. }
  371. return $this->success('操作成功');
  372. }
  373. }
  374. }
  375. /**
  376. * 是否还有未使用的优惠券
  377. */
  378. public function actionIsCoupon()
  379. {
  380. $id = Yii::$app->request->get('id');
  381. if (empty($id)) {
  382. return $this->error('参数错误');
  383. }
  384. $coupon = AddonsCoupon::findOne(['id' => $id]);
  385. if (empty($coupon)) {
  386. return $this->error('优惠券不存在');
  387. }
  388. if (!empty($coupon->store_id)) { // 门店优惠券
  389. $storeIds = explode(',', $coupon['store_id']);
  390. $count = StoreCouponUserRelate::find()
  391. ->where(['mall_id' => $this->mall_id, 'store_id' => $storeIds, 'coupon_id' => $coupon['id']])
  392. ->andWhere(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]])
  393. ->andWhere(['coupon_status' => 2, 'is_use' => 0])
  394. ->count();
  395. } else {
  396. $count = AddonsCouponUserRelate::find()
  397. ->where(['mall_id' => $this->mall_id, 'coupon_id' => $coupon['id']])
  398. ->andWhere(['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]])
  399. ->select(['count(1) as received_number', 'sum(if(is_use = 1, 1, 0)) as used_number'])
  400. ->andWhere(['coupon_status' => 2, 'is_use' => 0])
  401. ->count();
  402. }
  403. return $this->success('操作成功', ['count' => $count]);
  404. }
  405. }