123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020 |
- <?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;
- var_dump($data, 'p--------------1-------');
- // 构建查询条件
- $where = [];
- 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 列
- }
- // 处理时间范围查询
- $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 列
- })
- ->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'
- )
- ->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);
- }
- }
|