OrderService.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. $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. 'created_at' => date('Y-m-d H:i:s', time()),
  693. 'updated_at' => date('Y-m-d H:i:s', time()),
  694. ];
  695. //添加订单
  696. $orderid = Order::insertGetId($order);
  697. $adplace = $data['pid'];
  698. if (is_array($data['pid'])) {
  699. $adplace = AdPlace::whereIn('id', $data['pid'])->select('website_id', 'id')->get();
  700. $order_ad = [];
  701. foreach ($adplace as $key => $ads) {
  702. $order_ad[$key] = [
  703. 'order_id' => $orderid,
  704. 'order_num' => $ordernum,
  705. 'name' => $data['name'],
  706. 'fromtime' => $data['starttime'],
  707. 'totime' => $data['endtime'],
  708. 'image_src' => $data['imgsrc'],
  709. 'image_url' => $data['imgurl'],
  710. 'pid' => $ads['id'],
  711. 'website_id' => $ads['website_id'],
  712. ];
  713. }
  714. }
  715. $orderad_id = OrderAd::insert($order_ad);
  716. Db::commit();
  717. } catch (\Exception $e) {
  718. Db::rollBack();
  719. return Result::error($e->getMessage());
  720. }
  721. // $log = AdLog::insert($log);
  722. $result = [
  723. 'order_id' => $orderid,
  724. 'orderad_id' => $orderad_id,
  725. 'name' => $data['name'],
  726. 'ordernum' => $ordernum,
  727. ];
  728. return Result::success($result);
  729. }
  730. /**
  731.  * 获取订单列表
  732.  * @param
  733.  * @return void
  734. */
  735. public function getOrderList(array $data): array
  736. {
  737. $where = [
  738. 'user_id' => $data['user_id'],
  739. 'user_del' => 2,
  740. ];
  741. if (isset($data['status'])) {
  742. $where['status'] = $data['status'];
  743. }
  744. $orders = Order::where($where)
  745. ->limit($data['pageSize'])
  746. ->offset(($data['page'] - 1) * $data['pageSize'])
  747. ->orderBy("updated_at", "desc")
  748. ->get()
  749. ->all();
  750. //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  751. $count['all'] = Order::where($where)->count();
  752. $count['pass'] = Order::where($where)->where('status', 1)->count();
  753. $count['return'] = Order::where($where)->where('status', 2)->count();
  754. $count['recall'] = Order::where($where)->where('status', 3)->count();
  755. $count['update'] = Order::where($where)->where('status', 4)->count();
  756. $count['fail'] = Order::where($where)->where('status', 5)->count();
  757. $count['waite'] = Order::where($where)->where('status', 6)->count();
  758. $count['over'] = Order::where($where)->where('status', 7)->count();
  759. foreach ($orders as $key => $val) {
  760. $adsnum = OrderAd::where('order_id', $val['id'])->count();
  761. $rep[$key] = $val;
  762. $rep[$key]['num'] = $adsnum;
  763. }
  764. if (empty($rep)) {
  765. return Result::error("您暂时还没有下单");
  766. } else {
  767. $data = [
  768. 'rows' => $rep,
  769. 'count' => $count,
  770. ];
  771. }
  772. return Result::success($data);
  773. }
  774. /**
  775.  * 获取订单详情
  776.  * @param
  777.  * @return void
  778. */
  779. public function getOrderDetail(array $data): array
  780. {
  781. $order = Order::where('id', $data['order_id'])->first();
  782. $orderads = OrderAd::where('order_ad.order_id', $data['order_id'])
  783. ->leftJoin('website', 'order_ad.website_id', 'website.id')
  784. ->leftJoin('ad_place', 'order_ad.pid', 'ad_place.id')
  785. ->select('order_ad.*', 'website.website_name', 'website.id', 'ad_place.name')
  786. ->selectSub('website.id', 'webid')
  787. ->selectSub('order_ad.id', 'oid')
  788. ->selectSub('order_ad.name', 'adname')
  789. ->selectSub('ad_place.name', 'apname')
  790. ->orderBy("website.id", "asc")
  791. ->limit($data['pageSize'])
  792. ->offset(($data['page'] - 1) * $data['pageSize'])
  793. ->get();
  794. if (empty($order)) {
  795. return Result::error("订单id错误");
  796. }
  797. $count = OrderAd::where('order_ad.order_id', $data['order_id'])->count();
  798. $result = [
  799. 'order' => $order,
  800. 'orderads' => $orderads,
  801. 'count' => $count,
  802. ];
  803. return Result::success($result);
  804. }
  805. /**
  806.  * 撤回订单
  807.  * @param
  808.  * @return void
  809. */
  810. public function cancelOrder(array $data): array
  811. {
  812. date_default_timezone_set('Asia/Shanghai');
  813. $time = time();
  814. $timestamp = date('YmdHis', $time);
  815. Db::beginTransaction();
  816. try {
  817. $order = Order::where('id', $data['order_id'])->where('status', 6)->where('sttime', '>=', $timestamp)->update(['status' => 3, 'ad_status' => '3']);
  818. $ads = OrderAd::where('order_id', $data['order_id'])->where('status', 6)->where('fromtime', '>=', $timestamp)->update(['status' => 3]);
  819. Db::commit();
  820. } catch (\Exception $e) {
  821. Db::rollBack();
  822. return Result::error($e->getMessage());
  823. }
  824. $result = [
  825. 'order' => $order,
  826. 'ads' => $ads,
  827. ];
  828. if ($order == 0) {
  829. return Result::error("此订单不可撤回");
  830. }
  831. return Result::success($result);
  832. }
  833. /**
  834.  * 删除订单
  835.  * @param
  836.  * @return void
  837. */
  838. public function delOrderAD(array $data): array
  839. {
  840. $data['status'] = [
  841. 0 => 2,
  842. 1 => 3,
  843. 2 => 5,
  844. 3 => 7,
  845. ];
  846. //1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束;
  847. date_default_timezone_set('Asia/Shanghai');
  848. $time = time();
  849. $timestamp = date('YmdHis', $time);
  850. $where = [
  851. ['id', '=', $data['id']],
  852. ];
  853. $time = [['edtime', '<=', $timestamp]];
  854. $order = Order::where($where)->where($time)->update(['user_del' => 1]);
  855. if (empty($order)) {
  856. $order = Order::where($where)->whereIn('status', $data['status'])->update(['user_del' => 1]);
  857. }
  858. if ($order == 0) {
  859. return Result::error("此订单不可删除");
  860. }
  861. return Result::success($order);
  862. }
  863. /**
  864.  * 创建购物车
  865.  * @param
  866.  * @return void
  867. */
  868. public function addShoppingCart(array $data): array
  869. {
  870. $shop = ShoppingCart::where('user_id', $data['user_id'])->get()->all();
  871. $time = time();
  872. $timestamp = date('YmdHis', $time);
  873. $randomNumber = mt_rand(100, 999);
  874. $shopping_id = $randomNumber . $timestamp; // 时间戳与随机数拼接
  875. Db::beginTransaction();
  876. try {
  877. if (!empty($shop)) {
  878. $del_shop = ShoppingCart::where('user_id', $data['user_id'])->delete();
  879. if (empty($del_shop)) {
  880. Db::rollBack();
  881. return Result::error("删除购物车失败");
  882. // throw new \Exception("删除购物车失败");
  883. }
  884. }
  885. $shop = [
  886. 'user_id' => $data['user_id'],
  887. 'shopping_id' => $shopping_id,
  888. 'created_at' => date('Y-m-d H:i:s', time()),
  889. 'updated_at' => date('Y-m-d H:i:s', time()),
  890. ];
  891. $result = ShoppingCart::insert($shop);
  892. Db::commit();
  893. } catch (\Exception $e) {
  894. Db::rollBack();
  895. return Result::error($e->getMessage());
  896. }
  897. if (empty($result)) {
  898. return Result::error("创建失败");
  899. } else {
  900. return Result::success($shopping_id);
  901. }
  902. }
  903. /**
  904.  * 获取购物车中的广告位
  905.  * @param
  906.  * @return void
  907. */
  908. public function getShoppingCartAD(array $data): array
  909. {
  910. $shopcart = ShoppingCart::where('shopping_id', $data['shopping_id'])
  911. ->where('user_id', $data['user_id'])
  912. ->orderBy('pid')
  913. ->pluck('pid')
  914. ->toArray();
  915. $result['pid'] = $shopcart;
  916. if (empty($result)) {
  917. return Result::error("购物车id错误");
  918. } else {
  919. return Result::success($result);
  920. }
  921. }
  922. /**
  923.  * 添加购物车中的广告位
  924.  * @param
  925.  * @return void
  926. */
  927. public function addShoppingCartAD(array $data): array
  928. {
  929. $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->first();
  930. if (empty($shop)) {
  931. return Result::error("购物车id错误");
  932. } else {
  933. $ad = AdPlace::where('ad_place.id', $data['pid'])
  934. ->leftJoin('website', 'ad_place.website_id', 'website.id')
  935. ->select('ad_place.id as pid', 'website.id as website_id')
  936. ->first();
  937. if (empty($ad)) {
  938. return Result::error("广告位id错误");
  939. }
  940. if (empty($shop['pid']) && empty($shop['website_id'])) {
  941. $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id', $data['user_id'])->update(['pid' => $ad['pid'], 'website_id' => $ad['website_id']]);
  942. } else {
  943. if ($data['pid'] == $shop['pid']) {
  944. return Result::error("购物车中已存在该广告位");
  945. }
  946. $shop_ad = [
  947. 'pid' => $ad['pid'],
  948. 'website_id' => $ad['website_id'],
  949. 'shopping_id' => $shop['shopping_id'],
  950. 'user_id' => $data['user_id'],
  951. ];
  952. $result = ShoppingCart::insertGetId($shop_ad);
  953. }
  954. }
  955. if (empty($result)) {
  956. return Result::error("添加失败");
  957. } else {
  958. return Result::success($result);
  959. }
  960. }
  961. /**
  962.  * 删除购物车中的广告位
  963.  * @param
  964.  * @return void
  965. */
  966. public function delShoppingCartAD(array $data): array
  967. {
  968. $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->where('pid', $data['pid'])->first();
  969. if (empty($shop)) {
  970. return Result::error("不存在此条记录!(参数错误)");
  971. } else {
  972. $count = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->count();
  973. if ($count == 1) {
  974. $result = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id', $data['user_id'])->update(['pid' => null, 'website_id' => null]);
  975. } else {
  976. $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id', $data['user_id'])->where('pid', $data['pid'])->delete();
  977. }
  978. }
  979. if (empty($result)) {
  980. return Result::error("删除失败");
  981. } else {
  982. return Result::success($result);
  983. }
  984. }
  985. /**
  986.  * 获取广告位的尺寸
  987.  *
  988.  *
  989. */
  990. public function getAdSize(): array
  991. {
  992. $result = AdSize::get()->all();
  993. if (empty($result)) {
  994. return Result::error("暂无数据");
  995. }
  996. return Result::success($result);
  997. }
  998. }