SaasUser.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. use yii\db\Exception;
  10. /**
  11. * Class SaasUser
  12. * @package app\models
  13. * @property integer $id
  14. * @property integer $mobile
  15. * @property integer $is_delete
  16. * @property string $price
  17. * @property integer $parent_id
  18. * @property integer $platform_open_id
  19. * @property integer $platform_open_id_merchant
  20. * @property integer $platform_open_id_new
  21. * @property integer $share_profit
  22. * @property integer $created_at
  23. * @property integer $updated_at
  24. * @property integer $store_id
  25. * @property string $name
  26. * @property string $union_id
  27. * @property string $avatar
  28. * @property integer $gender
  29. * @property string $access_token
  30. * @property integer $integral
  31. * @property integer $total_integral
  32. * @property string $bytedance_open_id
  33. * @property string $withdraw_method
  34. * @property integer $is_salesman
  35. * @property string $ali_user_id
  36. * @property string $league_price
  37. * @property string $ali_openId
  38. * @property string $wechat_union_id
  39. * @property string $saas_union_id
  40. * @property integer $purchase_money
  41. * @property integer $is_cloud_inventory
  42. * @property integer $cloud_inventory_level
  43. * @property integer $level_name
  44. * @property integer $cloud_inventory_balance
  45. * @property integer $cloud_inventory_freeze_balance
  46. * @property integer $cloud_inventory_total_balance
  47. * @property integer $uid
  48. * @property integer $is_public_sphere
  49. *
  50. */
  51. class SaasUser extends \yii\db\ActiveRecord
  52. {
  53. /**
  54. * 是否删除
  55. */
  56. const DELETE_STATUS_TRUE = 1;
  57. /**
  58. * 是否删除
  59. */
  60. const DELETE_STATUS_FALSE = 0;
  61. const ERROR_MESSAGE = '';
  62. const DEFAULT_AVATAR = '/web/v1/statics/images/avatar.png';
  63. /**
  64. * @inheritdoc
  65. */
  66. public static function tableName()
  67. {
  68. return '{{%saas_user}}';
  69. }
  70. public function behaviors()
  71. {
  72. return [
  73. [
  74. // 自动更新创建和更新时间
  75. 'class' => TimestampBehavior::class,
  76. 'value' => time()
  77. ]
  78. ];
  79. }
  80. /**
  81. * @inheritdoc
  82. */
  83. public function rules()
  84. {
  85. return [
  86. ['share_profit', 'number'],
  87. [['platform_open_id', 'platform_open_id_merchant', 'access_token', 'name', 'avatar', 'bytedance_open_id', 'withdraw_method', 'ali_user_id', 'mobile', 'ali_openId'], 'string'],
  88. [['platform_open_id', 'platform_open_id_merchant', 'access_token', 'name', 'avatar', 'wechat_union_id', 'saas_union_id'], 'trim'],
  89. [['id', 'parent_id', 'is_delete', 'created_at', 'updated_at', 'store_id', 'gender', 'is_salesman', 'is_public_sphere'], 'integer'],
  90. [['integral', 'total_integral'], 'number'],
  91. [['created_at', 'update_at'], 'safe']
  92. ];
  93. }
  94. /**
  95. * @inheritdoc
  96. */
  97. public function attributeLabels()
  98. {
  99. return [
  100. 'id' => 'ID',
  101. 'mobile' => '手机号',
  102. 'created_at' => '创建时间',
  103. 'updated_at' => '更新时间',
  104. 'parent_id' => '父ID',
  105. 'is_delete' => '是否已删除',
  106. 'store_id' => '商城ID',
  107. 'platform_open_id' => '平台小程序openid',
  108. 'platform_open_id_merchant' => '批发端小程序openid',
  109. 'share_profit' => '联盟佣金',
  110. 'name' => '昵称',
  111. 'avatar' => '头像',
  112. 'gender' => '性别',
  113. 'access_token' => 'token',
  114. 'withdraw_method' => '提现方式',
  115. 'league_price' => '联盟券',
  116. 'ali_user_id' => '支付宝user_id',
  117. 'integral' => '积分',
  118. 'total_integral' => '累计积分',
  119. 'ali_openId' => '支付宝新openid',
  120. 'platform_open_id_new' => '串码联名小程序openid',
  121. ];
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public static function findIdentityByAccessToken($token)
  127. {
  128. return static::findOne(['access_token' => $token]);
  129. }
  130. /**
  131. * @param $user_id
  132. * @return int
  133. */
  134. public static function findSaasIdByUserId($user_id)
  135. {
  136. $user = User::find()->alias('u')
  137. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  138. ->where([
  139. 'u.id' => $user_id,
  140. ])
  141. ->select('su.id as saas_user_id, u.id as user_id')
  142. ->asArray()
  143. ->one();
  144. return $user ? $user['saas_user_id'] : 0;
  145. }
  146. public static function generatePhoneNumber()
  147. {
  148. $suffix = str_pad(mt_rand(0, 99999999), 8, '0', STR_PAD_LEFT);
  149. $phone = '100' . $suffix;
  150. while (self::find()->where(['mobile' => $phone])->exists()) {
  151. return self::generatePhoneNumber();
  152. }
  153. return $phone;
  154. }
  155. /**
  156. * @param $store_id
  157. * @param $user_id
  158. * @return int
  159. */
  160. public static function findUserIdByStoreIdAndSaasId($store_id, $saas_user_id)
  161. {
  162. $user = self::find()->alias('su')
  163. ->leftJoin(['u' => User::tableName()], 'su.mobile=u.binding')
  164. ->where([
  165. 'su.id' => $saas_user_id,
  166. 'u.store_id' => $store_id,
  167. ])
  168. ->select('su.id as saas_user_id, u.id as user_id')
  169. ->asArray()
  170. ->one();
  171. return $user ? $user['user_id'] : 0;
  172. }
  173. /**
  174. * @param $user_id
  175. * @return int
  176. */
  177. public static function findFirstStoreIdByUserId($user_id)
  178. {
  179. $user = User::find()->alias('u')
  180. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  181. ->where([
  182. 'u.id' => $user_id,
  183. ])
  184. ->select('su.id as saas_user_id, u.id as user_id, su.store_id')
  185. ->asArray()
  186. ->one();
  187. return $user ? $user['store_id'] : 0;
  188. }
  189. /**
  190. * @param $user_id
  191. * @return int
  192. */
  193. public static function findSaasParentIdByUserId($user_id)
  194. {
  195. $user = User::find()->alias('u')
  196. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  197. ->where([
  198. 'u.id' => $user_id,
  199. ])
  200. ->select('su.parent_id as saas_parent_user_id, u.id as user_id')
  201. ->asArray()
  202. ->one();
  203. return $user ? $user['saas_parent_user_id'] : 0;
  204. }
  205. public function beforeSave($insert)
  206. {
  207. if (parent::beforeSave($insert)) {
  208. if (empty($this->uid)) {
  209. $saasUser = SaasUser::find()->orderBy(['id' => SORT_DESC])->one();
  210. if (!empty($saasUser['uid'])) {
  211. $this->uid = intval($saasUser['uid'] + rand(10, 30));
  212. } else {
  213. $this->uid = intval($saasUser['id'] + rand(10, 30));
  214. }
  215. }
  216. return true;
  217. }
  218. return false;
  219. }
  220. public static function setUid()
  221. {
  222. $saasUser = SaasUser::find()->where(['is', 'uid', null])->all();
  223. if (!empty($saasUser)) {
  224. foreach ($saasUser as $item) {
  225. $maxSaasUser = SaasUser::find()->orderBy(['uid' => SORT_DESC])->one();
  226. $uid = intval($maxSaasUser['uid'] + rand(10, 30));
  227. SaasUser::updateAll(['uid' => $uid], ['id' => $item['id']]);
  228. }
  229. }
  230. return true;
  231. }
  232. public static function addPurchaseLog($saas_id, $store_id, $money, $desc, $log_type, $order_type)
  233. {
  234. $saasUser = SaasUser::findOne($saas_id);
  235. if ($log_type == 1) {
  236. $saasUser->purchase_money += $money;
  237. } else {
  238. if ($saasUser->purchase_money < $money) throw new \Exception("用户:{$saas_id} 云库存货款不足");
  239. $saasUser->purchase_money -= $money;
  240. }
  241. if (!$saasUser->save()) {
  242. throw new \Exception("用户:{$saas_id} 退采购金:{$money} 取消失败");
  243. }
  244. //3、写入资金变动记录
  245. $accountLog = new AccountLog();
  246. $accountLog->store_id = $store_id;
  247. $accountLog->user_id = $saasUser->id;
  248. $accountLog->saas_id = $saasUser->id;
  249. $accountLog->desc = $desc;
  250. $accountLog->log_type = $log_type;
  251. $accountLog->amount = $money;
  252. $accountLog->operator = $saasUser->name;
  253. $accountLog->operator_id = $saasUser->id;
  254. $accountLog->created_at = time();
  255. $accountLog->type = AccountLog::TYPE_PURCHASE_MONEY;
  256. $accountLog->order_type = $order_type;
  257. $accountLog->before = bcsub($saasUser->purchase_money, $money, 2);
  258. $accountLog->after = $saasUser->purchase_money;
  259. $accountLog->operator_type = 1;
  260. if (!$accountLog->save()) {
  261. throw new \Exception("写入资金变动记录失败");
  262. }
  263. }
  264. public static function addCloudInventoryLog($saas_id, $money, $desc, $status, $log_type)
  265. {
  266. $saasUser = SaasUser::findOne($saas_id);
  267. if ($log_type == 1) {
  268. $saasUser->cloud_inventory_balance += $money;
  269. } else {
  270. if ($saasUser->cloud_inventory_balance < $money) throw new \Exception("用户:{$saas_id} 云库存结算金不足");
  271. $saasUser->cloud_inventory_balance -= $money;
  272. }
  273. if (!$saasUser->save()) {
  274. throw new \Exception("用户:{$saas_id} 退采购金:{$money} 取消失败");
  275. }
  276. $cloudInventoryBalanceLog = new CloudInventoryBalanceLog();
  277. $cloudInventoryBalanceLog->action_id = $saasUser['id'];
  278. $cloudInventoryBalanceLog->saas_id = $saasUser['id'];
  279. $cloudInventoryBalanceLog->saas_name = $saasUser['name'];
  280. $cloudInventoryBalanceLog->saas_mobile = $saasUser['mobile'];
  281. $cloudInventoryBalanceLog->price = $money;
  282. $cloudInventoryBalanceLog->type = $log_type;
  283. $cloudInventoryBalanceLog->status = $status;
  284. $cloudInventoryBalanceLog->before_price = $saasUser->cloud_inventory_balance + $money;
  285. $cloudInventoryBalanceLog->after_price = $saasUser->cloud_inventory_balance;
  286. $cloudInventoryBalanceLog->desc = $desc;
  287. $cloudInventoryBalanceLog->purchase_order_id = 0;
  288. $cloudInventoryBalanceLog->purchase_order_no = 0;
  289. $cloudInventoryBalanceLog->order_type = CloudInventoryBalanceLog::TYPE_PURCHASE_CLOUD_INVENTORY_PURCHASE_MONEY;
  290. $cloudInventoryBalanceLog->created_at = time();
  291. if (!$cloudInventoryBalanceLog->save()) {
  292. throw new \Exception('云库存金额变动记录');
  293. }
  294. return true;
  295. }
  296. }