MemberController.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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 commissionList(Request $request)
  337. {
  338. $memberId = $request->get('member_id');
  339. if (!$memberId) {
  340. return json_fail("参数错误");
  341. }
  342. return MemberService::commissionList($request);
  343. }
  344. /**
  345. * @Desc 我的粉丝列表
  346. * @Author Gorden
  347. * @Date 2024/9/20 10:22
  348. *
  349. * @param Request $request
  350. * @return \support\Response
  351. */
  352. public function fansList(Request $request)
  353. {
  354. $memberId = $request->get('member_id');
  355. if (!$memberId) {
  356. return json_fail("参数错误");
  357. }
  358. return MemberService::fansList($request);
  359. }
  360. public function clearWelfare(Request $request)
  361. {
  362. $memberId = $request->post('member_id', '');
  363. if (!$memberId) {
  364. return json_fail("参数错误");
  365. }
  366. return MemberService::clearWelfare($request);
  367. }
  368. public function balanceInfo(Request $request)
  369. {
  370. $memberId = $request->get('member_id', '');
  371. if (!$memberId) {
  372. return json_fail("参数错误");
  373. }
  374. return MemberService::balanceInfo($request);
  375. }
  376. public function pointInfo(Request $request)
  377. {
  378. $memberId = $request->get('member_id', '');
  379. if (!$memberId) {
  380. return json_fail("参数错误");
  381. }
  382. return MemberService::pointInfo($memberId);
  383. }
  384. /**
  385. * @Desc 添加会员
  386. * @Author Gorden
  387. * @Date 2024/3/25 13:40
  388. *
  389. * @param Request $request
  390. * @return \support\Response
  391. */
  392. public function add(Request $request)
  393. {
  394. $validate = new MemberValidate();
  395. if (!$validate->scene('add')->check($request->post())) {
  396. return json_fail($validate->getError());
  397. }
  398. _syslog("添加用户", "用户【" . $request->post('member_cert_name') . '】');
  399. return MemberService::add($request->post());
  400. }
  401. public function update(Request $request)
  402. {
  403. $validate = new MemberValidate();
  404. if (!$validate->scene('update')->check($request->post())) {
  405. return json_fail($validate->getError());
  406. }
  407. _syslog("编辑用户", "用户【" . $request->post('member_cert_name') ?? $request->post('member_id') . '】');
  408. return MemberService::update($request->post());
  409. }
  410. /**
  411. * @Desc 绑定的设备
  412. * @Author Gorden
  413. * @Date 2024/4/7 10:20
  414. *
  415. * @param $id
  416. * @return \support\Response
  417. */
  418. public function device($id)
  419. {
  420. return MemberService::deviceList($id);
  421. }
  422. public function addDevice(Request $request)
  423. {
  424. return MemberService::addDevice($request);
  425. }
  426. public function updateDevice(Request $request)
  427. {
  428. return MemberService::updateDevice($request);
  429. }
  430. public function deleteDevice(Request $request)
  431. {
  432. return MemberService::deleteDevice($request);
  433. }
  434. /**
  435. * @Desc 卡券数量
  436. * @Author Gorden
  437. * @Date 2024/8/22 18:51
  438. *
  439. * @param Request $request
  440. * @return \support\Response
  441. */
  442. public function couponCount(Request $request)
  443. {
  444. $memberId = $request->get('member_id', '');
  445. $coupons = CouponDetail::leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  446. ->where('join_coupon_detail_member_id', $memberId)
  447. ->select('coupon.coupon_name', 'coupon.coupon_classify','coupon.coupon_value')
  448. ->get()
  449. ->toArray();
  450. $data = ['manjian' => 0, 'diyong' => 0, 'zhekou' => 0, 'lijian' => 0, 'zengpin' => 0, 'fuli' => 0, 'nianka' => 0, 'jika' => 0, 'yueka' => 0];
  451. foreach ($coupons as $item) {
  452. if ($item['coupon_classify'] == '满减券') {
  453. $data['manjian'] += 1;
  454. } elseif ($item['coupon_classify'] == '抵用券') {
  455. $data['diyong'] += 1;
  456. } elseif ($item['coupon_classify'] == '折扣券') {
  457. $data['zhekou'] += 1;
  458. } elseif ($item['coupon_classify'] == '立减券') {
  459. $data['lijian'] += 1;
  460. } elseif ($item['coupon_classify'] == '赠品券') {
  461. $data['zengpin'] += 1;
  462. } elseif ($item['coupon_classify'] == '福利券') {
  463. $data['fuli'] += 1;
  464. } elseif ($item['coupon_classify'] == '年卡') {
  465. $data['nianka'] += 1;
  466. } elseif ($item['coupon_classify'] == '季卡') {
  467. $data['jika'] += 1;
  468. } elseif ($item['coupon_classify'] == '月卡') {
  469. $data['yueka'] += 1;
  470. }
  471. }
  472. $data['total'] = $data['manjian'] + $data['diyong'] + $data['zhekou'] + $data['lijian'] + $data['zengpin'] + $data['fuli'] + $data['nianka'] + $data['jika'] + $data['yueka'];
  473. return json_success('', $data);
  474. }
  475. /**
  476. * @Desc 我的卡券
  477. * @Author Gorden
  478. * @Date 2024/8/22 18:45
  479. *
  480. * @param Request $request
  481. * @return \support\Response
  482. */
  483. public function coupon(Request $request)
  484. {
  485. $memberId = $request->get('member_id', '');
  486. $classify = $request->get('classify', '');
  487. $page = $request->get('page', 1);
  488. $pageSize = $request->get('pageSize', 20);
  489. $coupons = CouponDetail::with([
  490. 'member' => function ($query) {
  491. $query->select('member_id', 'member_mobile');
  492. },
  493. 'cert' => function ($query) {
  494. $query->select('join_cert_member_id', 'member_cert_name');
  495. },
  496. 'info' => function ($query) {
  497. $query->select('join_info_member_id', 'member_info_nickname');
  498. }
  499. ])
  500. ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  501. ->where('join_coupon_detail_member_id', $memberId)
  502. ->where('coupon.coupon_classify', $classify)
  503. ->groupBy('join_coupon_detail_member_id', 'join_detail_coupon_id');
  504. // $total = $coupons->count();
  505. $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");
  506. $total = 0;
  507. if (!empty($totalModel) && !empty($totalModel[0])) {
  508. $total = $totalModel[0]->total;
  509. }
  510. $rows = $coupons->select('coupon_detail.join_coupon_detail_member_id', 'coupon_detail.join_detail_coupon_id'
  511. , 'coupon.coupon_name', 'coupon.coupon_classify', 'coupon.coupon_value')
  512. ->selectRaw('COUNT(1) as total,
  513. COUNT(IF(app_coupon_detail.coupon_detail_status="USED",1,NULL)) as used_total,
  514. COUNT(IF(app_coupon_detail.coupon_detail_status="ACTIVED" or app_coupon_detail.coupon_detail_status="WAITING",1,NULL)) as unused_total,
  515. MAX(app_coupon_detail.coupon_detail_deadline_datetime) as deadline_datetime')
  516. ->orderBy('deadline_datetime', 'DESC')
  517. ->forPage($page, $pageSize)
  518. ->get()
  519. ->toArray();
  520. foreach ($rows as &$item) {
  521. $mobile = $certName = $nickname = '';
  522. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  523. $mobile = $item['member']['member_mobile'];
  524. }
  525. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  526. $certName = $item['cert']['member_cert_name'];
  527. }
  528. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  529. $nickname = $item['info']['member_info_nickname'];
  530. }
  531. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  532. unset($item['member'], $item['cert'], $item['info']);
  533. $item['invalid_total'] = $item['total'] - $item['used_total'] - $item['unused_total'];
  534. // 优惠券分类
  535. $coupon = Coupon::with('category')->where('coupon_id', $item['join_detail_coupon_id'])
  536. ->select('coupon_id', 'join_coupon_category_id')
  537. ->first();
  538. if (!empty($coupon) && !empty($coupon->category)) {
  539. $item['category_name'] = $coupon->category->category_name;
  540. }
  541. }
  542. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  543. }
  544. /**
  545. * @Desc 我的卡券
  546. * @Author Gorden
  547. * @Date 2024/8/22 18:45
  548. *
  549. * @param Request $request
  550. * @return \support\Response
  551. */
  552. public function allCoupon(Request $request)
  553. {
  554. $memberId = $request->get('member_id', '');
  555. $page = $request->get('page', 1);
  556. $pageSize = $request->get('pageSize', 20);
  557. $coupons = CouponDetail::with([
  558. 'member' => function ($query) {
  559. $query->select('member_id', 'member_mobile');
  560. },
  561. 'cert' => function ($query) {
  562. $query->select('join_cert_member_id', 'member_cert_name');
  563. },
  564. 'info' => function ($query) {
  565. $query->select('join_info_member_id', 'member_info_nickname');
  566. }
  567. ])
  568. ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  569. ->where('join_coupon_detail_member_id', $memberId)
  570. ->groupBy('join_coupon_detail_member_id', 'join_detail_coupon_id');
  571. // $total = $coupons->count();
  572. $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 . "' group by d.join_coupon_detail_member_id,d.join_detail_coupon_id) as a");
  573. $total = 0;
  574. if (!empty($totalModel) && !empty($totalModel[0])) {
  575. $total = $totalModel[0]->total;
  576. }
  577. $rows = $coupons->select('coupon_detail.join_coupon_detail_member_id', 'coupon_detail.join_detail_coupon_id'
  578. , 'coupon.coupon_name', 'coupon.coupon_classify', 'coupon.coupon_value')
  579. ->selectRaw('COUNT(1) as total,
  580. COUNT(IF(app_coupon_detail.coupon_detail_status="USED",1,NULL)) as used_total,
  581. COUNT(IF(app_coupon_detail.coupon_detail_status="ACTIVED" or app_coupon_detail.coupon_detail_status="WAITING",1,NULL)) as unused_total,
  582. MAX(app_coupon_detail.coupon_detail_deadline_datetime) as deadline_datetime')
  583. ->orderBy('deadline_datetime', 'DESC')
  584. ->forPage($page, $pageSize)
  585. ->get()
  586. ->toArray();
  587. foreach ($rows as &$item) {
  588. $mobile = $certName = $nickname = '';
  589. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  590. $mobile = $item['member']['member_mobile'];
  591. }
  592. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  593. $certName = $item['cert']['member_cert_name'];
  594. }
  595. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  596. $nickname = $item['info']['member_info_nickname'];
  597. }
  598. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  599. unset($item['member'], $item['cert'], $item['info']);
  600. $item['invalid_total'] = $item['total'] - $item['used_total'] - $item['unused_total'];
  601. // 优惠券分类
  602. $coupon = Coupon::with('category')->where('coupon_id', $item['join_detail_coupon_id'])
  603. ->select('coupon_id', 'join_coupon_category_id')
  604. ->first();
  605. if (!empty($coupon) && !empty($coupon->category)) {
  606. $item['category_name'] = $coupon->category->category_name;
  607. }
  608. }
  609. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  610. }
  611. /**
  612. * @Desc 我的卡券 领取记录
  613. * @Author Gorden
  614. * @Date 2024/8/22 18:45
  615. *
  616. * @param Request $request
  617. * @return \support\Response
  618. */
  619. public function couponDetail(Request $request)
  620. {
  621. $memberId = $request->get('member_id', '');
  622. $couponId = $request->get('coupon_id', '');
  623. $page = $request->get('page', 1);
  624. $pageSize = $request->get('pageSize', 20);
  625. $coupons = CouponDetail::with([
  626. 'member' => function ($query) {
  627. $query->select('member_id', 'member_mobile');
  628. },
  629. 'cert' => function ($query) {
  630. $query->select('join_cert_member_id', 'member_cert_name');
  631. },
  632. 'info' => function ($query) {
  633. $query->select('join_info_member_id', 'member_info_nickname');
  634. }
  635. ])
  636. ->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  637. ->where('join_coupon_detail_member_id', $memberId)
  638. ->where('coupon.coupon_id', $couponId);
  639. $total = $coupons->count();
  640. $rows = $coupons->select('coupon_detail.*', 'coupon.coupon_name', 'coupon.coupon_classify', 'coupon.coupon_value')
  641. ->orderBy('coupon_detail_deadline_datetime', 'desc')
  642. ->forPage($page, $pageSize)
  643. ->get()
  644. ->toArray();
  645. foreach ($rows as &$item) {
  646. $mobile = $certName = $nickname = '';
  647. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  648. $mobile = $item['member']['member_mobile'];
  649. }
  650. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  651. $certName = $item['cert']['member_cert_name'];
  652. }
  653. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  654. $nickname = $item['info']['member_info_nickname'];
  655. }
  656. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  657. unset($item['member'], $item['cert'], $item['info']);
  658. if (!empty($coupon) && !empty($coupon->category)) {
  659. $item['category_name'] = $coupon->category->category_name;
  660. }
  661. }
  662. return json_success('', compact('rows', 'page', 'pageSize', 'total'));
  663. }
  664. }