xhRechargeService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace common\services;
  3. use bizHd\merchant\classes\ShopClass;
  4. use Yii;
  5. use common\components\stringUtil;
  6. use common\components\dict;
  7. use common\models\xhRecharge;
  8. /**
  9. * 充值工具
  10. * @author sofashi
  11. */
  12. class xhRechargeService
  13. {
  14. public static function add($data)
  15. {
  16. return xhRecharge::add($data);
  17. }
  18. public static function getById($id)
  19. {
  20. return xhRecharge::getByCondition(['id' => $id]);
  21. }
  22. public static function updateById($id, $data)
  23. {
  24. return xhRecharge::updateById($id, $data);
  25. }
  26. public static function miniRecharge($orderId, $amount)
  27. {
  28. $recharge = self::getById($orderId);
  29. if (empty($recharge)) {
  30. return ['status' => false, 'msg' => '订单号:' . $orderId . '无效'];
  31. }
  32. if ($recharge['status'] == 1) {
  33. return ['status' => false, 'msg' => '订单已经充值成功了'];
  34. }
  35. $userId = $recharge['userId'];
  36. $user = xhUserService::getById($userId);
  37. $sjId = $recharge['sjId'];
  38. $adminId = 0;
  39. $merchant = xhMerchantService::getById($sjId);
  40. $merchantExtend = xhMerchantExtendService::getBySjId($sjId);
  41. $merchantAsset = xhMerchantAssetService::getBySjId($sjId);
  42. $rechargeAmount = $amount;
  43. //充值暂时算到默认门店
  44. $shopId = ShopClass::getDefaultShopId($merchant);
  45. $connection = Yii::$app->db;
  46. $transaction = $connection->beginTransaction();
  47. try {
  48. $payWay = 5;//小程序支付
  49. $payStyle = 0;
  50. $now = time();
  51. $date = date("Y-m-d H:i:s", $now);
  52. $capitalType = dict::getDict('capitalType','xhRecharge','id');//流水类型列表
  53. $userAsset = xhUserAssetService::getByUserId($userId);
  54. $gainIntegral = floor($rechargeAmount);//去掉小数点部分
  55. $totalIntegral = stringUtil::calcAdd($userAsset['integral'], $gainIntegral);
  56. $totalBalance = stringUtil::calcAdd($userAsset['balance'], $rechargeAmount);
  57. $totalRecharge = stringUtil::calcAdd($userAsset['totalRecharge'], $rechargeAmount);
  58. $merchantTotalRecharge = stringUtil::calcAdd($merchantAsset['totalRecharge'], $rechargeAmount);
  59. $rechargeData = [
  60. 'payWay' => $payWay,
  61. 'payStyle' => $payStyle,
  62. 'sjId' => $sjId,
  63. 'userId' => $userId,
  64. 'userName' => $user['userName'],
  65. 'amount' => $rechargeAmount,
  66. 'balance' => $totalBalance,
  67. 'event' => "现金充值 {$rechargeAmount}元",
  68. 'userTotalRecharge' => $totalRecharge,
  69. 'merchantTotalRecharge' => $merchantTotalRecharge,
  70. 'status' => 1,
  71. ];
  72. self::updateById($orderId, $rechargeData);
  73. $upData['integral'] = $totalIntegral;
  74. $upData['balance'] = $totalBalance;
  75. $upData['totalRecharge'] = $totalRecharge;
  76. xhUserAssetService::ioChangeAsset($user, $userAsset, $upData, $merchant, $merchantExtend, $recharge, $capitalType);//用户资产变化
  77. $mPreDeal = $merchantAsset['totalDeal'];
  78. $mDeal = $mPreDeal + 1;//交易量+1
  79. $upMData = ['totalDeal' => $mDeal, 'totalRecharge' => $merchantTotalRecharge];
  80. xhMerchantAssetService::incomeChangeAsset($merchant, $merchantAsset, $shopId, $upMData, $recharge, $capitalType);//商家资产变化
  81. $integralData = [
  82. 'userId' => $userId,
  83. 'userName' => $user['userName'],
  84. 'sjId' => $sjId,
  85. 'relateId' => (string)$recharge['id'],
  86. 'integral' => $totalIntegral,
  87. 'num' => $gainIntegral,
  88. 'io' => 1,
  89. 'payWay' => $payWay,
  90. 'event' => "小程序充值 {$rechargeAmount}元",
  91. 'operatorId' => $adminId,
  92. 'capitalType' => $capitalType,
  93. 'createTime' => $date,
  94. 'addTime' => $now,
  95. ];
  96. xhUserIntegralService::add($integralData);//用户兑换型积分流水增加
  97. $userCapitalData = [
  98. 'relateId' => (string)$recharge['id'],
  99. 'balance' => $totalBalance,
  100. 'totalIncome' => $userAsset['totalIncome'],
  101. 'totalExpend' => $userAsset['totalExpend'],
  102. 'amount' => $rechargeAmount,
  103. 'io' => 1,
  104. 'payWay' => $payWay,
  105. 'event' => "现金充值",
  106. 'sjId' => $sjId,
  107. 'userId' => $userId,
  108. 'userName' => $user['userName'],
  109. 'operateId' => $adminId,
  110. 'createTime' => $date,
  111. 'capitalType' => $capitalType,
  112. 'addTime' => time(),
  113. ];
  114. xhUserCapitalService::add($userCapitalData);//用户资金流水增加
  115. $transaction->commit();
  116. return ['status' => true, 'msg' => '充值成功'];
  117. } catch (Exception $e) {
  118. $transaction->rollBack();
  119. return ['status' => false, 'msg' => '充值没有成功'];
  120. }
  121. }
  122. }