MemberController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. <?php
  2. namespace app\admin\controller\member;
  3. use app\admin\service\member\MemberService;
  4. use app\admin\validate\member\MemberValidate;
  5. use app\model\CouponDetail;
  6. use app\model\Family;
  7. use app\model\FamilyMember;
  8. use app\model\Member;
  9. use app\model\MemberAccount;
  10. use app\model\MemberBenefit;
  11. use app\model\MemberQuota;
  12. use app\model\PayDetail;
  13. use support\Db;
  14. use support\Request;
  15. class MemberController
  16. {
  17. public function selectList(Request $request)
  18. {
  19. $keywords = $request->get('keywords');
  20. $isOwner = $request->get('member_is_owner', 'N');
  21. if (!$keywords) {
  22. return json_fail("查询错误");
  23. }
  24. $res = Db::table('member')
  25. ->leftjoin('member_cert', 'member.member_id', '=', 'member_cert.join_cert_member_id')
  26. ->leftjoin('member_info', 'member.member_id', '=', 'member_info.join_info_member_id')
  27. ->select('member.member_id', 'member.member_mobile', 'member_cert.member_cert_name', 'member_info.member_info_nickname')
  28. ->where('member.member_is_owner', $isOwner)
  29. ->where('member_status', 'ACTIVED')
  30. // ->where('member_mobile','<>','0000')
  31. ->where(function ($query) use ($keywords) {
  32. $query->where('member.member_mobile', 'like', '%' . $keywords . '%')
  33. ->orWhere("member.member_id", 'like', '%' . $keywords . '%')
  34. ->orWhere("member_cert.member_cert_name", 'like', '%' . $keywords . '%')
  35. ->orWhere("member_info.member_info_nickname", 'like', '%' . $keywords . '%');
  36. })->get();
  37. $data = [];
  38. foreach ($res as $item) {
  39. // $item->member_mobile = substr($item->member_mobile,0,3).'****'.substr($item->member_mobile,7);
  40. $name = '';
  41. if (!empty($item->member_cert_name)) {
  42. $name = $item->member_cert_name;
  43. } else if (!empty($item->member_info_nickname)) {
  44. $name = $item->member_info_nickname;
  45. }
  46. $value = !empty($name) ? $name . '-' : '';
  47. if ($item->member_mobile != '0000') {
  48. $value .= $item->member_mobile;
  49. }
  50. if (empty($value)) {
  51. $value = $item->member_mobile;
  52. }
  53. $value = rtrim($value, '-');
  54. $data[] = [
  55. 'key' => $item->member_id,
  56. 'value' => $value,
  57. ];
  58. }
  59. return json_success('', $data);
  60. }
  61. public function exportMember(Request $request)
  62. {
  63. return MemberService::exportMember($request);
  64. }
  65. public function selectWriteOffMember()
  66. {
  67. $members = Member::with([
  68. 'cert' => function ($query) {
  69. $query->select('join_cert_member_id', 'member_cert_name');
  70. }
  71. ])->whereRaw("JSON_EXTRACT(`member_json`, '$.user') IS NOT NULL")
  72. ->select('member_id', 'member_mobile', 'member_json')
  73. ->get()
  74. ->toArray();
  75. foreach ($members as &$member) {
  76. // $member['member_mobile'] = substr($member['member_mobile'],0,3).'****'.substr($member['member_mobile'],7);
  77. $member['member_json'] = json_decode($member['member_json'], true);
  78. $member['username'] = $member['member_mobile'];
  79. $member['user_id'] = $member['member_json']['user'] ? $member['member_json']['user']['user_id'] : '';
  80. if (isset($member['cert']['member_cert_name'])) {
  81. $member['username'] = $member['cert']['member_cert_name'] . '-' . $member['username'];
  82. }
  83. }
  84. return json_success('', $members);
  85. }
  86. public function selectFamilyMemberList(Request $request)
  87. {
  88. $keywords = $request->get('keywords');
  89. if (!$keywords) {
  90. return json_fail("查询错误");
  91. }
  92. // 创建的家庭
  93. $family = Family::where('join_family_creator_member_id', $request->get('memberId'))->get()->toArray();
  94. if (!$family) {
  95. return json_success('暂无数据');
  96. }
  97. $familyIds = array_column($family, 'family_id');
  98. $res = FamilyMember::whereIn('join_family_id', $familyIds)
  99. ->where("family_member_name", 'like', '%' . $keywords . '%')
  100. ->get()
  101. ->toArray();
  102. $data = [];
  103. foreach ($res as $item) {
  104. $data[] = [
  105. 'key' => $item['join_family_member_id'],
  106. 'value' => $item['family_member_name']
  107. ];
  108. }
  109. return json_success('', $data);
  110. }
  111. public function selectMemberAllQuota(Request $request)
  112. {
  113. $memberId = $request->get('member_id', '');
  114. $orderId = $request->get('order_id', '');
  115. $goodsId = $request->get('goods_id', '');
  116. $person = $request->get('person', '');
  117. $selectType = $request->get('select_type', 'ALL');
  118. if (!$memberId) {
  119. return json_fail('参数异常');
  120. }
  121. $data = [];
  122. $benefit = MemberBenefit::where('join_benefit_member_id', $memberId)
  123. ->whereColumn('member_benefit_limit_count', '>', 'member_benefit_used_count')
  124. ->select('member_benefit_id as id', 'member_benefit_name as name', 'member_benefit_limit_count as total', 'member_benefit_used_count as used')
  125. ->when($orderId != '', function ($query) use ($orderId) {
  126. $query->where('join_benefit_order_id', $orderId);
  127. })->when($goodsId != '', function ($query) use ($goodsId) {
  128. $query->where('join_benefit_goods_id', $goodsId);
  129. })
  130. ->get()
  131. ->toArray();
  132. foreach ($benefit as $item) {
  133. if ($item['total'] - $item['used'] < $person) {
  134. $item['disabled'] = true;
  135. }
  136. $item['cut'] = $item['total'] - $item['used'];
  137. $data[] = $item;
  138. }
  139. if ($selectType == 'BENEFIT') {
  140. return json_success('', $data);
  141. }
  142. $quotas = MemberQuota::where('join_quota_member_id', $memberId)
  143. ->when($goodsId != '', function ($query) use ($goodsId) {
  144. $query->whereJsonContains('member_quota_json', ['goods_id' => $goodsId]);
  145. })
  146. ->selectRaw('join_quota_member_id,join_member_rule_added_component_id,COUNT(*) as total')
  147. ->groupBy('join_quota_member_id', 'join_member_rule_added_component_id')
  148. ->get()
  149. ->toArray();
  150. foreach ($quotas as $quota) {
  151. $usedNum = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  152. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  153. ->where('member_quota_status', 'USED')
  154. ->count('*');
  155. if ($quota['total'] <= $usedNum) {
  156. continue;
  157. }
  158. $memberQuota = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  159. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  160. ->select('member_quota_id as id', 'member_quota_name as name')
  161. ->first()
  162. ->toArray();
  163. $memberQuota['total'] = $quota['total'];
  164. $memberQuota['used'] = $usedNum;
  165. if ($memberQuota['total'] - $memberQuota['used'] < $person) {
  166. $memberQuota['disabled'] = true;
  167. }
  168. $memberQuota['cut'] = $memberQuota['total'] - $memberQuota['used'];
  169. $data[] = $memberQuota;
  170. }
  171. if ($selectType == 'QUOTA') {
  172. return json_success('', $data);
  173. }
  174. return json_success('', $data);
  175. }
  176. /**
  177. * 预约核销用
  178. */
  179. public function selectMemberAllQuotaByAppointment(Request $request)
  180. {
  181. $memberId = $request->get('member_id', '');
  182. $orderId = $request->get('order_id', '');
  183. $goodsId = $request->get('goods_id', '');
  184. $person = $request->get('person', '');
  185. $selectType = $request->get('select_type', 'ALL');
  186. if (!$memberId) {
  187. return json_fail('参数异常');
  188. }
  189. $data = [];
  190. $benefit = MemberBenefit::where('join_benefit_member_id', $memberId)
  191. ->whereColumn('member_benefit_limit_count', '>', 'member_benefit_used_count')
  192. ->select('member_benefit_id as id', 'member_benefit_name as name', 'member_benefit_limit_count as total', 'member_benefit_used_count as used')
  193. ->when($orderId != '', function ($query) use ($orderId) {
  194. $query->where('join_benefit_order_id', $orderId);
  195. })->when($goodsId != '', function ($query) use ($goodsId) {
  196. $query->where('join_benefit_goods_id', $goodsId);
  197. })
  198. ->get()
  199. ->toArray();
  200. $data['benefit'] = [];
  201. foreach ($benefit as $item) {
  202. $item['benefit']['cut'] = $item['total'] - $item['used'];
  203. $data['benefit']['name'] = $item['name'];
  204. $data['benefit']['cut'] += $item['cut'];
  205. $data['benefit']['id'][] = $item['id'];
  206. }
  207. if ($selectType == 'BENEFIT') {
  208. return json_success('', $data);
  209. }
  210. $quotas = MemberQuota::where('join_quota_member_id', $memberId)
  211. ->when($goodsId != '', function ($query) use ($goodsId) {
  212. $query->whereJsonContains('member_quota_json', ['goods_id' => $goodsId]);
  213. })
  214. ->selectRaw('join_quota_member_id,join_member_rule_added_component_id,COUNT(*) as total')
  215. ->groupBy('join_quota_member_id', 'join_member_rule_added_component_id')
  216. ->get()
  217. ->toArray();
  218. $data['quota'] = [];
  219. foreach ($quotas as $quota) {
  220. $usedNum = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  221. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  222. ->where('member_quota_status', 'USED')
  223. ->count('*');
  224. if ($quota['total'] <= $usedNum) {
  225. continue;
  226. }
  227. $memberQuota = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  228. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  229. ->select('member_quota_id as id', 'member_quota_name as name')
  230. ->first()
  231. ->toArray();
  232. $memberQuota['total'] = $quota['total'];
  233. $memberQuota['used'] = $usedNum;
  234. if ($memberQuota['total'] - $memberQuota['used'] < $person) {
  235. $memberQuota['disabled'] = true;
  236. }
  237. $memberQuota['cut'] = $memberQuota['total'] - $memberQuota['used'];
  238. $data['quota']['name'] = $memberQuota['name'];
  239. $data['quota']['cut'] += $memberQuota['cut'];
  240. $data['quota']['id'][] = $memberQuota['id'];
  241. // $data[] = $memberQuota;
  242. }
  243. if ($selectType == 'QUOTA') {
  244. return json_success('', $data);
  245. }
  246. return json_success('', $data);
  247. }
  248. public function selectMemberAllQuotaByGoods(Request $request)
  249. {
  250. $memberId = $request->get('member_id', '');
  251. $orderId = $request->get('order_id', '');
  252. $person = $request->get('person', '');
  253. $selectType = $request->get('select_type', 'ALL');
  254. if (!$memberId) {
  255. return json_fail('参数异常');
  256. }
  257. $data = [];
  258. $benefit = MemberBenefit::where('join_benefit_member_id', $memberId)
  259. // ->whereColumn('member_benefit_limit_count','>','member_benefit_used_count')
  260. ->select('member_benefit_id as id', 'member_benefit_name as name', 'member_benefit_limit_count as total', 'member_benefit_used_count as used')
  261. ->when($orderId != '', function ($query) use ($orderId) {
  262. $query->where('join_benefit_order_id', $orderId);
  263. })
  264. ->get()
  265. ->toArray();
  266. foreach ($benefit as $item) {
  267. if ($item['total'] - $item['used'] < $person) {
  268. $item['disabled'] = true;
  269. }
  270. $item['cut'] = $item['total'] - $item['used'];
  271. $data[] = $item;
  272. }
  273. if ($selectType == 'BENEFIT') {
  274. return json_success('', $data);
  275. }
  276. $quotas = MemberQuota::where('join_quota_member_id', $memberId)
  277. ->selectRaw('join_quota_member_id,join_member_rule_added_component_id,COUNT(*) as total')
  278. ->groupBy('join_quota_member_id', 'join_member_rule_added_component_id')
  279. ->get()
  280. ->toArray();
  281. foreach ($quotas as $quota) {
  282. $usedNum = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  283. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  284. ->where('member_quota_status', 'USED')
  285. ->count('*');
  286. if ($quota['total'] <= $usedNum) {
  287. continue;
  288. }
  289. $memberQuota = MemberQuota::where('join_quota_member_id', $quota['join_quota_member_id'])
  290. ->where('join_member_rule_added_component_id', $quota['join_member_rule_added_component_id'])
  291. ->select('member_quota_id as id', 'member_quota_name as name')
  292. ->first()
  293. ->toArray();
  294. $memberQuota['total'] = $quota['total'];
  295. $memberQuota['used'] = $usedNum;
  296. if ($memberQuota['total'] - $memberQuota['used'] < $person) {
  297. $memberQuota['disabled'] = true;
  298. }
  299. $memberQuota['cut'] = $memberQuota['total'] - $memberQuota['used'];
  300. $data[] = $memberQuota;
  301. }
  302. if ($selectType == 'QUOTA') {
  303. return json_success('', $data);
  304. }
  305. return json_success('', $data);
  306. }
  307. public function list(Request $request)
  308. {
  309. return MemberService::list($request);
  310. }
  311. public function info(Request $request)
  312. {
  313. $memberId = $request->get('member_id', '');
  314. if (!$memberId) {
  315. return json_fail("参数错误");
  316. }
  317. return MemberService::info($memberId);
  318. }
  319. public function vipInfo(Request $request)
  320. {
  321. $memberId = $request->get('member_id', '');
  322. if (!$memberId) {
  323. return json_fail("参数错误");
  324. }
  325. return MemberService::vipInfo($request);
  326. }
  327. public function welfareInfo(Request $request)
  328. {
  329. $memberId = $request->get('member_id', '');
  330. if (!$memberId) {
  331. return json_fail("参数错误");
  332. }
  333. return MemberService::welfareInfo($request);
  334. }
  335. public function balanceInfo(Request $request)
  336. {
  337. $memberId = $request->get('member_id', '');
  338. if (!$memberId) {
  339. return json_fail("参数错误");
  340. }
  341. return MemberService::balanceInfo($request);
  342. }
  343. public function pointInfo(Request $request)
  344. {
  345. $memberId = $request->get('member_id', '');
  346. if (!$memberId) {
  347. return json_fail("参数错误");
  348. }
  349. return MemberService::pointInfo($memberId);
  350. }
  351. /**
  352. * @Desc 添加会员
  353. * @Author Gorden
  354. * @Date 2024/3/25 13:40
  355. *
  356. * @param Request $request
  357. * @return \support\Response
  358. */
  359. public function add(Request $request)
  360. {
  361. $validate = new MemberValidate();
  362. if (!$validate->scene('add')->check($request->post())) {
  363. return json_fail($validate->getError());
  364. }
  365. _syslog("添加用户", "用户【" . $request->post('member_cert_name') . '】');
  366. return MemberService::add($request->post());
  367. }
  368. public function update(Request $request)
  369. {
  370. $validate = new MemberValidate();
  371. if (!$validate->scene('update')->check($request->post())) {
  372. return json_fail($validate->getError());
  373. }
  374. _syslog("编辑用户", "用户【" . $request->post('member_cert_name') ?? $request->post('member_id') . '】');
  375. return MemberService::update($request->post());
  376. }
  377. /**
  378. * @Desc 绑定的设备
  379. * @Author Gorden
  380. * @Date 2024/4/7 10:20
  381. *
  382. * @param $id
  383. * @return \support\Response
  384. */
  385. public function device($id)
  386. {
  387. return MemberService::deviceList($id);
  388. }
  389. public function addDevice(Request $request)
  390. {
  391. return MemberService::addDevice($request);
  392. }
  393. public function updateDevice(Request $request)
  394. {
  395. return MemberService::updateDevice($request);
  396. }
  397. public function deleteDevice(Request $request)
  398. {
  399. return MemberService::deleteDevice($request);
  400. }
  401. /**
  402. * @Desc 卡券数量
  403. * @Author Gorden
  404. * @Date 2024/8/22 18:51
  405. *
  406. * @param Request $request
  407. * @return \support\Response
  408. */
  409. public function couponCount(Request $request)
  410. {
  411. $memberId = $request->get('member_id', '');
  412. $coupons = CouponDetail::leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  413. ->where('join_coupon_detail_member_id', $memberId)
  414. ->select('coupon.coupon_name', 'coupon.coupon_classify')
  415. ->get()
  416. ->toArray();
  417. $data = ['manjian'=>0,'diyong'=>0,'zhekou'=>0,'lijian'=>0,'zengpin'=>0,'fuli'=>0,'nianka'=>0,'jika'=>0,'yueka'=>0];
  418. foreach ($coupons as $item) {
  419. if ($item['coupon_classify'] == '满减券'){
  420. $data['manjian'] += 1;
  421. }elseif ($item['coupon_classify'] == '抵用券'){
  422. $data['diyong'] += 1;
  423. }elseif ($item['coupon_classify'] == '折扣券'){
  424. $data['zhekou'] += 1;
  425. }elseif ($item['coupon_classify'] == '立减券'){
  426. $data['lijian'] += 1;
  427. }elseif ($item['coupon_classify'] == '赠品券'){
  428. $data['zengpin'] += 1;
  429. }elseif ($item['coupon_classify'] == '福利券'){
  430. $data['fuli'] += 1;
  431. }elseif ($item['coupon_classify'] == '年卡'){
  432. $data['nianka'] += 1;
  433. }elseif ($item['coupon_classify'] == '季卡'){
  434. $data['jika'] += 1;
  435. }elseif ($item['coupon_classify'] == '月卡'){
  436. $data['yueka'] += 1;
  437. }
  438. }
  439. $data['total'] = $data['manjian']+$data['diyong']+$data['zhekou']+$data['lijian']+$data['zengpin']+$data['fuli']+$data['nianka']+$data['jika']+$data['yueka'];
  440. return json_success('',$data);
  441. }
  442. /**
  443. * @Desc 我的卡券
  444. * @Author Gorden
  445. * @Date 2024/8/22 18:45
  446. *
  447. * @param Request $request
  448. * @return \support\Response
  449. */
  450. public function coupon(Request $request)
  451. {
  452. $memberId = $request->get('member_id', '');
  453. $classify = $request->get('classify', '');
  454. $page = $request->get('page', 1);
  455. $pageSize = $request->get('pageSize', 20);
  456. $coupons = CouponDetail::with([
  457. 'member' => function ($query) {
  458. $query->select('member_id', 'member_mobile');
  459. },
  460. 'cert' => function ($query) {
  461. $query->select('join_cert_member_id', 'member_cert_name');
  462. },
  463. 'info' => function ($query) {
  464. $query->select('join_info_member_id', 'member_info_nickname');
  465. }
  466. ])
  467. ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  468. ->where('join_coupon_detail_member_id', $memberId)
  469. ->where('coupon.coupon_classify', $classify);
  470. $total = $coupons->count();
  471. $rows = $coupons->select('coupon_detail.*', 'coupon.coupon_name', 'coupon.coupon_classify')
  472. ->forPage($page, $pageSize)
  473. ->get()
  474. ->toArray();
  475. foreach ($rows as &$item) {
  476. $mobile = $certName = $nickname = '';
  477. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  478. $mobile = $item['member']['member_mobile'];
  479. }
  480. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  481. $certName = $item['cert']['member_cert_name'];
  482. }
  483. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  484. $nickname = $item['info']['member_info_nickname'];
  485. }
  486. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  487. unset($item['member'], $item['cert'], $item['info']);
  488. $data[$item['coupon_classify']][] = $item;
  489. }
  490. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  491. }
  492. }