| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- namespace common\services;
- use biz\merchant\classes\ShopClass;
- use Yii;
- use common\components\stringUtil;
- use common\components\configDict;
- 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);
- $merchantId = $recharge['merchantId'];
- $adminId = 0;
- $merchant = xhMerchantService::getById($merchantId);
- $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
- $merchantAsset = xhMerchantAssetService::getByMerchantId($merchantId);
- $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);
- $typeList = configDict::getConfig('capitalType');//流水类型列表
- $capitalType = $typeList['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,
- 'merchantId' => $merchantId,
- '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'],
- 'merchantId' => $merchantId,
- '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' => "现金充值",
- 'merchantId' => $merchantId,
- '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' => '充值没有成功'];
- }
- }
-
- public static function recharge($rechargeAmount, $user, $adminId, $merchant, $merchantExtend, $merchantAsset)
- {
- $connection = Yii::$app->db;//事务处理
- $transaction = $connection->beginTransaction();
- try {
- $merchantId = $merchant['id'];
- $userId = $user['id'];
- $payWay = 3;//现金方式
- $payStyle = 1;
- $now = time();
- $date = date("Y-m-d H:i:s", $now);
- $typeList = configDict::getConfig('capitalType');//流水类型列表
- $capitalType = $typeList['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,
- 'merchantId' => $merchantId,
- 'userId' => $userId,
- 'userName' => $user['userName'],
- 'amount' => $rechargeAmount,
- 'balance' => $totalBalance,
- 'event' => "现金充值 {$rechargeAmount}元",
- 'addTime' => $now,
- 'userTotalRecharge' => $totalRecharge,
- 'merchantTotalRecharge' => $merchantTotalRecharge,
- 'status' => 1,
- ];
- $order = self::add($rechargeData);//用户充值记录增加
-
- $upData['integral'] = $totalIntegral;
- $upData['balance'] = $totalBalance;
- $upData['totalRecharge'] = $totalRecharge;
- xhUserAssetService::ioChangeAsset($user, $userAsset, $upData, $merchant, $merchantExtend, $order, $capitalType);//用户资产变化
-
- //充值暂时算到默认门店
- $shopId = ShopClass::getDefaultShopId($merchant);
- $mPreDeal = $merchantAsset['totalDeal'];
- $mDeal = $mPreDeal + 1;//交易量+1
- $upMData = ['totalDeal' => $mDeal, 'totalRecharge' => $merchantTotalRecharge];
- xhMerchantAssetService::incomeChangeAsset($merchant, $merchantAsset, $shopId, $upMData, $order, $capitalType, true);//商家资产变化
-
- $integralData = [
- 'userId' => $userId,
- 'userName' => $user['userName'],
- 'merchantId' => $merchantId,
- 'relateId' => (string)$order['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)$order['id'],
- 'balance' => $totalBalance,
- 'totalIncome' => $userAsset['totalIncome'],
- 'totalExpend' => $userAsset['totalExpend'],
- 'amount' => $rechargeAmount,
- 'io' => 1,
- 'payWay' => $payWay,
- 'event' => "现金充值",
- 'merchantId' => $merchantId,
- 'userId' => $userId,
- 'userName' => $user['userName'],
- 'operateId' => $adminId,
- 'createTime' => $date,
- 'capitalType' => $capitalType,
- 'addTime' => time(),
- ];
- xhUserCapitalService::add($userCapitalData);//用户资金流水增加
-
- $transaction->commit();
- return ['code' => 'A0001', 'msg' => '充值成功', 'data' => []];
- } catch (Exception $e) {
- $transaction->rollBack();
- return ['code' => 'A0002', 'msg' => '充值没有成功'];
- }
-
- }
-
- }
|