123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- /**
- * @Name
- * @Description
- * @Author 刘学玺
- * @Date 2023/11/23 20:57
- */
- namespace App\Http\Services\Admin;
- use App\Exceptions\ApiException;
- use App\Http\Services\BaseService;
- use App\Models\Admin;
- use App\Models\Artificer;
- use App\Models\ArtificerSite;
- use App\Models\Order;
- use App\Models\OrderGrab;
- use App\Models\ProjectRelevancy;
- use App\Models\Role;
- use App\Models\User;
- use App\Models\UserWithdraw;
- use App\Models\UserWithdrawLog;
- use Exception;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\DB;
- class SystemService extends BaseService
- {
- private $message = [
- 'no_data' => '当前订单不存在!',
- 'delete_success' => '删除成功',
- 'delete_fail' => '删除失败!',
- 'reset_success' => '重置成功',
- 'reset_fail' => '重置失败!',
- 'over_success' => '结束成功',
- 'over_fail' => '结束失败!',
- 'audit_success' => '审核操作成功',
- 'audit_fail' => '审核操作失败!'
- ];
- /**
- * 提现管理
- * Method : Interface withdraw
- * @param array $data
- * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Foundation\Application|\Illuminate\Http\Response
- */
- public function users(array $data)
- {
- $model = $this->queryCondition(Admin::query(), $data);
- $list = $model
- ->latest()
- ->paginate($data['pageSize'])
- ->toArray();
- return $this->apiSuccess('', [
- 'list' => $list['data'],
- 'total' => $list['total']
- ]);
- }
- public function roles(array $data)
- {
- $model = $this->queryCondition(Role::query(), $data);
- $list = $model
- ->latest('create_time')
- ->paginate($data['pageSize'])
- ->toArray();
- return $this->apiSuccess('', [
- 'list' => $list['data'],
- 'total' => $list['total']
- ]);
- }
- public function audit(array $data)
- {
- $status = $data['status'];
- $id = $data['id'];
- $admin = Auth::user();
- DB::beginTransaction();
- try {
- if ($status == 2) {
- $withdraw = UserWithdraw::where('id', $id)->lockForUpdate()->first();
- !$withdraw && $this->apiError($this->message['audit_fail']);
- $withdraw->status && $this->apiError($this->message['audit_fail']);
- $user = User::where('id', $withdraw->user_id)->lockForUpdate()->first();
- !$user && $this->apiError($this->message['audit_fail']);
- $user->where('id', $withdraw->user_id)->increment('balance', $withdraw->money);
- $withdraw_data['status'] = $status;
- $withdraw_data['chuli_time'] = time();
- $withdraw_data['admin_id'] = $admin['id'];
- $this->commonUpdate(UserWithdraw::query(), $withdraw->id, $withdraw_data, '', $this->message['audit_fail'], false);
- $withdraw_log_data['user_id'] = $withdraw->user_id;
- $withdraw_log_data['money'] = $withdraw->money;
- $withdraw_log_data['type'] = 100;
- $withdraw_log_data['remark'] = '提现拒绝,余额返还';
- $withdraw_log_data['add_time'] = time();
- $withdraw_log_data['obj_id'] = intval($id);
- $withdraw_log_data['yue'] = sprintf("%01.2f",floatval($user->balance) + floatval($withdraw->money));
- $response = $this->commonCreate(UserWithdrawLog::query(), $withdraw_log_data, $this->message['audit_success'], $this->message['audit_fail'], false);
- }
- DB::commit();
- } catch (ApiException $e) {
- DB::rollback();
- $this->apiError($e->getMessage(), $e->getCode());
- } catch (Exception $e) {
- DB::rollback();
- throw $e;
- }
- return $response;
- }
- public function withdrawLog(array $data)
- {
- $model = $this->queryCondition(UserWithdrawLog::query(), $data,);
- // if (!empty($data['key'])) {
- // $where = [
- // ['id', 'like', '%' . $data['key'] . '%'],
- // ['user_nickname', 'like', '%' . $data['key'] . '%', 'or'],
- // ['mobile', 'like', '%' . $data['key'] . '%', 'or']
- // ];
- // $ids = User::where($where)->pluck('id');
- // $model->whereIn('user_id', $ids);
- // }
- $list = $model
- // ->with(['user','user.artificer','user.distributor','admin'])
- ->with(['user.artificer', 'withdraw.admin', 'order', 'order.project', 'order.user'])
- ->where('user_id', $data['user_id'])
- ->latest('add_time')
- ->paginate($data['pageSize'])
- ->toArray();
- return $this->apiSuccess('', [
- 'list' => $list['data'],
- 'total' => $list['total']
- ]);
- }
- /**
- * 重置接单技师
- * Method : Interface reset
- * @param array $data
- * @throws \App\Exceptions\ApiException
- */
- public function reset(array $data)
- {
- DB::beginTransaction();
- try {
- $order_id = $data['id'];
- $order = Order::where('id', $order_id)->lockForUpdate()->first();
- !$order && $this->apiError($this->message['no_data']);
- !in_array($order->status, [1, 2]) && $this->apiError($this->message['reset_fail']);
- $artificer_id = $order->jiedan_js_id;
- // 存在指派技师
- if ($artificer_id) {
- $grab = OrderGrab::where(['order_id' => $order_id, 'js_id' => $artificer_id])->lockForUpdate()->first();
- if ($grab) {
- // 重置抢单表状态
- $grab_data['status'] = 0;
- $this->commonUpdate(OrderGrab::query(), $grab->id, $grab_data, '', $this->message['reset_fail'], false);
- }
- }
- // 重置接单技师
- $order_data['jiedan_js_id'] = 0;
- $response = $this->commonUpdate(Order::query(), $order_id, $order_data, $this->message['reset_success'], $this->message['reset_fail'], false);
- DB::commit();
- } catch (ApiException $e) {
- DB::rollback();
- $this->apiError($e->getMessage(), $e->getCode());
- } catch (Exception $e) {
- DB::rollback();
- throw $e;
- }
- return $response;
- }
- public function delete(array $data)
- {
- $delete_id = $data['id'];
- $order = Order::find($delete_id);
- !$order && $this->apiError($this->message['no_data']);
- $order->status && $this->apiError($this->message['delete_fail']);
- return $this->commonIsDelete(Order::query(), [$delete_id], $this->message['delete_success'], $this->message['delete_fail']);
- }
- public function over(array $data)
- {
- DB::beginTransaction();
- try {
- $order_id = $data['id'];
- $order = Order::where('id', $order_id)->lockForUpdate()->first();
- !in_array($order->status, [2, 6]) && $this->apiError($this->message['over_fail']);
- $data = [
- 'status' => 3,
- 'finish_time' => time()
- ];
- $response = $this->commonUpdate(Order::query(), $order_id, $data, $this->message['over_success'], $this->message['over_fail']);
- DB::commit();
- } catch (ApiException $e) {
- DB::rollback();
- $this->apiError($e->getMessage(), $e->getCode());
- } catch (Exception $e) {
- DB::rollback();
- throw $e;
- }
- return $response;
- }
- public function grab(array $data)
- {
- $order_id = $data['id'];
- $order = Order::find($order_id)->toArray();
- $list = OrderGrab::with('artificer.site')->where('order_id', $order_id)->oldest('create_time')->get()->toArray();
- foreach ($list as &$item) {
- if ($item['artificer']['site'])
- $item['distance'] = get_distance([$order['lng'], $order['lat']], [$item['artificer']['site']['lng'], $item['artificer']['site']['lat']]);
- }
- return $this->apiSuccess('', ['list' => $list]);
- }
- public function vicinity(array $data)
- {
- $code = 156370600;
- $order_id = $data['id'];
- $page = $data['page'];
- $pageSize = $data['pageSize'];
- $range_artificer_ids = [];
- // 第一步 按照城市编码获取技师定位数据
- $order = Order::find($order_id);
- $site = ArtificerSite::with('time')->where('city_code', $code)->select('js_id', 'lng', 'lat')->get()->toArray();
- $week = date('w', time());
- $date = date('H:i');
- foreach ($site as $item) {
- $distance = get_distance([$order->lng, $order->lat], [$item['lng'], $item['lat']]);
- $status = 0;
- if ($distance <= 60 && !in_array($item['js_id'], $range_artificer_ids)) {
- if ($item['time'] && $item['time']['times'] && in_array($week, json_decode($item['time']['weeks']))) {
- // 开关类型
- $status = 1;
- // 时间段类型
- // foreach ($item['time']['times'] as $vo){
- // if(strtotime($date) >= strtotime($vo['js_start_time']) && strtotime($date) <= strtotime($vo['js_end_time'])){
- // $status = 1;
- // break;
- // }
- // }
- }
- $status && array_push($range_artificer_ids, $item['js_id']);
- }
- }
- // $artificer_ids = array_column($site,'js_id');
- // 第二步 筛选开通项目技师
- $auth_artificer_ids = ProjectRelevancy::whereIn('js_id', $range_artificer_ids)->where('project_id', $order->project_id)->where('status', '1')->pluck('js_id')->toArray();
- $list = Artificer::with('site')->whereIn('id', $auth_artificer_ids)->where('js_status', '1')->get()->toArray();
- // 第三步 循环排序距离
- foreach ($list as &$item) {
- $item['distance'] = get_distance([$order->lng, $order->lat], [$item['site']['lng'], $item['site']['lat']]);
- }
- // 根据列进行排序
- $distances = array_column($list, 'distance');
- array_multisort($distances, SORT_ASC, $list);
- // 第四步 分页
- $offset = ($page - 1) * $pageSize; // 开始截取的索引
- $result = array_slice($list, $offset, $pageSize);
- return $this->apiSuccess('', ['list' => $list, 'total' => count($list)]);
- }
- }
|