OrderService.php 26 KB

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