UserCopy.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 UserCopy extends Model
  9. {
  10. use SoftDeletes;
  11. protected $table='users_copy1';
  12. protected $hidden=['name','level_change','openid','unionid','sex',
  13. 'province','city','country','register_type','password',
  14. ];
  15. protected $fillable = ['mobile','name',
  16. 'nickname','realname','cre_num',
  17. 'level','status','cert_status','agent_id',
  18. 'recom_id','recom_code','openid','headimgurl',
  19. 'warea_id','crown_id','store_id','register_type','auth_code','auth_img','auth_startime','auth_endtime'];
  20. private static $validate = [
  21. //提示信息
  22. 'message' => [
  23. 'mobile.required'=>'手机号不能为空.',
  24. 'mobile.regex'=>'手机号格式有误.',
  25. 'mobile.unique'=>'手机号已存在.',
  26. 'cre_num.unique'=>'身份证号已存在.',
  27. 'share.required'=>'邀请人手机号不能为空.',
  28. 'share.regex'=>'邀请人手机号格式有误.',
  29. 'nickname.required'=>'代理昵称不能为空.',
  30. 'name.required'=>'名称不能为空.',
  31. 'address.required'=>'地址不能为空.',
  32. 'level.required'=>'请选择代理等级.',
  33. 'level.numeric'=>'代理等级有误.',
  34. ]
  35. ];
  36. public function validator($data,$rule)
  37. {
  38. return Validator::make(
  39. $data,$rule,
  40. self::$validate['message']
  41. );
  42. }
  43. public function changeAgentLevel($user,$level){
  44. // $user = User::find($id);//当前用户信息(等级变更前)
  45. $recom = User::find($user->recom_id);//邀请人信息
  46. if (empty($recom)) {
  47. return $this->error('400115', '代理邀请人信息不存在,升降级失败');
  48. }
  49. if ($user->level > $level) {
  50. $change = '1';//降级
  51. $data['level_change']='-1';
  52. } else {
  53. $change = '2';//升级
  54. $data['level_change']='1';
  55. }
  56. $data['level'] = $level;
  57. if ($level == '3') { //升到皇冠上级直接是公司
  58. $data['agent_id'] = '1';
  59. $data['crown_id']=NULL;
  60. } else {
  61. if ($level == $recom->level) {
  62. $data['agent_id'] = $recom->agent_id;
  63. $data['crown_id']=$recom->crown_id;
  64. $data['warea_id']=$recom->warea_id;
  65. }
  66. if ($level < $recom->level) {
  67. $data['agent_id'] = $recom->id;
  68. if($recom->level=='3'){
  69. $data['crown_id']=$recom->id;
  70. $data['warea_id']=$recom->warea_id;
  71. }else{
  72. $data['crown_id']=$recom->crown_id;
  73. $data['warea_id']=$recom->warea_id;
  74. }
  75. }
  76. if ($level > $recom->level) {
  77. $agent = User::find($recom->agent_id);//邀请人上级信息
  78. if($agent->level=='3'){
  79. $data['agent_id']=$agent->id;
  80. }else{
  81. $data['agent_id'] = $agent->agent_id;
  82. }
  83. $data['crown_id']=$recom->crown_id;
  84. $data['warea_id']=$recom->warea_id;
  85. }
  86. }
  87. try {
  88. User::where('id', $user->id)->update($data);
  89. if ($change == '1') {//降级
  90. $newInfo = User::find($user->id);//级别变更后代理人信息
  91. $warea_id=User::where('id',$newInfo->agent_id)->value('warea_id');
  92. if($level=='2'){
  93. $this->getAgentDownCrownId($user->id,$newInfo->agent_id,$warea_id);
  94. }
  95. User::where('agent_id', $user->id)->where('level', $newInfo->level)->update(['agent_id'=>$newInfo->agent_id]);
  96. }elseif($change == '2'){//升级
  97. if($level=='2'){
  98. $this->getAgentLevelInfoOne($user->id,$user->id);
  99. }elseif($level=='3'){
  100. $this->getAgentLevelInfoTwo($user->id,$user->id);
  101. // $warea_id=User::where('id',$user->id)->value('warea_id');
  102. $this->getAgentUpCrownId($user->id,$user->id);
  103. }
  104. }
  105. return true;
  106. }Catch(Exception $e){
  107. return false;
  108. }
  109. }
  110. //特级升顶级,被邀请的人上级变更
  111. public function getAgentLevelInfoOne($uid,$aid){
  112. $list=User::where('recom_id', $uid)->get();
  113. if($list){
  114. for($i=0;$i<count($list);$i++){
  115. if($list[$i]->level=='1'){
  116. User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
  117. $this->getAgentLevelInfoOne($list[$i]->id,$aid);
  118. }
  119. }
  120. }
  121. }
  122. //顶级升皇冠,被邀请的人上级变更
  123. public function getAgentLevelInfoTwo($uid,$aid){
  124. $list=User::where('recom_id', $uid)->get();
  125. if($list){
  126. for($i=0;$i<count($list);$i++){
  127. if($list[$i]->level=='2'){
  128. User::where('id', $list[$i]->id)->update(['agent_id'=>$aid]);
  129. $this->getAgentLevelInfoTwo($list[$i]->id,$aid);
  130. }
  131. if($list[$i]->level=='1'){
  132. $this->getAgentLevelInfoTwo($list[$i]->id,$aid);
  133. }
  134. }
  135. }
  136. }
  137. //顶级升皇冠时,下级代理的皇冠,战区变更
  138. public function getAgentUpCrownId($uid,$cid){
  139. $list=User::where('recom_id', $uid)->get();
  140. if($list){
  141. foreach($list as $key=>$val){
  142. if($val->crown_id!=$uid){
  143. User::where('id', $val->id)->update(['crown_id'=>$cid]);
  144. $this->getAgentUpCrownId($val->id,$cid);
  145. }
  146. }
  147. }
  148. }
  149. //皇冠降顶级时,下级代理的皇冠,战区变更
  150. public function getAgentDownCrownId($uid,$cid,$wid){
  151. $list=User::where('agent_id', $uid)->get();
  152. if($list){
  153. foreach($list as $key=>$val){
  154. User::where('id', $val->id)->update(['crown_id'=>$cid,'warea_id'=>$wid]);
  155. $childAgent=User::where('agent_id', $val->id)->get();
  156. if($childAgent){
  157. foreach($childAgent as $k=>$v){
  158. User::where('id', $v->id)->update(['crown_id'=>$cid,'warea_id'=>$wid]);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. public function getAgentSalesResult($user_id){
  165. $account=DB::table('order')
  166. ->leftJoin('users', 'order.user_id', '=', 'users.id')
  167. ->leftJoin('order_goods', 'order.id', '=', 'order_goods.order_id')
  168. ->where('users.id',$user_id)
  169. ->where('order.status', 2)
  170. ->sum('totalprice');
  171. return $account;
  172. }
  173. public function order()
  174. {
  175. return $this->hasMany('App\Models\Order');
  176. }
  177. public function ordergoods()
  178. {
  179. return $this->hasMany('App\Models\OrderGoods','ordersn');
  180. }
  181. public function warea()
  182. {
  183. return $this->hasOne('App\Models\Warea','id','warea_id');
  184. }
  185. public function recom()
  186. {
  187. return $this->hasOne('App\Models\UserCopy','id','recom_id')->select('id','nickname','mobile');
  188. }
  189. public function agent()
  190. {
  191. return $this->belongsTo('App\Models\UserCopy','agent_id','id')->select('id','nickname','mobile');
  192. }
  193. public function childAgent()
  194. {
  195. return $this->hasMany('App\Models\UserCopy','agent_id','id')->select('id','nickname','mobile','level');
  196. }
  197. public function usercert()
  198. {
  199. return $this->hasOne('App\Models\UserCert');
  200. }
  201. // public function ordergoods()
  202. // {
  203. // return $this->hasManyThrough(
  204. // 'App\Models\OrderGoods',
  205. // 'App\Models\Order',
  206. // 'user_id', // 用户表外键
  207. // 'order_id', // 订单表外键
  208. // 'id', // 用户表本地键
  209. // 'id' // 订单表本地键
  210. // );
  211. // }
  212. }