OrderService.php 39 KB

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