OrderService.php 26 KB

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