OrderService.php 42 KB

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