PullNew.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\admin\controller\user;
  8. use app\admin\controller\AuthController;
  9. use crmeb\repositories\OrderRepository;
  10. use crmeb\repositories\ShortLetterRepositories;
  11. use crmeb\services\{ExpressService,
  12. JsonService,
  13. JsonService as Json,
  14. MiniProgramService,
  15. WechatService,
  16. FormBuilder as Form,
  17. CacheService,
  18. UtilService as Util};
  19. use app\admin\model\ump\StorePink;
  20. use crmeb\basic\BaseModel;
  21. use think\cache\driver\Redis;
  22. use think\facade\Route as Url;
  23. use crmeb\services\YLYService;
  24. use think\facade\Log;
  25. use think\facade\Validate;
  26. /**
  27. * 订单管理控制器 同一个订单表放在一个控制器
  28. * Class StoreOrder
  29. * @package app\admin\controller\store
  30. */
  31. class PullNew extends AuthController
  32. {
  33. /**
  34. * @return mixed
  35. */
  36. public function index()
  37. {
  38. return $this->fetch();
  39. }
  40. public function list()
  41. {
  42. $where = Util::getMore([
  43. ['page', 1],
  44. ['limit', 20],
  45. ]);
  46. return Json::successlayui(\app\admin\model\user\PullNew::list($where));
  47. }
  48. /**
  49. * 显示创建资源表单页.
  50. *
  51. * @return \think\Response
  52. */
  53. public function create($id = 0)
  54. {
  55. $f = [];
  56. $f[] = Form::input('title', '标题')->col(12);
  57. $f[] = Form::dateTime('add_time' ,'开始时间');
  58. $f[] = Form::dateTime('end_time', '结束时间');
  59. if ($id) $f[] = Form::hidden('id', $id);
  60. $form = Form::make_post_form('添加', $f, Url::buildUrl('save'));
  61. $this->assign(compact('form'));
  62. return $this->fetch('public/form-builder');
  63. }
  64. public function save()
  65. {
  66. $model = new \app\admin\model\user\PullNew();
  67. $data = Util::postMore([
  68. 'title',
  69. 'add_time',
  70. 'end_time',
  71. ]);
  72. $validate = Validate::rule([
  73. 'title' => 'require',
  74. 'add_time' => 'require',
  75. 'end_time' => 'require',
  76. ]);
  77. $validate->message([
  78. 'title.require' => '标题不能为空',
  79. 'add_time.require' => '请选择进场时间',
  80. 'end_time.require' => '请选择结束时间',
  81. ]);
  82. if (!$validate->check($data)) {
  83. return Json::fail($validate->getError());
  84. }
  85. $data['add_time'] = strtotime($data['add_time']);
  86. $data['end_time'] = strtotime($data['end_time']);
  87. $res = $model->save($data);
  88. if ($res) return Json::successful('添加成功');
  89. return Json::fail('添加失败');
  90. }
  91. public function ranking($id)
  92. {
  93. $this->assign('new_id', $id);
  94. return $this->fetch();
  95. }
  96. /**
  97. * 拉新排行
  98. * @param $new_id
  99. * @return void
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. */
  104. public function ranking_list($new_id)
  105. {
  106. $where = Util::getMore([
  107. ['page', 1],
  108. ['limit', 20],
  109. ]);
  110. $redis = new Redis();
  111. $redis->connect('127.0.0.1','6379', 3600);
  112. if (!$redis->get('Ranking_'.$new_id)){
  113. $new = \app\admin\model\user\PullNew::where('id', $new_id)->find();
  114. $user = \app\admin\model\user\User::select();
  115. $user_count = [];
  116. foreach ($user as $item){
  117. $uids = get_downline_dow_time($user, $item['uid'], $new['add_time'], $new['end_time']);
  118. if (count($uids) > 0){
  119. $user_count[] = ['uid' => $item['uid'],'nickname' => $item['nickname'], 'count' => count($uids), 'push' => \app\admin\model\user\User::where('spread_uid', $item['uid'])->whereBetweenTime('add_time', $new['add_time'], $new['end_time'])->count()];
  120. }
  121. }
  122. }else{
  123. $user_count = $redis->get('Ranking');
  124. }
  125. $count = array_column($user_count, 'count');
  126. array_multisort($count, SORT_DESC, $user_count);
  127. $redis->set('Ranking_'.$new_id, $user_count);
  128. $data['count'] = count($user_count);
  129. $start=($where['page']-1)*$where['limit'];//偏移量,当前页-1乘以每页显示条数
  130. $data['data'] = array_slice($user_count,$start,$where['limit']);
  131. return Json::successlayui($data);
  132. }
  133. /**
  134. * 删除
  135. * @param $id
  136. * @return void
  137. * @throws \Exception
  138. */
  139. public function delete($id)
  140. {
  141. if (!$id) Json::fail('删除失败');
  142. $model = new \app\admin\model\user\PullNew();
  143. $res = \app\admin\model\user\PullNew::destroy($id);
  144. if ($res){
  145. return Json::success('删除成功!');
  146. }else{
  147. return Json::fail($model->getErrorInfo());
  148. }
  149. }
  150. }