OrderService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace App\JsonRpc;
  3. use App\Model\Ad;
  4. use App\Model\Order;
  5. use App\Model\OrderAd;
  6. use App\Tools\Result;
  7. use Hyperf\DbConnection\Db;
  8. use Hyperf\RpcServer\Annotation\RpcService;
  9. #[RpcService(name: "OrderService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
  10. class OrderService implements OrderServiceInterface
  11. {
  12. /**
  13.  * 查询没有广告的广告位
  14.  * @param
  15.  * @return void
  16. */
  17. public function getAD(array $data): array
  18. {
  19. $where = [
  20. 'width' => $data['width'],
  21. 'height' => $data['height'],
  22. ];
  23. $start = $data['starttime'];
  24. $end = $data['endtime'];
  25. $rep = Ad::where($where)
  26. ->where('fromtime', '<', $start)
  27. ->where('totime', '>', $end)
  28. ->orderBy('id')
  29. ->limit($data['pageSize'])
  30. ->offset(($data['page'] - 1) * $data['pageSize'])->get();
  31. $count = Ad::where($where)->count();
  32. $data = [
  33. 'rows' => $rep->toArray(),
  34. 'count' => $count,
  35. ];
  36. $ads = Ad::whereIn($data['id'])
  37. ->leftJoin('ad_place', 'ad.pid', 'ad_place.id')
  38. ->leftJoin("article_data", "article.id", "article_data.article_id")
  39. ->select("ad_place.*", "ad.*")
  40. ->orderBy("ad.id", "desc")
  41. ->limit($data['pageSize'])
  42. ->offset(($data['page'] - 1) * $data['pageSize'])->get();
  43. $count = Ad::whereIn($data['id'])->count();
  44. $data = [
  45. 'rows' => $ads->toArray(),
  46. 'count' => $count,
  47. ];
  48. if (empty($rep)) {
  49. return Result::error("没有信息数据");
  50. }
  51. return Result::success($data);
  52. }
  53. /**
  54. * 添加订单
  55. * @param
  56. * @return void
  57. */
  58. public function addOrder(array $data): array
  59. {
  60. $ads = Ad::whereIn($data['id'])
  61. ->leftJoin('ad_place', 'ad.pid', 'ad_place.id')
  62. ->leftJoin("article_data", "article.id", "article_data.article_id")
  63. ->select("ad_place.*", "ad.*")
  64. ->orderBy("ad.id", "desc")
  65. ->limit($data['pageSize'])
  66. ->offset(($data['page'] - 1) * $data['pageSize'])->get();
  67. $count = Ad::whereIn($data['id'])->count();
  68. $data = [
  69. 'rows' => $ads->toArray(),
  70. 'count' => $count,
  71. ];
  72. if (empty($rep)) {
  73. return Result::error("没有信息数据");
  74. }
  75. return Result::success($data);
  76. }
  77. /**
  78. * 获取订单列表
  79. * @param
  80. * @return void
  81. */
  82. public function getOrderListAdmin(array $data): array
  83. {
  84. // 获取分页参数,默认每页 10 条记录
  85. $page = isset($data['page']) ? (int) $data['page'] : 1;
  86. $perPage = isset($data['pagesize']) ? (int) $data['pagesize'] : 10;
  87. // 构建查询条件
  88. $where = [
  89. 'order.status' => $data['status'], // 明确指定 order 表的 status 列
  90. ];
  91. // 添加订单号查询条件
  92. if (!empty($data['order_num'])) {
  93. $where['order.order_num'] = $data['order_num']; // 明确指定 order 表的 order_num 列
  94. }
  95. // 处理时间范围查询
  96. $start = $data['sttime'];
  97. $end = $data['edtime'];
  98. // 查询数据并分页
  99. $query = Order::where($where)
  100. ->when(!empty($start) && !empty($end), function ($q) use ($start, $end) {
  101. $q->whereBetween('order.fromtime', [$start, $end]); // 明确指定 order 表的 fromtime 列
  102. })
  103. ->when(!empty($start), function ($q) use ($start) {
  104. $q->where('order.fromtime', '>=', $start); // 明确指定 order 表的 fromtime 列
  105. })
  106. ->when(!empty($end), function ($q) use ($end) {
  107. $q->where('order.totime', '<=', $end); // 明确指定 order 表的 totime 列
  108. })
  109. ->leftJoin('user as admin_user', 'order.admin_user_id', '=', 'admin_user.id')
  110. ->leftJoin('user as user', 'order.user_id', '=', 'user.id')
  111. ->select(
  112. 'order.*',
  113. 'admin_user.user_name as admin_user_name',
  114. 'user.user_name as user_name'
  115. )
  116. ->orderBy('order.id');
  117. // 执行分页查询
  118. $result = $query->paginate($perPage, ['*'], 'page', $page);
  119. // 返回分页结果
  120. return Result::success([
  121. 'count' => $result->total(),
  122. 'current_page' => $result->currentPage(),
  123. 'last_page' => $result->lastPage(),
  124. 'pagesize' => $result->perPage(),
  125. 'rows' => $result->items(),
  126. ]);
  127. }
  128. /**
  129. * 获取订单详情
  130. * @param
  131. * @return void
  132. */
  133. public function getOrderDetailAdmin(array $data): array
  134. {
  135. $order = Order::where('order.id', $data['id'])
  136. ->leftJoin('user as admin_user', 'order.admin_user_id', '=', 'admin_user.id')
  137. ->leftJoin('user as user', 'order.user_id', '=', 'user.id')
  138. ->select(
  139. 'order.*',
  140. 'admin_user.user_name as admin_user_name',
  141. 'user.user_name as user_name'
  142. )->first();
  143. if (empty($order)) {
  144. return Result::error("没有信息数据");
  145. }
  146. $pid = $order['id'];
  147. $ad = OrderAd::where('order_id', $pid)->get();
  148. $order['ad'] = $ad;
  149. return Result::success($order);
  150. }
  151. /**
  152. * 修改订单价格
  153. * @param
  154. * @return void
  155. */
  156. public function editPriceOrderAdmin(array $data): array
  157. {
  158. $order = Order::where('id', $data['id'])->first();
  159. if (empty($order)) {
  160. return Result::error("没有信息数据");
  161. }
  162. $order->price = $data['price'];
  163. $order->save();
  164. return Result::success($order);
  165. }
  166. /**
  167. *拒绝订单
  168. * @param
  169. * @return void
  170. */
  171. public function rejectOrderAdmin(array $data): array
  172. {
  173. $order = Order::where('id', $data['id'])->first();
  174. if (empty($order)) {
  175. return Result::error("没有信息数据");
  176. }
  177. Db::beginTransaction();
  178. try {
  179. $order->status = 2; //订单状态:1:通过;2:驳回;3:撤回;4:修改;5:过期;6:待审核;7:结束
  180. $order->ad_status = 2;
  181. $order->reason = $data['reason'];
  182. $order->bhtime = date('Y-m-d H:i:s');
  183. $order->save();
  184. OrderAd::where('order_id', $data['id'])
  185. ->update(['status' => 2]);
  186. Db::commit();
  187. } catch (\Exception $e) {
  188. Db::rollBack();
  189. return Result::error("操作失败");
  190. }
  191. return Result::success($order);
  192. }
  193. /**
  194. * 结束订单
  195. * @param
  196. * @return void
  197. */
  198. public function endOrderAdmin(array $data): array
  199. {
  200. $order = Order::where('id', $data['id'])->first();
  201. if (empty($order)) {
  202. return Result::error("没有信息数据");
  203. }
  204. Db::beginTransaction();
  205. try {
  206. $order->status = 7;
  207. $order->jstime = date('Y-m-d H:i:s');
  208. $order->save();
  209. // 获取 order_ad 表中的记录
  210. $orderAds = OrderAd::where('order_id', $data['id'])
  211. ->update(['status' => 7]);
  212. // 在ad表中删除相同pid的数据
  213. Ad::where('order_id',$data['id'])->delete();
  214. Db::commit();
  215. } catch (\Exception $e) {
  216. Db::rollBack();
  217. return Result::error("操作失败" . $e->getMessage());
  218. }
  219. return Result::success($order);
  220. }
  221. /**
  222. * 删除订单
  223. * @param
  224. * @return void
  225. */
  226. public function delOrderAdmin(array $data): array
  227. {
  228. // 获取订单信息
  229. $order = Order::where('id', $data['id'])->first();
  230. if (empty($order)) {
  231. return Result::error("没有信息数据");
  232. }
  233. Db::beginTransaction();
  234. try {
  235. // 获取 order_ad 表中的记录
  236. OrderAd::where('order_id', $data['id'])->delete();
  237. // 删除 ad 表中的记录
  238. Ad::where('order_id', $order['id'])->delete();)
  239. // 提交事务
  240. Db::commit();
  241. } catch (\Exception $e) {
  242. // 回滚事务
  243. Db::rollBack();
  244. // 返回错误信息
  245. return Result::error($e->getMessage());
  246. }
  247. // 返回成功信息
  248. return Result::success("删除成功");
  249. }
  250. /**
  251. * 审核订单
  252. * @param
  253. * @return void
  254. */
  255. public function applyOrderStatusAdmin(array $data): array
  256. {
  257. $order = Order::where('id', $data['id'])
  258. ->where('status', 6)
  259. ->first();
  260. if (empty($order)) {
  261. return Result::error("没有信息数据");
  262. }
  263. Db::beginTransaction();
  264. try {
  265. $order->status = 1;
  266. $order->ad_status = 1;
  267. $order->shtime = date('Y-m-d H:i:s');
  268. $order->save();
  269. // 批量更新 order_ad 表中的状态
  270. OrderAd::where('order_id', $data['id'])
  271. ->where('status', 6)
  272. ->update(['status' => 1]);
  273. //判断的当前时间是否在订单的开始时间之后,并且小于等于订单的结束时间
  274. if (time() >= strtotime($order->sttime) && time() < strtotime($order->edtime)) {
  275. $ad_status = 1; //审核生效
  276. } else {
  277. $ad_status = 2; //审核
  278. }
  279. $ads = [];
  280. foreach ($orderAds as $orderAd) {
  281. $ads[] = [
  282. 'name' => $orderAd->name,
  283. 'pid' => $orderAd->pid,
  284. 'areaid' => $orderAd->areaid,
  285. 'amount' => $orderAd->amount,
  286. 'introduce' => $orderAd->introduce,
  287. 'hits' => $orderAd->hits,
  288. 'admin_user_id' => $orderAd->admin_user_id,
  289. 'fromtime' => $orderAd->fromtime,
  290. 'totime' => $orderAd->totime,
  291. 'text_name' => $orderAd->text_name,
  292. 'text_url' => $orderAd->text_url,
  293. 'text_title' => $orderAd->text_title,
  294. 'image_src' => $orderAd->image_src,
  295. 'image_url' => $orderAd->image_url,
  296. 'image_alt' => $orderAd->image_alt,
  297. 'video_src' => $orderAd->video_src,
  298. 'video_url' => $orderAd->video_url,
  299. 'video_auto' => $orderAd->video_auto,
  300. 'video_loop' => $orderAd->video_loop,
  301. 'status' => $ad_status,
  302. 'order_id' => $orderAd->order_id,
  303. ];
  304. }
  305. Ad::insert($ads);
  306. Db::commit();
  307. } catch (\Exception $e) {
  308. Db::rollBack();
  309. return Result::error($e->getMessage());
  310. }
  311. return Result::success($order);
  312. }
  313. }