OrderService.php 41 KB

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