SystemService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * @Name
  4. * @Description
  5. * @Author 刘学玺
  6. * @Date 2023/11/23 20:57
  7. */
  8. namespace App\Http\Services\Admin;
  9. use App\Exceptions\ApiException;
  10. use App\Http\Services\BaseService;
  11. use App\Models\Admin;
  12. use App\Models\Artificer;
  13. use App\Models\ArtificerSite;
  14. use App\Models\Order;
  15. use App\Models\OrderGrab;
  16. use App\Models\ProjectRelevancy;
  17. use App\Models\Role;
  18. use App\Models\User;
  19. use App\Models\UserWithdraw;
  20. use App\Models\UserWithdrawLog;
  21. use Exception;
  22. use Illuminate\Support\Facades\Auth;
  23. use Illuminate\Support\Facades\DB;
  24. class SystemService extends BaseService
  25. {
  26. private $message = [
  27. 'no_data' => '当前订单不存在!',
  28. 'delete_success' => '删除成功',
  29. 'delete_fail' => '删除失败!',
  30. 'reset_success' => '重置成功',
  31. 'reset_fail' => '重置失败!',
  32. 'over_success' => '结束成功',
  33. 'over_fail' => '结束失败!',
  34. 'audit_success' => '审核操作成功',
  35. 'audit_fail' => '审核操作失败!'
  36. ];
  37. /**
  38. * 提现管理
  39. * Method : Interface withdraw
  40. * @param array $data
  41. * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Foundation\Application|\Illuminate\Http\Response
  42. */
  43. public function users(array $data)
  44. {
  45. $model = $this->queryCondition(Admin::query(), $data);
  46. $list = $model
  47. ->latest()
  48. ->paginate($data['pageSize'])
  49. ->toArray();
  50. return $this->apiSuccess('', [
  51. 'list' => $list['data'],
  52. 'total' => $list['total']
  53. ]);
  54. }
  55. public function roles(array $data)
  56. {
  57. $model = $this->queryCondition(Role::query(), $data);
  58. $list = $model
  59. ->latest('create_time')
  60. ->paginate($data['pageSize'])
  61. ->toArray();
  62. return $this->apiSuccess('', [
  63. 'list' => $list['data'],
  64. 'total' => $list['total']
  65. ]);
  66. }
  67. public function audit(array $data)
  68. {
  69. $status = $data['status'];
  70. $id = $data['id'];
  71. $admin = Auth::user();
  72. DB::beginTransaction();
  73. try {
  74. if ($status == 2) {
  75. $withdraw = UserWithdraw::where('id', $id)->lockForUpdate()->first();
  76. !$withdraw && $this->apiError($this->message['audit_fail']);
  77. $withdraw->status && $this->apiError($this->message['audit_fail']);
  78. $user = User::where('id', $withdraw->user_id)->lockForUpdate()->first();
  79. !$user && $this->apiError($this->message['audit_fail']);
  80. $user->where('id', $withdraw->user_id)->increment('balance', $withdraw->money);
  81. $withdraw_data['status'] = $status;
  82. $withdraw_data['chuli_time'] = time();
  83. $withdraw_data['admin_id'] = $admin['id'];
  84. $this->commonUpdate(UserWithdraw::query(), $withdraw->id, $withdraw_data, '', $this->message['audit_fail'], false);
  85. $withdraw_log_data['user_id'] = $withdraw->user_id;
  86. $withdraw_log_data['money'] = $withdraw->money;
  87. $withdraw_log_data['type'] = 100;
  88. $withdraw_log_data['remark'] = '提现拒绝,余额返还';
  89. $withdraw_log_data['add_time'] = time();
  90. $withdraw_log_data['obj_id'] = intval($id);
  91. $withdraw_log_data['yue'] = sprintf("%01.2f",floatval($user->balance) + floatval($withdraw->money));
  92. $response = $this->commonCreate(UserWithdrawLog::query(), $withdraw_log_data, $this->message['audit_success'], $this->message['audit_fail'], false);
  93. }
  94. DB::commit();
  95. } catch (ApiException $e) {
  96. DB::rollback();
  97. $this->apiError($e->getMessage(), $e->getCode());
  98. } catch (Exception $e) {
  99. DB::rollback();
  100. throw $e;
  101. }
  102. return $response;
  103. }
  104. public function withdrawLog(array $data)
  105. {
  106. $model = $this->queryCondition(UserWithdrawLog::query(), $data,);
  107. // if (!empty($data['key'])) {
  108. // $where = [
  109. // ['id', 'like', '%' . $data['key'] . '%'],
  110. // ['user_nickname', 'like', '%' . $data['key'] . '%', 'or'],
  111. // ['mobile', 'like', '%' . $data['key'] . '%', 'or']
  112. // ];
  113. // $ids = User::where($where)->pluck('id');
  114. // $model->whereIn('user_id', $ids);
  115. // }
  116. $list = $model
  117. // ->with(['user','user.artificer','user.distributor','admin'])
  118. ->with(['user.artificer', 'withdraw.admin', 'order', 'order.project', 'order.user'])
  119. ->where('user_id', $data['user_id'])
  120. ->latest('add_time')
  121. ->paginate($data['pageSize'])
  122. ->toArray();
  123. return $this->apiSuccess('', [
  124. 'list' => $list['data'],
  125. 'total' => $list['total']
  126. ]);
  127. }
  128. /**
  129. * 重置接单技师
  130. * Method : Interface reset
  131. * @param array $data
  132. * @throws \App\Exceptions\ApiException
  133. */
  134. public function reset(array $data)
  135. {
  136. DB::beginTransaction();
  137. try {
  138. $order_id = $data['id'];
  139. $order = Order::where('id', $order_id)->lockForUpdate()->first();
  140. !$order && $this->apiError($this->message['no_data']);
  141. !in_array($order->status, [1, 2]) && $this->apiError($this->message['reset_fail']);
  142. $artificer_id = $order->jiedan_js_id;
  143. // 存在指派技师
  144. if ($artificer_id) {
  145. $grab = OrderGrab::where(['order_id' => $order_id, 'js_id' => $artificer_id])->lockForUpdate()->first();
  146. if ($grab) {
  147. // 重置抢单表状态
  148. $grab_data['status'] = 0;
  149. $this->commonUpdate(OrderGrab::query(), $grab->id, $grab_data, '', $this->message['reset_fail'], false);
  150. }
  151. }
  152. // 重置接单技师
  153. $order_data['jiedan_js_id'] = 0;
  154. $response = $this->commonUpdate(Order::query(), $order_id, $order_data, $this->message['reset_success'], $this->message['reset_fail'], false);
  155. DB::commit();
  156. } catch (ApiException $e) {
  157. DB::rollback();
  158. $this->apiError($e->getMessage(), $e->getCode());
  159. } catch (Exception $e) {
  160. DB::rollback();
  161. throw $e;
  162. }
  163. return $response;
  164. }
  165. public function delete(array $data)
  166. {
  167. $delete_id = $data['id'];
  168. $order = Order::find($delete_id);
  169. !$order && $this->apiError($this->message['no_data']);
  170. $order->status && $this->apiError($this->message['delete_fail']);
  171. return $this->commonIsDelete(Order::query(), [$delete_id], $this->message['delete_success'], $this->message['delete_fail']);
  172. }
  173. public function over(array $data)
  174. {
  175. DB::beginTransaction();
  176. try {
  177. $order_id = $data['id'];
  178. $order = Order::where('id', $order_id)->lockForUpdate()->first();
  179. !in_array($order->status, [2, 6]) && $this->apiError($this->message['over_fail']);
  180. $data = [
  181. 'status' => 3,
  182. 'finish_time' => time()
  183. ];
  184. $response = $this->commonUpdate(Order::query(), $order_id, $data, $this->message['over_success'], $this->message['over_fail']);
  185. DB::commit();
  186. } catch (ApiException $e) {
  187. DB::rollback();
  188. $this->apiError($e->getMessage(), $e->getCode());
  189. } catch (Exception $e) {
  190. DB::rollback();
  191. throw $e;
  192. }
  193. return $response;
  194. }
  195. public function grab(array $data)
  196. {
  197. $order_id = $data['id'];
  198. $order = Order::find($order_id)->toArray();
  199. $list = OrderGrab::with('artificer.site')->where('order_id', $order_id)->oldest('create_time')->get()->toArray();
  200. foreach ($list as &$item) {
  201. if ($item['artificer']['site'])
  202. $item['distance'] = get_distance([$order['lng'], $order['lat']], [$item['artificer']['site']['lng'], $item['artificer']['site']['lat']]);
  203. }
  204. return $this->apiSuccess('', ['list' => $list]);
  205. }
  206. public function vicinity(array $data)
  207. {
  208. $code = 156370600;
  209. $order_id = $data['id'];
  210. $page = $data['page'];
  211. $pageSize = $data['pageSize'];
  212. $range_artificer_ids = [];
  213. // 第一步 按照城市编码获取技师定位数据
  214. $order = Order::find($order_id);
  215. $site = ArtificerSite::with('time')->where('city_code', $code)->select('js_id', 'lng', 'lat')->get()->toArray();
  216. $week = date('w', time());
  217. $date = date('H:i');
  218. foreach ($site as $item) {
  219. $distance = get_distance([$order->lng, $order->lat], [$item['lng'], $item['lat']]);
  220. $status = 0;
  221. if ($distance <= 60 && !in_array($item['js_id'], $range_artificer_ids)) {
  222. if ($item['time'] && $item['time']['times'] && in_array($week, json_decode($item['time']['weeks']))) {
  223. // 开关类型
  224. $status = 1;
  225. // 时间段类型
  226. // foreach ($item['time']['times'] as $vo){
  227. // if(strtotime($date) >= strtotime($vo['js_start_time']) && strtotime($date) <= strtotime($vo['js_end_time'])){
  228. // $status = 1;
  229. // break;
  230. // }
  231. // }
  232. }
  233. $status && array_push($range_artificer_ids, $item['js_id']);
  234. }
  235. }
  236. // $artificer_ids = array_column($site,'js_id');
  237. // 第二步 筛选开通项目技师
  238. $auth_artificer_ids = ProjectRelevancy::whereIn('js_id', $range_artificer_ids)->where('project_id', $order->project_id)->where('status', '1')->pluck('js_id')->toArray();
  239. $list = Artificer::with('site')->whereIn('id', $auth_artificer_ids)->where('js_status', '1')->get()->toArray();
  240. // 第三步 循环排序距离
  241. foreach ($list as &$item) {
  242. $item['distance'] = get_distance([$order->lng, $order->lat], [$item['site']['lng'], $item['site']['lat']]);
  243. }
  244. // 根据列进行排序
  245. $distances = array_column($list, 'distance');
  246. array_multisort($distances, SORT_ASC, $list);
  247. // 第四步 分页
  248. $offset = ($page - 1) * $pageSize; // 开始截取的索引
  249. $result = array_slice($list, $offset, $pageSize);
  250. return $this->apiSuccess('', ['list' => $list, 'total' => count($list)]);
  251. }
  252. }