OrderService.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Ad;
  4. use App\Model\AdPlace;
  5. use App\Model\Order;
  6. use App\Model\OrderAd;
  7. use App\Model\Website;
  8. use App\Tools\Result;
  9. use Carbon\Carbon;
  10. use Hyperf\DbConnection\Db;
  11. use Hyperf\RpcServer\Annotation\RpcService;
  12. #[RpcService(name: "OrderService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  13. class OrderService implements OrderServiceInterface
  14. {
  15. /**
  16.  * 查询没有广告的广告位
  17.  * @param
  18.  * @return void
  19. */
  20. public function getAD(array $data): array
  21. {
  22. $where = [
  23. 'ad_place.width' => $data['width'],
  24. 'ad_place.height' => $data['height'],
  25. ];
  26. $start = Carbon::parse($data['starttime']);
  27. $end = Carbon::parse($data['endtime']);
  28. $status = [
  29. 0 => '1',
  30. 1 => '4',
  31. 2 => '6',
  32. ];
  33. $ads = Ad::where('fromtime', '<=', $start)->where('totime', '>=', $end)->select('pid')->get()->all();
  34. $orderads = OrderAd::where('fromtime', '<=', $start)->where('totime', '>=', $end)->whereIn('status', $status)->select('pid')->get()->all();
  35. $pids = array_merge($ads, $orderads);
  36. $ad_pids = array_unique($pids);
  37. // $ad_pid = [1,2,3];
  38. // 所有与用户有时间冲突的广告位
  39. if (!empty($ad_pids)) {
  40. $pid = [];
  41. foreach ($ad_pids as $val) {
  42. array_push($pid, $val['pid']);
  43. }
  44. $placeids = AdPlace::whereNotIn('id', $pid)->where($where)->select('id')->get()->all();
  45. //所有去掉与用户时间冲突的广告并且符合图片尺寸的广告位
  46. // 符合用户条件的空广告位
  47. $place_id = [];
  48. foreach ($placeids as $val) {
  49. array_push($place_id, $val['id']);
  50. }
  51. $rep = AdPlace::whereIn('ad_place.id', $place_id)
  52. ->leftJoin('website', 'ad_place.website_id', 'website.id')
  53. ->select('ad_place.*', 'website.website_name', 'website.id')
  54. ->selectSub('website.id', 'webid')
  55. ->selectSub('ad_place.id', 'pid')
  56. ->orderBy("website.id", "asc")
  57. ->limit($data['pageSize'])
  58. ->offset(($data['page'] - 1) * $data['pageSize'])
  59. ->get();
  60. } else {
  61. $rep = AdPlace::where($where)
  62. ->leftJoin('website', 'ad_place.website_id', 'website.id')
  63. ->select('ad_place.*', 'website.website_name', 'website.id')
  64. ->selectSub('website.id', 'webid')
  65. ->selectSub('ad_place.id', 'pid')
  66. ->orderBy("website.id", "asc")
  67. ->limit($data['pageSize'])
  68. ->offset(($data['page'] - 1) * $data['pageSize'])
  69. ->get();
  70. }
  71. $startTime = strtotime($data['starttime']);
  72. $endTime = strtotime($data['endtime']);
  73. $time = ($endTime - $startTime) / (24 * 60 * 60);
  74. $roundedValue = round($time, 2);
  75. $days = number_format($roundedValue, 2, '.', '');
  76. $count = count($rep);
  77. $data = [
  78. 'rows' => $rep->toArray(),
  79. 'count' => $count,
  80. 'days' => $days,
  81. ];
  82. if (empty($data['rows'])) {
  83. return Result::error("暂时没有符合您条件的广告位");
  84. }
  85. return Result::success($data);
  86. }
  87. /**
  88. * 添加订单
  89. * @param
  90. * @return void
  91. */
  92. public function addOrder(array $data): array
  93. {
  94. $ads = Ad::whereIn($data['id'])
  95. ->leftJoin('ad_place', 'ad.pid', 'ad_place.id')
  96. ->leftJoin("article_data", "article.id", "article_data.article_id")
  97. ->select("ad_place.*", "ad.*")
  98. ->orderBy("ad.id", "desc")
  99. ->limit($data['pageSize'])
  100. ->offset(($data['page'] - 1) * $data['pageSize'])->get();
  101. $count = Ad::whereIn($data['id'])->count();
  102. $data = [
  103. 'rows' => $ads->toArray(),
  104. 'count' => $count,
  105. ];
  106. if (empty($rep)) {
  107. return Result::error("没有信息数据");
  108. }
  109. return Result::success($data);
  110. }
  111. /**
  112. * 获取订单列表
  113. * @param
  114. * @return void
  115. */
  116. public function getOrderListAdmin(array $data): array
  117. {
  118. // 获取分页参数,默认每页 10 条记录
  119. $page = isset($data['page']) ? (int) $data['page'] : 1;
  120. $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10;
  121. // 构建查询条件
  122. $where = [];
  123. if(!empty($data['status'])) {
  124. $where = [
  125. 'order.status' => $data['status'], // 明确指定 order 表的 status 列
  126. ];
  127. }
  128. // 添加订单号查询条件
  129. if (!empty($data['order_num'])) {
  130. $where['order.order_num'] = $data['order_num']; // 明确指定 order 表的 order_num 列
  131. }
  132. // 处理时间范围查询
  133. $start = $data['sttime'];
  134. $end = $data['edtime'];
  135. // 查询数据并分页
  136. $query = Order::where($where)
  137. ->when(!empty($start) && !empty($end), function ($q) use ($start, $end) {
  138. $q->whereBetween('order.fromtime', [$start, $end]); // 明确指定 order 表的 fromtime 列
  139. })
  140. ->when(!empty($start), function ($q) use ($start) {
  141. $q->where('order.fromtime', '>=', $start); // 明确指定 order 表的 fromtime 列
  142. })
  143. ->when(!empty($end), function ($q) use ($end) {
  144. $q->where('order.totime', '<=', $end); // 明确指定 order 表的 totime 列
  145. })
  146. ->leftJoin('user as admin_user', 'order.admin_user_id', '=', 'admin_user.id')
  147. ->leftJoin('user as user', 'order.user_id', '=', 'user.id')
  148. ->select(
  149. 'order.*',
  150. 'admin_user.user_name as admin_user_name',
  151. 'user.user_name as user_name'
  152. )
  153. ->orderBy('order.id');
  154. // 执行分页查询
  155. $result = $query->paginate($perPage, ['*'], 'page', $page);
  156. // 返回分页结果
  157. return Result::success([
  158. 'count' => $result->total(),
  159. 'current_page' => $result->currentPage(),
  160. 'last_page' => $result->lastPage(),
  161. 'pagesize' => $result->perPage(),
  162. 'rows' => $result->items(),
  163. ]);
  164. }
  165. /**
  166. * 获取订单详情
  167. * @param
  168. * @return void
  169. */
  170. public function getOrderDetailAdmin(array $data): array
  171. {
  172. $order = Order::where('order.id', $data['id'])
  173. ->leftJoin('user as admin_user', 'order.admin_user_id', '=', 'admin_user.id')
  174. ->leftJoin('user as user', 'order.user_id', '=', 'user.id')
  175. ->select(
  176. 'order.*',
  177. 'admin_user.user_name as admin_user_name',
  178. 'user.user_name as user_name'
  179. )->first();
  180. if (empty($order)) {
  181. return Result::error("没有信息数据");
  182. }
  183. $pid = $order['id'];
  184. $ad = OrderAd::where('order_id', $pid)->get();
  185. $order['ad'] = $ad;
  186. return Result::success($order);
  187. }
  188. /**
  189. * 修改订单价格
  190. * @param
  191. * @return void
  192. */
  193. public function editPriceOrderAdmin(array $data): array
  194. {
  195. $order = Order::where('id', $data['id'])->first();
  196. if (empty($order)) {
  197. return Result::error("没有信息数据");
  198. }
  199. $order->price = $data['price'];
  200. $order->save();
  201. return Result::success($order);
  202. }
  203. /**
  204. *拒绝订单
  205. * @param
  206. * @return void
  207. */
  208. public function rejectOrderAdmin(array $data): array
  209. {
  210. $order = Order::where('id', $data['id'])->first();
  211. if (empty($order)) {
  212. return Result::error("没有信息数据");
  213. }
  214. Db::beginTransaction();
  215. try {
  216. $order->status = 2; //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  217. $order->ad_status = 2;
  218. $order->reason = $data['reason'];
  219. $order->bhtime = date('Y-m-d H:i:s');
  220. $order->save();
  221. OrderAd::where('order_id', $data['id'])
  222. ->update(['status' => 2]);
  223. Db::commit();
  224. } catch (\Exception $e) {
  225. Db::rollBack();
  226. return Result::error("操作失败");
  227. }
  228. return Result::success($order);
  229. }
  230. /**
  231. * 结束订单
  232. * @param
  233. * @return void
  234. */
  235. public function endOrderAdmin(array $data): array
  236. {
  237. $order = Order::where('id', $data['id'])->first();
  238. if (empty($order)) {
  239. return Result::error("没有信息数据");
  240. }
  241. Db::beginTransaction();
  242. try {
  243. $order->status = 7;
  244. $order->jstime = date('Y-m-d H:i:s');
  245. $order->save();
  246. // 获取 order_ad 表中的记录
  247. $orderAds = OrderAd::where('order_id', $data['id'])
  248. ->update(['status' => 7]);
  249. // 在ad表中删除相同pid的数据
  250. Ad::where('order_id', $data['id'])->delete();
  251. Db::commit();
  252. } catch (\Exception $e) {
  253. Db::rollBack();
  254. return Result::error("操作失败" . $e->getMessage());
  255. }
  256. return Result::success($order);
  257. }
  258. /**
  259. * 删除订单
  260. * @param
  261. * @return void
  262. */
  263. public function delOrderAdmin(array $data): array
  264. {
  265. // 获取订单信息
  266. $order = Order::where('id', $data['id'])->first();
  267. if (empty($order)) {
  268. return Result::error("没有信息数据");
  269. }
  270. Db::beginTransaction();
  271. try {
  272. // 获取 order_ad 表中的记录
  273. OrderAd::where('order_id', $data['id'])->delete();
  274. // 删除 ad 表中的记录
  275. Ad::where('order_id', $data['id'])->delete();
  276. // 提交事务
  277. Db::commit();
  278. } catch (\Exception $e) {
  279. // 回滚事务
  280. Db::rollBack();
  281. // 返回错误信息
  282. return Result::error($e->getMessage());
  283. }
  284. // 返回成功信息
  285. return Result::success("删除成功");
  286. }
  287. /**
  288. * 审核订单
  289. * @param
  290. * @return void
  291. */
  292. public function applyOrderStatusAdmin(array $data): array
  293. {
  294. $order = Order::where('id', $data['id'])
  295. ->where('status', 6)
  296. ->first();
  297. if (empty($order)) {
  298. return Result::error("没有信息数据");
  299. }
  300. Db::beginTransaction();
  301. try {
  302. $order->status = 1;
  303. //判断是否在周期范围内
  304. $ad_status = 8; // 待生效
  305. if ($order->starttime < date('Y-m-d H:i:s') && date('Y-m-d H:i:s') < $order->endtime) {
  306. // 条件满足时的逻辑
  307. $ad_status = 1;
  308. }
  309. $order->ad_status = 1;
  310. $order->shtime = date('Y-m-d H:i:s');
  311. $order->save();
  312. // 批量更新 order_ad 表中的状态
  313. OrderAd::where('order_id', $data['id'])
  314. ->where('status', 6)
  315. ->update(['status' => 1]);
  316. //判断的当前时间是否在订单的开始时间之后,并且小于等于订单的结束时间
  317. if (time() >= strtotime($order->sttime) && time() < strtotime($order->edtime)) {
  318. $ad_status = 1; //审核生效
  319. } else {
  320. $ad_status = 2; //审核
  321. }
  322. $ads = [];
  323. $orderAds = OrderAd::where('order_id', $data['id'])->get();
  324. foreach ($orderAds as $orderAd) {
  325. $ads[] = [
  326. 'name' => $orderAd->name,
  327. 'pid' => $orderAd->pid,
  328. 'areaid' => $orderAd->areaid,
  329. 'amount' => $orderAd->amount,
  330. 'introduce' => $orderAd->introduce,
  331. 'hits' => $orderAd->hits,
  332. 'admin_user_id' => $orderAd->admin_user_id,
  333. 'fromtime' => $orderAd->fromtime,
  334. 'totime' => $orderAd->totime,
  335. 'text_name' => $orderAd->text_name,
  336. 'text_url' => $orderAd->text_url,
  337. 'text_title' => $orderAd->text_title,
  338. 'image_src' => $orderAd->image_src,
  339. 'image_url' => $orderAd->image_url,
  340. 'image_alt' => $orderAd->image_alt,
  341. 'video_src' => $orderAd->video_src,
  342. 'video_url' => $orderAd->video_url,
  343. 'video_auto' => $orderAd->video_auto,
  344. 'video_loop' => $orderAd->video_loop,
  345. 'status' => $ad_status,
  346. 'order_id' => $orderAd->order_id,
  347. ];
  348. }
  349. Ad::insert($ads);
  350. Db::commit();
  351. } catch (\Exception $e) {
  352. Db::rollBack();
  353. return Result::error($e->getMessage());
  354. }
  355. return Result::success($order);
  356. }
  357. /*
  358.  * 根据用户条件及网站搜索没有广告的广告位
  359.  * @param
  360.  * @return void
  361. */
  362. public function getWebsiteAd(array $data): array
  363. {
  364. $where = [
  365. 'ad_place.width' => $data['width'],
  366. 'ad_place.height' => $data['height'],
  367. ];
  368. $start = Carbon::parse($data['starttime']);
  369. $end = Carbon::parse($data['endtime']);
  370. $status = [
  371. 0 => '1',
  372. 1 => '4',
  373. 2 => '6',
  374. ];
  375. //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  376. $ads = Ad::where('fromtime', '<=', $start)->where('totime', '>=', $end)->select('pid')->get()->all();
  377. $orderads = OrderAd::where('fromtime', '<=', $start)->where('totime', '>=', $end)->whereIn('status', $status)->select('pid')->get()->all();
  378. $pids = array_merge($ads, $orderads);
  379. $ad_pids = array_unique($pids);
  380. //$ad_pids=所有与用户提交的时间有冲突的广告位pid
  381. if (!empty($ad_pids)) {
  382. $pid = [];
  383. foreach ($ad_pids as $val) {
  384. array_push($pid, $val['pid']);
  385. }
  386. $placeids = AdPlace::whereNotIn('id', $pid)->where($where)->select('id', 'website_id')->get()->all();
  387. //去掉时间冲突并且符合图片尺寸的广告位
  388. if (!isset($data['website_id'])) {
  389. $website_id = [];
  390. foreach ($placeids as $v) {
  391. array_push($website_id, $v['website_id']);
  392. }
  393. $rep = Website::whereIn('id', $website_id)->get();
  394. //若不存在网站id参数直接返回符合条件的广告位相关联的网站名称
  395. } else {
  396. $place_id = [];
  397. foreach ($placeids as $val) {
  398. array_push($place_id, $val['id']);
  399. }
  400. $rep = AdPlace::where($where)
  401. ->whereIn('ad_place.id', $place_id)
  402. ->where('ad_place.website_id', $data['website_id'])
  403. ->leftJoin('website', 'ad_place.website_id', 'website.id')
  404. ->select('ad_place.*', 'website.website_name', 'website.id')
  405. ->selectSub('website.id', 'webid')
  406. ->selectSub('ad_place.id', 'pid')
  407. ->orderBy("website.id", "asc")
  408. ->limit($data['pageSize'])
  409. ->offset(($data['page'] - 1) * $data['pageSize'])
  410. ->get();
  411. //若存在网站id,关联查询是需要添加website_id条件查询
  412. }
  413. } else {
  414. //若不存在有时间冲突的广告位则所有符合图片尺寸的广告位皆可以使用,只需要判断是否存在网站id参数即可
  415. if (isset($data['website_id'])) {
  416. $rep = AdPlace::where($where)
  417. ->where('ad_place.website_id', $data['website_id'])
  418. ->leftJoin('website', 'ad_place.website_id', 'website.id')
  419. ->select('ad_place.*', 'website.website_name', 'website.id')
  420. ->selectSub('website.id', 'webid')
  421. ->selectSub('ad_place.id', 'pid')
  422. ->orderBy("website.id", "asc")
  423. ->limit($data['pageSize'])
  424. ->offset(($data['page'] - 1) * $data['pageSize'])
  425. ->get();
  426. } else {
  427. $place_all = AdPlace::where($where)->select('website_id')->get()->all();
  428. $place_allads = [];
  429. foreach ($place_all as $v) {
  430. array_push($place_allads, $v['website_id']);
  431. }
  432. $rep = Website::whereIn('id', $place_allads)->get();
  433. }
  434. }
  435. if (empty($rep)) {
  436. return Result::error("暂时没有符合您条件的广告位");
  437. }
  438. $startTime = strtotime($data['starttime']);
  439. $endTime = strtotime($data['endtime']);
  440. $time = ($endTime - $startTime) / (24 * 60 * 60);
  441. $roundedValue = round($time, 2);
  442. $days = number_format($roundedValue, 2, '.', '');
  443. $count = count($rep);
  444. $result['days'] = $days;
  445. $result = [
  446. 'rows' => $rep->toArray(),
  447. 'count' => $count,
  448. ];
  449. return Result::success($result);
  450. }
  451. /**
  452.  * 添加广告订单
  453.  * @param
  454.  * @return void
  455. */
  456. public function addAD(array $data): array
  457. {
  458. date_default_timezone_set('Asia/Shanghai');
  459. $time = time();
  460. $startTime = strtotime($data['starttime']);
  461. $endTime = strtotime($data['endtime']);
  462. $con_time = ($endTime - $startTime) / (24 * 60 * 60);
  463. $roundedValue = round($con_time, 2);
  464. $days = number_format($roundedValue, 2, '.', '');
  465. $timestamp = date('YmdHis', $time);
  466. $catetime = date('Y-m-d H:i:s', $time);
  467. $randomNumber = mt_rand(1000, 9999);
  468. $ordernum = $randomNumber . $timestamp; // 时间戳与随机数拼接
  469. // var_dump(($time));
  470. Db::beginTransaction();
  471. try {
  472. $order = [
  473. 'order_num' => $ordernum,
  474. 'sttime' => $data['starttime'],
  475. 'edtime' => $data['endtime'],
  476. 'user_id' => $data['user_id'],
  477. 'cttime' => $catetime,
  478. 'height' => $data['height'],
  479. 'width' => $data['width'],
  480. 'days' => $days,
  481. 'created_at' => date('Y-m-d H:i:s', time()),
  482. 'updated_at' => date('Y-m-d H:i:s', time()),
  483. ];
  484. $orderid = Order::insertGetId($order);
  485. $adplace = $data['pid'];
  486. if (is_array($data['pid'])) {
  487. $adplace = AdPlace::whereIn('id', $data['pid'])->select('website_id', 'id')->get();
  488. foreach ($adplace as $key => $ads) {
  489. $order_ad[$key] = [
  490. 'order_id' => $orderid,
  491. 'order_num' => $ordernum,
  492. 'name' => $data['name'],
  493. 'fromtime' => $data['starttime'],
  494. 'totime' => $data['endtime'],
  495. 'image_src' => $data['imgsrc'],
  496. 'image_url' => $data['imgurl'],
  497. 'pid' => $ads['id'],
  498. 'website_id' => $ads['website_id'],
  499. ];
  500. // $log = [
  501. // 'order_id' => $orderid,
  502. // 'user_id' => $data['user_id'],
  503. // 'pid' => $ads['id'],
  504. // 'website_id' => $ads['website_id'],
  505. // 'action' => '添加',
  506. // 'time' => $catetime
  507. // ];
  508. }
  509. } else {
  510. $order_ad = [
  511. 'order_id' => $orderid,
  512. 'order_num' => $ordernum,
  513. 'name' => $data['name'],
  514. 'website_id' => $adplace['website_id'],
  515. 'fromtime' => $data['starttime'],
  516. 'totime' => $data['endtime'],
  517. 'image_src' => $data['imgsrc'],
  518. 'image_url' => $data['imgurl'],
  519. 'pid' => $adplace,
  520. 'created_at' => date('Y-m-d H:i:s', time()),
  521. 'updated_at' => date('Y-m-d H:i:s', time()),
  522. ];
  523. // $log = [
  524. // 'order_id' => $orderid,
  525. // 'user_id' => $data['user_id'],
  526. // 'pid' => $adplace,
  527. // 'website_id' => $ads['website_id'],
  528. // 'action' => '添加',
  529. // 'time' => $catetime
  530. // ];
  531. }
  532. $orderad_id = OrderAd::insert($order_ad);
  533. Db::commit();
  534. } catch (\Exception $e) {
  535. Db::rollBack();
  536. return Result::error($e->getMessage());
  537. }
  538. // $log = AdLog::insert($log);
  539. $result = [
  540. 'order_id' => $orderid,
  541. 'orderad_id' => $orderad_id,
  542. 'name' => $data['name'],
  543. '$ordernum' => $ordernum,
  544. ];
  545. return Result::success($result);
  546. }
  547. /**
  548.  * 获取订单列表
  549.  * @param
  550.  * @return void
  551. */
  552. public function getOrderList(array $data): array
  553. {
  554. $where = [
  555. 'user_id' => $data['user_id'],
  556. 'user_del' => 2,
  557. ];
  558. if (isset($data['status'])) {
  559. $where['status'] = $data['status'];
  560. }
  561. $orders = Order::where($where)
  562. ->limit($data['pageSize'])
  563. ->offset(($data['page'] - 1) * $data['pageSize'])
  564. ->get()->all();
  565. //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  566. $status = [1,4,5,7];
  567. $count['all'] = Order::where('user_id',$data['user_id'])->where('user_del','2')->count();
  568. $count['wait'] = Order::where('user_id',$data['user_id'])->where('user_del','2')->where('status',6)->count();
  569. $count['pass'] = Order::where('user_id',$data['user_id'])->where('user_del','2')->whereIn('status',$status)->whereNull('bhtime')->count();
  570. $count['return'] = Order::where('user_id',$data['user_id'])->where('user_del','2')->whereNotNull('bhtime')->count();
  571. foreach($orders as $key => $val){
  572. $adsnum = OrderAd::where('order_id',$val['id'])->count();
  573. $rep[$key] = $val;
  574. $rep[$key]['num'] = $adsnum;
  575. }
  576. if (empty($rep)) {
  577. return Result::error("您暂时还没有下单");
  578. } else {
  579. $data = [
  580. 'rows' => $rep,
  581. 'count' => $count,
  582. ];
  583. }
  584. return Result::success($data);
  585. }
  586. /**
  587.  * 获取订单详情
  588.  * @param
  589.  * @return void
  590. */
  591. public function getOrderDetail(array $data): array
  592. {
  593. $order = Order::where('id', $data['order_id'])->first();
  594. $orderads = OrderAd::where('order_ad.order_id', $data['order_id'])
  595. ->leftJoin('website', 'order_ad.website_id', 'website.id')
  596. ->leftJoin('ad_place','order_ad.pid','ad_place.id')
  597. ->select('order_ad.*', 'website.website_name', 'website.id','ad_place.name')
  598. ->selectSub('website.id', 'webid')
  599. ->selectSub('order_ad.id', 'oid')
  600. ->selectSub('order_ad.name','adname')
  601. ->selectSub('ad_place.name','apname')
  602. ->orderBy("website.id", "asc")
  603. ->limit($data['pageSize'])
  604. ->offset(($data['page'] - 1) * $data['pageSize'])
  605. ->get();
  606. if (empty($order)) {
  607. return Result::error("订单id错误");
  608. }
  609. $count = OrderAd::where('order_ad.order_id', $data['order_id'])->count();
  610. $result = [
  611. 'order' => $order,
  612. 'orderads' => $orderads,
  613. 'count' => $count
  614. ];
  615. return Result::success($result);
  616. }
  617. /**
  618.  * 撤回订单
  619.  * @param
  620.  * @return void
  621. */
  622. public function cancelOrder(array $data): array
  623. {
  624. date_default_timezone_set('Asia/Shanghai');
  625. $time = time();
  626. $timestamp = date('YmdHis', $time);
  627. Db::beginTransaction();
  628. try {
  629. $order = Order::where('id', $data['order_id'])->where('status', 6)->where('sttime', '>=', $timestamp)->update(['status' => 3, 'ad_status' => '3']);
  630. $ads = OrderAd::where('order_id', $data['order_id'])->where('status', 6)->where('fromtime', '>=', $timestamp)->update(['status' => 3]);
  631. Db::commit();
  632. } catch (\Exception $e) {
  633. Db::rollBack();
  634. return Result::error($e->getMessage());
  635. }
  636. $result = [
  637. 'order' => $order,
  638. 'ads' => $ads,
  639. ];
  640. if($order==0){
  641. return Result::error("此订单不可撤回");
  642. }
  643. return Result::success($result);
  644. }
  645. /**
  646.  * 删除订单
  647.  * @param
  648.  * @return void
  649. */
  650. public function delOrderAD(array $data): array
  651. {
  652. $data['status'] = [
  653. 0 => 2,
  654. 1 => 3,
  655. 2 => 5,
  656. 3 => 7,
  657. ];
  658. //1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束;
  659. date_default_timezone_set('Asia/Shanghai');
  660. $time = time();
  661. $timestamp = date('YmdHis', $time);
  662. $where = [
  663. ['id', '=', $data['id']],
  664. ];
  665. $time = [['edtime', '<=', $timestamp]];
  666. $order = Order::where($where)->where($time)->update(['user_del' => 1]);
  667. if (empty($order)) {
  668. $order = Order::where($where)->whereIn('status', $data['status'])->update(['user_del' => 1]);
  669. }
  670. if ($order == 0) {
  671. return Result::error("此订单不可删除");
  672. }
  673. return Result::success($order);
  674. }
  675. }