OrderService.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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. if (empty($order['price'])) {
  243. return Result::error("价格字段错误");
  244. }
  245. if (empty($data['sttime'])) {
  246. return Result::error("开始时间错误");
  247. }
  248. if (empty($data['edtime'])) {
  249. return Result::error("结束时间错误");
  250. }
  251. $order->price = $data['price'];
  252. $order->sttime = $data['sttime'];
  253. $order->edtime = $data['edtime'];
  254. $order->save();
  255. return Result::success($order);
  256. }
  257. /**
  258. *拒绝订单
  259. * @param
  260. * @return void
  261. */
  262. public function rejectOrderAdmin(array $data): array
  263. {
  264. $order = Order::where('id', $data['id'])->first();
  265. if (empty($order)) {
  266. return Result::error("没有信息数据");
  267. }
  268. Db::beginTransaction();
  269. try {
  270. $order->status = 2; //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  271. $order->ad_status = 2;
  272. $order->reason = $data['reason'];
  273. $order->bhtime = date('Y-m-d H:i:s');
  274. $order->save();
  275. OrderAd::where('order_id', $data['id'])
  276. ->update(['status' => 2]);
  277. Db::commit();
  278. } catch (\Exception $e) {
  279. Db::rollBack();
  280. return Result::error("操作失败");
  281. }
  282. return Result::success($order);
  283. }
  284. /**
  285. * 结束订单
  286. * @param
  287. * @return void
  288. */
  289. public function endOrderAdmin(array $data): array
  290. {
  291. $order = Order::where('id', $data['id'])->first();
  292. if (empty($order)) {
  293. return Result::error("没有信息数据");
  294. }
  295. Db::beginTransaction();
  296. try {
  297. $order->status = 7;
  298. $order->jstime = date('Y-m-d H:i:s');
  299. $order->save();
  300. // 获取 order_ad 表中的记录
  301. $orderAds = OrderAd::where('order_id', $data['id'])
  302. ->update(['status' => 7]);
  303. // 在ad表中删除相同pid的数据
  304. Ad::where('order_id', $data['id'])->delete();
  305. Db::commit();
  306. } catch (\Exception $e) {
  307. Db::rollBack();
  308. return Result::error("操作失败" . $e->getMessage());
  309. }
  310. return Result::success($order);
  311. }
  312. /**
  313. * 删除订单
  314. * @param
  315. * @return void
  316. */
  317. public function delOrderAdmin(array $data): array
  318. {
  319. // 获取订单信息
  320. $order = Order::where('id', $data['id'])->first();
  321. if (empty($order)) {
  322. return Result::error("没有信息数据");
  323. }
  324. Db::beginTransaction();
  325. try {
  326. Order::where('id', $data['id'])->delete();
  327. var_dump(11111111);
  328. // 获取 order_ad 表中的记录
  329. OrderAd::where('order_id', $data['id'])->delete();
  330. // 删除 ad 表中的记录
  331. Ad::where('order_id', $data['id'])->delete();
  332. // 提交事务
  333. Db::commit();
  334. } catch (\Exception $e) {
  335. // 回滚事务
  336. Db::rollBack();
  337. // 返回错误信息
  338. return Result::error($e->getMessage());
  339. }
  340. // 返回成功信息
  341. return Result::success("删除成功");
  342. }
  343. /**
  344. * 审核订单
  345. * @param
  346. * @return void
  347. */
  348. public function applyOrderStatusAdmin(array $data): array
  349. {
  350. $order = Order::where('id', $data['id'])
  351. ->where('status', 6)
  352. ->first();
  353. if (empty($order)) {
  354. return Result::error("没有信息数据");
  355. }
  356. Db::beginTransaction();
  357. try {
  358. $order->status = 1;
  359. //判断是否在周期范围内
  360. $ad_status = 8; // 待生效
  361. if ($order->starttime < date('Y-m-d H:i:s') && date('Y-m-d H:i:s') < $order->endtime) {
  362. // 条件满足时的逻辑
  363. $ad_status = 1;
  364. }
  365. $order->ad_status = 1;
  366. $order->shtime = date('Y-m-d H:i:s');
  367. $order->save();
  368. // 批量更新 order_ad 表中的状态
  369. OrderAd::where('order_id', $data['id'])
  370. ->where('status', 6)
  371. ->update(['status' => 1]);
  372. //判断的当前时间是否在订单的开始时间之后,并且小于等于订单的结束时间
  373. if (time() >= strtotime($order->sttime) && time() < strtotime($order->edtime)) {
  374. $ad_status = 1; //审核生效
  375. } else {
  376. $ad_status = 2; //审核
  377. }
  378. $ads = [];
  379. $orderAds = OrderAd::where('order_id', $data['id'])->get();
  380. foreach ($orderAds as $orderAd) {
  381. $ads[] = [
  382. 'name' => $orderAd->name,
  383. 'pid' => $orderAd->pid,
  384. 'areaid' => $orderAd->areaid,
  385. 'amount' => $orderAd->amount,
  386. 'introduce' => $orderAd->introduce,
  387. 'hits' => $orderAd->hits,
  388. 'admin_user_id' => $orderAd->admin_user_id,
  389. 'fromtime' => $orderAd->fromtime,
  390. 'totime' => $orderAd->totime,
  391. 'text_name' => $orderAd->text_name,
  392. 'text_url' => $orderAd->text_url,
  393. 'text_title' => $orderAd->text_title,
  394. 'image_src' => $orderAd->image_src,
  395. 'image_url' => $orderAd->image_url,
  396. 'image_alt' => $orderAd->image_alt,
  397. 'video_src' => $orderAd->video_src,
  398. 'video_url' => $orderAd->video_url,
  399. 'video_auto' => $orderAd->video_auto,
  400. 'video_loop' => $orderAd->video_loop,
  401. 'status' => $ad_status,
  402. 'order_id' => $orderAd->order_id,
  403. ];
  404. }
  405. Ad::insert($ads);
  406. Db::commit();
  407. } catch (\Exception $e) {
  408. Db::rollBack();
  409. return Result::error($e->getMessage());
  410. }
  411. return Result::success($order);
  412. }
  413. /*
  414.  * 根据用户条件及网站搜索没有广告的广告位
  415.  * @param
  416.  * @return void
  417. */
  418. public function getWebsiteAd(array $data): array
  419. {
  420. $where['website_id'] = $data['website_id'];
  421. $where['ad_size_id'] = $data['ad_size_id'] ?? 1;
  422. $start = Carbon::parse($data['starttime']);
  423. $end = Carbon::parse($data['endtime']);
  424. $status = [
  425. 0 => '1',
  426. 1 => '4',
  427. 2 => '6',
  428. ];
  429. //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  430. $ads = Ad::where('fromtime', '<=', $start)->where('totime', '>=', $end)->select('pid')->get()->all();
  431. $orderads = OrderAd::where('fromtime', '<=', $start)->where('totime', '>=', $end)->whereIn('status', $status)->select('pid')->get()->all();
  432. $ads = array_column($ads, 'pid');
  433. $orderads = array_column($orderads, 'pid');
  434. $pids = array_merge($ads, $orderads);
  435. var_dump($pids, '-----------1-------------');
  436. //取出pid
  437. $ad_pids = array_unique($pids);
  438. var_dump($ad_pids, '----------3------------');
  439. $placeids = AdPlace::whereNotIn('id', $ad_pids)->where($where)->select('id')->get()->all();
  440. $ad_pids = array_column($placeids, 'id');
  441. $rep = AdPlace::where($where)
  442. ->whereIn('ad_place.id', $ad_pids)
  443. ->where('ad_place.website_id', $data['website_id'])
  444. ->leftJoin('website', 'ad_place.website_id', 'website.id')
  445. ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
  446. ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
  447. ->selectSub('website.id', 'webid')
  448. ->selectSub('ad_place.id', 'pid')
  449. ->selectSub('ad_size.width', 'size_width')
  450. ->selectSub('ad_size.height', 'size_height')
  451. ->orderBy("website.id", "asc")
  452. ->limit($data['pageSize'])
  453. ->offset(($data['page'] - 1) * $data['pageSize'])
  454. ->get();
  455. $count = AdPlace::where($where)->where('ad_place.website_id', $data['website_id'])->whereIn('ad_place.id', $ad_pids)->count();
  456. $startTime = strtotime($data['starttime']);
  457. $endTime = strtotime($data['endtime']);
  458. $time = ($endTime - $startTime) / (24 * 60 * 60);
  459. $roundedValue = round($time, 2);
  460. $days = number_format($roundedValue, 2, '.', '');
  461. $result = [
  462. 'rows' => $rep->toArray(),
  463. 'count' => $count,
  464. 'days' => $days,
  465. ];
  466. return Result::success($result);
  467. // //ad_size_id 必选改可选
  468. // //单选
  469. // if (isset($data['ad_size_id']) && is_string($data['ad_size_id'])) {
  470. // $where = [
  471. // 'ad_place.ad_size_id' => $data['ad_size_id'],
  472. // ];
  473. // }
  474. // //如果有website_id
  475. // if (!isset($data['ad_size_id'])) {
  476. // // $adsiteids = AdPlace::where('website_id', $data['website_id'])->select('id')->get()->all();
  477. // // $ad_pids = array_column($adsiteids, 'id');
  478. // // // $where = [
  479. // // // 'ad_place.ad_size_id' => $data['ad_size_id'],
  480. // // // ];
  481. // // var_dump($ad_pids, 'p--------------3-------');
  482. // // // $where[] = ['ad_place.id', 'in', $ad_pids];
  483. // // $where = [
  484. // // 'ad_place.website_id' => $data['website_id'],
  485. // // ];
  486. // }
  487. // //数组
  488. // $start = Carbon::parse($data['starttime']);
  489. // $end = Carbon::parse($data['endtime']);
  490. // $status = [
  491. // 0 => '1',
  492. // 1 => '4',
  493. // 2 => '6',
  494. // ];
  495. // //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  496. // $ads = Ad::where('fromtime', '<=', $start)->where('totime', '>=', $end)->select('pid')->get()->all();
  497. // $orderads = OrderAd::where('fromtime', '<=', $start)->where('totime', '>=', $end)->whereIn('status', $status)->select('pid')->get()->all();
  498. // $pids = array_merge($ads, $orderads);
  499. // $ad_pids = array_unique($pids);
  500. // //$ad_pids=所有与用户提交的时间有冲突的广告位pid
  501. // if (!empty($ad_pids)) {
  502. // $pid = [];
  503. // foreach ($ad_pids as $val) {
  504. // array_push($pid, $val['pid']);
  505. // }
  506. // //去掉时间冲突并且符合图片尺寸的广告位
  507. // $placeids = AdPlace::whereNotIn('id', $pid)->where($where)->select('id', 'website_id')->get()->all();
  508. // if (empty($placeids)) {
  509. // return Result::error('暂无数据!');
  510. // }
  511. // if (!isset($data['website_id'])) {
  512. // $websiteIds = array_column($placeids, 'website_id');
  513. // $website_id = array_unique($websiteIds);
  514. // $rep = Website::whereIn('id', $website_id)->get();
  515. // $count = Website::whereIn('id', $website_id)->count();
  516. // //若不存在网站id参数直接返回符合条件的广告位相关联的网站名称
  517. // } else {
  518. // $place_id = [];
  519. // foreach ($placeids as $val) {
  520. // array_push($place_id, $val['id']);
  521. // }
  522. // $rep = AdPlace::where($where)
  523. // ->whereIn('ad_place.id', $place_id)
  524. // ->where('ad_place.website_id', $data['website_id'])
  525. // ->leftJoin('website', 'ad_place.website_id', 'website.id')
  526. // ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
  527. // ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
  528. // ->selectSub('website.id', 'webid')
  529. // ->selectSub('ad_place.id', 'pid')
  530. // ->selectSub('ad_size.width', 'size_width')
  531. // ->selectSub('ad_size.height', 'size_height')
  532. // ->orderBy("website.id", "asc")
  533. // ->limit($data['pageSize'])
  534. // ->offset(($data['page'] - 1) * $data['pageSize'])
  535. // ->get();
  536. // $count = AdPlace::where($where)->where('ad_place.website_id', $data['website_id'])->count();
  537. // //若存在网站id,关联查询是需要添加website_id条件查询
  538. // }
  539. // } else {
  540. // //若不存在有时间冲突的广告位则所有符合图片尺寸的广告位皆可以使用,只需要判断是否存在网站id参数即可
  541. // if (isset($data['website_id'])) {
  542. // $rep = AdPlace::where($where)
  543. // ->where('ad_place.website_id', $data['website_id'])
  544. // ->leftJoin('website', 'ad_place.website_id', 'website.id')
  545. // ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
  546. // ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
  547. // ->selectSub('website.id', 'webid')
  548. // ->selectSub('ad_place.id', 'pid')
  549. // ->selectSub('ad_size.width', 'size_width')
  550. // ->selectSub('ad_size.height', 'size_height')
  551. // ->orderBy("website.id", "asc")
  552. // ->limit($data['pageSize'])
  553. // ->offset(($data['page'] - 1) * $data['pageSize'])
  554. // ->get();
  555. // $count = AdPlace::where($where)->where('ad_place.website_id', $data['website_id'])->count();
  556. // } else {
  557. // $place_all = AdPlace::where($where)->select('website_id')->get()->all();
  558. // $place_allads = [];
  559. // foreach ($place_all as $v) {
  560. // array_push($place_allads, $v['website_id']);
  561. // }
  562. // $rep = Website::whereIn('id', $place_allads)->get();
  563. // $count = Website::whereIn('id', $place_allads)->count();
  564. // }
  565. // }
  566. // if (empty($rep)) {
  567. // return Result::error("暂无符合您条件的广告位!");
  568. // }
  569. // $startTime = strtotime($data['starttime']);
  570. // $endTime = strtotime($data['endtime']);
  571. // $time = ($endTime - $startTime) / (24 * 60 * 60);
  572. // $roundedValue = round($time, 2);
  573. // $days = number_format($roundedValue, 2, '.', '');
  574. // $result = [
  575. // 'rows' => $rep->toArray(),
  576. // 'count' => $count,
  577. // 'days' => $days,
  578. // ];
  579. // return Result::success($result);
  580. }
  581. /**
  582.  * 获取广告金额
  583.  * @param
  584.  * @return void
  585. */
  586. public function getPrice(array $data): array
  587. {
  588. // $data['pid'] = [15,16];
  589. $startTime = strtotime($data['starttime']);
  590. $endTime = strtotime($data['endtime']);
  591. $days = ($endTime - $startTime) / (24 * 60 * 60); //计算共计多少天
  592. $price = 0;
  593. if (isset($data['pid'])) {
  594. $ad_price = AdPlace::whereIn('id', $data['pid'])->select('id', 'price')->get();
  595. foreach ($ad_price as $v) {
  596. $price += number_format($v['price'] * $days, 2, '.', '');
  597. }
  598. } else {
  599. $price = 0;
  600. }
  601. // 确保 $price带有两位小数位数
  602. $price = number_format((float) $price, 2, '.', '');
  603. return Result::success($price);
  604. }
  605. /**
  606.  * 添加广告订单
  607.  * @param
  608.  * @return void
  609. */
  610. public function addAD(array $data): array
  611. {
  612. date_default_timezone_set('Asia/Shanghai');
  613. $time = time();
  614. $startTime = strtotime($data['starttime']);
  615. $endTime = strtotime($data['endtime']);
  616. $days = ($endTime - $startTime) / (24 * 60 * 60); //计算共计多少天
  617. $timestamp = date('YmdHis', $time);
  618. $catetime = date('Y-m-d H:i:s', $time);
  619. $randomNumber = mt_rand(1000, 9999);
  620. $ordernum = $randomNumber . $timestamp; // 时间戳与随机数拼接
  621. $order_size = AdSize::where('id', $data['ad_size_id'])->first();
  622. // var_dump(($time));
  623. Db::beginTransaction();
  624. try {
  625. $order = [
  626. 'order_num' => $ordernum,
  627. 'name' => $data['name'],
  628. 'sttime' => $data['starttime'],
  629. 'edtime' => $data['endtime'],
  630. 'user_id' => $data['user_id'],
  631. 'cttime' => $catetime,
  632. 'width' => $order_size['width'],
  633. 'height' => $order_size['height'],
  634. 'days' => $days,
  635. 'price' => $data['price'],
  636. 'created_at' => date('Y-m-d H:i:s', time()),
  637. 'updated_at' => date('Y-m-d H:i:s', time()),
  638. ];
  639. //添加订单
  640. $orderid = Order::insertGetId($order);
  641. $adplace = $data['pid'];
  642. if (is_array($data['pid'])) {
  643. $adplace = AdPlace::whereIn('id', $data['pid'])->select('website_id', 'id')->get();
  644. $order_ad = [];
  645. foreach ($adplace as $key => $ads) {
  646. $order_ad[$key] = [
  647. 'order_id' => $orderid,
  648. 'order_num' => $ordernum,
  649. 'name' => $data['name'],
  650. 'fromtime' => $data['starttime'],
  651. 'totime' => $data['endtime'],
  652. 'image_src' => $data['imgsrc'],
  653. 'image_url' => $data['imgurl'],
  654. 'pid' => $ads['id'],
  655. 'website_id' => $ads['website_id'],
  656. ];
  657. }
  658. }
  659. $orderad_id = OrderAd::insert($order_ad);
  660. Db::commit();
  661. } catch (\Exception $e) {
  662. Db::rollBack();
  663. return Result::error($e->getMessage());
  664. }
  665. // $log = AdLog::insert($log);
  666. $result = [
  667. 'order_id' => $orderid,
  668. 'orderad_id' => $orderad_id,
  669. 'name' => $data['name'],
  670. 'ordernum' => $ordernum,
  671. ];
  672. return Result::success($result);
  673. }
  674. /**
  675.  * 获取订单列表
  676.  * @param
  677.  * @return void
  678. */
  679. public function getOrderList(array $data): array
  680. {
  681. $where = [
  682. 'user_id' => $data['user_id'],
  683. 'user_del' => 2,
  684. ];
  685. if (isset($data['status'])) {
  686. $where['status'] = $data['status'];
  687. }
  688. $orders = Order::where($where)
  689. ->limit($data['pageSize'])
  690. ->offset(($data['page'] - 1) * $data['pageSize'])
  691. ->orderBy("updated_at", "desc")
  692. ->get()
  693. ->all();
  694. //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  695. $count['all'] = Order::where($where)->count();
  696. $count['pass'] = Order::where($where)->where('status', 1)->count();
  697. $count['return'] = Order::where($where)->where('status', 2)->count();
  698. $count['recall'] = Order::where($where)->where('status', 3)->count();
  699. $count['update'] = Order::where($where)->where('status', 4)->count();
  700. $count['fail'] = Order::where($where)->where('status', 5)->count();
  701. $count['waite'] = Order::where($where)->where('status', 6)->count();
  702. $count['over'] = Order::where($where)->where('status', 7)->count();
  703. foreach ($orders as $key => $val) {
  704. $adsnum = OrderAd::where('order_id', $val['id'])->count();
  705. $rep[$key] = $val;
  706. $rep[$key]['num'] = $adsnum;
  707. }
  708. if (empty($rep)) {
  709. return Result::error("您暂时还没有下单");
  710. } else {
  711. $data = [
  712. 'rows' => $rep,
  713. 'count' => $count,
  714. ];
  715. }
  716. return Result::success($data);
  717. }
  718. /**
  719.  * 获取订单详情
  720.  * @param
  721.  * @return void
  722. */
  723. public function getOrderDetail(array $data): array
  724. {
  725. $order = Order::where('id', $data['order_id'])->first();
  726. $orderads = OrderAd::where('order_ad.order_id', $data['order_id'])
  727. ->leftJoin('website', 'order_ad.website_id', 'website.id')
  728. ->leftJoin('ad_place', 'order_ad.pid', 'ad_place.id')
  729. ->select('order_ad.*', 'website.website_name', 'website.id', 'ad_place.name')
  730. ->selectSub('website.id', 'webid')
  731. ->selectSub('order_ad.id', 'oid')
  732. ->selectSub('order_ad.name', 'adname')
  733. ->selectSub('ad_place.name', 'apname')
  734. ->orderBy("website.id", "asc")
  735. ->limit($data['pageSize'])
  736. ->offset(($data['page'] - 1) * $data['pageSize'])
  737. ->get();
  738. if (empty($order)) {
  739. return Result::error("订单id错误");
  740. }
  741. $count = OrderAd::where('order_ad.order_id', $data['order_id'])->count();
  742. $result = [
  743. 'order' => $order,
  744. 'orderads' => $orderads,
  745. 'count' => $count,
  746. ];
  747. return Result::success($result);
  748. }
  749. /**
  750.  * 撤回订单
  751.  * @param
  752.  * @return void
  753. */
  754. public function cancelOrder(array $data): array
  755. {
  756. date_default_timezone_set('Asia/Shanghai');
  757. $time = time();
  758. $timestamp = date('YmdHis', $time);
  759. Db::beginTransaction();
  760. try {
  761. $order = Order::where('id', $data['order_id'])->where('status', 6)->where('sttime', '>=', $timestamp)->update(['status' => 3, 'ad_status' => '3']);
  762. $ads = OrderAd::where('order_id', $data['order_id'])->where('status', 6)->where('fromtime', '>=', $timestamp)->update(['status' => 3]);
  763. Db::commit();
  764. } catch (\Exception $e) {
  765. Db::rollBack();
  766. return Result::error($e->getMessage());
  767. }
  768. $result = [
  769. 'order' => $order,
  770. 'ads' => $ads,
  771. ];
  772. if ($order == 0) {
  773. return Result::error("此订单不可撤回");
  774. }
  775. return Result::success($result);
  776. }
  777. /**
  778.  * 删除订单
  779.  * @param
  780.  * @return void
  781. */
  782. public function delOrderAD(array $data): array
  783. {
  784. $data['status'] = [
  785. 0 => 2,
  786. 1 => 3,
  787. 2 => 5,
  788. 3 => 7,
  789. ];
  790. //1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束;
  791. date_default_timezone_set('Asia/Shanghai');
  792. $time = time();
  793. $timestamp = date('YmdHis', $time);
  794. $where = [
  795. ['id', '=', $data['id']],
  796. ];
  797. $time = [['edtime', '<=', $timestamp]];
  798. $order = Order::where($where)->where($time)->update(['user_del' => 1]);
  799. if (empty($order)) {
  800. $order = Order::where($where)->whereIn('status', $data['status'])->update(['user_del' => 1]);
  801. }
  802. if ($order == 0) {
  803. return Result::error("此订单不可删除");
  804. }
  805. return Result::success($order);
  806. }
  807. /**
  808.  * 创建购物车
  809.  * @param
  810.  * @return void
  811. */
  812. public function addShoppingCart(array $data): array
  813. {
  814. $shop = ShoppingCart::where('user_id', $data['user_id'])->get()->all();
  815. $time = time();
  816. $timestamp = date('YmdHis', $time);
  817. $randomNumber = mt_rand(100, 999);
  818. $shopping_id = $randomNumber . $timestamp; // 时间戳与随机数拼接
  819. Db::beginTransaction();
  820. try {
  821. if (!empty($shop)) {
  822. $del_shop = ShoppingCart::where('user_id', $data['user_id'])->delete();
  823. if (empty($del_shop)) {
  824. return Result::error("删除失败");
  825. }
  826. }
  827. $shop = [
  828. 'user_id' => $data['user_id'],
  829. 'shopping_id' => $shopping_id,
  830. 'created_at' => date('Y-m-d H:i:s', time()),
  831. 'updated_at' => date('Y-m-d H:i:s', time()),
  832. ];
  833. $result = ShoppingCart::insert($shop);
  834. Db::commit();
  835. } catch (\Exception $e) {
  836. Db::rollBack();
  837. return Result::error($e->getMessage());
  838. }
  839. if (empty($result)) {
  840. return Result::error("创建失败");
  841. } else {
  842. return Result::success($shopping_id);
  843. }
  844. }
  845. /**
  846.  * 获取购物车中的广告位
  847.  * @param
  848.  * @return void
  849. */
  850. public function getShoppingCartAD(array $data): array
  851. {
  852. $shopcart = ShoppingCart::where('shopping_id', $data['shopping_id'])
  853. ->where('user_id', $data['user_id'])
  854. ->orderBy('pid')
  855. ->pluck('pid')
  856. ->toArray();
  857. $result['pid'] = $shopcart;
  858. if (empty($result)) {
  859. return Result::error("购物车id错误");
  860. } else {
  861. return Result::success($result);
  862. }
  863. }
  864. /**
  865.  * 添加购物车中的广告位
  866.  * @param
  867.  * @return void
  868. */
  869. public function addShoppingCartAD(array $data): array
  870. {
  871. $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->first();
  872. if (empty($shop)) {
  873. return Result::error("购物车id错误");
  874. } else {
  875. $ad = AdPlace::where('ad_place.id', $data['pid'])
  876. ->leftJoin('website', 'ad_place.website_id', 'website.id')
  877. ->select('ad_place.id as pid', 'website.id as website_id')
  878. ->first();
  879. if (empty($ad)) {
  880. return Result::error("广告位id错误");
  881. }
  882. if (empty($shop['pid']) && empty($shop['website_id'])) {
  883. $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id', $data['user_id'])->update(['pid' => $ad['pid'], 'website_id' => $ad['website_id']]);
  884. } else {
  885. if ($data['pid'] == $shop['pid']) {
  886. return Result::error("购物车中已存在该广告位");
  887. }
  888. $shop_ad = [
  889. 'pid' => $ad['pid'],
  890. 'website_id' => $ad['website_id'],
  891. 'shopping_id' => $shop['shopping_id'],
  892. 'user_id' => $data['user_id'],
  893. ];
  894. $result = ShoppingCart::insertGetId($shop_ad);
  895. }
  896. }
  897. if (empty($result)) {
  898. return Result::error("添加失败");
  899. } else {
  900. return Result::success($result);
  901. }
  902. }
  903. /**
  904.  * 删除购物车中的广告位
  905.  * @param
  906.  * @return void
  907. */
  908. public function delShoppingCartAD(array $data): array
  909. {
  910. $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->where('pid', $data['pid'])->first();
  911. if (empty($shop)) {
  912. return Result::error("不存在此条记录!(参数错误)");
  913. } else {
  914. $count = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->count();
  915. if ($count == 1) {
  916. $result = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->update(['pid' => null, 'website_id' => null]);
  917. } else {
  918. $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id', $data['user_id'])->where('pid', $data['pid'])->delete();
  919. }
  920. }
  921. if (empty($result)) {
  922. return Result::error("删除失败");
  923. } else {
  924. return Result::success($result);
  925. }
  926. }
  927. /**
  928.  * 获取广告位的尺寸
  929.  *
  930.  *
  931. */
  932. public function getAdSize(): array
  933. {
  934. $result = AdSize::get()->all();
  935. if (empty($result)) {
  936. return Result::error("暂无数据");
  937. }
  938. return Result::success($result);
  939. }
  940. }