CouponDetailController.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 support\Request;
  10. use support\Response;
  11. class CouponDetailController extends Curd
  12. {
  13. public function __construct()
  14. {
  15. $this->model = new CouponDetail();
  16. }
  17. public function select(Request $request): Response
  18. {
  19. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  20. // $where['join_coupon_detail_member_id'] = ['<>', ''];
  21. $order = $request->get('order', 'desc');
  22. $field = $field ?? 'coupon_detail_addtimes';
  23. $couponName = $request->get('coupon_name', '');
  24. if (!empty($couponName)) {
  25. $couponIds = Coupon::where('coupon_name', 'like', '%' . $couponName . '%')->pluck('coupon_id')->toArray();
  26. if (empty($couponIds)) {
  27. $couponIds = ['default'];
  28. }
  29. $where['join_detail_coupon_id'] = ['in', $couponIds];
  30. }
  31. $addTimes = $request->get('coupon_detail_addtimes', []);
  32. if (!empty($addTimes)) {
  33. $addTimes[0] = isset($addTimes[0]) ? strtotime($addTimes[0]) : time();
  34. $addTimes[1] = isset($addTimes[1]) ? strtotime($addTimes[1]) : time();
  35. $where['coupon_detail_addtimes'] = $addTimes;
  36. }
  37. $query = $this->doSelect($where, $field, $order);
  38. return $this->doFormat($query, $format, $limit);
  39. }
  40. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  41. {
  42. $model = $this->model->with([
  43. 'detail' => function ($query) {
  44. $query->select('join_detail_coupon_id', 'coupon_detail_status', 'coupon_detail_gain_datetime', 'coupon_detail_used_datetime', 'coupon_detail_remark');
  45. },
  46. 'coupon',
  47. 'member' => function ($query) {
  48. $query->select('member_id', 'member_mobile');
  49. },
  50. 'cert' => function ($query) {
  51. $query->select('join_cert_member_id', 'member_cert_name');
  52. },
  53. 'info' => function ($query) {
  54. $query->select('join_info_member_id', 'member_info_nickname');
  55. }
  56. ]);
  57. foreach ($where as $column => $value) {
  58. if (is_array($value)) {
  59. if ($value[0] === 'like' || $value[0] === 'not like') {
  60. $model = $model->where($column, $value[0], "%$value[1]%");
  61. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  62. $model = $model->where($column, $value[0], $value[1]);
  63. } elseif ($value[0] == 'in' && !empty($value[1])) {
  64. $valArr = $value[1];
  65. if (is_string($value[1])) {
  66. $valArr = explode(",", trim($value[1]));
  67. }
  68. $model = $model->whereIn($column, $valArr);
  69. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  70. $valArr = $value[1];
  71. if (is_string($value[1])) {
  72. $valArr = explode(",", trim($value[1]));
  73. }
  74. $model = $model->whereNotIn($column, $valArr);
  75. } elseif ($value[0] == 'null') {
  76. $model = $model->whereNull($column);
  77. } elseif ($value[0] == 'not null') {
  78. $model = $model->whereNotNull($column);
  79. } elseif ($value[0] !== '' || $value[1] !== '') {
  80. $model = $model->whereBetween($column, $value);
  81. }
  82. } else {
  83. $model = $model->where($column, $value);
  84. }
  85. }
  86. // $model = $model->selectRaw('join_detail_coupon_id,join_coupon_detail_member_id,MAX(coupon_detail_addtimes) as coupon_detail_addtimes,COUNT(*) as total');
  87. // $model = $model->groupBy('join_coupon_detail_member_id', 'join_detail_coupon_id');
  88. if ($field) {
  89. $model = $model->orderBy($field, $order);
  90. }
  91. return $model;
  92. }
  93. public function afterQuery($items)
  94. {
  95. foreach ($items as &$item) {
  96. $mobile = $certName = $nickname = '';
  97. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  98. $mobile = $item['member']['member_mobile'];
  99. }
  100. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  101. $certName = $item['cert']['member_cert_name'];
  102. }
  103. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  104. $nickname = $item['info']['member_info_nickname'];
  105. }
  106. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  107. $item['member_used'] = 0;
  108. foreach ($item['detail'] as $detail) {
  109. if ($detail['coupon_detail_status'] == 'USED') {
  110. $item['member_used'] += 1;
  111. }
  112. }
  113. if (!empty($item['coupon_detail_extend_json'])) {
  114. $couponDetailExtendJson = json_decode($item['coupon_detail_extend_json'], true);
  115. if (isset($couponDetailExtendJson['gettype'])) {
  116. $item['gettype'] = $couponDetailExtendJson['gettype'];
  117. }
  118. }
  119. }
  120. return $items;
  121. }
  122. /**
  123. * @Desc 修改状态
  124. * @Author Gorden
  125. * @Date 2024/8/22 8:44
  126. *
  127. * @param Request $request
  128. * @return Response
  129. */
  130. public function updateStatus(Request $request)
  131. {
  132. $detailId = $request->post('coupon_detail_id', '');
  133. $status = $request->post('status', '');
  134. $detail = CouponDetail::where('coupon_detail_id', $detailId)->first();
  135. if (!$detail) {
  136. return json_fail('数据不存在');
  137. }
  138. //状态 INIT|PENDING=等待领取|ACTIVED=可用|WAITING=已占用(待完成)|USED=已使用|DISABLED|CANCEL|EXPIRED
  139. if (!in_array($status, ['INIT', 'PENDING', 'ACTIVED', 'WAITING', 'USED', 'DISABLED', 'CANCEL', 'EXPIRED'])) {
  140. return json_fail("状态异常");
  141. }
  142. try {
  143. CouponDetail::where('coupon_detail_id', $detailId)->update(['coupon_detail_status' => $status]);
  144. return json_success('success');
  145. } catch (\Exception $e) {
  146. dump($e->getMessage());
  147. return json_fail('数据修改失败');
  148. }
  149. }
  150. /**
  151. * @Desc 详情
  152. * @Author Gorden
  153. * @Date 2024/8/22 11:46
  154. *
  155. * @param Request $request
  156. * @return Response
  157. */
  158. public function info(Request $request): Response
  159. {
  160. $detailId = $request->get('detail_id', '');
  161. if (empty($detailId)) {
  162. return json_fail('参数异常');
  163. }
  164. $detail = CouponDetail::where('coupon_detail_id', $detailId)
  165. ->select('coupon_detail_id', 'coupon_detail_extend_json')
  166. ->first();
  167. if (!empty($detail->coupon_detail_extend_json)) {
  168. $couponDetailExtendJson = json_decode($detail->coupon_detail_extend_json, true);
  169. if (isset($couponDetailExtendJson['from'])) {
  170. $detail['from'] = MemberService::getMemberNameByMemberId($couponDetailExtendJson['from']);
  171. }
  172. }
  173. return json_success('', $detail);
  174. }
  175. public function myCouponGrouping(Request $request)
  176. {
  177. $memberId = $request->get('member_id', '');
  178. $goods = $request->get('goods', []);
  179. $settlementNow = $request->get('settlement_now', 'N');
  180. if ($memberId == '') {
  181. return json_fail("参数异常");
  182. }
  183. $goodsIds = array_column($goods, 'goods_id');
  184. $couponGoods = CouponGoods::whereIn('join_coupon_goods_id', $goodsIds)->get()->toArray();
  185. $couponIds = [];
  186. foreach ($couponGoods as $couponGood) {
  187. foreach ($goods as $good) {
  188. if ($good['sku_id'] = $couponGood['join_coupon_goods_sku_id']) {
  189. $couponIds[] = $couponGood['join_goods_coupon_id'];
  190. }
  191. }
  192. }
  193. $details = CouponDetail::with([
  194. 'coupon' => function ($query) {
  195. $query->select('coupon_id', 'coupon_name', 'coupon_classify', 'coupon_value', 'coupon_category', 'coupon_minimum_limit');
  196. }
  197. ])->whereIn('join_detail_coupon_id', $couponIds)
  198. ->where('join_coupon_detail_member_id', $memberId);
  199. if ($settlementNow == 'Y') {
  200. $details = $details->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']);
  201. } else {
  202. $details = $details->where('coupon_detail.coupon_detail_status', 'ACTIVED');
  203. }
  204. $details = $details->selectRaw('join_detail_coupon_id,join_coupon_detail_member_id,COUNT(*) as count')
  205. ->groupBy('join_detail_coupon_id', 'join_coupon_detail_member_id')
  206. ->get()
  207. ->toArray();
  208. $data = [];
  209. foreach ($details as $detail) {
  210. if (empty($detail['coupon'])) {
  211. continue;
  212. }
  213. $classify = CouponService::couponClassifyInfo($detail['coupon']['coupon_classify'], $detail['coupon']['coupon_category'], $detail['coupon']['coupon_value'], $detail['coupon']['coupon_minimum_limit']);
  214. $data[] = [
  215. 'coupon_id' => $detail['coupon']['coupon_id'],
  216. 'coupon_name' => $detail['coupon']['coupon_name'],
  217. 'classify' => $classify,
  218. 'count' => $detail['count'],
  219. 'coupon_classify' => $detail['coupon']['coupon_classify'],
  220. 'coupon_value' => $detail['coupon']['coupon_value'] ?? '',
  221. 'coupon_minimum_limit' => $detail['coupon']['coupon_minimum_limit'] ?? '',
  222. ];
  223. }
  224. return json_success('', $data);
  225. }
  226. }