OrderService.php 40 KB

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