User.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-04-28
  7. *
  8. *
  9. */
  10. namespace app\common\model\user;
  11. use app\common\model\BaseModel;
  12. use app\common\model\store\service\StoreService;
  13. use app\common\model\wechat\WechatUser;
  14. use app\common\repositories\store\coupon\StoreCouponUserRepository;
  15. use app\common\repositories\store\order\StoreGroupOrderRepository;
  16. use app\common\repositories\store\service\StoreServiceLogRepository;
  17. use app\common\repositories\user\UserBillRepository;
  18. use app\common\repositories\user\UserExtractRepository;
  19. use app\common\repositories\user\UserRechargeRepository;
  20. use app\common\repositories\user\UserRelationRepository;
  21. use app\common\repositories\user\UserRepository;
  22. use app\common\repositories\user\UserVisitRepository;
  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. /**
  60. * @param $value
  61. * @return array
  62. * @author xaboy
  63. * @day 2020-05-09
  64. */
  65. public function getLabelIdAttr($value)
  66. {
  67. return $value ? explode(',', $value) : [];
  68. }
  69. /**
  70. * @param $value
  71. * @return string
  72. * @author xaboy
  73. * @day 2020-05-09
  74. */
  75. public function setLabelIdAttr($value)
  76. {
  77. return implode(',', $value);
  78. }
  79. public function getValidSpreadUidAttr()
  80. {
  81. if (!$this->spread_uid) return 0;
  82. else {
  83. $data = self::getDB()->where('uid', $this->spread_uid)->field('is_promoter,spread_uid')->find();
  84. if ($data && $data['is_promoter'])
  85. return $this->spread_uid;
  86. else
  87. return 0;
  88. }
  89. }
  90. public function getValidTopUidAttr()
  91. {
  92. if (!$this->top_uid) return 0;
  93. else {
  94. $data = self::getDB()->where('uid', $this->top_uid)->field('is_promoter,spread_uid')->find();
  95. if ($data && $data['is_promoter'])
  96. return $this->top_uid;
  97. else
  98. return 0;
  99. }
  100. }
  101. public function getTopUidAttr()
  102. {
  103. return self::getDB()->where('uid', $this->spread_uid)->value('spread_uid') ?: 0;
  104. }
  105. /**
  106. * @return \think\model\relation\HasOne
  107. * @author xaboy
  108. * @day 2020-05-09
  109. */
  110. public function group()
  111. {
  112. return $this->hasOne(UserGroup::class, 'group_id', 'group_id');
  113. }
  114. public function spread()
  115. {
  116. return $this->hasOne(User::class, 'uid', 'spread_uid');
  117. }
  118. public function certification(){
  119. return $this->hasOne(UserCertification::class, 'uid', 'uid')->where(['status'=>1]);
  120. }
  121. /**
  122. * @param $spreadUid
  123. * @author xaboy
  124. * @day 2020-04-28
  125. */
  126. public function setSpread($spreadUid)
  127. {
  128. if (self::getDB()->where('uid', $spreadUid)->value('is_promoter'))
  129. $this->save([
  130. 'spread_uid' => $spreadUid,
  131. 'spread_time' => date('Y-m-d H:i:s')
  132. ]);
  133. }
  134. public function service()
  135. {
  136. return $this->hasOne(StoreService::class, 'uid', 'uid')->field('service_id,uid,nickname,avatar,customer,mer_id,is_verify')->where('is_del', 0);
  137. }
  138. public function getLockBrokerageAttr()
  139. {
  140. return app()->make(UserBillRepository::class)->lockBrokerage($this->uid) ?: 0;
  141. }
  142. public function getYesterdayBrokerageAttr()
  143. {
  144. return app()->make(UserBillRepository::class)->yesterdayBrokerage($this->uid) ?: 0;
  145. }
  146. public function getTotalExtractAttr()
  147. {
  148. return app()->make(UserExtractRepository::class)->userTotalExtract($this->uid) ?: 0;
  149. }
  150. public function getTotalBrokerageAttr()
  151. {
  152. return app()->make(UserBillRepository::class)->totalBrokerage($this->uid) ?: 0;
  153. }
  154. public function getTotalRechargeAttr()
  155. {
  156. return app()->make(UserRechargeRepository::class)->userRechargePrice($this->uid);
  157. }
  158. public function getTotalConsumeAttr()
  159. {
  160. return app()->make(StoreGroupOrderRepository::class)->totalNowMoney($this->uid);
  161. }
  162. public function getTotalCollectProductAttr()
  163. {
  164. return app()->make(UserRelationRepository::class)->getWhereCount(['uid' => $this->uid, 'type' => 1]);
  165. }
  166. public function getTotalCollectStoreAttr()
  167. {
  168. return app()->make(UserRelationRepository::class)->getWhereCount(['uid' => $this->uid, 'type' => 10]);
  169. }
  170. public function getTotalVisitProductAttr()
  171. {
  172. return app()->make(UserVisitRepository::class)->userTotalVisit($this->uid);
  173. }
  174. public function getTotalCouponAttr()
  175. {
  176. return app()->make(StoreCouponUserRepository::class)->userTotal($this->uid);
  177. }
  178. public function getTotalUnreadAttr()
  179. {
  180. return app()->make(StoreServiceLogRepository::class)->totalUnReadNum($this->uid);
  181. }
  182. public function getOneLevelCountAttr()
  183. {
  184. return app()->make(UserRepository::class)->getOneLevelCount($this->uid);
  185. }
  186. public function getTwoLevelCountAttr()
  187. {
  188. return app()->make(UserRepository::class)->getTwoLevelCount($this->uid);
  189. }
  190. public function getSpreadTotalAttr()
  191. {
  192. return $this->one_level_count + $this->two_level_count;
  193. }
  194. public function wechat()
  195. {
  196. return $this->hasOne(WechatUser::class,'wechat_user_id','wechat_user_id');
  197. }
  198. public function userRich()
  199. {
  200. return $this->hasOne(UserRich::class,'uid','uid');
  201. }
  202. public function areaManage()
  203. {
  204. return $this->hasOne(AreaManage::class,'uid','uid');
  205. }
  206. }