CouponForm.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\Cat;
  9. use app\models\Coupon;
  10. use app\models\CouponAutoSend;
  11. use app\models\Goods;
  12. use app\models\Mch;
  13. use app\models\MchCat;
  14. use app\models\SaasUser;
  15. use app\models\User;
  16. use app\models\UserCoupon;
  17. use yii\base\BaseObject;
  18. use yii\base\Model;
  19. use app\modules\admin\models\mochat\MochatForm;
  20. class CouponForm extends Model
  21. {
  22. public $store_id;
  23. public $user_id;
  24. public $status;
  25. public $id;
  26. public $coupon_id;
  27. public $page = 1;
  28. public function rules()
  29. {
  30. return [
  31. [['status', 'id', 'coupon_id', 'user_id', 'page'], 'integer'],
  32. ];
  33. }
  34. /**
  35. * 优惠券列表
  36. * @return array
  37. */
  38. public function search()
  39. {
  40. if (!$this->validate()) {
  41. return [
  42. 'code' => 1,
  43. 'msg' => $this->getErrorSummary(false)[0],
  44. ];
  45. }
  46. $query = UserCoupon::find()->alias('uc')->leftJoin(['c' => Coupon::tableName()], 'uc.coupon_id=c.id')
  47. ->leftJoin(['cas' => CouponAutoSend::tableName()], 'cas.id=uc.coupon_auto_send_id')
  48. ->where(['uc.user_id' => $this->user_id]);
  49. if ($this->status == 0) {
  50. $query->andWhere([
  51. 'uc.is_delete' => 0,
  52. ]);
  53. }
  54. if ($this->status == 1) {
  55. $query->andWhere([
  56. 'uc.is_delete' => 0,
  57. 'uc.is_use' => 0,
  58. 'uc.is_expire' => 0,
  59. ]);
  60. }
  61. if ($this->status == 2) {
  62. $query->andWhere([
  63. 'uc.is_delete' => 0,
  64. 'uc.is_use' => 1,
  65. 'uc.is_expire' => 0,
  66. ]);
  67. }
  68. if ($this->status == 3) {
  69. $query->andWhere([
  70. 'uc.is_delete' => 0,
  71. 'uc.is_expire' => 1,
  72. ]);
  73. }
  74. if ($this->status == 0) {
  75. $query->orderBy('uc.is_expire ASC, uc.is_use ASC, uc.created_at DESC');
  76. } else {
  77. $query->orderBy('uc.created_at DESC');
  78. }
  79. $page = $this->page;
  80. $limit = 10;
  81. $query->offset(($page - 1) * $limit);
  82. $query->limit($limit);
  83. $list = $query
  84. ->select('c.id coupon_id, uc.id user_coupon_id,c.sub_price,c.mch_id,c.min_price,uc.begin_time,uc.end_time,uc.is_use,
  85. uc.is_expire,cas.event,uc.type,c.appoint_type,c.cat_id_list,c.goods_id_list,c.name,c.rule,c.desc,c.discount,c.discount_max_price,c.discount_type,c.expire_type,c.expire_day,c.is_give,uc.give_user_id')->asArray()->all();
  86. $events = [
  87. 0 => '平台发放',
  88. 1 => '分享红包',
  89. 2 => '购物返券',
  90. 3 => '领券中心',
  91. 4 => '积分兑换',
  92. ];
  93. foreach ($list as $i => $item) {
  94. $list[$i]['can_send'] = $item['is_give'];
  95. if ($item['is_use'] || $item['is_expire']) {
  96. $list[$i]['can_send'] = 0;
  97. }
  98. $list[$i]['status'] = 0;
  99. if ($item['is_use']) {
  100. $list[$i]['status'] = 1;
  101. }
  102. if ($item['is_expire']) {
  103. $list[$i]['status'] = 2;
  104. }
  105. $list[$i]['begintime'] = date('Y.m.d', $item['begin_time']);
  106. $list[$i]['endtime'] = date('Y.m.d', $item['end_time']);
  107. $list[$i]['min_price_desc'] = $item['min_price'] == 0 ? '无门槛' : '满' . $item['min_price'] . '元可用';
  108. if (!$item['event']) {
  109. if ($item['type'] == 2) {
  110. $list[$i]['event'] = $item['event'] = 3;
  111. } elseif ($item['type'] == 0) {
  112. $list[$i]['event'] = $item['event'] = 0;
  113. } else {
  114. $list[$i]['event'] = $item['event'] = 4;
  115. }
  116. }
  117. $list[$i]['event_desc'] = $events[$item['event']];
  118. if ($item['appoint_type'] == 1 && $item['cat_id_list'] !== 'null' && $item['cat_id_list'] !== '[]') {
  119. $list[$i]['cat'] = Cat::find()->select('id, name')->where(['store_id' => $this->store_id, 'is_delete' => 0,
  120. 'id' => json_decode($item['cat_id_list'])])->asArray()->all();
  121. $cat_list = [];
  122. foreach ($list[$i]['cat'] as $value) {
  123. $cat_list[] = $value['name'];
  124. }
  125. $list[$i]['content'] = "适用范围:仅限分类:".implode('、', $cat_list)."使用";
  126. $list[$i]['goods'] = [];
  127. } elseif ($item['appoint_type'] == 2 && $item['goods_id_list'] !== 'null' && $item['goods_id_list'] !== null) {
  128. $list[$i]['goods'] = Goods::find()->select('id, name, price, cover_pic')->where(['store_id' => $this->store_id, 'is_delete' => 0, 'id' => json_decode($item['goods_id_list'])])->asArray()->all();
  129. $list[$i]['content'] = "适用范围:指定商品使用";
  130. $list[$i]['cat'] = [];
  131. } else {
  132. $list[$i]['goods'] = [];
  133. $list[$i]['cat'] = [];
  134. }
  135. $list[$i]['give_user_info'] = [];
  136. if ($item['give_user_id']) {
  137. $list[$i]['give_user_info'] = User::find()->where(['u.id' => $item['give_user_id']])->alias('u')->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')->select('su.avatar,su.name')->asArray()->one();
  138. }
  139. }
  140. return [
  141. 'code' => 0,
  142. 'msg' => 'success',
  143. 'data' => [
  144. 'list' => $list,
  145. ],
  146. ];
  147. }
  148. /**
  149. * 优惠券列表
  150. * @return array
  151. */
  152. public function saasSearch()
  153. {
  154. if (!$this->validate()) {
  155. return [
  156. 'code' => 1,
  157. 'msg' => $this->getErrorSummary(false)[0],
  158. ];
  159. }
  160. $query = UserCoupon::find()->alias('uc')->leftJoin(['c' => Coupon::tableName()], 'uc.coupon_id=c.id')
  161. ->leftJoin(['cas' => CouponAutoSend::tableName()], 'cas.id=uc.coupon_auto_send_id')
  162. ->where(['uc.user_id' => $this->user_id]);
  163. if ($this->status == 0) {
  164. $query->andWhere([
  165. 'uc.is_delete' => 0,
  166. 'uc.is_use' => 0,
  167. 'uc.is_expire' => 0,
  168. ]);
  169. }
  170. if ($this->status == 1) {
  171. $query->andWhere([
  172. 'uc.is_delete' => 0,
  173. 'uc.is_use' => 1,
  174. 'uc.is_expire' => 0,
  175. ]);
  176. }
  177. if ($this->status == 2) {
  178. $query->andWhere([
  179. 'uc.is_delete' => 0,
  180. 'uc.is_use' => 0,
  181. 'uc.is_expire' => 1,
  182. ]);
  183. }
  184. if ($this->status == 3) {
  185. $query->andWhere([
  186. 'uc.is_delete' => 0,
  187. 'uc.type' => 4, // 转赠
  188. ]);
  189. }
  190. $page = $this->page;
  191. $limit = 10;
  192. $query->offset(($page - 1) * $limit);
  193. $query->limit($limit);
  194. $list = $query->orderBy('uc.created_at DESC')
  195. ->select('c.*, uc.id user_coupon_id,uc.begin_time,uc.end_time,uc.is_use,uc.is_expire,cas.event,uc.type')->asArray()->all();
  196. $events = [
  197. 0 => '平台发放',
  198. 1 => '分享红包',
  199. 2 => '购物返券',
  200. 3 => '领券中心',
  201. 4 => '积分兑换',
  202. 5 => '福利中心',
  203. 6 => '转赠'
  204. ];
  205. foreach ($list as $i => $item) {
  206. // TODO: store 优惠券所在店铺信息
  207. $list[$i]['store'] = [
  208. 'store_id' => $item['store_id'],
  209. 'name' => 'ceshi_name',
  210. 'logo' => 'https://quanqudao.we10.cn/web/v1/statics/shop/img/shop-logo.png'
  211. ];
  212. $list[$i]['status'] = 0;
  213. if ($item['is_use']) {
  214. $list[$i]['status'] = 1;
  215. }
  216. if ($item['is_expire']) {
  217. $list[$i]['status'] = 2;
  218. }
  219. $list[$i]['content'] = "适用范围:全场通用";
  220. if ($item['appoint_type'] == 1 && $item['cat_id_list'] !== 'null') {
  221. $list[$i]['cat'] = Cat::find()->select('id, name')->where(['is_delete' => 0, 'id' => json_decode($item['cat_id_list'])])->asArray()->all();
  222. $cat_list = [];
  223. foreach ($list[$i]['cat'] as $value) {
  224. $cat_list[] = $value['name'];
  225. }
  226. $list[$i]['content'] = "适用范围:仅限分类:".implode('、', $cat_list)."使用";
  227. $list[$i]['goods'] = [];
  228. } elseif ($item['appoint_type'] == 2 && $item['goods_id_list'] !== 'null') {
  229. $list[$i]['goods'] = Goods::find()->select('id')->where(['store_id' => $item['store_id'], 'is_delete' => 0, 'id' => json_decode($item['goods_id_list'])])->asArray()->all();
  230. $list[$i]['cat'] = [];
  231. $list[$i]['content'] = "指定商品使用 点击查看指定商品";
  232. } else {
  233. $list[$i]['goods'] = [];
  234. $list[$i]['cat'] = [];
  235. }
  236. $list[$i]['min_price_desc'] = $item['min_price'] == 0 ? '无门槛' : '满' . $item['min_price'] . '元可用';
  237. $list[$i]['begin_time'] = date('Y.m.d H:i', $item['begin_time']);
  238. $list[$i]['end_time'] = date('Y.m.d H:i', $item['end_time']);
  239. if (!$item['event']) {
  240. if ($item['type'] == 2) {
  241. $list[$i]['event'] = $item['event'] = 3;
  242. } elseif ($item['type'] == 0) {
  243. $list[$i]['event'] = $item['event'] = 0;
  244. } elseif ($item['type'] == 3) {
  245. $list[$i]['event'] = $item['event'] = 5;
  246. } elseif ($item['type'] == 4) {
  247. $list[$i]['event'] = $item['event'] = 6;
  248. } else {
  249. $list[$i]['event'] = $item['event'] = 4;
  250. }
  251. }
  252. // TODO: store 信息
  253. $list[$i]['event_desc'] = $events[$item['event']];
  254. }
  255. return [
  256. 'code' => 0,
  257. 'msg' => 'success',
  258. 'data' => [
  259. 'list' => $list,
  260. ],
  261. ];
  262. }
  263. /**
  264. * 优惠券详情
  265. * @return array
  266. */
  267. public function detail()
  268. {
  269. if ($this->coupon_id) {
  270. $list = Coupon::find()->alias('c')
  271. ->where([
  272. 'c.is_delete' => 0,
  273. 'c.is_join' => 2,
  274. 'c.store_id' => $this->store_id,
  275. 'c.id' => $this->coupon_id,
  276. ])
  277. ->leftJoin(UserCoupon::tableName() . ' uc', "uc.coupon_id=c.id and uc.user_id ={$this->user_id}
  278. and uc.type = 2 and uc.is_delete=0")
  279. ->select(['c.*', '(case when isnull(uc.id) then 0 else 1 end) as is_receive'])
  280. ->asArray()
  281. ->one();
  282. $coupon = $list;
  283. };
  284. if ($this->id) {
  285. $list = UserCoupon::find()
  286. ->where([
  287. 'store_id' => $this->store_id,
  288. 'is_delete' => 0,
  289. 'id' => $this->id,
  290. ])->with('coupon')->asArray()->one();
  291. if ($coupon = $list['coupon']) {
  292. $list['min_price'] = $coupon['min_price'];
  293. $list['sub_price'] = $coupon['sub_price'];
  294. $list['appoint_type'] = $coupon['appoint_type'];
  295. $list['rule'] = $coupon['rule'];
  296. $list['expire_type'] = 2;
  297. $list['status'] = 0;
  298. if ($list['is_use']) {
  299. $list['status'] = 1;
  300. }
  301. if ($list['is_expire']) {
  302. $list['status'] = 2;
  303. }
  304. if ($list['give_user_id']) {
  305. $list['status'] = 3;
  306. }
  307. }
  308. }
  309. if ($list) {
  310. $list['can_send'] = $coupon['is_give'];
  311. if ($list['is_use'] || $list['is_expire']) {
  312. $list['can_send'] = 0;
  313. }
  314. $list['sub_price'] = $coupon['sub_price'];
  315. $list['min_price'] = $coupon['min_price'];
  316. $list['min_price_desc'] = $coupon['min_price'] == 0 ? '无门槛' : '满' . $coupon['min_price'] . '元可用';
  317. $list['begin_time'] = date('Y.m.d', $list['begin_time']);
  318. $list['end_time'] = date('Y.m.d', $list['end_time']);
  319. if ($list['appoint_type'] == 1 && $coupon['cat_id_list'] !== 'null' && $coupon['cat_id_list'] !== '[]') {
  320. $list['cat'] = Cat::find()->select('id, name')->where(['store_id' => $this->store_id, 'is_delete' => 0,
  321. 'id' => json_decode($coupon['cat_id_list'])])->asArray()->all();
  322. $cat_list = [];
  323. foreach ($list['cat'] as $value) {
  324. $cat_list[] = $value['name'];
  325. }
  326. $list['content'] = "适用范围:仅限分类:".implode('、', $cat_list)."使用";
  327. $list['goods'] = [];
  328. } elseif ($list['appoint_type'] == 2 && $coupon['goods_id_list'] !== 'null' && $coupon['goods_id_list'] !== null) {
  329. $list['goods'] = Goods::find()->select('id, name, price, cover_pic')->where(['store_id' => $this->store_id, 'is_delete' => 0, 'id' => json_decode($coupon['goods_id_list'])])->asArray()->all();
  330. $list['content'] = "适用范围:指定商品使用";
  331. $list['cat'] = [];
  332. } else {
  333. $list['goods'] = [];
  334. $list['cat'] = [];
  335. }
  336. return [
  337. 'code' => 0,
  338. 'msg' => 'success',
  339. 'data' => [
  340. 'list' => $list,
  341. ],
  342. ];
  343. } else {
  344. return [
  345. 'code' => 0,
  346. 'msg' => '不存在',
  347. 'data' => [
  348. 'list' => [],
  349. ],
  350. ];
  351. }
  352. }
  353. /**
  354. * 优惠券详情
  355. * @return array
  356. */
  357. public function saasDetail()
  358. {
  359. if ($this->coupon_id) {
  360. $list = Coupon::find()->alias('c')
  361. ->where([
  362. 'c.is_delete' => 0,
  363. 'c.is_join' => 2,
  364. 'c.store_id' => $this->store_id,
  365. 'c.id' => $this->coupon_id,
  366. ])
  367. ->leftJoin(UserCoupon::tableName() . ' uc', "uc.coupon_id=c.id and uc.user_id ={$this->user_id}
  368. and uc.type = 2 and uc.is_delete=0")
  369. ->select(['c.*', '(case when isnull(uc.id) then 0 else 1 end) as is_receive'])
  370. ->asArray()
  371. ->one();
  372. $coupon = $list;
  373. };
  374. if ($this->id) {
  375. $list = UserCoupon::find()
  376. ->where([
  377. 'store_id' => $this->store_id,
  378. 'is_delete' => 0,
  379. 'id' => $this->id,
  380. 'user_id' => $this->user_id,
  381. ])->with('coupon')->asArray()->one();
  382. if ($coupon = $list['coupon']) {
  383. $list['min_price'] = $coupon['min_price'];
  384. $list['sub_price'] = $coupon['sub_price'];
  385. $list['appoint_type'] = $coupon['appoint_type'];
  386. $list['rule'] = $coupon['rule'];
  387. $list['expire_type'] = 2;
  388. $list['status'] = 0;
  389. if ($list['is_use']) {
  390. $list['status'] = 1;
  391. }
  392. if ($list['is_expire']) {
  393. $list['status'] = 2;
  394. }
  395. }
  396. }
  397. if ($list) {
  398. if ($coupon['sub_price'] >= 100) {
  399. $list['sub_price'] = (int)$coupon['sub_price'];
  400. }
  401. if ($coupon['min_price'] >= 100) {
  402. $list['min_price'] = (int)$coupon['min_price'];
  403. }
  404. $list['min_price_desc'] = $coupon['min_price'] == 0 ? '无门槛' : '满' . $coupon['min_price'] . '元可用';
  405. $list['begin_time'] = date('Y.m.d H:i', $list['begin_time']);
  406. $list['end_time'] = date('Y.m.d H:i', $list['end_time']);
  407. if ($list['appoint_type'] == 1) {
  408. $list['cat'] = Cat::find()->select('id,name')->where(['store_id'=>$this->store_id,'is_delete'=>0,
  409. 'id'=>json_decode($coupon['cat_id_list'])])->asArray()->all();
  410. $list['goods'] = [];
  411. } elseif ($list['appoint_type'] == 2) {
  412. $list['goods'] = json_decode($coupon['goods_id_list']);
  413. $list['cat'] = [];
  414. } else {
  415. $list['goods'] = [];
  416. $list['cat'] = [];
  417. }
  418. return [
  419. 'code' => 0,
  420. 'msg' => 'success',
  421. 'data' => [
  422. 'list' => $list,
  423. ],
  424. ];
  425. } else {
  426. return [
  427. 'code' => 0,
  428. 'msg' => '不存在',
  429. 'data' => [
  430. 'list' => [],
  431. ],
  432. ];
  433. }
  434. }
  435. /**
  436. * 分享页面送优惠券
  437. * @return array
  438. */
  439. public function saveCoupon()
  440. {
  441. $coupon_auto_send_list = CouponAutoSend::find()->where([
  442. 'store_id' => $this->store_id,
  443. 'is_delete' => 0,
  444. 'event' => 1,
  445. ])->all();
  446. $count = 0;
  447. $coupon_list = [];
  448. foreach ($coupon_auto_send_list as $coupon_auto_send) {
  449. if (Coupon::userAddCoupon($this->user_id, $coupon_auto_send->coupon_id, $coupon_auto_send->id)) {
  450. $count++;
  451. $coupon = Coupon::find()->select('name,discount_type,min_price,sub_price,discount,discount_max_price,expire_type,
  452. expire_day,begin_time,end_time,goods_id_list,appoint_type')->where(['id' => $coupon_auto_send->coupon_id])
  453. ->asArray()->one();
  454. if ($coupon['expire_type'] == 1) {
  455. $coupon['desc'] = "本券有效期为发放后{$coupon['expire_day']}天内";
  456. } else {
  457. if($coupon['end_time'] <= time()) {
  458. continue;
  459. }
  460. $coupon['desc'] = "本券有效期" . date('Y-m-d H:i:s', $coupon['begin_time']) . "至"
  461. . date('Y-m-d H:i:s', $coupon['end_time']);
  462. }
  463. if (count($coupon['goods_id_list']) > 0 && $coupon['appoint_type'] == 2) {
  464. $goodIds = implode(",", json_decode($coupon['goods_id_list']));
  465. $coupon['url'] = '/other/list/list?goods_id=' . $goodIds;
  466. } else {
  467. $coupon['url'] = '/other/list/list';
  468. }
  469. $coupon_list[] = $coupon;
  470. }
  471. }
  472. if ($count == 0) {
  473. return [
  474. 'code' => 1,
  475. 'msg' => '没有发放优惠券',
  476. ];
  477. }
  478. return [
  479. 'code' => 0,
  480. 'msg' => 'success',
  481. 'data' => [
  482. 'list' => $coupon_list,
  483. ],
  484. ];
  485. }
  486. /**
  487. * 领取优惠券
  488. * @return array
  489. */
  490. public function sendCoupon()
  491. {
  492. $coupon = Coupon::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'id' => $this->id])->asArray()->one();
  493. if (!$coupon) {
  494. return [
  495. 'code' => 1,
  496. 'msg' => '网络异常'
  497. ];
  498. }
  499. $coupon['type'] = 2;
  500. $user_coupon = UserCoupon::find()
  501. ->where(['store_id' => $this->store_id , 'coupon_id' => $this->id, 'user_id' => $this->user_id, 'type' => 2,
  502. 'is_delete' => 0])->exists();
  503. if ($user_coupon) {
  504. $coupon_list[] = $coupon;
  505. $coupon_list_new = Coupon::getList();
  506. return [
  507. 'code' => 1,
  508. 'msg' => '已领取',
  509. 'data'=> [
  510. 'coupon_list'=>$coupon_list_new
  511. ]
  512. ];
  513. }
  514. $coupon_count = UserCoupon::find()->where(['store_id' => $this->store_id, 'is_delete' => 0,
  515. 'coupon_id' => $coupon['id']])->andWhere(['!=', 'type', 3])->count();
  516. if ($coupon['total_count'] != -1 && $coupon['total_count'] <= $coupon_count) {
  517. return [
  518. 'code' => 1,
  519. 'msg' => '优惠券已领完'
  520. ];
  521. }
  522. $res = new UserCoupon();
  523. $res->user_id = $this->user_id;
  524. $res->store_id = $this->store_id;
  525. $res->coupon_id = $this->id;
  526. $res->coupon_auto_send_id = 0;
  527. $res->type = 2;
  528. $res->is_use = 0;
  529. $res->is_expire = 0;
  530. $res->is_delete = 0;
  531. $res->created_at = time();
  532. if ($coupon['expire_type'] == 1) {
  533. $res->begin_time = time();
  534. $res->end_time = time() + max(0, 86400 * $coupon['expire_day']);
  535. } elseif ($coupon['expire_type'] == 2) {
  536. $res->begin_time = $coupon['begin_time'];
  537. $res->end_time = $coupon['end_time'];
  538. }
  539. $subStock = Coupon::subStock($this->id);
  540. if($subStock['code']){
  541. return $subStock;
  542. }
  543. if ($res->save()) {
  544. MochatForm::sendMsg(1, $this->store_id, MochatForm::MSG_TYPE_GET_COUPON, get_saas_user_id(), ['coupon_id' => $this->id]);
  545. $coupon_list[] = $coupon;
  546. $coupon_list_new = Coupon::getList();
  547. return [
  548. 'code' => 0,
  549. 'msg' => 'success',
  550. 'data' => [
  551. 'list' => $coupon_list,
  552. 'coupon_list' => $coupon_list_new
  553. ],
  554. ];
  555. } else {
  556. return [
  557. 'code' => 1,
  558. 'msg' => '网络异常'
  559. ];
  560. }
  561. }
  562. /**
  563. * 领取优惠券
  564. * @return array
  565. */
  566. public function sendSaasCoupon()
  567. {
  568. $coupon = Coupon::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'id' => $this->id])->asArray()->one();
  569. if (!$coupon) {
  570. return [
  571. 'code' => 1,
  572. 'msg' => '网络异常'
  573. ];
  574. }
  575. $coupon['type'] = 3;
  576. $user_coupon = UserCoupon::find()
  577. ->where(['store_id' => $this->store_id, 'coupon_id' => $this->id, 'user_id' => $this->user_id, 'type' => 3,
  578. 'is_delete' => 0])->exists();
  579. if ($user_coupon) {
  580. $coupon_list_new = Coupon::getList();
  581. return [
  582. 'code' => 1,
  583. 'msg' => '已领取',
  584. 'data'=> [
  585. 'coupon_list' => $coupon_list_new
  586. ]
  587. ];
  588. }
  589. $coupon_count = UserCoupon::find()->where(['store_id' => $this->store_id, 'is_delete' => 0, 'coupon_id' => $coupon['id'],
  590. 'type' => 3])->count();
  591. if ($coupon['total_count'] != -1 && $coupon['total_count'] <= $coupon_count) {
  592. return [
  593. 'code' => 1,
  594. 'msg' => '优惠券已领完'
  595. ];
  596. }
  597. $res = new UserCoupon();
  598. $res->user_id = $this->user_id;
  599. $res->store_id = $this->store_id;
  600. $res->coupon_id = $this->id;
  601. $res->coupon_auto_send_id = 0;
  602. $res->type = 3;
  603. $res->is_use = 0;
  604. $res->is_expire = 0;
  605. $res->is_delete = 0;
  606. $res->created_at = time();
  607. if ($coupon['expire_type'] == 1) {
  608. $res->begin_time = time();
  609. $res->end_time = time() + max(0, 86400 * $coupon['expire_day']);
  610. } elseif ($coupon['expire_type'] == 2) {
  611. $res->begin_time = $coupon['begin_time'];
  612. $res->end_time = $coupon['end_time'];
  613. }
  614. if ($res->save()) {
  615. $coupon_list[] = $coupon;
  616. $coupon_list_new = Coupon::getList();
  617. return [
  618. 'code' => 0,
  619. 'msg' => 'success',
  620. 'data' => [
  621. 'list' => $coupon_list,
  622. 'coupon_list' => $coupon_list_new
  623. ],
  624. ];
  625. } else {
  626. return [
  627. 'code' => 1,
  628. 'msg' => '网络异常'
  629. ];
  630. }
  631. }
  632. /**
  633. * 支付成功送优惠券
  634. * @return array
  635. */
  636. public function paySaveCoupon()
  637. {
  638. $coupon_auto_send_list = CouponAutoSend::find()->where([
  639. 'store_id' => $this->store_id,
  640. 'is_delete' => 0,
  641. 'event' => 2,
  642. ])->all();
  643. $count = 0;
  644. $coupon_list = [];
  645. foreach ($coupon_auto_send_list as $coupon_auto_send) {
  646. if (Coupon::userAddCoupon($this->user_id, $coupon_auto_send->coupon_id, $coupon_auto_send->id)) {
  647. $count++;
  648. $coupon = Coupon::find()->select('name,discount_type,min_price,sub_price,discount,discount_max_price,expire_type,
  649. expire_day,begin_time,end_time')->where(['id' => $coupon_auto_send->coupon_id])->asArray()->one();
  650. if ($coupon['expire_type'] == 1) {
  651. $coupon['desc'] = "本券有效期为发放后{$coupon['expire_day']}天内";
  652. } else {
  653. $coupon['desc'] = "本券有效期" . date('Y-m-d H:i:s', $coupon['begin_time']) . "至"
  654. . date('Y-m-d H:i:s', $coupon['end_time']);
  655. }
  656. $coupon_list[] = $coupon;
  657. }
  658. }
  659. if ($count == 0) {
  660. return [
  661. 'code' => 1,
  662. 'msg' => '没有发放优惠券',
  663. ];
  664. }
  665. return [
  666. 'code' => 0,
  667. 'msg' => 'success',
  668. 'data' => [
  669. 'list' => $coupon_list,
  670. ],
  671. ];
  672. }
  673. }