RedpackService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <?php
  2. namespace App\Services\Share;
  3. use App\Exceptions\AlertException;
  4. use App\Models\Fpdx\PairModel;
  5. use App\Models\Share\RedpackCheckModel;
  6. use App\Models\Share\RedpackListModel;
  7. use App\Models\User\UserModel;
  8. use App\Services\Pay\OrderService;
  9. use App\Services\Service;
  10. use Illuminate\Support\Collection;
  11. class RedpackService extends Service
  12. {
  13. private $fee = 2;
  14. /**
  15. * 滚动气泡
  16. */
  17. public function bannerlist()
  18. {
  19. $listModel = new RedpackListModel();
  20. $datas = $listModel->where('is_withdraw', 1)->take(10)->orderBy('updated_at', 'desc')->get(['uid', 'fee']);
  21. foreach ($datas as &$data) {
  22. $user = UserModel::findOrFail($data->uid);
  23. $data->nickname = $user->nickname;
  24. $data->headimgurl = $user->headimgurl;
  25. $data->sex = $user->sex;
  26. $data->address = $user->address;
  27. }
  28. return $datas;
  29. }
  30. /**
  31. * 创建现金红包
  32. * @param int $uid
  33. * @return bool
  34. * @throws AlertException
  35. */
  36. public function store(int $uid)
  37. {
  38. throw new AlertException("第一波红包活动已结束,敬请期待下一波。");
  39. $listModel = new RedpackListModel();
  40. if (
  41. $listModel->where([
  42. ['uid', $uid],
  43. ['expired_at', '>', time()],
  44. ['is_withdraw', 0],
  45. ])->OrWhere([
  46. ['uid', $uid],
  47. ['completeness', '>=', 100],
  48. ['is_withdraw', 0],
  49. ])->exists()
  50. ) {
  51. return false;
  52. } else {
  53. if (!PairModel::where('uid', $uid)->exists()) {
  54. throw new AlertException("( ̄▽ ̄)无权限生成新的红包,去首页玩玩吧", 404);
  55. }
  56. $total = \DB::table('kdgx_charge_redpack_list')->where([
  57. ['uid', $uid],
  58. ['is_withdraw', 1],
  59. ])->sum('fee');
  60. if ($total >= 6) {
  61. throw new AlertException('红包已达上限', 402);
  62. }
  63. $completeness = 60;
  64. $list = $listModel->fill([
  65. 'expired_at' => time() + 86400,
  66. 'completeness' => $completeness,
  67. 'uid' => $uid,
  68. 'fee' => $this->fee,
  69. ]);
  70. if ($list->save()) {
  71. \DB::table('kdgx_charge_redpack_check')->insert([
  72. 'created_at' => time(),
  73. 'updated_at' => time(),
  74. 'list_id' => $list->id,
  75. 'check_uid' => $uid,
  76. 'fee' => sprintf('%.2f', $this->fee * $completeness / 100),
  77. ]);
  78. return $list->toArray();
  79. } else {
  80. return false;
  81. }
  82. }
  83. }
  84. /**
  85. * 最后一个红包
  86. * @param int $uid
  87. * @return Collection
  88. */
  89. public function ingredpack(int $uid)
  90. {
  91. $redpackModel = new RedpackListModel();
  92. $data = $redpackModel->where('uid', $uid)->orderBy('id', 'desc')->first();
  93. if (collect($data)->isEmpty()) {
  94. return array();
  95. } else {
  96. return $this->get($data->id);
  97. }
  98. }
  99. /**
  100. * 获取正在进行的现金红包信息
  101. * @param int $id
  102. * @return array
  103. */
  104. public function get(int $id)
  105. {
  106. $redpackModel = new RedpackListModel();
  107. $redpack = $redpackModel->findOrFail($id);
  108. $redpack->get_fee = (string)sprintf('%.2f', $redpack->fee * $redpack->completeness / 100);
  109. $self = UserModel::findOrFail($redpack->uid);
  110. $redpack->self = array(
  111. 'headimgurl' => $self->headimgurl,
  112. 'nickname' => $self->nickname,
  113. );
  114. $checkModel = new RedpackCheckModel();
  115. $checks = $checkModel->where('list_id', $redpack->id)->get();
  116. $list = array();
  117. foreach ($checks as $tmp) {
  118. $user = UserModel::findOrFail($tmp->check_uid);
  119. $list[] = array(
  120. 'uid' => $tmp->check_uid,
  121. 'headimgurl' => $user->headimgurl,
  122. 'nickname' => $user->nickname,
  123. 'fee' => $tmp->fee,
  124. );
  125. }
  126. $redpack->list = $list;
  127. return $redpack->toArray();
  128. }
  129. /**
  130. * 新用户现金红包检验
  131. * @param int $uid
  132. * @param int $share_uid
  133. * @return bool
  134. * @throws AlertException
  135. * @throws \Throwable
  136. */
  137. public function check(int $uid, int $share_uid)
  138. {
  139. $listModel = new RedpackListModel();
  140. $data = $listModel->where([
  141. ['uid', $share_uid],
  142. ['completeness', '<', 100],
  143. ['expired_at', '>', time()],
  144. ])->first();
  145. if (collect($data)->isEmpty()) {
  146. return false;
  147. } else {
  148. $data = $this->pcheck($uid, $data->id);
  149. return $data['get_fee'];
  150. }
  151. }
  152. /**
  153. * 新用户现金红包检验
  154. * @param int $uid
  155. * @param int $redpack_id
  156. * @return bool
  157. * @throws AlertException
  158. * @throws \Throwable
  159. */
  160. public function pcheck(int $uid, int $redpack_id)
  161. {
  162. $user = UserModel::findOrFail($uid);
  163. if ((int)$user->toArray()['created_at'] < time() - 300) {
  164. throw new AlertException("老朋友,欢迎回来", 201);
  165. }
  166. $listModel = new RedpackListModel();
  167. $ls = $listModel->where('uid', $uid)->get();
  168. $checkModel = new RedpackCheckModel();
  169. if (
  170. $checkModel->where([
  171. ['check_uid', $user->uid],
  172. ])->whereNotIn('list_id', $ls->pluck('id'))->exists()
  173. ) {
  174. throw new AlertException("老朋友,欢迎回来!", 202);
  175. }
  176. $list = $listModel->findOrFail($redpack_id);
  177. if ($list->expired_at < time() || $list->completeness >= 100) {
  178. throw new AlertException("分享已经失效", 203);
  179. }
  180. $cnt = $checkModel->where('list_id', $list->id)->count();
  181. \DB::beginTransaction();
  182. try {
  183. switch ($cnt) {
  184. case 0:
  185. $completeness = 60;
  186. break;
  187. case 1:
  188. $completeness = 15;
  189. break;
  190. case 3:
  191. case 2:
  192. $completeness = 10;
  193. break;
  194. case 4:
  195. $completeness = 5;
  196. break;
  197. default:
  198. $completeness = 0;
  199. break;
  200. }
  201. if (($list->completeness + $completeness) > 100) {
  202. $completeness = 100 - $list->completeness;
  203. }
  204. $get_fee = sprintf('%.2f', $list->fee * $completeness / 100);
  205. \DB::table('kdgx_charge_redpack_check')->insert([
  206. 'created_at' => time(),
  207. 'updated_at' => time(),
  208. 'list_id' => $redpack_id,
  209. 'check_uid' => $uid,
  210. 'fee' => $get_fee,
  211. ]);
  212. \DB::table('kdgx_charge_redpack_list')->where([
  213. ['id', $list->id],
  214. ['completeness', $list->completeness],
  215. ])->increment('completeness', $completeness);
  216. if (($list->completeness + $completeness) == 100 && $completeness != 0) {
  217. $self = UserModel::findOrFail($list->uid);
  218. if (!empty($self->phone)) {
  219. $total = \DB::table('kdgx_charge_redpack_list')->where([
  220. ['uid', $list->uid],
  221. ['is_withdraw', 1],
  222. ])->sum('fee');
  223. if ($total > 6) {
  224. throw new AlertException('提现申请已提交。超过提现额度,暂无法自主提现。系统会在2个工作日内陆续下发红包', 402);
  225. }
  226. \DB::table('kdgx_charge_redpack_list')->where([
  227. ['id', $list->id],
  228. ['is_withdraw', 0],
  229. ])->update(['is_withdraw' => 1]);
  230. $os = new OrderService();
  231. $os->payments($list->uid, $list->fee * 100, "分配对象现金红包提现");
  232. }
  233. }
  234. \DB::commit();
  235. return [
  236. 'get_fee' => $get_fee,
  237. 'user' => [
  238. 'nickname' => $self->nickname,
  239. ],
  240. ];
  241. } catch (\Exception $e) {
  242. \DB::rollBack();
  243. return false;
  244. }
  245. }
  246. /**
  247. * 切换通知状态
  248. * @param int $uid
  249. * @param int $list_id
  250. * @return bool
  251. * @throws AlertException
  252. */
  253. public function notice(int $uid, int $list_id)
  254. {
  255. $listModel = new RedpackListModel();
  256. $list = $listModel->findOrFail($list_id);
  257. if ($list->uid != $uid) {
  258. throw new AlertException("无权限", 403);
  259. }
  260. $list->notice = abs($list->notice - 1);
  261. if ($list->save()) {
  262. return true;
  263. } else {
  264. return false;
  265. }
  266. }
  267. /**
  268. * 已完成的现金红包
  269. * @param int $uid
  270. * @param array $pages
  271. * @return array
  272. */
  273. public function susslist(int $uid, array $pages): array
  274. {
  275. $listmodel = new RedpackListModel();
  276. $total = $listmodel->where([
  277. ['uid', $uid],
  278. ['completeness', 100],
  279. ])->count();
  280. $datas = $listmodel->where([
  281. ['uid', $uid],
  282. ['completeness', 100],
  283. ])->orderBy('id', 'desc')->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  284. $res = array();
  285. foreach ($datas as &$data) {
  286. $data->type = 4;
  287. $data->photo = 'https://oss.pocketuniversity.cn/media/2018-10-10/5bbd7c7ac8c90.png';
  288. $data->title = '现金红包分享';
  289. $data->desc = "分享成功后可到账{$data->fee}元";
  290. }
  291. return [
  292. 'page' => $pages['page'],
  293. 'total' => $total,
  294. 'limit' => $pages['limit'],
  295. 'list' => $res,
  296. ];
  297. }
  298. /**
  299. * 已完成的现金红包
  300. * @param int $uid
  301. * @param array $pages
  302. * @return array
  303. */
  304. public function faillist(int $uid, array $pages): array
  305. {
  306. $listmodel = new RedpackListModel();
  307. $total = $listmodel->where([
  308. ['uid', $uid],
  309. ['completeness', '<', 100],
  310. ['expired_at', '>', time()],
  311. ])->count();
  312. $datas = $listmodel->where([
  313. ['uid', $uid],
  314. ['completeness', '<', 100],
  315. ['expired_at', '>', time()],
  316. ])->orderBy('id', 'desc')->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  317. $res = array();
  318. foreach ($datas as &$data) {
  319. $data->type = 4;
  320. $data->photo = 'https://oss.pocketuniversity.cn/media/2018-10-10/5bbd7c7ac8c90.png';
  321. $data->title = '现金红包分享';
  322. $data->desc = "分享成功后可到账{$data->fee}元";
  323. }
  324. return [
  325. 'page' => $pages['page'],
  326. 'total' => $total,
  327. 'limit' => $pages['limit'],
  328. 'list' => $res,
  329. ];
  330. }
  331. /**
  332. * 现金红包提现
  333. * @param int $uid
  334. * @param int $list_id
  335. * @return bool
  336. * @throws AlertException
  337. */
  338. public function withdraw(int $uid, int $list_id)
  339. {
  340. $listmodel = new RedpackListModel();
  341. $list = $listmodel->findOrFail($list_id);
  342. if ($list->uid != $uid) {
  343. throw new AlertException('无权限', 403);
  344. }
  345. $user = UserModel::findOrFail($list->uid);
  346. if (empty($user->phone)) {
  347. throw new AlertException('请补全手机号', 201);
  348. }
  349. if ($list->completeness < 100) {
  350. throw new AlertException('分享任务未完成', 401);
  351. }
  352. if ($list->is_withdraw != 0) {
  353. throw new AlertException('红包已提现', 401);
  354. }
  355. $total = \DB::table('kdgx_charge_redpack_list')->where([
  356. ['uid', $list->uid],
  357. ['is_withdraw', 1],
  358. ])->sum('fee');
  359. if ($total > 6) {
  360. throw new AlertException('提现申请已提交。超过提现额度,暂无法自主提现。系统会在2个工作日内陆续下发红包', 402);
  361. }
  362. try {
  363. $os = new OrderService();
  364. $os->payments($list->uid, $list->fee * 100, "分配对象现金红包提现");
  365. \DB::table('kdgx_charge_redpack_list')->where([
  366. ['id', $list->id],
  367. ['is_withdraw', 0],
  368. ])->update(['is_withdraw' => 1]);
  369. return true;
  370. } catch (\Exception $e) {
  371. \DB::table('kdgx_charge_redpack_list')->where([
  372. ['id', $list->id],
  373. ['is_withdraw', 1],
  374. ])->update(['is_withdraw' => 0]);
  375. throw new AlertException($e->getMessage(), 500);
  376. }
  377. }
  378. }