| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- <?php
- namespace App\Modes;
- use Carbon\Carbon;
- use ClassesWithParents\D;
- use Illuminate\Database\Eloquent\Model;
- use PhpParser\Error;
- /**
- * App\Modes\Proxy
- *
- * @property int $id
- * @property int $uid 用户id
- * @property int $user_level 等级(user_level)
- * @property int $province 省份(area.id)
- * @property int $city 市(area.id)
- * @property int $district 区(area.id)
- * @property int $area_type 区域类型 0-所有区域 1-省 2-市 3-区
- * @property float $upgrade_money 升级条件(四选一)-交费/w
- * @property float $upgrade_business_month 升级条件(四选一)-业务累积/月/w
- * @property float $upgrade_business_year 升级条件(四选一)-业务累积/年/w
- * @property float $upgrade_invite 升级条件(四选一)-推荐量
- * @property float $proxy_invite 代理直推/%
- * @property float $proxy_invite_global 代理全局/%
- * @property float $adver_invite 广告直推/%
- * @property float $adver_invite_global 广告全局/%
- * @property float $money 广告每次/元
- * @property float $min_money 广告发布最低额度
- * @property \Illuminate\Support\Carbon $created_at
- * @property \Illuminate\Support\Carbon $updated_at
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy newModelQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy newQuery()
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy query()
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereAdverInvite($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereAdverInviteGlobal($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereAreaType($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereCity($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereCreatedAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereDistrict($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereId($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereMinMoney($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereMoney($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereProvince($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereProxyInvite($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereProxyInviteGlobal($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUid($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUpdatedAt($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUpgradeBusinessMonth($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUpgradeBusinessYear($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUpgradeInvite($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUpgradeMoney($value)
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUserLevel($value)
- * @mixin \Eloquent
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Modes\ProxyInvite[] $proxyInvite
- * @property string $order_id 订单id
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereOrderId($value)
- * @property float $coin 点币汇率
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereCoin($value)
- * @property int $status 是否冻结 1-否 0-是
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereStatus($value)
- * @property int $free_num 免费次数
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereFreeNum($value)
- * @property float $apply_money 申请代理金额(仅F/G级)
- * @property-read \Illuminate\Database\Eloquent\Collection|\App\Modes\ProxyInvite[] $proxyInviteModel
- * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereApplyMoney($value)
- */
- class Proxy extends Model
- {
- protected $table = 'proxy';
- public static function getActivityFreeNum($uid,$province,$city,$district,$level){
- if($uid){//首先获取用户的
- $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid($uid)->whereAreaType(4)->first();
- }
- if(empty($proxy->id)){
- $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 3, 'province' => $province, 'city' => $city, 'district' => $district])->first();
- }
- $num = $proxy? $proxy->free_num : 0;
- $proxyUid = $proxy? $proxy->uid : 0;
- $free_num = 0; // 剩余免费次数
- // 有活动又有单独设置,按照单独设置为准
- // 有活动没有单独设置,按照活动为准
- if ($proxyUid == \Auth::id() && $num > 0) {
- $free_num = $num;
- //$free_num = $proxyConfig->free_num - AdverActivityLog::whereActivityId(0)->whereUid(\Auth::id())->whereStatus(3)->sum('free_num');
- } else {
- $exist = Advertising::whereUid($uid)->whereIn('status',[3,4,9])->where('activity_id','=',0)->exists();
- //不是首次发布,不能参与免费
- if ($exist) {
- return 0;
- }
- // 获取活动信息
- $now = Carbon::now();
- $activity = AdverActivity::where([
- ['start_time', '<', $now],
- ['end_time', '>', $now]
- ])->orderBy('created_at','desc')->first();
- if ($activity) {
- // 剩余次数
- //$free_num = $activity->free_num - AdverActivityLog::whereActivityId($activity->id)->whereUid(\Auth::id())->whereStatus(3)->sum('free_num');
- $free_num = $activity->free_num;
- }
- }
- return $free_num > 0 ? $free_num : 0;
- }
- /**
- * 根据地区获取配置信息
- * @author lyh
- * @date 2019/3/25
- * @param $uid
- * @param null $province
- * @param null $city
- * @param null $district
- * @param null $level
- * @return Proxy|\Illuminate\Database\Eloquent\Builder|Model|null|object
- * @description
- */
- public static function getConfig($uid, $province = null, $city = null, $district = null, $level = null)
- {
- $user = \DB::table('user')->where('id', $uid)->first();
- if (!empty($user->province)) {
- $province = $user->province;
- }
- if (!empty($user->city)) {
- $city = $user->city;
- }
- if (!empty($user->district)) {
- $district = $user->district;
- }
- $level = $level !== null ? $level : $user->level;
-
- // \DB::connection()->enableQueryLog();
- // 获取用户自定义配置
- if ($uid && $district == null&&$city == null) {
- $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid($uid)->whereAreaType(4)->first();
- }
- // 获取县区代理配置
- if ($district != null && empty($proxy)) {
- $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 3, 'province' => $province, 'city' => $city, 'district' => $district])->first();
- }
- // 获取地级市代理配置
- if ($city != null && empty($proxy)) {
- $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 2, 'province' => $province, 'city' => $city])->first();
- }
- // 获取省级代理配置
- if ($proxy != null && empty($proxy)) {
- $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 1, 'province' => $province])->first();
- }
- // 获取全局配置
- if (empty($proxy)) {
- $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 0])->first();
- }
- if (empty($proxy)) {
- ErrorLog::saveMsg('获取地区代理配置失败', ['uid' => $user, 'province' => $province, 'city' => $city, 'district' => $district/*, 'queryLog' => \DB::getQueryLog()*/]);
- }
- $proxy->invite = \DB::table('proxy_invite')->where('proxy_id', '=', $proxy->id)->orderBy('max','desc')->get();
- return $proxy;
- }
- /*
- * 根据用户信息获取jian1jian2的百分比设置
- * author wsl
- * 20190627
- * $jiantype
- * */
- public static function getJianPct($userinfo,$jiantype=1,$proxy_type=2,$ids){
- $childcount=count($ids);
- //先找到等级配置
- $jiansetting=Proxy::getJian1OrJian2Setting($userinfo['level'],$userinfo['province'],$userinfo['city'],$userinfo['district'],$jiantype,$proxy_type);
- //获取当前间2配置的百分比
- if ($childcount <= $jiansetting[1]['max']) {
- $pct =$jiansetting[0]['pct'];
- } elseif ($childcount > $jiansetting[1]['max'] && $childcount < $jiansetting[2]['max']) {
- $pct = $jiansetting[1]['pct'];
- } else {
- $pct = $jiansetting[2]['pct'];
- }
- return $pct;
- }
- /*
- * 根据等级和所处区域查找配置
- * addby wsl 20190626
- *
- public static function getSettingByAreaAndLevel($level,$province,$city,$district){
- $setting=Proxy::where('user_level','=',$level)
- ->where('uid','=',0)
- ->where('province','=',$province)
- ->where('city','=',$city)
- ->where('district','=',$district)
- ->first();
- //如果对应区域设置为空的话则找全局配置
- if(empty($setting)){
- $setting=Proxy::where('user_level','=',$level)
- ->where('uid','=',0)
- ->where('province','=',0)
- ->where('city','=',0)
- ->where('district','=',0)
- ->first();
- }
- $ss=empty($setting)?array():$setting->toArray();
- return $ss;
- } */
- /*根据等级和所处区域查找区域,如果所处区域有空值,则全局配置补上
- author:wsl
- 20190704*/
- public static function getSettingByAreaAndLevel($level,$province,$city,$district){
- if($level>7){
- return array();
- }
- $areasetting=Proxy::whereUserLevel($level)->whereUid(0)->whereStatus(1)
- ->whereProvince($province)->whereCity($city)->whereDistrict($district)
- ->first();
- $globsetting=Proxy::whereUserLevel($level)->whereUid(0)
- ->whereProvince(0)->whereCity(0)->whereDistrict(0)
- ->first()->toArray();
- if(!empty($areasetting)){
- $areasetting=$areasetting->toArray();
- if(!empty($areasetting)){
- foreach ($areasetting as $key=>$item){
- if(empty($item)||$item=='0.000'||$item==null||$item=='0.00'||$item=='0.0'){
- $setting[$key]=$globsetting[$key];
- }else{
- $setting[$key]=$item;
- }
- }
- }else $setting=$globsetting;
- }else{
- $setting=$globsetting;
- }
- return $setting;
- }
- /*
- * 获取间1或者间2配置
- * addbywsl
- * 20190626
- *
- public static function getJian1OrJian2Setting($level,$province=0,$city=0,$district=0,$jiantype=1,$proxy_type=2){
- $jiansetting=ProxyInvite::leftJoin('proxy','proxy_invite.proxy_id','proxy.id')
- ->select('proxy_invite.*','proxy.id')
- ->where('proxy.user_level','=',$level)
- ->where('proxy.province','=',$province)
- ->where('proxy.city','=',$city)
- ->where('proxy.district','=',$district)
- ->where('proxy_invite.type','=',$jiantype)
- ->where('proxy_invite.proxy_type','=',$proxy_type)
- ->orderBy('proxy_invite.id','asc')
- ->get()->toArray();
- if(empty($jiansetting)){
- $jiansetting=ProxyInvite::leftJoin('proxy','proxy_invite.proxy_id','proxy.id')
- ->select('proxy_invite.*')
- ->where('proxy.user_level','=',$level)
- ->where('proxy.province','=',0)
- ->where('proxy.city','=',0)
- ->where('proxy.district','=',0)
- ->where('proxy_invite.type','=',$jiantype)
- ->where('proxy_invite.proxy_type','=',$proxy_type)
- ->orderBy('proxy_invite.id','asc')
- ->get()->toArray();
- }
- return $jiansetting;
- }*/
- /*
- * 获取间1或者间2配置,当配置为0时就用全局配置
- * addbywsl
- * 20190703
- * */
- public static function getJian1OrJian2Setting($level,$province=0,$city=0,$district=0,$jiantype=1,$proxy_type=2){
- $setting=[];
- $jiansetting=ProxyInvite::leftJoin('proxy','proxy_invite.proxy_id','proxy.id')
- ->select('proxy_invite.*','proxy.id')
- ->where('proxy.user_level','=',$level)
- ->where('proxy.province','=',$province)
- ->where('proxy.city','=',$city)
- ->where('proxy.uid','=',0)
- ->where('proxy.status','=',1)
- ->where('proxy.district','=',$district)
- ->where('proxy_invite.type','=',$jiantype)
- ->where('proxy_invite.proxy_type','=',$proxy_type)
- ->orderBy('proxy_invite.id','asc')
- ->get()->toArray();
- $globaljiansetting=ProxyInvite::leftJoin('proxy','proxy_invite.proxy_id','proxy.id')
- ->select('proxy_invite.*')
- ->where('proxy.user_level','=',$level)
- ->where('proxy.province','=',0)
- ->where('proxy.city','=',0)
- ->where('proxy.uid','=',0)
- ->where('proxy.district','=',0)
- ->where('proxy_invite.type','=',$jiantype)
- ->where('proxy_invite.proxy_type','=',$proxy_type)
- ->orderBy('proxy_invite.id','asc')
- ->get()->toArray();
- if(!empty($jiansetting)){
- foreach ($jiansetting as $key=>$item){
- foreach ($item as $k=>$v){
- if(empty($v)||$v=='0.000'||$v==null||$v=='0.00'||$v=='0.0'){
- $setting[$key][$k]=$globaljiansetting[$key][$k];
- }else{
- $setting[$key][$k]=$v;
- }
- }
- }
- }else{
- $setting=$globaljiansetting;
- }
- return $setting;
- }
- public static function getApplyMoneyByConfig($province = null, $city = null, $district = null, $level = 6)
- {
- $proxy = '';
- // 获取县区代理配置
- if ($district != null && empty($proxy)) {
- $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid(0)->where(['area_type' => 3, 'province' => $province, 'city' => $city, 'district' => $district])->first();
- }
- // 获取地级市代理配置
- if ($city != null && empty($proxy)) {
- $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid(0)->where(['area_type' => 2, 'province' => $province, 'city' => $city])->first();
- }
- // 获取省级代理配置
- if ($province != null && empty($proxy)) {
- $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid(0)->where(['area_type' => 1, 'province' => $province])->first();
- }
- $globalproxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid(0)->where(['area_type' => 0])->first()->toArray();
- if(empty($proxy)){
- $proxy=$globalproxy;
- }else{
- $proxy=$proxy->toArray();
- foreach($proxy as $k=>$item){
- if(empty($item)||$item=='0.0'||$item=='0.00'||$item=='0.000'||$item==null){
- $proxy[$k]=$globalproxy[$k];
- }
- }
- }
- if (empty($proxy)) {
- ErrorLog::saveMsg('获取地区代理配置失败', ['province' => $province, 'city' => $city, 'district' => $district/*, 'queryLog' => \DB::getQueryLog()*/]);
- }
- return $proxy['apply_money'];
- }
- /**
- * 获取直推,间推1,间推2返佣比
- * @author lyh
- * @date 2019/3/27
- * @param int $id
- * @param int $total 人数
- * @param int $type 类型 1-间推1 2-间推2
- * @param int $proxyType 类型 1-代理 2-广告
- * @description
- */
- public
- static function getProxyInvite(int $id, int $total, $type = 0, $proxyType = 1)
- {
- $config = Proxy::getConfig($id);
- if ($type == 0) {
- return $config->proxy_invite;
- } else {
- $proxyInvite = $config->invite;
- foreach ($proxyInvite as $item) {
- if ($item->type == $type && $proxyType == $item->proxy_type) {
- if ($total >= $item->max) {
- return $item->pct;
- }
- }
- }
- return 0;
- }
- }
- /**
- * 获取广告剩余免费次数
- * @author lyh
- * @date 2019/4/22
- * @param int $id
- * @param $proxyConfig
- * @return int|mixed
- * @description
- */
- public
- static function getFreeNum(int $id, $proxyConfig)
- {
- $free_num = 0; // 剩余免费次数
- // 有活动又有单独设置,按照单独设置为准
- // 有活动没有单独设置,按照活动为准
- if ($proxyConfig->uid = \Auth::id() && $proxyConfig->free_num > 0) {
- $free_num = $proxyConfig->free_num;
- //$free_num = $proxyConfig->free_num - AdverActivityLog::whereActivityId(0)->whereUid(\Auth::id())->whereStatus(3)->sum('free_num');
- } else {
- $exist = Advertising::whereUid($id)->whereIn('status',[3,4,9])->where('activity_id','=',0)->exists();
- //不是首次发布,不能参与免费
- if ($exist) {
- return 0;
- }
- // 获取活动信息
- $now = Carbon::now();
- $activity = AdverActivity::where([
- ['start_time', '<', $now],
- ['end_time', '>', $now]
- ])->orderBy('created_at','desc')->first();
- if ($activity) {
- // 剩余次数
- //$free_num = $activity->free_num - AdverActivityLog::whereActivityId($activity->id)->whereUid(\Auth::id())->whereStatus(3)->sum('free_num');
- $free_num = $activity->free_num;
- }
- }
- return $free_num > 0 ? $free_num : 0;
- }
- public
- function proxyInviteModel()
- {
- return $this->hasMany('App\Modes\ProxyInvite', 'proxy_id', 'id');
- }
- }
|