CouponDetailController.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace app\admin\controller\coupon;
  3. use app\admin\service\member\MemberService;
  4. use app\controller\Curd;
  5. use app\model\Coupon;
  6. use app\model\CouponDetail;
  7. use support\Request;
  8. use support\Response;
  9. class CouponDetailController extends Curd
  10. {
  11. public function __construct()
  12. {
  13. $this->model = new CouponDetail();
  14. }
  15. public function select(Request $request): Response
  16. {
  17. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  18. // $where['join_coupon_detail_member_id'] = ['<>', ''];
  19. $order = $request->get('order', 'desc');
  20. $field = $field ?? 'coupon_detail_addtimes';
  21. $couponName = $request->get('coupon_name', '');
  22. if (!empty($couponName)) {
  23. $couponIds = Coupon::where('coupon_name', 'like', '%' . $couponName . '%')->pluck('coupon_id')->toArray();
  24. if (empty($couponIds)) {
  25. $couponIds = ['default'];
  26. }
  27. $where['join_detail_coupon_id'] = ['in', $couponIds];
  28. }
  29. $addTimes = $request->get('coupon_detail_addtimes', []);
  30. if (!empty($addTimes)) {
  31. $addTimes[0] = isset($addTimes[0]) ? strtotime($addTimes[0]) : time();
  32. $addTimes[1] = isset($addTimes[1]) ? strtotime($addTimes[1]) : time();
  33. $where['coupon_detail_addtimes'] = $addTimes;
  34. }
  35. $query = $this->doSelect($where, $field, $order);
  36. return $this->doFormat($query, $format, $limit);
  37. }
  38. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  39. {
  40. $model = $this->model->with([
  41. 'detail' => function ($query) {
  42. $query->select('join_detail_coupon_id', 'coupon_detail_status', 'coupon_detail_gain_datetime', 'coupon_detail_used_datetime', 'coupon_detail_remark');
  43. },
  44. 'coupon',
  45. 'member' => function ($query) {
  46. $query->select('member_id', 'member_mobile');
  47. },
  48. 'cert' => function ($query) {
  49. $query->select('join_cert_member_id', 'member_cert_name');
  50. },
  51. 'info' => function ($query) {
  52. $query->select('join_info_member_id', 'member_info_nickname');
  53. }
  54. ]);
  55. foreach ($where as $column => $value) {
  56. if (is_array($value)) {
  57. if ($value[0] === 'like' || $value[0] === 'not like') {
  58. $model = $model->where($column, $value[0], "%$value[1]%");
  59. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  60. $model = $model->where($column, $value[0], $value[1]);
  61. } elseif ($value[0] == 'in' && !empty($value[1])) {
  62. $valArr = $value[1];
  63. if (is_string($value[1])) {
  64. $valArr = explode(",", trim($value[1]));
  65. }
  66. $model = $model->whereIn($column, $valArr);
  67. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  68. $valArr = $value[1];
  69. if (is_string($value[1])) {
  70. $valArr = explode(",", trim($value[1]));
  71. }
  72. $model = $model->whereNotIn($column, $valArr);
  73. } elseif ($value[0] == 'null') {
  74. $model = $model->whereNull($column);
  75. } elseif ($value[0] == 'not null') {
  76. $model = $model->whereNotNull($column);
  77. } elseif ($value[0] !== '' || $value[1] !== '') {
  78. $model = $model->whereBetween($column, $value);
  79. }
  80. } else {
  81. $model = $model->where($column, $value);
  82. }
  83. }
  84. // $model = $model->selectRaw('join_detail_coupon_id,join_coupon_detail_member_id,MAX(coupon_detail_addtimes) as coupon_detail_addtimes,COUNT(*) as total');
  85. // $model = $model->groupBy('join_coupon_detail_member_id', 'join_detail_coupon_id');
  86. if ($field) {
  87. $model = $model->orderBy($field, $order);
  88. }
  89. return $model;
  90. }
  91. public function afterQuery($items)
  92. {
  93. foreach ($items as &$item) {
  94. $mobile = $certName = $nickname = '';
  95. if (!empty($item['member']) && !empty($item['member']['member_mobile'])) {
  96. $mobile = $item['member']['member_mobile'];
  97. }
  98. if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) {
  99. $certName = $item['cert']['member_cert_name'];
  100. }
  101. if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) {
  102. $nickname = $item['info']['member_info_nickname'];
  103. }
  104. $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
  105. $item['member_used'] = 0;
  106. foreach ($item['detail'] as $detail) {
  107. if ($detail['coupon_detail_status'] == 'USED') {
  108. $item['member_used'] += 1;
  109. }
  110. }
  111. if (!empty($item['coupon_detail_extend_json'])) {
  112. $couponDetailExtendJson = json_decode($item['coupon_detail_extend_json'], true);
  113. if (isset($couponDetailExtendJson['gettype'])){
  114. $item['gettype'] = $couponDetailExtendJson['gettype'];
  115. }
  116. }
  117. }
  118. return $items;
  119. }
  120. /**
  121. * @Desc 修改状态
  122. * @Author Gorden
  123. * @Date 2024/8/22 8:44
  124. *
  125. * @param Request $request
  126. * @return Response
  127. */
  128. public function updateStatus(Request $request)
  129. {
  130. $detailId = $request->post('coupon_detail_id', '');
  131. $status = $request->post('status', '');
  132. $detail = CouponDetail::where('coupon_detail_id', $detailId)->first();
  133. if (!$detail) {
  134. return json_fail('数据不存在');
  135. }
  136. //状态 INIT|PENDING=等待领取|ACTIVED=可用|WAITING=已占用(待完成)|USED=已使用|DISABLED|CANCEL|EXPIRED
  137. if (!in_array($status, ['INIT', 'PENDING', 'ACTIVED', 'WAITING', 'USED', 'DISABLED', 'CANCEL', 'EXPIRED'])) {
  138. return json_fail("状态异常");
  139. }
  140. try {
  141. CouponDetail::where('coupon_detail_id', $detailId)->update(['coupon_detail_status' => $status]);
  142. return json_success('success');
  143. } catch (\Exception $e) {
  144. dump($e->getMessage());
  145. return json_fail('数据修改失败');
  146. }
  147. }
  148. /**
  149. * @Desc 详情
  150. * @Author Gorden
  151. * @Date 2024/8/22 11:46
  152. *
  153. * @param Request $request
  154. * @return Response
  155. */
  156. public function info(Request $request): Response
  157. {
  158. $detailId = $request->get('detail_id','');
  159. if (empty($detailId)){
  160. return json_fail('参数异常');
  161. }
  162. $detail = CouponDetail::where('coupon_detail_id',$detailId)
  163. ->select('coupon_detail_id','coupon_detail_extend_json')
  164. ->first();
  165. if (!empty($detail->coupon_detail_extend_json)){
  166. $couponDetailExtendJson = json_decode($detail->coupon_detail_extend_json,true);
  167. if (isset($couponDetailExtendJson['from'])){
  168. $detail['from'] = MemberService::getMemberNameByMemberId($couponDetailExtendJson['from']);
  169. }
  170. }
  171. return json_success('',$detail);
  172. }
  173. }