MemberRecommend.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\memberrecommend\model;
  11. use app\model\system\Cron;
  12. use app\model\BaseModel;
  13. use think\facade\Db;
  14. use app\model\member\MemberAccount;
  15. use addon\coupon\model\Coupon;
  16. use addon\coupon\model\CouponType;
  17. /**
  18. * 邀请奖励
  19. */
  20. class MemberRecommend extends BaseModel
  21. {
  22. //状态
  23. private $status = [
  24. 0 => '未开始',
  25. 1 => '进行中',
  26. 2 => '已结束',
  27. -1 => '已关闭',
  28. ];
  29. public function getStatus()
  30. {
  31. return $this->status;
  32. }
  33. /**
  34. * 添加邀请奖励
  35. * @param $data
  36. * @return array
  37. */
  38. public function addRecommend($data)
  39. {
  40. //时间检测
  41. if ($data[ 'end_time' ] < time()) {
  42. return $this->error('', '结束时间不能早于当前时间');
  43. }
  44. $activity_info = model('member_recommend')->getInfo([
  45. [ 'status', 'in', "0,1" ],
  46. [ 'site_id', '=', $data[ 'site_id' ] ],
  47. [ '', 'exp', Db::raw('not ( (`start_time` > ' . $data[ 'end_time' ] . ' and `start_time` > ' . $data[ 'start_time' ] . ' ) or (`end_time` < ' . $data[ 'start_time' ] . ' and `end_time` < ' . $data[ 'end_time' ] . '))') ]
  48. ], 'recommend_name,start_time,end_time');
  49. if (!empty($activity_info)) {
  50. return $this->error('', '此时间段已有同类型的活动');
  51. }
  52. $data[ 'create_time' ] = time();
  53. if ($data[ 'start_time' ] <= time()) {
  54. $data[ 'status' ] = 1;//直接启动
  55. } else {
  56. $data[ 'status' ] = 0;
  57. }
  58. model('member_recommend')->startTrans();
  59. try {
  60. $recommend_id = model('member_recommend')->add($data);
  61. $cron = new Cron();
  62. if ($data[ 'start_time' ] <= time()) {
  63. $cron->addCron(1, 0, "邀请奖励关闭", "CloseRecommend", $data[ 'end_time' ], $recommend_id);
  64. } else {
  65. $cron->addCron(1, 0, "邀请奖励开启", "OpenRecommend", $data[ 'start_time' ], $recommend_id);
  66. $cron->addCron(1, 0, "邀请奖励关闭", "CloseRecommend", $data[ 'end_time' ], $recommend_id);
  67. }
  68. model('member_recommend')->commit();
  69. return $this->success();
  70. } catch (\Exception $e) {
  71. model('member_recommend')->rollback();
  72. return $this->error('', $e->getMessage());
  73. }
  74. }
  75. /**
  76. * 修改邀请奖励
  77. * @param $data
  78. * @return array
  79. */
  80. public function editRecommend($data)
  81. {
  82. $recommend_id = $data[ 'recommend_id' ];
  83. unset($data[ 'recommend_id' ]);
  84. $recommend_status = model('member_recommend')->getInfo([ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'recommend_id', '=', $recommend_id ] ], 'status');
  85. if ($recommend_status[ 'status' ] != 0) {
  86. return $this->error('', '只有未开始的活动才能进行修改');
  87. }
  88. //时间检测
  89. if ($data[ 'end_time' ] < time()) {
  90. return $this->error('', '结束时间不能早于当前时间');
  91. }
  92. $activity_info = model('member_recommend')->getInfo([
  93. [ 'status', 'in', "0,1" ],
  94. [ 'site_id', '=', $data[ 'site_id' ] ],
  95. [ '', 'exp', Db::raw('not ( (`start_time` > ' . $data[ 'end_time' ] . ' and `start_time` > ' . $data[ 'start_time' ] . ' ) or (`end_time` < ' . $data[ 'start_time' ] . ' and `end_time` < ' . $data[ 'end_time' ] . '))') ],
  96. [ 'recommend_id', '<>', $recommend_id ],
  97. ], 'recommend_name,start_time,end_time');
  98. if (!empty($activity_info)) {
  99. return $this->error('', '此时间段已有同类型的活动');
  100. }
  101. $data[ 'update_time' ] = time();
  102. if ($data[ 'start_time' ] <= time()) {
  103. $data[ 'status' ] = 1;//直接启动
  104. } else {
  105. $data[ 'status' ] = 0;
  106. }
  107. model('member_recommend')->startTrans();
  108. try {
  109. model('member_recommend')->update($data, [ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'recommend_id', '=', $recommend_id ] ]);
  110. $cron = new Cron();
  111. $cron->deleteCron([ [ 'event', '=', 'CloseRecommend' ], [ 'relate_id', '=', $recommend_id ] ]);
  112. $cron->deleteCron([ [ 'event', '=', 'OpenRecommend' ], [ 'relate_id', '=', $recommend_id ] ]);
  113. if ($data[ 'start_time' ] <= time()) {
  114. $cron->addCron(1, 0, "邀请奖励关闭", "CloseRecommend", $data[ 'end_time' ], $recommend_id);
  115. } else {
  116. $cron->addCron(1, 0, "邀请奖励开启", "OpenRecommend", $data[ 'start_time' ], $recommend_id);
  117. $cron->addCron(1, 0, "邀请奖励关闭", "CloseRecommend", $data[ 'end_time' ], $recommend_id);
  118. }
  119. model('member_recommend')->commit();
  120. return $this->success();
  121. } catch (\Exception $e) {
  122. model('member_recommend')->rollback();
  123. return $this->error('', $e->getMessage());
  124. }
  125. }
  126. /**
  127. * 删除邀请奖励
  128. * @param $recommend_id
  129. * @param $site_id
  130. * @return array
  131. */
  132. public function deleteRecommend($recommend_id, $site_id)
  133. {
  134. $condition = [
  135. [ 'recommend_id', '=', $recommend_id ],
  136. [ 'site_id', '=', $site_id ]
  137. ];
  138. $res = model('member_recommend')->delete($condition);
  139. $cron = new Cron();
  140. $cron->deleteCron([ [ 'event', '=', 'OpenRecommend' ], [ 'relate_id', '=', $recommend_id ] ]);
  141. $cron->deleteCron([ [ 'event', '=', 'CloseRecommend' ], [ 'relate_id', '=', $recommend_id ] ]);
  142. return $this->success($res);
  143. }
  144. /**
  145. * 活动信息
  146. * @param $condition
  147. * @param string $field
  148. * @return array
  149. */
  150. public function getRecommendInfo($condition, $field = '*')
  151. {
  152. $res = model('member_recommend')->getInfo($condition, $field);
  153. return $this->success($res);
  154. }
  155. /**
  156. * 活动详情
  157. * @param $recommend_id
  158. * @param $site_id
  159. * @param string $field
  160. * @return array
  161. */
  162. public function getRecommendDetail($recommend_id, $site_id, $field = '*')
  163. {
  164. $res = model('member_recommend')->getInfo([ [ 'recommend_id', '=', $recommend_id ], [ 'site_id', '=', $site_id ] ], $field);
  165. if (!empty($res)) {
  166. $res[ 'type' ] = explode(',', $res[ 'type' ]);
  167. //获取优惠券信息
  168. if (isset($res[ 'coupon' ]) && !empty($res[ 'coupon' ])) {
  169. //优惠券字段
  170. $coupon_field = 'coupon_type_id,type,coupon_name,image,money,discount,validity_type,fixed_term,status,is_limit,at_least,count,lead_count,end_time,goods_type,max_fetch';
  171. $model = new CouponType();
  172. $coupon = $model->getCouponTypeList([ [ 'coupon_type_id', 'in', $res[ 'coupon' ] ] ], $coupon_field);
  173. $res[ 'coupon_list' ] = $coupon[ 'data' ];
  174. }
  175. }
  176. return $this->success($res);
  177. }
  178. /**
  179. * 活动列表
  180. * @param array $condition
  181. * @param string $field
  182. * @param string $order
  183. * @param null $limit
  184. * @return array
  185. */
  186. public function getRecommendList($condition = [], $field = '*', $order = 'recommend_id desc', $limit = null)
  187. {
  188. $list = model('member_recommend')->getList($condition, $field, $order, '', '', '', $limit);
  189. return $this->success($list);
  190. }
  191. /**
  192. * 活动分页列表
  193. * @param array $condition
  194. * @param int $page
  195. * @param int $page_size
  196. * @param string $order
  197. * @param string $field
  198. * @return array
  199. */
  200. public function getRecommendPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  201. {
  202. $list = model('member_recommend')->pageList($condition, $field, $order, $page, $page_size);
  203. return $this->success($list);
  204. }
  205. /**
  206. * 定时开启活动
  207. * @param $recommend_id
  208. * @return array
  209. */
  210. public function cronOpenRecommend($recommend_id)
  211. {
  212. $info = model('member_recommend')->getInfo([ [ 'recommend_id', '=', $recommend_id ] ], 'start_time,status');
  213. if (!empty($info)) {
  214. if ($info[ 'start_time' ] <= time() && $info[ 'status' ] == 0) {
  215. model('member_recommend')->startTrans();
  216. try {
  217. model('member_recommend')->update([ 'status' => 1 ], [ [ 'recommend_id', '=', $recommend_id ] ]);
  218. model('member_recommend')->commit();
  219. return $this->success();
  220. } catch (\Exception $e) {
  221. model('member_recommend')->rollback();
  222. return $this->error('', $e->getMessage());
  223. }
  224. } else {
  225. return $this->error("", "邀请奖励活动已开启或者关闭");
  226. }
  227. } else {
  228. return $this->error("", "邀请奖励活动不存在");
  229. }
  230. }
  231. /**
  232. * 定时关闭活动
  233. * @param $recommend_id
  234. * @return array
  235. */
  236. public function cronCloseRecommend($recommend_id)
  237. {
  238. $info = model('member_recommend')->getInfo([ [ 'recommend_id', '=', $recommend_id ] ], 'status');
  239. if (!empty($info)) {
  240. if ($info[ 'status' ] == 1) {
  241. model('member_recommend')->startTrans();
  242. try {
  243. model('member_recommend')->update([ 'status' => 2 ], [ [ 'recommend_id', '=', $recommend_id ] ]);
  244. model('member_recommend')->commit();
  245. return $this->success();
  246. } catch (\Exception $e) {
  247. model('member_recommend')->rollback();
  248. return $this->error('', $e->getMessage());
  249. }
  250. } else {
  251. return $this->error("", "邀请奖励活动已关闭");
  252. }
  253. } else {
  254. return $this->error("", "邀请奖励活动不存在");
  255. }
  256. }
  257. /**
  258. * 关闭活动
  259. * @param $recommend_id
  260. * @param $site_id
  261. * @return array
  262. */
  263. public function closeRecommend($recommend_id, $site_id)
  264. {
  265. $condition = array (
  266. [ 'recommend_id', '=', $recommend_id ],
  267. [ 'site_id', "=", $site_id ]
  268. );
  269. $info = model('member_recommend')->getInfo($condition, 'start_time,end_time,status');
  270. if (!empty($info)) {
  271. if ($info[ 'status' ] == 1) {
  272. model('member_recommend')->startTrans();
  273. try {
  274. model('member_recommend')->update([ 'status' => -1 ], [ [ 'recommend_id', '=', $recommend_id ], [ 'site_id', "=", $site_id ] ]);
  275. model('member_recommend')->commit();
  276. return $this->success();
  277. } catch (\Exception $e) {
  278. model('member_recommend')->rollback();
  279. return $this->error('', $e->getMessage());
  280. }
  281. } else {
  282. return $this->error("", "邀请奖励活动已关闭");
  283. }
  284. } else {
  285. return $this->error("", "邀请奖励活动不存在");
  286. }
  287. }
  288. /**
  289. * 活动奖励信息
  290. * @param $condition
  291. * @param string $field
  292. * @return array
  293. */
  294. public function getRecommendAwardInfo($condition, $field = '*')
  295. {
  296. $res = model('member_recommend_award')->getInfo($condition, $field);
  297. return $this->success($res);
  298. }
  299. /**
  300. * 查询数量
  301. * @param $condition
  302. * @param string $field
  303. * @return array
  304. */
  305. public function getRecommendAwardCount($condition, $field = 'award_id')
  306. {
  307. $count = model('member_recommend_award')->getCount($condition, $field);
  308. return $this->success($count);
  309. }
  310. /**
  311. * 活动奖励详情
  312. * @param $award_id
  313. * @param $site_id
  314. * @param string $field
  315. * @return array
  316. */
  317. public function getRecommendAwardDetail($award_id, $site_id, $field = '*')
  318. {
  319. $res = model('member_recommend_award')->getInfo([ [ 'award_id', '=', $award_id ], [ 'site_id', '=', $site_id ] ], $field);
  320. if (!empty($res)) {
  321. if (!empty($res[ 'coupon' ])) {
  322. $coupon_list = model('promotion_coupon_type')->getList([ [ 'coupon_type_id', 'in', $res[ 'coupon' ] ] ]);
  323. $res[ 'coupon_list' ] = $coupon_list;
  324. }
  325. }
  326. return $this->success($res);
  327. }
  328. /**
  329. * 活动奖励列表
  330. * @param array $condition
  331. * @param string $field
  332. * @param string $order
  333. * @param null $limit
  334. * @return array
  335. */
  336. public function getRecommendAwardList($condition = [], $field = '*', $order = 'award_id desc', $limit = null)
  337. {
  338. $list = model('member_recommend_award')->getList($condition, $field, $order, '', '', '', $limit);
  339. return $this->success($list);
  340. }
  341. /**
  342. * 活动奖励分页列表
  343. * @param array $condition
  344. * @param int $page
  345. * @param int $page_size
  346. * @param string $order
  347. * @param string $field
  348. * @return array
  349. */
  350. public function getRecommendAwardPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'create_time desc', $field = '*')
  351. {
  352. $list = model('member_recommend_award')->pageList($condition, $field, $order, $page, $page_size);
  353. if (!empty($list[ 'list' ])) {
  354. foreach ($list[ 'list' ] as $key => $value) {
  355. $list[ 'list' ][ $key ][ 'coupon_list' ] = [];
  356. if (!empty($value[ 'coupon' ])) {
  357. $coupon_list = model('promotion_coupon_type')->getList([ [ 'coupon_type_id', 'in', $value[ 'coupon' ] ] ]);
  358. $list[ 'list' ][ $key ][ 'coupon_list' ] = $coupon_list;
  359. }
  360. $member_info = model('member')->getInfo([ 'member_id' => $value[ 'source_member' ] ], 'headimg,nickname');
  361. $list[ 'list' ][ $key ][ 'headimg' ] = $member_info[ 'headimg' ];
  362. $list[ 'list' ][ $key ][ 'source_member_nickname' ] = $member_info[ 'nickname' ];
  363. }
  364. }
  365. return $this->success($list);
  366. }
  367. /**
  368. * 发放奖励
  369. * @param $data
  370. * @return array
  371. */
  372. public function receiveAward($data)
  373. {
  374. $member_info = model('member')->getInfo([ 'member_id' => $data[ 'member_id' ], 'site_id' => $data[ 'site_id' ] ], 'source_member,nickname');
  375. if (!empty($member_info[ 'source_member' ])) {
  376. $info = model('member_recommend')->getInfo([
  377. [ 'status', '=', "1" ],
  378. [ 'site_id', '=', $data[ 'site_id' ] ]
  379. ], '*');
  380. if (!empty($info)) {
  381. //判断领取次数
  382. $count = model('member_recommend_award')->getCount([ 'recommend_id' => $info[ 'recommend_id' ], 'member_id' => $member_info[ 'source_member' ] ], 'award_id');
  383. if ($info[ 'max_fetch' ] == 0 || $count < $info[ 'max_fetch' ]) {
  384. $type = explode(',', $info[ 'type' ]);
  385. //邀请人信息
  386. $source_member_info = model('member')->getInfo([ 'member_id' => $member_info[ 'source_member' ], 'site_id' => $data[ 'site_id' ] ], 'nickname');
  387. $member_account_model = new MemberAccount();
  388. model('member_recommend_award')->startTrans();
  389. try {
  390. $balance = 0;
  391. $point = 0;
  392. $coupon = '';
  393. $coupon_num = 0;
  394. //赠送红包
  395. if ($info[ 'balance' ] > 0 && in_array('balance', $type)) {
  396. $balance = $info[ 'balance' ];
  397. $member_account_model->addMemberAccount($data[ 'site_id' ], $member_info[ 'source_member' ], 'balance', $balance, 'memberrecommend', '邀请得红包' . $balance, '活动奖励发放');
  398. }
  399. //赠送积分
  400. if ($info[ 'point' ] > 0 && in_array('point', $type)) {
  401. $point = $info[ 'point' ];
  402. $member_account_model->addMemberAccount($data[ 'site_id' ], $member_info[ 'source_member' ], 'point', $point, 'memberrecommend', '邀请得积分' . $point, '活动奖励发放');
  403. }
  404. if (!empty($info[ 'coupon' ]) && in_array('coupon', $type)) {
  405. //给用户发放优惠券
  406. $coupon_model = new Coupon();
  407. $coupon_array = explode(',', $info[ 'coupon' ]);
  408. $coupon_array = array_map(function($value) {
  409. return [ 'coupon_type_id' => $value, 'num' => 1 ];
  410. }, $coupon_array);
  411. $res = $coupon_model->giveCoupon($coupon_array, $data[ 'site_id' ], $member_info[ 'source_member' ], 6);
  412. //更新已得优惠券数量
  413. if ($res[ 'code' ] >= 0) {
  414. $coupon_num += $res[ 'data' ];
  415. $coupon = $info[ 'coupon' ];
  416. }
  417. }
  418. //有一项不为0 添加邀请奖励
  419. if (!empty($point) || !empty($balance) || !empty($coupon)) {
  420. $data = [
  421. "site_id" => $data[ 'site_id' ],
  422. "recommend_id" => $info[ 'recommend_id' ],
  423. "recommend_name" => $info[ 'recommend_name' ],
  424. "member_id" => $member_info[ 'source_member' ],
  425. "member_nickname" => $source_member_info[ 'nickname' ],
  426. "source_member" => $data[ 'member_id' ],
  427. "source_member_nickname" => $member_info[ 'nickname' ],
  428. "create_time" => time(),
  429. "remark" => '邀请奖励',
  430. "point" => $point,
  431. "balance" => $balance,
  432. "coupon" => $coupon,
  433. "coupon_num" => $coupon_num,
  434. ];
  435. model('member_recommend_award')->add($data);
  436. }
  437. model('member_recommend_award')->commit();
  438. return $this->success();
  439. } catch (\Exception $e) {
  440. model('member_recommend_award')->rollback();
  441. return $this->error('', $e->getMessage());
  442. }
  443. }
  444. }
  445. }
  446. }
  447. /**
  448. * 获取最新一条信息
  449. * @param $site_id
  450. * @return array
  451. */
  452. public function getRecommendFirstData($site_id)
  453. {
  454. $condition = [
  455. [ 'site_id', '=', $site_id ],
  456. [ 'status', '=', 1 ],
  457. [ 'end_time', '>', time() ]
  458. ];
  459. $res = model('member_recommend')->getFirstData($condition, '*', 'create_time desc');
  460. if (!empty($res)) {
  461. //获取优惠券信息
  462. if (isset($res[ 'coupon' ]) && !empty($res[ 'coupon' ])) {
  463. //优惠券字段
  464. $coupon_field = 'coupon_type_id,coupon_name,money,count,lead_count,max_fetch,at_least,end_time,image,validity_type,fixed_term';
  465. $model = new CouponType();
  466. $coupon = $model->getCouponTypeList([ [ 'coupon_type_id', 'in', $res[ 'coupon' ] ] ], $coupon_field);
  467. $res[ 'coupon_list' ] = $coupon;
  468. }
  469. }
  470. return $this->success($res);
  471. }
  472. }