MemberModel.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. /**
  3. * MemberModel.php UTF-8
  4. * 玩家Model
  5. *
  6. * @date : 2017/11/18 17:18
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\member;
  13. use huo\model\agent\AgentGameModel;
  14. use huo\model\common\CommonModel;
  15. use huo\model\user\UserModel;
  16. use huolib\constant\CacheConst;
  17. use huolib\constant\MemConst;
  18. use huolib\constant\OrderConst;
  19. use huomp\model\member\MemInvitedLogModel;
  20. class MemberModel extends CommonModel {
  21. protected $name = 'member';
  22. protected $pk = 'id';
  23. //设置只读字段
  24. protected $readonly = ['create_time'];
  25. // 开启自动写入时间戳字段
  26. protected $autoWriteTimestamp = true;
  27. //类型转换
  28. protected $type
  29. = [
  30. 'mobile_prefix' => 'integer',
  31. 'app_id' => 'integer',
  32. 'agent_id' => 'integer',
  33. 'parent_mem_id' => 'integer'
  34. ];
  35. protected $cache_key_prefix = CacheConst::CACHE_MEM_PREFIX;
  36. public function getAvatarAttr($value) {
  37. if (!empty($value)) {
  38. return cmf_get_image_url($value);
  39. }
  40. return $value;
  41. }
  42. public function getStatusLabelAttr($value) {
  43. if (!empty($value)) {
  44. return MemConst::getStatus($value);
  45. }
  46. return $value;
  47. }
  48. /**
  49. * 通过玩家ID获取注册时间
  50. *
  51. * @param int $mem_id 玩家ID
  52. *
  53. * @return string
  54. */
  55. public function getCreateTimeById($mem_id) {
  56. $_mem_data = $this->getInfoById($mem_id);
  57. return get_val($_mem_data, 'create_time', null);
  58. }
  59. public function operator() {
  60. return $this->hasOne('huo\model\game\OperatorModel', 'id', 'op_id')
  61. ->field(['id', 'name']);
  62. }
  63. public function country() {
  64. return $this->hasOne('huo\model\district\CountryConfModel', 'mobile_prefix', 'mobile_prefix');
  65. }
  66. public function agent() {
  67. return $this->hasOne(UserModel::className(), 'id', 'agent_id')
  68. ->field('id,user_login,user_nicename,role_id');
  69. }
  70. public function game() {
  71. return $this->hasOne('huo\model\game\GameModel', 'id', 'app_id')
  72. ->field(['id', 'name']);
  73. }
  74. public function mem() {
  75. return $this->hasOne(self::className(), 'id', 'parent_mem_id')
  76. ->field('id,username,nickname');
  77. }
  78. public function submem() {
  79. return $this->hasMany('huo\model\member\MemberModel', 'member_model.id', 'parent_mem_id');
  80. }
  81. public function invited() {
  82. return $this->belongsTo(MemInvitedLogModel::className(), 'id', 'mem_id');
  83. }
  84. /**
  85. * 关联mem_ext表
  86. *
  87. * @return \think\model\relation\HasOne
  88. */
  89. public function ext() {
  90. return $this->hasOne('huo\model\member\MemberExtModel', 'mem_id', 'id', [], 'left')->setEagerlyType(0);
  91. }
  92. public function mg() {
  93. return $this->hasMany("huo\model\member\MemGameModel", 'mem_id', 'id');
  94. }
  95. /**
  96. * @param string $value
  97. *
  98. * @return string
  99. */
  100. public function setPayPwdAttr($value) {
  101. if (!empty($value)) {
  102. return cmf_password($value);
  103. }
  104. return $value;
  105. }
  106. /**
  107. * @param string $value
  108. *
  109. * @return string
  110. */
  111. public function setPasswordAttr($value = '') {
  112. if (!empty($value)) {
  113. return cmf_password($value);
  114. }
  115. return $value;
  116. }
  117. /**
  118. * @param array $data
  119. *
  120. * @return bool|mixed
  121. */
  122. public function addMem($data = []) {
  123. $_data['username'] = get_val($data, 'username', '');
  124. $_data['nickname'] = get_val($data, 'nickname', '');
  125. $_data['reg_mobile'] = get_val($data, 'reg_mobile', '');
  126. $_data['reg_email'] = get_val($data, 'reg_email', '');
  127. $_data['password'] = get_val($data, 'password', '');
  128. $_data['pay_pwd'] = get_val($data, 'pay_pwd', '');
  129. $_data['email'] = get_val($data, 'email', '');
  130. $_data['mobile'] = get_val($data, 'mobile', '');
  131. $_data['from_device'] = get_val($data, 'from_device', '');
  132. $_data['device_id'] = get_val($data, 'device_id', '');
  133. $_data['app_id'] = get_val($data, 'app_id', 0);
  134. $_data['agent_id'] = get_val($data, 'agent_id', 0);
  135. $_data['agent_game'] = get_val($data, 'agent_game', '');
  136. $_data['status'] = get_val($data, 'status', MemConst::STATUS_NORMAL);
  137. $_data['is_switch'] = get_val($data, 'is_switch', OrderConst::PAY_SWITCH_NO);
  138. $_data['reg_ip'] = get_val($data, 'reg_ip', '');
  139. $_data['avatar'] = get_val($data, 'avatar', '');
  140. $_data['vb_id'] = get_val($data, 'vb_id', 0);
  141. $_data['parent_mem_id'] = get_val($data, 'parent_mem_id', 0);
  142. $_data['create_time'] = get_val($data, 'create_time', time());
  143. if ($_obj = self::create($_data, true)) {
  144. /* 写入扩展 */
  145. $_ext_data['last_login_time'] = time();
  146. $_ext_data['last_login_ip'] = $_data['reg_ip'];
  147. $_obj->ext()->save($_ext_data);
  148. /* Modified by guxiannong BEGIN 2018/8/01 ISSUES:5314 LTV */
  149. // $_ltv_class = new \huolib\ads\Ltv();
  150. // $_ltv_class->reg($_data['app_id'], $_data['agent_id'], $_ext_data['last_login_time']);
  151. /* END 2018/8/1 ISSUES:5314 */
  152. return $_obj->id;
  153. } else {
  154. return false;
  155. }
  156. }
  157. /**
  158. * 计算付费玩家数
  159. *
  160. * @param $where
  161. *
  162. * @return int|string
  163. */
  164. public function paidCount($where) {
  165. $where['sum_money'] = ['>', 0];
  166. return $this->with('ext')->where($where)->count('id');
  167. }
  168. /**
  169. * 总计
  170. *
  171. * @param array $where
  172. *
  173. * @return int
  174. */
  175. public function total($where = []) {
  176. return $this->getRegisterCount(0, time(), $where);
  177. // return $this->where($where)->count('id');
  178. }
  179. /**
  180. * 昨天新增
  181. *
  182. * @param array $where
  183. *
  184. * @return int
  185. */
  186. public function yesterdayCount($where = []) {
  187. return $this->getRegisterCount(strtotime('yesterday'), strtotime('today') - 1, $where);
  188. }
  189. /**
  190. * 今天新增
  191. *
  192. * @param array $map
  193. *
  194. * @return int
  195. */
  196. public function todayCount($map = []) {
  197. return $this->getRegisterCount(strtotime('today'), time(), $map);
  198. }
  199. /**
  200. * 本周新增
  201. *
  202. * @param array $map
  203. *
  204. * @return int
  205. */
  206. public function thisWeekCount($map = []) {
  207. $_start_time = strtotime(date('Y-m-d 00:00:00', strtotime('this week')));
  208. return $this->getRegisterCount($_start_time, time(), $map);
  209. }
  210. /**
  211. * 本月新增
  212. *
  213. * @param array $map
  214. *
  215. * @return int
  216. */
  217. public function thisMonthCount($map = []) {
  218. $_start_time = strtotime(date('Y-m-01 00:00:00'));
  219. return $this->getRegisterCount($_start_time, time(), $map);
  220. }
  221. /**
  222. * 注册数
  223. *
  224. * @param int $start_time 开始时间,包括
  225. * @param int $end_time 结束时间,包括
  226. *
  227. * @param array $map
  228. *
  229. * @return int
  230. */
  231. public function getRegisterCount($start_time, $end_time, $map = []) {
  232. $_map = [
  233. 'create_time' => ['between', [$start_time, $end_time]],
  234. // 'status' => MemConst::STATUS_NORMAL,
  235. ];
  236. return $this->where($map)->where($_map)->count('id');
  237. }
  238. /**
  239. * 自然注册玩家数
  240. *
  241. * @param array $map
  242. *
  243. * @return int|string
  244. */
  245. public function nonAgentUserCount($map = []) {
  246. $_map = $map;
  247. return $this->where($_map)->count('id');
  248. }
  249. /**
  250. * 渠道注册玩家数
  251. *
  252. * @param array $map
  253. *
  254. * @return int|string
  255. */
  256. public function agentUserCount($map = []) {
  257. $_map = $map;
  258. $_map['agent_id'] = ['>', 0];
  259. return $this->where($_map)->count('id');
  260. }
  261. /**
  262. * 今日活跃
  263. *
  264. * @param array $map
  265. *
  266. * @return int
  267. */
  268. public function todayActive($map = []) {
  269. $_map = $map;
  270. $_map['last_login_time'] = ['gt', strtotime('today')];
  271. return $this->with('ext')->where($_map)->count('id');
  272. }
  273. /**
  274. * 总活跃数
  275. *
  276. * @param array $where
  277. *
  278. * @return int
  279. */
  280. public function totalActive($where = []) {
  281. $where['last_login_time'] = ['gt', 0];
  282. $result = $this->with('ext')->where($where)->count('id');
  283. return $result;
  284. }
  285. /**
  286. * 昨天活跃数
  287. *
  288. * @param array $where
  289. *
  290. * @return int
  291. */
  292. public function yesterdayActive($where = []) {
  293. return $this->getActiveCount(strtotime('yesterday'), strtotime('today') - 1, $where);
  294. }
  295. /**
  296. * 本周活跃数
  297. *
  298. * @param array $where
  299. *
  300. * @return int
  301. */
  302. public function thisWeekActive($where = []) {
  303. return $this->getActiveCount(strtotime(date('Y-m-d 00:00:00', strtotime('this week'))), time(), $where);
  304. }
  305. /**
  306. * 本月活跃数
  307. *
  308. * @param array $where
  309. *
  310. * @return int
  311. */
  312. public function thisMonthActive($where = []) {
  313. return $this->getActiveCount(strtotime(date('Y-m-01 00:00:00')), time(), $where);
  314. }
  315. /**
  316. * 获取活跃数
  317. *
  318. * @param int $start_time 开始时间
  319. * @param int $end_time 结束时间
  320. * @param array $where 其他条件
  321. *
  322. * @return int|string
  323. */
  324. public function getActiveCount($start_time, $end_time, $where = []) {
  325. return $this->with('ext')->where($where)->where('last_login_time', 'between', [$start_time, $end_time])
  326. ->count('DISTINCT id');
  327. }
  328. public function changeMemAgent($mem_id = 0, $agent_id = 0, $app_id = 0) {
  329. if (empty($mem_id)) {
  330. $mem_id = $this->id;
  331. }
  332. if (empty($agent_id)) {
  333. return false;
  334. }
  335. $_data = array();
  336. if ($app_id) {
  337. $_data['app_id'] = $app_id;
  338. $_agent_game_model = new AgentGameModel();
  339. $_agent_game = $_agent_game_model->where(array('agent_id' => $agent_id, 'app_id' => $app_id))->value(
  340. 'agent_game'
  341. );
  342. if ($_agent_game) {
  343. $_data['agent_game'] = $_agent_game;
  344. }
  345. }
  346. $_data['agent_id'] = $agent_id;
  347. $_re = self::update($_data, array('id' => $mem_id));
  348. if ($_re === false) {
  349. return false;
  350. }
  351. return true;
  352. }
  353. /***
  354. * 获取玩家ids
  355. *
  356. * @param array $map
  357. *
  358. * @return array
  359. */
  360. public function getMemIds($map = []) {
  361. return $this->where($map)->column('id');
  362. }
  363. public function getIdsByParentMemId($parent_mem_id, $where = []) {
  364. return $this->where('parent_mem_id', '=', $parent_mem_id)->where($where)->column('id');
  365. }
  366. public function getIdByUsername($username) {
  367. return $this->where('username', '=', $username)->value('id');
  368. }
  369. /**
  370. * 计算数量
  371. *
  372. * @param array $where
  373. *
  374. * @return int
  375. */
  376. public function getCnt($where = []) {
  377. $_map = $where;
  378. $_cnt = $this->useGlobalScope(false)->where($_map)->count();
  379. if (empty($_cnt)) {
  380. return 0;
  381. }
  382. return $_cnt;
  383. }
  384. /**
  385. * 查找玩家支付宝是否绑定玩家
  386. *
  387. * @param string $alipay_account
  388. *
  389. * @return int
  390. */
  391. public function getMemIdByAlipayAccount($alipay_account) {
  392. $_map = [
  393. 'alipay_account' => $alipay_account
  394. ];
  395. $_mem_id = $this->where($_map)->value('id');
  396. if (empty($_mem_id)) {
  397. return 0;
  398. }
  399. return $_mem_id;
  400. }
  401. public function getMemListByMobile($mobile) {
  402. if (empty($mobile)) {
  403. return [];
  404. }
  405. $_map = [
  406. 'mobile' => $mobile
  407. ];
  408. $_list = $this->where($_map)->select();
  409. if (empty($_list)) {
  410. return [];
  411. }
  412. return $_list;
  413. }
  414. public function getMemNumber($mobile) {
  415. if (empty($mobile)) {
  416. return [];
  417. }
  418. $_map = [
  419. 'mobile' => $mobile
  420. ];
  421. $_count = $this->where($_map)->count();
  422. if (empty($_count)) {
  423. return 0;
  424. }
  425. return $_count;
  426. }
  427. /**
  428. * 通过玩家ID获取手机号
  429. *
  430. * @param int $mem_id 玩家ID
  431. *
  432. * @return string
  433. */
  434. public function getMobileById($mem_id) {
  435. $_mem_data = $this->getInfoById($mem_id);
  436. return get_val($_mem_data, 'mobile', '');
  437. }
  438. /**
  439. * 查询玩家状态
  440. *
  441. * @param int $mem_id 玩家ID
  442. *
  443. * @return int
  444. */
  445. public function getStatus($mem_id) {
  446. $_data = $this->getInfoById($mem_id);
  447. return get_val($_data, 'status', 0);
  448. }
  449. }