CouponDetailController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. if (!empty($item['coupon_detail_deadline_datetime'])) {
  120. $item['coupon_detail_deadline_datetime'] = date('Y/m/d H:i:s', strtotime($item['coupon_detail_deadline_datetime']));
  121. }
  122. if (!empty($item['coupon_detail_gain_datetime'])) {
  123. $item['coupon_detail_gain_datetime'] = date('Y/m/d H:i:s', strtotime($item['coupon_detail_gain_datetime']));
  124. }
  125. }
  126. return $items;
  127. }
  128. /**
  129. * @Desc 修改状态
  130. * @Author Gorden
  131. * @Date 2024/8/22 8:44
  132. *
  133. * @param Request $request
  134. * @return Response
  135. */
  136. public function updateStatus(Request $request)
  137. {
  138. $detailId = $request->post('coupon_detail_id', '');
  139. $status = $request->post('status', '');
  140. $detail = CouponDetail::where('coupon_detail_id', $detailId)->first();
  141. if (!$detail) {
  142. return json_fail('数据不存在');
  143. }
  144. //状态 INIT|PENDING=等待领取|ACTIVED=可用|WAITING=已占用(待完成)|USED=已使用|DISABLED|CANCEL|EXPIRED
  145. if (!in_array($status, ['INIT', 'PENDING', 'ACTIVED', 'WAITING', 'USED', 'DISABLED', 'CANCEL', 'EXPIRED'])) {
  146. return json_fail("状态异常");
  147. }
  148. try {
  149. CouponDetail::where('coupon_detail_id', $detailId)->update(['coupon_detail_status' => $status]);
  150. return json_success('success');
  151. } catch (\Exception $e) {
  152. dump($e->getMessage());
  153. return json_fail('数据修改失败');
  154. }
  155. }
  156. /**
  157. * @Desc 详情
  158. * @Author Gorden
  159. * @Date 2024/8/22 11:46
  160. *
  161. * @param Request $request
  162. * @return Response
  163. */
  164. public function info(Request $request): Response
  165. {
  166. $detailId = $request->get('detail_id', '');
  167. if (empty($detailId)) {
  168. return json_fail('参数异常');
  169. }
  170. $detail = CouponDetail::where('coupon_detail_id', $detailId)
  171. ->select('coupon_detail_id', 'coupon_detail_extend_json')
  172. ->first();
  173. if (!empty($detail->coupon_detail_extend_json)) {
  174. $couponDetailExtendJson = json_decode($detail->coupon_detail_extend_json, true);
  175. if (isset($couponDetailExtendJson['from'])) {
  176. $detail['from'] = MemberService::getMemberNameByMemberId($couponDetailExtendJson['from']);
  177. }
  178. }
  179. return json_success('', $detail);
  180. }
  181. public function myCouponGrouping(Request $request)
  182. {
  183. $memberId = $request->get('member_id', '');
  184. $goods = $request->get('goods', []);
  185. $settlementNow = $request->get('settlement_now', 'N');
  186. if ($memberId == '') {
  187. return json_fail("参数异常");
  188. }
  189. $goodsIds = array_column($goods, 'goods_id');
  190. $couponGoods = CouponGoods::whereIn('join_coupon_goods_id', $goodsIds)->get()->toArray();
  191. $couponIds = [];
  192. foreach ($couponGoods as $couponGood) {
  193. foreach ($goods as $good) {
  194. if ($good['sku_id'] = $couponGood['join_coupon_goods_sku_id']) {
  195. $couponIds[] = $couponGood['join_goods_coupon_id'];
  196. }
  197. }
  198. }
  199. $details = CouponDetail::with([
  200. 'coupon' => function ($query) {
  201. $query->select('coupon_id', 'coupon_name', 'coupon_classify', 'coupon_value', 'coupon_category', 'coupon_minimum_limit');
  202. }
  203. ])->whereIn('join_detail_coupon_id', $couponIds)
  204. ->where('join_coupon_detail_member_id', $memberId)
  205. ->where('coupon_detail_gain_datetime','<',date('Y-m-d H:i:s'))
  206. ->where('coupon_detail_deadline_datetime','>',date('Y-m-d H:i:s'));
  207. if ($settlementNow == 'Y') {
  208. $details = $details->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']);
  209. } else {
  210. $details = $details->where('coupon_detail.coupon_detail_status', 'ACTIVED');
  211. }
  212. $details = $details->selectRaw('join_detail_coupon_id,join_coupon_detail_member_id,COUNT(*) as count')
  213. ->groupBy('join_detail_coupon_id', 'join_coupon_detail_member_id')
  214. ->get()
  215. ->toArray();
  216. $data = [];
  217. foreach ($details as $detail) {
  218. if (empty($detail['coupon'])) {
  219. continue;
  220. }
  221. $classify = CouponService::couponClassifyInfo($detail['coupon']['coupon_classify'], $detail['coupon']['coupon_category'], $detail['coupon']['coupon_value'], $detail['coupon']['coupon_minimum_limit']);
  222. $data[] = [
  223. 'coupon_id' => $detail['coupon']['coupon_id'],
  224. 'coupon_name' => $detail['coupon']['coupon_name'],
  225. 'classify' => $classify,
  226. 'count' => $detail['count'],
  227. 'coupon_classify' => $detail['coupon']['coupon_classify'],
  228. 'coupon_value' => $detail['coupon']['coupon_value'] ?? '',
  229. 'coupon_minimum_limit' => $detail['coupon']['coupon_minimum_limit'] ?? '',
  230. ];
  231. }
  232. return json_success('', $data);
  233. }
  234. }