FranchiseeController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\admin\service\member\MemberService;
  4. use app\controller\Curd;
  5. use app\model\CouponDetail;
  6. use app\model\Member;
  7. use app\model\MemberRole;
  8. use app\model\SysDept;
  9. use app\model\SysUser;
  10. use support\Request;
  11. class FranchiseeController extends Curd
  12. {
  13. public function list(Request $request)
  14. {
  15. $page = $request->get('page', 1);
  16. $pageSize = $request->get('pageSize', 10);
  17. $name = $request->get('name', '');
  18. $nickname = $request->get('nickname', '');
  19. $mobile = $request->get('mobile', '');
  20. $level = $request->get('level', '');
  21. $isAuth = $request->get('is_auth', '');
  22. $addtime = $request->get('member_addtimes', []);
  23. $type = $request->get('type', '');
  24. $status = $request->get('member_status', '');
  25. $memberIsFranchisee = $request->get('member_is_franchisee', '');
  26. // 兼容老等级搜索
  27. $levelIds = [];
  28. if (!empty($level)) {
  29. $levelName = MemberRole::where('member_role_id', $level)->value('member_role_name');
  30. if (!empty($levelName)) {
  31. $levelIds = MemberRole::where('member_role_name', $levelName)->pluck('member_role_id')->toArray();
  32. }
  33. }
  34. $rows = Member::with([
  35. 'account' => function ($query) {
  36. $query->where('member_account_status', 'ACTIVED');
  37. }
  38. ])->where('member_is_owner', 'N')
  39. ->where('member_mobile', '<>', '0000')
  40. ->leftJoin('member_info', 'member_info.join_info_member_id', 'member.member_id')
  41. ->leftJoin('member_cert', 'member_cert.join_cert_member_id', 'member.member_id')
  42. ->leftJoin('member_role', 'member_role.member_role_id', 'member.join_member_role_id')
  43. ->when($name != '', function ($query) use ($name) {
  44. $query->where('member_cert.member_cert_name', 'like', '%' . $name . '%');
  45. })->when($mobile != '', function ($query) use ($mobile) {
  46. $query->where('member.member_mobile', 'like', '%' . $mobile . '%');
  47. })->when($level != '', function ($query) use ($level, $levelIds) {
  48. if ($level == 'other') {
  49. $query->where('member.join_member_role_id', NULL)->orWhere('member.join_member_role_id', '');
  50. } else if ($level == 'VIP') {
  51. $query->where('member.member_is_vip', 'Y');
  52. } else if (!empty($levelIds)) {
  53. $query->whereIn('member_role.member_role_id', $levelIds);
  54. } else {
  55. $query->where('member_role.member_role_id', $level);
  56. }
  57. })->when($isAuth != '', function ($query) use ($isAuth) {
  58. if ($isAuth == 'Y') {
  59. $query->where('member_cert.member_cert_name', '<>', NULL)->where('member_cert.member_cert_nbr', '<>', NULL)->where('member_cert.member_cert_name', '<>', '')->where('member_cert.member_cert_nbr', '<>', '');
  60. } else {
  61. $query->where('member_cert.member_cert_name', NULL)->orWhere('member_cert.member_cert_nbr', NULL)->orWhere('member_cert.member_cert_name', '')->orWhere('member_cert.member_cert_nbr', '');
  62. }
  63. })->when(!empty($addtime), function ($query) use ($addtime) {
  64. $addtime[0] = strtotime($addtime[0]);
  65. $addtime[1] = strtotime($addtime[1]);
  66. $query->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_franchisee_json,'$.datetime'))) as SIGNED) >= {$addtime[0]}")
  67. ->whereRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_franchisee_json,'$.datetime'))) as SIGNED) <= {$addtime[1]}");
  68. })->when(!empty($type), function ($query) use ($type) {
  69. if ($type == 'todayAdd') {
  70. $addtime[0] = strtotime(date('Y-m-d') . ' 00:00:00');
  71. $addtime[1] = strtotime(date('Y-m-d') . ' 23:59:59');
  72. } else if ($type == 'monthAdd') {
  73. $addtime[0] = strtotime(date('Y-m-01') . ' 00:00:00');
  74. $addtime[1] = strtotime(date('Y-m-t') . ' 23:59:59');
  75. }
  76. $query->whereBetween('member.member_addtimes', $addtime);
  77. })->when(!empty($nickname), function ($query) use ($nickname) {
  78. $query->where(function ($query2) use ($nickname) {
  79. $query2->where('member_info.member_info_nickname', 'like', '%' . $nickname . '%')->orWhere('member.member_mobile', 'like', '%' . $nickname . '%');
  80. });
  81. })->when(!empty($status), function ($query) use ($status) {
  82. $query->where('member_status', $status);
  83. })->when(!empty($memberIsFranchisee), function ($query) use ($memberIsFranchisee) {
  84. if ($memberIsFranchisee == 'Y') {
  85. $query->where('member_is_franchisee', 'Y');
  86. }
  87. })
  88. ->select('member_id', 'member_is_vip', 'member_is_owner', 'join_invite_member_id', 'member_is_partner','member_is_franchisee', 'member_is_referrer', 'member_classify', 'member_status', 'member_mobile', 'member_from', 'member_addtimes', 'member_extend_json',
  89. 'member_info.member_info_nickname', 'member_info.member_info_headimg', 'member_info.member_info_referee', 'member_info.member_info_business', 'member_info.member_info_service', 'member_info.member_info_city', 'member_info.member_info_address', 'member_info.member_info_gender', 'member_info.member_info_birthday',
  90. 'member_role.member_role_id', 'member_role.member_role_name',
  91. 'member_cert.member_cert_birth', 'member_cert.member_cert_gender', 'member_cert.member_cert_name', 'member_cert.member_cert_nbr', 'member_cert.member_cert_province', 'member_cert.member_cert_addr', 'member_cert.member_cert_face', 'member_cert.member_cert_photo', 'member_cert.member_cert_nation'
  92. // 'member_account.member_account_surplus'
  93. )->selectRaw("CAST(UNIX_TIMESTAMP(JSON_UNQUOTE(JSON_EXTRACT(member_franchisee_json,'$.datetime'))) as SIGNED) as franchisee_add_times");
  94. // 总条数
  95. $total = $rows->count('member_id');
  96. // 已认证条数
  97. $authModel = clone $rows;
  98. $authTotal = $authModel->where('member_cert.member_cert_name','<>','')
  99. ->where('member_cert.member_cert_nbr','<>','')
  100. ->count();
  101. $rows = $rows->orderBy('franchisee_add_times', 'DESC')
  102. ->forPage($page, $pageSize)
  103. ->get()
  104. ->toArray();
  105. foreach ($rows as &$row) {
  106. $row['franchisee_add_times'] = date('Y-m-d H:i:s',$row['franchisee_add_times']);
  107. $row['coupon_count'] = CouponDetail::where('join_coupon_detail_member_id', $row['member_id'])->count();
  108. $row['member_mobile'] = substr($row['member_mobile'], 0, 3) . '****' . substr($row['member_mobile'], 7);
  109. // 推荐人
  110. if (!empty($row['join_invite_member_id'])) {
  111. $row['referrer_name'] = MemberService::getMemberNameByMemberId($row['join_invite_member_id']);
  112. }
  113. $row['info'] = [
  114. 'member_info_nickname' => !empty($row['member_info_nickname']) ? $row['member_info_nickname'] : substr($row['member_mobile'], -4, 4) . '会员',
  115. 'member_info_headimg' => !empty($row['member_info_headimg']) ? $row['member_info_headimg'] : '',
  116. 'member_info_referee' => !empty($row['member_info_referee']) ? $row['member_info_referee'] : '',
  117. 'member_info_business' => !empty($row['member_info_business']) ? $row['member_info_business'] : '',
  118. 'member_info_service' => !empty($row['member_info_service']) ? $row['member_info_service'] : '',
  119. 'member_info_city' => !empty($row['member_info_city']) ? explode('-', $row['member_info_city']) : '',
  120. 'member_info_address' => !empty($row['member_info_address']) ? $row['member_info_address'] : '',
  121. 'member_info_birthday' => !empty($row['member_info_birthday']) ? $row['member_info_birthday'] : '',
  122. 'member_info_gender' => !empty($row['member_info_gender']) ? $row['member_info_gender'] : '',
  123. ];
  124. if (substr($row['info']['member_info_headimg'], 0, 2) == '..') {
  125. $row['info']['member_info_headimg'] = '';
  126. }
  127. if (!empty($row['info']['member_info_address']) && in_array($row['info']['member_info_address'], ['请选择', '省-市-区'])) {
  128. $row['info']['member_info_address'] = '';
  129. }
  130. $row['cert'] = [
  131. 'member_cert_birth' => $row['member_cert_birth'],
  132. 'member_cert_gender' => $row['member_cert_gender'],
  133. 'member_cert_name' => $row['member_cert_name'],
  134. 'member_cert_nbr' => $row['member_cert_nbr'],
  135. 'member_cert_province' => !empty($row['member_cert_province']) && is_json($row['member_cert_province']) ? json_decode($row['member_cert_province']) : '',
  136. 'member_cert_addr' => $row['member_cert_addr'],
  137. 'member_cert_face' => !empty($row['member_cert_face']) ? getenv('STORAGE_DOMAIN') . $row['member_cert_face'] : '',
  138. 'member_cert_photo' => !empty($row['member_cert_photo']) ? getenv('STORAGE_DOMAIN') . $row['member_cert_photo'] : '',
  139. 'member_cert_nation' => $row['member_cert_nation'],
  140. ];
  141. $account = [];
  142. if (!empty($row['account'])) {
  143. foreach ($row['account'] as $item) {
  144. if ($item['member_account_classify'] == 'WELFARE') {
  145. $account['welfare'] = [
  146. 'member_account_name' => $item['member_account_name'],
  147. 'member_account_income' => $item['member_account_income'],
  148. 'member_account_expend' => $item['member_account_expend'],
  149. 'member_account_surplus' => $item['member_account_surplus'],
  150. 'member_account_added' => $item['member_account_added'],
  151. ];
  152. } else if ($item['member_account_classify'] == 'POINTS') {
  153. $account['points'] = [
  154. 'member_account_name' => $item['member_account_name'],
  155. 'member_account_income' => $item['member_account_income'],
  156. 'member_account_expend' => $item['member_account_expend'],
  157. 'member_account_surplus' => $item['member_account_surplus'],
  158. 'member_account_added' => $item['member_account_added'],
  159. ];
  160. } else if ($item['member_account_classify'] == 'CASH') {
  161. $account['cash'] = [
  162. 'member_account_name' => $item['member_account_name'],
  163. 'member_account_income' => $item['member_account_income'],
  164. 'member_account_expend' => $item['member_account_expend'],
  165. 'member_account_surplus' => $item['member_account_surplus'],
  166. 'member_account_added' => $item['member_account_added'],
  167. ];
  168. } else if ($item['member_account_classify'] == 'CARD') {
  169. $account['card'] = [
  170. 'member_account_name' => $item['member_account_name'],
  171. 'member_account_income' => $item['member_account_income'],
  172. 'member_account_expend' => $item['member_account_expend'],
  173. 'member_account_surplus' => $item['member_account_surplus'],
  174. 'member_account_added' => $item['member_account_added'],
  175. ];
  176. } else if ($item['member_account_classify'] == 'VIP') {
  177. $account['vip'] = [
  178. 'member_account_name' => $item['member_account_name'],
  179. 'member_account_income' => $item['member_account_income'],
  180. 'member_account_expend' => $item['member_account_expend'],
  181. 'member_account_surplus' => $item['member_account_surplus'],
  182. 'member_account_added' => $item['member_account_added'],
  183. ];
  184. }
  185. }
  186. }
  187. $row['account'] = $account;
  188. $row['is_auth'] = "未认证";
  189. if (!empty($row['member_cert_nbr']) && !empty($row['member_cert_name'])) {
  190. $row['is_auth'] = "已认证";
  191. }
  192. $row['source'] = "APP";
  193. $row['belong'] = [];
  194. $row['referee'] = [];
  195. if (!empty($row['member_extend_json'])) {
  196. $extendJson = json_decode($row['member_extend_json'], true);
  197. if (isset($extendJson['come_from'])) {
  198. $row['source'] = $extendJson['come_from'] ?? '';
  199. }
  200. if (isset($extendJson['belong'])) {
  201. $user = SysUser::where('user_id', $extendJson['belong']['user_id'])->select('user_name', 'user_mobile')->first();
  202. if ($user) {
  203. $row['belong']['user_id'] = $extendJson['belong']['user_id'];
  204. $row['belong']['username'] = $user->user_name;
  205. $row['belong']['user_mobile'] = $user->user_mobile;
  206. }
  207. $row['belong']['deptName'] = SysDept::where('dept_id', $extendJson['belong']['premises'])->value('dept_name');
  208. }
  209. if (isset($extendJson['referee'])) {
  210. foreach ($extendJson['referee'] as $key => $referee) {
  211. if (preg_match('/^1[3-9]\d{9}$/', $referee)) {
  212. $member = Member::with([
  213. 'cert' => function ($query) {
  214. $query->select('join_cert_member_id', 'member_cert_name');
  215. }
  216. ])->where('member_mobile', $referee)->where('member_status', 'ACTIVED')
  217. ->first();
  218. if ($member->cert && $member->cert->member_cert_name) {
  219. $referee = $member->cert->member_cert_name . '-' . $referee;
  220. }
  221. }
  222. $row['referee'][] = [
  223. 'name' => $referee,
  224. 'time' => $key
  225. ];
  226. }
  227. }
  228. }
  229. // 粉丝
  230. $fans1 = Member::where('join_invite_member_id', $row['member_id'])
  231. ->select('member_id')
  232. ->get()
  233. ->toArray();
  234. $fans1Ids = array_column($fans1, 'member_id');
  235. $fans2Count = Member::whereIn('join_invite_member_id', $fans1Ids)
  236. ->count();
  237. $row['fans_total'] = count($fans1) + $fans2Count;
  238. }
  239. return json_success('', compact('rows', 'page', 'pageSize', 'total','authTotal'));
  240. }
  241. }