OrderService.php 37 KB

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