OrderService.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Ad;
  4. use App\Model\AdSize;
  5. use App\Model\AdPlace;
  6. use App\Model\Order;
  7. use App\Model\OrderAd;
  8. use App\Model\ShoppingCart;
  9. use App\Model\Website;
  10. use App\Tools\Result;
  11. use Carbon\Carbon;
  12. use Hyperf\DbConnection\Db;
  13. use Hyperf\RpcServer\Annotation\RpcService;
  14. #[RpcService(name: "OrderService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  15. class OrderService implements OrderServiceInterface
  16. {
  17. /**
  18.  * 查询没有广告的广告位
  19. *获取站点下所有的广告尺寸
  20.  * @param
  21.  * @return void
  22. */
  23. public function getAD(array $data): array
  24. {
  25. // if (!empty($data)) {
  26. // $where = [
  27. // 'ad_place.ad_size_id' => $data['ad_size_id'],
  28. // ];
  29. // $start = Carbon::parse($data['starttime']);
  30. // $end = Carbon::parse($data['endtime']);
  31. // $status = [
  32. // 0 => '1',
  33. // 1 => '4',
  34. // 2 => '6',
  35. // ];
  36. // $ads = Ad::where('fromtime', '<=', $start)->where('totime', '>=', $end)->select('pid')->get()->all();
  37. // $orderads = OrderAd::where('fromtime', '<=', $start)->where('totime', '>=', $end)->whereIn('status', $status)->select('pid')->get()->all();
  38. // $pids = array_merge($ads, $orderads);
  39. // $ad_pids = array_unique($pids);
  40. // // $ad_pid = [1,2,3];
  41. // // 所有与用户有时间冲突的广告位
  42. // if (!empty($ad_pids)) {
  43. // $pid = [];
  44. // foreach ($ad_pids as $val) {
  45. // array_push($pid, $val['pid']);
  46. // }
  47. // $placeids = AdPlace::whereNotIn('id', $pid)->where($where)->select('id')->get()->all();
  48. // //所有去掉与用户时间冲突的广告并且符合图片尺寸的广告位
  49. // // 符合用户条件的空广告位
  50. // $place_id = [];
  51. // foreach ($placeids as $val) {
  52. // array_push($place_id, $val['id']);
  53. // }
  54. // $rep = AdPlace::whereIn('ad_place.id', $place_id)
  55. // ->leftJoin('website', 'ad_place.website_id', 'website.id')
  56. // ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
  57. // ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
  58. // ->selectSub('website.id', 'webid')
  59. // ->selectSub('ad_place.id', 'pid')
  60. // ->selectSub('ad_size.width', 'size_width')
  61. // ->selectSub('ad_size.height', 'size_height')
  62. // ->orderBy("website.id", "asc")
  63. // ->limit($data['pageSize'])
  64. // ->offset(($data['page'] - 1) * $data['pageSize'])
  65. // ->get();
  66. // $count = AdPlace::whereIn('ad_place.id', $place_id)->count();
  67. // } else {
  68. // $rep = AdPlace::where($where)
  69. // ->leftJoin('website', 'ad_place.website_id', 'website.id')
  70. // ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
  71. // ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
  72. // ->selectSub('website.id', 'webid')
  73. // ->selectSub('ad_place.id', 'pid')
  74. // ->selectSub('ad_size.width', 'size_width')
  75. // ->selectSub('ad_size.height', 'size_height')
  76. // ->orderBy("website.id", "asc")
  77. // ->limit($data['pageSize'])
  78. // ->offset(($data['page'] - 1) * $data['pageSize'])
  79. // ->get();
  80. // $count = AdPlace::where($where)->count();
  81. // }
  82. // $startTime = strtotime($data['starttime']);
  83. // $endTime = strtotime($data['endtime']);
  84. // $time = ($endTime - $startTime) / (24 * 60 * 60);
  85. // $roundedValue = round($time, 2);
  86. // $days = number_format($roundedValue, 2, '.', '');
  87. // $result = [
  88. // 'rows' => $rep->toArray(),
  89. // 'count' => $count,
  90. // 'days' => $days,
  91. // ];
  92. // if (empty($result['rows'])) {
  93. // return Result::error("暂无符合您条件的广告位!");
  94. // }
  95. // } else {
  96. // $result = AdSize::get();
  97. // if (empty($result)) {
  98. // return Result::error('暂无广告尺寸!');
  99. // }
  100. // }
  101. //获取站点下所有的广告尺寸
  102. $adssizeid = AdPlace::where('website_id', $data['website_id'] ?? 2)->select('ad_size_id')->get()->all();
  103. $adssizeidArray = array_unique(array_column($adssizeid, 'ad_size_id'));
  104. var_dump($adssizeidArray, 'p--------------1-------');
  105. $adssize = AdSize::whereIn('id', $adssizeidArray)->select('id', 'width', 'height')->get();
  106. return Result::success($adssize);
  107. }
  108. /**
  109. * 添加订单
  110. * @param
  111. * @return void
  112. */
  113. public function addOrder(array $data): array
  114. {
  115. $ads = Ad::whereIn($data['id'])
  116. ->leftJoin('ad_place', 'ad.pid', 'ad_place.id')
  117. ->leftJoin("article_data", "article.id", "article_data.article_id")
  118. ->select("ad_place.*", "ad.*")
  119. ->orderBy("ad.id", "desc")
  120. ->limit($data['pageSize'])
  121. ->offset(($data['page'] - 1) * $data['pageSize'])->get();
  122. $count = Ad::whereIn($data['id'])->count();
  123. $data = [
  124. 'rows' => $ads->toArray(),
  125. 'count' => $count,
  126. ];
  127. if (empty($rep)) {
  128. return Result::error("没有信息数据");
  129. }
  130. return Result::success($data);
  131. }
  132. /**
  133. * 获取订单列表
  134. * @param
  135. * @return void
  136. */
  137. public function getOrderListAdmin(array $data): array
  138. {
  139. // 获取分页参数,默认每页 10 条记录
  140. $page = isset($data['page']) ? (int) $data['page'] : 1;
  141. $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10;
  142. var_dump($data, 'p--------------1-------');
  143. // 构建查询条件
  144. $where = [];
  145. if (!empty($data['status'])) {
  146. $where = [
  147. 'order.status' => $data['status'], // 明确指定 order 表的 status 列
  148. ];
  149. }
  150. //广告订单状态
  151. if (!empty($data['ad_status'])) {
  152. $where = [
  153. 'order.ad_status' => $data['ad_status'],
  154. ];
  155. }
  156. // 添加订单号查询条件
  157. if (!empty($data['order_num'])) {
  158. $where['order.order_num'] = $data['order_num']; // 明确指定 order 表的 order_num 列
  159. }
  160. // 处理时间范围查询
  161. $start = $data['sttime'] ?? '';
  162. $end = $data['edtime'] ?? '';
  163. // 查询数据并分页
  164. $query = Order::where($where)
  165. ->when(!empty($start) && !empty($end), function ($q) use ($start, $end) {
  166. $q->whereBetween('order.fromtime', [$start, $end]); // 明确指定 order 表的 fromtime 列
  167. })
  168. ->when(!empty($start), function ($q) use ($start) {
  169. $q->where('order.fromtime', '>=', $start); // 明确指定 order 表的 fromtime 列
  170. })
  171. ->when(!empty($end), function ($q) use ($end) {
  172. $q->where('order.totime', '<=', $end); // 明确指定 order 表的 totime 列
  173. })
  174. ->leftJoin('user as admin_user', 'order.admin_user_id', '=', 'admin_user.id')
  175. ->leftJoin('user as user', 'order.user_id', '=', 'user.id')
  176. ->select(
  177. 'order.*',
  178. 'admin_user.user_name as admin_user_name',
  179. 'user.user_name as user_name'
  180. )
  181. ->orderBy('order.id', 'desc');
  182. // 执行分页查询
  183. $result = $query->paginate($perPage, ['*'], 'page', $page);
  184. // 循环获取到订单的广告位置名称信息
  185. $orderData = $result->items();
  186. foreach ($orderData as $key => $value) {
  187. $pid = $value['id'];
  188. //取出pid
  189. $ad = OrderAd::where('order_id', $pid)->select('pid')->get()->all();
  190. //ad_place 取出name
  191. $ad = array_column($ad, 'pid');
  192. var_dump($ad, 'p--------------2-------');
  193. $ad = AdPlace::whereIn('id', $ad)->select('name')->get()->all();
  194. $orderData[$key]['adname'] = implode(',', array_column($ad, 'name'));
  195. }
  196. // 返回分页结果
  197. return Result::success([
  198. 'count1' => $result->total(),
  199. 'current_page' => $result->currentPage(),
  200. 'last_page' => $result->lastPage(),
  201. 'pagesize' => $result->perPage(),
  202. 'rows' => $orderData,
  203. ]);
  204. }
  205. /**
  206. * 获取订单详情
  207. * @param
  208. * @return void
  209. */
  210. public function getOrderDetailAdmin(array $data): array
  211. {
  212. $order = Order::where('order.id', $data['id'])
  213. ->leftJoin('user as admin_user', 'order.admin_user_id', '=', 'admin_user.id')
  214. ->leftJoin('user as user', 'order.user_id', '=', 'user.id')
  215. ->select(
  216. 'order.*',
  217. 'admin_user.user_name as admin_user_name',
  218. 'user.user_name as user_name'
  219. )->first();
  220. if (empty($order)) {
  221. return Result::error("没有信息数据");
  222. }
  223. $pid = $order['id'];
  224. $ad = OrderAd::where('order_id', $pid)
  225. ->leftJoin('ad_place as ad', 'order_ad.pid', '=', 'ad.id')
  226. ->select('order_ad.*', 'ad.name as ad_name')
  227. ->get();
  228. $order['ad'] = $ad;
  229. return Result::success($order);
  230. }
  231. /**
  232. * 修改订单价格
  233. * @param
  234. * @return void
  235. */
  236. public function editPriceOrderAdmin(array $data): array
  237. {
  238. $order = Order::where('id', $data['id'])->first();
  239. if (empty($order)) {
  240. return Result::error("没有信息数据");
  241. }
  242. $order->price = $data['price'];
  243. $order->save();
  244. return Result::success($order);
  245. }
  246. /**
  247. *拒绝订单
  248. * @param
  249. * @return void
  250. */
  251. public function rejectOrderAdmin(array $data): array
  252. {
  253. $order = Order::where('id', $data['id'])->first();
  254. if (empty($order)) {
  255. return Result::error("没有信息数据");
  256. }
  257. Db::beginTransaction();
  258. try {
  259. $order->status = 2; //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  260. $order->ad_status = 2;
  261. $order->reason = $data['reason'];
  262. $order->bhtime = date('Y-m-d H:i:s');
  263. $order->save();
  264. OrderAd::where('order_id', $data['id'])
  265. ->update(['status' => 2]);
  266. Db::commit();
  267. } catch (\Exception $e) {
  268. Db::rollBack();
  269. return Result::error("操作失败");
  270. }
  271. return Result::success($order);
  272. }
  273. /**
  274. * 结束订单
  275. * @param
  276. * @return void
  277. */
  278. public function endOrderAdmin(array $data): array
  279. {
  280. $order = Order::where('id', $data['id'])->first();
  281. if (empty($order)) {
  282. return Result::error("没有信息数据");
  283. }
  284. Db::beginTransaction();
  285. try {
  286. $order->status = 7;
  287. $order->jstime = date('Y-m-d H:i:s');
  288. $order->save();
  289. // 获取 order_ad 表中的记录
  290. $orderAds = OrderAd::where('order_id', $data['id'])
  291. ->update(['status' => 7]);
  292. // 在ad表中删除相同pid的数据
  293. Ad::where('order_id', $data['id'])->delete();
  294. Db::commit();
  295. } catch (\Exception $e) {
  296. Db::rollBack();
  297. return Result::error("操作失败" . $e->getMessage());
  298. }
  299. return Result::success($order);
  300. }
  301. /**
  302. * 删除订单
  303. * @param
  304. * @return void
  305. */
  306. public function delOrderAdmin(array $data): array
  307. {
  308. // 获取订单信息
  309. $order = Order::where('id', $data['id'])->first();
  310. if (empty($order)) {
  311. return Result::error("没有信息数据");
  312. }
  313. Db::beginTransaction();
  314. try {
  315. Order::where('id', $data['id'])->delete();
  316. var_dump(11111111);
  317. // 获取 order_ad 表中的记录
  318. OrderAd::where('order_id', $data['id'])->delete();
  319. // 删除 ad 表中的记录
  320. Ad::where('order_id', $data['id'])->delete();
  321. // 提交事务
  322. Db::commit();
  323. } catch (\Exception $e) {
  324. // 回滚事务
  325. Db::rollBack();
  326. // 返回错误信息
  327. return Result::error($e->getMessage());
  328. }
  329. // 返回成功信息
  330. return Result::success("删除成功");
  331. }
  332. /**
  333. * 审核订单
  334. * @param
  335. * @return void
  336. */
  337. public function applyOrderStatusAdmin(array $data): array
  338. {
  339. $order = Order::where('id', $data['id'])
  340. ->where('status', 6)
  341. ->first();
  342. if (empty($order)) {
  343. return Result::error("没有信息数据");
  344. }
  345. Db::beginTransaction();
  346. try {
  347. $order->status = 1;
  348. //判断是否在周期范围内
  349. $ad_status = 8; // 待生效
  350. if ($order->starttime < date('Y-m-d H:i:s') && date('Y-m-d H:i:s') < $order->endtime) {
  351. // 条件满足时的逻辑
  352. $ad_status = 1;
  353. }
  354. $order->ad_status = 1;
  355. $order->shtime = date('Y-m-d H:i:s');
  356. $order->save();
  357. // 批量更新 order_ad 表中的状态
  358. OrderAd::where('order_id', $data['id'])
  359. ->where('status', 6)
  360. ->update(['status' => 1]);
  361. //判断的当前时间是否在订单的开始时间之后,并且小于等于订单的结束时间
  362. if (time() >= strtotime($order->sttime) && time() < strtotime($order->edtime)) {
  363. $ad_status = 1; //审核生效
  364. } else {
  365. $ad_status = 2; //审核
  366. }
  367. $ads = [];
  368. $orderAds = OrderAd::where('order_id', $data['id'])->get();
  369. foreach ($orderAds as $orderAd) {
  370. $ads[] = [
  371. 'name' => $orderAd->name,
  372. 'pid' => $orderAd->pid,
  373. 'areaid' => $orderAd->areaid,
  374. 'amount' => $orderAd->amount,
  375. 'introduce' => $orderAd->introduce,
  376. 'hits' => $orderAd->hits,
  377. 'admin_user_id' => $orderAd->admin_user_id,
  378. 'fromtime' => $orderAd->fromtime,
  379. 'totime' => $orderAd->totime,
  380. 'text_name' => $orderAd->text_name,
  381. 'text_url' => $orderAd->text_url,
  382. 'text_title' => $orderAd->text_title,
  383. 'image_src' => $orderAd->image_src,
  384. 'image_url' => $orderAd->image_url,
  385. 'image_alt' => $orderAd->image_alt,
  386. 'video_src' => $orderAd->video_src,
  387. 'video_url' => $orderAd->video_url,
  388. 'video_auto' => $orderAd->video_auto,
  389. 'video_loop' => $orderAd->video_loop,
  390. 'status' => $ad_status,
  391. 'order_id' => $orderAd->order_id,
  392. ];
  393. }
  394. Ad::insert($ads);
  395. Db::commit();
  396. } catch (\Exception $e) {
  397. Db::rollBack();
  398. return Result::error($e->getMessage());
  399. }
  400. return Result::success($order);
  401. }
  402. /*
  403.  * 根据用户条件及网站搜索没有广告的广告位
  404.  * @param
  405.  * @return void
  406. */
  407. public function getWebsiteAd(array $data): array
  408. {
  409. $where['website_id'] = $data['website_id'];
  410. $where['ad_size_id'] = $data['ad_size_id'] ?? 1;
  411. $start = Carbon::parse($data['starttime']);
  412. $end = Carbon::parse($data['endtime']);
  413. // $status = [
  414. // 0 => '1',
  415. // 1 => '7',
  416. // 2 => '6',
  417. // ];
  418. //订单状态:1:通过;2:驳回;3:撤回;5:过期;6:待审核;7:结束
  419. // 筛选已上架的广告有时间冲突的广告位
  420. $ads = Ad::where('fromtime', '<=', $start)->where('totime', '>=', $end)->select('pid')->get()->all();
  421. $orderads = OrderAd::where('fromtime', '<=', $start)->where('totime', '>=', $end)->where('status', 1)->select('pid')->get()->all();
  422. $ads = array_column($ads, 'pid');
  423. $orderads = array_column($orderads, 'pid');
  424. $pids = array_merge($ads, $orderads);
  425. var_dump($pids, '-----------1-------------');
  426. //取出pid
  427. $ad_pids = array_unique($pids);
  428. var_dump($ad_pids, '----------3------------');
  429. $placeids = AdPlace::whereNotIn('id', $ad_pids)->where($where)->select('id')->get()->all();
  430. $ad_pids = array_column($placeids, 'id');
  431. $rep = AdPlace::where($where)
  432. ->whereIn('ad_place.id', $ad_pids)
  433. ->where('ad_place.website_id', $data['website_id'])
  434. ->leftJoin('website', 'ad_place.website_id', 'website.id')
  435. ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
  436. ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
  437. ->selectSub('website.id', 'webid')
  438. ->selectSub('ad_place.id', 'pid')
  439. ->selectSub('ad_size.width', 'size_width')
  440. ->selectSub('ad_size.height', 'size_height')
  441. ->orderBy("website.id", "asc")
  442. ->limit($data['pageSize'])
  443. ->offset(($data['page'] - 1) * $data['pageSize'])
  444. ->get();
  445. $count = AdPlace::where($where)->where('ad_place.website_id', $data['website_id'])->whereIn('ad_place.id', $ad_pids)->count();
  446. $startTime = strtotime($data['starttime']);
  447. $endTime = strtotime($data['endtime']);
  448. $time = ($endTime - $startTime) / (24 * 60 * 60);
  449. $roundedValue = round($time, 2);
  450. $days = number_format($roundedValue, 2, '.', '');
  451. $result = [
  452. 'rows' => $rep->toArray(),
  453. 'count' => $count,
  454. 'days' => $days,
  455. ];
  456. return Result::success($result);
  457. // //ad_size_id 必选改可选
  458. // //单选
  459. // if (isset($data['ad_size_id']) && is_string($data['ad_size_id'])) {
  460. // $where = [
  461. // 'ad_place.ad_size_id' => $data['ad_size_id'],
  462. // ];
  463. // }
  464. // //如果有website_id
  465. // if (!isset($data['ad_size_id'])) {
  466. // // $adsiteids = AdPlace::where('website_id', $data['website_id'])->select('id')->get()->all();
  467. // // $ad_pids = array_column($adsiteids, 'id');
  468. // // // $where = [
  469. // // // 'ad_place.ad_size_id' => $data['ad_size_id'],
  470. // // // ];
  471. // // var_dump($ad_pids, 'p--------------3-------');
  472. // // // $where[] = ['ad_place.id', 'in', $ad_pids];
  473. // // $where = [
  474. // // 'ad_place.website_id' => $data['website_id'],
  475. // // ];
  476. // }
  477. // //数组
  478. // $start = Carbon::parse($data['starttime']);
  479. // $end = Carbon::parse($data['endtime']);
  480. // $status = [
  481. // 0 => '1',
  482. // 1 => '4',
  483. // 2 => '6',
  484. // ];
  485. // //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  486. // $ads = Ad::where('fromtime', '<=', $start)->where('totime', '>=', $end)->select('pid')->get()->all();
  487. // $orderads = OrderAd::where('fromtime', '<=', $start)->where('totime', '>=', $end)->whereIn('status', $status)->select('pid')->get()->all();
  488. // $pids = array_merge($ads, $orderads);
  489. // $ad_pids = array_unique($pids);
  490. // //$ad_pids=所有与用户提交的时间有冲突的广告位pid
  491. // if (!empty($ad_pids)) {
  492. // $pid = [];
  493. // foreach ($ad_pids as $val) {
  494. // array_push($pid, $val['pid']);
  495. // }
  496. // //去掉时间冲突并且符合图片尺寸的广告位
  497. // $placeids = AdPlace::whereNotIn('id', $pid)->where($where)->select('id', 'website_id')->get()->all();
  498. // if (empty($placeids)) {
  499. // return Result::error('暂无数据!');
  500. // }
  501. // if (!isset($data['website_id'])) {
  502. // $websiteIds = array_column($placeids, 'website_id');
  503. // $website_id = array_unique($websiteIds);
  504. // $rep = Website::whereIn('id', $website_id)->get();
  505. // $count = Website::whereIn('id', $website_id)->count();
  506. // //若不存在网站id参数直接返回符合条件的广告位相关联的网站名称
  507. // } else {
  508. // $place_id = [];
  509. // foreach ($placeids as $val) {
  510. // array_push($place_id, $val['id']);
  511. // }
  512. // $rep = AdPlace::where($where)
  513. // ->whereIn('ad_place.id', $place_id)
  514. // ->where('ad_place.website_id', $data['website_id'])
  515. // ->leftJoin('website', 'ad_place.website_id', 'website.id')
  516. // ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
  517. // ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
  518. // ->selectSub('website.id', 'webid')
  519. // ->selectSub('ad_place.id', 'pid')
  520. // ->selectSub('ad_size.width', 'size_width')
  521. // ->selectSub('ad_size.height', 'size_height')
  522. // ->orderBy("website.id", "asc")
  523. // ->limit($data['pageSize'])
  524. // ->offset(($data['page'] - 1) * $data['pageSize'])
  525. // ->get();
  526. // $count = AdPlace::where($where)->where('ad_place.website_id', $data['website_id'])->count();
  527. // //若存在网站id,关联查询是需要添加website_id条件查询
  528. // }
  529. // } else {
  530. // //若不存在有时间冲突的广告位则所有符合图片尺寸的广告位皆可以使用,只需要判断是否存在网站id参数即可
  531. // if (isset($data['website_id'])) {
  532. // $rep = AdPlace::where($where)
  533. // ->where('ad_place.website_id', $data['website_id'])
  534. // ->leftJoin('website', 'ad_place.website_id', 'website.id')
  535. // ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
  536. // ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
  537. // ->selectSub('website.id', 'webid')
  538. // ->selectSub('ad_place.id', 'pid')
  539. // ->selectSub('ad_size.width', 'size_width')
  540. // ->selectSub('ad_size.height', 'size_height')
  541. // ->orderBy("website.id", "asc")
  542. // ->limit($data['pageSize'])
  543. // ->offset(($data['page'] - 1) * $data['pageSize'])
  544. // ->get();
  545. // $count = AdPlace::where($where)->where('ad_place.website_id', $data['website_id'])->count();
  546. // } else {
  547. // $place_all = AdPlace::where($where)->select('website_id')->get()->all();
  548. // $place_allads = [];
  549. // foreach ($place_all as $v) {
  550. // array_push($place_allads, $v['website_id']);
  551. // }
  552. // $rep = Website::whereIn('id', $place_allads)->get();
  553. // $count = Website::whereIn('id', $place_allads)->count();
  554. // }
  555. // }
  556. // if (empty($rep)) {
  557. // return Result::error("暂无符合您条件的广告位!");
  558. // }
  559. // $startTime = strtotime($data['starttime']);
  560. // $endTime = strtotime($data['endtime']);
  561. // $time = ($endTime - $startTime) / (24 * 60 * 60);
  562. // $roundedValue = round($time, 2);
  563. // $days = number_format($roundedValue, 2, '.', '');
  564. // $result = [
  565. // 'rows' => $rep->toArray(),
  566. // 'count' => $count,
  567. // 'days' => $days,
  568. // ];
  569. // return Result::success($result);
  570. }
  571. /**
  572.  * 获取广告金额
  573.  * @param
  574.  * @return void
  575. */
  576. public function getPrice(array $data): array
  577. {
  578. // $data['pid'] = [15,16];
  579. $startTime = strtotime($data['starttime']);
  580. $endTime = strtotime($data['endtime']);
  581. $days = ($endTime - $startTime) / (24 * 60 * 60); //计算共计多少天
  582. $price = 0;
  583. if (isset($data['pid'])) {
  584. $ad_price = AdPlace::whereIn('id', $data['pid'])->select('id', 'price')->get();
  585. foreach ($ad_price as $v) {
  586. $price += number_format($v['price'] * $days, 2, '.', '');
  587. }
  588. } else {
  589. $price = 0;
  590. }
  591. // 确保 $price带有两位小数位数
  592. $price = number_format((float) $price, 2, '.', '');
  593. return Result::success($price);
  594. }
  595. /**
  596.  * 添加广告订单
  597.  * @param
  598.  * @return void
  599. */
  600. public function addAD(array $data): array
  601. {
  602. date_default_timezone_set('Asia/Shanghai');
  603. $time = time();
  604. $startTime = strtotime($data['starttime']);
  605. $endTime = strtotime($data['endtime']);
  606. $days = ($endTime - $startTime) / (24 * 60 * 60); //计算共计多少天
  607. $timestamp = date('YmdHis', $time);
  608. $catetime = date('Y-m-d H:i:s', $time);
  609. $randomNumber = mt_rand(1000, 9999);
  610. $ordernum = $randomNumber . $timestamp; // 时间戳与随机数拼接
  611. $order_size = AdSize::where('id', $data['ad_size_id'])->first();
  612. // var_dump(($time));
  613. Db::beginTransaction();
  614. try {
  615. $order = [
  616. 'order_num' => $ordernum,
  617. 'name' => $data['name'],
  618. 'sttime' => $data['starttime'],
  619. 'edtime' => $data['endtime'],
  620. 'user_id' => $data['user_id'],
  621. 'cttime' => $catetime,
  622. 'width' => $order_size['width'],
  623. 'height' => $order_size['height'],
  624. 'days' => $days,
  625. 'price' => $data['price'],
  626. 'created_at' => date('Y-m-d H:i:s', time()),
  627. 'updated_at' => date('Y-m-d H:i:s', time()),
  628. ];
  629. //添加订单
  630. $orderid = Order::insertGetId($order);
  631. $adplace = $data['pid'];
  632. if (is_array($data['pid'])) {
  633. $adplace = AdPlace::whereIn('id', $data['pid'])->select('website_id', 'id')->get();
  634. $order_ad = [];
  635. foreach ($adplace as $key => $ads) {
  636. $order_ad[$key] = [
  637. 'order_id' => $orderid,
  638. 'order_num' => $ordernum,
  639. 'name' => $data['name'],
  640. 'fromtime' => $data['starttime'],
  641. 'totime' => $data['endtime'],
  642. 'image_src' => $data['imgsrc'],
  643. 'image_url' => $data['imgurl'],
  644. 'pid' => $ads['id'],
  645. 'website_id' => $ads['website_id'],
  646. ];
  647. }
  648. }
  649. $orderad_id = OrderAd::insert($order_ad);
  650. Db::commit();
  651. } catch (\Exception $e) {
  652. Db::rollBack();
  653. return Result::error($e->getMessage());
  654. }
  655. // $log = AdLog::insert($log);
  656. $result = [
  657. 'order_id' => $orderid,
  658. 'orderad_id' => $orderad_id,
  659. 'name' => $data['name'],
  660. 'ordernum' => $ordernum,
  661. ];
  662. return Result::success($result);
  663. }
  664. /**
  665.  * 获取订单列表
  666.  * @param
  667.  * @return void
  668. */
  669. public function getOrderList(array $data): array
  670. {
  671. $where = [
  672. 'user_id' => $data['user_id'],
  673. 'user_del' => 2,
  674. ];
  675. if (isset($data['status'])) {
  676. $where['status'] = $data['status'];
  677. }
  678. $orders = Order::where($where)
  679. ->limit($data['pageSize'])
  680. ->offset(($data['page'] - 1) * $data['pageSize'])
  681. ->orderBy("updated_at", "desc")
  682. ->get()
  683. ->all();
  684. //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  685. $count['all'] = Order::where($where)->count();
  686. $count['pass'] = Order::where($where)->where('status', 1)->count();
  687. $count['return'] = Order::where($where)->where('status', 2)->count();
  688. $count['recall'] = Order::where($where)->where('status', 3)->count();
  689. $count['update'] = Order::where($where)->where('status', 4)->count();
  690. $count['fail'] = Order::where($where)->where('status', 5)->count();
  691. $count['waite'] = Order::where($where)->where('status', 6)->count();
  692. $count['over'] = Order::where($where)->where('status', 7)->count();
  693. foreach ($orders as $key => $val) {
  694. $adsnum = OrderAd::where('order_id', $val['id'])->count();
  695. $rep[$key] = $val;
  696. $rep[$key]['num'] = $adsnum;
  697. }
  698. if (empty($rep)) {
  699. return Result::error("您暂时还没有下单");
  700. } else {
  701. $data = [
  702. 'rows' => $rep,
  703. 'count' => $count,
  704. ];
  705. }
  706. return Result::success($data);
  707. }
  708. /**
  709.  * 获取订单详情
  710.  * @param
  711.  * @return void
  712. */
  713. public function getOrderDetail(array $data): array
  714. {
  715. $order = Order::where('id', $data['order_id'])->first();
  716. $orderads = OrderAd::where('order_ad.order_id', $data['order_id'])
  717. ->leftJoin('website', 'order_ad.website_id', 'website.id')
  718. ->leftJoin('ad_place', 'order_ad.pid', 'ad_place.id')
  719. ->select('order_ad.*', 'website.website_name', 'website.id', 'ad_place.name')
  720. ->selectSub('website.id', 'webid')
  721. ->selectSub('order_ad.id', 'oid')
  722. ->selectSub('order_ad.name', 'adname')
  723. ->selectSub('ad_place.name', 'apname')
  724. ->orderBy("website.id", "asc")
  725. ->limit($data['pageSize'])
  726. ->offset(($data['page'] - 1) * $data['pageSize'])
  727. ->get();
  728. if (empty($order)) {
  729. return Result::error("订单id错误");
  730. }
  731. $count = OrderAd::where('order_ad.order_id', $data['order_id'])->count();
  732. $result = [
  733. 'order' => $order,
  734. 'orderads' => $orderads,
  735. 'count' => $count,
  736. ];
  737. return Result::success($result);
  738. }
  739. /**
  740.  * 撤回订单
  741.  * @param
  742.  * @return void
  743. */
  744. public function cancelOrder(array $data): array
  745. {
  746. date_default_timezone_set('Asia/Shanghai');
  747. $time = time();
  748. $timestamp = date('YmdHis', $time);
  749. Db::beginTransaction();
  750. try {
  751. $order = Order::where('id', $data['order_id'])->where('status', 6)->where('sttime', '>=', $timestamp)->update(['status' => 3, 'ad_status' => '3']);
  752. $ads = OrderAd::where('order_id', $data['order_id'])->where('status', 6)->where('fromtime', '>=', $timestamp)->update(['status' => 3]);
  753. Db::commit();
  754. } catch (\Exception $e) {
  755. Db::rollBack();
  756. return Result::error($e->getMessage());
  757. }
  758. $result = [
  759. 'order' => $order,
  760. 'ads' => $ads,
  761. ];
  762. if ($order == 0) {
  763. return Result::error("此订单不可撤回");
  764. }
  765. return Result::success($result);
  766. }
  767. /**
  768.  * 删除订单
  769.  * @param
  770.  * @return void
  771. */
  772. public function delOrderAD(array $data): array
  773. {
  774. $data['status'] = [
  775. 0 => 2,
  776. 1 => 3,
  777. 2 => 5,
  778. 3 => 7,
  779. ];
  780. //1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束;
  781. date_default_timezone_set('Asia/Shanghai');
  782. $time = time();
  783. $timestamp = date('YmdHis', $time);
  784. $where = [
  785. ['id', '=', $data['id']],
  786. ];
  787. $time = [['edtime', '<=', $timestamp]];
  788. $order = Order::where($where)->where($time)->update(['user_del' => 1]);
  789. if (empty($order)) {
  790. $order = Order::where($where)->whereIn('status', $data['status'])->update(['user_del' => 1]);
  791. }
  792. if ($order == 0) {
  793. return Result::error("此订单不可删除");
  794. }
  795. return Result::success($order);
  796. }
  797. /**
  798.  * 创建购物车
  799.  * @param
  800.  * @return void
  801. */
  802. public function addShoppingCart(array $data): array
  803. {
  804. $shop = ShoppingCart::where('user_id', $data['user_id'])->get()->all();
  805. $time = time();
  806. $timestamp = date('YmdHis', $time);
  807. $randomNumber = mt_rand(100, 999);
  808. $shopping_id = $randomNumber . $timestamp; // 时间戳与随机数拼接
  809. Db::beginTransaction();
  810. try {
  811. if (!empty($shop)) {
  812. $del_shop = ShoppingCart::where('user_id', $data['user_id'])->delete();
  813. if (empty($del_shop)) {
  814. return Result::error("删除失败");
  815. }
  816. }
  817. $shop = [
  818. 'user_id' => $data['user_id'],
  819. 'shopping_id' => $shopping_id,
  820. 'created_at' => date('Y-m-d H:i:s', time()),
  821. 'updated_at' => date('Y-m-d H:i:s', time()),
  822. ];
  823. $result = ShoppingCart::insert($shop);
  824. Db::commit();
  825. } catch (\Exception $e) {
  826. Db::rollBack();
  827. return Result::error($e->getMessage());
  828. }
  829. if (empty($result)) {
  830. return Result::error("创建失败");
  831. } else {
  832. return Result::success($shopping_id);
  833. }
  834. }
  835. /**
  836.  * 获取购物车中的广告位
  837.  * @param
  838.  * @return void
  839. */
  840. public function getShoppingCartAD(array $data): array
  841. {
  842. $shopcart = ShoppingCart::where('shopping_id', $data['shopping_id'])
  843. ->where('user_id', $data['user_id'])
  844. ->orderBy('pid')
  845. ->pluck('pid')
  846. ->toArray();
  847. $result['pid'] = $shopcart;
  848. if (empty($result)) {
  849. return Result::error("购物车id错误");
  850. } else {
  851. return Result::success($result);
  852. }
  853. }
  854. /**
  855.  * 添加购物车中的广告位
  856.  * @param
  857.  * @return void
  858. */
  859. public function addShoppingCartAD(array $data): array
  860. {
  861. $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->first();
  862. if (empty($shop)) {
  863. return Result::error("购物车id错误");
  864. } else {
  865. $ad = AdPlace::where('ad_place.id', $data['pid'])
  866. ->leftJoin('website', 'ad_place.website_id', 'website.id')
  867. ->select('ad_place.id as pid', 'website.id as website_id')
  868. ->first();
  869. if (empty($ad)) {
  870. return Result::error("广告位id错误");
  871. }
  872. if (empty($shop['pid']) && empty($shop['website_id'])) {
  873. $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id', $data['user_id'])->update(['pid' => $ad['pid'], 'website_id' => $ad['website_id']]);
  874. } else {
  875. if ($data['pid'] == $shop['pid']) {
  876. return Result::error("购物车中已存在该广告位");
  877. }
  878. $shop_ad = [
  879. 'pid' => $ad['pid'],
  880. 'website_id' => $ad['website_id'],
  881. 'shopping_id' => $shop['shopping_id'],
  882. 'user_id' => $data['user_id'],
  883. ];
  884. $result = ShoppingCart::insertGetId($shop_ad);
  885. }
  886. }
  887. if (empty($result)) {
  888. return Result::error("添加失败");
  889. } else {
  890. return Result::success($result);
  891. }
  892. }
  893. /**
  894.  * 删除购物车中的广告位
  895.  * @param
  896.  * @return void
  897. */
  898. public function delShoppingCartAD(array $data): array
  899. {
  900. $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->where('pid', $data['pid'])->first();
  901. if (empty($shop)) {
  902. return Result::error("不存在此条记录!(参数错误)");
  903. } else {
  904. $count = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->count();
  905. if ($count == 1) {
  906. $result = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->update(['pid' => null, 'website_id' => null]);
  907. } else {
  908. $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id', $data['user_id'])->where('pid', $data['pid'])->delete();
  909. }
  910. }
  911. if (empty($result)) {
  912. return Result::error("删除失败");
  913. } else {
  914. return Result::success($result);
  915. }
  916. }
  917. /**
  918.  * 获取广告位的尺寸
  919.  *
  920.  *
  921. */
  922. public function getAdSize(): array
  923. {
  924. $result = AdSize::get()->all();
  925. if (empty($result)) {
  926. return Result::error("暂无数据");
  927. }
  928. return Result::success($result);
  929. }
  930. }