Proxy.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <?php
  2. namespace App\Modes;
  3. use Carbon\Carbon;
  4. use ClassesWithParents\D;
  5. use Illuminate\Database\Eloquent\Model;
  6. use PhpParser\Error;
  7. /**
  8. * App\Modes\Proxy
  9. *
  10. * @property int $id
  11. * @property int $uid 用户id
  12. * @property int $user_level 等级(user_level)
  13. * @property int $province 省份(area.id)
  14. * @property int $city 市(area.id)
  15. * @property int $district 区(area.id)
  16. * @property int $area_type 区域类型 0-所有区域 1-省 2-市 3-区
  17. * @property float $upgrade_money 升级条件(四选一)-交费/w
  18. * @property float $upgrade_business_month 升级条件(四选一)-业务累积/月/w
  19. * @property float $upgrade_business_year 升级条件(四选一)-业务累积/年/w
  20. * @property float $upgrade_invite 升级条件(四选一)-推荐量
  21. * @property float $proxy_invite 代理直推/%
  22. * @property float $proxy_invite_global 代理全局/%
  23. * @property float $adver_invite 广告直推/%
  24. * @property float $adver_invite_global 广告全局/%
  25. * @property float $money 广告每次/元
  26. * @property float $min_money 广告发布最低额度
  27. * @property \Illuminate\Support\Carbon $created_at
  28. * @property \Illuminate\Support\Carbon $updated_at
  29. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy newModelQuery()
  30. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy newQuery()
  31. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy query()
  32. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereAdverInvite($value)
  33. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereAdverInviteGlobal($value)
  34. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereAreaType($value)
  35. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereCity($value)
  36. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereCreatedAt($value)
  37. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereDistrict($value)
  38. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereId($value)
  39. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereMinMoney($value)
  40. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereMoney($value)
  41. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereProvince($value)
  42. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereProxyInvite($value)
  43. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereProxyInviteGlobal($value)
  44. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUid($value)
  45. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUpdatedAt($value)
  46. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUpgradeBusinessMonth($value)
  47. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUpgradeBusinessYear($value)
  48. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUpgradeInvite($value)
  49. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUpgradeMoney($value)
  50. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereUserLevel($value)
  51. * @mixin \Eloquent
  52. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Modes\ProxyInvite[] $proxyInvite
  53. * @property string $order_id 订单id
  54. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereOrderId($value)
  55. * @property float $coin 点币汇率
  56. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereCoin($value)
  57. * @property int $status 是否冻结 1-否 0-是
  58. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereStatus($value)
  59. * @property int $free_num 免费次数
  60. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereFreeNum($value)
  61. * @property float $apply_money 申请代理金额(仅F/G级)
  62. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Modes\ProxyInvite[] $proxyInviteModel
  63. * @method static \Illuminate\Database\Eloquent\Builder|\App\Modes\Proxy whereApplyMoney($value)
  64. */
  65. class Proxy extends Model
  66. {
  67. protected $table = 'proxy';
  68. public static function getActivityFreeNum($uid,$province,$city,$district,$level){
  69. if($uid){//首先获取用户的
  70. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid($uid)->whereAreaType(4)->first();
  71. }
  72. if(empty($proxy->id)){
  73. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 3, 'province' => $province, 'city' => $city, 'district' => $district])->first();
  74. }
  75. $num = $proxy? $proxy->free_num : 0;
  76. $proxyUid = $proxy? $proxy->uid : 0;
  77. $free_num = 0; // 剩余免费次数
  78. // 有活动又有单独设置,按照单独设置为准
  79. // 有活动没有单独设置,按照活动为准
  80. if ($proxyUid == \Auth::id() && $num > 0) {
  81. $free_num = $num;
  82. //$free_num = $proxyConfig->free_num - AdverActivityLog::whereActivityId(0)->whereUid(\Auth::id())->whereStatus(3)->sum('free_num');
  83. } else {
  84. $exist = Advertising::whereUid($uid)->whereIn('status',[3,4,9])->where('activity_id','=',0)->exists();
  85. //不是首次发布,不能参与免费
  86. if ($exist) {
  87. return 0;
  88. }
  89. // 获取活动信息
  90. $now = Carbon::now();
  91. $activity = AdverActivity::where([
  92. ['start_time', '<', $now],
  93. ['end_time', '>', $now]
  94. ])->orderBy('created_at','desc')->first();
  95. if ($activity) {
  96. // 剩余次数
  97. //$free_num = $activity->free_num - AdverActivityLog::whereActivityId($activity->id)->whereUid(\Auth::id())->whereStatus(3)->sum('free_num');
  98. $free_num = $activity->free_num;
  99. }
  100. }
  101. return $free_num > 0 ? $free_num : 0;
  102. }
  103. /**
  104. * 根据地区获取配置信息
  105. * @author lyh
  106. * @date 2019/3/25
  107. * @param $uid
  108. * @param null $province
  109. * @param null $city
  110. * @param null $district
  111. * @param null $level
  112. * @return Proxy|\Illuminate\Database\Eloquent\Builder|Model|null|object
  113. * @description
  114. */
  115. public static function getConfig($uid, $province = null, $city = null, $district = null, $level = null)
  116. {
  117. $user = \DB::table('user')->where('id', $uid)->first();
  118. if (!empty($user->province)) {
  119. $province = $user->province;
  120. }
  121. if (!empty($user->city)) {
  122. $city = $user->city;
  123. }
  124. if (!empty($user->district)) {
  125. $district = $user->district;
  126. }
  127. $level = $level !== null ? $level : $user->level;
  128. // \DB::connection()->enableQueryLog();
  129. // 获取用户自定义配置
  130. if ($uid && $district == null&&$city == null) {
  131. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid($uid)->whereAreaType(4)->first();
  132. }
  133. // 获取县区代理配置
  134. if ($district != null && empty($proxy)) {
  135. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 3, 'province' => $province, 'city' => $city, 'district' => $district])->first();
  136. }
  137. // 获取地级市代理配置
  138. if ($city != null && empty($proxy)) {
  139. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 2, 'province' => $province, 'city' => $city])->first();
  140. }
  141. // 获取省级代理配置
  142. if ($proxy != null && empty($proxy)) {
  143. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 1, 'province' => $province])->first();
  144. }
  145. // 获取全局配置
  146. if (empty($proxy)) {
  147. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 0])->first();
  148. }
  149. if (empty($proxy)) {
  150. ErrorLog::saveMsg('获取地区代理配置失败', ['uid' => $user, 'province' => $province, 'city' => $city, 'district' => $district/*, 'queryLog' => \DB::getQueryLog()*/]);
  151. }
  152. $proxy->invite = \DB::table('proxy_invite')->where('proxy_id', '=', $proxy->id)->orderBy('max','desc')->get();
  153. return $proxy;
  154. }
  155. /*
  156. * 根据用户信息获取jian1jian2的百分比设置
  157. * author wsl
  158. * 20190627
  159. * $jiantype
  160. * */
  161. public static function getJianPct($userinfo,$jiantype=1,$proxy_type=2,$ids){
  162. $childcount=count($ids);
  163. //先找到等级配置
  164. $jiansetting=Proxy::getJian1OrJian2Setting($userinfo['level'],$userinfo['province'],$userinfo['city'],$userinfo['district'],$jiantype,$proxy_type);
  165. //获取当前间2配置的百分比
  166. if ($childcount <= $jiansetting[1]['max']) {
  167. $pct =$jiansetting[0]['pct'];
  168. } elseif ($childcount > $jiansetting[1]['max'] && $childcount < $jiansetting[2]['max']) {
  169. $pct = $jiansetting[1]['pct'];
  170. } else {
  171. $pct = $jiansetting[2]['pct'];
  172. }
  173. return $pct;
  174. }
  175. /*
  176. * 根据等级和所处区域查找配置
  177. * addby wsl 20190626
  178. *
  179. public static function getSettingByAreaAndLevel($level,$province,$city,$district){
  180. $setting=Proxy::where('user_level','=',$level)
  181. ->where('uid','=',0)
  182. ->where('province','=',$province)
  183. ->where('city','=',$city)
  184. ->where('district','=',$district)
  185. ->first();
  186. //如果对应区域设置为空的话则找全局配置
  187. if(empty($setting)){
  188. $setting=Proxy::where('user_level','=',$level)
  189. ->where('uid','=',0)
  190. ->where('province','=',0)
  191. ->where('city','=',0)
  192. ->where('district','=',0)
  193. ->first();
  194. }
  195. $ss=empty($setting)?array():$setting->toArray();
  196. return $ss;
  197. } */
  198. /*根据等级和所处区域查找区域,如果所处区域有空值,则全局配置补上
  199. author:wsl
  200. 20190704*/
  201. public static function getSettingByAreaAndLevel($level,$province,$city,$district){
  202. if($level>7){
  203. return array();
  204. }
  205. $areasetting=Proxy::whereUserLevel($level)->whereUid(0)->whereStatus(1)
  206. ->whereProvince($province)->whereCity($city)->whereDistrict($district)
  207. ->first();
  208. $globsetting=Proxy::whereUserLevel($level)->whereUid(0)
  209. ->whereProvince(0)->whereCity(0)->whereDistrict(0)
  210. ->first()->toArray();
  211. if(!empty($areasetting)){
  212. $areasetting=$areasetting->toArray();
  213. if(!empty($areasetting)){
  214. foreach ($areasetting as $key=>$item){
  215. if(empty($item)||$item=='0.000'||$item==null||$item=='0.00'||$item=='0.0'){
  216. $setting[$key]=$globsetting[$key];
  217. }else{
  218. $setting[$key]=$item;
  219. }
  220. }
  221. }else $setting=$globsetting;
  222. }else{
  223. $setting=$globsetting;
  224. }
  225. return $setting;
  226. }
  227. /*
  228. * 获取间1或者间2配置
  229. * addbywsl
  230. * 20190626
  231. *
  232. public static function getJian1OrJian2Setting($level,$province=0,$city=0,$district=0,$jiantype=1,$proxy_type=2){
  233. $jiansetting=ProxyInvite::leftJoin('proxy','proxy_invite.proxy_id','proxy.id')
  234. ->select('proxy_invite.*','proxy.id')
  235. ->where('proxy.user_level','=',$level)
  236. ->where('proxy.province','=',$province)
  237. ->where('proxy.city','=',$city)
  238. ->where('proxy.district','=',$district)
  239. ->where('proxy_invite.type','=',$jiantype)
  240. ->where('proxy_invite.proxy_type','=',$proxy_type)
  241. ->orderBy('proxy_invite.id','asc')
  242. ->get()->toArray();
  243. if(empty($jiansetting)){
  244. $jiansetting=ProxyInvite::leftJoin('proxy','proxy_invite.proxy_id','proxy.id')
  245. ->select('proxy_invite.*')
  246. ->where('proxy.user_level','=',$level)
  247. ->where('proxy.province','=',0)
  248. ->where('proxy.city','=',0)
  249. ->where('proxy.district','=',0)
  250. ->where('proxy_invite.type','=',$jiantype)
  251. ->where('proxy_invite.proxy_type','=',$proxy_type)
  252. ->orderBy('proxy_invite.id','asc')
  253. ->get()->toArray();
  254. }
  255. return $jiansetting;
  256. }*/
  257. /*
  258. * 获取间1或者间2配置,当配置为0时就用全局配置
  259. * addbywsl
  260. * 20190703
  261. * */
  262. public static function getJian1OrJian2Setting($level,$province=0,$city=0,$district=0,$jiantype=1,$proxy_type=2){
  263. $setting=[];
  264. $jiansetting=ProxyInvite::leftJoin('proxy','proxy_invite.proxy_id','proxy.id')
  265. ->select('proxy_invite.*','proxy.id')
  266. ->where('proxy.user_level','=',$level)
  267. ->where('proxy.province','=',$province)
  268. ->where('proxy.city','=',$city)
  269. ->where('proxy.uid','=',0)
  270. ->where('proxy.status','=',1)
  271. ->where('proxy.district','=',$district)
  272. ->where('proxy_invite.type','=',$jiantype)
  273. ->where('proxy_invite.proxy_type','=',$proxy_type)
  274. ->orderBy('proxy_invite.id','asc')
  275. ->get()->toArray();
  276. $globaljiansetting=ProxyInvite::leftJoin('proxy','proxy_invite.proxy_id','proxy.id')
  277. ->select('proxy_invite.*')
  278. ->where('proxy.user_level','=',$level)
  279. ->where('proxy.province','=',0)
  280. ->where('proxy.city','=',0)
  281. ->where('proxy.uid','=',0)
  282. ->where('proxy.district','=',0)
  283. ->where('proxy_invite.type','=',$jiantype)
  284. ->where('proxy_invite.proxy_type','=',$proxy_type)
  285. ->orderBy('proxy_invite.id','asc')
  286. ->get()->toArray();
  287. if(!empty($jiansetting)){
  288. foreach ($jiansetting as $key=>$item){
  289. foreach ($item as $k=>$v){
  290. if(empty($v)||$v=='0.000'||$v==null||$v=='0.00'||$v=='0.0'){
  291. $setting[$key][$k]=$globaljiansetting[$key][$k];
  292. }else{
  293. $setting[$key][$k]=$v;
  294. }
  295. }
  296. }
  297. }else{
  298. $setting=$globaljiansetting;
  299. }
  300. return $setting;
  301. }
  302. public static function getApplyMoneyByConfig($province = null, $city = null, $district = null, $level = 6)
  303. {
  304. $proxy = '';
  305. // 获取县区代理配置
  306. if ($district != null && empty($proxy)) {
  307. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid(0)->where(['area_type' => 3, 'province' => $province, 'city' => $city, 'district' => $district])->first();
  308. }
  309. // 获取地级市代理配置
  310. if ($city != null && empty($proxy)) {
  311. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid(0)->where(['area_type' => 2, 'province' => $province, 'city' => $city])->first();
  312. }
  313. // 获取省级代理配置
  314. if ($province != null && empty($proxy)) {
  315. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid(0)->where(['area_type' => 1, 'province' => $province])->first();
  316. }
  317. $globalproxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid(0)->where(['area_type' => 0])->first()->toArray();
  318. if(empty($proxy)){
  319. $proxy=$globalproxy;
  320. }else{
  321. $proxy=$proxy->toArray();
  322. foreach($proxy as $k=>$item){
  323. if(empty($item)||$item=='0.0'||$item=='0.00'||$item=='0.000'||$item==null){
  324. $proxy[$k]=$globalproxy[$k];
  325. }
  326. }
  327. }
  328. if (empty($proxy)) {
  329. ErrorLog::saveMsg('获取地区代理配置失败', ['province' => $province, 'city' => $city, 'district' => $district/*, 'queryLog' => \DB::getQueryLog()*/]);
  330. }
  331. return $proxy['apply_money'];
  332. }
  333. /**
  334. * 获取直推,间推1,间推2返佣比
  335. * @author lyh
  336. * @date 2019/3/27
  337. * @param int $id
  338. * @param int $total 人数
  339. * @param int $type 类型 1-间推1 2-间推2
  340. * @param int $proxyType 类型 1-代理 2-广告
  341. * @description
  342. */
  343. public
  344. static function getProxyInvite(int $id, int $total, $type = 0, $proxyType = 1)
  345. {
  346. $config = Proxy::getConfig($id);
  347. if ($type == 0) {
  348. return $config->proxy_invite;
  349. } else {
  350. $proxyInvite = $config->invite;
  351. foreach ($proxyInvite as $item) {
  352. if ($item->type == $type && $proxyType == $item->proxy_type) {
  353. if ($total >= $item->max) {
  354. return $item->pct;
  355. }
  356. }
  357. }
  358. return 0;
  359. }
  360. }
  361. /**
  362. * 获取广告剩余免费次数
  363. * @author lyh
  364. * @date 2019/4/22
  365. * @param int $id
  366. * @param $proxyConfig
  367. * @return int|mixed
  368. * @description
  369. */
  370. public
  371. static function getFreeNum(int $id, $proxyConfig)
  372. {
  373. $free_num = 0; // 剩余免费次数
  374. // 有活动又有单独设置,按照单独设置为准
  375. // 有活动没有单独设置,按照活动为准
  376. if ($proxyConfig->uid = \Auth::id() && $proxyConfig->free_num > 0) {
  377. $free_num = $proxyConfig->free_num;
  378. //$free_num = $proxyConfig->free_num - AdverActivityLog::whereActivityId(0)->whereUid(\Auth::id())->whereStatus(3)->sum('free_num');
  379. } else {
  380. $exist = Advertising::whereUid($id)->whereIn('status',[3,4,9])->where('activity_id','=',0)->exists();
  381. //不是首次发布,不能参与免费
  382. if ($exist) {
  383. return 0;
  384. }
  385. // 获取活动信息
  386. $now = Carbon::now();
  387. $activity = AdverActivity::where([
  388. ['start_time', '<', $now],
  389. ['end_time', '>', $now]
  390. ])->orderBy('created_at','desc')->first();
  391. if ($activity) {
  392. // 剩余次数
  393. //$free_num = $activity->free_num - AdverActivityLog::whereActivityId($activity->id)->whereUid(\Auth::id())->whereStatus(3)->sum('free_num');
  394. $free_num = $activity->free_num;
  395. }
  396. }
  397. return $free_num > 0 ? $free_num : 0;
  398. }
  399. public
  400. function proxyInviteModel()
  401. {
  402. return $this->hasMany('App\Modes\ProxyInvite', 'proxy_id', 'id');
  403. }
  404. }