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. 'website_id' => $data['website_id'],
  642. 'created_at' => date('Y-m-d H:i:s', time()),
  643. 'updated_at' => date('Y-m-d H:i:s', time()),
  644. ];
  645. //添加订单
  646. $orderid = Order::insertGetId($order);
  647. $adplace = $data['pid'];
  648. if (is_array($data['pid'])) {
  649. $adplace = AdPlace::whereIn('id', $data['pid'])->select('website_id', 'id')->get();
  650. $order_ad = [];
  651. foreach ($adplace as $key => $ads) {
  652. $order_ad[$key] = [
  653. 'order_id' => $orderid,
  654. 'order_num' => $ordernum,
  655. 'name' => $data['name'],
  656. 'fromtime' => $data['starttime'],
  657. 'totime' => $data['endtime'],
  658. 'image_src' => $data['imgsrc'],
  659. 'image_url' => $data['imgurl'],
  660. 'pid' => $ads['id'],
  661. 'website_id' => $ads['website_id'],
  662. ];
  663. }
  664. }
  665. $orderad_id = OrderAd::insert($order_ad);
  666. Db::commit();
  667. } catch (\Exception $e) {
  668. Db::rollBack();
  669. return Result::error($e->getMessage());
  670. }
  671. // $log = AdLog::insert($log);
  672. $result = [
  673. 'order_id' => $orderid,
  674. 'orderad_id' => $orderad_id,
  675. 'name' => $data['name'],
  676. 'ordernum' => $ordernum,
  677. ];
  678. return Result::success($result);
  679. }
  680. /**
  681.  * 获取订单列表
  682.  * @param
  683.  * @return void
  684. */
  685. public function getOrderList(array $data): array
  686. {
  687. $where = [
  688. 'user_id' => $data['user_id'],
  689. 'user_del' => 2,
  690. ];
  691. if (isset($data['status'])) {
  692. $where['status'] = $data['status'];
  693. }
  694. $orders = Order::where($where)
  695. ->limit($data['pageSize'])
  696. ->offset(($data['page'] - 1) * $data['pageSize'])
  697. ->orderBy("updated_at", "desc")
  698. ->get()
  699. ->all();
  700. //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  701. $count['all'] = Order::where($where)->count();
  702. $count['pass'] = Order::where($where)->where('status', 1)->count();
  703. $count['return'] = Order::where($where)->where('status', 2)->count();
  704. $count['recall'] = Order::where($where)->where('status', 3)->count();
  705. $count['update'] = Order::where($where)->where('status', 4)->count();
  706. $count['fail'] = Order::where($where)->where('status', 5)->count();
  707. $count['waite'] = Order::where($where)->where('status', 6)->count();
  708. $count['over'] = Order::where($where)->where('status', 7)->count();
  709. foreach ($orders as $key => $val) {
  710. $adsnum = OrderAd::where('order_id', $val['id'])->count();
  711. $rep[$key] = $val;
  712. $rep[$key]['num'] = $adsnum;
  713. }
  714. if (empty($rep)) {
  715. return Result::error("您暂时还没有下单");
  716. } else {
  717. $data = [
  718. 'rows' => $rep,
  719. 'count' => $count,
  720. ];
  721. }
  722. return Result::success($data);
  723. }
  724. /**
  725.  * 获取订单详情
  726.  * @param
  727.  * @return void
  728. */
  729. public function getOrderDetail(array $data): array
  730. {
  731. $order = Order::where('id', $data['order_id'])->first();
  732. $orderads = OrderAd::where('order_ad.order_id', $data['order_id'])
  733. ->leftJoin('website', 'order_ad.website_id', 'website.id')
  734. ->leftJoin('ad_place', 'order_ad.pid', 'ad_place.id')
  735. ->select('order_ad.*', 'website.website_name', 'website.id', 'ad_place.name')
  736. ->selectSub('website.id', 'webid')
  737. ->selectSub('order_ad.id', 'oid')
  738. ->selectSub('order_ad.name', 'adname')
  739. ->selectSub('ad_place.name', 'apname')
  740. ->orderBy("website.id", "asc")
  741. ->limit($data['pageSize'])
  742. ->offset(($data['page'] - 1) * $data['pageSize'])
  743. ->get();
  744. if (empty($order)) {
  745. return Result::error("订单id错误");
  746. }
  747. $count = OrderAd::where('order_ad.order_id', $data['order_id'])->count();
  748. $result = [
  749. 'order' => $order,
  750. 'orderads' => $orderads,
  751. 'count' => $count,
  752. ];
  753. return Result::success($result);
  754. }
  755. /**
  756.  * 撤回订单
  757.  * @param
  758.  * @return void
  759. */
  760. public function cancelOrder(array $data): array
  761. {
  762. date_default_timezone_set('Asia/Shanghai');
  763. $time = time();
  764. $timestamp = date('YmdHis', $time);
  765. Db::beginTransaction();
  766. try {
  767. $order = Order::where('id', $data['order_id'])->where('status', 6)->where('sttime', '>=', $timestamp)->update(['status' => 3, 'ad_status' => '3']);
  768. $ads = OrderAd::where('order_id', $data['order_id'])->where('status', 6)->where('fromtime', '>=', $timestamp)->update(['status' => 3]);
  769. Db::commit();
  770. } catch (\Exception $e) {
  771. Db::rollBack();
  772. return Result::error($e->getMessage());
  773. }
  774. $result = [
  775. 'order' => $order,
  776. 'ads' => $ads,
  777. ];
  778. if ($order == 0) {
  779. return Result::error("此订单不可撤回");
  780. }
  781. return Result::success($result);
  782. }
  783. /**
  784.  * 删除订单
  785.  * @param
  786.  * @return void
  787. */
  788. public function delOrderAD(array $data): array
  789. {
  790. $data['status'] = [
  791. 0 => 2,
  792. 1 => 3,
  793. 2 => 5,
  794. 3 => 7,
  795. ];
  796. //1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束;
  797. date_default_timezone_set('Asia/Shanghai');
  798. $time = time();
  799. $timestamp = date('YmdHis', $time);
  800. $where = [
  801. ['id', '=', $data['id']],
  802. ];
  803. $time = [['edtime', '<=', $timestamp]];
  804. $order = Order::where($where)->where($time)->update(['user_del' => 1]);
  805. if (empty($order)) {
  806. $order = Order::where($where)->whereIn('status', $data['status'])->update(['user_del' => 1]);
  807. }
  808. if ($order == 0) {
  809. return Result::error("此订单不可删除");
  810. }
  811. return Result::success($order);
  812. }
  813. /**
  814.  * 创建购物车
  815.  * @param
  816.  * @return void
  817. */
  818. public function addShoppingCart(array $data): array
  819. {
  820. $shop = ShoppingCart::where('user_id', $data['user_id'])->get()->all();
  821. $time = time();
  822. $timestamp = date('YmdHis', $time);
  823. $randomNumber = mt_rand(100, 999);
  824. $shopping_id = $randomNumber . $timestamp; // 时间戳与随机数拼接
  825. Db::beginTransaction();
  826. try {
  827. if (!empty($shop)) {
  828. $del_shop = ShoppingCart::where('user_id', $data['user_id'])->delete();
  829. if (empty($del_shop)) {
  830. Db::rollBack();
  831. return Result::error("删除购物车失败");
  832. // throw new \Exception("删除购物车失败");
  833. }
  834. }
  835. $shop = [
  836. 'user_id' => $data['user_id'],
  837. 'shopping_id' => $shopping_id,
  838. 'created_at' => date('Y-m-d H:i:s', time()),
  839. 'updated_at' => date('Y-m-d H:i:s', time()),
  840. ];
  841. $result = ShoppingCart::insert($shop);
  842. Db::commit();
  843. } catch (\Exception $e) {
  844. Db::rollBack();
  845. return Result::error($e->getMessage());
  846. }
  847. if (empty($result)) {
  848. return Result::error("创建失败");
  849. } else {
  850. return Result::success($shopping_id);
  851. }
  852. }
  853. /**
  854.  * 获取购物车中的广告位
  855.  * @param
  856.  * @return void
  857. */
  858. public function getShoppingCartAD(array $data): array
  859. {
  860. $shopcart = ShoppingCart::where('shopping_id', $data['shopping_id'])
  861. ->where('user_id', $data['user_id'])
  862. ->orderBy('pid')
  863. ->pluck('pid')
  864. ->toArray();
  865. $result['pid'] = $shopcart;
  866. if (empty($result)) {
  867. return Result::error("购物车id错误");
  868. } else {
  869. return Result::success($result);
  870. }
  871. }
  872. /**
  873.  * 添加购物车中的广告位
  874.  * @param
  875.  * @return void
  876. */
  877. public function addShoppingCartAD(array $data): array
  878. {
  879. $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->first();
  880. if (empty($shop)) {
  881. return Result::error("购物车id错误");
  882. } else {
  883. $ad = AdPlace::where('ad_place.id', $data['pid'])
  884. ->leftJoin('website', 'ad_place.website_id', 'website.id')
  885. ->select('ad_place.id as pid', 'website.id as website_id')
  886. ->first();
  887. if (empty($ad)) {
  888. return Result::error("广告位id错误");
  889. }
  890. if (empty($shop['pid']) && empty($shop['website_id'])) {
  891. $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id', $data['user_id'])->update(['pid' => $ad['pid'], 'website_id' => $ad['website_id']]);
  892. } else {
  893. if ($data['pid'] == $shop['pid']) {
  894. return Result::error("购物车中已存在该广告位");
  895. }
  896. $shop_ad = [
  897. 'pid' => $ad['pid'],
  898. 'website_id' => $ad['website_id'],
  899. 'shopping_id' => $shop['shopping_id'],
  900. 'user_id' => $data['user_id'],
  901. ];
  902. $result = ShoppingCart::insertGetId($shop_ad);
  903. }
  904. }
  905. if (empty($result)) {
  906. return Result::error("添加失败");
  907. } else {
  908. return Result::success($result);
  909. }
  910. }
  911. /**
  912.  * 删除购物车中的广告位
  913.  * @param
  914.  * @return void
  915. */
  916. public function delShoppingCartAD(array $data): array
  917. {
  918. $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->where('pid', $data['pid'])->first();
  919. if (empty($shop)) {
  920. return Result::error("不存在此条记录!(参数错误)");
  921. } else {
  922. $count = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->count();
  923. if ($count == 1) {
  924. $result = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->update(['pid' => null, 'website_id' => null]);
  925. } else {
  926. $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id', $data['user_id'])->where('pid', $data['pid'])->delete();
  927. }
  928. }
  929. if (empty($result)) {
  930. return Result::error("删除失败");
  931. } else {
  932. return Result::success($result);
  933. }
  934. }
  935. /**
  936.  * 获取广告位的尺寸
  937.  *
  938.  *
  939. */
  940. public function getAdSize(): array
  941. {
  942. $result = AdSize::get()->all();
  943. if (empty($result)) {
  944. return Result::error("暂无数据");
  945. }
  946. return Result::success($result);
  947. }
  948. }