| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace common\models\user;
- use common\enums\StatusEnum;
- use common\helpers\WechatHelper;
- use common\models\base\BaseActiveRecord;
- use common\models\mall\Mall;
- use common\models\order\OrderMarketing;
- use PHPUnit\Util\Exception;
- use Yii;
- /**
- * This is the model class for table "{{%user_wallet}}".
- *
- *
- * @property int $id
- * @property int $user_id 会员id
- * @property int $mall_id 商城id
- * @property float $score 可用积分
- * @property float $frozen_score 冻结积分
- * @property float $total_score 总积分
- * @property float $balance 可用余额
- * @property float $frozen_balance 冻结余额
- * @property float $total_balance 累计余额
- * @property float $commission 可用佣金
- * @property float $frozen_commission 冻结佣金
- * @property float $total_commission 总佣金
- * @property float $total_use_score 已使用积分
- * @property float $total_use_balance 已使用余额
- * @property float $total_use_commission 已使用佣金
- * @property float $balance_withdrawal 余额提现总金额
- * @property float $commission_withdrawal 佣金提现总金额
- * @property int|null $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class UserWallet extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%user_wallet}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['user_id','mall_id', 'status', 'created_at', 'updated_at'], 'integer'],
- [['score', 'frozen_score', 'total_score', 'balance', 'frozen_balance', 'total_balance', 'commission', 'frozen_commission', 'total_commission',
- 'total_use_score', 'total_use_balance', 'total_use_commission', 'balance_withdrawal', 'commission_withdrawal'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'user_id' => '会员id',
- 'mall_id' => '商城id',
- 'score' => '可用积分',
- 'frozen_score' => '冻结积分',
- 'total_score' => '总积分',
- 'balance' => '可用余额',
- 'frozen_balance' => '冻结余额',
- 'total_balance' => '累计余额',
- 'commission' => '可用佣金',
- 'frozen_commission' => '冻结佣金',
- 'total_commission' => '总佣金',
- 'total_use_score' => '已使用积分',
- 'total_use_balance' => '已使用余额',
- 'total_use_commission' => '已使用佣金',
- 'balance_withdrawal' => '余额提现总金额',
- 'commission_withdrawal' => '佣金提现总金额',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- /**
- * 保存数据
- * @param array $params
- * @return bool|UserWallet
- */
- public static function setData(array $params)
- {
- try {
- if (!empty($params['user_id'])) {
- $model = self::findOne(['user_id' => $params['user_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- }
- } else {
- if (empty($model)) throw new Exception('用户尚未注册,请先注册');
- }
- if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- return $model;
- } catch (Exception $e) {
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * @desc:
- * @Author: hua
- * @Date: 2022/1/6
- * @Time: 9:15
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $mall_id
- * @param $user_id
- * @param $order_id
- * @param $order_detail_ids
- * @param $source_table
- * @param $from_type
- * @param $capital_type
- * @return array
- */
- public static function getDeductScoreInsertWallet($mall_id,$user_id,$order_id,$order_detail_ids,$source_table,$from_type,$capital_type){
- $list = OrderMarketing::findAll(['order_detail_id'=>$order_detail_ids]);
- $deduct_score = 0;
- foreach ($list as $value){
- $deduct_score+=$value['deduct_score'];
- }
- $desc = '用户('.$user_id.'),下单('.$order_id.')抵扣积分,数量:'.$deduct_score;
- // echo $desc.PHP_EOL;
- if($deduct_score>0){
- return ['mall_id' => $mall_id, 'user_id' => $user_id,
- 'is_frozen' => false, 'wallet_type' => 'score', 'money' => -$deduct_score,
- 'desc' => $desc,
- 'source_table' => $source_table,
- 'source_table_id' => $order_id,
- 'from_type' => $from_type,
- 'capital_type' => $capital_type,
- ];
- }else{
- return [];
- }
- }
- //发送公众号消息
- // $send_message_data = ['user_id'=>$user_id,'money'=>$data['money'],'wallet_type'=>$wallet_type, 'mall_id' => $data['mall_id'], 'change_type'=>$setData['change_type'],];
- public static function receiptSendMessage($send_message_data){
- $mall_id = $send_message_data['mall_id'];
- $user_id = $send_message_data['user_id'];
- if($send_message_data['change_type']!='add'||$send_message_data['wallet_type']!='commission') return false;
- $user_info = UserInfo::findOne(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED,'platform'=>'wechat']);
- if (empty($user_info)) return false;
- $h5 = \Yii::$app->services->setting->mall->get($mall_id, 'basic.web_url');
- if (empty($h5)) return false;
- $mall = Mall::findOne(['id'=>$mall_id]);
- if(empty($mall)) return false;
- $h5 = substr($h5, 0, strpos($h5, '?'));
- $url = $h5.'plugins/public/promotion/index?title=';
- WechatHelper::sendTemplateMessage($mall_id,'commission_receipt',$user_info['openid'],$url,[]);
- }
- /**
- * 获取用户余额
- *
- * @param integer $user_id
- * @return float
- */
- public static function getUserBalance(int $user_id): float
- {
- return static::find()
- ->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])
- ->select('balance')
- ->scalar();
- }
- /**
- * 获取用户积分
- *
- * @param integer $user_id
- * @return float
- */
- public static function getUserScore(int $user_id): float
- {
- return static::find()
- ->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])
- ->select('score')
- ->scalar();
- }
- /**
- * 获取用户钱包数据
- *
- * @param integer $user_id
- * @param string $field
- * @return array
- */
- public static function getUserWallet(int $user_id, string $field = 'balance, score as total_score'): array
- {
- return static::find()
- ->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])
- ->select($field)
- ->asArray()
- ->one();
- }
- }
|