OrderService.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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 => '7',
  427. // 2 => '6',
  428. // ];
  429. //订单状态:1:通过;2:驳回;3:撤回;5:过期;6:待审核;7:结束
  430. // 筛选已上架的广告有时间冲突的广告位
  431. $ads = Ad::where('fromtime', '<=', $start)->where('totime', '>=', $end)->select('pid')->get()->all();
  432. $orderads = OrderAd::where('fromtime', '<=', $start)->where('totime', '>=', $end)->where('status', 1)->select('pid')->get()->all();
  433. $ads = array_column($ads, 'pid');
  434. $orderads = array_column($orderads, 'pid');
  435. $pids = array_merge($ads, $orderads);
  436. var_dump($pids, '-----------1-------------');
  437. //取出pid
  438. $ad_pids = array_unique($pids);
  439. var_dump($ad_pids, '----------3------------');
  440. $placeids = AdPlace::whereNotIn('id', $ad_pids)->where($where)->select('id')->get()->all();
  441. $ad_pids = array_column($placeids, 'id');
  442. $rep = AdPlace::where($where)
  443. ->whereIn('ad_place.id', $ad_pids)
  444. ->where('ad_place.website_id', $data['website_id'])
  445. ->leftJoin('website', 'ad_place.website_id', 'website.id')
  446. ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
  447. ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
  448. ->selectSub('website.id', 'webid')
  449. ->selectSub('ad_place.id', 'pid')
  450. ->selectSub('ad_size.width', 'size_width')
  451. ->selectSub('ad_size.height', 'size_height')
  452. ->orderBy("website.id", "asc")
  453. ->limit($data['pageSize'])
  454. ->offset(($data['page'] - 1) * $data['pageSize'])
  455. ->get();
  456. $count = AdPlace::where($where)->where('ad_place.website_id', $data['website_id'])->whereIn('ad_place.id', $ad_pids)->count();
  457. $startTime = strtotime($data['starttime']);
  458. $endTime = strtotime($data['endtime']);
  459. $time = ($endTime - $startTime) / (24 * 60 * 60);
  460. $roundedValue = round($time, 2);
  461. $days = number_format($roundedValue, 2, '.', '');
  462. $result = [
  463. 'rows' => $rep->toArray(),
  464. 'count' => $count,
  465. 'days' => $days,
  466. ];
  467. return Result::success($result);
  468. // //ad_size_id 必选改可选
  469. // //单选
  470. // if (isset($data['ad_size_id']) && is_string($data['ad_size_id'])) {
  471. // $where = [
  472. // 'ad_place.ad_size_id' => $data['ad_size_id'],
  473. // ];
  474. // }
  475. // //如果有website_id
  476. // if (!isset($data['ad_size_id'])) {
  477. // // $adsiteids = AdPlace::where('website_id', $data['website_id'])->select('id')->get()->all();
  478. // // $ad_pids = array_column($adsiteids, 'id');
  479. // // // $where = [
  480. // // // 'ad_place.ad_size_id' => $data['ad_size_id'],
  481. // // // ];
  482. // // var_dump($ad_pids, 'p--------------3-------');
  483. // // // $where[] = ['ad_place.id', 'in', $ad_pids];
  484. // // $where = [
  485. // // 'ad_place.website_id' => $data['website_id'],
  486. // // ];
  487. // }
  488. // //数组
  489. // $start = Carbon::parse($data['starttime']);
  490. // $end = Carbon::parse($data['endtime']);
  491. // $status = [
  492. // 0 => '1',
  493. // 1 => '4',
  494. // 2 => '6',
  495. // ];
  496. // //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  497. // $ads = Ad::where('fromtime', '<=', $start)->where('totime', '>=', $end)->select('pid')->get()->all();
  498. // $orderads = OrderAd::where('fromtime', '<=', $start)->where('totime', '>=', $end)->whereIn('status', $status)->select('pid')->get()->all();
  499. // $pids = array_merge($ads, $orderads);
  500. // $ad_pids = array_unique($pids);
  501. // //$ad_pids=所有与用户提交的时间有冲突的广告位pid
  502. // if (!empty($ad_pids)) {
  503. // $pid = [];
  504. // foreach ($ad_pids as $val) {
  505. // array_push($pid, $val['pid']);
  506. // }
  507. // //去掉时间冲突并且符合图片尺寸的广告位
  508. // $placeids = AdPlace::whereNotIn('id', $pid)->where($where)->select('id', 'website_id')->get()->all();
  509. // if (empty($placeids)) {
  510. // return Result::error('暂无数据!');
  511. // }
  512. // if (!isset($data['website_id'])) {
  513. // $websiteIds = array_column($placeids, 'website_id');
  514. // $website_id = array_unique($websiteIds);
  515. // $rep = Website::whereIn('id', $website_id)->get();
  516. // $count = Website::whereIn('id', $website_id)->count();
  517. // //若不存在网站id参数直接返回符合条件的广告位相关联的网站名称
  518. // } else {
  519. // $place_id = [];
  520. // foreach ($placeids as $val) {
  521. // array_push($place_id, $val['id']);
  522. // }
  523. // $rep = AdPlace::where($where)
  524. // ->whereIn('ad_place.id', $place_id)
  525. // ->where('ad_place.website_id', $data['website_id'])
  526. // ->leftJoin('website', 'ad_place.website_id', 'website.id')
  527. // ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
  528. // ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
  529. // ->selectSub('website.id', 'webid')
  530. // ->selectSub('ad_place.id', 'pid')
  531. // ->selectSub('ad_size.width', 'size_width')
  532. // ->selectSub('ad_size.height', 'size_height')
  533. // ->orderBy("website.id", "asc")
  534. // ->limit($data['pageSize'])
  535. // ->offset(($data['page'] - 1) * $data['pageSize'])
  536. // ->get();
  537. // $count = AdPlace::where($where)->where('ad_place.website_id', $data['website_id'])->count();
  538. // //若存在网站id,关联查询是需要添加website_id条件查询
  539. // }
  540. // } else {
  541. // //若不存在有时间冲突的广告位则所有符合图片尺寸的广告位皆可以使用,只需要判断是否存在网站id参数即可
  542. // if (isset($data['website_id'])) {
  543. // $rep = AdPlace::where($where)
  544. // ->where('ad_place.website_id', $data['website_id'])
  545. // ->leftJoin('website', 'ad_place.website_id', 'website.id')
  546. // ->leftJoin('ad_size', 'ad_place.ad_size_id', 'ad_size.id')
  547. // ->select('ad_place.*', 'website.website_name', 'website.id', 'ad_size.*')
  548. // ->selectSub('website.id', 'webid')
  549. // ->selectSub('ad_place.id', 'pid')
  550. // ->selectSub('ad_size.width', 'size_width')
  551. // ->selectSub('ad_size.height', 'size_height')
  552. // ->orderBy("website.id", "asc")
  553. // ->limit($data['pageSize'])
  554. // ->offset(($data['page'] - 1) * $data['pageSize'])
  555. // ->get();
  556. // $count = AdPlace::where($where)->where('ad_place.website_id', $data['website_id'])->count();
  557. // } else {
  558. // $place_all = AdPlace::where($where)->select('website_id')->get()->all();
  559. // $place_allads = [];
  560. // foreach ($place_all as $v) {
  561. // array_push($place_allads, $v['website_id']);
  562. // }
  563. // $rep = Website::whereIn('id', $place_allads)->get();
  564. // $count = Website::whereIn('id', $place_allads)->count();
  565. // }
  566. // }
  567. // if (empty($rep)) {
  568. // return Result::error("暂无符合您条件的广告位!");
  569. // }
  570. // $startTime = strtotime($data['starttime']);
  571. // $endTime = strtotime($data['endtime']);
  572. // $time = ($endTime - $startTime) / (24 * 60 * 60);
  573. // $roundedValue = round($time, 2);
  574. // $days = number_format($roundedValue, 2, '.', '');
  575. // $result = [
  576. // 'rows' => $rep->toArray(),
  577. // 'count' => $count,
  578. // 'days' => $days,
  579. // ];
  580. // return Result::success($result);
  581. }
  582. /**
  583.  * 获取广告金额
  584.  * @param
  585.  * @return void
  586. */
  587. public function getPrice(array $data): array
  588. {
  589. // $data['pid'] = [15,16];
  590. $startTime = strtotime($data['starttime']);
  591. $endTime = strtotime($data['endtime']);
  592. $days = ($endTime - $startTime) / (24 * 60 * 60); //计算共计多少天
  593. $price = 0;
  594. if (isset($data['pid'])) {
  595. $ad_price = AdPlace::whereIn('id', $data['pid'])->select('id', 'price')->get();
  596. foreach ($ad_price as $v) {
  597. $price += number_format($v['price'] * $days, 2, '.', '');
  598. }
  599. } else {
  600. $price = 0;
  601. }
  602. // 确保 $price带有两位小数位数
  603. $price = number_format((float) $price, 2, '.', '');
  604. return Result::success($price);
  605. }
  606. /**
  607.  * 添加广告订单
  608.  * @param
  609.  * @return void
  610. */
  611. public function addAD(array $data): array
  612. {
  613. date_default_timezone_set('Asia/Shanghai');
  614. $time = time();
  615. $startTime = strtotime($data['starttime']);
  616. $endTime = strtotime($data['endtime']);
  617. $days = ($endTime - $startTime) / (24 * 60 * 60); //计算共计多少天
  618. $timestamp = date('YmdHis', $time);
  619. $catetime = date('Y-m-d H:i:s', $time);
  620. $randomNumber = mt_rand(1000, 9999);
  621. $ordernum = $randomNumber . $timestamp; // 时间戳与随机数拼接
  622. $order_size = AdSize::where('id', $data['ad_size_id'])->first();
  623. // var_dump(($time));
  624. Db::beginTransaction();
  625. try {
  626. $order = [
  627. 'order_num' => $ordernum,
  628. 'name' => $data['name'],
  629. 'sttime' => $data['starttime'],
  630. 'edtime' => $data['endtime'],
  631. 'user_id' => $data['user_id'],
  632. 'cttime' => $catetime,
  633. 'width' => $order_size['width'],
  634. 'height' => $order_size['height'],
  635. 'days' => $days,
  636. 'price' => $data['price'],
  637. 'created_at' => date('Y-m-d H:i:s', time()),
  638. 'updated_at' => date('Y-m-d H:i:s', time()),
  639. ];
  640. //添加订单
  641. $orderid = Order::insertGetId($order);
  642. $adplace = $data['pid'];
  643. if (is_array($data['pid'])) {
  644. $adplace = AdPlace::whereIn('id', $data['pid'])->select('website_id', 'id')->get();
  645. $order_ad = [];
  646. foreach ($adplace as $key => $ads) {
  647. $order_ad[$key] = [
  648. 'order_id' => $orderid,
  649. 'order_num' => $ordernum,
  650. 'name' => $data['name'],
  651. 'fromtime' => $data['starttime'],
  652. 'totime' => $data['endtime'],
  653. 'image_src' => $data['imgsrc'],
  654. 'image_url' => $data['imgurl'],
  655. 'pid' => $ads['id'],
  656. 'website_id' => $ads['website_id'],
  657. ];
  658. }
  659. }
  660. $orderad_id = OrderAd::insert($order_ad);
  661. Db::commit();
  662. } catch (\Exception $e) {
  663. Db::rollBack();
  664. return Result::error($e->getMessage());
  665. }
  666. // $log = AdLog::insert($log);
  667. $result = [
  668. 'order_id' => $orderid,
  669. 'orderad_id' => $orderad_id,
  670. 'name' => $data['name'],
  671. 'ordernum' => $ordernum,
  672. ];
  673. return Result::success($result);
  674. }
  675. /**
  676.  * 获取订单列表
  677.  * @param
  678.  * @return void
  679. */
  680. public function getOrderList(array $data): array
  681. {
  682. $where = [
  683. 'user_id' => $data['user_id'],
  684. 'user_del' => 2,
  685. ];
  686. if (isset($data['status'])) {
  687. $where['status'] = $data['status'];
  688. }
  689. $orders = Order::where($where)
  690. ->limit($data['pageSize'])
  691. ->offset(($data['page'] - 1) * $data['pageSize'])
  692. ->orderBy("updated_at", "desc")
  693. ->get()
  694. ->all();
  695. //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  696. $count['all'] = Order::where($where)->count();
  697. $count['pass'] = Order::where($where)->where('status', 1)->count();
  698. $count['return'] = Order::where($where)->where('status', 2)->count();
  699. $count['recall'] = Order::where($where)->where('status', 3)->count();
  700. $count['update'] = Order::where($where)->where('status', 4)->count();
  701. $count['fail'] = Order::where($where)->where('status', 5)->count();
  702. $count['waite'] = Order::where($where)->where('status', 6)->count();
  703. $count['over'] = Order::where($where)->where('status', 7)->count();
  704. foreach ($orders as $key => $val) {
  705. $adsnum = OrderAd::where('order_id', $val['id'])->count();
  706. $rep[$key] = $val;
  707. $rep[$key]['num'] = $adsnum;
  708. }
  709. if (empty($rep)) {
  710. return Result::error("您暂时还没有下单");
  711. } else {
  712. $data = [
  713. 'rows' => $rep,
  714. 'count' => $count,
  715. ];
  716. }
  717. return Result::success($data);
  718. }
  719. /**
  720.  * 获取订单详情
  721.  * @param
  722.  * @return void
  723. */
  724. public function getOrderDetail(array $data): array
  725. {
  726. $order = Order::where('id', $data['order_id'])->first();
  727. $orderads = OrderAd::where('order_ad.order_id', $data['order_id'])
  728. ->leftJoin('website', 'order_ad.website_id', 'website.id')
  729. ->leftJoin('ad_place', 'order_ad.pid', 'ad_place.id')
  730. ->select('order_ad.*', 'website.website_name', 'website.id', 'ad_place.name')
  731. ->selectSub('website.id', 'webid')
  732. ->selectSub('order_ad.id', 'oid')
  733. ->selectSub('order_ad.name', 'adname')
  734. ->selectSub('ad_place.name', 'apname')
  735. ->orderBy("website.id", "asc")
  736. ->limit($data['pageSize'])
  737. ->offset(($data['page'] - 1) * $data['pageSize'])
  738. ->get();
  739. if (empty($order)) {
  740. return Result::error("订单id错误");
  741. }
  742. $count = OrderAd::where('order_ad.order_id', $data['order_id'])->count();
  743. $result = [
  744. 'order' => $order,
  745. 'orderads' => $orderads,
  746. 'count' => $count,
  747. ];
  748. return Result::success($result);
  749. }
  750. /**
  751.  * 撤回订单
  752.  * @param
  753.  * @return void
  754. */
  755. public function cancelOrder(array $data): array
  756. {
  757. date_default_timezone_set('Asia/Shanghai');
  758. $time = time();
  759. $timestamp = date('YmdHis', $time);
  760. Db::beginTransaction();
  761. try {
  762. $order = Order::where('id', $data['order_id'])->where('status', 6)->where('sttime', '>=', $timestamp)->update(['status' => 3, 'ad_status' => '3']);
  763. $ads = OrderAd::where('order_id', $data['order_id'])->where('status', 6)->where('fromtime', '>=', $timestamp)->update(['status' => 3]);
  764. Db::commit();
  765. } catch (\Exception $e) {
  766. Db::rollBack();
  767. return Result::error($e->getMessage());
  768. }
  769. $result = [
  770. 'order' => $order,
  771. 'ads' => $ads,
  772. ];
  773. if ($order == 0) {
  774. return Result::error("此订单不可撤回");
  775. }
  776. return Result::success($result);
  777. }
  778. /**
  779.  * 删除订单
  780.  * @param
  781.  * @return void
  782. */
  783. public function delOrderAD(array $data): array
  784. {
  785. $data['status'] = [
  786. 0 => 2,
  787. 1 => 3,
  788. 2 => 5,
  789. 3 => 7,
  790. ];
  791. //1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束;
  792. date_default_timezone_set('Asia/Shanghai');
  793. $time = time();
  794. $timestamp = date('YmdHis', $time);
  795. $where = [
  796. ['id', '=', $data['id']],
  797. ];
  798. $time = [['edtime', '<=', $timestamp]];
  799. $order = Order::where($where)->where($time)->update(['user_del' => 1]);
  800. if (empty($order)) {
  801. $order = Order::where($where)->whereIn('status', $data['status'])->update(['user_del' => 1]);
  802. }
  803. if ($order == 0) {
  804. return Result::error("此订单不可删除");
  805. }
  806. return Result::success($order);
  807. }
  808. /**
  809.  * 创建购物车
  810.  * @param
  811.  * @return void
  812. */
  813. public function addShoppingCart(array $data): array
  814. {
  815. $shop = ShoppingCart::where('user_id', $data['user_id'])->get()->all();
  816. $time = time();
  817. $timestamp = date('YmdHis', $time);
  818. $randomNumber = mt_rand(100, 999);
  819. $shopping_id = $randomNumber . $timestamp; // 时间戳与随机数拼接
  820. Db::beginTransaction();
  821. try {
  822. if (!empty($shop)) {
  823. $del_shop = ShoppingCart::where('user_id', $data['user_id'])->delete();
  824. if (empty($del_shop)) {
  825. return Result::error("删除失败");
  826. }
  827. }
  828. $shop = [
  829. 'user_id' => $data['user_id'],
  830. 'shopping_id' => $shopping_id,
  831. 'created_at' => date('Y-m-d H:i:s', time()),
  832. 'updated_at' => date('Y-m-d H:i:s', time()),
  833. ];
  834. $result = ShoppingCart::insert($shop);
  835. Db::commit();
  836. } catch (\Exception $e) {
  837. Db::rollBack();
  838. return Result::error($e->getMessage());
  839. }
  840. if (empty($result)) {
  841. return Result::error("创建失败");
  842. } else {
  843. return Result::success($shopping_id);
  844. }
  845. }
  846. /**
  847.  * 获取购物车中的广告位
  848.  * @param
  849.  * @return void
  850. */
  851. public function getShoppingCartAD(array $data): array
  852. {
  853. $shopcart = ShoppingCart::where('shopping_id', $data['shopping_id'])
  854. ->where('user_id', $data['user_id'])
  855. ->orderBy('pid')
  856. ->pluck('pid')
  857. ->toArray();
  858. $result['pid'] = $shopcart;
  859. if (empty($result)) {
  860. return Result::error("购物车id错误");
  861. } else {
  862. return Result::success($result);
  863. }
  864. }
  865. /**
  866.  * 添加购物车中的广告位
  867.  * @param
  868.  * @return void
  869. */
  870. public function addShoppingCartAD(array $data): array
  871. {
  872. $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->first();
  873. if (empty($shop)) {
  874. return Result::error("购物车id错误");
  875. } else {
  876. $ad = AdPlace::where('ad_place.id', $data['pid'])
  877. ->leftJoin('website', 'ad_place.website_id', 'website.id')
  878. ->select('ad_place.id as pid', 'website.id as website_id')
  879. ->first();
  880. if (empty($ad)) {
  881. return Result::error("广告位id错误");
  882. }
  883. if (empty($shop['pid']) && empty($shop['website_id'])) {
  884. $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id', $data['user_id'])->update(['pid' => $ad['pid'], 'website_id' => $ad['website_id']]);
  885. } else {
  886. if ($data['pid'] == $shop['pid']) {
  887. return Result::error("购物车中已存在该广告位");
  888. }
  889. $shop_ad = [
  890. 'pid' => $ad['pid'],
  891. 'website_id' => $ad['website_id'],
  892. 'shopping_id' => $shop['shopping_id'],
  893. 'user_id' => $data['user_id'],
  894. ];
  895. $result = ShoppingCart::insertGetId($shop_ad);
  896. }
  897. }
  898. if (empty($result)) {
  899. return Result::error("添加失败");
  900. } else {
  901. return Result::success($result);
  902. }
  903. }
  904. /**
  905.  * 删除购物车中的广告位
  906.  * @param
  907.  * @return void
  908. */
  909. public function delShoppingCartAD(array $data): array
  910. {
  911. $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->where('pid', $data['pid'])->first();
  912. if (empty($shop)) {
  913. return Result::error("不存在此条记录!(参数错误)");
  914. } else {
  915. $count = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->count();
  916. if ($count == 1) {
  917. $result = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->update(['pid' => null, 'website_id' => null]);
  918. } else {
  919. $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id', $data['user_id'])->where('pid', $data['pid'])->delete();
  920. }
  921. }
  922. if (empty($result)) {
  923. return Result::error("删除失败");
  924. } else {
  925. return Result::success($result);
  926. }
  927. }
  928. /**
  929.  * 获取广告位的尺寸
  930.  *
  931.  *
  932. */
  933. public function getAdSize(): array
  934. {
  935. $result = AdSize::get()->all();
  936. if (empty($result)) {
  937. return Result::error("暂无数据");
  938. }
  939. return Result::success($result);
  940. }
  941. }