MemberShopInfo.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/2
  6. * Time: 下午4:18
  7. */
  8. namespace app\common\models;
  9. use app\backend\models\BackendModel;
  10. use app\backend\modules\member\models\MemberRecord;
  11. use app\common\events\member\MemberChangeRelationEvent;
  12. use app\common\events\member\MemberCreateRelationEvent;
  13. use app\common\events\member\RegisterByAgent;
  14. use app\common\observers\member\MemberObserver;
  15. use app\frontend\modules\member\models\SubMemberModel;
  16. use app\Jobs\ModifyRelationJob;
  17. use Illuminate\Database\Eloquent\Builder;
  18. use Illuminate\Database\Eloquent\SoftDeletes;
  19. use Yunshop\Commission\models\Agents;
  20. use Yunshop\Hotel\common\models\Hotel;
  21. use Yunshop\Supplier\common\models\Supplier;
  22. use Yunshop\TeamDividend\models\TeamDividendAgencyModel;
  23. /**
  24. * Class MemberShopInfo
  25. * @package app\common\models
  26. *
  27. * @property int m_id
  28. * @property int member_id
  29. * @property int uniacid
  30. * @property int parent_id
  31. * @property int group_id
  32. * @property int level_id
  33. * @property int inviter
  34. * @property int is_black
  35. * @property string province_name
  36. * @property string city_name
  37. * @property string area_name
  38. * @property int province
  39. * @property int city
  40. * @property int area
  41. * @property string address
  42. * @property string referralsn
  43. * @property int is_agent
  44. * @property string alipayname
  45. * @property string alipay
  46. * @property string content
  47. * @property int status
  48. * @property int child_time
  49. * @property int agent_time
  50. * @property int apply_time
  51. * @property string relation
  52. * @property int created_at
  53. * @property int updated_at
  54. * @property int deleted_at
  55. * @property string custom_value
  56. * @property int validity
  57. * @property int member_form
  58. * @property string pay_password
  59. * @property string salt
  60. * @property string withdraw_mobile
  61. * @property string wechat
  62. * @property string yz_openid
  63. * @property string invite_code
  64. *
  65. * @property MemberLevel level
  66. *
  67. * @method static self search($search = [])
  68. */
  69. class MemberShopInfo extends BaseModel
  70. {
  71. use SoftDeletes;
  72. protected $connection = 'mysql';
  73. protected $table = 'yz_member';
  74. protected $guarded = [''];
  75. protected $hidden = ['pay_password','salt','access_token_1','access_expires_in_1','refresh_token_1','refresh_expires_in_1','access_token_2','access_expires_in_2','refresh_token_2','refresh_expires_in_2'];
  76. public $primaryKey = 'member_id';
  77. public static function boot()
  78. {
  79. parent::boot();
  80. static::observe(new MemberObserver());
  81. static::addGlobalScope('uniacid', function (Builder $builder) {
  82. return $builder->uniacid();
  83. });
  84. }
  85. public function level()
  86. {
  87. return $this->hasOne('app\common\models\MemberLevel', 'id', 'level_id');
  88. }
  89. public function group()
  90. {
  91. return $this->hasOne('app\common\models\MemberGroup', 'id', 'group_id');
  92. }
  93. public function hasPayPassword()
  94. {
  95. return isset($this->pay_password) && !empty($this->pay_password);
  96. }
  97. /**
  98. * @param static $query
  99. * @param array $search
  100. */
  101. public function scopeSearch($query, $search)
  102. {
  103. if ($search['level_id']) $query->where('level_id', $search['level_id']);
  104. if ($search['group_id']) $query->where('group_id', $search['group_id']);;
  105. //todo 移除 member_level、member_group 检索,规范使用
  106. if ($search['member_level']) $query->where('level_id', $search['member_level']);
  107. if ($search['member_group']) $query->where('group_id', $search['member_group']);;
  108. }
  109. /**
  110. * 检索关联会员等级表
  111. * @param $query
  112. * @return static
  113. */
  114. public function scopeWithLevel($query)
  115. {
  116. return $query->with([
  117. 'level' => function ($query) {
  118. return $query;
  119. }
  120. ]);
  121. }
  122. /**
  123. * 获取用户信息
  124. *
  125. * @param $memberId
  126. * @return mixed
  127. * @throws \app\common\exceptions\AppException
  128. */
  129. public static function getMemberShopInfo($memberId)
  130. {
  131. // 为了方便解决重复查询当前用户的bug
  132. if (!is_null(Member::current()->yzMember) && (Member::current()->yzMember->member_id == $memberId)) {
  133. return Member::current()->yzMember;
  134. }
  135. return self::select('*')->where('member_id', $memberId)
  136. ->uniacid()
  137. ->first();
  138. }
  139. /**
  140. * 通过 openid 获取用户信息
  141. * @param $openid
  142. * @return mixed
  143. */
  144. public static function getMemberShopInfoByOpenid($openid)
  145. {
  146. return static::uniacid()->whereHas('hasOneMappingFans', function ($query) use ($openid) {
  147. $query->where('openid', '=', $openid);
  148. })->first();
  149. }
  150. /**
  151. * 获取我的下线
  152. *
  153. * @return mixed
  154. */
  155. public static function getAgentCount()
  156. {
  157. return self::uniacid()
  158. ->where('parent_id', \YunShop::app()->getMemberId())
  159. ->where('is_black', 0)
  160. ->count();
  161. }
  162. /**
  163. * 获取指定推荐人的下线
  164. *
  165. * @param $uids
  166. * @return mixed
  167. */
  168. public static function getAgentAllCount($uids)
  169. {
  170. return self::selectRaw('parent_id, count(member_id) as total')
  171. ->uniacid()
  172. ->whereIn('parent_id', $uids)
  173. ->where('is_black', 0)
  174. ->groupBy('parent_id')
  175. ->get();
  176. }
  177. public function hasManySelf()
  178. {
  179. return $this->hasMany('app\common\models\MemberShopInfo', 'parent_id', 'member_id');
  180. }
  181. public function hasOnePreSelf()
  182. {
  183. return $this->hasOne('app\common\models\MemberShopInfo', 'member_id', 'parent_id');
  184. }
  185. public function hasOneMappingFans()
  186. {
  187. return $this->hasOne('app\common\models\McMappingFans', 'uid', 'member_id');
  188. }
  189. /**
  190. * 用户是否为黑名单用户
  191. *
  192. * @param $member_id
  193. * @return bool
  194. */
  195. public static function isBlack($member_id)
  196. {
  197. $member_model = self::getMemberShopInfo($member_id);
  198. if (1 == $member_model->is_black) {
  199. return true;
  200. } else {
  201. return false;
  202. }
  203. }
  204. public static function getUserInfo($mobile)
  205. {
  206. return self::uniacid()
  207. ->where('withdraw_mobile', $mobile)
  208. ->first();
  209. }
  210. /**
  211. * 获取该公众号下所有用户的 member ID
  212. *
  213. * @return mixed
  214. */
  215. public static function getYzMembersId()
  216. {
  217. return static::uniacid()
  218. ->select(['member_id'])
  219. ->get();
  220. }
  221. public static function getSubLevelMember($uid, $pos)
  222. {
  223. return self::uniacid()
  224. ->select(['member_id', 'parent_id', 'relation'])
  225. ->whereRaw('FIND_IN_SET(?,relation) = ?', [$uid, $pos])
  226. ->get();
  227. }
  228. //新增关联订单表
  229. public function hasOneOrder()
  230. {
  231. return $this->hasOne(Order::class, 'uid', 'member_id');
  232. }
  233. //主表yz_member,从表mc_member
  234. public function hasOneMember()
  235. {
  236. return $this->hasOne(Member::class, 'uid', 'member_id');
  237. }
  238. //关联供应商
  239. public function hasOneSupplier()
  240. {
  241. return $this->hasOne(Supplier::class, 'member_id', 'member_id');
  242. }
  243. //关联门店
  244. public function hasOneStore()
  245. {
  246. return $this->hasOne(Store::class, 'uid', 'member_id');
  247. }
  248. //关联酒店
  249. public function hasOneHotel()
  250. {
  251. return $this->hasOne(Hotel::class, 'uid', 'member_id');
  252. }
  253. public static function chkInviteCode($code)
  254. {
  255. return self::select('member_id')->where('invite_code', $code)
  256. ->uniacid()
  257. ->count();
  258. }
  259. public static function updateInviteCode($member_id, $code)
  260. {
  261. return self::uniacid()
  262. ->where('member_id', $member_id)
  263. ->update(['invite_code' => $code]);
  264. }
  265. public static function getMemberIdForInviteCode($code)
  266. {
  267. return self::uniacid()
  268. ->where('invite_code', $code)
  269. ->pluck('member_id');
  270. }
  271. public static function change_relation($uid, $parent_id)
  272. {
  273. if (is_numeric($parent_id)) {
  274. if (!empty($parent_id)) {
  275. $parent = SubMemberModel::getMemberShopInfo($parent_id);
  276. $parent_is_agent = !empty($parent) && $parent->is_agent == 1 && $parent->status == 2;
  277. if (!$parent_is_agent) {
  278. return ['status' => -1];
  279. }
  280. }
  281. $member_relation = Member::setMemberRelation($uid, $parent_id);
  282. $plugin_commission = app('plugins')->isEnabled('commission');
  283. $plugin_team = app('plugins')->isEnabled('team-dividend');
  284. if (isset($member_relation) && $member_relation !== false) {
  285. $member = MemberShopInfo::getMemberShopInfo($uid);
  286. $record = new MemberRecord();
  287. $record->uniacid = \YunShop::app()->uniacid;
  288. $record->uid = $uid;
  289. $record->parent_id = $member->parent_id;
  290. $record->after_parent_id = $parent_id;
  291. $record->save();
  292. $rs = event(new MemberChangeRelationEvent($uid, $parent_id));
  293. \Log::debug('----change relation----', [$uid, $parent_id, $rs]);
  294. foreach ($rs as $item) {
  295. if (is_null($item)) {
  296. continue;
  297. }
  298. if (1 == $item['status']) {
  299. $member->parent_id = $parent_id;
  300. $member->inviter = 1;
  301. $member->save();
  302. if ($plugin_team) {
  303. $team = TeamDividendAgencyModel::getAgentByUidId($uid)->first();
  304. if (!is_null($team)) {
  305. $team->parent_id = $parent_id;
  306. $team->relation = $member->relation;
  307. $team->save();
  308. }
  309. }
  310. if ($plugin_commission) {
  311. $agents = Agents::uniacid()->where('member_id', $uid)->first();
  312. if (!is_null($agents)) {
  313. $agents->parent_id = $parent_id;
  314. $agents->parent = $member->relation;
  315. $agents->save();
  316. }
  317. $agent_data = [
  318. 'member_id' => $uid,
  319. 'parent_id' => $parent_id,
  320. 'parent' => $member->relation
  321. ];
  322. event(new RegisterByAgent($agent_data));
  323. }
  324. //更新2、3级会员上线和分销关系
  325. dispatch(new ModifyRelationJob($uid, $member_relation, $plugin_commission));
  326. return ['status' => 1];
  327. }
  328. }
  329. }
  330. return ['status' => 0];
  331. }
  332. }
  333. /**
  334. * 查询会员邀请码
  335. *
  336. * @return mixed
  337. */
  338. public function getInviteCode($inviteCode)
  339. {
  340. $data = self::select('member_id')->where('invite_code', $inviteCode)
  341. ->uniacid()
  342. ->count();
  343. if ($data > 0) {
  344. return true;
  345. } else {
  346. return false;
  347. }
  348. }
  349. /**
  350. * 查询邀请码会员
  351. *
  352. * @return mixed
  353. */
  354. public function getInviteCodeMember($inviteCode)
  355. {
  356. $member = self::select('member_id')
  357. ->where('invite_code', $inviteCode)
  358. ->with([
  359. 'hasOneMember' => function ($q) {
  360. $q->select('uid', 'nickname', 'avatar', 'realname');
  361. }
  362. ])
  363. ->uniacid()
  364. ->first();
  365. if ($member) {
  366. return $member;
  367. } else {
  368. return false;
  369. }
  370. }
  371. /**
  372. * 获取父id
  373. *
  374. * @param $member_id
  375. * @return int parent_id
  376. */
  377. public function getParentId($member_id)
  378. {
  379. return self::uniacid()->where('member_id', $member_id)->value('parent_id');
  380. }
  381. /**
  382. * 会员ID检索
  383. * @param $query
  384. * @param $memberId
  385. * @return mixed
  386. */
  387. public function scopeOfMemberId($query, $memberId)
  388. {
  389. return $query->where('member_id', $memberId);
  390. }
  391. }