CouponDetailController.php 28 KB

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