| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace addons\ScoreExpansion\common\services;
- use addons\ScoreExpansion\common\enums\ScoreExpansionEnum;
- use addons\ScoreExpansion\common\models\ScoreExpansionInvertLog;
- use addons\ScoreExpansion\common\models\ScoreExpansionWallet;
- use addons\ScoreExpansion\common\models\ScoreExpansionWalletLog;
- use addons\Store\common\models\StoreOrderDetail;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\common\CapitalLog;
- use common\models\order\OrderDetail;
- use Exception;
- use RuntimeException;
- use services\common\UserWalletService;
- use Yii;
- class WalletService extends BaseService
- {
- /**
- * 积分转化
- *
- * @param array $params
- *
- * @return bool
- * @throws Exception
- */
- public function scoreInvite(array $params): bool
- {
- if ($params['order_source'] === 'Store') {
- $find = StoreOrderDetail::find();
- $sourceTable = 'store_order_detail';
- } else {
- $sourceTable = 'order_detail';
- $find = OrderDetail::find();
- }
- $orderDetailIds = $find
- ->where([
- 'order_id' => $params['order_id'],
- 'status' => StatusEnum::ENABLED,
- 'mall_id' => $this->mall_id,
- ])
- ->select('id')
- ->column();
- if (!$orderDetailIds) {
- $this->setError('未查询到订单');
- return false;
- }
- $existScoreLogId = ScoreExpansionInvertLog::find()
- ->where([
- 'mall_id' => $this->mall_id,
- 'status' => StatusEnum::ENABLED,
- 'addons_name' => $params['addons_name'],
- 'source_table' => $sourceTable,
- 'source_table_id' => $orderDetailIds
- ])
- ->select('score_log_id')
- ->column();
- CapitalLog::$capitalType = 'score';
- $scoreList = CapitalLog::lists([
- 'where' => [
- [
- 'source_table_id' => $orderDetailIds,
- 'mall_id' => $this->mall_id,
- 'source_table' => $sourceTable,
- 'from_type' => $params['addons_name'],
- 'change_type' => 'add',
- ],
- ['not in', 'id', $existScoreLogId]
- ]
- ]);
- if (!$scoreList) {
- $this->setError('未查询到积分记录');
- return false;
- }
- // 循环扣减积分,及转为积分拓客的冻结积分
- $t = Yii::$app->db->beginTransaction();
- try {
- $insertWallet = [];
- $desc = sprintf(
- '订单(%d)%s积分奖励转化为积分拓客冻结积分',
- $params['order_id'],
- [AddonsEnum::ADDONS_NAME_DISTRIBUTION => '分销商', AddonsEnum::ADDONS_NAME_AGENT => '渠道分红'][$params['addons_name']]
- );
- $insetInvertLog = [];
- $now = time();
- foreach ($scoreList as $item) {
- // 插入用户积分表
- $wallet = ScoreExpansionWallet::setData([
- 'mall_id' => $item['mall_id'],
- 'user_id' => $item['user_id'],
- 'is_frozen' => true,
- 'integral' => $item['change_num'],
- 'source_table' => CapitalLog::tableName(),
- 'source_table_id' => $item['id'],
- 'from_type' => ScoreExpansionEnum::FROM_SCORE_CONVERSION,
- 'desc' => $desc,
- ]);
- if ($wallet === false) {
- throw new RuntimeException(ScoreExpansionWallet::getStaticError());
- }
- $insertWallet[] = [
- 'mall_id' => $item['mall_id'],
- 'user_id' => $item['user_id'],
- 'is_frozen' => false,
- 'wallet_type' => 'score',
- 'money' => -$item['change_num'],
- 'desc' => $desc,
- 'source_table' => ScoreExpansionWalletLog::tableName(),
- 'source_table_id' => $wallet['log']['id'] ?? 0,
- 'from_type' => ScoreExpansionEnum::ADDONS_NAME,
- 'capital_type' => 'score_expansion_score_conversion',
- 'order_no' => $item['order_no'],
- ];
- $insetInvertLog[] = [
- 'mall_id' => $this->mall_id,
- 'addons_name' => $params['addons_name'],
- 'source_table' => $item['source_table'],
- 'source_table_id' => $item['source_table_id'],
- 'score_log_id' => $item['id'],
- 'created_at' => $now,
- 'updated_at' => $now,
- ];
- }
- !empty($insetInvertLog) && Yii::$app
- ->db
- ->createCommand()
- ->batchInsert(
- ScoreExpansionInvertLog::tableName(),
- array_keys($insetInvertLog[0]),
- $insetInvertLog
- )
- ->execute();
- $res = UserWalletService::batchHandleByQueue($insertWallet);
- if (empty($res)) {
- throw new RuntimeException(UserWalletService::getStaticError());
- }
- $t->commit();
- }catch (Exception $e) {
- $t->rollBack();
- $this->setError($e->getMessage());
- return false;
- }
- return true;
- }
- }
|