User.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\UserRechargeRepository;
  22. use app\common\repositories\user\UserRelationRepository;
  23. use app\common\repositories\user\UserRepository;
  24. use app\common\repositories\user\UserVisitRepository;
  25. /**
  26. * Class User
  27. * @package app\common\model\user
  28. * @author xaboy
  29. * @day 2020-04-28
  30. */
  31. class User extends BaseModel
  32. {
  33. /**
  34. * @return string
  35. * @author xaboy
  36. * @day 2020-03-30
  37. */
  38. public static function tablePk(): string
  39. {
  40. return 'uid';
  41. }
  42. /**
  43. * @return string
  44. * @author xaboy
  45. * @day 2020-03-30
  46. */
  47. public static function tableName(): string
  48. {
  49. return 'user';
  50. }
  51. /**
  52. * @param $value
  53. * @return string
  54. * @author xaboy
  55. * @day 2020-05-09
  56. */
  57. public function getBirthdayAttr($value)
  58. {
  59. return $value == '0000-00-00' ? '' : $value;
  60. }
  61. /**
  62. * @param $value
  63. * @return array
  64. * @author xaboy
  65. * @day 2020-05-09
  66. */
  67. public function getLabelIdAttr($value)
  68. {
  69. return $value ? explode(',', $value) : [];
  70. }
  71. /**
  72. * @param $value
  73. * @return string
  74. * @author xaboy
  75. * @day 2020-05-09
  76. */
  77. public function setLabelIdAttr($value)
  78. {
  79. return implode(',', $value);
  80. }
  81. public function getValidSpreadUidAttr()
  82. {
  83. if (!$this->spread_uid) return 0;
  84. else {
  85. $data = self::getDB()->where('uid', $this->spread_uid)->field('is_promoter,spread_uid')->find();
  86. if ($data && $data['is_promoter'])
  87. return $this->spread_uid;
  88. else
  89. return 0;
  90. }
  91. }
  92. public function getValidTopUidAttr()
  93. {
  94. if (!$this->top_uid) return 0;
  95. else {
  96. $data = self::getDB()->where('uid', $this->top_uid)->field('is_promoter,spread_uid')->find();
  97. if ($data && $data['is_promoter'])
  98. return $this->top_uid;
  99. else
  100. return 0;
  101. }
  102. }
  103. public function getTopUidAttr()
  104. {
  105. return self::getDB()->where('uid', $this->spread_uid)->value('spread_uid') ?: 0;
  106. }
  107. /**
  108. * @return \think\model\relation\HasOne
  109. * @author xaboy
  110. * @day 2020-05-09
  111. */
  112. public function group()
  113. {
  114. return $this->hasOne(UserGroup::class, 'group_id', 'group_id');
  115. }
  116. public function spread()
  117. {
  118. return $this->hasOne(User::class, 'uid', 'spread_uid');
  119. }
  120. /**
  121. * @param $spreadUid
  122. * @author xaboy
  123. * @day 2020-04-28
  124. */
  125. public function setSpread($spreadUid)
  126. {
  127. if (self::getDB()->where('uid', $spreadUid)->value('is_promoter'))
  128. $this->save([
  129. 'spread_uid' => $spreadUid,
  130. 'spread_time' => date('Y-m-d H:i:s')
  131. ]);
  132. }
  133. public function service()
  134. {
  135. return $this->hasOne(StoreService::class, 'uid', 'uid')->field('service_id,uid,nickname,avatar,customer,mer_id,is_verify')->where('is_del', 0)->where('status', 1);
  136. }
  137. public function getLockBrokerageAttr()
  138. {
  139. return app()->make(UserBillRepository::class)->lockBrokerage($this->uid) ?: 0;
  140. }
  141. public function getYesterdayBrokerageAttr()
  142. {
  143. return app()->make(UserBillRepository::class)->yesterdayBrokerage($this->uid) ?: 0;
  144. }
  145. public function getTotalExtractAttr()
  146. {
  147. return app()->make(UserExtractRepository::class)->userTotalExtract($this->uid) ?: 0;
  148. }
  149. public function getTotalBrokerageAttr()
  150. {
  151. return app()->make(UserBillRepository::class)->totalBrokerage($this->uid) ?: 0;
  152. }
  153. public function getTotalBrokeragePriceAttr()
  154. {
  155. return bcadd($this->lock_brokerage, $this->brokerage_price, 2);
  156. }
  157. public function getTotalRechargeAttr()
  158. {
  159. return app()->make(UserBillRepository::class)->userNowMoneyIncTotal($this->uid);
  160. }
  161. public function getTotalConsumeAttr()
  162. {
  163. return app()->make(StoreGroupOrderRepository::class)->totalNowMoney($this->uid);
  164. }
  165. public function getTotalCollectProductAttr()
  166. {
  167. return app()->make(UserRelationRepository::class)->getWhereCount(['uid' => $this->uid, 'type' => 1]);
  168. }
  169. public function getTotalCollectStoreAttr()
  170. {
  171. return app()->make(UserRelationRepository::class)->getWhereCount(['uid' => $this->uid, 'type' => 10]);
  172. }
  173. public function getTotalVisitProductAttr()
  174. {
  175. return app()->make(UserHistoryRepository::class)->userTotalHistory($this->uid);
  176. }
  177. public function getTotalCouponAttr()
  178. {
  179. return app()->make(StoreCouponUserRepository::class)->userTotal($this->uid);
  180. }
  181. public function getTotalUnreadAttr()
  182. {
  183. return app()->make(StoreServiceLogRepository::class)->totalUnReadNum($this->uid);
  184. }
  185. public function getOneLevelCountAttr()
  186. {
  187. return app()->make(UserRepository::class)->getOneLevelCount($this->uid);
  188. }
  189. public function getTwoLevelCountAttr()
  190. {
  191. return app()->make(UserRepository::class)->getTwoLevelCount($this->uid);
  192. }
  193. public function getSpreadTotalAttr()
  194. {
  195. return $this->one_level_count + $this->two_level_count;
  196. }
  197. public function wechat()
  198. {
  199. return $this->hasOne(WechatUser::class,'wechat_user_id','wechat_user_id');
  200. }
  201. public function getUserTypeAttr()
  202. {
  203. if($this->wechat['openid']){
  204. return 'wechat';
  205. }elseif($this->wechat['routine_openid']){
  206. return 'routine';
  207. }else{
  208. return 'H5';
  209. }
  210. }
  211. public function getSubscribeAttr()
  212. {
  213. if($this->wechat['openid'] && $this->wechat['subscribe']){
  214. return true;
  215. }else{
  216. return false;
  217. }
  218. }
  219. public function getAvatarAttr($value)
  220. {
  221. return $value ? $value : '/static/f.png';
  222. }
  223. }