model = new CouponDetail(); } public function select(Request $request): Response { [$where, $format, $limit, $field, $order] = $this->selectInput($request); // $where['join_coupon_detail_member_id'] = ['<>', '']; $order = $request->get('order', 'desc'); $field = $field ?? 'coupon_detail_addtimes'; $couponName = $request->get('coupon_name', ''); if (!empty($couponName)) { $couponIds = Coupon::where('coupon_name', 'like', '%' . $couponName . '%')->pluck('coupon_id')->toArray(); if (empty($couponIds)) { $couponIds = ['default']; } $where['join_detail_coupon_id'] = ['in', $couponIds]; } $addTimes = $request->get('coupon_detail_addtimes', []); if (!empty($addTimes)) { $addTimes[0] = isset($addTimes[0]) ? strtotime($addTimes[0]) : time(); $addTimes[1] = isset($addTimes[1]) ? strtotime($addTimes[1]) : time(); $where['coupon_detail_addtimes'] = $addTimes; } $query = $this->doSelect($where, $field, $order); return $this->doFormat($query, $format, $limit); } protected function doSelect(array $where, string $field = null, string $order = 'desc') { $model = $this->model->with([ 'detail' => function ($query) { $query->select('join_detail_coupon_id', 'coupon_detail_status', 'coupon_detail_gain_datetime', 'coupon_detail_used_datetime', 'coupon_detail_remark'); }, 'coupon', 'member' => function ($query) { $query->select('member_id', 'member_mobile'); }, 'cert' => function ($query) { $query->select('join_cert_member_id', 'member_cert_name'); }, 'info' => function ($query) { $query->select('join_info_member_id', 'member_info_nickname'); } ]); foreach ($where as $column => $value) { if (is_array($value)) { if ($value[0] === 'like' || $value[0] === 'not like') { $model = $model->where($column, $value[0], "%$value[1]%"); } elseif (in_array($value[0], ['>', '=', '<', '<>'])) { $model = $model->where($column, $value[0], $value[1]); } elseif ($value[0] == 'in' && !empty($value[1])) { $valArr = $value[1]; if (is_string($value[1])) { $valArr = explode(",", trim($value[1])); } $model = $model->whereIn($column, $valArr); } elseif ($value[0] == 'not in' && !empty($value[1])) { $valArr = $value[1]; if (is_string($value[1])) { $valArr = explode(",", trim($value[1])); } $model = $model->whereNotIn($column, $valArr); } elseif ($value[0] == 'null') { $model = $model->whereNull($column); } elseif ($value[0] == 'not null') { $model = $model->whereNotNull($column); } elseif ($value[0] !== '' || $value[1] !== '') { $model = $model->whereBetween($column, $value); } } else { $model = $model->where($column, $value); } } // $model = $model->selectRaw('join_detail_coupon_id,join_coupon_detail_member_id,MAX(coupon_detail_addtimes) as coupon_detail_addtimes,COUNT(*) as total'); // $model = $model->groupBy('join_coupon_detail_member_id', 'join_detail_coupon_id'); if ($field) { $model = $model->orderBy($field, $order); } return $model; } public function afterQuery($items) { foreach ($items as &$item) { $mobile = $certName = $nickname = ''; if (!empty($item['member']) && !empty($item['member']['member_mobile'])) { $mobile = $item['member']['member_mobile']; } if (!empty($item['cert']) && !empty($item['cert']['member_cert_name'])) { $certName = $item['cert']['member_cert_name']; } if (!empty($item['info']) && !empty($item['info']['member_info_nickname'])) { $nickname = $item['info']['member_info_nickname']; } $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname); $item['member_used'] = 0; foreach ($item['detail'] as $detail) { if ($detail['coupon_detail_status'] == 'USED') { $item['member_used'] += 1; } } if (!empty($item['coupon_detail_extend_json'])) { $couponDetailExtendJson = json_decode($item['coupon_detail_extend_json'], true); if (isset($couponDetailExtendJson['gettype'])) { $item['gettype'] = $couponDetailExtendJson['gettype']; } } if (!empty($item['coupon_detail_deadline_datetime'])) { $item['coupon_detail_deadline_datetime'] = date('Y/m/d H:i:s', strtotime($item['coupon_detail_deadline_datetime'])); } if (!empty($item['coupon_detail_gain_datetime'])) { $item['coupon_detail_gain_datetime'] = date('Y/m/d H:i:s', strtotime($item['coupon_detail_gain_datetime'])); } } return $items; } /** * @Desc 修改状态 * @Author Gorden * @Date 2024/8/22 8:44 * * @param Request $request * @return Response */ public function updateStatus(Request $request) { $detailId = $request->post('coupon_detail_id', ''); $status = $request->post('status', ''); $detail = CouponDetail::where('coupon_detail_id', $detailId)->first(); if (!$detail) { return json_fail('数据不存在'); } //状态 INIT|PENDING=等待领取|ACTIVED=可用|WAITING=已占用(待完成)|USED=已使用|DISABLED|CANCEL|EXPIRED if (!in_array($status, ['INIT', 'PENDING', 'ACTIVED', 'WAITING', 'USED', 'DISABLED', 'CANCEL', 'EXPIRED'])) { return json_fail("状态异常"); } try { CouponDetail::where('coupon_detail_id', $detailId)->update(['coupon_detail_status' => $status]); return json_success('success'); } catch (\Exception $e) { dump($e->getMessage()); return json_fail('数据修改失败'); } } /** * @Desc 详情 * @Author Gorden * @Date 2024/8/22 11:46 * * @param Request $request * @return Response */ public function info(Request $request): Response { $detailId = $request->get('detail_id', ''); if (empty($detailId)) { return json_fail('参数异常'); } $detail = CouponDetail::where('coupon_detail_id', $detailId) ->select('coupon_detail_id', 'coupon_detail_extend_json') ->first(); if (!empty($detail->coupon_detail_extend_json)) { $couponDetailExtendJson = json_decode($detail->coupon_detail_extend_json, true); if (isset($couponDetailExtendJson['from'])) { $detail['from'] = MemberService::getMemberNameByMemberId($couponDetailExtendJson['from']); } } return json_success('', $detail); } public function myCouponGrouping(Request $request) { $memberId = $request->get('member_id', ''); $goods = $request->get('goods', []); $settlementNow = $request->get('settlement_now', 'N'); if ($memberId == '') { return json_fail("参数异常"); } $goodsIds = array_column($goods, 'goods_id'); $couponGoods = CouponGoods::whereIn('join_coupon_goods_id', $goodsIds)->get()->toArray(); $couponIds = []; foreach ($couponGoods as $couponGood) { foreach ($goods as $good) { if ($good['sku_id'] = $couponGood['join_coupon_goods_sku_id']) { $couponIds[] = $couponGood['join_goods_coupon_id']; } } } $details = CouponDetail::with([ 'coupon' => function ($query) { $query->select('coupon_id', 'coupon_name', 'coupon_classify', 'coupon_value', 'coupon_category', 'coupon_minimum_limit'); } ])->whereIn('join_detail_coupon_id', $couponIds) ->where('join_coupon_detail_member_id', $memberId) ->where('coupon_detail_gain_datetime','<',date('Y-m-d H:i:s')) ->where('coupon_detail_deadline_datetime','>',date('Y-m-d H:i:s')); if ($settlementNow == 'Y') { $details = $details->whereIn('coupon_detail.coupon_detail_status', ['ACTIVED', 'WAITING']); } else { $details = $details->where('coupon_detail.coupon_detail_status', 'ACTIVED'); } $details = $details->selectRaw('join_detail_coupon_id,join_coupon_detail_member_id,COUNT(*) as count') ->groupBy('join_detail_coupon_id', 'join_coupon_detail_member_id') ->get() ->toArray(); $data = []; foreach ($details as $detail) { if (empty($detail['coupon'])) { continue; } $classify = CouponService::couponClassifyInfo($detail['coupon']['coupon_classify'], $detail['coupon']['coupon_category'], $detail['coupon']['coupon_value'], $detail['coupon']['coupon_minimum_limit']); $data[] = [ 'coupon_id' => $detail['coupon']['coupon_id'], 'coupon_name' => $detail['coupon']['coupon_name'], 'classify' => $classify, 'count' => $detail['count'], 'coupon_classify' => $detail['coupon']['coupon_classify'], 'coupon_value' => $detail['coupon']['coupon_value'] ?? '', 'coupon_minimum_limit' => $detail['coupon']['coupon_minimum_limit'] ?? '', ]; } return json_success('', $data); } }