User.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\model\user;
  12. use app\common\model\BaseModel;
  13. use app\common\model\store\service\StoreService;
  14. use app\common\model\wechat\WechatUser;
  15. use app\common\repositories\store\coupon\StoreCouponUserRepository;
  16. use app\common\repositories\store\order\StoreGroupOrderRepository;
  17. use app\common\repositories\store\service\StoreServiceLogRepository;
  18. use app\common\repositories\user\UserBillRepository;
  19. use app\common\repositories\user\UserExtractRepository;
  20. use app\common\repositories\user\UserHistoryRepository;
  21. use app\common\repositories\user\UserRelationRepository;
  22. use app\common\repositories\user\UserRepository;
  23. /**
  24. * Class User
  25. * @package app\common\model\user
  26. * @author xaboy
  27. * @day 2020-04-28
  28. */
  29. class User extends BaseModel
  30. {
  31. /**
  32. * @return string
  33. * @author xaboy
  34. * @day 2020-03-30
  35. */
  36. public static function tablePk(): string
  37. {
  38. return 'uid';
  39. }
  40. /**
  41. * @return string
  42. * @author xaboy
  43. * @day 2020-03-30
  44. */
  45. public static function tableName(): string
  46. {
  47. return 'user';
  48. }
  49. /**
  50. * @param $value
  51. * @return string
  52. * @author xaboy
  53. * @day 2020-05-09
  54. */
  55. public function getBirthdayAttr($value)
  56. {
  57. return $value == '0000-00-00' ? '' : $value;
  58. }
  59. public function getIsSvipAttr($value)
  60. {
  61. if (systemConfig('svip_switch_status') == 1) {
  62. return $value;
  63. } else {
  64. return $value > 0 ? 0 : $value;
  65. }
  66. }
  67. /**
  68. * @param $value
  69. * @return array
  70. * @author xaboy
  71. * @day 2020-05-09
  72. */
  73. public function getLabelIdAttr($value)
  74. {
  75. return $value ? explode(',', $value) : [];
  76. }
  77. /**
  78. * @param $value
  79. * @return string
  80. * @author xaboy
  81. * @day 2020-05-09
  82. */
  83. public function setLabelIdAttr($value)
  84. {
  85. return is_array($value) ? implode(',', $value) : $value;
  86. }
  87. public function getValidSpreadUidAttr()
  88. {
  89. if (!$this->spread_uid) return 0;
  90. else {
  91. $data = self::getDB()->where('uid', $this->spread_uid)->field('is_promoter,spread_uid')->find();
  92. if ($data && $data['is_promoter'])
  93. return $this->spread_uid;
  94. else
  95. return 0;
  96. }
  97. }
  98. public function getValidSpreadAttr()
  99. {
  100. if (!$this->spread_uid) return null;
  101. else {
  102. $data = self::getDB()->where('uid', $this->spread_uid)->field('is_promoter,spread_uid,brokerage_level,uid')->find();
  103. if ($data && $data['is_promoter'])
  104. return $data;
  105. else
  106. return null;
  107. }
  108. }
  109. public function getValidTopUidAttr()
  110. {
  111. if (!$this->top_uid) return 0;
  112. else {
  113. $data = self::getDB()->where('uid', $this->top_uid)->field('is_promoter,spread_uid')->find();
  114. if ($data && $data['is_promoter'])
  115. return $this->top_uid;
  116. else
  117. return 0;
  118. }
  119. }
  120. public function getValidTopAttr()
  121. {
  122. if (!$this->top_uid) return null;
  123. else {
  124. $data = self::getDB()->where('uid', $this->top_uid)->field('is_promoter,spread_uid,brokerage_level,uid')->find();
  125. if ($data && $data['is_promoter'])
  126. return $data;
  127. else
  128. return null;
  129. }
  130. }
  131. public function getTopUidAttr()
  132. {
  133. return self::getDB()->where('uid', $this->spread_uid)->value('spread_uid') ?: 0;
  134. }
  135. /**
  136. * @return \think\model\relation\HasOne
  137. * @author xaboy
  138. * @day 2020-05-09
  139. */
  140. public function group()
  141. {
  142. return $this->hasOne(UserGroup::class, 'group_id', 'group_id');
  143. }
  144. public function spread()
  145. {
  146. return $this->hasOne(User::class, 'uid', 'spread_uid');
  147. }
  148. public function brokerage()
  149. {
  150. return $this->hasOne(UserBrokerage::class, 'brokerage_level', 'brokerage_level')->where('type',0);
  151. }
  152. public function member()
  153. {
  154. return $this->hasOne(UserBrokerage::class, 'brokerage_level', 'member_level')->where('type',1);
  155. }
  156. /**
  157. * @param $spreadUid
  158. * @author xaboy
  159. * @day 2020-04-28
  160. */
  161. public function setSpread($spreadUid)
  162. {
  163. if (self::getDB()->where('uid', $spreadUid)->value('is_promoter'))
  164. $this->save([
  165. 'spread_uid' => $spreadUid,
  166. 'spread_time' => date('Y-m-d H:i:s')
  167. ]);
  168. }
  169. public function service()
  170. {
  171. return $this->hasOne(StoreService::class, 'uid', 'uid')
  172. ->where('mer_id', '<>', 0)->field('service_id,uid,nickname,avatar,customer,mer_id,is_verify,is_goods')->where('is_del', 0)->where('status', 1)
  173. ->order('is_verify DESC,customer DESC');
  174. }
  175. public function topService()
  176. {
  177. return $this->hasOne(StoreService::class, 'uid', 'uid')
  178. ->where('mer_id', 0)->field('service_id,uid,nickname,avatar,customer,mer_id,is_verify,is_goods')->where('is_del', 0)->where('status', 1)
  179. ->order('is_verify DESC,customer DESC');
  180. }
  181. public function getLockBrokerageAttr()
  182. {
  183. return app()->make(UserBillRepository::class)->lockBrokerage($this->uid) ?: 0;
  184. }
  185. public function getLockIntegralAttr()
  186. {
  187. return app()->make(UserBillRepository::class)->lockIntegral($this->uid) ?: 0;
  188. }
  189. public function getYesterdayBrokerageAttr()
  190. {
  191. return app()->make(UserBillRepository::class)->yesterdayBrokerage($this->uid) ?: 0;
  192. }
  193. public function getTotalExtractAttr()
  194. {
  195. return app()->make(UserExtractRepository::class)->userTotalExtract($this->uid) ?: 0;
  196. }
  197. public function getTotalBrokerageAttr()
  198. {
  199. return app()->make(UserBillRepository::class)->totalBrokerage($this->uid) ?: 0;
  200. }
  201. public function getTotalBrokeragePriceAttr()
  202. {
  203. return bcadd($this->lock_brokerage, $this->brokerage_price, 2);
  204. }
  205. public function getTotalIntegralAttr()
  206. {
  207. return bcadd($this->lock_integral, $this->integral, 0);
  208. }
  209. public function getTotalRechargeAttr()
  210. {
  211. return app()->make(UserBillRepository::class)->userNowMoneyIncTotal($this->uid);
  212. }
  213. public function getTotalConsumeAttr()
  214. {
  215. return app()->make(StoreGroupOrderRepository::class)->totalNowMoney($this->uid);
  216. }
  217. public function getTotalCollectProductAttr()
  218. {
  219. return app()->make(UserRelationRepository::class)->getWhereCount(['uid' => $this->uid, 'type' => 1]);
  220. }
  221. public function getTotalCollectStoreAttr()
  222. {
  223. return app()->make(UserRelationRepository::class)->getWhereCount(['uid' => $this->uid, 'type' => 10]);
  224. }
  225. public function getTotalVisitProductAttr()
  226. {
  227. return app()->make(UserHistoryRepository::class)->userTotalHistory($this->uid);
  228. }
  229. public function getTotalCouponAttr()
  230. {
  231. return app()->make(StoreCouponUserRepository::class)->userTotal($this->uid, 0);
  232. }
  233. public function getTotalUnreadAttr()
  234. {
  235. return app()->make(StoreServiceLogRepository::class)->totalUnReadNum($this->uid);
  236. }
  237. public function getOneLevelCountAttr()
  238. {
  239. return app()->make(UserRepository::class)->getOneLevelCount($this->uid);
  240. }
  241. public function getTwoLevelCountAttr()
  242. {
  243. return app()->make(UserRepository::class)->getTwoLevelCount($this->uid);
  244. }
  245. public function getSpreadTotalAttr()
  246. {
  247. return $this->one_level_count + $this->two_level_count;
  248. }
  249. public function wechat()
  250. {
  251. return $this->hasOne(WechatUser::class, 'wechat_user_id', 'wechat_user_id');
  252. }
  253. public function getIntegralAttr($val)
  254. {
  255. return is_null($val) ? 0 : $val;
  256. }
  257. // public function getUserTypeAttr()
  258. // {
  259. // if($this->wechat['openid']){
  260. // return 'wechat';
  261. // }elseif($this->wechat['routine_openid']){
  262. // return 'routine';
  263. // }else{
  264. // return 'H5';
  265. // }
  266. // }
  267. public function getSubscribeAttr()
  268. {
  269. if ($this->wechat['openid'] && $this->wechat['subscribe']) {
  270. return true;
  271. } else {
  272. return false;
  273. }
  274. }
  275. }