model = new CouponDetail(); } public function select(Request $request): Response { [$where, $format, $limit, $field, $order] = $this->selectInput($request); // if (empty($where['coupon_detail_status'])) { // $where['coupon_detail_status'] = ['<>', 'DISABLED']; // } // $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, $request); return $this->doFormat($query, $format, $limit); } protected function doSelect(array $where, string $field = null, string $order = 'desc') { $request = \request(); $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'); } ]); if (!empty($request->get('coupon_classify'))) { $model = $model->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id') ->select('coupon.coupon_classify', 'coupon_detail.*') ->where('coupon_classify', $request->get('coupon_classify')); } if (!empty($request->get('get_type'))) { if ($request->get('get_type') == 'SEND') { $model = $model->where(function ($query2) { $query2->whereJsonContains("coupon_detail_extend_json->gettype", 'SEND') ->orWhereJsonContains("coupon_detail_extend_json->gettype", 'SYSTEM'); }); } else { $model = $model->whereJsonContains("coupon_detail_extend_json->gettype", $request->get('get_type')); } } 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); } $model = $model->orderBy('coupon_detail_gain_datetime', 'ASC'); 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; } } unset($item['detail']); 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/11/1 11:33 * * @param Request $request * @return Response */ public function statistics(Request $request) { $params = $request->get(); $couponDetailModel = CouponDetail::leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id') ->when(!empty($params['coupon_classify']), function ($query) use ($params) { $query->where('coupon.coupon_classify', $params['coupon_classify']); })->when(!empty($params['coupon_detail_status']), function ($query) use ($params) { $query->where('coupon_detail_status', $params['coupon_detail_status']); })->when(!empty($params['get_type']), function ($query) use ($params) { if ($params['get_type'] == 'SEND') { $query->where(function ($query2) { $query2->whereJsonContains('coupon_detail_extend_json->gettype', 'SEND') ->orWhereJsonContains('coupon_detail_extend_json->gettype', 'SYSTEN'); }); } else { $query->whereJsonContains('coupon_detail_extend_json->gettype', $params['get_type']); } })->when(!empty($params['coupon_name']), function ($query) use ($params) { $query->where('coupon_name', 'like', '%' . trim($params['coupon_name']) . '%'); })->when(!empty($params['join_coupon_detail_member_id']), function ($query) use ($params) { $query->where('join_coupon_detail_member_id', $params['join_coupon_detail_member_id']); })->when(!empty($params['coupon_detail_addtimes']), function ($query) use ($params) { $params['coupon_detail_addtimes'][0] = strtotime($params['coupon_detail_addtimes'][0]); $params['coupon_detail_addtimes'][1] = strtotime($params['coupon_detail_addtimes'][1]); $query->whereBetween('coupon_detail_addtimes', $params['coupon_detail_addtimes']); }); $totalModel = clone $couponDetailModel; $total = $totalModel->count('coupon_detail_id'); $unUsedTotalModel = clone $couponDetailModel; $unUsedTotal = $unUsedTotalModel->whereIn('coupon_detail_status', ['ACTIVED'])->count('coupon_detail_id'); $waitingTotalModel = clone $couponDetailModel; $waitingTotal = $waitingTotalModel->whereIn('coupon_detail_status', ['WAITING'])->count('coupon_detail_id'); $usedTotalModel = clone $couponDetailModel; $usedTotal = $usedTotalModel->whereIn('coupon_detail_status', ['USED'])->count('coupon_detail_id'); $disabledTotalModel = clone $couponDetailModel; $disabledTotal = $disabledTotalModel->whereIn('coupon_detail_status', ['DISABLED'])->count('coupon_detail_id'); $expiredTotalModel = clone $couponDetailModel; $expiredTotal = $expiredTotalModel->whereIn('coupon_detail_status', ['EXPIRED'])->count('coupon_detail_id'); $pendingTotalModel = clone $couponDetailModel; $pendingTotal = $pendingTotalModel->whereIn('coupon_detail_status', ['INIT', 'PENDING'])->count('coupon_detail_id'); return json_success('success', [ 'total' => $total, 'usedTotal' => $usedTotal, 'unUsedTotal' => $unUsedTotal, 'disabledTotal' => $disabledTotal, 'expiredTotal' => $expiredTotal, 'waitingTotal' => $waitingTotal, 'pendingTotal' => $pendingTotal ]); } /** * @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', 'join_coupon_detail_member_id') ->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']); } } $member = Member::with([ 'info' => function ($query) { $query->select('join_info_member_id', 'member_info_headimg'); }, 'role' => function ($query) { $query->select('member_role_id', 'member_role_name'); } ])->where('member_id', $detail->join_coupon_detail_member_id) ->select('member_id', 'join_member_role_id', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer') ->first(); $member->info->member_info_headimg = MemberService::getAvatarUrl(!empty($member->info) && !empty($member->info->member_info_headimg) ? $member->info->member_info_headimg : ''); $detail->member = $member; 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); } /** * @Desc 推广的优惠券 * @Author Gorden * @Date 2024/11/5 13:39 * * @param Request $request * @return Response */ public function promotionCoupon(Request $request) { $goods = $request->get('goods', []); $goodsIds = array_column($goods, 'goods_id'); $goods = array_column($goods, null, 'goods_id'); $coupons = Coupon::where('coupon_category', 'PROMOTION') ->where('coupon_status', 'ACTIVED') ->select('coupon_id') ->get() ->toArray(); $data = []; foreach ($coupons as $coupon) { $couponGoods = CouponGoods::whereIn('join_coupon_goods_id', $goodsIds) ->where('join_goods_coupon_id', $coupon['coupon_id']) ->select('join_goods_coupon_id', 'join_coupon_goods_id') ->get() ->toArray(); $money = 0; $moneyGoodsIds = []; foreach ($couponGoods as $couponGood) { if (isset($goods[$couponGood['join_coupon_goods_id']]) && !in_array($couponGood['join_coupon_goods_id'], $moneyGoodsIds)) { $good = $goods[$couponGood['join_coupon_goods_id']]; $money = $money + ($good['price'] * $good['nbr']); $moneyGoodsIds[] = $couponGood['join_coupon_goods_id']; } } if ($money <= 0) { continue; } $promotionCoupon = Coupon::where('coupon_id', $coupon['coupon_id']) ->where(function ($query) use ($money) { $query->where('coupon_minimum_limit', 0) ->orWhere('coupon_minimum_limit', '<=', $money); })->select('coupon_id', 'coupon_name', 'coupon_minimum_limit', 'coupon_classify', 'coupon_value', 'coupon_category', 'coupon_priority') ->first(); if (empty($promotionCoupon)) { continue; } $promotionCoupon = $promotionCoupon->toArray(); $classify = CouponService::couponClassifyInfo($promotionCoupon['coupon_classify'], $promotionCoupon['coupon_category'], $promotionCoupon['coupon_value'], $promotionCoupon['coupon_minimum_limit']); $data[] = [ 'coupon_id' => $promotionCoupon['coupon_id'], 'coupon_name' => $promotionCoupon['coupon_name'], 'classify' => $classify, 'count' => 1, 'coupon_classify' => $promotionCoupon['coupon_classify'], 'coupon_value' => $promotionCoupon['coupon_value'] ?? '', 'coupon_minimum_limit' => $promotionCoupon['coupon_minimum_limit'] ?? '', 'priority' => $promotionCoupon['coupon_priority'] ]; } return json_success('', $data); } /** * @Desc 导出优惠券明细 * @Author Gorden * @Date 2024/11/1 9:19 * * @param Request $request * @return Response */ public function export(Request $request) { $couponName = $request->get('coupon_name', ''); $couponClassify = $request->get('coupon_classify'); $getType = $request->get('get_type'); $couponDetailStatus = $request->get('coupon_detail_status'); $memberId = $request->get('join_coupon_detail_member_id'); $couponDetailAddtimes = $request->get('coupon_detail_addtimes'); $couponDetailIds = $request->get('coupon_detail_ids', []); $model = CouponDetail::with([ 'member' => function ($query) { $query->select('member_id', 'member_mobile', 'join_invite_member_id', 'member_extend_json'); }, 'cert' => function ($query) { $query->select('join_cert_member_id', 'member_cert_name'); }, 'info' => function ($query) { $query->select('join_info_member_id', 'member_info_business', 'member_info_service'); } ])->leftJoin('coupon', 'coupon.coupon_id', '=', 'coupon_detail.join_detail_coupon_id') ->select('coupon_detail.*', 'coupon.coupon_id', 'coupon.coupon_name', 'coupon.coupon_json', 'coupon.coupon_classify'); if ($couponClassify) { $model = $model->where('coupon.coupon_classify', $couponClassify); } if (!empty($getType)) { $model = $model->whereJsonContains("coupon_detail_extend_json->gettype", $getType); } if (!empty($couponDetailStatus)) { $model = $model->where('coupon_detail_status', $couponDetailStatus); } else { $model = $model->where('coupon_detail_status', '<>', 'DISABLED'); } if (!empty($memberId)) { $model = $model->where('join_coupon_detail_member_id', $memberId); } if (!empty($couponDetailAddtimes)) { $couponDetailAddtimes[0] = strtotime($couponDetailAddtimes[0]); $couponDetailAddtimes[1] = strtotime($couponDetailAddtimes[1]); $model = $model->whereBetween('coupon_detail_addtimes', $couponDetailAddtimes); } if (!empty($couponDetailIds)) { $model = $model->whereIn('coupon_detail_id', $couponDetailIds); } if (!empty($couponName)) { $couponIds = Coupon::where('coupon_name', 'like', '%' . $couponName . '%')->pluck('coupon_id')->toArray(); $model = $model->whereIn('join_detail_coupon_id', $couponIds); } $details = $model->orderBy('coupon_detail_addtimes', 'DESC') ->orderBy('coupon_detail_gain_datetime', 'ASC') ->get() ->toArray(); // 查出所有门店 $depts = SysDept::where('dept_category', '营业场所')->pluck('dept_name', 'dept_id')->toArray(); $data = []; foreach ($details as $detail) { $mobile = !empty($detail['member']) ? $detail['member']['member_mobile'] : ''; $certName = !empty($detail['cert']) ? $detail['cert']['member_cert_name'] : ''; $business = !empty($detail['info']) ? $detail['info']['member_info_business'] : ''; $service = !empty($detail['info']) ? $detail['info']['member_info_service'] : ''; // 推荐人 if (!empty($detail['member']) && !empty($detail['member']['join_invite_member_id'])) { $referrer = Member::with([ 'cert' => function ($query) { $query->select('join_cert_member_id', 'member_cert_name'); } ])->where('member_id', $detail['member']['join_invite_member_id']) ->select('member_id', 'member_mobile') ->first(); $referrerMobile = !empty($referrer->member_mobile) ? $referrer->member_mobile : ''; $referrerCertName = !empty($referrer->cert) ? $referrer->cert->member_cert_name : ''; $detail['referrer_name'] = MemberService::getMemberCertName($referrerMobile, $referrerCertName, ''); } // 顾问 if (!empty($detail['member']) && !empty($detail['member']['member_extend_json'])) { $memberExtendJson = json_decode($detail['member']['member_extend_json'], true); if (!empty($memberExtendJson['referee'])) { $consultant = []; foreach ($memberExtendJson['referee'] as $item) { $consultant[] = $item; } $detail['consultant'] = implode(',', $consultant); } } // 门店 if (!empty($detail['coupon_json'])) { $couponJson = json_decode($detail['coupon_json'], true); if (!isset($couponJson['premises'])) { $detail['premises'] = "所有门店"; } else { $deptName = []; foreach ($couponJson['premises'] as $premisesId) { if (isset($depts[$premisesId]) && !in_array($depts[$premisesId], $deptName)) { $deptName[] = $depts[$premisesId]; } } $detail['premises'] = implode(',', $deptName); } } // 获得方式 if (!empty($detail['coupon_detail_extend_json'])) { $couponDetailExtendsJson = json_decode($detail['coupon_detail_extend_json'], true); if (isset($couponDetailExtendsJson['gettype'])) { $detail['get_type'] = self::$getType[$couponDetailExtendsJson['gettype']] ?? ''; } } // 有效期限 $detail['coupon_detail_gain_datetime'] = !empty($detail['coupon_detail_gain_datetime']) ? date('Y/m/d H:i:s', strtotime($detail['coupon_detail_gain_datetime'])) : ''; $detail['coupon_detail_deadline_datetime'] = !empty($detail['coupon_detail_deadline_datetime']) ? date('Y/m/d H:i:s', strtotime($detail['coupon_detail_deadline_datetime'])) : ''; $data[] = [ 'cert_name' => $certName, 'mobile' => $mobile, // 'member_name' => MemberService::getMemberCertName($mobile, $certName, ''), 'referrer_name' => $detail['referrer_name'] ?? '', 'coupon_name' => $detail['coupon_name'] ?? '', 'coupon_classify' => $detail['coupon_classify'] ?? '', 'premises' => $detail['premises'] ?? '', 'get_type' => $detail['get_type'] ?? '', 'status' => self::$detailStatus[$detail['coupon_detail_status']] ?? '', 'coupon_detail_used_datetime' => $detail['coupon_detail_used_datetime'] ?? '', 'valid_date' => !empty($detail['coupon_detail_gain_datetime']) ? $detail['coupon_detail_gain_datetime'] . ' - ' . $detail['coupon_detail_deadline_datetime'] : '', 'coupon_detail_addtimes' => $detail['coupon_detail_addtimes'], 'consultant' => $detail['consultant'] ?? '', 'business' => $business, 'service' => $service ]; } return json_success('success', $data); } /** * @var string[] */ public static $getType = [ 'VIP' => '购买VIP', 'RECEIVE' => '领取', 'SEND' => '后台发放', 'GIVE' => '转增', 'EXCHANGE' => '兑换', 'COMBINE' => '新客专享', 'ROLE' => '会员升级', 'PARTNER' => '会员合伙人', 'REFERRER' => '康养推荐官', 'PACKAGE' => '购买套包', 'SYSTEM' => '后台发放' ]; /** * @var string[] */ public static $detailStatus = [ 'INIT' => '初始状态', 'PENDING' => '待领取', 'ACTIVED' => '可用', 'WAITING' => '待完成', 'USED' => '已使用', 'DISABLED' => '禁用', 'CANCEL' => '取消', 'EXPIRED' => '过期', ]; }