MemberController.php 25 KB

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