FlowerService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace App\Services\Share;
  3. use App\Exceptions\AlertException;
  4. use App\Models\Share\FlowerCheckModel;
  5. use App\Models\Share\FlowerListModel;
  6. use App\Models\User\UserModel;
  7. use App\Services\Service;
  8. class FlowerService extends Service
  9. {
  10. private $fee = 2;
  11. public function latest(int $uid)
  12. {
  13. $listmodel = new FlowerListModel();
  14. $data = $listmodel->where('uid', $uid)->orderBy('id', 'desc')->first();
  15. if (collect($data)->isEmpty()) {
  16. return array();
  17. } else {
  18. return $this->get($data->id);
  19. }
  20. }
  21. /**
  22. * 获取小花分享信息
  23. * @param int $list_id
  24. * @return array
  25. */
  26. public function get(int $list_id): array
  27. {
  28. $listmodel = new FlowerListModel();
  29. $data = $listmodel->findOrFail($list_id);
  30. $data->get_fee = (string)sprintf('%.2f', $data->fee * $data->completeness / 100);
  31. $self = UserModel::findOrFail($data->uid);
  32. $data->self = array(
  33. 'headimgurl' => $self->headimgurl,
  34. 'nickname' => $self->nickname,
  35. );
  36. $checkModel = new FlowerCheckModel();
  37. $checks = $checkModel->where('list_id', $data->id)->get();
  38. $list = array();
  39. foreach ($checks as $tmp) {
  40. $user = UserModel::findOrFail($tmp->check_uid);
  41. $list[] = array(
  42. 'uid' => $tmp->check_uid,
  43. 'headimgurl' => $user->headimgurl,
  44. 'nickname' => $user->nickname,
  45. 'fee' => $tmp->fee,
  46. 'type' => 3,
  47. );
  48. }
  49. $data->list = $list;
  50. return $data->toArray();
  51. }
  52. /**
  53. * 滚动气泡
  54. */
  55. public function bannerlist()
  56. {
  57. $listModel = new FlowerListModel();
  58. $datas = $listModel->where('completeness', 100)->take(10)->orderBy('updated_at', 'desc')->get(['uid', 'fee']);
  59. foreach ($datas as &$data) {
  60. $user = UserModel::findOrFail($data->uid);
  61. $data->nickname = $user->nickname;
  62. $data->headimgurl = $user->headimgurl;
  63. $data->sex = $user->sex;
  64. $data->address = $user->address;
  65. }
  66. return $datas;
  67. }
  68. /**
  69. * 创建小fa红包
  70. * @param int $uid
  71. * @return bool|array
  72. */
  73. public function store(int $uid): bool
  74. {
  75. $listModel = new FlowerListModel();
  76. if (
  77. $listModel->where([
  78. ['uid', $uid],
  79. ['completeness', '<', 100],
  80. ['expired_at', '>', time()],
  81. ])->exists()
  82. ) {
  83. return false;
  84. } else {
  85. $completeness = 40;
  86. $list = $listModel->fill([
  87. 'expired_at' => time() + 86400,
  88. 'completeness' => $completeness,
  89. 'uid' => $uid,
  90. 'fee' => $this->fee,
  91. ]);
  92. if ($list->save()) {
  93. \DB::table('kdgx_charge_flower_check')->insert([
  94. 'created_at' => time(),
  95. 'updated_at' => time(),
  96. 'list_id' => $list->id,
  97. 'check_uid' => $uid,
  98. 'fee' => sprintf('%.2f', $this->fee * $completeness / 100),
  99. ]);
  100. return $list->toArray();
  101. } else {
  102. return false;
  103. }
  104. }
  105. }
  106. /**
  107. * 正在进行的小花红包
  108. * @param int $uid
  109. * @return array
  110. */
  111. public function ing(int $uid): array
  112. {
  113. $listmodel = new FlowerListModel();
  114. $data = $listmodel->where([
  115. ['uid', $uid],
  116. ['completeness', '<', 100],
  117. ['expired_at', '>', time()],
  118. ])->first();
  119. if (collect($data)->isEmpty()) {
  120. return array();
  121. } else {
  122. $res[] = array(
  123. 'id' => $data->id,
  124. 'expired_at' => $data->expired_at,
  125. 'completeness' => $data->completeness,
  126. 'photo' => 'https://oss.pocketuniversity.cn/media/2018-10-10/5bbd7c5d8090c.png',
  127. 'title' => '小fa分享',
  128. 'desc' => "分享成功后可到账{$data->fee}多小fa",
  129. 'type' => 3,
  130. );
  131. return $res;
  132. }
  133. }
  134. /**
  135. * 已完成的小花红包
  136. * @param int $uid
  137. * @param array $pages
  138. * @return array
  139. */
  140. public function susslist(int $uid, array $pages): array
  141. {
  142. $listmodel = new FlowerListModel();
  143. $total = $listmodel->where([
  144. ['uid', $uid],
  145. ['completeness', '>=', 100],
  146. ])->count();
  147. $datas = $listmodel->where([
  148. ['uid', $uid],
  149. ['completeness', '>=', 100],
  150. ])->orderBy('id', 'desc')->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  151. foreach ($datas as &$data) {
  152. $data->photo = 'https://oss.pocketuniversity.cn/media/2018-10-10/5bbd7c5d8090c.png';
  153. $data->type = 3;
  154. $data->title = '小fa分享';
  155. $data->desc = "分享成功后可到账{$data->fee}多小fa";
  156. }
  157. return [
  158. 'page' => $pages['page'],
  159. 'total' => $total,
  160. 'limit' => $pages['limit'],
  161. 'list' => $datas,
  162. ];
  163. }
  164. /**
  165. * 失效列表
  166. * @param int $uid
  167. * @param array $pages
  168. * @return array
  169. */
  170. public function faillist(int $uid, array $pages): array
  171. {
  172. $listmodel = new FlowerListModel();
  173. $total = $listmodel->where([
  174. ['uid', $uid],
  175. ['completeness', '<', 100],
  176. ['expired_at', '<', time()],
  177. ])->count();
  178. $datas = $listmodel->where([
  179. ['uid', $uid],
  180. ['completeness', '<', 100],
  181. ['expired_at', '<', time()],
  182. ])->orderBy('id', 'desc')->skip(($pages['page'] - 1) * $pages['limit'])->take($pages['limit'])->get();
  183. foreach ($datas as &$data) {
  184. $data->photo = 'https://oss.pocketuniversity.cn/media/2018-10-10/5bbd7c5d8090c.png';
  185. $data->type = 3;
  186. $data->title = '小fa分享';
  187. $data->desc = "分享成功后可到账{$data->fee}多小fa";
  188. }
  189. return [
  190. 'page' => $pages['page'],
  191. 'total' => $total,
  192. 'limit' => $pages['limit'],
  193. 'list' => $datas,
  194. ];
  195. }
  196. /**
  197. * 分享助力核销
  198. * @param int $uid
  199. * @param int $list_id
  200. * @return string
  201. * @throws \Throwable
  202. */
  203. public function check(int $uid, int $list_id)
  204. {
  205. $listModel = new FlowerListModel();
  206. $list = $listModel->findOrFail($list_id);
  207. $rs = new RedpackService();
  208. try {
  209. $rs->check($uid, $list->uid);
  210. } catch (\Exception $e) {
  211. }
  212. if ($list->expired_at < time() || $list->completeness >= 100) {
  213. throw new AlertException("分享已经失效", 204);
  214. }
  215. $checkModel = new FlowerCheckModel();
  216. if (
  217. $checkModel->where([
  218. ['check_uid', $uid],
  219. ['list_id', $list_id],
  220. ])->exists()
  221. ) {
  222. throw new AlertException("你已经帮Ta助力过了哦", 202);
  223. }
  224. $ls = $listModel->where('uid', $uid)->get();
  225. if (
  226. $checkModel->where([
  227. ['check_uid', $uid],
  228. ['created_at', '>', mktime(0, 0, 0)],
  229. ])->whereNotIn('list_id', $ls->pluck('id'))->count() >= 2
  230. ) {
  231. throw new AlertException("今日助力次数已用完", 203);
  232. }
  233. $cnt = $checkModel->where('list_id', $list->id)->count();
  234. \DB::beginTransaction();
  235. try {
  236. switch ($cnt) {
  237. case 0:
  238. $completeness = 40;
  239. break;
  240. case 1:
  241. $completeness = 25;
  242. break;
  243. case 2:
  244. $completeness = 15;
  245. break;
  246. case 4:
  247. case 3:
  248. $completeness = 10;
  249. break;
  250. default:
  251. $completeness = 0;
  252. break;
  253. }
  254. if (($list->completeness + $completeness) > 100) {
  255. $completeness = 100 - $list->completeness;
  256. }
  257. $get = sprintf('%.2f', $list->fee * $completeness / 100);
  258. \DB::table('kdgx_charge_flower_check')->insert([
  259. 'created_at' => time(),
  260. 'updated_at' => time(),
  261. 'list_id' => $list->id,
  262. 'check_uid' => $uid,
  263. 'fee' => $get,
  264. ]);
  265. \DB::table('kdgx_charge_flower_list')->where([
  266. ['id', $list->id],
  267. ['completeness', $list->completeness],
  268. ])->increment('completeness', $completeness);
  269. $self = UserModel::findOrFail($list->uid);
  270. if (($list->completeness + $completeness) == 100) {
  271. \DB::table('kdgx_partner_charge_user')->where('uid', $list->uid)->increment('red_flower', $list->fee);
  272. \DB::table('kdgx_partner_charge_pay_logs')->insert([
  273. 'uid' => $list->uid,
  274. 'create_at' => time(),
  275. 'type' => 12,
  276. 'red_flower' => $list->fee,
  277. 'remark' => "助力红包",
  278. ]);
  279. }
  280. \DB::commit();
  281. return array(
  282. 'get_fee' => (string)$get,
  283. 'user' => [
  284. 'nickname' => $self->nickname,
  285. ],
  286. );
  287. } catch (\Exception $e) {
  288. \DB::rollBack();
  289. app('sentry')->captureException($e);
  290. return false;
  291. }
  292. }
  293. /**
  294. * 切换通知状态
  295. * @param int $uid
  296. * @param int $list_id
  297. * @return bool
  298. * @throws AlertException
  299. */
  300. public function notice(int $uid, int $list_id)
  301. {
  302. $listModel = new FlowerListModel();
  303. $list = $listModel->findOrFail($list_id);
  304. if ($list->uid != $uid) {
  305. throw new AlertException("无权限", 403);
  306. }
  307. $list->notice = abs($list->notice - 1);
  308. if ($list->save()) {
  309. return true;
  310. }
  311. return false;
  312. }
  313. }