CardController.php 29 KB

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