1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045 |
- <?php
- namespace App\JsonRpc;
- use App\Model\Ad;
- use App\Model\AdSize;
- use App\Model\AdPlace;
- use App\Model\Order;
- use App\Model\OrderAd;
- use App\Model\ShoppingCart;
- use App\Model\Website;
- use App\Tools\Result;
- use Carbon\Carbon;
- use Hyperf\DbConnection\Db;
- use Hyperf\RpcServer\Annotation\RpcService;
- #[RpcService(name: "OrderService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
- class OrderService implements OrderServiceInterface
- {
- /**
- * 查询没有广告的广告位
- *获取站点下所有的广告尺寸
- * @param
- * @return void
- */
- public function getAD(array $data): array
- {
- // if (!empty($data)) {
- // $where = [
- // 'ad_place.ad_size_id' => $data['ad_size_id'],
- // ];
- // $start = Carbon::parse($data['starttime']);
- // $end = Carbon::parse($data['endtime']);
- // $status = [
- // 0 => '1',
- // 1 => '4',
- // 2 => '6',
- // ];
- // $ads = Ad::where('fromtime', '<=', $start)->where('totime', '>=', $end)->select('pid')->get()->all();
- // $orderads = OrderAd::where('fromtime', '<=', $start)->where('totime', '>=', $end)->whereIn('status', $status)->select('pid')->get()->all();
- // $pids = array_merge($ads, $orderads);
- // $ad_pids = array_unique($pids);
- // // $ad_pid = [1,2,3];
- // // 所有与用户有时间冲突的广告位
- // if (!empty($ad_pids)) {
- // $pid = [];
- // foreach ($ad_pids as $val) {
- // array_push($pid, $val['pid']);
- // }
- // $placeids = AdPlace::whereNotIn('id', $pid)->where($where)->select('id')->get()->all();
- // //所有去掉与用户时间冲突的广告并且符合图片尺寸的广告位
- // // 符合用户条件的空广告位
- // $place_id = [];
- // foreach ($placeids as $val) {
- // array_push($place_id, $val['id']);
- // }
- // $rep = AdPlace::whereIn('ad_place.id', $place_id)
- // ->leftJoin('website', 'ad_place.website_id', 'website.id')
- // ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
- // ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
- // ->selectSub('website.id', 'webid')
- // ->selectSub('ad_place.id', 'pid')
- // ->selectSub('ad_size.width', 'size_width')
- // ->selectSub('ad_size.height', 'size_height')
- // ->orderBy("website.id", "asc")
- // ->limit($data['pageSize'])
- // ->offset(($data['page'] - 1) * $data['pageSize'])
- // ->get();
- // $count = AdPlace::whereIn('ad_place.id', $place_id)->count();
- // } else {
- // $rep = AdPlace::where($where)
- // ->leftJoin('website', 'ad_place.website_id', 'website.id')
- // ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
- // ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
- // ->selectSub('website.id', 'webid')
- // ->selectSub('ad_place.id', 'pid')
- // ->selectSub('ad_size.width', 'size_width')
- // ->selectSub('ad_size.height', 'size_height')
- // ->orderBy("website.id", "asc")
- // ->limit($data['pageSize'])
- // ->offset(($data['page'] - 1) * $data['pageSize'])
- // ->get();
- // $count = AdPlace::where($where)->count();
- // }
- // $startTime = strtotime($data['starttime']);
- // $endTime = strtotime($data['endtime']);
- // $time = ($endTime - $startTime) / (24 * 60 * 60);
- // $roundedValue = round($time, 2);
- // $days = number_format($roundedValue, 2, '.', '');
- // $result = [
- // 'rows' => $rep->toArray(),
- // 'count' => $count,
- // 'days' => $days,
- // ];
- // if (empty($result['rows'])) {
- // return Result::error("暂无符合您条件的广告位!");
- // }
- // } else {
- // $result = AdSize::get();
- // if (empty($result)) {
- // return Result::error('暂无广告尺寸!');
- // }
- // }
- //获取站点下所有的广告尺寸
- $adssizeid = AdPlace::where('website_id', $data['website_id'] ?? 2)->select('ad_size_id')->get()->all();
- $adssizeidArray = array_unique(array_column($adssizeid, 'ad_size_id'));
- var_dump($adssizeidArray, 'p--------------1-------');
- $adssize = AdSize::whereIn('id', $adssizeidArray)->select('id', 'width', 'height')->get();
- return Result::success($adssize);
- }
- /**
- * 添加订单
- * @param
- * @return void
- */
- public function addOrder(array $data): array
- {
- $ads = Ad::whereIn($data['id'])
- ->leftJoin('ad_place', 'ad.pid', 'ad_place.id')
- ->leftJoin("article_data", "article.id", "article_data.article_id")
- ->select("ad_place.*", "ad.*")
- ->orderBy("ad.id", "desc")
- ->limit($data['pageSize'])
- ->offset(($data['page'] - 1) * $data['pageSize'])->get();
- $count = Ad::whereIn($data['id'])->count();
- $data = [
- 'rows' => $ads->toArray(),
- 'count' => $count,
- ];
- if (empty($rep)) {
- return Result::error("没有信息数据");
- }
- return Result::success($data);
- }
- /**
- * 获取订单列表
- * @param
- * @return void
- */
- public function getOrderListAdmin(array $data): array
- {
- // 获取分页参数,默认每页 10 条记录
- $page = isset($data['page']) ? (int) $data['page'] : 1;
- $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10;
- $user_id = isset($data['user_id']) ? (int) $data['user_id'] : 0;
- $type_id = isset($data['type_id']) ? (int) $data['type_id'] : 0;
- $website_id = isset($data['website_id']) ? (int) $data['website_id'] : 0;
- var_dump($data, 'p-----------3---1-------');
- // 构建查询条件
- $where = [];
- if ($type_id == 10000) {
- $where[] = [
- 'order.user_id',
- '=',
- $user_id
- ];
- };
- if (!empty($data['status'])) {
- $where = [
- 'order.status' => $data['status'], // 明确指定 order 表的 status 列
- ];
- }
- //广告订单状态
- if (!empty($data['ad_status'])) {
- $where = [
- 'order.ad_status' => $data['ad_status'],
- ];
- }
- // 添加订单号查询条件
- if (!empty($data['order_num'])) {
- $where['order.order_num'] = $data['order_num']; // 明确指定 order 表的 order_num 列
- }
- $where1 = [];
- //nickname like
- if (!empty($data['user_name'])) {
- $where1[] = ['user.user_name', 'like', '%' . $data['user_name'] . '%'];
- }
- //website_name
- if (!empty($data['website_name'])) {
- $where[] = ['website.website_name', 'like', '%' . $data['website_name'] . '%'];
- }
- // 处理时间范围查询
- $start = $data['sttime'] ?? '';
- $end = $data['edtime'] ?? '';
- // 查询数据并分页
- $query = Order::where($where)
- ->when(!empty($start) && !empty($end), function ($q) use ($start, $end) {
- $q->whereBetween('order.fromtime', [$start, $end]); // 明确指定 order 表的 fromtime 列
- })
- ->when(!empty($start), function ($q) use ($start) {
- $q->where('order.fromtime', '>=', $start); // 明确指定 order 表的 fromtime 列
- })
- ->when(!empty($end), function ($q) use ($end) {
- $q->where('order.totime', '<=', $end); // 明确指定 order 表的 totime 列
- })
- ->when(!empty($where1), function ($q) use ($where1) {
- $q->where($where1); // 明确指定 user 表的 nickname 列
- })
- ->leftJoin('user as admin_user', 'order.admin_user_id', '=', 'admin_user.id')
- ->leftJoin('user as user', 'order.user_id', '=', 'user.id')
- ->leftJoin('website', 'order.website_id', '=', 'website.id')
- ->select(
- 'order.*',
- 'admin_user.user_name as admin_user_name',
- 'user.user_name as user_name',
- 'website.website_name as website_name'
- )
- ->orderBy('order.id', 'desc');
- // 执行分页查询
- $result = $query->paginate($perPage, ['*'], 'page', $page);
- // 循环获取到订单的广告位置名称信息
- $orderData = $result->items();
- foreach ($orderData as $key => $value) {
- $pid = $value['id'];
- //取出pid
- $ad = OrderAd::where('order_id', $pid)->select('pid')->get()->all();
- //ad_place 取出name
- $ad = array_column($ad, 'pid');
- var_dump($ad, 'p--------------2-------');
- $ad = AdPlace::whereIn('id', $ad)->select('name')->get()->all();
- $orderData[$key]['adname'] = implode(',', array_column($ad, 'name'));
- }
- // 返回分页结果
- return Result::success([
- 'count1' => $result->total(),
- 'current_page' => $result->currentPage(),
- 'last_page' => $result->lastPage(),
- 'pagesize' => $result->perPage(),
- 'rows' => $orderData,
- ]);
- }
- /**
- * 获取订单详情
- * @param
- * @return void
- */
- public function getOrderDetailAdmin(array $data): array
- {
- $order = Order::where('order.id', $data['id'])
- ->leftJoin('user as admin_user', 'order.admin_user_id', '=', 'admin_user.id')
- ->leftJoin('user as user', 'order.user_id', '=', 'user.id')
- ->select(
- 'order.*',
- 'admin_user.user_name as admin_user_name',
- 'user.user_name as user_name'
- )->first();
- if (empty($order)) {
- return Result::error("没有信息数据");
- }
- $pid = $order['id'];
- $ad = OrderAd::where('order_id', $pid)
- ->leftJoin('ad_place as ad', 'order_ad.pid', '=', 'ad.id')
- ->select('order_ad.*', 'ad.name as ad_name')
- ->get();
- $order['ad'] = $ad;
- return Result::success($order);
- }
- private function startOder(array $data): void
- {
- Db::beginTransaction();
- try {
- //ad 变更状态为已经审核但是不生效,或者删除订单,或者加入别的状态:比如过期
- OrderAd::whereIn('order_id', $data)->update(['status' => 1]);
- $pid = OrderAd::whereIn('order_id', $data)->pluck('pid')->toArray();
- Ad::whereIn('pid', $pid)->update(['status' => 1]);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- var_dump("处理订单ID: {$data['id']} 时发生错误: " . $e->getMessage());
- }
- }
- /**
- * 修改订单价格
- * @param
- * @return void
- */
- public function editPriceOrderAdmin(array $data): array
- {
- //设置东八区
- date_default_timezone_set('Asia/Shanghai');
- // $currentTime = date('Y-m-d H:i:s');
- // $orderIdsTO2 = Order::where('status', 1) //已经审核,等待开始
- // ->where('sttime', '<=', $currentTime)
- // ->where('edtime', '>=', $currentTime)
- // ->pluck('id')
- // ->toArray();
- // if ($orderIdsTO2) {
- // var_dump('需要处理的订单ID列表:' . implode(', ', $orderIdsTO2));
- // $this->startOder($orderIdsTO2);
- // }
- // return Result::success($data);
- $order = Order::where('id', $data['id'])->first();
- if (empty($order)) {
- return Result::error("没有信息数据");
- }
- if (empty($order['price'])) {
- return Result::error("价格字段错误");
- }
- if (empty($data['sttime'])) {
- return Result::error("开始时间错误");
- }
- if (empty($data['edtime'])) {
- return Result::error("结束时间错误");
- }
- $order->price = $data['price'];
- $order->sttime = $data['sttime'];
- $order->edtime = $data['edtime'];
- //保留小数点两位
- $days = strtotime($data['edtime']) - strtotime($data['sttime']);
- $days = round($days / 86400, 2);
- $order->days = $days;
- $order->save();
- return Result::success($order);
- }
- /**
- *拒绝订单
- * @param
- * @return void
- */
- public function rejectOrderAdmin(array $data): array
- {
- $order = Order::where('id', $data['id'])->first();
- if (empty($order)) {
- return Result::error("没有信息数据");
- }
- Db::beginTransaction();
- try {
- $order->status = 2; //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
- $order->ad_status = 2;
- $order->reason = $data['reason'];
- $order->bhtime = date('Y-m-d H:i:s');
- $order->save();
- OrderAd::where('order_id', $data['id'])
- ->update(['status' => 2]);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error("操作失败");
- }
- return Result::success($order);
- }
- /**
- * 结束订单
- * @param
- * @return void
- */
- public function endOrderAdmin(array $data): array
- {
- $order = Order::where('id', $data['id'])->first();
- if (empty($order)) {
- return Result::error("没有信息数据");
- }
- Db::beginTransaction();
- try {
- $order->status = 7;
- $order->jstime = date('Y-m-d H:i:s');
- $order->save();
- // 获取 order_ad 表中的记录
- $orderAds = OrderAd::where('order_id', $data['id'])
- ->update(['status' => 7]);
- // 在ad表中删除相同pid的数据
- Ad::where('order_id', $data['id'])->delete();
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error("操作失败" . $e->getMessage());
- }
- return Result::success($order);
- }
- /**
- * 删除订单
- * @param
- * @return void
- */
- public function delOrderAdmin(array $data): array
- {
- // 获取订单信息
- $order = Order::where('id', $data['id'])->first();
- if (empty($order)) {
- return Result::error("没有信息数据");
- }
- Db::beginTransaction();
- try {
- Order::where('id', $data['id'])->delete();
- var_dump(11111111);
- // 获取 order_ad 表中的记录
- OrderAd::where('order_id', $data['id'])->delete();
- // 删除 ad 表中的记录
- Ad::where('order_id', $data['id'])->delete();
- // 提交事务
- Db::commit();
- } catch (\Exception $e) {
- // 回滚事务
- Db::rollBack();
- // 返回错误信息
- return Result::error($e->getMessage());
- }
- // 返回成功信息
- return Result::success("删除成功");
- }
- /**
- * 审核订单
- * @param
- * @return void
- */
- public function applyOrderStatusAdmin(array $data): array
- {
- $order = Order::where('id', $data['id'])
- ->where('status', 6)
- ->first();
- if (empty($order)) {
- return Result::error("没有信息数据");
- }
- Db::beginTransaction();
- try {
- $order->status = 1;
- //判断是否在周期范围内
- $ad_status = 8; // 待生效
- if ($order->starttime < date('Y-m-d H:i:s') && date('Y-m-d H:i:s') < $order->endtime) {
- // 条件满足时的逻辑
- $ad_status = 1;
- }
- $order->ad_status = 1;
- $order->shtime = date('Y-m-d H:i:s');
- $order->save();
- // 批量更新 order_ad 表中的状态
- OrderAd::where('order_id', $data['id'])
- ->where('status', 6)
- ->update(['status' => 1]);
- //判断的当前时间是否在订单的开始时间之后,并且小于等于订单的结束时间
- if (time() >= strtotime($order->sttime) && time() < strtotime($order->edtime)) {
- $ad_status = 1; //审核生效
- } else {
- $ad_status = 2; //审核
- }
- $ads = [];
- $orderAds = OrderAd::where('order_id', $data['id'])->get();
- foreach ($orderAds as $orderAd) {
- $ads[] = [
- 'name' => $orderAd->name,
- 'pid' => $orderAd->pid,
- 'areaid' => $orderAd->areaid,
- 'amount' => $orderAd->amount,
- 'introduce' => $orderAd->introduce,
- 'hits' => $orderAd->hits,
- 'admin_user_id' => $orderAd->admin_user_id,
- 'fromtime' => $orderAd->fromtime,
- 'totime' => $orderAd->totime,
- 'text_name' => $orderAd->text_name,
- 'text_url' => $orderAd->text_url,
- 'text_title' => $orderAd->text_title,
- 'image_src' => $orderAd->image_src,
- 'image_url' => $orderAd->image_url,
- 'image_alt' => $orderAd->image_alt,
- 'video_src' => $orderAd->video_src,
- 'video_url' => $orderAd->video_url,
- 'video_auto' => $orderAd->video_auto,
- 'video_loop' => $orderAd->video_loop,
- 'status' => $ad_status,
- 'order_id' => $orderAd->order_id,
- ];
- }
- Ad::insert($ads);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error($e->getMessage());
- }
- return Result::success($order);
- }
- /*
- * 根据用户条件及网站搜索没有广告的广告位
- * @param
- * @return void
- */
- public function getWebsiteAd(array $data): array
- {
- $where['website_id'] = $data['website_id'];
- $where['ad_size_id'] = $data['ad_size_id'] ?? 1;
- $start = Carbon::parse($data['starttime']);
- $end = Carbon::parse($data['endtime']);
- // $status = [
- // 0 => '1',
- // 1 => '7',
- // 2 => '6',
- // ];
- //订单状态:1:通过;2:驳回;3:撤回;5:过期;6:待审核;7:结束
- // 筛选已上架的广告有时间冲突的广告位
- $ads = Ad::where('fromtime', '<=', $start)->where('totime', '>=', $end)->select('pid')->get()->all();
- $orderads = OrderAd::where('fromtime', '<=', $start)->where('totime', '>=', $end)->where('status', 1)->select('pid')->get()->all();
- $ads = array_column($ads, 'pid');
- $orderads = array_column($orderads, 'pid');
- $pids = array_merge($ads, $orderads);
- var_dump($pids, '-----------1-------------');
- //取出pid
- $ad_pids = array_unique($pids);
- var_dump($ad_pids, '----------3------------');
- $placeids = AdPlace::whereNotIn('id', $ad_pids)->where($where)->select('id')->get()->all();
- $ad_pids = array_column($placeids, 'id');
- $rep = AdPlace::where($where)
- ->whereIn('ad_place.id', $ad_pids)
- ->where('ad_place.website_id', $data['website_id'])
- ->leftJoin('website', 'ad_place.website_id', 'website.id')
- ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
- ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
- ->selectSub('website.id', 'webid')
- ->selectSub('ad_place.id', 'pid')
- ->selectSub('ad_size.width', 'size_width')
- ->selectSub('ad_size.height', 'size_height')
- ->orderBy("website.id", "asc")
- ->limit($data['pageSize'])
- ->offset(($data['page'] - 1) * $data['pageSize'])
- ->get();
- $count = AdPlace::where($where)->where('ad_place.website_id', $data['website_id'])->whereIn('ad_place.id', $ad_pids)->count();
- $startTime = strtotime($data['starttime']);
- $endTime = strtotime($data['endtime']);
- $time = ($endTime - $startTime) / (24 * 60 * 60);
- $roundedValue = round($time, 2);
- $days = number_format($roundedValue, 2, '.', '');
- $result = [
- 'rows' => $rep->toArray(),
- 'count' => $count,
- 'days' => $days,
- ];
- return Result::success($result);
- // //ad_size_id 必选改可选
- // //单选
- // if (isset($data['ad_size_id']) && is_string($data['ad_size_id'])) {
- // $where = [
- // 'ad_place.ad_size_id' => $data['ad_size_id'],
- // ];
- // }
- // //如果有website_id
- // if (!isset($data['ad_size_id'])) {
- // // $adsiteids = AdPlace::where('website_id', $data['website_id'])->select('id')->get()->all();
- // // $ad_pids = array_column($adsiteids, 'id');
- // // // $where = [
- // // // 'ad_place.ad_size_id' => $data['ad_size_id'],
- // // // ];
- // // var_dump($ad_pids, 'p--------------3-------');
- // // // $where[] = ['ad_place.id', 'in', $ad_pids];
- // // $where = [
- // // 'ad_place.website_id' => $data['website_id'],
- // // ];
- // }
- // //数组
- // $start = Carbon::parse($data['starttime']);
- // $end = Carbon::parse($data['endtime']);
- // $status = [
- // 0 => '1',
- // 1 => '4',
- // 2 => '6',
- // ];
- // //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
- // $ads = Ad::where('fromtime', '<=', $start)->where('totime', '>=', $end)->select('pid')->get()->all();
- // $orderads = OrderAd::where('fromtime', '<=', $start)->where('totime', '>=', $end)->whereIn('status', $status)->select('pid')->get()->all();
- // $pids = array_merge($ads, $orderads);
- // $ad_pids = array_unique($pids);
- // //$ad_pids=所有与用户提交的时间有冲突的广告位pid
- // if (!empty($ad_pids)) {
- // $pid = [];
- // foreach ($ad_pids as $val) {
- // array_push($pid, $val['pid']);
- // }
- // //去掉时间冲突并且符合图片尺寸的广告位
- // $placeids = AdPlace::whereNotIn('id', $pid)->where($where)->select('id', 'website_id')->get()->all();
- // if (empty($placeids)) {
- // return Result::error('暂无数据!');
- // }
- // if (!isset($data['website_id'])) {
- // $websiteIds = array_column($placeids, 'website_id');
- // $website_id = array_unique($websiteIds);
- // $rep = Website::whereIn('id', $website_id)->get();
- // $count = Website::whereIn('id', $website_id)->count();
- // //若不存在网站id参数直接返回符合条件的广告位相关联的网站名称
- // } else {
- // $place_id = [];
- // foreach ($placeids as $val) {
- // array_push($place_id, $val['id']);
- // }
- // $rep = AdPlace::where($where)
- // ->whereIn('ad_place.id', $place_id)
- // ->where('ad_place.website_id', $data['website_id'])
- // ->leftJoin('website', 'ad_place.website_id', 'website.id')
- // ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
- // ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
- // ->selectSub('website.id', 'webid')
- // ->selectSub('ad_place.id', 'pid')
- // ->selectSub('ad_size.width', 'size_width')
- // ->selectSub('ad_size.height', 'size_height')
- // ->orderBy("website.id", "asc")
- // ->limit($data['pageSize'])
- // ->offset(($data['page'] - 1) * $data['pageSize'])
- // ->get();
- // $count = AdPlace::where($where)->where('ad_place.website_id', $data['website_id'])->count();
- // //若存在网站id,关联查询是需要添加website_id条件查询
- // }
- // } else {
- // //若不存在有时间冲突的广告位则所有符合图片尺寸的广告位皆可以使用,只需要判断是否存在网站id参数即可
- // if (isset($data['website_id'])) {
- // $rep = AdPlace::where($where)
- // ->where('ad_place.website_id', $data['website_id'])
- // ->leftJoin('website', 'ad_place.website_id', 'website.id')
- // ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
- // ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
- // ->selectSub('website.id', 'webid')
- // ->selectSub('ad_place.id', 'pid')
- // ->selectSub('ad_size.width', 'size_width')
- // ->selectSub('ad_size.height', 'size_height')
- // ->orderBy("website.id", "asc")
- // ->limit($data['pageSize'])
- // ->offset(($data['page'] - 1) * $data['pageSize'])
- // ->get();
- // $count = AdPlace::where($where)->where('ad_place.website_id', $data['website_id'])->count();
- // } else {
- // $place_all = AdPlace::where($where)->select('website_id')->get()->all();
- // $place_allads = [];
- // foreach ($place_all as $v) {
- // array_push($place_allads, $v['website_id']);
- // }
- // $rep = Website::whereIn('id', $place_allads)->get();
- // $count = Website::whereIn('id', $place_allads)->count();
- // }
- // }
- // if (empty($rep)) {
- // return Result::error("暂无符合您条件的广告位!");
- // }
- // $startTime = strtotime($data['starttime']);
- // $endTime = strtotime($data['endtime']);
- // $time = ($endTime - $startTime) / (24 * 60 * 60);
- // $roundedValue = round($time, 2);
- // $days = number_format($roundedValue, 2, '.', '');
- // $result = [
- // 'rows' => $rep->toArray(),
- // 'count' => $count,
- // 'days' => $days,
- // ];
- // return Result::success($result);
- }
- /**
- * 获取广告金额
- * @param
- * @return void
- */
- public function getPrice(array $data): array
- {
- // $data['pid'] = [15,16];
- $startTime = strtotime($data['starttime']);
- $endTime = strtotime($data['endtime']);
- $days = ($endTime - $startTime) / (24 * 60 * 60); //计算共计多少天
- $price = 0;
- if (isset($data['pid'])) {
- $ad_price = AdPlace::whereIn('id', $data['pid'])->select('id', 'price')->get();
- foreach ($ad_price as $v) {
- $price += number_format($v['price'] * $days, 2, '.', '');
- }
- } else {
- $price = 0;
- }
- // 确保 $price带有两位小数位数
- $price = number_format((float) $price, 2, '.', '');
- return Result::success($price);
- }
- /**
- * 添加广告订单
- * @param
- * @return void
- */
- public function addAD(array $data): array
- {
- date_default_timezone_set('Asia/Shanghai');
- $time = time();
- $startTime = strtotime($data['starttime']);
- $endTime = strtotime($data['endtime']);
- $days = ($endTime - $startTime) / (24 * 60 * 60); //计算共计多少天
- $timestamp = date('YmdHis', $time);
- $catetime = date('Y-m-d H:i:s', $time);
- $randomNumber = mt_rand(1000, 9999);
- $ordernum = $randomNumber . $timestamp; // 时间戳与随机数拼接
- $order_size = AdSize::where('id', $data['ad_size_id'])->first();
- // var_dump(($time));
- Db::beginTransaction();
- try {
- $order = [
- 'order_num' => $ordernum,
- 'name' => $data['name'],
- 'sttime' => $data['starttime'],
- 'edtime' => $data['endtime'],
- 'user_id' => $data['user_id'],
- 'cttime' => $catetime,
- 'width' => $order_size['width'],
- 'height' => $order_size['height'],
- 'days' => $days,
- 'price' => $data['price'],
- 'created_at' => date('Y-m-d H:i:s', time()),
- 'updated_at' => date('Y-m-d H:i:s', time()),
- ];
- //添加订单
- $orderid = Order::insertGetId($order);
- $adplace = $data['pid'];
- if (is_array($data['pid'])) {
- $adplace = AdPlace::whereIn('id', $data['pid'])->select('website_id', 'id')->get();
- $order_ad = [];
- foreach ($adplace as $key => $ads) {
- $order_ad[$key] = [
- 'order_id' => $orderid,
- 'order_num' => $ordernum,
- 'name' => $data['name'],
- 'fromtime' => $data['starttime'],
- 'totime' => $data['endtime'],
- 'image_src' => $data['imgsrc'],
- 'image_url' => $data['imgurl'],
- 'pid' => $ads['id'],
- 'website_id' => $ads['website_id'],
- ];
- }
- }
- $orderad_id = OrderAd::insert($order_ad);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error($e->getMessage());
- }
- // $log = AdLog::insert($log);
- $result = [
- 'order_id' => $orderid,
- 'orderad_id' => $orderad_id,
- 'name' => $data['name'],
- 'ordernum' => $ordernum,
- ];
- return Result::success($result);
- }
- /**
- * 获取订单列表
- * @param
- * @return void
- */
- public function getOrderList(array $data): array
- {
- $where = [
- 'user_id' => $data['user_id'],
- 'user_del' => 2,
- ];
- if (isset($data['status'])) {
- $where['status'] = $data['status'];
- }
- $orders = Order::where($where)
- ->limit($data['pageSize'])
- ->offset(($data['page'] - 1) * $data['pageSize'])
- ->orderBy("updated_at", "desc")
- ->get()
- ->all();
- //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
- $count['all'] = Order::where($where)->count();
- $count['pass'] = Order::where($where)->where('status', 1)->count();
- $count['return'] = Order::where($where)->where('status', 2)->count();
- $count['recall'] = Order::where($where)->where('status', 3)->count();
- $count['update'] = Order::where($where)->where('status', 4)->count();
- $count['fail'] = Order::where($where)->where('status', 5)->count();
- $count['waite'] = Order::where($where)->where('status', 6)->count();
- $count['over'] = Order::where($where)->where('status', 7)->count();
- foreach ($orders as $key => $val) {
- $adsnum = OrderAd::where('order_id', $val['id'])->count();
- $rep[$key] = $val;
- $rep[$key]['num'] = $adsnum;
- }
- if (empty($rep)) {
- return Result::error("您暂时还没有下单");
- } else {
- $data = [
- 'rows' => $rep,
- 'count' => $count,
- ];
- }
- return Result::success($data);
- }
- /**
- * 获取订单详情
- * @param
- * @return void
- */
- public function getOrderDetail(array $data): array
- {
- $order = Order::where('id', $data['order_id'])->first();
- $orderads = OrderAd::where('order_ad.order_id', $data['order_id'])
- ->leftJoin('website', 'order_ad.website_id', 'website.id')
- ->leftJoin('ad_place', 'order_ad.pid', 'ad_place.id')
- ->select('order_ad.*', 'website.website_name', 'website.id', 'ad_place.name')
- ->selectSub('website.id', 'webid')
- ->selectSub('order_ad.id', 'oid')
- ->selectSub('order_ad.name', 'adname')
- ->selectSub('ad_place.name', 'apname')
- ->orderBy("website.id", "asc")
- ->limit($data['pageSize'])
- ->offset(($data['page'] - 1) * $data['pageSize'])
- ->get();
- if (empty($order)) {
- return Result::error("订单id错误");
- }
- $count = OrderAd::where('order_ad.order_id', $data['order_id'])->count();
- $result = [
- 'order' => $order,
- 'orderads' => $orderads,
- 'count' => $count,
- ];
- return Result::success($result);
- }
- /**
- * 撤回订单
- * @param
- * @return void
- */
- public function cancelOrder(array $data): array
- {
- date_default_timezone_set('Asia/Shanghai');
- $time = time();
- $timestamp = date('YmdHis', $time);
- Db::beginTransaction();
- try {
- $order = Order::where('id', $data['order_id'])->where('status', 6)->where('sttime', '>=', $timestamp)->update(['status' => 3, 'ad_status' => '3']);
- $ads = OrderAd::where('order_id', $data['order_id'])->where('status', 6)->where('fromtime', '>=', $timestamp)->update(['status' => 3]);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error($e->getMessage());
- }
- $result = [
- 'order' => $order,
- 'ads' => $ads,
- ];
- if ($order == 0) {
- return Result::error("此订单不可撤回");
- }
- return Result::success($result);
- }
- /**
- * 删除订单
- * @param
- * @return void
- */
- public function delOrderAD(array $data): array
- {
- $data['status'] = [
- 0 => 2,
- 1 => 3,
- 2 => 5,
- 3 => 7,
- ];
- //1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束;
- date_default_timezone_set('Asia/Shanghai');
- $time = time();
- $timestamp = date('YmdHis', $time);
- $where = [
- ['id', '=', $data['id']],
- ];
- $time = [['edtime', '<=', $timestamp]];
- $order = Order::where($where)->where($time)->update(['user_del' => 1]);
- if (empty($order)) {
- $order = Order::where($where)->whereIn('status', $data['status'])->update(['user_del' => 1]);
- }
- if ($order == 0) {
- return Result::error("此订单不可删除");
- }
- return Result::success($order);
- }
- /**
- * 创建购物车
- * @param
- * @return void
- */
- public function addShoppingCart(array $data): array
- {
- $shop = ShoppingCart::where('user_id', $data['user_id'])->get()->all();
- $time = time();
- $timestamp = date('YmdHis', $time);
- $randomNumber = mt_rand(100, 999);
- $shopping_id = $randomNumber . $timestamp; // 时间戳与随机数拼接
- Db::beginTransaction();
- try {
- if (!empty($shop)) {
- $del_shop = ShoppingCart::where('user_id', $data['user_id'])->delete();
- if (empty($del_shop)) {
- Db::rollBack();
- return Result::error("删除购物车失败");
- // throw new \Exception("删除购物车失败");
- }
- }
- $shop = [
- 'user_id' => $data['user_id'],
- 'shopping_id' => $shopping_id,
- 'created_at' => date('Y-m-d H:i:s', time()),
- 'updated_at' => date('Y-m-d H:i:s', time()),
- ];
- $result = ShoppingCart::insert($shop);
- Db::commit();
- } catch (\Exception $e) {
- Db::rollBack();
- return Result::error($e->getMessage());
- }
- if (empty($result)) {
- return Result::error("创建失败");
- } else {
- return Result::success($shopping_id);
- }
- }
- /**
- * 获取购物车中的广告位
- * @param
- * @return void
- */
- public function getShoppingCartAD(array $data): array
- {
- $shopcart = ShoppingCart::where('shopping_id', $data['shopping_id'])
- ->where('user_id', $data['user_id'])
- ->orderBy('pid')
- ->pluck('pid')
- ->toArray();
- $result['pid'] = $shopcart;
- if (empty($result)) {
- return Result::error("购物车id错误");
- } else {
- return Result::success($result);
- }
- }
- /**
- * 添加购物车中的广告位
- * @param
- * @return void
- */
- public function addShoppingCartAD(array $data): array
- {
- $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->first();
- if (empty($shop)) {
- return Result::error("购物车id错误");
- } else {
- $ad = AdPlace::where('ad_place.id', $data['pid'])
- ->leftJoin('website', 'ad_place.website_id', 'website.id')
- ->select('ad_place.id as pid', 'website.id as website_id')
- ->first();
- if (empty($ad)) {
- return Result::error("广告位id错误");
- }
- if (empty($shop['pid']) && empty($shop['website_id'])) {
- $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id', $data['user_id'])->update(['pid' => $ad['pid'], 'website_id' => $ad['website_id']]);
- } else {
- if ($data['pid'] == $shop['pid']) {
- return Result::error("购物车中已存在该广告位");
- }
- $shop_ad = [
- 'pid' => $ad['pid'],
- 'website_id' => $ad['website_id'],
- 'shopping_id' => $shop['shopping_id'],
- 'user_id' => $data['user_id'],
- ];
- $result = ShoppingCart::insertGetId($shop_ad);
- }
- }
- if (empty($result)) {
- return Result::error("添加失败");
- } else {
- return Result::success($result);
- }
- }
- /**
- * 删除购物车中的广告位
- * @param
- * @return void
- */
- public function delShoppingCartAD(array $data): array
- {
- $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->where('pid', $data['pid'])->first();
- if (empty($shop)) {
- return Result::error("不存在此条记录!(参数错误)");
- } else {
- $count = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->count();
- if ($count == 1) {
- $result = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->update(['pid' => null, 'website_id' => null]);
- } else {
- $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id', $data['user_id'])->where('pid', $data['pid'])->delete();
- }
- }
- if (empty($result)) {
- return Result::error("删除失败");
- } else {
- return Result::success($result);
- }
- }
- /**
- * 获取广告位的尺寸
- *
- *
- */
- public function getAdSize(): array
- {
- $result = AdSize::get()->all();
- if (empty($result)) {
- return Result::error("暂无数据");
- }
- return Result::success($result);
- }
- }
|