xhRechargeService.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace common\services;
  3. use Yii;
  4. use common\components\stringUtil;
  5. use common\components\configDict;
  6. use common\models\xhRecharge;
  7. /**
  8. * 充值工具
  9. * @author sofashi
  10. */
  11. class xhRechargeService
  12. {
  13. public static function add($data)
  14. {
  15. return xhRecharge::add($data);
  16. }
  17. public static function getById($id)
  18. {
  19. return xhRecharge::getByCondition(['id'=>$id]);
  20. }
  21. public static function updateById($id, $data)
  22. {
  23. return xhRecharge::updateById($id, $data);
  24. }
  25. public static function miniRecharge($orderId, $amount)
  26. {
  27. $recharge = self::getById($orderId);
  28. if (empty($recharge)) {
  29. return ['status' => false, 'msg' => '订单号:' . $orderId . '无效'];
  30. }
  31. if ($recharge['status'] == 1) {
  32. return ['status' => false, 'msg' => '订单已经充值成功了'];
  33. }
  34. $userId = $recharge['userId'];
  35. $user = xhUserService::getById($userId);
  36. $merchantId = $recharge['merchantId'];
  37. $adminId = 0;
  38. $merchant = xhMerchantService::getById($merchantId);
  39. $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
  40. $merchantAsset = xhMerchantAssetService::getByMerchantId($merchantId);
  41. $rechargeAmount = $amount;
  42. $connection = Yii::$app->db;//事务处理
  43. $transaction = $connection->beginTransaction();
  44. try {
  45. $payWay = 5;//小程序支付
  46. $payStyle = 0;
  47. $now = time();
  48. $date = date("Y-m-d H:i:s", $now);
  49. $typeList = configDict::getConfig('capitalType');//流水类型列表
  50. $capitalType = $typeList['xhRecharge']['id'];//流水类型为充值订单
  51. $userAsset = xhUserAssetService::getByUserId($userId);
  52. $gainIntegral = floor($rechargeAmount);//去掉小数点部分
  53. $totalIntegral = stringUtil::calcAdd($userAsset['integral'], $gainIntegral);
  54. $totalBalance = stringUtil::calcAdd($userAsset['balance'], $rechargeAmount);
  55. $totalRecharge = stringUtil::calcAdd($userAsset['totalRecharge'], $rechargeAmount);
  56. $merchantTotalRecharge = stringUtil::calcAdd($merchantAsset['totalRecharge'], $rechargeAmount);
  57. $rechargeData = [
  58. 'payWay' => $payWay,
  59. 'payStyle' => $payStyle,
  60. 'merchantId' => $merchantId,
  61. 'userId' => $userId,
  62. 'userName' => $user['userName'],
  63. 'amount' => $rechargeAmount,
  64. 'balance' => $totalBalance,
  65. 'event' => "现金充值 {$rechargeAmount}元",
  66. 'userTotalRecharge' => $totalRecharge,
  67. 'merchantTotalRecharge' => $merchantTotalRecharge,
  68. 'status' => 1,
  69. ];
  70. self::updateById($orderId, $rechargeData);
  71. $upData['integral'] = $totalIntegral;
  72. $upData['balance'] = $totalBalance;
  73. $upData['totalRecharge'] = $totalRecharge;
  74. xhUserAssetService::ioChangeAsset($user, $userAsset, $upData, $merchant, $merchantExtend, $recharge, $capitalType);//用户资产变化
  75. $mPreDeal = $merchantAsset['totalDeal'];
  76. $mDeal = $mPreDeal + 1;//交易量+1
  77. $upMData = ['totalDeal' => $mDeal, 'totalRecharge' => $merchantTotalRecharge];
  78. xhMerchantAssetService::incomeChangeAsset($merchant, $merchantAsset, $upMData, $recharge, $capitalType);//商家资产变化
  79. $integralData = [
  80. 'userId' => $userId,
  81. 'userName' => $user['userName'],
  82. 'merchantId' => $merchantId,
  83. 'relateId' => (string)$recharge['id'],
  84. 'integral' => $totalIntegral,
  85. 'num' => $gainIntegral,
  86. 'io' => 1,
  87. 'payWay' => $payWay,
  88. 'event' => "小程序充值 {$rechargeAmount}元",
  89. 'operatorId' => $adminId,
  90. 'capitalType' => $capitalType,
  91. 'createTime' => $date,
  92. 'addTime' => $now,
  93. ];
  94. xhUserIntegralService::add($integralData);//用户兑换型积分流水增加
  95. $userCapitalData = [
  96. 'relateId' => (string)$recharge['id'],
  97. 'balance' => $totalBalance,
  98. 'totalIncome' => $userAsset['totalIncome'],
  99. 'totalExpend' => $userAsset['totalExpend'],
  100. 'amount' => $rechargeAmount,
  101. 'io' => 1,
  102. 'payWay' => $payWay,
  103. 'event' => "现金充值",
  104. 'merchantId' => $merchantId,
  105. 'userId' => $userId,
  106. 'userName' => $user['userName'],
  107. 'operateId' => $adminId,
  108. 'createTime' => $date,
  109. 'capitalType' => $capitalType,
  110. 'addTime' => time(),
  111. ];
  112. xhUserCapitalService::add($userCapitalData);//用户资金流水增加
  113. $transaction->commit();
  114. return ['status' => true, 'msg' => '充值成功'];
  115. } catch (Exception $e) {
  116. $transaction->rollBack();
  117. return ['status' => false, 'msg' => '充值没有成功'];
  118. }
  119. }
  120. public static function recharge($rechargeAmount, $user, $adminId, $merchant, $merchantExtend, $merchantAsset)
  121. {
  122. $connection = Yii::$app->db;//事务处理
  123. $transaction = $connection->beginTransaction();
  124. try {
  125. $merchantId = $merchant['id'];
  126. $userId = $user['id'];
  127. $payWay = 3;//现金方式
  128. $payStyle = 1;
  129. $now = time();
  130. $date = date("Y-m-d H:i:s", $now);
  131. $typeList = configDict::getConfig('capitalType');//流水类型列表
  132. $capitalType = $typeList['xhRecharge']['id'];//流水类型为充值订单
  133. $userAsset = xhUserAssetService::getByUserId($userId);
  134. $gainIntegral = floor($rechargeAmount);//去掉小数点部分
  135. $totalIntegral = stringUtil::calcAdd($userAsset['integral'], $gainIntegral);
  136. $totalBalance = stringUtil::calcAdd($userAsset['balance'], $rechargeAmount);
  137. $totalRecharge = stringUtil::calcAdd($userAsset['totalRecharge'], $rechargeAmount);
  138. $merchantTotalRecharge = stringUtil::calcAdd($merchantAsset['totalRecharge'], $rechargeAmount);
  139. $rechargeData = [
  140. 'id' => stringUtil::generateOrderNo($merchantId, $userId),
  141. 'payWay' => $payWay,
  142. 'payStyle' => $payStyle,
  143. 'merchantId' => $merchantId,
  144. 'userId' => $userId,
  145. 'userName' => $user['userName'],
  146. 'amount' => $rechargeAmount,
  147. 'balance' => $totalBalance,
  148. 'event' => "现金充值 {$rechargeAmount}元",
  149. 'createTime' => $date,
  150. 'addTime' => $now,
  151. 'userTotalRecharge' => $totalRecharge,
  152. 'merchantTotalRecharge' => $merchantTotalRecharge,
  153. 'status'=>1,
  154. ];
  155. $order = self::add($rechargeData);//用户充值记录增加
  156. $upData['integral'] = $totalIntegral;
  157. $upData['balance'] = $totalBalance;
  158. $upData['totalRecharge'] = $totalRecharge;
  159. xhUserAssetService::ioChangeAsset($user, $userAsset, $upData, $merchant, $merchantExtend, $order, $capitalType);//用户资产变化
  160. $mPreDeal = $merchantAsset['totalDeal'];
  161. $mDeal = $mPreDeal + 1;//交易量+1
  162. $upMData = ['totalDeal' => $mDeal, 'totalRecharge' => $merchantTotalRecharge];
  163. xhMerchantAssetService::incomeChangeAsset($merchant, $merchantAsset, $upMData, $order, $capitalType);//商家资产变化
  164. $integralData = [
  165. 'userId' => $userId,
  166. 'userName' => $user['userName'],
  167. 'merchantId' => $merchantId,
  168. 'relateId' => (string)$order['id'],
  169. 'integral' => $totalIntegral,
  170. 'num' => $gainIntegral,
  171. 'io' => 1,
  172. 'payWay' => $payWay,
  173. 'event' => "现金充值 {$rechargeAmount}元",
  174. 'operatorId' => $adminId,
  175. 'capitalType' => $capitalType,
  176. 'createTime' => $date,
  177. 'addTime' => $now,
  178. ];
  179. xhUserIntegralService::add($integralData);//用户兑换型积分流水增加
  180. $userCapitalData = [
  181. 'relateId' => (string)$order['id'],
  182. 'balance' => $totalBalance,
  183. 'totalIncome' => $userAsset['totalIncome'],
  184. 'totalExpend' => $userAsset['totalExpend'],
  185. 'amount' => $rechargeAmount,
  186. 'io' => 1,
  187. 'payWay' => $payWay,
  188. 'event' => "现金充值",
  189. 'merchantId' => $merchantId,
  190. 'userId' => $userId,
  191. 'userName' => $user['userName'],
  192. 'operateId' => $adminId,
  193. 'createTime' => $date,
  194. 'capitalType' => $capitalType,
  195. 'addTime' => time(),
  196. ];
  197. xhUserCapitalService::add($userCapitalData);//用户资金流水增加
  198. $transaction->commit();
  199. return ['code' => 'A0001', 'msg' => '充值成功', 'data' => []];
  200. } catch (Exception $e) {
  201. $transaction->rollBack();
  202. return ['code' => 'A0002', 'msg' => '充值没有成功'];
  203. }
  204. }
  205. }