MemberAccountController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace app\admin\controller\finance;
  3. use app\model\Member;
  4. use app\model\MemberRole;
  5. use support\Db;
  6. use support\Request;
  7. class MemberAccountController
  8. {
  9. public function list(Request $request)
  10. {
  11. $page = $request->get('page', 1);
  12. $pageSize = $request->get('pageSize', 20);
  13. // $classify = $request->get('classify', 'CASH');
  14. $days = $request->get('member_addtimes', []);
  15. $level = $request->get('level', '');
  16. $memberId = $request->get('member_id');
  17. $accountType = $request->get('account_type', 'CASH');
  18. $premisesId = intval($request->get('premises_id', ''));
  19. if (!empty($days)) {
  20. $days[0] = strtotime($days[0]);
  21. $days[1] = strtotime($days[1]);
  22. $month = date('Ym', $days[0]);
  23. $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
  24. } else {
  25. $month = date('Ym');
  26. }
  27. // 兼容老等级搜索
  28. $levelIds = [];
  29. if (!empty($level)) {
  30. $levelName = MemberRole::where('member_role_id', $level)->value('member_role_name');
  31. if (!empty($levelName)) {
  32. $levelIds = MemberRole::where('member_role_name', $levelName)->pluck('member_role_id')->toArray();
  33. }
  34. }
  35. $rows = Member::with([
  36. 'cert' => function ($query) {
  37. $query->select('join_cert_member_id', 'member_cert_name');
  38. },
  39. 'info' => function ($query) {
  40. $query->select('join_info_member_id', 'member_info_nickname');
  41. },
  42. 'role' => function ($query) {
  43. $query->select('member_role_id', 'member_role_name');
  44. }
  45. ])->join('member_account as ma', function ($join) use ($accountType) {
  46. $join->on('member.member_id', '=', 'ma.join_account_member_id')->where('ma.member_account_classify', '=', $accountType);
  47. })->leftJoin('member_role', 'member_role.member_role_id', 'member.join_member_role_id')
  48. ->when(!empty($level), function ($query) use ($level, $levelIds) {
  49. if ($level == '00') {
  50. $query->where('join_member_role_id', '')->orWhere('join_member_role_id', NULL);
  51. } else if ($level == 'VIP') {
  52. $query->where('member.member_is_vip', 'Y');
  53. } else if (!empty($levelIds)) {
  54. $query->whereIn('member_role.member_role_id', $levelIds);
  55. } else {
  56. $query->where('join_member_role_id', $level);
  57. }
  58. })->when(!empty($memberId), function ($query) use ($memberId) {
  59. $query->where('member.member_id', $memberId);
  60. })->when(!empty($days), function ($query) use ($days) {
  61. $query->whereBetween('member.member_addtimes', $days);
  62. })->when(!empty($premisesId), function ($query) use ($premisesId) {
  63. $query->whereJsonContains('member.member_extend_json->belong->premises', $premisesId);
  64. })->where('member.member_mobile', '<>', '0000');
  65. $memberTotalModel = clone $rows;
  66. $statistics['memberTotal'] = $memberTotalModel->distinct('ma.join_account_member_id')->count();
  67. $total = $rows->count('member_id');
  68. $statistics['surplus'] = $rows->sum('ma.member_account_surplus');
  69. $statistics['income'] = $rows->sum('ma.member_account_income');
  70. $statistics['expend'] = $rows->sum('member_account_expend');
  71. $statistics['added'] = $rows->sum('member_account_added');
  72. // $statistics['memberTotal'] = Member::where('member_mobile', '<>', '0000')
  73. // ->when(!empty($level), function ($query) use ($level, $levelIds) {
  74. // if ($level == '00') {
  75. // $query->where('join_member_role_id', '')->orWhere('join_member_role_id', NULL);
  76. // } else if ($level == 'VIP') {
  77. // $query->where('member_is_vip', 'Y');
  78. // } else if (!empty($levelIds)) {
  79. // $query->whereIn('member_role_id', $levelIds);
  80. // } else {
  81. // $query->where('join_member_role_id', $level);
  82. // }
  83. //
  84. // })->when(!empty($memberId), function ($query) use ($memberId) {
  85. // $query->where('member_id', $memberId);
  86. // })->when(!empty($days), function ($query) use ($days) {
  87. // $query->whereBetween('member_addtimes', $days);
  88. // })->when(!empty($premisesId), function ($query) use ($premisesId) {
  89. // $query->whereJsonContains('member_extend_json->belong->premises', $premisesId);
  90. // })->count();
  91. $rows = $rows->select('member.member_id', 'member.member_mobile', 'member.member_addtimes', 'member.join_member_role_id',
  92. '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'
  93. )
  94. ->orderBy('member.member_addtimes', 'DESC')
  95. ->forPage($page, $pageSize)
  96. ->get()
  97. ->toArray();
  98. return json_success('', compact('rows', 'page', 'pageSize', 'total', 'statistics'));
  99. }
  100. /**
  101. * @Desc 导出
  102. * @Author Gorden
  103. * @Date 2024/11/19 8:56
  104. *
  105. * @param Request $request
  106. * @return \support\Response
  107. */
  108. public function exportMemberAccount(Request $request)
  109. {
  110. $days = $request->get('member_addtimes', []);
  111. $level = $request->get('level', '');
  112. $memberId = $request->get('member_id');
  113. $accountType = $request->get('account_type', 'CASH');
  114. $premisesId = intval($request->get('premises_id', ''));
  115. $memberAccountIds = $request->get('member_account_ids');
  116. if (!empty($days)) {
  117. $days[0] = strtotime($days[0]);
  118. $days[1] = strtotime($days[1]);
  119. $month = date('Ym', $days[0]);
  120. $days[1] = strtotime(date('Y-m-d', $days[1]) . " 23:59:59");
  121. } else {
  122. $month = date('Ym');
  123. }
  124. // 兼容老等级搜索
  125. $levelIds = [];
  126. if (!empty($level)) {
  127. $levelName = MemberRole::where('member_role_id', $level)->value('member_role_name');
  128. if (!empty($levelName)) {
  129. $levelIds = MemberRole::where('member_role_name', $levelName)->pluck('member_role_id')->toArray();
  130. }
  131. }
  132. $rows = Member::with([
  133. 'cert' => function ($query) {
  134. $query->select('join_cert_member_id', 'member_cert_name');
  135. },
  136. 'info' => function ($query) {
  137. $query->select('join_info_member_id', 'member_info_nickname');
  138. },
  139. 'role' => function ($query) {
  140. $query->select('member_role_id', 'member_role_name');
  141. }
  142. ])->join('member_account as ma', function ($join) use ($accountType) {
  143. $join->on('member.member_id', '=', 'ma.join_account_member_id')->where('ma.member_account_classify', '=', $accountType);
  144. })->leftJoin('member_role', 'member_role.member_role_id', 'member.join_member_role_id')
  145. ->when(!empty($level), function ($query) use ($level, $levelIds) {
  146. if ($level == '00') {
  147. $query->where('join_member_role_id', '')->orWhere('join_member_role_id', NULL);
  148. } else if ($level == 'VIP') {
  149. $query->where('member.member_is_vip', 'Y');
  150. } else if (!empty($levelIds)) {
  151. $query->whereIn('member_role.member_role_id', $levelIds);
  152. } else {
  153. $query->where('join_member_role_id', $level);
  154. }
  155. })->when(!empty($memberId), function ($query) use ($memberId) {
  156. $query->where('member.member_id', $memberId);
  157. })->when(!empty($days), function ($query) use ($days) {
  158. $query->whereBetween('member.member_addtimes', $days);
  159. })->when(!empty($premisesId), function ($query) use ($premisesId) {
  160. $query->whereJsonContains('member.member_extend_json->belong->premises', $premisesId);
  161. })->when(!empty($memberAccountIds),function($query) use ($memberAccountIds){
  162. $query->whereIn('member_account_id',$memberAccountIds);
  163. })->where('member.member_mobile', '<>', '0000')
  164. ->select('member.member_id', 'member.member_mobile', 'member.member_addtimes', 'member.join_member_role_id',
  165. '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'
  166. )
  167. ->orderBy('member.member_addtimes', 'DESC')
  168. ->get()
  169. ->toArray();
  170. $data = [];
  171. foreach ($rows as $row) {
  172. $data[] = [
  173. 'nickname' => $row['info']['member_info_nickname'] ?? '--',
  174. 'mobile' => $row['member_mobile'] ?? '--',
  175. 'role_name' => $row['role']['member_role_name'] ?? '--',
  176. 'balance' => sprintf('%.2f', $row['member_account_surplus'] + $row['member_account_added']),
  177. 'income' => $row['member_account_income'],
  178. 'expend' => $row['member_account_expend'],
  179. 'added' => $row['member_account_added'],
  180. 'addtimes' => $row['member_addtimes']
  181. ];
  182. }
  183. return json_success('success', $data);
  184. }
  185. }