// +---------------------------------------------------------------------- namespace App\Services\Common; use App\Models\AccountLogModel; use App\Models\ActionLogModel; use App\Models\AgentModel; use App\Models\AgentRegionModel; use App\Models\MemberModel; use App\Services\BaseService; use Illuminate\Support\Facades\DB; /** * 代理管理-服务类 * @author laravel开发员 * @since 2020/11/11 * @package App\Services\Common */ class AgentService extends BaseService { public static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 */ public function __construct() { $this->model = new AgentModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 列表 * @param $params * @param int $pageSize * @return array */ public function getDataList($params, $pageSize = 15) { $where = ['a.mark' => 1]; $list = $this->model->with(['user'])->from('agent as a') ->leftJoin('member as c', 'c.id', '=', 'a.user_id') ->where($where) ->where(function ($query) use ($params) { $keyword = isset($params['keyword']) ? $params['keyword'] : ''; if ($keyword) { $query->where('a.realname', 'like', "%{$keyword}%"); } }) ->where(function ($query) use ($params) { $mobile = isset($params['mobile']) ? trim($params['mobile']) : ''; if ($mobile) { $query->where('a.mobile', 'like', "%{$mobile}%"); } $status = isset($params['status']) ? $params['status'] : 0; if ($status > 0 && is_array($status)) { $query->whereIn('a.status', $status); } else if ($status) { $query->where('a.status', $status); } }) ->select(['a.*', 'c.nickname', 'c.mobile as user_mobile', 'c.balance as balance']) ->orderBy('a.create_time', 'desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $list = $list ? $list->toArray() : []; if ($list) { foreach ($list['data'] as &$item) { $item['selected'] = false; $item['nickname'] = trim($item['nickname']); $item['apply_user'] = trim($item['nickname']) . '-' . $item['user_mobile']; $item['create_time'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d H.i.s') : ''; $item['idcard_back_img'] = isset($item['idcard_back_img']) && $item['idcard_back_img'] ? get_image_url($item['idcard_back_img']) : ''; $item['idcard_front_img'] = isset($item['idcard_front_img']) && $item['idcard_front_img'] ? get_image_url($item['idcard_front_img']) : ''; $item['idcard_hand_img'] = isset($item['idcard_hand_img']) && $item['idcard_hand_img'] ? get_image_url($item['idcard_hand_img']) : ''; $item['city'] = []; if (isset($item['area']) && $item['area']) { $regions = AgentRegionModel::whereIn('name', [$item['province'], $item['area']])->orderBy('level', 'asc')->pluck('id'); $parentId = AgentRegionModel::where(['name' => $item['area']])->value('pid'); if ($regions) { $provinceId = isset($regions[0]) ? $regions[0] : 0; $cityId = isset($regions[1]) ? $regions[1] : 0; $hasChildren = AgentRegionModel::where(['pid' => $cityId])->value('id'); if (!$hasChildren) { $item['city'] = $parentId == $provinceId ? [$provinceId, $cityId] : [$provinceId, $parentId, $cityId]; } else { $item['city'] = $parentId == $provinceId ? [$provinceId, $cityId, $cityId] : [$provinceId, $parentId, $cityId]; } } } } } return [ 'pageSize' => $pageSize, 'total' => isset($list['total']) ? $list['total'] : 0, 'list' => isset($list['data']) ? $list['data'] : [] ]; } /** * 按日期统计注册司机数 * @param string $beginAt 开始时间 * @param string $endAt 结束时间 * @param int[] $status 状态:数组或数值 * @return mixed */ public function getRegisterCount($beginAt = '', $endAt = '', $status = [2, 4]) { $where = ['mark' => 1]; return $this->model->where($where)->where(function ($query) use ($beginAt, $endAt, $status) { if ($beginAt && $endAt) { $query->whereBetween('create_time', [strtotime($beginAt), strtotime($endAt)]); } else if ($beginAt) { $query->where('create_time', '>=', strtotime($beginAt)); } if ($status && is_array($status)) { $query->whereIn('status', $status); } else if ($status) { $query->where('status', $status); } })->count('id'); } /** * 用户选项 * @return array */ public function options() { // 获取参数 $param = request()->all(); // 用户ID $keyword = getter($param, "keyword"); $parentId = getter($param, "parent_id"); $userId = getter($param, "id"); $datas = $this->model->where(function ($query) use ($parentId) { if ($parentId) { $query->where(['id' => $parentId, 'mark' => 1]); } else { $query->where(['status' => 1, 'mark' => 1]); } }) ->where(function ($query) use ($userId) { if ($userId) { $query->whereNotIn('id', [$userId]); } }) ->where(function ($query) use ($keyword) { if ($keyword) { $query->where('realname', 'like', "%{$keyword}%")->orWhere('mobile', 'like', "%{$keyword}%"); } }) ->select(['id', 'area', 'mobile', 'realname', 'status']) ->get(); return $datas ? $datas->toArray() : []; } /** * 添加会编辑会员 * @return array * @since 2020/11/11 * @author laravel开发员 */ public function edit() { // 请求参数 $data = request()->all(); $id = isset($data['id']) ? $data['id'] : 0; $cityData = isset($data['city']) ? $data['city'] : []; if ($cityData) { if (count($cityData) > 2) { unset($cityData[1]); } $regions = AgentRegionModel::whereIn('id', $cityData)->orderBy('level', 'asc')->pluck('name'); $data['province'] = isset($regions[0]) ? $regions[0] : ''; $data['area'] = isset($regions[1]) ? $regions[1] : ''; $checkId = $this->model->where(['area' => $data['area'], 'status' => 2, 'mark' => 1])->value('id'); if ($data['area'] && $checkId && ($checkId != $id) && $data['status'] == 2) { return message("该区域【{$data['area']}】代理已存在", false); } } $info = $this->model->where(['id' => $id])->first(); // 设置日志标题 ActionLogModel::setTitle("审核或修改代理信息"); ActionLogModel::record(); unset($data['city']); unset($data['user']); DB::beginTransaction(); $result = parent::edit($data); // TODO: Change the autogenerated stub $success = isset($result['success']) ? $result['success'] : ''; $status = isset($info['status']) ? $info['status'] : 0; $deposit = isset($info['deposit']) ? $info['deposit'] : 0; $userId = isset($data['user_id'])? $data['user_id'] : 0; if ($success) { // 审核通过 if ($status != 2 && $data['status'] == 2) { if(!\App\Services\Api\FinanceService::make()->tzScoreAward($deposit, $userId, $data['id'], 2)){ DB::rollBack(); return message('审核失败', false); } } } DB::commit(); return $result; } }