Proxy.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. $free_num = 0; // 剩余免费次数
  76. // 有活动又有单独设置,按照单独设置为准
  77. // 有活动没有单独设置,按照活动为准
  78. if ($proxy->uid = \Auth::id() && $proxy->free_num > 0) {
  79. $free_num = $proxy? $proxy->free_num : 0;
  80. //$free_num = $proxyConfig->free_num - AdverActivityLog::whereActivityId(0)->whereUid(\Auth::id())->whereStatus(3)->sum('free_num');
  81. } else {
  82. $exist = Advertising::whereUid($uid)->whereIn('status',[3,4,9])->where('activity_id','=',0)->exists();
  83. //不是首次发布,不能参与免费
  84. if ($exist) {
  85. return 0;
  86. }
  87. // 获取活动信息
  88. $now = Carbon::now();
  89. $activity = AdverActivity::where([
  90. ['start_time', '<', $now],
  91. ['end_time', '>', $now]
  92. ])->orderBy('created_at','desc')->first();
  93. if ($activity) {
  94. // 剩余次数
  95. //$free_num = $activity->free_num - AdverActivityLog::whereActivityId($activity->id)->whereUid(\Auth::id())->whereStatus(3)->sum('free_num');
  96. $free_num = $activity->free_num;
  97. }
  98. }
  99. return $free_num > 0 ? $free_num : 0;
  100. }
  101. /**
  102. * 根据地区获取配置信息
  103. * @author lyh
  104. * @date 2019/3/25
  105. * @param $uid
  106. * @param null $province
  107. * @param null $city
  108. * @param null $district
  109. * @param null $level
  110. * @return Proxy|\Illuminate\Database\Eloquent\Builder|Model|null|object
  111. * @description
  112. */
  113. public static function getConfig($uid, $province = null, $city = null, $district = null, $level = null)
  114. {
  115. $user = \DB::table('user')->where('id', $uid)->first();
  116. if (!empty($user->province)) {
  117. $province = $user->province;
  118. }
  119. if (!empty($user->city)) {
  120. $city = $user->city;
  121. }
  122. if (!empty($user->district)) {
  123. $district = $user->district;
  124. }
  125. $level = $level !== null ? $level : $user->level;
  126. // \DB::connection()->enableQueryLog();
  127. // 获取用户自定义配置
  128. if ($uid && $district == null&&$city == null) {
  129. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid($uid)->whereAreaType(4)->first();
  130. }
  131. // 获取县区代理配置
  132. if ($district != null && empty($proxy)) {
  133. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 3, 'province' => $province, 'city' => $city, 'district' => $district])->first();
  134. }
  135. // 获取地级市代理配置
  136. if ($city != null && empty($proxy)) {
  137. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 2, 'province' => $province, 'city' => $city])->first();
  138. }
  139. // 获取省级代理配置
  140. if ($proxy != null && empty($proxy)) {
  141. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 1, 'province' => $province])->first();
  142. }
  143. // 获取全局配置
  144. if (empty($proxy)) {
  145. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->where(['area_type' => 0])->first();
  146. }
  147. if (empty($proxy)) {
  148. ErrorLog::saveMsg('获取地区代理配置失败', ['uid' => $user, 'province' => $province, 'city' => $city, 'district' => $district/*, 'queryLog' => \DB::getQueryLog()*/]);
  149. }
  150. $proxy->invite = \DB::table('proxy_invite')->where('proxy_id', '=', $proxy->id)->orderBy('max','desc')->get();
  151. return $proxy;
  152. }
  153. /*
  154. * 根据用户信息获取jian1jian2的百分比设置
  155. * author wsl
  156. * 20190627
  157. * $jiantype
  158. * */
  159. public static function getJianPct($userinfo,$jiantype=1,$proxy_type=2,$ids){
  160. $childcount=count($ids);
  161. //先找到等级配置
  162. $jiansetting=Proxy::getJian1OrJian2Setting($userinfo['level'],$userinfo['province'],$userinfo['city'],$userinfo['district'],$jiantype,$proxy_type);
  163. //获取当前间2配置的百分比
  164. if ($childcount <= $jiansetting[1]['max']) {
  165. $pct =$jiansetting[0]['pct'];
  166. } elseif ($childcount > $jiansetting[1]['max'] && $childcount < $jiansetting[2]['max']) {
  167. $pct = $jiansetting[1]['pct'];
  168. } else {
  169. $pct = $jiansetting[2]['pct'];
  170. }
  171. return $pct;
  172. }
  173. /*
  174. * 根据等级和所处区域查找配置
  175. * addby wsl 20190626
  176. *
  177. public static function getSettingByAreaAndLevel($level,$province,$city,$district){
  178. $setting=Proxy::where('user_level','=',$level)
  179. ->where('uid','=',0)
  180. ->where('province','=',$province)
  181. ->where('city','=',$city)
  182. ->where('district','=',$district)
  183. ->first();
  184. //如果对应区域设置为空的话则找全局配置
  185. if(empty($setting)){
  186. $setting=Proxy::where('user_level','=',$level)
  187. ->where('uid','=',0)
  188. ->where('province','=',0)
  189. ->where('city','=',0)
  190. ->where('district','=',0)
  191. ->first();
  192. }
  193. $ss=empty($setting)?array():$setting->toArray();
  194. return $ss;
  195. } */
  196. /*根据等级和所处区域查找区域,如果所处区域有空值,则全局配置补上
  197. author:wsl
  198. 20190704*/
  199. public static function getSettingByAreaAndLevel($level,$province,$city,$district){
  200. if($level>7){
  201. return array();
  202. }
  203. $areasetting=Proxy::whereUserLevel($level)->whereUid(0)->whereStatus(1)
  204. ->whereProvince($province)->whereCity($city)->whereDistrict($district)
  205. ->first();
  206. $globsetting=Proxy::whereUserLevel($level)->whereUid(0)
  207. ->whereProvince(0)->whereCity(0)->whereDistrict(0)
  208. ->first()->toArray();
  209. if(!empty($areasetting)){
  210. $areasetting=$areasetting->toArray();
  211. if(!empty($areasetting)){
  212. foreach ($areasetting as $key=>$item){
  213. if(empty($item)||$item=='0.000'||$item==null||$item=='0.00'||$item=='0.0'){
  214. $setting[$key]=$globsetting[$key];
  215. }else{
  216. $setting[$key]=$item;
  217. }
  218. }
  219. }else $setting=$globsetting;
  220. }else{
  221. $setting=$globsetting;
  222. }
  223. return $setting;
  224. }
  225. /*
  226. * 获取间1或者间2配置
  227. * addbywsl
  228. * 20190626
  229. *
  230. public static function getJian1OrJian2Setting($level,$province=0,$city=0,$district=0,$jiantype=1,$proxy_type=2){
  231. $jiansetting=ProxyInvite::leftJoin('proxy','proxy_invite.proxy_id','proxy.id')
  232. ->select('proxy_invite.*','proxy.id')
  233. ->where('proxy.user_level','=',$level)
  234. ->where('proxy.province','=',$province)
  235. ->where('proxy.city','=',$city)
  236. ->where('proxy.district','=',$district)
  237. ->where('proxy_invite.type','=',$jiantype)
  238. ->where('proxy_invite.proxy_type','=',$proxy_type)
  239. ->orderBy('proxy_invite.id','asc')
  240. ->get()->toArray();
  241. if(empty($jiansetting)){
  242. $jiansetting=ProxyInvite::leftJoin('proxy','proxy_invite.proxy_id','proxy.id')
  243. ->select('proxy_invite.*')
  244. ->where('proxy.user_level','=',$level)
  245. ->where('proxy.province','=',0)
  246. ->where('proxy.city','=',0)
  247. ->where('proxy.district','=',0)
  248. ->where('proxy_invite.type','=',$jiantype)
  249. ->where('proxy_invite.proxy_type','=',$proxy_type)
  250. ->orderBy('proxy_invite.id','asc')
  251. ->get()->toArray();
  252. }
  253. return $jiansetting;
  254. }*/
  255. /*
  256. * 获取间1或者间2配置,当配置为0时就用全局配置
  257. * addbywsl
  258. * 20190703
  259. * */
  260. public static function getJian1OrJian2Setting($level,$province=0,$city=0,$district=0,$jiantype=1,$proxy_type=2){
  261. $setting=[];
  262. $jiansetting=ProxyInvite::leftJoin('proxy','proxy_invite.proxy_id','proxy.id')
  263. ->select('proxy_invite.*','proxy.id')
  264. ->where('proxy.user_level','=',$level)
  265. ->where('proxy.province','=',$province)
  266. ->where('proxy.city','=',$city)
  267. ->where('proxy.uid','=',0)
  268. ->where('proxy.status','=',1)
  269. ->where('proxy.district','=',$district)
  270. ->where('proxy_invite.type','=',$jiantype)
  271. ->where('proxy_invite.proxy_type','=',$proxy_type)
  272. ->orderBy('proxy_invite.id','asc')
  273. ->get()->toArray();
  274. $globaljiansetting=ProxyInvite::leftJoin('proxy','proxy_invite.proxy_id','proxy.id')
  275. ->select('proxy_invite.*')
  276. ->where('proxy.user_level','=',$level)
  277. ->where('proxy.province','=',0)
  278. ->where('proxy.city','=',0)
  279. ->where('proxy.uid','=',0)
  280. ->where('proxy.district','=',0)
  281. ->where('proxy_invite.type','=',$jiantype)
  282. ->where('proxy_invite.proxy_type','=',$proxy_type)
  283. ->orderBy('proxy_invite.id','asc')
  284. ->get()->toArray();
  285. if(!empty($jiansetting)){
  286. foreach ($jiansetting as $key=>$item){
  287. foreach ($item as $k=>$v){
  288. if(empty($v)||$v=='0.000'||$v==null||$v=='0.00'||$v=='0.0'){
  289. $setting[$key][$k]=$globaljiansetting[$key][$k];
  290. }else{
  291. $setting[$key][$k]=$v;
  292. }
  293. }
  294. }
  295. }else{
  296. $setting=$globaljiansetting;
  297. }
  298. return $setting;
  299. }
  300. public static function getApplyMoneyByConfig($province = null, $city = null, $district = null, $level = 6)
  301. {
  302. $proxy = '';
  303. // 获取县区代理配置
  304. if ($district != null && empty($proxy)) {
  305. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid(0)->where(['area_type' => 3, 'province' => $province, 'city' => $city, 'district' => $district])->first();
  306. }
  307. // 获取地级市代理配置
  308. if ($city != null && empty($proxy)) {
  309. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid(0)->where(['area_type' => 2, 'province' => $province, 'city' => $city])->first();
  310. }
  311. // 获取省级代理配置
  312. if ($province != null && empty($proxy)) {
  313. $proxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid(0)->where(['area_type' => 1, 'province' => $province])->first();
  314. }
  315. $globalproxy = Proxy::whereUserLevel($level)->whereStatus(1)->whereUid(0)->where(['area_type' => 0])->first()->toArray();
  316. if(empty($proxy)){
  317. $proxy=$globalproxy;
  318. }else{
  319. $proxy=$proxy->toArray();
  320. foreach($proxy as $k=>$item){
  321. if(empty($item)||$item=='0.0'||$item=='0.00'||$item=='0.000'||$item==null){
  322. $proxy[$k]=$globalproxy[$k];
  323. }
  324. }
  325. }
  326. if (empty($proxy)) {
  327. ErrorLog::saveMsg('获取地区代理配置失败', ['province' => $province, 'city' => $city, 'district' => $district/*, 'queryLog' => \DB::getQueryLog()*/]);
  328. }
  329. return $proxy['apply_money'];
  330. }
  331. /**
  332. * 获取直推,间推1,间推2返佣比
  333. * @author lyh
  334. * @date 2019/3/27
  335. * @param int $id
  336. * @param int $total 人数
  337. * @param int $type 类型 1-间推1 2-间推2
  338. * @param int $proxyType 类型 1-代理 2-广告
  339. * @description
  340. */
  341. public
  342. static function getProxyInvite(int $id, int $total, $type = 0, $proxyType = 1)
  343. {
  344. $config = Proxy::getConfig($id);
  345. if ($type == 0) {
  346. return $config->proxy_invite;
  347. } else {
  348. $proxyInvite = $config->invite;
  349. foreach ($proxyInvite as $item) {
  350. if ($item->type == $type && $proxyType == $item->proxy_type) {
  351. if ($total >= $item->max) {
  352. return $item->pct;
  353. }
  354. }
  355. }
  356. return 0;
  357. }
  358. }
  359. /**
  360. * 获取广告剩余免费次数
  361. * @author lyh
  362. * @date 2019/4/22
  363. * @param int $id
  364. * @param $proxyConfig
  365. * @return int|mixed
  366. * @description
  367. */
  368. public
  369. static function getFreeNum(int $id, $proxyConfig)
  370. {
  371. $free_num = 0; // 剩余免费次数
  372. // 有活动又有单独设置,按照单独设置为准
  373. // 有活动没有单独设置,按照活动为准
  374. if ($proxyConfig->uid = \Auth::id() && $proxyConfig->free_num > 0) {
  375. $free_num = $proxyConfig->free_num;
  376. //$free_num = $proxyConfig->free_num - AdverActivityLog::whereActivityId(0)->whereUid(\Auth::id())->whereStatus(3)->sum('free_num');
  377. } else {
  378. $exist = Advertising::whereUid($id)->whereIn('status',[3,4,9])->where('activity_id','=',0)->exists();
  379. //不是首次发布,不能参与免费
  380. if ($exist) {
  381. return 0;
  382. }
  383. // 获取活动信息
  384. $now = Carbon::now();
  385. $activity = AdverActivity::where([
  386. ['start_time', '<', $now],
  387. ['end_time', '>', $now]
  388. ])->orderBy('created_at','desc')->first();
  389. if ($activity) {
  390. // 剩余次数
  391. //$free_num = $activity->free_num - AdverActivityLog::whereActivityId($activity->id)->whereUid(\Auth::id())->whereStatus(3)->sum('free_num');
  392. $free_num = $activity->free_num;
  393. }
  394. }
  395. return $free_num > 0 ? $free_num : 0;
  396. }
  397. public
  398. function proxyInviteModel()
  399. {
  400. return $this->hasMany('App\Modes\ProxyInvite', 'proxy_id', 'id');
  401. }
  402. }