| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use yii\behaviors\TimestampBehavior;
- use yii\db\Exception;
- /**
- * Class SaasUser
- * @package app\models
- * @property integer $id
- * @property integer $mobile
- * @property integer $is_delete
- * @property string $price
- * @property integer $parent_id
- * @property integer $platform_open_id
- * @property integer $platform_open_id_merchant
- * @property integer $platform_open_id_new
- * @property integer $share_profit
- * @property integer $created_at
- * @property integer $updated_at
- * @property integer $store_id
- * @property string $name
- * @property string $union_id
- * @property string $avatar
- * @property integer $gender
- * @property string $access_token
- * @property integer $integral
- * @property integer $total_integral
- * @property string $bytedance_open_id
- * @property string $withdraw_method
- * @property integer $is_salesman
- * @property string $ali_user_id
- * @property string $league_price
- * @property string $ali_openId
- * @property string $wechat_union_id
- * @property string $saas_union_id
- * @property integer $purchase_money
- * @property integer $is_cloud_inventory
- * @property integer $cloud_inventory_level
- * @property integer $level_name
- * @property integer $cloud_inventory_balance
- * @property integer $cloud_inventory_freeze_balance
- * @property integer $cloud_inventory_total_balance
- * @property integer $uid
- * @property integer $is_public_sphere
- *
- */
- class SaasUser extends \yii\db\ActiveRecord
- {
- /**
- * 是否删除
- */
- const DELETE_STATUS_TRUE = 1;
- /**
- * 是否删除
- */
- const DELETE_STATUS_FALSE = 0;
- const ERROR_MESSAGE = '';
- const DEFAULT_AVATAR = '/web/v1/statics/images/avatar.png';
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%saas_user}}';
- }
- public function behaviors()
- {
- return [
- [
- // 自动更新创建和更新时间
- 'class' => TimestampBehavior::class,
- 'value' => time()
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- ['share_profit', 'number'],
- [['platform_open_id', 'platform_open_id_merchant', 'access_token', 'name', 'avatar', 'bytedance_open_id', 'withdraw_method', 'ali_user_id', 'mobile', 'ali_openId'], 'string'],
- [['platform_open_id', 'platform_open_id_merchant', 'access_token', 'name', 'avatar', 'wechat_union_id', 'saas_union_id'], 'trim'],
- [['id', 'parent_id', 'is_delete', 'created_at', 'updated_at', 'store_id', 'gender', 'is_salesman', 'is_public_sphere'], 'integer'],
- [['integral', 'total_integral'], 'number'],
- [['created_at', 'update_at'], 'safe']
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'mobile' => '手机号',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'parent_id' => '父ID',
- 'is_delete' => '是否已删除',
- 'store_id' => '商城ID',
- 'platform_open_id' => '平台小程序openid',
- 'platform_open_id_merchant' => '批发端小程序openid',
- 'share_profit' => '联盟佣金',
- 'name' => '昵称',
- 'avatar' => '头像',
- 'gender' => '性别',
- 'access_token' => 'token',
- 'withdraw_method' => '提现方式',
- 'league_price' => '联盟券',
- 'ali_user_id' => '支付宝user_id',
- 'integral' => '积分',
- 'total_integral' => '累计积分',
- 'ali_openId' => '支付宝新openid',
- 'platform_open_id_new' => '串码联名小程序openid',
- ];
- }
- /**
- * {@inheritdoc}
- */
- public static function findIdentityByAccessToken($token)
- {
- return static::findOne(['access_token' => $token]);
- }
- /**
- * @param $user_id
- * @return int
- */
- public static function findSaasIdByUserId($user_id)
- {
- $user = User::find()->alias('u')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->where([
- 'u.id' => $user_id,
- ])
- ->select('su.id as saas_user_id, u.id as user_id')
- ->asArray()
- ->one();
- return $user ? $user['saas_user_id'] : 0;
- }
- public static function generatePhoneNumber()
- {
- $suffix = str_pad(mt_rand(0, 99999999), 8, '0', STR_PAD_LEFT);
- $phone = '100' . $suffix;
- while (self::find()->where(['mobile' => $phone])->exists()) {
- return self::generatePhoneNumber();
- }
- return $phone;
- }
- /**
- * @param $store_id
- * @param $user_id
- * @return int
- */
- public static function findUserIdByStoreIdAndSaasId($store_id, $saas_user_id)
- {
- $user = self::find()->alias('su')
- ->leftJoin(['u' => User::tableName()], 'su.mobile=u.binding')
- ->where([
- 'su.id' => $saas_user_id,
- 'u.store_id' => $store_id,
- ])
- ->select('su.id as saas_user_id, u.id as user_id')
- ->asArray()
- ->one();
- return $user ? $user['user_id'] : 0;
- }
- /**
- * @param $user_id
- * @return int
- */
- public static function findFirstStoreIdByUserId($user_id)
- {
- $user = User::find()->alias('u')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->where([
- 'u.id' => $user_id,
- ])
- ->select('su.id as saas_user_id, u.id as user_id, su.store_id')
- ->asArray()
- ->one();
- return $user ? $user['store_id'] : 0;
- }
- /**
- * @param $user_id
- * @return int
- */
- public static function findSaasParentIdByUserId($user_id)
- {
- $user = User::find()->alias('u')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
- ->where([
- 'u.id' => $user_id,
- ])
- ->select('su.parent_id as saas_parent_user_id, u.id as user_id')
- ->asArray()
- ->one();
- return $user ? $user['saas_parent_user_id'] : 0;
- }
- public function beforeSave($insert)
- {
- if (parent::beforeSave($insert)) {
- if (empty($this->uid)) {
- $saasUser = SaasUser::find()->orderBy(['id' => SORT_DESC])->one();
- if (!empty($saasUser['uid'])) {
- $this->uid = intval($saasUser['uid'] + rand(10, 30));
- } else {
- $this->uid = intval($saasUser['id'] + rand(10, 30));
- }
- }
- return true;
- }
- return false;
- }
- public static function setUid()
- {
- $saasUser = SaasUser::find()->where(['is', 'uid', null])->all();
- if (!empty($saasUser)) {
- foreach ($saasUser as $item) {
- $maxSaasUser = SaasUser::find()->orderBy(['uid' => SORT_DESC])->one();
- $uid = intval($maxSaasUser['uid'] + rand(10, 30));
- SaasUser::updateAll(['uid' => $uid], ['id' => $item['id']]);
- }
- }
- return true;
- }
- public static function addPurchaseLog($saas_id, $store_id, $money, $desc, $log_type, $order_type)
- {
- $saasUser = SaasUser::findOne($saas_id);
- if ($log_type == 1) {
- $saasUser->purchase_money += $money;
- } else {
- if ($saasUser->purchase_money < $money) throw new \Exception("用户:{$saas_id} 云库存货款不足");
- $saasUser->purchase_money -= $money;
- }
- if (!$saasUser->save()) {
- throw new \Exception("用户:{$saas_id} 退采购金:{$money} 取消失败");
- }
- //3、写入资金变动记录
- $accountLog = new AccountLog();
- $accountLog->store_id = $store_id;
- $accountLog->user_id = $saasUser->id;
- $accountLog->saas_id = $saasUser->id;
- $accountLog->desc = $desc;
- $accountLog->log_type = $log_type;
- $accountLog->amount = $money;
- $accountLog->operator = $saasUser->name;
- $accountLog->operator_id = $saasUser->id;
- $accountLog->created_at = time();
- $accountLog->type = AccountLog::TYPE_PURCHASE_MONEY;
- $accountLog->order_type = $order_type;
- $accountLog->before = bcsub($saasUser->purchase_money, $money, 2);
- $accountLog->after = $saasUser->purchase_money;
- $accountLog->operator_type = 1;
- if (!$accountLog->save()) {
- throw new \Exception("写入资金变动记录失败");
- }
- }
- public static function addCloudInventoryLog($saas_id, $money, $desc, $status, $log_type)
- {
- $saasUser = SaasUser::findOne($saas_id);
- if ($log_type == 1) {
- $saasUser->cloud_inventory_balance += $money;
- } else {
- if ($saasUser->cloud_inventory_balance < $money) throw new \Exception("用户:{$saas_id} 云库存结算金不足");
- $saasUser->cloud_inventory_balance -= $money;
- }
- if (!$saasUser->save()) {
- throw new \Exception("用户:{$saas_id} 退采购金:{$money} 取消失败");
- }
- $cloudInventoryBalanceLog = new CloudInventoryBalanceLog();
- $cloudInventoryBalanceLog->action_id = $saasUser['id'];
- $cloudInventoryBalanceLog->saas_id = $saasUser['id'];
- $cloudInventoryBalanceLog->saas_name = $saasUser['name'];
- $cloudInventoryBalanceLog->saas_mobile = $saasUser['mobile'];
- $cloudInventoryBalanceLog->price = $money;
- $cloudInventoryBalanceLog->type = $log_type;
- $cloudInventoryBalanceLog->status = $status;
- $cloudInventoryBalanceLog->before_price = $saasUser->cloud_inventory_balance + $money;
- $cloudInventoryBalanceLog->after_price = $saasUser->cloud_inventory_balance;
- $cloudInventoryBalanceLog->desc = $desc;
- $cloudInventoryBalanceLog->purchase_order_id = 0;
- $cloudInventoryBalanceLog->purchase_order_no = 0;
- $cloudInventoryBalanceLog->order_type = CloudInventoryBalanceLog::TYPE_PURCHASE_CLOUD_INVENTORY_PURCHASE_MONEY;
- $cloudInventoryBalanceLog->created_at = time();
- if (!$cloudInventoryBalanceLog->save()) {
- throw new \Exception('云库存金额变动记录');
- }
- return true;
- }
- }
|