User.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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=['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','login_log_count','service_status'];
  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. public static function validator($data,$rule)
  36. {
  37. return Validator::make(
  38. $data,$rule,
  39. self::$validate['message']
  40. );
  41. }
  42. public function changeAgentLevel($user,$level){
  43. // $user = User::find($id);//当前用户信息(等级变更前)
  44. $recom = User::find($user->recom_id);//邀请人信息
  45. $data['auth_img']=null;
  46. if ($user->level > $level) {
  47. $change = '1';//降级
  48. $data['level_change']='-1';
  49. } else {
  50. $change = '2';//升级
  51. $data['level_change']='1';
  52. }
  53. $data['level'] = $level;
  54. if ($level == '3') { //升到皇冠上级直接是公司
  55. $data['agent_id'] = '1';
  56. $data['crown_id']=NULL;
  57. } else {
  58. if ($level == $recom->level) {
  59. $data['agent_id'] = $recom->agent_id;
  60. $data['crown_id']=$recom->crown_id;
  61. $data['warea_id']=$recom->warea_id;
  62. }
  63. if ($level < $recom->level) {
  64. $data['agent_id'] = $recom->id;
  65. if($recom->level=='3'){
  66. $data['crown_id']=$recom->id;
  67. $data['warea_id']=$recom->warea_id;
  68. }else{
  69. $data['crown_id']=$recom->crown_id;
  70. $data['warea_id']=$recom->warea_id;
  71. }
  72. }
  73. if ($level > $recom->level) {
  74. $agent = User::find($recom->agent_id);//邀请人上级信息
  75. if($agent->level=='3'){
  76. $data['agent_id']=$agent->id;
  77. }else{
  78. $data['agent_id'] = $agent->agent_id;
  79. }
  80. $data['crown_id']=$recom->crown_id;
  81. $data['warea_id']=$recom->warea_id;
  82. }
  83. }
  84. try {
  85. User::where('id', $user->id)->update($data);
  86. if ($change == '1') {//降级
  87. $newInfo = User::find($user->id);//级别变更后代理人信息
  88. $warea_id=User::where('id',$newInfo->agent_id)->value('warea_id');
  89. if($level=='2'){
  90. $this->getAgentDownCrownId($user->id,$newInfo->agent_id,$warea_id);
  91. }
  92. User::where('agent_id', $user->id)->where('level', $newInfo->level)->update(['agent_id'=>$newInfo->agent_id]);
  93. }elseif($change == '2'){//升级
  94. if($level=='2'){
  95. $this->getAgentLevelInfoOne($user->id,$user->id);
  96. }elseif($level=='3'){
  97. $this->getAgentLevelInfoTwo($user->id,$user->id);
  98. // $warea_id=User::where('id',$user->id)->value('warea_id');
  99. $this->getAgentUpCrownId($user->id,$user->id);
  100. }
  101. }
  102. return true;
  103. }Catch(Exception $e){
  104. return false;
  105. }
  106. }
  107. //特级升顶级,被邀请的人上级变更
  108. public function getAgentLevelInfoOne($uid,$aid){
  109. $list=User::where('recom_id', $uid)->get();
  110. if($list){
  111. for($i=0;$i<count($list);$i++){
  112. if($list[$i]->level=='1'){
  113. User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
  114. $this->getAgentLevelInfoOne($list[$i]->id,$aid);
  115. }
  116. }
  117. }
  118. }
  119. //顶级升皇冠,被邀请的人上级变更
  120. public function getAgentLevelInfoTwo($uid,$aid){
  121. $list=User::where('recom_id', $uid)->get();
  122. if($list){
  123. for($i=0;$i<count($list);$i++){
  124. if($list[$i]->level=='2'){
  125. User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
  126. $this->getAgentLevelInfoTwo($list[$i]->id,$aid);
  127. }
  128. if($list[$i]->level=='1'){
  129. $this->getAgentLevelInfoTwo($list[$i]->id,$aid);
  130. }
  131. }
  132. }
  133. }
  134. //顶级升皇冠时,下级代理的皇冠,战区变更
  135. public function getAgentUpCrownId($uid,$cid){
  136. $list=User::where('recom_id', $uid)->get();
  137. if($list){
  138. foreach($list as $key=>$val){
  139. if($val->crown_id!=$uid){
  140. User::where('id', $val->id)->update(['crown_id'=>$cid]);
  141. $this->getAgentUpCrownId($val->id,$cid);
  142. }
  143. }
  144. }
  145. }
  146. //皇冠降顶级时,下级代理的皇冠,战区变更
  147. public function getAgentDownCrownId($uid,$cid,$wid){
  148. $list=User::where('agent_id', $uid)->get();
  149. if($list){
  150. foreach($list as $key=>$val){
  151. User::where('id', $val->id)->update(['crown_id'=>$cid,'warea_id'=>$wid]);
  152. $childAgent=User::where('agent_id', $val->id)->get();
  153. if($childAgent){
  154. foreach($childAgent as $k=>$v){
  155. User::where('id', $v->id)->update(['crown_id'=>$cid,'warea_id'=>$wid]);
  156. }
  157. }
  158. }
  159. }
  160. }
  161. //顶级邀请人变更
  162. public function changeAgentLevelInfoTwo($uid,$aid){
  163. $list=User::where('recom_id', $uid)->get();
  164. if($list){
  165. for($i=0;$i<count($list);$i++){
  166. if($list[$i]->level=='2'){
  167. User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
  168. $this->changeAgentLevelInfoTwo($list[$i]->id,$aid);
  169. }
  170. if($list[$i]->level=='1'){
  171. $this->changeAgentLevelInfoTwo($list[$i]->id,$aid);
  172. }
  173. }
  174. }
  175. }
  176. //te 级邀请人变更
  177. // public function changeAgentLevelInfoOne($uid,$aid){
  178. // $list=User::where('recom_id', $uid)->get();
  179. // if($list){
  180. // for($i=0;$i<count($list);$i++){
  181. // if($list[$i]->level=='1' || $list[$i]->level=='2'){
  182. // User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
  183. // $this->changeAgentLevelInfoOne($list[$i]->id,$aid);
  184. // }
  185. // }
  186. // }
  187. // }
  188. //te 级邀请人变更 3
  189. public function changeAgentLevelInfoThree($uid,$aid,$rid){
  190. $list=User::where('recom_id', $uid)->get();
  191. if($list){
  192. for($i=0;$i<count($list);$i++){
  193. if($list[$i]->level=='2'){
  194. User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
  195. $this->changeAgentLevelInfoTwo($list[$i]->id,$aid);
  196. }
  197. if($list[$i]->level=='1'){
  198. User::where('id', $list[$i]->id)->update(['agent_id'=>$rid]);
  199. $this->changeAgentInfoSuper($list[$i]->id,$aid,$rid);
  200. }
  201. }
  202. }
  203. }
  204. public function changeAgentRecomCrown($uid,$crown_id,$warea_id){
  205. $list=User::where('recom_id', $uid)->get();
  206. if($list){
  207. for($i=0;$i<count($list);$i++){
  208. if($list[$i]->level=='2' || $list[$i]->level=='1'){
  209. User::where('id', $list[$i]->id)->update(['crown_id'=>$crown_id,'warea_id'=>$warea_id]);
  210. $this->changeAgentRecomCrown($list[$i]->id,$crown_id,$warea_id);
  211. }
  212. }
  213. }
  214. }
  215. public function changeAgentInfoSuper($uid,$aid,$rid){
  216. $list=User::where('recom_id', $uid)->get();
  217. foreach($list as $key=>$val){
  218. if($val->level=='1'){
  219. User::where('id', $val->id)->update(['agent_id'=>$rid]);
  220. $this->changeAgentInfoSuper($val->id,$aid,$rid);
  221. }elseif($val->level=='2'){
  222. User::where('id', $val->id)->update(['agent_id'=>$aid]);
  223. $this->changeAgentLevelInfoTwo($val->id,$aid);
  224. }
  225. }
  226. }
  227. public function changeRecomSystem($uid,$mobile){
  228. $list=User::where('recom_id', $uid)->get();
  229. foreach($list as $key=>$val){
  230. if($val->mobile==$mobile){
  231. return '222';
  232. }else{
  233. return $this->changeRecomSystem($val->id,$mobile);
  234. }
  235. }
  236. }
  237. public function getAgentSalesResult($user_id){
  238. $account=DB::table('order')
  239. ->leftJoin('users', 'order.user_id', '=', 'users.id')
  240. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  241. ->where('users.id',$user_id)
  242. ->where('order.status', 2)
  243. ->sum('totalprice');
  244. return $account;
  245. }
  246. public function order()
  247. {
  248. return $this->hasMany('App\Models\Order');
  249. }
  250. public function login_log()
  251. {
  252. return $this->hasMany(LoginLog::class);
  253. }
  254. public function order_test()//订单ordertest
  255. {
  256. return $this->hasMany(Ordertest::class,'user_id','id');
  257. }
  258. public function every_act_data()
  259. {
  260. return $this->hasMany(EveryActData::class,'user_id','id')->orderBy('activity_id');
  261. }
  262. public function nopayinfo()
  263. {
  264. return $this->hasMany(NoPayInfo::class);
  265. }
  266. public function user_pay()
  267. {
  268. return $this->hasMany(Userpay::class);
  269. }
  270. public function userz(){
  271. return $this->hasOne('App\Models\UserZ','phone','mobile');
  272. }
  273. public function ordergoods()
  274. {
  275. return $this->hasMany('App\Models\OrderGoods','ordersn');
  276. }
  277. public function warea()
  278. {
  279. return $this->hasOne('App\Models\Warea','id','warea_id');
  280. }
  281. public function recom()
  282. {
  283. return $this->belongsTo('App\Models\User','recom_id','id')->withTrashed();
  284. }
  285. public function childRecom()
  286. {
  287. return $this->hasMany('App\Models\User','id','recom_id')->withTrashed();
  288. }
  289. public function agent()
  290. {
  291. return $this->belongsTo('App\Models\User','agent_id','id')->withTrashed();
  292. }
  293. public function childAgent()
  294. {
  295. return $this->hasMany('App\Models\User','agent_id','id')->withTrashed();
  296. }
  297. public function usercert()
  298. {
  299. return $this->hasOne('App\Models\UserCert');
  300. }
  301. // public function ordergoods()
  302. // {
  303. // return $this->hasManyThrough(
  304. // 'App\Models\OrderGoods',
  305. // 'App\Models\Order',
  306. // 'user_id', // 用户表外键
  307. // 'order_id', // 订单表外键
  308. // 'id', // 用户表本地键
  309. // 'id' // 订单表本地键
  310. // );
  311. // }
  312. public function get_order_de(){
  313. return $this->hasManyThrough(Orderdetail::class,Ordertest::class,'user_id','order_id');
  314. }
  315. public function get_order_des(){
  316. return $this->belongsToMany(Orderdetail::class,'order_test','user_id','order_id');
  317. }
  318. /**政委审核信息**/
  319. public function get_service(){
  320. return $this->hasOne(ServiceReason::class,'user_id');
  321. }
  322. public function get_storage(){
  323. return $this->hasMany(UserStorage::class,'user_id');
  324. }
  325. public function get_put_storage(){
  326. return $this->hasMany(StoragePutLog::class,'user_id');
  327. }public function get_out_storage(){
  328. return $this->hasMany(ExpressStorage::class,'user_id');
  329. }
  330. public function get_storage_detail(){
  331. return $this->hasMany(ExpressStorage::class,'user_id');
  332. }
  333. public function get_storage_details(){
  334. return $this->hasMany(ExpressStorage::class,'user_id');
  335. }
  336. public function getQuantityAttribute(){
  337. return $this->get_storage()->sum('num');
  338. }
  339. public function tag_remind_storage(){
  340. return $this->belongsTo(TagRemind::class,'id','user_id');
  341. }
  342. }