CardController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. namespace app\admin\controller\finance;
  3. use app\model\Card;
  4. use app\model\CardMain;
  5. use app\model\Member;
  6. use app\model\MemberRole;
  7. use support\Db;
  8. use support\Request;
  9. class CardController
  10. {
  11. public function list(Request $request)
  12. {
  13. $page = $request->get('page', 1);
  14. $pageSize = $request->get('pageSize', 20);
  15. $times = $request->get('times', [date('Y-m-01 00:00:00'), date('Y-m-d 23:59:59')]);
  16. $times[0] = strtotime($times[0]);
  17. $times[1] = strtotime(date('Y-m-d 23:59:59', strtotime($times[1])));
  18. $name = $request->get('card_main_name');
  19. $cardMain = CardMain::when(!empty($name), function ($query) use ($name) {
  20. $query->where('card_main_name', 'like', '%' . $name . '%');
  21. })->get()
  22. ->toArray();
  23. $cardIds = array_column($cardMain, 'card_main_id');
  24. $cardAmount = array_column($cardMain, 'card_main_amount', 'card_main_id');
  25. $cardName = array_column($cardMain, 'card_main_name', 'card_main_id');
  26. $cardStatus = [
  27. 'issue' => 'ISSUE',
  28. 'add' => 'ADD',
  29. 'waiting' => 'WAITING',
  30. 'pending' => 'PENDING',
  31. 'used' => 'USED',
  32. 'done' => 'DONE',
  33. 'expired' => 'EXPIRED',
  34. 'paused' => 'PAUSED'
  35. ];
  36. $data = [];
  37. foreach ($cardStatus as $key => $status) {
  38. $cards = Card::when(!empty($cardIds), function ($query) use ($cardIds) {
  39. $query->whereIn('join_card_main_id', $cardIds);
  40. })->when($status == 'ISSUE', function ($query) use ($times) {
  41. $query->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.issue_datetime'))) as SIGNED) >= {$times[0]}")
  42. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.issue_datetime'))) as SIGNED) <= {$times[1]}")
  43. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP( JSON_UNQUOTE( JSON_EXTRACT( card_extend_json, '$.issue_datetime' )))),'%Y-%m-%d') AS group_by_time");
  44. })->when($status == 'ADD', function ($query) use ($times) {
  45. $query->whereBetween('card_addtimes', $times)
  46. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(card_addtimes),'%Y-%m-%d') AS group_by_time");
  47. })->when($status == 'WAITING', function ($query) use ($times) {
  48. $query->whereRaw("CAST(UNIX_TIMESTAMP(card_assign_datetime) as SIGNED) >= {$times[0]}")
  49. ->whereRaw("CAST(UNIX_TIMESTAMP(card_assign_datetime) as SIGNED) <= {$times[1]}")
  50. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(card_assign_datetime)),'%Y-%m-%d') AS group_by_time");
  51. })->when($status == 'PENDING', function ($query) use ($times) {
  52. $query->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.buy_datetime'))) as SIGNED) >= {$times[0]}")
  53. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.buy_datetime'))) as SIGNED) <= {$times[1]}")
  54. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP( JSON_UNQUOTE( JSON_EXTRACT( card_extend_json, '$.buy_datetime' )))),'%Y-%m-%d') AS group_by_time");
  55. })->when($status == 'USED', function ($query) use ($times) {
  56. $query->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.used_datetime'))) as SIGNED) >= {$times[0]}")
  57. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.used_datetime'))) as SIGNED) <= {$times[1]}")
  58. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP( JSON_UNQUOTE( JSON_EXTRACT( card_extend_json, '$.used_datetime' )))),'%Y-%m-%d') AS group_by_time");
  59. })->when($status == 'DONE', function ($query) use ($times) {
  60. $query->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.done_time'))) as SIGNED) >= {$times[0]}")
  61. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.done_time'))) as SIGNED) <= {$times[1]}")
  62. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP( JSON_UNQUOTE( JSON_EXTRACT( card_extend_json, '$.done_time' )))),'%Y-%m-%d') AS group_by_time");
  63. })->when($status == 'EXPIRED', function ($query) use ($times) {
  64. $query->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.expired_datetime'))) as SIGNED) >= {$times[0]}")
  65. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.expired_datetime'))) as SIGNED) <= {$times[1]}")
  66. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP( JSON_UNQUOTE( JSON_EXTRACT( card_extend_json, '$.expired_datetime' )))),'%Y-%m-%d') AS group_by_time");
  67. })->when($status == 'PAUSED', function ($query) use ($times) {
  68. $query->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.paused_datetime'))) as SIGNED) >= {$times[0]}")
  69. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.paused_datetime'))) as SIGNED) <= {$times[1]}")
  70. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP( JSON_UNQUOTE( JSON_EXTRACT( card_extend_json, '$.paused_datetime' )))),'%Y-%m-%d') AS group_by_time");
  71. })->selectRaw('count(card_id) as total,join_card_main_id')
  72. ->groupBy('join_card_main_id', 'group_by_time')
  73. ->get()
  74. ->toArray();
  75. $data[$key] = $cards;
  76. }
  77. $start = new \DateTime(date('Y-m-d', $times[0]));
  78. $end = new \DateTime(date('Y-m-d 23:59:59', $times[1]));
  79. // 创建一个 DateInterval,设置为一天
  80. $interval = new \DateInterval('P1D');
  81. // 创建一个日期范围对象,用于迭代所有日期
  82. $dateRange = new \DatePeriod($start, $interval, $end);
  83. $rows = [];
  84. $statistics = [
  85. 'issue' => ['total' => 0, 'amount' => 0],
  86. 'add' => ['total' => 0, 'amount' => 0],
  87. 'waiting' => ['total' => 0, 'amount' => 0],
  88. 'pending' => ['total' => 0, 'amount' => 0],
  89. 'used' => ['total' => 0, 'amount' => 0],
  90. 'done' => ['total' => 0, 'amount' => 0],
  91. 'expired' => ['total' => 0, 'amount' => 0],
  92. 'paused' => ['total' => 0, 'amount' => 0],
  93. ];
  94. foreach ($dateRange as $date) {
  95. $dayDate = $date->format('Y-m-d');
  96. foreach ($data as $key => $datum) {
  97. if (!empty($datum)) {
  98. foreach ($datum as $item) {
  99. if ($item['group_by_time'] == $dayDate) {
  100. $dateKey = $dayDate . '_' . $item['join_card_main_id'];
  101. $total = $item['total'];
  102. $statistics[$key]['total'] += $total;
  103. $mainAmount = $cardAmount[$item['join_card_main_id']] ?? 0;
  104. $amount = $total * $mainAmount;
  105. $statistics[$key]['amount'] += $amount;
  106. if (!empty($rows[$dateKey]) && !empty($rows[$dateKey][$key]) && !empty($rows[$dateKey][$key]['total'])) {
  107. $total = $rows[$dateKey][$key]['total'] + $total;
  108. $amount = $rows[$dateKey][$key]['amount'] + $amount;
  109. }
  110. $rows[$dateKey]['key'] = strtotime($dayDate) . '_' . $item['join_card_main_id'];
  111. $rows[$dateKey]['date'] = $dayDate;
  112. $rows[$dateKey]['card_main_id'] = $item['join_card_main_id'];
  113. $rows[$dateKey]['card_main_name'] = $cardName[$item['join_card_main_id']] ?? '';
  114. $rows[$dateKey]['card_main_amount'] = $mainAmount;
  115. $rows[$dateKey][$key] = [
  116. 'total' => $total,
  117. 'amount' => $amount
  118. ];
  119. }
  120. }
  121. }
  122. }
  123. }
  124. $rows = array_reverse($rows);
  125. $rowsCount = count($rows);
  126. $start = $pageSize * ($page - 1);
  127. $data = array_slice($rows, $start, $pageSize);
  128. return json_success('success', [
  129. 'total' => $rowsCount,
  130. 'rows' => array_values($data),
  131. 'page' => $page,
  132. 'pageSize' => $pageSize,
  133. 'statistics' => $statistics
  134. ]);
  135. }
  136. public function exportCard(Request $request)
  137. {
  138. $times = $request->get('times', [date('Y-m-01 00:00:00'), date('Y-m-d 23:59:59')]);
  139. $times[0] = strtotime($times[0]);
  140. $times[1] = strtotime(date('Y-m-d 23:59:59', strtotime($times[1])));
  141. $name = $request->get('card_main_name');
  142. $keys = $request->get('keys', []);
  143. $cardMain = CardMain::when(!empty($name), function ($query) use ($name) {
  144. $query->where('card_main_name', 'like', '%' . $name . '%');
  145. })->get()
  146. ->toArray();
  147. $cardIds = array_column($cardMain, 'card_main_id');
  148. $cardAmount = array_column($cardMain, 'card_main_amount', 'card_main_id');
  149. $cardName = array_column($cardMain, 'card_main_name', 'card_main_id');
  150. $cardStatus = [
  151. 'issue' => 'ISSUE',
  152. 'add' => 'ADD',
  153. 'waiting' => 'WAITING',
  154. 'pending' => 'PENDING',
  155. 'used' => 'USED',
  156. 'done' => 'DONE',
  157. 'expired' => 'EXPIRED',
  158. 'paused' => 'PAUSED'
  159. ];
  160. $data = [];
  161. foreach ($cardStatus as $key => $status) {
  162. $cards = Card::when(!empty($cardIds), function ($query) use ($cardIds) {
  163. $query->whereIn('join_card_main_id', $cardIds);
  164. })->when($status == 'ISSUE', function ($query) use ($times) {
  165. $query->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.issue_datetime'))) as SIGNED) >= {$times[0]}")
  166. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.issue_datetime'))) as SIGNED) <= {$times[1]}")
  167. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP( JSON_UNQUOTE( JSON_EXTRACT( card_extend_json, '$.issue_datetime' )))),'%Y-%m-%d') AS group_by_time");
  168. })->when($status == 'ADD', function ($query) use ($times) {
  169. $query->whereBetween('card_addtimes', $times)
  170. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(card_addtimes),'%Y-%m-%d') AS group_by_time");
  171. })->when($status == 'WAITING', function ($query) use ($times) {
  172. $query->whereRaw("CAST(UNIX_TIMESTAMP(card_assign_datetime) as SIGNED) >= {$times[0]}")
  173. ->whereRaw("CAST(UNIX_TIMESTAMP(card_assign_datetime) as SIGNED) <= {$times[1]}")
  174. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP(card_assign_datetime)),'%Y-%m-%d') AS group_by_time");
  175. })->when($status == 'PENDING', function ($query) use ($times) {
  176. $query->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.buy_datetime'))) as SIGNED) >= {$times[0]}")
  177. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.buy_datetime'))) as SIGNED) <= {$times[1]}")
  178. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP( JSON_UNQUOTE( JSON_EXTRACT( card_extend_json, '$.buy_datetime' )))),'%Y-%m-%d') AS group_by_time");
  179. })->when($status == 'USED', function ($query) use ($times) {
  180. $query->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.used_datetime'))) as SIGNED) >= {$times[0]}")
  181. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.used_datetime'))) as SIGNED) <= {$times[1]}")
  182. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP( JSON_UNQUOTE( JSON_EXTRACT( card_extend_json, '$.used_datetime' )))),'%Y-%m-%d') AS group_by_time");
  183. })->when($status == 'DONE', function ($query) use ($times) {
  184. $query->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.done_time'))) as SIGNED) >= {$times[0]}")
  185. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.done_time'))) as SIGNED) <= {$times[1]}")
  186. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP( JSON_UNQUOTE( JSON_EXTRACT( card_extend_json, '$.done_time' )))),'%Y-%m-%d') AS group_by_time");
  187. })->when($status == 'EXPIRED', function ($query) use ($times) {
  188. $query->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.expired_datetime'))) as SIGNED) >= {$times[0]}")
  189. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.expired_datetime'))) as SIGNED) <= {$times[1]}")
  190. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP( JSON_UNQUOTE( JSON_EXTRACT( card_extend_json, '$.expired_datetime' )))),'%Y-%m-%d') AS group_by_time");
  191. })->when($status == 'PAUSED', function ($query) use ($times) {
  192. $query->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.paused_datetime'))) as SIGNED) >= {$times[0]}")
  193. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(card_extend_json,'$.paused_datetime'))) as SIGNED) <= {$times[1]}")
  194. ->selectRaw("DATE_FORMAT(FROM_UNIXTIME(UNIX_TIMESTAMP( JSON_UNQUOTE( JSON_EXTRACT( card_extend_json, '$.paused_datetime' )))),'%Y-%m-%d') AS group_by_time");
  195. })->selectRaw('count(card_id) as total,join_card_main_id')
  196. ->groupBy('join_card_main_id', 'group_by_time')
  197. ->get()
  198. ->toArray();
  199. $data[$key] = $cards;
  200. }
  201. $start = new \DateTime(date('Y-m-d', $times[0]));
  202. $end = new \DateTime(date('Y-m-d 23:59:59', $times[1]));
  203. // 创建一个 DateInterval,设置为一天
  204. $interval = new \DateInterval('P1D');
  205. // 创建一个日期范围对象,用于迭代所有日期
  206. $dateRange = new \DatePeriod($start, $interval, $end);
  207. $rows = [];
  208. $statistics = [
  209. 'issue' => ['total' => 0, 'amount' => 0],
  210. 'add' => ['total' => 0, 'amount' => 0],
  211. 'waiting' => ['total' => 0, 'amount' => 0],
  212. 'pending' => ['total' => 0, 'amount' => 0],
  213. 'used' => ['total' => 0, 'amount' => 0],
  214. 'done' => ['total' => 0, 'amount' => 0],
  215. 'expired' => ['total' => 0, 'amount' => 0],
  216. 'paused' => ['total' => 0, 'amount' => 0],
  217. ];
  218. foreach ($dateRange as $date) {
  219. $dayDate = $date->format('Y-m-d');
  220. foreach ($data as $key => $datum) {
  221. if (!empty($datum)) {
  222. foreach ($datum as $item) {
  223. if ($item['group_by_time'] == $dayDate) {
  224. // 有选择,过滤
  225. if (!empty($keys) && !in_array(strtotime($dayDate) . '_' . $item['join_card_main_id'], $keys)) {
  226. continue;
  227. }
  228. $dateKey = $dayDate . '_' . $item['join_card_main_id'];
  229. $total = $item['total'];
  230. $statistics[$key]['total'] += $total;
  231. $mainAmount = $cardAmount[$item['join_card_main_id']] ?? 0;
  232. $amount = $total * $mainAmount;
  233. $statistics[$key]['amount'] += $amount;
  234. if (!empty($rows[$dateKey]) && !empty($rows[$dateKey][$key]) && !empty($rows[$dateKey][$key]['total'])) {
  235. $total = $rows[$dateKey][$key]['total'] + $total;
  236. $amount = $rows[$dateKey][$key]['amount'] + $amount;
  237. }
  238. $rows[$dateKey]['date'] = $dayDate;
  239. $rows[$dateKey]['card_main_id'] = $item['join_card_main_id'];
  240. $rows[$dateKey]['card_main_name'] = $cardName[$item['join_card_main_id']] ?? '';
  241. $rows[$dateKey]['card_main_amount'] = $mainAmount;
  242. $rows[$dateKey][$key] = [
  243. 'total' => $total,
  244. 'amount' => $amount
  245. ];
  246. }
  247. }
  248. }
  249. }
  250. }
  251. $rows = array_reverse($rows);
  252. $exportData = [];
  253. foreach ($rows as $row) {
  254. $exportData[] = [
  255. 'date' => $row['date'],
  256. 'card_main_name' => $row['card_main_name'],
  257. 'card_main_amount' => $row['card_main_amount'],
  258. 'issue' => isset($row['issue']) ? $row['issue']['total'] . '张 (¥' . sprintf('%.2f', $row['issue']['amount']) . ')' : '--',
  259. 'add' => isset($row['add']) ? $row['add']['total'] . '张 (¥' . sprintf('%.2f', $row['add']['amount']) . ')' : '--',
  260. 'waiting' => isset($row['waiting']) ? $row['waiting']['total'] . '张 (¥' . sprintf('%.2f', $row['waiting']['amount']) . ')' : '--',
  261. 'pending' => isset($row['pending']) ? $row['pending']['total'] . '张 (¥' . sprintf('%.2f', $row['pending']['amount']) . ')' : '--',
  262. 'used' => isset($row['used']) ? $row['used']['total'] . '张 (¥' . sprintf('%.2f', $row['used']['amount']) . ')' : '--',
  263. 'done' => isset($row['done']) ? $row['done']['total'] . '张 (¥' . sprintf('%.2f', $row['done']['amount']) . ')' : '--',
  264. 'expired' => isset($row['expired']) ? $row['expired']['total'] . '张 (¥' . sprintf('%.2f', $row['expired']['amount']) . ')' : '--',
  265. 'paused' => isset($row['paused']) ? $row['paused']['total'] . '张 (¥' . sprintf('%.2f', $row['paused']['amount']) . ')' : '--'
  266. ];
  267. }
  268. return json_success('success', $exportData);
  269. }
  270. public function listOld(Request $request)
  271. {
  272. $page = $request->get('page', 1);
  273. $pageSize = $request->get('pageSize', 20);
  274. $classify = $request->get('card_main_classify');
  275. $categoryId = $request->get('join_card_main_category_id');
  276. $name = $request->get('card_main_name');
  277. $addtimes = $request->get('card_main_addtimes');
  278. $cardMain = CardMain::when(!empty($classify), function ($query) use ($classify) {
  279. $query->where('card_main_classify', $classify);
  280. })->when(!empty($categoryId), function ($query) use ($categoryId) {
  281. $query->where('join_card_main_category_id', $categoryId);
  282. })->when(!empty($name), function ($query) use ($name) {
  283. $query->where('card_main_name', 'like', '%' . $name . '%');
  284. })->when(!empty($addtimes), function ($query) use ($addtimes) {
  285. $addtimes[0] = strtotime($addtimes[0]);
  286. $addtimes[1] = strtotime($addtimes[1]);
  287. $query->whereBetween('card_main_addtimes', $addtimes);
  288. });
  289. $total = $cardMain->count();
  290. $rows = $cardMain->select('card_main_id', 'card_main_amount', 'card_main_name')
  291. ->orderBy('card_main_addtimes', 'DESC')
  292. ->forPage($page, $pageSize)
  293. ->get()
  294. ->toArray();
  295. $statistics = [
  296. 'total' => ['total' => 0, 'amount' => 0],
  297. 'is_issue' => ['total' => 0, 'amount' => 0],
  298. 'init' => ['total' => 0, 'amount' => 0],
  299. 'waiting' => ['total' => 0, 'amount' => 0],
  300. 'pending' => ['total' => 0, 'amount' => 0],
  301. 'used' => ['total' => 0, 'amount' => 0],
  302. 'done' => ['total' => 0, 'amount' => 0],
  303. 'paused' => ['total' => 0, 'amount' => 0]
  304. ];
  305. foreach ($rows as &$row) {
  306. // 发行统计
  307. $isIssueTotal = Card::where('is_issue', 'Y')->where('join_card_main_id', $row['card_main_id'])->count();
  308. $row['is_issue']['total'] = $isIssueTotal;
  309. $row['is_issue']['amount'] = sprintf('%.2f', $isIssueTotal * $row['card_main_amount']);
  310. $statistics['is_issue']['total'] += $isIssueTotal;
  311. $statistics['is_issue']['amount'] = sprintf('%.2f', $isIssueTotal * $row['card_main_amount'] + $statistics['is_issue']['amount']);
  312. // 待分配统计
  313. $initTotal = Card::where('card_status', 'INIT')->where('join_card_main_id', $row['card_main_id'])->count();
  314. $row['init']['total'] = $initTotal;
  315. $row['init']['amount'] = sprintf('%.2f', $initTotal * $row['card_main_amount']);
  316. $statistics['init']['total'] += $initTotal;
  317. $statistics['init']['amount'] = sprintf('%.2f', $initTotal * $row['card_main_amount'] + $statistics['init']['amount']);
  318. // 已分配,待售统计
  319. $waitingTotal = Card::where('card_status', 'WAITING')->where('join_card_main_id', $row['card_main_id'])->count();
  320. $row['waiting']['total'] = $waitingTotal;
  321. $row['waiting']['amount'] = sprintf('%.2f', $waitingTotal * $row['card_main_amount']);
  322. $statistics['waiting']['total'] += $waitingTotal;
  323. $statistics['waiting']['amount'] = sprintf('%.2f', $waitingTotal * $row['card_main_amount'] + $statistics['waiting']['amount']);
  324. // 已售,待激活统计
  325. $pendingTotal = Card::where('card_status', 'PENDING')->where('join_card_main_id', $row['card_main_id'])->count();
  326. $row['pending']['total'] = $pendingTotal;
  327. $row['pending']['amount'] = sprintf('%.2f', $pendingTotal * $row['card_main_amount']);
  328. $statistics['pending']['total'] += $pendingTotal;
  329. $statistics['pending']['amount'] = sprintf('%.2f', $pendingTotal * $row['card_main_amount'] + $statistics['pending']['amount']);
  330. // 已激活统计
  331. $usedTotal = Card::where('card_status', 'USED')->where('join_card_main_id', $row['card_main_id'])->count();
  332. $row['used']['total'] = $usedTotal;
  333. $row['used']['amount'] = sprintf('%.2f', $usedTotal * $row['card_main_amount']);
  334. $statistics['used']['total'] += $usedTotal;
  335. $statistics['used']['amount'] = sprintf('%.2f', $usedTotal * $row['card_main_amount'] + $statistics['used']['amount']);
  336. // 使用完成统计
  337. $doneTotal = Card::where('card_status', 'DONE')->where('join_card_main_id', $row['card_main_id'])->count();
  338. $row['done']['total'] = $doneTotal;
  339. $row['done']['amount'] = sprintf('%.2f', $doneTotal * $row['card_main_amount']);
  340. $statistics['done']['total'] += $doneTotal;
  341. $statistics['done']['amount'] = sprintf('%.2f', $doneTotal * $row['card_main_amount'] + $statistics['done']['amount']);
  342. // 冻结统计
  343. $pausedTotal = Card::where('card_status', 'PAUSED')->where('join_card_main_id', $row['card_main_id'])->count();
  344. $row['paused']['total'] = $pausedTotal;
  345. $row['paused']['amount'] = sprintf('%.2f', $pausedTotal * $row['card_main_amount']);
  346. $statistics['paused']['total'] += $pausedTotal;
  347. $statistics['paused']['amount'] = sprintf('%.2f', $pausedTotal * $row['card_main_amount'] + $statistics['paused']['total']);
  348. }
  349. $statistics['total']['total'] = $statistics['init']['total'] + $statistics['waiting']['total']
  350. + $statistics['pending']['total'] + $statistics['used']['total'] + $statistics['done']['total']
  351. + $statistics['paused']['total'];
  352. $statistics['total']['amount'] = sprintf('%.2f', $statistics['init']['amount']
  353. + $statistics['waiting']['amount'] + $statistics['pending']['amount'] + $statistics['used']['amount']
  354. + $statistics['done']['amount'] + $statistics['paused']['amount']);
  355. return json_success('success', compact('rows', 'page', 'pageSize', 'total', 'statistics'));
  356. }
  357. /**
  358. * @Desc 导出
  359. * @Author Gorden
  360. * @Date 2024/11/19 8:56
  361. *
  362. * @param Request $request
  363. * @return \support\Response
  364. */
  365. public function exportMemberAccount(Request $request)
  366. {
  367. $days = $request->get('member_addtimes', []);
  368. $level = $request->get('level', '');
  369. $memberId = $request->get('member_id');
  370. $accountType = $request->get('account_type', 'CASH');
  371. $premisesId = intval($request->get('premises_id', ''));
  372. $memberAccountIds = $request->get('member_account_ids');
  373. if (!empty($days)) {
  374. $days[0] = strtotime($days[0]);
  375. $days[1] = strtotime($days[1]);
  376. $month = date('Ym', $days[0]);
  377. $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
  378. } else {
  379. $month = date('Ym');
  380. }
  381. // 兼容老等级搜索
  382. $levelIds = [];
  383. if (!empty($level)) {
  384. $levelName = MemberRole::where('member_role_id', $level)->value('member_role_name');
  385. if (!empty($levelName)) {
  386. $levelIds = MemberRole::where('member_role_name', $levelName)->pluck('member_role_id')->toArray();
  387. }
  388. }
  389. $rows = Member::with([
  390. 'cert' => function ($query) {
  391. $query->select('join_cert_member_id', 'member_cert_name');
  392. },
  393. 'info' => function ($query) {
  394. $query->select('join_info_member_id', 'member_info_nickname');
  395. },
  396. 'role' => function ($query) {
  397. $query->select('member_role_id', 'member_role_name');
  398. }
  399. ])->join('member_account as ma', function ($join) use ($accountType) {
  400. $join->on('member.member_id', '=', 'ma.join_account_member_id')->where('ma.member_account_classify', '=', $accountType);
  401. })->leftJoin('member_role', 'member_role.member_role_id', 'member.join_member_role_id')
  402. ->when(!empty($level), function ($query) use ($level, $levelIds) {
  403. if ($level == '00') {
  404. $query->where('join_member_role_id', '')->orWhere('join_member_role_id', NULL);
  405. } else if ($level == 'VIP') {
  406. $query->where('member.member_is_vip', 'Y');
  407. } else if (!empty($levelIds)) {
  408. $query->whereIn('member_role.member_role_id', $levelIds);
  409. } else {
  410. $query->where('join_member_role_id', $level);
  411. }
  412. })->when(!empty($memberId), function ($query) use ($memberId) {
  413. $query->where('member.member_id', $memberId);
  414. })->when(!empty($days), function ($query) use ($days) {
  415. $query->whereBetween('member.member_addtimes', $days);
  416. })->when(!empty($premisesId), function ($query) use ($premisesId) {
  417. $query->whereJsonContains('member.member_extend_json->belong->premises', $premisesId);
  418. })->when(!empty($memberAccountIds), function ($query) use ($memberAccountIds) {
  419. $query->whereIn('member_account_id', $memberAccountIds);
  420. })->where('member.member_mobile', '<>', '0000')
  421. ->select('member.member_id', 'member.member_mobile', 'member.member_addtimes', 'member.join_member_role_id',
  422. 'ma.member_account_id as member_account_id', 'ma.member_account_income as member_account_income', 'ma.member_account_expend as member_account_expend', 'ma.member_account_surplus as member_account_surplus', 'ma.member_account_added as member_account_added'
  423. )
  424. ->orderBy('member.member_addtimes', 'DESC')
  425. ->get()
  426. ->toArray();
  427. $data = [];
  428. foreach ($rows as $row) {
  429. $data[] = [
  430. 'nickname' => $row['info']['member_info_nickname'] ?? '--',
  431. 'mobile' => $row['member_mobile'] ?? '--',
  432. 'role_name' => $row['role']['member_role_name'] ?? '--',
  433. 'balance' => sprintf('%.2f', $row['member_account_surplus'] + $row['member_account_added']),
  434. 'income' => $row['member_account_income'],
  435. 'expend' => $row['member_account_expend'],
  436. 'added' => $row['member_account_added'],
  437. 'addtimes' => $row['member_addtimes']
  438. ];
  439. }
  440. return json_success('success', $data);
  441. }
  442. }