where('province', '!=', 0) ->where('area_type',3); //管理员查看对应的代理 $district=AdminArea::getAdminArea(); if(!empty($district)){ if(!empty($district['city'])){ $proxy->whereCity($district['city']); }else{ $proxy->whereDistrict($district['district']); } } $res=$proxy->groupBy(['province', 'city', 'district']) ->orderByDesc('created_at') ->paginate(perPage()); if ($res->isNotEmpty()) { collect($res->items())->each(function ($item, $key) { $item->provinceMsg = Area::getName($item->province); $item->cityMsg = Area::getName($item->city); $item->districtMsg = Area::getName($item->district); }); } return showJsonSucc(1001, $res); } /** * 添加代理区域 * @author lyh * @date 2019/4/15 * @param Request $request * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response * @description */ public function add(Request $request) { $validator = \Validator::make($param = $request->all(), [ 'province' => 'required|exists:area,id', 'city' => 'required', 'district' => 'required', ]); if ($validator->fails()) { return showJsonErr($validator->errors()->first()); } if (\Auth::user()->is_super != 1) { return showJsonErr('抱歉,您没有权限添加代理地区'); } \DB::enableQueryLog(); // 区域类型 0-所有区域 1-省 2-市 3-区 4-个人 $areaType = 0; if (!empty($param['province'])) { $areaType = 1; } if (!empty($param['city'])) { $areaType = 2; } if (!empty($param['district'])) { $areaType = 3; } $proxy = Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district']); if ($proxy->exists()) { return showJsonErr('请勿重复添加该地区', $proxy->with('proxyInviteModel')->get()); } // 获取全局配置 $proxyConfig = Proxy::whereAreaType(0)->with('proxyInviteModel')->get(); if ($proxyConfig->isEmpty()) { return showJsonErr('系统问题,请联系技术添加初始配置'); } \DB::beginTransaction(); try { // 添加配置 collect($proxyConfig)->each(function ($pc, $pc_key) use ($areaType, $param) { //add by wsl start当前区域的G级代理费统一 $apply_money=0; //更改所有属于当前市的G级城市代理金额 $curproxy=Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereUserLevel(7)->first(); if(!empty($curproxy)&&$pc_key==6){ $apply_money=$curproxy->apply_money; } //add by wsl end $pc_id = Proxy::insertGetId([ 'uid' => 0, 'user_level' => $pc->user_level, 'province' => $param['province'], 'city' => $param['city'], 'district' => $param['district'], 'area_type' => $areaType,//区域类型 0-所有区域 1-省 2-市 3-区 4-个人 'upgrade_money' => $pc->upgrade_money, 'upgrade_business_month' => $pc->upgrade_business_month, 'upgrade_business_year' => $pc->upgrade_business_year, 'upgrade_invite' => $pc->upgrade_invite, 'proxy_invite' => $pc->proxy_invite, 'proxy_invite_global' => $pc->proxy_invite_global, 'adver_invite' => $pc->adver_invite, 'adver_invite_global' => $pc->adver_invite_global, 'money' => $pc->money, 'min_money' => $pc->min_money, 'coin' => $pc->coin, 'apply_money'=>$apply_money ]); collect($pc->proxyInviteModel)->each(function ($pi, $pi_key) use ($pc_id) { ProxyInvite::insert([ 'proxy_id' => $pc_id, 'title' => $pi->title, 'min' => $pi->min, 'max' => $pi->max, 'pct' => $pi->pct, 'type' => $pi->type, 'proxy_type' => $pi->proxy_type, 'position' => $pi->position ]); }); }); \DB::commit(); return showJsonSucc('添加代理地区成功', $proxy->with('proxyInviteModel')->get()); } catch (\Exception $exception) { \DB::rollBack(); return showJsonErr($exception->getMessage()); } } /** * 修改状态 * @author lyh * @date 2019/4/15 * @param Request $request * @description * 关闭后,用户将不能申请为该区域的代理(后台假删除)且已申请该城区代理的账号设为冻结状态 */ public function modifyStatus(Request $request) { $validator = \Validator::make($param = $request->all(), [ 'id' => 'required|exists:proxy,id', ]); if ($validator->fails()) { return showJsonErr($validator->errors()->first()); } $proxy = Proxy::whereId($param['id'])->where('area_type', '!=', 0)->first(); if (empty($proxy)) { return showJsonErr('该地区不能修改'); } \DB::beginTransaction(); try { $status = $proxy->status == 1 ? 0 : 1; $userStatus = $status == 1 ?: 2; $proxyConfig = Proxy::whereProvince($proxy->province)->whereCity($proxy->city); $user = User::whereProvince($proxy->province)->whereCity($proxy->city); if (!empty($proxy->district)) { $user->whereDistrict($proxy->district); $proxyConfig->whereDistrict($proxy->district); } // 更改地区设置状态 $proxyConfig->update(['status' => $status]); // 更改用户状态 $user->update(['status' => $userStatus]); \DB::commit(); return showJsonSucc('修改地区状态成功'); } catch (\Exception $exception) { \DB::rollBack(); return showJsonErr($exception->getMessage()); } } /** * 显示开放地区信息 * @author lyh * @date 2019/4/15 * @param Request $request * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response * @description */ public function show(Request $request) { $validator = \Validator::make($param = $request->all(), [ 'province' => 'required|exists:area,id', 'city' => 'required|exists:area,id', 'district' => 'required|integer', ]); if ($validator->fails()) { return showJsonErr($validator->errors()->first()); } if (\Auth::user()->is_super == 3) { if (\Auth::user()->province != $param['province'] || \Auth::user()->city != $param['city'] || \Auth::user()->district != $param['district']) { return showJsonErr('抱歉,该地区信息您不能修改'); } } $res = Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district'])->whereUid(0)->with('proxyInviteModel')->get(); if (empty($res)) { return showJsonErr('当前地区未开放'); } return showJsonSucc(1001, $res); } /** * 代理区设置-城区代理设置 * @author lyh * @date 2019/4/16 * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response * @description */ public function global() { $res = Proxy::whereAreaType(0)->with('proxyInviteModel')->get(); return showJsonSucc(1001, $res); } /** * [global_set 区代理设置-获取全局设置] * @author lgs * @DateTime 2019-04-30T09:47:13+0800 * @return [type] [全局设置的字段] */ public function global_set(Request $request) { $validator = \Validator::make($param = $request->all(), [ 'province' => 'required|integer', 'city' => 'required|integer', 'district' => 'required|integer', ]); if ($validator->fails()) { return showJsonErr($validator->errors()->first()); } $f = Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district'])->whereUid(0)->where('user_level',6)->select('proxy_invite_global as f_proxy_invite_global','adver_invite_global as f_adver_invite_global','min_money','money','guarantee_min_money')->first()->toArray();//F级区县代理分佣、广告分佣 $g = Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district'])->whereUid(0)->where('user_level',7)->select('proxy_invite_global as g_proxy_invite_global','adver_invite_global as g_adver_invite_global')->first()->toArray();//G级市代理分佣、广告分佣 $res = array_merge($f,$g); return showJsonSucc(1001, $res); } /** * [modify_global 城区代理全局修改] * @author lgs * @DateTime 2019-04-30T11:19:39+0800 * @return [type] [description] */ public function modify_global(Request $request){ $validator = \Validator::make($param = $request->all(), [ 'f_proxy_invite_global' => 'required|numeric', 'f_adver_invite_global' => 'required|numeric', 'money' => 'required|numeric', 'min_money' => 'required|numeric', 'g_proxy_invite_global' => 'required|numeric', 'g_adver_invite_global' => 'required|numeric', 'province' => 'required|integer', 'city' => 'required|integer', 'district' => 'required|integer', ]); if ($validator->fails()) { return showJsonErr($validator->errors()->first()); } \DB::beginTransaction(); try { Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district'])->whereUid(0)->where('user_level',6)->update([ 'proxy_invite_global' => $param['f_proxy_invite_global'], 'adver_invite_global' => $param['f_adver_invite_global'], ]); Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district'])->whereUid(0)->where('user_level',7)->update([ 'proxy_invite_global' => $param['g_proxy_invite_global'], 'adver_invite_global' => $param['g_adver_invite_global'], ]); Proxy::whereProvince($param['province'])->whereCity($param['city'])->whereDistrict($param['district'])->whereUid(0)->update([ 'money' => $param['money'], 'min_money' => $param['min_money'], 'guarantee_min_money' => empty($param['guarantee_min_money'])?0:$param['guarantee_min_money'], ]); \DB::commit(); return showJsonSucc('设置成功'); } catch (\Exception $e) { \DB::rollBack(); return showJsonErr($e->getMessage()); } } /** * 修改信息 * @author lyh * @date 2019/4/15 * @param Request $request * @description */ public function modify(Request $request) { $validator = \Validator::make($param = $request->all(), [ 'id' => 'required|exists:proxy,id', 'upgrade_money' => 'required|numeric', 'upgrade_business_month' => 'required|numeric', 'upgrade_business_year' => 'required|numeric', 'upgrade_invite' => 'required|numeric', 'proxy_invite' => 'required|numeric', 'proxy_invite_global' => 'required|numeric', 'adver_invite' => 'required|numeric', 'adver_invite_global' => 'required|numeric', 'money' => 'required|numeric', 'min_money' => 'required|numeric', 'coin' => 'required|numeric', 'proxyInvite' => 'array', 'apply_money' => 'required|numeric', //申请区县代理费用 ]); if ($validator->fails()) { return showJsonErr($validator->errors()->first()); } // 然后根据省市区id和等级查询出符合条件的数据(单条)然后进行数据更新操作 $proxyData = array_filter($param, function ($item, $key) { return in_array($key, [ 'upgrade_money', 'upgrade_business_month', 'upgrade_business_year', 'upgrade_invite', 'proxy_invite', 'proxy_invite_global', 'adver_invite', 'adver_invite_global', 'money', 'min_money', 'coin', 'apply_money', 'protocol' ]); }, ARRAY_FILTER_USE_BOTH); \DB::beginTransaction(); try { Proxy::whereId($param['id'])->update($proxyData); //addby wsl start // if(!empty($proxyData['apply_money'])&&$proxyData['apply_money']>0){ // //更改所有属于当前市的G级城市代理金额 // $curproxy=Proxy::whereId($param['id'])->first(); // $proxyGarr=Proxy::whereProvince($curproxy->province) // ->whereCity($curproxy->city)->whereUserLevel(7)->get(); // $parr=[]; // if(!empty($proxyGarr)){ // foreach ($proxyGarr as $gk=>$gv){ // $parr[]=$gv->id; // } // } // if(!empty($parr))Proxy::whereIn('id',$parr)->update(['apply_money'=>$param['apply_money']]); // } //addby wsl end // 更新代理区域百分比信息 foreach ($param['proxyInvite'] as $pi) { $proxyInvite = ProxyInvite::find($pi['id']); // if ($proxyInvite->position == 2 && ($pi['max'] < $pi['min'] || $pi['max'] < 0 || $pi['min'] < 0)) { // 区间范围 // return showJsonErr('请输入正确的数据'); // } else if ($proxyInvite->position == 1) { // 小于 // $pi['max'] = 0; // } else if ($proxyInvite->position == 3) { // 大于 // $pi['min'] = 0; // }else if ($proxyInvite->position == 4) { // $pi['max'] = 0; // } ProxyInvite::whereId($pi['id'])->update([ 'pct' => $pi['pct'], 'min' => $pi['min'], 'max' => $pi['max'], ]); } \DB::commit(); return showJsonSucc('设置成功'); } catch (\Exception $e) { \DB::rollBack(); return showJsonErr($e->getMessage()); } } /** * 设置用户配置 * @author lyh * @date 2019/4/9 * @param Request $request * @description */ public function setByUser(Request $request) { $validator = \Validator::make($param = $request->all(), [ 'id' => 'required|exists:user,id', 'coin' => 'numeric', 'type' => 'required|integer|between:0,2', //0-其他设置 1-代理 2-广告 'proxy' => 'required_if:type,1,2',// 直推 'proxy_one' => 'required_if:type,1,2', // 间一 'proxy_one_left' => 'required_if:type,1,2', // 间一 'proxy_one_right' => 'required_if:type,1,2', // 间一 'proxy_two' => 'required_if:type,1,2', // 间二 'proxy_two_left' => 'required_if:type,1,2', // 间二 'proxy_two_right' => 'required_if:type,1,2', // 间二 //'money' => 'numeric', // 广告计费金额 //'min_money' => 'numeric', // 广告起投 'free_num' => 'integer', // 免费次数 ]); if ($validator->fails()) { return showJsonErr($validator->errors()->first()); } $user = User::find($param['id']); $proxyData = []; // 点币汇率 if (isset($param['coin'])) { $proxyData['coin'] = $param['coin']; } // 广告每次/元 if (!empty($param['money'])) { // $proxyData['money'] = $param['money']; } //广告发布最低额度 if (!empty($param['min_money'])) { //$proxyData['min_money'] = $param['min_money']; } // 广告免费次数 if (!empty($param['free_num'])) { $proxyData['free_num'] = $param['free_num']; } // 代理、广告直推 if ($param['type'] == 2) { $proxyData['adver_invite'] = $param['proxy']; } else if ($param['type'] == 1) { $proxyData['proxy_invite'] = $param['proxy']; } \DB::beginTransaction(); try { $proxy = Proxy::whereUid($param['id'])->whereUserLevel($user->level)->first(); if ($proxy) { if (!!isset($proxyData)) { $result = Proxy::whereId($proxy->id)->update($proxyData); } $id = $proxy->id; } else { $proxyConfig = Proxy::getConfig($param['id']); $proxyOldData = [ 'uid' => $param['id'], 'user_level' => $proxyConfig->user_level, 'province' => $proxyConfig->province, 'city' => $proxyConfig->city, 'district' => $proxyConfig->district, //区域类型 0-所有区域 1-省 2-市 3-区 4-个人 'area_type' => 4, 'upgrade_money' => $proxyConfig->upgrade_money, 'upgrade_business_month' => $proxyConfig->upgrade_business_month, 'upgrade_business_year' => $proxyConfig->upgrade_business_year, 'upgrade_invite' => $proxyConfig->upgrade_invite, 'proxy_invite' => $proxyConfig->proxy_invite, 'proxy_invite_global' => $proxyConfig->proxy_invite_global, 'adver_invite' => $proxyConfig->adver_invite, 'adver_invite_global' => $proxyConfig->adver_invite_global, 'money' => $proxyConfig->money, 'min_money' => $proxyConfig->min_money, 'coin' => $proxyConfig->coin, 'free_num' => 0, // 广告免费次数不继承 ]; $proxyData = array_merge($proxyOldData, $proxyData); $result = $id = Proxy::insertGetId($proxyData); // 将广告/代理的直推/间一/间二的数据迁移成为独立的一份 $proxyInvite = $proxyConfig->invite; if (empty($proxyInvite)) { \DB::rollBack(); return showJsonErr('比例配置为空'); } foreach ($proxyInvite as $item) { $PIresult = ProxyInvite::insert([ 'proxy_id' => $id, 'title' => $item->title, 'min' => $item->min, 'max' => $item->max, 'pct' => $item->pct, 'type' => $item->type, 'proxy_type' => $item->type, 'position' => $item->position ]); if (!$PIresult) { \DB::rollBack(); return showJsonErr('迁移比例配置失败'); } } User::whereId($param['id'])->update(['commission_status' => 2]); } // 1-代理 2-广告 比例设置,则更新比例 if (in_array($param['type'], [1, 2])) { // 间推一 if ($param['proxy_one_left']) { ProxyInvite::whereProxyId($id)->where('position', '=', 1)->whereType(1)->update(['pct' => $param['proxy_one_left']]); } if ($param['proxy_one']) { ProxyInvite::whereProxyId($id)->where('position', '=', 2)->whereType(1)->update(['pct' => $param['proxy_one']]); } if ($param['proxy_one_right']) { ProxyInvite::whereProxyId($id)->where('position', '=', 3)->whereType(1)->update(['pct' => $param['proxy_one_right']]); } // 间推二 if ($param['proxy_one_left']) { ProxyInvite::whereProxyId($id)->where('position', '=', 1)->whereType(2)->update(['pct' => $param['proxy_one_left']]); } if ($param['proxy_one']) { ProxyInvite::whereProxyId($id)->where('position', '=', 2)->whereType(2)->update(['pct' => $param['proxy_one']]); } if ($param['proxy_one_right']) { ProxyInvite::whereProxyId($id)->where('position', '=', 3)->whereType(2)->update(['pct' => $param['proxy_one_right']]); } } if (empty($result)) { \DB::rollBack(); return showJsonErr('设置信息失败'); } \DB::commit(); return showJsonSucc('设置信息成功', Proxy::getConfig($param['id'])); } catch (\Exception $exception) { \DB::rollBack(); return showJsonErr($exception->getMessage()); } } }