OrderService.php 38 KB

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