WalletService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace addons\ScoreExpansion\common\services;
  3. use addons\ScoreExpansion\common\enums\ScoreExpansionEnum;
  4. use addons\ScoreExpansion\common\models\ScoreExpansionInvertLog;
  5. use addons\ScoreExpansion\common\models\ScoreExpansionWallet;
  6. use addons\ScoreExpansion\common\models\ScoreExpansionWalletLog;
  7. use addons\Store\common\models\StoreOrderDetail;
  8. use common\enums\AddonsEnum;
  9. use common\enums\StatusEnum;
  10. use common\helpers\FormatHelper;
  11. use common\models\common\CapitalLog;
  12. use common\models\order\OrderDetail;
  13. use Exception;
  14. use RuntimeException;
  15. use services\common\UserWalletService;
  16. use Yii;
  17. class WalletService extends BaseService
  18. {
  19. /**
  20. * 积分转化
  21. *
  22. * @param array $params
  23. *
  24. * @return bool
  25. * @throws Exception
  26. */
  27. public function scoreInvite(array $params): bool
  28. {
  29. if ($params['order_source'] === 'Store') {
  30. $find = StoreOrderDetail::find();
  31. $sourceTable = 'store_order_detail';
  32. } else {
  33. $sourceTable = 'order_detail';
  34. $find = OrderDetail::find();
  35. }
  36. $orderDetailIds = $find
  37. ->where([
  38. 'order_id' => $params['order_id'],
  39. 'status' => StatusEnum::ENABLED,
  40. 'mall_id' => $this->mall_id,
  41. ])
  42. ->select('id')
  43. ->column();
  44. if (!$orderDetailIds) {
  45. $this->setError('未查询到订单');
  46. return false;
  47. }
  48. $existScoreLogId = ScoreExpansionInvertLog::find()
  49. ->where([
  50. 'mall_id' => $this->mall_id,
  51. 'status' => StatusEnum::ENABLED,
  52. 'addons_name' => $params['addons_name'],
  53. 'source_table' => $sourceTable,
  54. 'source_table_id' => $orderDetailIds
  55. ])
  56. ->select('score_log_id')
  57. ->column();
  58. CapitalLog::$capitalType = 'score';
  59. $scoreList = CapitalLog::lists([
  60. 'where' => [
  61. [
  62. 'source_table_id' => $orderDetailIds,
  63. 'mall_id' => $this->mall_id,
  64. 'source_table' => $sourceTable,
  65. 'from_type' => $params['addons_name'],
  66. 'change_type' => 'add',
  67. ],
  68. ['not in', 'id', $existScoreLogId]
  69. ]
  70. ]);
  71. if (!$scoreList) {
  72. $this->setError('未查询到积分记录');
  73. return false;
  74. }
  75. // 循环扣减积分,及转为积分拓客的冻结积分
  76. $t = Yii::$app->db->beginTransaction();
  77. try {
  78. $insertWallet = [];
  79. $desc = sprintf(
  80. '订单(%d)%s积分奖励转化为积分拓客冻结积分',
  81. $params['order_id'],
  82. [AddonsEnum::ADDONS_NAME_DISTRIBUTION => '分销商', AddonsEnum::ADDONS_NAME_AGENT => '渠道分红'][$params['addons_name']]
  83. );
  84. $insetInvertLog = [];
  85. $now = time();
  86. foreach ($scoreList as $item) {
  87. // 插入用户积分表
  88. $wallet = ScoreExpansionWallet::setData([
  89. 'mall_id' => $item['mall_id'],
  90. 'user_id' => $item['user_id'],
  91. 'is_frozen' => true,
  92. 'integral' => $item['change_num'],
  93. 'source_table' => CapitalLog::tableName(),
  94. 'source_table_id' => $item['id'],
  95. 'from_type' => ScoreExpansionEnum::FROM_SCORE_CONVERSION,
  96. 'desc' => $desc,
  97. ]);
  98. if ($wallet === false) {
  99. throw new RuntimeException(ScoreExpansionWallet::getStaticError());
  100. }
  101. $insertWallet[] = [
  102. 'mall_id' => $item['mall_id'],
  103. 'user_id' => $item['user_id'],
  104. 'is_frozen' => false,
  105. 'wallet_type' => 'score',
  106. 'money' => -$item['change_num'],
  107. 'desc' => $desc,
  108. 'source_table' => ScoreExpansionWalletLog::tableName(),
  109. 'source_table_id' => $wallet['log']['id'] ?? 0,
  110. 'from_type' => ScoreExpansionEnum::ADDONS_NAME,
  111. 'capital_type' => 'score_expansion_score_conversion',
  112. 'order_no' => $item['order_no'],
  113. ];
  114. $insetInvertLog[] = [
  115. 'mall_id' => $this->mall_id,
  116. 'addons_name' => $params['addons_name'],
  117. 'source_table' => $item['source_table'],
  118. 'source_table_id' => $item['source_table_id'],
  119. 'score_log_id' => $item['id'],
  120. 'created_at' => $now,
  121. 'updated_at' => $now,
  122. ];
  123. }
  124. !empty($insetInvertLog) && Yii::$app
  125. ->db
  126. ->createCommand()
  127. ->batchInsert(
  128. ScoreExpansionInvertLog::tableName(),
  129. array_keys($insetInvertLog[0]),
  130. $insetInvertLog
  131. )
  132. ->execute();
  133. $res = UserWalletService::batchHandleByQueue($insertWallet);
  134. if (empty($res)) {
  135. throw new RuntimeException(UserWalletService::getStaticError());
  136. }
  137. $t->commit();
  138. }catch (Exception $e) {
  139. $t->rollBack();
  140. $this->setError($e->getMessage());
  141. return false;
  142. }
  143. return true;
  144. }
  145. }