xhRechargeService.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace common\services;
  3. use biz\merchant\classes\ShopClass;
  4. use Yii;
  5. use common\components\stringUtil;
  6. use common\components\configDict;
  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. $merchantId = $recharge['merchantId'];
  38. $adminId = 0;
  39. $merchant = xhMerchantService::getById($merchantId);
  40. $merchantExtend = xhMerchantExtendService::getByMerchantId($merchantId);
  41. $merchantAsset = xhMerchantAssetService::getByMerchantId($merchantId);
  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. $typeList = configDict::getConfig('capitalType');//流水类型列表
  53. $capitalType = $typeList['xhRecharge']['id'];//流水类型为充值订单
  54. $userAsset = xhUserAssetService::getByUserId($userId);
  55. $gainIntegral = floor($rechargeAmount);//去掉小数点部分
  56. $totalIntegral = stringUtil::calcAdd($userAsset['integral'], $gainIntegral);
  57. $totalBalance = stringUtil::calcAdd($userAsset['balance'], $rechargeAmount);
  58. $totalRecharge = stringUtil::calcAdd($userAsset['totalRecharge'], $rechargeAmount);
  59. $merchantTotalRecharge = stringUtil::calcAdd($merchantAsset['totalRecharge'], $rechargeAmount);
  60. $rechargeData = [
  61. 'payWay' => $payWay,
  62. 'payStyle' => $payStyle,
  63. 'merchantId' => $merchantId,
  64. 'userId' => $userId,
  65. 'userName' => $user['userName'],
  66. 'amount' => $rechargeAmount,
  67. 'balance' => $totalBalance,
  68. 'event' => "现金充值 {$rechargeAmount}元",
  69. 'userTotalRecharge' => $totalRecharge,
  70. 'merchantTotalRecharge' => $merchantTotalRecharge,
  71. 'status' => 1,
  72. ];
  73. self::updateById($orderId, $rechargeData);
  74. $upData['integral'] = $totalIntegral;
  75. $upData['balance'] = $totalBalance;
  76. $upData['totalRecharge'] = $totalRecharge;
  77. xhUserAssetService::ioChangeAsset($user, $userAsset, $upData, $merchant, $merchantExtend, $recharge, $capitalType);//用户资产变化
  78. $mPreDeal = $merchantAsset['totalDeal'];
  79. $mDeal = $mPreDeal + 1;//交易量+1
  80. $upMData = ['totalDeal' => $mDeal, 'totalRecharge' => $merchantTotalRecharge];
  81. xhMerchantAssetService::incomeChangeAsset($merchant, $merchantAsset, $shopId, $upMData, $recharge, $capitalType);//商家资产变化
  82. $integralData = [
  83. 'userId' => $userId,
  84. 'userName' => $user['userName'],
  85. 'merchantId' => $merchantId,
  86. 'relateId' => (string)$recharge['id'],
  87. 'integral' => $totalIntegral,
  88. 'num' => $gainIntegral,
  89. 'io' => 1,
  90. 'payWay' => $payWay,
  91. 'event' => "小程序充值 {$rechargeAmount}元",
  92. 'operatorId' => $adminId,
  93. 'capitalType' => $capitalType,
  94. 'createTime' => $date,
  95. 'addTime' => $now,
  96. ];
  97. xhUserIntegralService::add($integralData);//用户兑换型积分流水增加
  98. $userCapitalData = [
  99. 'relateId' => (string)$recharge['id'],
  100. 'balance' => $totalBalance,
  101. 'totalIncome' => $userAsset['totalIncome'],
  102. 'totalExpend' => $userAsset['totalExpend'],
  103. 'amount' => $rechargeAmount,
  104. 'io' => 1,
  105. 'payWay' => $payWay,
  106. 'event' => "现金充值",
  107. 'merchantId' => $merchantId,
  108. 'userId' => $userId,
  109. 'userName' => $user['userName'],
  110. 'operateId' => $adminId,
  111. 'createTime' => $date,
  112. 'capitalType' => $capitalType,
  113. 'addTime' => time(),
  114. ];
  115. xhUserCapitalService::add($userCapitalData);//用户资金流水增加
  116. $transaction->commit();
  117. return ['status' => true, 'msg' => '充值成功'];
  118. } catch (Exception $e) {
  119. $transaction->rollBack();
  120. return ['status' => false, 'msg' => '充值没有成功'];
  121. }
  122. }
  123. public static function recharge($rechargeAmount, $user, $adminId, $merchant, $merchantExtend, $merchantAsset)
  124. {
  125. $connection = Yii::$app->db;//事务处理
  126. $transaction = $connection->beginTransaction();
  127. try {
  128. $merchantId = $merchant['id'];
  129. $userId = $user['id'];
  130. $payWay = 3;//现金方式
  131. $payStyle = 1;
  132. $now = time();
  133. $date = date("Y-m-d H:i:s", $now);
  134. $typeList = configDict::getConfig('capitalType');//流水类型列表
  135. $capitalType = $typeList['xhRecharge']['id'];//流水类型为充值订单
  136. $userAsset = xhUserAssetService::getByUserId($userId);
  137. $gainIntegral = floor($rechargeAmount);//去掉小数点部分
  138. $totalIntegral = stringUtil::calcAdd($userAsset['integral'], $gainIntegral);
  139. $totalBalance = stringUtil::calcAdd($userAsset['balance'], $rechargeAmount);
  140. $totalRecharge = stringUtil::calcAdd($userAsset['totalRecharge'], $rechargeAmount);
  141. $merchantTotalRecharge = stringUtil::calcAdd($merchantAsset['totalRecharge'], $rechargeAmount);
  142. $rechargeData = [
  143. 'payWay' => $payWay,
  144. 'payStyle' => $payStyle,
  145. 'merchantId' => $merchantId,
  146. 'userId' => $userId,
  147. 'userName' => $user['userName'],
  148. 'amount' => $rechargeAmount,
  149. 'balance' => $totalBalance,
  150. 'event' => "现金充值 {$rechargeAmount}元",
  151. 'addTime' => $now,
  152. 'userTotalRecharge' => $totalRecharge,
  153. 'merchantTotalRecharge' => $merchantTotalRecharge,
  154. 'status' => 1,
  155. ];
  156. $order = self::add($rechargeData);//用户充值记录增加
  157. $upData['integral'] = $totalIntegral;
  158. $upData['balance'] = $totalBalance;
  159. $upData['totalRecharge'] = $totalRecharge;
  160. xhUserAssetService::ioChangeAsset($user, $userAsset, $upData, $merchant, $merchantExtend, $order, $capitalType);//用户资产变化
  161. //充值暂时算到默认门店
  162. $shopId = ShopClass::getDefaultShopId($merchant);
  163. $mPreDeal = $merchantAsset['totalDeal'];
  164. $mDeal = $mPreDeal + 1;//交易量+1
  165. $upMData = ['totalDeal' => $mDeal, 'totalRecharge' => $merchantTotalRecharge];
  166. xhMerchantAssetService::incomeChangeAsset($merchant, $merchantAsset, $shopId, $upMData, $order, $capitalType, true);//商家资产变化
  167. $integralData = [
  168. 'userId' => $userId,
  169. 'userName' => $user['userName'],
  170. 'merchantId' => $merchantId,
  171. 'relateId' => (string)$order['id'],
  172. 'integral' => $totalIntegral,
  173. 'num' => $gainIntegral,
  174. 'io' => 1,
  175. 'payWay' => $payWay,
  176. 'event' => "现金充值 {$rechargeAmount}元",
  177. 'operatorId' => $adminId,
  178. 'capitalType' => $capitalType,
  179. 'createTime' => $date,
  180. 'addTime' => $now,
  181. ];
  182. //xhUserIntegralService::add($integralData);//用户兑换型积分流水增加
  183. $userCapitalData = [
  184. 'relateId' => (string)$order['id'],
  185. 'balance' => $totalBalance,
  186. 'totalIncome' => $userAsset['totalIncome'],
  187. 'totalExpend' => $userAsset['totalExpend'],
  188. 'amount' => $rechargeAmount,
  189. 'io' => 1,
  190. 'payWay' => $payWay,
  191. 'event' => "现金充值",
  192. 'merchantId' => $merchantId,
  193. 'userId' => $userId,
  194. 'userName' => $user['userName'],
  195. 'operateId' => $adminId,
  196. 'createTime' => $date,
  197. 'capitalType' => $capitalType,
  198. 'addTime' => time(),
  199. ];
  200. xhUserCapitalService::add($userCapitalData);//用户资金流水增加
  201. $transaction->commit();
  202. return ['code' => 'A0001', 'msg' => '充值成功', 'data' => []];
  203. } catch (Exception $e) {
  204. $transaction->rollBack();
  205. return ['code' => 'A0002', 'msg' => '充值没有成功'];
  206. }
  207. }
  208. }