OrderService.php 32 KB

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