SaasUserNew.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use yii\behaviors\TimestampBehavior;
  9. /**
  10. * Class SaasUser
  11. * @package app\models
  12. * @property integer $id
  13. * @property integer $mobile
  14. * @property integer $is_delete
  15. * @property string $price
  16. * @property integer $parent_id
  17. * @property integer $platform_open_id
  18. * @property integer $platform_open_id_merchant
  19. * @property integer $share_profit
  20. * @property integer $created_at
  21. * @property integer $updated_at
  22. * @property integer $store_id
  23. * @property string $name
  24. * @property string $avatar
  25. * @property integer $gender
  26. * @property string $access_token
  27. * @property integer $integral
  28. * @property integer $total_integral
  29. * @property string $bytedance_open_id
  30. * @property string $withdraw_method
  31. * @property integer $is_salesman
  32. * @property string $ali_user_id
  33. * @property string $league_price
  34. * @property string $ali_openId
  35. * @property integer $purchase_money
  36. * @property integer $is_cloud_inventory
  37. * @property integer $cloud_inventory_level
  38. * @property integer $level_name
  39. * @property integer $cloud_inventory_balance
  40. * @property integer $cloud_inventory_freeze_balance
  41. * @property integer $cloud_inventory_total_balance
  42. * @property integer $uid
  43. *
  44. */
  45. class SaasUserNew extends \yii\db\ActiveRecord
  46. {
  47. /**
  48. * 是否删除
  49. */
  50. const DELETE_STATUS_TRUE = 1;
  51. /**
  52. * 是否删除
  53. */
  54. const DELETE_STATUS_FALSE = 0;
  55. /**
  56. * @inheritdoc
  57. */
  58. public static function tableName()
  59. {
  60. return '{{%saas_user_new}}';
  61. }
  62. public function behaviors()
  63. {
  64. return [
  65. [
  66. // 自动更新创建和更新时间
  67. 'class' => TimestampBehavior::class,
  68. 'value' => time()
  69. ]
  70. ];
  71. }
  72. /**
  73. * @inheritdoc
  74. */
  75. public function rules()
  76. {
  77. return [
  78. ['share_profit', 'number'],
  79. [['platform_open_id', 'platform_open_id_merchant', 'access_token', 'name', 'avatar', 'bytedance_open_id', 'withdraw_method', 'ali_user_id', 'mobile', 'ali_openId'], 'string'],
  80. [['platform_open_id', 'platform_open_id_merchant', 'access_token', 'name', 'avatar'], 'trim'],
  81. [['id', 'parent_id', 'is_delete', 'created_at', 'updated_at', 'store_id', 'gender', 'is_salesman'], 'integer'],
  82. [['integral', 'total_integral'], 'number'],
  83. [['created_at', 'update_at'], 'safe']
  84. ];
  85. }
  86. /**
  87. * @inheritdoc
  88. */
  89. public function attributeLabels()
  90. {
  91. return [
  92. 'id' => 'ID',
  93. 'mobile' => '手机号',
  94. 'created_at' => '创建时间',
  95. 'updated_at' => '更新时间',
  96. 'parent_id' => '父ID',
  97. 'is_delete' => '是否已删除',
  98. 'store_id' => '商城ID',
  99. 'platform_open_id' => '平台小程序openid',
  100. 'platform_open_id_merchant' => '批发端小程序openid',
  101. 'share_profit' => '联盟佣金',
  102. 'name' => '昵称',
  103. 'avatar' => '头像',
  104. 'gender' => '性别',
  105. 'access_token' => 'token',
  106. 'withdraw_method' => '提现方式',
  107. 'league_price' => '联盟券',
  108. 'ali_user_id' => '支付宝user_id',
  109. 'integral' => '积分',
  110. 'total_integral' => '累计积分',
  111. 'ali_openId' => '支付宝新openid'
  112. ];
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public static function findIdentityByAccessToken($token)
  118. {
  119. return static::findOne(['access_token' => $token]);
  120. }
  121. /**
  122. * @param $user_id
  123. * @return int
  124. */
  125. public static function findSaasIdByUserId($user_id)
  126. {
  127. $user = User::find()->alias('u')
  128. ->leftJoin(['su' => SaasUserNew::tableName()], 'su.mobile=u.binding')
  129. ->where([
  130. 'u.id' => $user_id,
  131. ])
  132. ->select('su.id as saas_user_id, u.id as user_id')
  133. ->asArray()
  134. ->one();
  135. return $user ? $user['saas_user_id'] : 0;
  136. }
  137. /**
  138. * @param $store_id
  139. * @param $user_id
  140. * @return int
  141. */
  142. public static function findUserIdByStoreIdAndSaasId($store_id, $saas_user_id)
  143. {
  144. $user = self::find()->alias('su')
  145. ->leftJoin(['u' => User::tableName()], 'su.mobile=u.binding')
  146. ->where([
  147. 'su.id' => $saas_user_id,
  148. 'u.store_id' => $store_id,
  149. ])
  150. ->select('su.id as saas_user_id, u.id as user_id')
  151. ->asArray()
  152. ->one();
  153. return $user ? $user['user_id'] : 0;
  154. }
  155. /**
  156. * @param $user_id
  157. * @return int
  158. */
  159. public static function findFirstStoreIdByUserId($user_id)
  160. {
  161. $user = User::find()->alias('u')
  162. ->leftJoin(['su' => SaasUserNew::tableName()], 'su.mobile=u.binding')
  163. ->where([
  164. 'u.id' => $user_id,
  165. ])
  166. ->select('su.id as saas_user_id, u.id as user_id, su.store_id')
  167. ->asArray()
  168. ->one();
  169. return $user ? $user['store_id'] : 0;
  170. }
  171. /**
  172. * @param $user_id
  173. * @return int
  174. */
  175. public static function findSaasParentIdByUserId($user_id)
  176. {
  177. $user = User::find()->alias('u')
  178. ->leftJoin(['su' => SaasUserNew::tableName()], 'su.mobile=u.binding')
  179. ->where([
  180. 'u.id' => $user_id,
  181. ])
  182. ->select('su.parent_id as saas_parent_user_id, u.id as user_id')
  183. ->asArray()
  184. ->one();
  185. return $user ? $user['saas_parent_user_id'] : 0;
  186. }
  187. public function beforeSave($insert)
  188. {
  189. if (parent::beforeSave($insert)) {
  190. if (empty($this->uid)){
  191. $saasUser = SaasUserNew::find()->orderBy(['id' => SORT_DESC])->one();
  192. if (!empty($saasUser['uid'])){
  193. $this->uid = intval($saasUser['uid'] + rand(10,30));
  194. }else{
  195. $this->uid = intval($saasUser['id'] + rand(10,30));
  196. }
  197. }
  198. return true;
  199. }
  200. return false;
  201. }
  202. public static function setUid()
  203. {
  204. $saasUser = SaasUserNew::find()->where(['is', 'uid', null])->all();
  205. if (!empty($saasUser)){
  206. foreach ($saasUser as $item){
  207. $maxSaasUser = SaasUserNew::find()->orderBy(['uid' => SORT_DESC])->one();
  208. $uid = intval($maxSaasUser['uid'] + rand(10,30));
  209. SaasUserNew::updateAll(['uid'=> $uid],['id'=> $item['id']]);
  210. }
  211. }
  212. return true;
  213. }
  214. }