UserWallet.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace common\models\user;
  3. use common\enums\StatusEnum;
  4. use common\helpers\WechatHelper;
  5. use common\models\base\BaseActiveRecord;
  6. use common\models\mall\Mall;
  7. use common\models\order\OrderMarketing;
  8. use PHPUnit\Util\Exception;
  9. use Yii;
  10. /**
  11. * This is the model class for table "{{%user_wallet}}".
  12. *
  13. *
  14. * @property int $id
  15. * @property int $user_id 会员id
  16. * @property int $mall_id 商城id
  17. * @property float $score 可用积分
  18. * @property float $frozen_score 冻结积分
  19. * @property float $total_score 总积分
  20. * @property float $balance 可用余额
  21. * @property float $frozen_balance 冻结余额
  22. * @property float $total_balance 累计余额
  23. * @property float $commission 可用佣金
  24. * @property float $frozen_commission 冻结佣金
  25. * @property float $total_commission 总佣金
  26. * @property float $total_use_score 已使用积分
  27. * @property float $total_use_balance 已使用余额
  28. * @property float $total_use_commission 已使用佣金
  29. * @property float $balance_withdrawal 余额提现总金额
  30. * @property float $commission_withdrawal 佣金提现总金额
  31. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  32. * @property int $created_at
  33. * @property int $updated_at
  34. */
  35. class UserWallet extends BaseActiveRecord
  36. {
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public static function tableName()
  41. {
  42. return '{{%user_wallet}}';
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function rules()
  48. {
  49. return [
  50. [['user_id','mall_id', 'status', 'created_at', 'updated_at'], 'integer'],
  51. [['score', 'frozen_score', 'total_score', 'balance', 'frozen_balance', 'total_balance', 'commission', 'frozen_commission', 'total_commission',
  52. 'total_use_score', 'total_use_balance', 'total_use_commission', 'balance_withdrawal', 'commission_withdrawal'], 'number'],
  53. ];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'user_id' => '会员id',
  63. 'mall_id' => '商城id',
  64. 'score' => '可用积分',
  65. 'frozen_score' => '冻结积分',
  66. 'total_score' => '总积分',
  67. 'balance' => '可用余额',
  68. 'frozen_balance' => '冻结余额',
  69. 'total_balance' => '累计余额',
  70. 'commission' => '可用佣金',
  71. 'frozen_commission' => '冻结佣金',
  72. 'total_commission' => '总佣金',
  73. 'total_use_score' => '已使用积分',
  74. 'total_use_balance' => '已使用余额',
  75. 'total_use_commission' => '已使用佣金',
  76. 'balance_withdrawal' => '余额提现总金额',
  77. 'commission_withdrawal' => '佣金提现总金额',
  78. 'status' => '状态[-1:删除;0:禁用;1启用]',
  79. 'created_at' => 'Created At',
  80. 'updated_at' => 'Updated At',
  81. ];
  82. }
  83. /**
  84. * 保存数据
  85. * @param array $params
  86. * @return bool|UserWallet
  87. */
  88. public static function setData(array $params)
  89. {
  90. try {
  91. if (!empty($params['user_id'])) {
  92. $model = self::findOne(['user_id' => $params['user_id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  93. if (empty($model)) {
  94. $model = new self();
  95. $model->loadDefaultValues();
  96. }
  97. } else {
  98. if (empty($model)) throw new Exception('用户尚未注册,请先注册');
  99. }
  100. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  101. return $model;
  102. } catch (Exception $e) {
  103. self::$static_error = $e->getMessage();
  104. return false;
  105. }
  106. }
  107. /**
  108. * @desc:
  109. * @Author: hua
  110. * @Date: 2022/1/6
  111. * @Time: 9:15
  112. * @Copyright: copyright (c) 2021 广东七件事集团
  113. * @param $mall_id
  114. * @param $user_id
  115. * @param $order_id
  116. * @param $order_detail_ids
  117. * @param $source_table
  118. * @param $from_type
  119. * @param $capital_type
  120. * @return array
  121. */
  122. public static function getDeductScoreInsertWallet($mall_id,$user_id,$order_id,$order_detail_ids,$source_table,$from_type,$capital_type){
  123. $list = OrderMarketing::findAll(['order_detail_id'=>$order_detail_ids]);
  124. $deduct_score = 0;
  125. foreach ($list as $value){
  126. $deduct_score+=$value['deduct_score'];
  127. }
  128. $desc = '用户('.$user_id.'),下单('.$order_id.')抵扣积分,数量:'.$deduct_score;
  129. // echo $desc.PHP_EOL;
  130. if($deduct_score>0){
  131. return ['mall_id' => $mall_id, 'user_id' => $user_id,
  132. 'is_frozen' => false, 'wallet_type' => 'score', 'money' => -$deduct_score,
  133. 'desc' => $desc,
  134. 'source_table' => $source_table,
  135. 'source_table_id' => $order_id,
  136. 'from_type' => $from_type,
  137. 'capital_type' => $capital_type,
  138. ];
  139. }else{
  140. return [];
  141. }
  142. }
  143. //发送公众号消息
  144. // $send_message_data = ['user_id'=>$user_id,'money'=>$data['money'],'wallet_type'=>$wallet_type, 'mall_id' => $data['mall_id'], 'change_type'=>$setData['change_type'],];
  145. public static function receiptSendMessage($send_message_data){
  146. $mall_id = $send_message_data['mall_id'];
  147. $user_id = $send_message_data['user_id'];
  148. if($send_message_data['change_type']!='add'||$send_message_data['wallet_type']!='commission') return false;
  149. $user_info = UserInfo::findOne(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED,'platform'=>'wechat']);
  150. if (empty($user_info)) return false;
  151. $h5 = \Yii::$app->services->setting->mall->get($mall_id, 'basic.web_url');
  152. if (empty($h5)) return false;
  153. $mall = Mall::findOne(['id'=>$mall_id]);
  154. if(empty($mall)) return false;
  155. $h5 = substr($h5, 0, strpos($h5, '?'));
  156. $url = $h5.'plugins/public/promotion/index?title=';
  157. WechatHelper::sendTemplateMessage($mall_id,'commission_receipt',$user_info['openid'],$url,[]);
  158. }
  159. /**
  160. * 获取用户余额
  161. *
  162. * @param integer $user_id
  163. * @return float
  164. */
  165. public static function getUserBalance(int $user_id): float
  166. {
  167. return static::find()
  168. ->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])
  169. ->select('balance')
  170. ->scalar();
  171. }
  172. /**
  173. * 获取用户积分
  174. *
  175. * @param integer $user_id
  176. * @return float
  177. */
  178. public static function getUserScore(int $user_id): float
  179. {
  180. return static::find()
  181. ->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])
  182. ->select('score')
  183. ->scalar();
  184. }
  185. /**
  186. * 获取用户钱包数据
  187. *
  188. * @param integer $user_id
  189. * @param string $field
  190. * @return array
  191. */
  192. public static function getUserWallet(int $user_id, string $field = 'balance, score as total_score'): array
  193. {
  194. return static::find()
  195. ->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])
  196. ->select($field)
  197. ->asArray()
  198. ->one();
  199. }
  200. }