| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace common\services;
- use bizHd\merchant\classes\ShopClass;
- use Yii;
- use common\components\stringUtil;
- use common\components\dict;
- use common\models\xhRecharge;
- /**
- * 充值工具
- * @author sofashi
- */
- class xhRechargeService
- {
- public static function add($data)
- {
- return xhRecharge::add($data);
- }
- public static function getById($id)
- {
- return xhRecharge::getByCondition(['id' => $id]);
- }
- public static function updateById($id, $data)
- {
- return xhRecharge::updateById($id, $data);
- }
- public static function miniRecharge($orderId, $amount)
- {
- $recharge = self::getById($orderId);
- if (empty($recharge)) {
- return ['status' => false, 'msg' => '订单号:' . $orderId . '无效'];
- }
- if ($recharge['status'] == 1) {
- return ['status' => false, 'msg' => '订单已经充值成功了'];
- }
- $userId = $recharge['userId'];
- $user = xhUserService::getById($userId);
- $sjId = $recharge['sjId'];
- $adminId = 0;
- $merchant = xhMerchantService::getById($sjId);
- $merchantExtend = xhMerchantExtendService::getBySjId($sjId);
- $merchantAsset = xhMerchantAssetService::getBySjId($sjId);
- $rechargeAmount = $amount;
- //充值暂时算到默认门店
- $shopId = ShopClass::getDefaultShopId($merchant);
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- $payWay = 5;//小程序支付
- $payStyle = 0;
- $now = time();
- $date = date("Y-m-d H:i:s", $now);
- $capitalType = dict::getDict('capitalType','xhRecharge','id');//流水类型列表
- $userAsset = xhUserAssetService::getByUserId($userId);
- $gainIntegral = floor($rechargeAmount);//去掉小数点部分
- $totalIntegral = stringUtil::calcAdd($userAsset['integral'], $gainIntegral);
- $totalBalance = stringUtil::calcAdd($userAsset['balance'], $rechargeAmount);
- $totalRecharge = stringUtil::calcAdd($userAsset['totalRecharge'], $rechargeAmount);
- $merchantTotalRecharge = stringUtil::calcAdd($merchantAsset['totalRecharge'], $rechargeAmount);
- $rechargeData = [
- 'payWay' => $payWay,
- 'payStyle' => $payStyle,
- 'sjId' => $sjId,
- 'userId' => $userId,
- 'userName' => $user['userName'],
- 'amount' => $rechargeAmount,
- 'balance' => $totalBalance,
- 'event' => "现金充值 {$rechargeAmount}元",
- 'userTotalRecharge' => $totalRecharge,
- 'merchantTotalRecharge' => $merchantTotalRecharge,
- 'status' => 1,
- ];
- self::updateById($orderId, $rechargeData);
- $upData['integral'] = $totalIntegral;
- $upData['balance'] = $totalBalance;
- $upData['totalRecharge'] = $totalRecharge;
- xhUserAssetService::ioChangeAsset($user, $userAsset, $upData, $merchant, $merchantExtend, $recharge, $capitalType);//用户资产变化
- $mPreDeal = $merchantAsset['totalDeal'];
- $mDeal = $mPreDeal + 1;//交易量+1
- $upMData = ['totalDeal' => $mDeal, 'totalRecharge' => $merchantTotalRecharge];
- xhMerchantAssetService::incomeChangeAsset($merchant, $merchantAsset, $shopId, $upMData, $recharge, $capitalType);//商家资产变化
- $integralData = [
- 'userId' => $userId,
- 'userName' => $user['userName'],
- 'sjId' => $sjId,
- 'relateId' => (string)$recharge['id'],
- 'integral' => $totalIntegral,
- 'num' => $gainIntegral,
- 'io' => 1,
- 'payWay' => $payWay,
- 'event' => "小程序充值 {$rechargeAmount}元",
- 'operatorId' => $adminId,
- 'capitalType' => $capitalType,
- 'createTime' => $date,
- 'addTime' => $now,
- ];
- xhUserIntegralService::add($integralData);//用户兑换型积分流水增加
- $userCapitalData = [
- 'relateId' => (string)$recharge['id'],
- 'balance' => $totalBalance,
- 'totalIncome' => $userAsset['totalIncome'],
- 'totalExpend' => $userAsset['totalExpend'],
- 'amount' => $rechargeAmount,
- 'io' => 1,
- 'payWay' => $payWay,
- 'event' => "现金充值",
- 'sjId' => $sjId,
- 'userId' => $userId,
- 'userName' => $user['userName'],
- 'operateId' => $adminId,
- 'createTime' => $date,
- 'capitalType' => $capitalType,
- 'addTime' => time(),
- ];
- xhUserCapitalService::add($userCapitalData);//用户资金流水增加
- $transaction->commit();
- return ['status' => true, 'msg' => '充值成功'];
- } catch (Exception $e) {
- $transaction->rollBack();
- return ['status' => false, 'msg' => '充值没有成功'];
- }
- }
- }
|