User.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\Validator;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. use \Exception;
  8. class User extends Model
  9. {
  10. use SoftDeletes;
  11. protected $table='users';
  12. protected $hidden=['name','level_change','openid','unionid','sex',
  13. 'province','city','country','register_type','password'];
  14. protected $fillable = ['mobile','name',
  15. 'nickname','realname','cre_num',
  16. 'level','status','cert_status','agent_id',
  17. 'recom_id','recom_code','openid','headimgurl',
  18. 'warea_id','crown_id','store_id','register_type','auth_code','auth_img','auth_startime','auth_endtime','password','remark'];
  19. private static $validate = [
  20. //提示信息
  21. 'message' => [
  22. 'mobile.required'=>'手机号不能为空.',
  23. 'mobile.regex'=>'手机号格式有误.',
  24. 'mobile.unique'=>'手机号已存在.',
  25. 'cre_num.unique'=>'身份证号已存在.',
  26. 'share.required'=>'邀请人手机号不能为空.',
  27. 'share.regex'=>'邀请人手机号格式有误.',
  28. 'nickname.required'=>'代理昵称不能为空.',
  29. 'name.required'=>'名称不能为空.',
  30. 'address.required'=>'地址不能为空.',
  31. 'level.required'=>'请选择代理等级.',
  32. 'level.numeric'=>'代理等级有误.',
  33. ]
  34. ];
  35. //<<<<<<< HEAD
  36. public static function validator($data,$rule)
  37. //=======
  38. // public function validator($data,$rule)
  39. //>>>>>>> 9aa477479386789a8615d89285e74706ec72fbab
  40. {
  41. return Validator::make(
  42. $data,$rule,
  43. self::$validate['message']
  44. );
  45. }
  46. public function changeAgentLevel($user,$level){
  47. // $user = User::find($id);//当前用户信息(等级变更前)
  48. $recom = User::find($user->recom_id);//邀请人信息
  49. $data['auth_img']=null;
  50. if ($user->level > $level) {
  51. $change = '1';//降级
  52. $data['level_change']='-1';
  53. } else {
  54. $change = '2';//升级
  55. $data['level_change']='1';
  56. }
  57. $data['level'] = $level;
  58. if ($level == '3') { //升到皇冠上级直接是公司
  59. $data['agent_id'] = '1';
  60. $data['crown_id']=NULL;
  61. } else {
  62. if ($level == $recom->level) {
  63. $data['agent_id'] = $recom->agent_id;
  64. $data['crown_id']=$recom->crown_id;
  65. $data['warea_id']=$recom->warea_id;
  66. }
  67. if ($level < $recom->level) {
  68. $data['agent_id'] = $recom->id;
  69. if($recom->level=='3'){
  70. $data['crown_id']=$recom->id;
  71. $data['warea_id']=$recom->warea_id;
  72. }else{
  73. $data['crown_id']=$recom->crown_id;
  74. $data['warea_id']=$recom->warea_id;
  75. }
  76. }
  77. if ($level > $recom->level) {
  78. $agent = User::find($recom->agent_id);//邀请人上级信息
  79. if($agent->level=='3'){
  80. $data['agent_id']=$agent->id;
  81. }else{
  82. $data['agent_id'] = $agent->agent_id;
  83. }
  84. $data['crown_id']=$recom->crown_id;
  85. $data['warea_id']=$recom->warea_id;
  86. }
  87. }
  88. try {
  89. User::where('id', $user->id)->update($data);
  90. if ($change == '1') {//降级
  91. $newInfo = User::find($user->id);//级别变更后代理人信息
  92. $warea_id=User::where('id',$newInfo->agent_id)->value('warea_id');
  93. if($level=='2'){
  94. $this->getAgentDownCrownId($user->id,$newInfo->agent_id,$warea_id);
  95. }
  96. User::where('agent_id', $user->id)->where('level', $newInfo->level)->update(['agent_id'=>$newInfo->agent_id]);
  97. }elseif($change == '2'){//升级
  98. if($level=='2'){
  99. $this->getAgentLevelInfoOne($user->id,$user->id);
  100. }elseif($level=='3'){
  101. $this->getAgentLevelInfoTwo($user->id,$user->id);
  102. // $warea_id=User::where('id',$user->id)->value('warea_id');
  103. $this->getAgentUpCrownId($user->id,$user->id);
  104. }
  105. }
  106. return true;
  107. }Catch(Exception $e){
  108. return false;
  109. }
  110. }
  111. //特级升顶级,被邀请的人上级变更
  112. public function getAgentLevelInfoOne($uid,$aid){
  113. $list=User::where('recom_id', $uid)->get();
  114. if($list){
  115. for($i=0;$i<count($list);$i++){
  116. if($list[$i]->level=='1'){
  117. User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
  118. $this->getAgentLevelInfoOne($list[$i]->id,$aid);
  119. }
  120. }
  121. }
  122. }
  123. //顶级升皇冠,被邀请的人上级变更
  124. public function getAgentLevelInfoTwo($uid,$aid){
  125. $list=User::where('recom_id', $uid)->get();
  126. if($list){
  127. for($i=0;$i<count($list);$i++){
  128. if($list[$i]->level=='2'){
  129. User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
  130. $this->getAgentLevelInfoTwo($list[$i]->id,$aid);
  131. }
  132. if($list[$i]->level=='1'){
  133. $this->getAgentLevelInfoTwo($list[$i]->id,$aid);
  134. }
  135. }
  136. }
  137. }
  138. //顶级升皇冠时,下级代理的皇冠,战区变更
  139. public function getAgentUpCrownId($uid,$cid){
  140. $list=User::where('recom_id', $uid)->get();
  141. if($list){
  142. foreach($list as $key=>$val){
  143. if($val->crown_id!=$uid){
  144. User::where('id', $val->id)->update(['crown_id'=>$cid]);
  145. $this->getAgentUpCrownId($val->id,$cid);
  146. }
  147. }
  148. }
  149. }
  150. //皇冠降顶级时,下级代理的皇冠,战区变更
  151. public function getAgentDownCrownId($uid,$cid,$wid){
  152. $list=User::where('agent_id', $uid)->get();
  153. if($list){
  154. foreach($list as $key=>$val){
  155. User::where('id', $val->id)->update(['crown_id'=>$cid,'warea_id'=>$wid]);
  156. $childAgent=User::where('agent_id', $val->id)->get();
  157. if($childAgent){
  158. foreach($childAgent as $k=>$v){
  159. User::where('id', $v->id)->update(['crown_id'=>$cid,'warea_id'=>$wid]);
  160. }
  161. }
  162. }
  163. }
  164. }
  165. //顶级邀请人变更
  166. public function changeAgentLevelInfoTwo($uid,$aid){
  167. $list=User::where('recom_id', $uid)->get();
  168. if($list){
  169. for($i=0;$i<count($list);$i++){
  170. if($list[$i]->level=='2'){
  171. User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
  172. $this->changeAgentLevelInfoTwo($list[$i]->id,$aid);
  173. }
  174. if($list[$i]->level=='1'){
  175. $this->changeAgentLevelInfoTwo($list[$i]->id,$aid);
  176. }
  177. }
  178. }
  179. }
  180. //te 级邀请人变更
  181. // public function changeAgentLevelInfoOne($uid,$aid){
  182. // $list=User::where('recom_id', $uid)->get();
  183. // if($list){
  184. // for($i=0;$i<count($list);$i++){
  185. // if($list[$i]->level=='1' || $list[$i]->level=='2'){
  186. // User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
  187. // $this->changeAgentLevelInfoOne($list[$i]->id,$aid);
  188. // }
  189. // }
  190. // }
  191. // }
  192. //te 级邀请人变更 3
  193. public function changeAgentLevelInfoThree($uid,$aid,$rid){
  194. $list=User::where('recom_id', $uid)->get();
  195. if($list){
  196. for($i=0;$i<count($list);$i++){
  197. if($list[$i]->level=='2'){
  198. User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
  199. $this->changeAgentLevelInfoTwo($list[$i]->id,$aid);
  200. }
  201. if($list[$i]->level=='1'){
  202. User::where('id', $list[$i]->id)->update(['agent_id'=>$rid]);
  203. $this->changeAgentInfoSuper($list[$i]->id,$aid,$rid);
  204. }
  205. }
  206. }
  207. }
  208. public function changeAgentRecomCrown($uid,$crown_id,$warea_id){
  209. $list=User::where('recom_id', $uid)->get();
  210. if($list){
  211. for($i=0;$i<count($list);$i++){
  212. if($list[$i]->level=='2' || $list[$i]->level=='1'){
  213. User::where('id', $list[$i]->id)->update(['crown_id'=>$crown_id,'warea_id'=>$warea_id]);
  214. $this->changeAgentRecomCrown($list[$i]->id,$crown_id,$warea_id);
  215. }
  216. }
  217. }
  218. }
  219. public function changeAgentInfoSuper($uid,$aid,$rid){
  220. $list=User::where('recom_id', $uid)->get();
  221. foreach($list as $key=>$val){
  222. if($val->level=='1'){
  223. User::where('id', $val->id)->update(['agent_id'=>$rid]);
  224. $this->changeAgentInfoSuper($val->id,$aid,$rid);
  225. }elseif($val->level=='2'){
  226. User::where('id', $val->id)->update(['agent_id'=>$aid]);
  227. $this->changeAgentLevelInfoTwo($val->id,$aid);
  228. }
  229. }
  230. }
  231. public function changeRecomSystem($uid,$mobile){
  232. $list=User::where('recom_id', $uid)->get();
  233. foreach($list as $key=>$val){
  234. if($val->mobile==$mobile){
  235. return '222';
  236. }else{
  237. return $this->changeRecomSystem($val->id,$mobile);
  238. }
  239. }
  240. }
  241. public function getAgentSalesResult($user_id){
  242. $account=DB::table('order')
  243. ->leftJoin('users', 'order.user_id', '=', 'users.id')
  244. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  245. ->where('users.id',$user_id)
  246. ->where('order.status', 2)
  247. ->sum('totalprice');
  248. return $account;
  249. }
  250. public function order()
  251. {
  252. return $this->hasMany('App\Models\Order');
  253. }
  254. public function orders()//订单ordertest
  255. {
  256. return $this->hasMany(Ordertest::class);
  257. }
  258. public function nopayinfo()
  259. {
  260. return $this->hasMany(NoPayInfo::class);
  261. }
  262. public function user_pay()
  263. {
  264. return $this->hasMany(Userpay::class);
  265. }
  266. public function userz(){
  267. return $this->hasOne('App\Models\UserZ','phone','mobile');
  268. }
  269. public function ordergoods()
  270. {
  271. return $this->hasMany('App\Models\OrderGoods','ordersn');
  272. }
  273. public function warea()
  274. {
  275. return $this->hasOne('App\Models\Warea','id','warea_id');
  276. }
  277. public function recom()
  278. {
  279. return $this->belongsTo('App\Models\User','recom_id','id')->withTrashed();
  280. }
  281. public function childRecom()
  282. {
  283. return $this->hasMany('App\Models\User','id','recom_id')->withTrashed();
  284. }
  285. public function agent()
  286. {
  287. return $this->belongsTo('App\Models\User','agent_id','id')->withTrashed();
  288. }
  289. public function childAgent()
  290. {
  291. return $this->hasMany('App\Models\User','agent_id','id')->withTrashed();
  292. }
  293. public function usercert()
  294. {
  295. return $this->hasOne('App\Models\UserCert');
  296. }
  297. // public function ordergoods()
  298. // {
  299. // return $this->hasManyThrough(
  300. // 'App\Models\OrderGoods',
  301. // 'App\Models\Order',
  302. // 'user_id', // 用户表外键
  303. // 'order_id', // 订单表外键
  304. // 'id', // 用户表本地键
  305. // 'id' // 订单表本地键
  306. // );
  307. // }
  308. public function get_order_de(){
  309. return $this->hasManyThrough(Orderdetail::class,Ordertest::class,'user_id','order_id');
  310. }
  311. public function get_order_des(){
  312. return $this->belongsToMany(Orderdetail::class,'order_test','user_id','order_id');
  313. }
  314. }