CouponDetailController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. <?php
  2. namespace app\admin\controller\coupon;
  3. use app\admin\service\coupon\CouponService;
  4. use app\admin\service\member\MemberService;
  5. use app\controller\Curd;
  6. use app\model\Coupon;
  7. use app\model\CouponDetail;
  8. use app\model\CouponGoods;
  9. use app\model\Member;
  10. use app\model\SysDept;
  11. use support\Request;
  12. use support\Response;
  13. class CouponDetailController extends Curd
  14. {
  15. public function __construct()
  16. {
  17. $this->model = new CouponDetail();
  18. }
  19. public function select(Request $request): Response
  20. {
  21. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  22. // if (empty($where['coupon_detail_status'])) {
  23. // $where['coupon_detail_status'] = ['<>', 'DISABLED'];
  24. // }
  25. // $where['join_coupon_detail_member_id'] = ['<>', ''];
  26. $order = $request->get('order', 'desc');
  27. $field = $field ?? 'coupon_detail_addtimes';
  28. $couponName = $request->get('coupon_name', '');
  29. if (!empty($couponName)) {
  30. $couponIds = Coupon::where('coupon_name', 'like', '%' . $couponName . '%')->pluck('coupon_id')->toArray();
  31. if (empty($couponIds)) {
  32. $couponIds = ['default'];
  33. }
  34. $where['join_detail_coupon_id'] = ['in', $couponIds];
  35. }
  36. $addTimes = $request->get('coupon_detail_addtimes', []);
  37. if (!empty($addTimes)) {
  38. $addTimes[0] = isset($addTimes[0]) ? strtotime($addTimes[0]) : time();
  39. $addTimes[1] = isset($addTimes[1]) ? strtotime($addTimes[1]) : time();
  40. $where['coupon_detail_addtimes'] = $addTimes;
  41. }
  42. $query = $this->doSelect($where, $field, $order, $request);
  43. return $this->doFormat($query, $format, $limit);
  44. }
  45. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  46. {
  47. $request = \request();
  48. $model = $this->model->with([
  49. 'detail' => function ($query) {
  50. $query->select('join_detail_coupon_id', 'coupon_detail_status', 'coupon_detail_gain_datetime', 'coupon_detail_used_datetime', 'coupon_detail_remark');
  51. },
  52. 'coupon',
  53. 'member' => function ($query) {
  54. $query->select('member_id', 'member_mobile');
  55. },
  56. 'cert' => function ($query) {
  57. $query->select('join_cert_member_id', 'member_cert_name');
  58. },
  59. 'info' => function ($query) {
  60. $query->select('join_info_member_id', 'member_info_nickname');
  61. }
  62. ]);
  63. if (!empty($request->get('coupon_classify'))) {
  64. $model = $model->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  65. ->select('coupon.coupon_classify', 'coupon_detail.*')
  66. ->where('coupon_classify', $request->get('coupon_classify'));
  67. }
  68. if (!empty($request->get('get_type'))) {
  69. if ($request->get('get_type') == 'SEND') {
  70. $model = $model->where(function ($query2) {
  71. $query2->whereJsonContains("coupon_detail_extend_json->gettype", 'SEND')
  72. ->orWhereJsonContains("coupon_detail_extend_json->gettype", 'SYSTEM');
  73. });
  74. } else {
  75. $model = $model->whereJsonContains("coupon_detail_extend_json->gettype", $request->get('get_type'));
  76. }
  77. }
  78. foreach ($where as $column => $value) {
  79. if (is_array($value)) {
  80. if ($value[0] === 'like' || $value[0] === 'not like') {
  81. $model = $model->where($column, $value[0], "%$value[1]%");
  82. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  83. $model = $model->where($column, $value[0], $value[1]);
  84. } elseif ($value[0] == 'in' && !empty($value[1])) {
  85. $valArr = $value[1];
  86. if (is_string($value[1])) {
  87. $valArr = explode(",", trim($value[1]));
  88. }
  89. $model = $model->whereIn($column, $valArr);
  90. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  91. $valArr = $value[1];
  92. if (is_string($value[1])) {
  93. $valArr = explode(",", trim($value[1]));
  94. }
  95. $model = $model->whereNotIn($column, $valArr);
  96. } elseif ($value[0] == 'null') {
  97. $model = $model->whereNull($column);
  98. } elseif ($value[0] == 'not null') {
  99. $model = $model->whereNotNull($column);
  100. } elseif ($value[0] !== '' || $value[1] !== '') {
  101. $model = $model->whereBetween($column, $value);
  102. }
  103. } else {
  104. $model = $model->where($column, $value);
  105. }
  106. }
  107. // $model = $model->selectRaw('join_detail_coupon_id,join_coupon_detail_member_id,MAX(coupon_detail_addtimes) as coupon_detail_addtimes,COUNT(*) as total');
  108. // $model = $model->groupBy('join_coupon_detail_member_id', 'join_detail_coupon_id');
  109. if ($field) {
  110. $model = $model->orderBy($field, $order);
  111. }
  112. $model = $model->orderBy('coupon_detail_gain_datetime', 'ASC');
  113. return $model;
  114. }
  115. public function afterQuery($items)
  116. {
  117. foreach ($items as &$item) {
  118. $mobile = $certName = $nickname = '';
  119. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  120. $mobile = $item['member']['member_mobile'];
  121. }
  122. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  123. $certName = $item['cert']['member_cert_name'];
  124. }
  125. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  126. $nickname = $item['info']['member_info_nickname'];
  127. }
  128. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  129. $item['member_used'] = 0;
  130. foreach ($item['detail'] as $detail) {
  131. if ($detail['coupon_detail_status'] == 'USED') {
  132. $item['member_used'] += 1;
  133. }
  134. }
  135. unset($item['detail']);
  136. if (!empty($item['coupon_detail_extend_json'])) {
  137. $couponDetailExtendJson = json_decode($item['coupon_detail_extend_json'], true);
  138. if (isset($couponDetailExtendJson['gettype'])) {
  139. $item['gettype'] = $couponDetailExtendJson['gettype'];
  140. }
  141. }
  142. if (!empty($item['coupon_detail_deadline_datetime'])) {
  143. $item['coupon_detail_deadline_datetime'] = date('Y/m/d H:i:s', strtotime($item['coupon_detail_deadline_datetime']));
  144. }
  145. if (!empty($item['coupon_detail_gain_datetime'])) {
  146. $item['coupon_detail_gain_datetime'] = date('Y/m/d H:i:s', strtotime($item['coupon_detail_gain_datetime']));
  147. }
  148. }
  149. return $items;
  150. }
  151. /**
  152. * @Desc 优惠券详情顶部统计
  153. * @Author Gorden
  154. * @Date 2024/11/1 11:33
  155. *
  156. * @param Request $request
  157. * @return Response
  158. */
  159. public function statistics(Request $request)
  160. {
  161. $params = $request->get();
  162. $couponDetailModel = CouponDetail::leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  163. ->when(!empty($params['coupon_classify']), function ($query) use ($params) {
  164. $query->where('coupon.coupon_classify', $params['coupon_classify']);
  165. })->when(!empty($params['coupon_detail_status']), function ($query) use ($params) {
  166. $query->where('coupon_detail_status', $params['coupon_detail_status']);
  167. })->when(!empty($params['get_type']), function ($query) use ($params) {
  168. if ($params['get_type'] == 'SEND') {
  169. $query->where(function ($query2) {
  170. $query2->whereJsonContains('coupon_detail_extend_json->gettype', 'SEND')
  171. ->orWhereJsonContains('coupon_detail_extend_json->gettype', 'SYSTEN');
  172. });
  173. } else {
  174. $query->whereJsonContains('coupon_detail_extend_json->gettype', $params['get_type']);
  175. }
  176. })->when(!empty($params['coupon_name']), function ($query) use ($params) {
  177. $query->where('coupon_name', 'like', '%' . trim($params['coupon_name']) . '%');
  178. })->when(!empty($params['join_coupon_detail_member_id']), function ($query) use ($params) {
  179. $query->where('join_coupon_detail_member_id', $params['join_coupon_detail_member_id']);
  180. })->when(!empty($params['coupon_detail_addtimes']), function ($query) use ($params) {
  181. $params['coupon_detail_addtimes'][0] = strtotime($params['coupon_detail_addtimes'][0]);
  182. $params['coupon_detail_addtimes'][1] = strtotime($params['coupon_detail_addtimes'][1]);
  183. $query->whereBetween('coupon_detail_addtimes', $params['coupon_detail_addtimes']);
  184. });
  185. $totalModel = clone $couponDetailModel;
  186. $total = $totalModel->count('coupon_detail_id');
  187. $unUsedTotalModel = clone $couponDetailModel;
  188. $unUsedTotal = $unUsedTotalModel->whereIn('coupon_detail_status', ['ACTIVED'])->count('coupon_detail_id');
  189. $waitingTotalModel = clone $couponDetailModel;
  190. $waitingTotal = $waitingTotalModel->whereIn('coupon_detail_status', ['WAITING'])->count('coupon_detail_id');
  191. $usedTotalModel = clone $couponDetailModel;
  192. $usedTotal = $usedTotalModel->whereIn('coupon_detail_status', ['USED'])->count('coupon_detail_id');
  193. $disabledTotalModel = clone $couponDetailModel;
  194. $disabledTotal = $disabledTotalModel->whereIn('coupon_detail_status', ['DISABLED'])->count('coupon_detail_id');
  195. $expiredTotalModel = clone $couponDetailModel;
  196. $expiredTotal = $expiredTotalModel->whereIn('coupon_detail_status', ['EXPIRED'])->count('coupon_detail_id');
  197. $pendingTotalModel = clone $couponDetailModel;
  198. $pendingTotal = $pendingTotalModel->whereIn('coupon_detail_status', ['INIT', 'PENDING'])->count('coupon_detail_id');
  199. return json_success('success', [
  200. 'total' => $total,
  201. 'usedTotal' => $usedTotal,
  202. 'unUsedTotal' => $unUsedTotal,
  203. 'disabledTotal' => $disabledTotal,
  204. 'expiredTotal' => $expiredTotal,
  205. 'waitingTotal' => $waitingTotal,
  206. 'pendingTotal' => $pendingTotal
  207. ]);
  208. }
  209. /**
  210. * @Desc 修改状态
  211. * @Author Gorden
  212. * @Date 2024/8/22 8:44
  213. *
  214. * @param Request $request
  215. * @return Response
  216. */
  217. public function updateStatus(Request $request)
  218. {
  219. $detailId = $request->post('coupon_detail_id', '');
  220. $status = $request->post('status', '');
  221. $detail = CouponDetail::where('coupon_detail_id', $detailId)->first();
  222. if (!$detail) {
  223. return json_fail('数据不存在');
  224. }
  225. //状态 INIT|PENDING=等待领取|ACTIVED=可用|WAITING=已占用(待完成)|USED=已使用|DISABLED|CANCEL|EXPIRED
  226. if (!in_array($status, ['INIT', 'PENDING', 'ACTIVED', 'WAITING', 'USED', 'DISABLED', 'CANCEL', 'EXPIRED'])) {
  227. return json_fail("状态异常");
  228. }
  229. try {
  230. CouponDetail::where('coupon_detail_id', $detailId)->update(['coupon_detail_status' => $status]);
  231. return json_success('success');
  232. } catch (\Exception $e) {
  233. dump($e->getMessage());
  234. return json_fail('数据修改失败');
  235. }
  236. }
  237. /**
  238. * @Desc 详情
  239. * @Author Gorden
  240. * @Date 2024/8/22 11:46
  241. *
  242. * @param Request $request
  243. * @return Response
  244. */
  245. public function info(Request $request): Response
  246. {
  247. $detailId = $request->get('detail_id', '');
  248. if (empty($detailId)) {
  249. return json_fail('参数异常');
  250. }
  251. $detail = CouponDetail::where('coupon_detail_id', $detailId)
  252. ->select('coupon_detail_id', 'coupon_detail_extend_json', 'join_coupon_detail_member_id')
  253. ->first();
  254. if (!empty($detail->coupon_detail_extend_json)) {
  255. $couponDetailExtendJson = json_decode($detail->coupon_detail_extend_json, true);
  256. if (isset($couponDetailExtendJson['from'])) {
  257. $detail['from'] = MemberService::getMemberNameByMemberId($couponDetailExtendJson['from']);
  258. }
  259. }
  260. $member = Member::with([
  261. 'info' => function ($query) {
  262. $query->select('join_info_member_id', 'member_info_headimg');
  263. },
  264. 'role' => function ($query) {
  265. $query->select('member_role_id', 'member_role_name');
  266. }
  267. ])->where('member_id', $detail->join_coupon_detail_member_id)
  268. ->select('member_id', 'join_member_role_id', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer')
  269. ->first();
  270. $member->info->member_info_headimg = MemberService::getAvatarUrl(!empty($member->info) && !empty($member->info->member_info_headimg) ? $member->info->member_info_headimg : '');
  271. $detail->member = $member;
  272. return json_success('', $detail);
  273. }
  274. public function myCouponGrouping(Request $request)
  275. {
  276. $memberId = $request->get('member_id', '');
  277. $goods = $request->get('goods', []);
  278. $settlementNow = $request->get('settlement_now', 'N');
  279. if ($memberId == '') {
  280. return json_fail("参数异常");
  281. }
  282. $goodsIds = array_column($goods, 'goods_id');
  283. $couponGoods = CouponGoods::whereIn('join_coupon_goods_id', $goodsIds)->get()->toArray();
  284. $couponIds = [];
  285. foreach ($couponGoods as $couponGood) {
  286. foreach ($goods as $good) {
  287. if ($good['sku_id'] = $couponGood['join_coupon_goods_sku_id']) {
  288. $couponIds[] = $couponGood['join_goods_coupon_id'];
  289. }
  290. }
  291. }
  292. $details = CouponDetail::with([
  293. 'coupon' => function ($query) {
  294. $query->select('coupon_id', 'coupon_name', 'coupon_classify', 'coupon_value', 'coupon_category', 'coupon_minimum_limit');
  295. }
  296. ])->whereIn('join_detail_coupon_id', $couponIds)
  297. ->where('join_coupon_detail_member_id', $memberId)
  298. ->where('coupon_detail_gain_datetime', '<', date('Y-m-d H:i:s'))
  299. ->where('coupon_detail_deadline_datetime', '>', date('Y-m-d H:i:s'));
  300. if ($settlementNow == 'Y') {
  301. $details = $details->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']);
  302. } else {
  303. $details = $details->where('coupon_detail.coupon_detail_status', 'ACTIVED');
  304. }
  305. $details = $details->selectRaw('join_detail_coupon_id,join_coupon_detail_member_id,COUNT(*) as count')
  306. ->groupBy('join_detail_coupon_id', 'join_coupon_detail_member_id')
  307. ->get()
  308. ->toArray();
  309. $data = [];
  310. foreach ($details as $detail) {
  311. if (empty($detail['coupon'])) {
  312. continue;
  313. }
  314. $classify = CouponService::couponClassifyInfo($detail['coupon']['coupon_classify'], $detail['coupon']['coupon_category'], $detail['coupon']['coupon_value'], $detail['coupon']['coupon_minimum_limit']);
  315. $data[] = [
  316. 'coupon_id' => $detail['coupon']['coupon_id'],
  317. 'coupon_name' => $detail['coupon']['coupon_name'],
  318. 'classify' => $classify,
  319. 'count' => $detail['count'],
  320. 'coupon_classify' => $detail['coupon']['coupon_classify'],
  321. 'coupon_value' => $detail['coupon']['coupon_value'] ?? '',
  322. 'coupon_minimum_limit' => $detail['coupon']['coupon_minimum_limit'] ?? '',
  323. ];
  324. }
  325. return json_success('', $data);
  326. }
  327. /**
  328. * @Desc 推广的优惠券
  329. * @Author Gorden
  330. * @Date 2024/11/5 13:39
  331. *
  332. * @param Request $request
  333. * @return Response
  334. */
  335. public function promotionCoupon(Request $request)
  336. {
  337. $goods = $request->get('goods', []);
  338. $goodsIds = array_column($goods, 'goods_id');
  339. $goods = array_column($goods, null, 'goods_id');
  340. $coupons = Coupon::where('coupon_category', 'PROMOTION')
  341. ->where('coupon_status', 'ACTIVED')
  342. ->select('coupon_id')
  343. ->get()
  344. ->toArray();
  345. $data = [];
  346. foreach ($coupons as $coupon) {
  347. $couponGoods = CouponGoods::whereIn('join_coupon_goods_id', $goodsIds)
  348. ->where('join_goods_coupon_id', $coupon['coupon_id'])
  349. ->select('join_goods_coupon_id', 'join_coupon_goods_id')
  350. ->get()
  351. ->toArray();
  352. $money = 0;
  353. $moneyGoodsIds = [];
  354. foreach ($couponGoods as $couponGood) {
  355. if (isset($goods[$couponGood['join_coupon_goods_id']]) && !in_array($couponGood['join_coupon_goods_id'], $moneyGoodsIds)) {
  356. $good = $goods[$couponGood['join_coupon_goods_id']];
  357. $money = $money + ($good['price'] * $good['nbr']);
  358. $moneyGoodsIds[] = $couponGood['join_coupon_goods_id'];
  359. }
  360. }
  361. if ($money <= 0) {
  362. continue;
  363. }
  364. $promotionCoupon = Coupon::where('coupon_id', $coupon['coupon_id'])
  365. ->where(function ($query) use ($money) {
  366. $query->where('coupon_minimum_limit', 0)
  367. ->orWhere('coupon_minimum_limit', '<=', $money);
  368. })->select('coupon_id', 'coupon_name', 'coupon_minimum_limit', 'coupon_classify', 'coupon_value', 'coupon_category', 'coupon_priority')
  369. ->first();
  370. if (empty($promotionCoupon)) {
  371. continue;
  372. }
  373. $promotionCoupon = $promotionCoupon->toArray();
  374. $classify = CouponService::couponClassifyInfo($promotionCoupon['coupon_classify'], $promotionCoupon['coupon_category'], $promotionCoupon['coupon_value'], $promotionCoupon['coupon_minimum_limit']);
  375. $data[] = [
  376. 'coupon_id' => $promotionCoupon['coupon_id'],
  377. 'coupon_name' => $promotionCoupon['coupon_name'],
  378. 'classify' => $classify,
  379. 'count' => 1,
  380. 'coupon_classify' => $promotionCoupon['coupon_classify'],
  381. 'coupon_value' => $promotionCoupon['coupon_value'] ?? '',
  382. 'coupon_minimum_limit' => $promotionCoupon['coupon_minimum_limit'] ?? '',
  383. 'priority' => $promotionCoupon['coupon_priority']
  384. ];
  385. }
  386. return json_success('', $data);
  387. }
  388. /**
  389. * @Desc 导出优惠券明细
  390. * @Author Gorden
  391. * @Date 2024/11/1 9:19
  392. *
  393. * @param Request $request
  394. * @return Response
  395. */
  396. public function export(Request $request)
  397. {
  398. $couponName = $request->get('coupon_name', '');
  399. $couponClassify = $request->get('coupon_classify');
  400. $getType = $request->get('get_type');
  401. $couponDetailStatus = $request->get('coupon_detail_status');
  402. $memberId = $request->get('join_coupon_detail_member_id');
  403. $couponDetailAddtimes = $request->get('coupon_detail_addtimes');
  404. $couponDetailIds = $request->get('coupon_detail_ids', []);
  405. $model = CouponDetail::with([
  406. 'member' => function ($query) {
  407. $query->select('member_id', 'member_mobile', 'join_invite_member_id', 'member_extend_json');
  408. },
  409. 'cert' => function ($query) {
  410. $query->select('join_cert_member_id', 'member_cert_name');
  411. },
  412. 'info' => function ($query) {
  413. $query->select('join_info_member_id', 'member_info_business', 'member_info_service');
  414. }
  415. ])->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id')
  416. ->select('coupon_detail.*', 'coupon.coupon_id', 'coupon.coupon_name', 'coupon.coupon_json', 'coupon.coupon_classify');
  417. if ($couponClassify) {
  418. $model = $model->where('coupon.coupon_classify', $couponClassify);
  419. }
  420. if (!empty($getType)) {
  421. $model = $model->whereJsonContains("coupon_detail_extend_json->gettype", $getType);
  422. }
  423. if (!empty($couponDetailStatus)) {
  424. $model = $model->where('coupon_detail_status', $couponDetailStatus);
  425. } else {
  426. $model = $model->where('coupon_detail_status', '<>', 'DISABLED');
  427. }
  428. if (!empty($memberId)) {
  429. $model = $model->where('join_coupon_detail_member_id', $memberId);
  430. }
  431. if (!empty($couponDetailAddtimes)) {
  432. $couponDetailAddtimes[0] = strtotime($couponDetailAddtimes[0]);
  433. $couponDetailAddtimes[1] = strtotime($couponDetailAddtimes[1]);
  434. $model = $model->whereBetween('coupon_detail_addtimes', $couponDetailAddtimes);
  435. }
  436. if (!empty($couponDetailIds)) {
  437. $model = $model->whereIn('coupon_detail_id', $couponDetailIds);
  438. }
  439. if (!empty($couponName)) {
  440. $couponIds = Coupon::where('coupon_name', 'like', '%' . $couponName . '%')->pluck('coupon_id')->toArray();
  441. $model = $model->whereIn('join_detail_coupon_id', $couponIds);
  442. }
  443. $details = $model->orderBy('coupon_detail_addtimes', 'DESC')
  444. ->orderBy('coupon_detail_gain_datetime', 'ASC')
  445. ->get()
  446. ->toArray();
  447. // 查出所有门店
  448. $depts = SysDept::where('dept_category', '营业场所')->pluck('dept_name', 'dept_id')->toArray();
  449. $data = [];
  450. foreach ($details as $detail) {
  451. $mobile = !empty($detail['member']) ? $detail['member']['member_mobile'] : '';
  452. $certName = !empty($detail['cert']) ? $detail['cert']['member_cert_name'] : '';
  453. $business = !empty($detail['info']) ? $detail['info']['member_info_business'] : '';
  454. $service = !empty($detail['info']) ? $detail['info']['member_info_service'] : '';
  455. // 推荐人
  456. if (!empty($detail['member']) && !empty($detail['member']['join_invite_member_id'])) {
  457. $referrer = Member::with([
  458. 'cert' => function ($query) {
  459. $query->select('join_cert_member_id', 'member_cert_name');
  460. }
  461. ])->where('member_id', $detail['member']['join_invite_member_id'])
  462. ->select('member_id', 'member_mobile')
  463. ->first();
  464. $referrerMobile = !empty($referrer->member_mobile) ? $referrer->member_mobile : '';
  465. $referrerCertName = !empty($referrer->cert) ? $referrer->cert->member_cert_name : '';
  466. $detail['referrer_name'] = MemberService::getMemberCertName($referrerMobile, $referrerCertName, '');
  467. }
  468. // 顾问
  469. if (!empty($detail['member']) && !empty($detail['member']['member_extend_json'])) {
  470. $memberExtendJson = json_decode($detail['member']['member_extend_json'], true);
  471. if (!empty($memberExtendJson['referee'])) {
  472. $consultant = [];
  473. foreach ($memberExtendJson['referee'] as $item) {
  474. $consultant[] = $item;
  475. }
  476. $detail['consultant'] = implode(',', $consultant);
  477. }
  478. }
  479. // 门店
  480. if (!empty($detail['coupon_json'])) {
  481. $couponJson = json_decode($detail['coupon_json'], true);
  482. if (!isset($couponJson['premises'])) {
  483. $detail['premises'] = "所有门店";
  484. } else {
  485. $deptName = [];
  486. foreach ($couponJson['premises'] as $premisesId) {
  487. if (isset($depts[$premisesId]) && !in_array($depts[$premisesId], $deptName)) {
  488. $deptName[] = $depts[$premisesId];
  489. }
  490. }
  491. $detail['premises'] = implode(',', $deptName);
  492. }
  493. }
  494. // 获得方式
  495. if (!empty($detail['coupon_detail_extend_json'])) {
  496. $couponDetailExtendsJson = json_decode($detail['coupon_detail_extend_json'], true);
  497. if (isset($couponDetailExtendsJson['gettype'])) {
  498. $detail['get_type'] = self::$getType[$couponDetailExtendsJson['gettype']] ?? '';
  499. }
  500. }
  501. // 有效期限
  502. $detail['coupon_detail_gain_datetime'] = !empty($detail['coupon_detail_gain_datetime']) ? date('Y/m/d H:i:s', strtotime($detail['coupon_detail_gain_datetime'])) : '';
  503. $detail['coupon_detail_deadline_datetime'] = !empty($detail['coupon_detail_deadline_datetime']) ? date('Y/m/d H:i:s', strtotime($detail['coupon_detail_deadline_datetime'])) : '';
  504. $data[] = [
  505. 'cert_name' => $certName,
  506. 'mobile' => $mobile,
  507. // 'member_name' => MemberService::getMemberCertName($mobile, $certName, ''),
  508. 'referrer_name' => $detail['referrer_name'] ?? '',
  509. 'coupon_name' => $detail['coupon_name'] ?? '',
  510. 'coupon_classify' => $detail['coupon_classify'] ?? '',
  511. 'premises' => $detail['premises'] ?? '',
  512. 'get_type' => $detail['get_type'] ?? '',
  513. 'status' => self::$detailStatus[$detail['coupon_detail_status']] ?? '',
  514. 'coupon_detail_used_datetime' => $detail['coupon_detail_used_datetime'] ?? '',
  515. 'valid_date' => !empty($detail['coupon_detail_gain_datetime']) ? $detail['coupon_detail_gain_datetime'] . ' - ' . $detail['coupon_detail_deadline_datetime'] : '',
  516. 'coupon_detail_addtimes' => $detail['coupon_detail_addtimes'],
  517. 'consultant' => $detail['consultant'] ?? '',
  518. 'business' => $business,
  519. 'service' => $service
  520. ];
  521. }
  522. return json_success('success', $data);
  523. }
  524. /**
  525. * @var string[]
  526. */
  527. public static $getType = [
  528. 'VIP' => '购买VIP',
  529. 'RECEIVE' => '领取',
  530. 'SEND' => '后台发放',
  531. 'GIVE' => '转增',
  532. 'EXCHANGE' => '兑换',
  533. 'COMBINE' => '新客专享',
  534. 'ROLE' => '会员升级',
  535. 'PARTNER' => '会员合伙人',
  536. 'REFERRER' => '康养推荐官',
  537. 'PACKAGE' => '购买套包',
  538. 'SYSTEM' => '后台发放'
  539. ];
  540. /**
  541. * @var string[]
  542. */
  543. public static $detailStatus = [
  544. 'INIT' => '初始状态',
  545. 'PENDING' => '待领取',
  546. 'ACTIVED' => '可用',
  547. 'WAITING' => '待完成',
  548. 'USED' => '已使用',
  549. 'DISABLED' => '禁用',
  550. 'CANCEL' => '取消',
  551. 'EXPIRED' => '过期',
  552. ];
  553. }