| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace addons\QiShangTong\common\service;
- use addons\QiShangTong\common\enums\QiShangTongEnums;
- use addons\QiShangTong\common\models\QiShangTongBalance;
- use addons\QiShangTong\common\models\QiShangTongBalanceLog;
- use common\enums\AddonsEnum;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\mall\Mall;
- use common\models\order\Order;
- class FundService
- {
- public function page(int $mall_id, array $params)
- {
- $where = [
- ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
- ];
- if (SettingService::isMasterMall($mall_id)) {
- if (!empty($params['keyword'])) {
- $ids = Mall::find()->where(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->andWhere(['or', ['like', 'id', $params['keyword']], ['like', 'id', $params['keyword']]])->select('id')->column();
- if (empty($ids)) return [];
- $where[] = ['mall_id' => $ids];
- }
- } else {
- $where[] = ['mall_id' => $mall_id];
- }
- if (!empty($params['date_start'])) $where[] = ['>=', 'created_at', strtotime($params['date_start'])];
- if (!empty($params['date_end'])) $where[] = ['<=', 'created_at', strtotime($params['date_end'])];
- $ret = QiShangTongBalanceLog::listPage([
- 'where' => $where,
- 'order' => 'id desc',
- 'limit' => $params['limit'] ?? 10,
- 'with' => ['mall']
- ]);
- if (!empty(QiShangTongBalanceLog::getStaticError())) return QiShangTongBalanceLog::getStaticError();
- if (!empty($ret['list'])) {
- foreach ($ret['list'] as &$item) {
- $item['type_txt'] = QiShangTongEnums::BALANCE_LOG_TYPE_MAP[$item['type']] ?? '未知类型';
- $item['logo'] = MallService::getLogo($item['id']);
- }
- }
- return $ret;
- }
- /**
- * 异动
- * @param array $params mall_id, amount, remark, type, direction, order_no, addons
- * @return string|QiShangTongBalanceLog
- */
- public function create(array $params)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $params['status'] = SettingService::isMasterMall($params['mall_id']) ? StatusEnum::DELETE : StatusEnum::ENABLED;
- $params['amount'] = intval(bcmul($params['amount'], 100));
- if ($params['status'] == StatusEnum::ENABLED) {
- $account = QiShangTongBalance::findOne(['mall_id' => $params['mall_id']]);
- if (empty($account)) {
- $account = new QiShangTongBalance();
- $account->mall_id = $params['mall_id'];
- }
- $balance = $account->balance;
- if ($params['direction'] == StatusEnum::ENABLED) { // +
- $account->balance = bcadd($account->balance, $params['amount']);
- } else { // -
- $account->balance = bcsub($account->balance, $params['amount']);
- }
- if ($account->balance < 0) throw new \Exception("余额不足,账户金额为: " .
- bcmul(empty($balance) ? 0 : $balance, 0.01, 2));
- if (!$account->save()) throw new \Exception($account->getErrorMessage());
- $params['before_amount'] = $balance;
- $params['after_amount'] = $account->balance;
- }
- $model = new QiShangTongBalanceLog();
- $model->loadDefaultValues();
- if (!$model->load($params, '') || !$model->save()) throw new \Exception($model->getErrorMessage());
- $t->commit();
- } catch (\Exception $exception) {
- $t->rollBack();
- return $exception->getMessage();
- }
- return $model;
- }
- /**
- * 充值
- * @param array $params
- * @param $admin
- * @return string|QiShangTongBalanceLog
- */
- public function recharge(array $params, $admin)
- {
- $data = [
- 'mall_id' => $params['mall_id'],
- 'amount' => abs($params['amount']),
- 'remark' => "ID为[{$admin['id']}]的雇员,为商城[{$params['mall_id']}]操作金额为[{$params['amount']}],其备注信息为[{$params['remark']}]",
- 'type' => $params['amount'] > 0 ? QiShangTongEnums::BALANCE_LOG_TYPE_RECHARGE : QiShangTongEnums::BALANCE_LOG_TYPE_DEDUCT,
- 'direction' => $params['amount'] > 0 ? 1 : 2,
- 'order_no' => Order::generateOrderNo('RC'),
- 'addons' => AddonsEnum::ADDONS_NAME_QI_SHANG_TONG
- ];
- return $this->create($data);
- }
- /**
- * @param array $params
- * @return mixed|string|true|null
- */
- public function setRate(array $params)
- {
- $account = QiShangTongBalance::findOne(['mall_id' => $params['mall_id']]);
- if (empty($account)) {
- $account = new QiShangTongBalance();
- $account->mall_id = $params['mall_id'];
- }
- $account->{$params['field']} = bcmul($params['val'], 100);
- if (!$account->save()) return $account->getErrorMessage();
- try {
- (new GoodsService())->changePrice($params['mall_id']);
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- return true;
- }
- public function statistics(array $params)
- {
- $rQuery = QiShangTongBalanceLog::find()->where(['direction' => 1]);
- $dQuery = QiShangTongBalanceLog::find()->where(['direction' => 2]);
- if (!empty($params['mall_id'])) {
- $rQuery->andWhere(['mall_id' => $params['mall_id']]);
- $dQuery->andWhere(['mall_id' => $params['mall_id']]);
- }
- $r = $rQuery->sum('amount');
- $d = $dQuery->sum('amount');
- return [
- 'recharge' => !empty($r) ? $r : 0,
- 'deduct' => !empty($d) ? $d : 0,
- ];
- }
- public static function asyncRefund(int $id)
- {
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE, ['id' => $id],
- self::class, 'refund');
- }
- public function refund($params)
- {
- if (empty($params) || empty($params['id'])) return true;
- $id = $params['id'];
- $t = \Yii::$app->db->beginTransaction();
- try {
- $fund = QiShangTongBalanceLog::findOne($id);
- if (empty($fund)) return true;
- $fund->status = StatusEnum::DELETE;
- if (!$fund->save()) throw new \Exception($fund->getErrorMessage());
- $mall = QiShangTongBalance::findOne(['mall_id' => $fund->mall_id]);
- $mall->balance += $fund->amount;
- if (!$mall->save()) throw new \Exception($mall->getErrorMessage());
- $t->commit();
- } catch (\Exception $e) {
- $t->rollBack();
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- return false;
- }
- return true;
- }
- }
|