| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace addons\HiGo\backend\controllers;
- use addons\HiGo\common\enums\HiGoEnum;
- use addons\HiGo\common\logic\HiBeiLogic;
- use addons\HiGo\common\logic\WalletLogic;
- use addons\HiGo\common\models\HigoHibeiLog;
- use addons\HiGo\common\models\HigoMall;
- use addons\HiGo\common\models\HigoMerchantRewardLog;
- use addons\HiGo\common\models\HigoWallet;
- use addons\HiGo\common\models\HigoWalletLog;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use Yii;
- use Exception;
- /**
- * 默认控制器
- *
- * Class HiBeiController
- * @package addons\HiGo\backend\controllers
- */
- class HiBeiController extends BaseController
- {
- public $enableCsrfValidation = false;
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- if (Yii::$app->request->isAjax) {
- $get = Yii::$app->request->get();
- $user_id = $get['user_id'] ?? 0;
- $keyword = $get['keyword'] ?? '';
- $remark = $get['remark'] ?? '';
- $type = $get['type'] ?? '';
- $from_type = $get['from_type'] ?? '';
- $filter = [];
- if ($user_id > 0) {
- $filter[] = ['user_id' => $user_id];
- }
- if (!empty($keyword)) {
- $search_user_ids = User::find()->where([
- 'or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]
- ])->select(['id'])->asArray()->all();
- $search_user_ids = array_column($search_user_ids, 'id');
- if ($search_user_ids) {
- $filter[] = ['user_id' => $search_user_ids];
- } else {
- $filter[] = ['user_id' => []];
- }
- }
- if (!empty($remark)) {
- $filter[] = ['like', 'desc', $remark];
- }
- if (!empty($type)) {
- $filter[] = ['change_type' => $type];
- }
- if (!empty($from_type)) {
- $filter[] = ['from_type' => $from_type];
- }
- $params = [
- 'where' => array_merge([
- ['mall_id' => $this->mall_id],
- ['wallet_type' => HiGoEnum::WALLET_TYPE_HI_BEI]
- ], $filter),
- 'with' => ['user'],
- 'order' => 'id DESC'
- ];
- $list = HigoWalletLog::listPage($params);
- if ($list === false) {
- return $this->error(HigoWalletLog::getStaticError());
- }
- $from_type_map = HiGoEnum::getFromTypeMap($this->mall_id);
- foreach ($list['list'] as &$item) {
- if($item['addon_name'] == HiGoEnum::ADDONS_NAME){
- $item['from_type'] = $from_type_map[$item['from_type']] ?? '';
- }else{
- $item['from_type'] = HiGoEnum::getAddonsFromTypeMap($item['addon_name'],$item['from_type']);
- }
- $item['before_num'] = $item['change_type'] == 'add' ? ($item['after_num'] - $item['change_num']) : ($item['after_num'] + $item['change_num']);
- }
- unset($item);
- $higo_mall = HigoMall::getOne([['mall_id' => $this->mall_id]], true);
- $total = HigoMerchantRewardLog::find()->where(['mall_id' => $this->mall_id])
- ->select('sum(profit_price) as total_profit_price,sum(profit) as total_profit')->asArray()->one();
- $list['orderStatistics'] = [
- 'total_send_hi_bei' => $higo_mall['hi_bei'] ?? 0,
- 'total_profit' => $total['total_profit_price'] ?? 0,
- 'total_deduct_hi_bei' => $total['total_profit'] ?? 0
- ];
- $list['from_type_config'] = HiGoEnum::getFromTypeMap();
- return $this->success('success', $list);
- }
- return $this->render('index', []);
- }
- public function actionRecharge()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $from_type = [
- HiGoEnum::WALLET_TYPE_HI_VALUE => HiGoEnum::FROM_TYPE_ADMIN_HI_VALUE,
- HiGoEnum::WALLET_TYPE_HI_BEI => HiGoEnum::FROM_TYPE_ADMIN_HI_BEI
- ];
- $rules = [
- [['user_id', 'price', 'wallet_type'], 'required'],
- [['price'], 'number'],
- [['remark'], 'string'],
- ['wallet_type', 'in', 'range' => array_keys($from_type)]
- ];
- $res = Yii::$app->services->validate->validate($params, $rules);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- try {
- $higo_wallet = HigoWallet::findOne(['mall_id' => $this->mall_id, 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED]);
- $name = WalletLogic::getWalletTypeMsg($this->mall_id, $params['wallet_type']);
- if ($params['price'] < 0) {
- $wallet_value = $higo_wallet ? $higo_wallet->{$params['wallet_type']} : 0;
- $after_num = bcsub($wallet_value, abs($params['price']), 10);
- if (bccomp($after_num, 0, 10) < 0) {
- throw new Exception('用户' . $name . '不足');
- }
- } else {
- if (!HiBeiLogic::checkAddHibei($this->mall_id, $params['price'])) {
- throw new Exception('系统嗨呗不足');
- }
- }
- $params['remark'] = !empty($params['remark']) ? trim($params['remark']) : '系统后台' . ($params['price'] > 0 ? '充值' : '扣除') . strval(abs($params['price'])) . $name;
- $wallet_log = [
- 'mall_id' => $this->mall_id,
- 'user_id' => $params['user_id'],
- 'change_num' => $params['price'],
- 'desc' => $params['remark'],
- 'source_table' => HigoWallet::tableName(),
- 'source_table_id' => $higo_wallet ? $higo_wallet->id : 0,
- 'from_type' => $from_type[$params['wallet_type']],
- 'wallet_type' => $params['wallet_type'],
- 'is_send' => true
- ];
- $result = WalletLogic::handleInQueue($wallet_log);
- if ($result === false) {
- throw new Exception('系统繁忙,请稍后再试');
- }
- return $this->success('success');
- } catch (Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- return $this->error('非法的请求');
- }
- }
- /**
- * 嗨呗销毁记录
- * @return mixed|string|void
- */
- public function actionDestory()
- {
- if (Yii::$app->request->isAjax) {
- $get = Yii::$app->request->get();
- $params = [
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['from_type' => [HigoHibeiLog::FROM_TYPE_DESTROY,HigoHibeiLog::FROM_TYPE_TRANSFER_DESTROY]],
- ['change_type' =>'sub'],
- ],
- 'order' => 'id DESC'
- ];
- $list = HigoHibeiLog::listPage($params);
- if ($list === false) {
- return $this->error(HigoHibeiLog::getStaticError());
- }
- // foreach ($list['list'] as &$item) {
- //
- // }
- // unset($item);
- //计算总数量
- $total = HigoHibeiLog::find()->where(['mall_id' => $this->mall_id,'from_type' => [HigoHibeiLog::FROM_TYPE_DESTROY,HigoHibeiLog::FROM_TYPE_TRANSFER_DESTROY],'change_type' =>'sub'])
- ->select('sum(change_num) as total')->asArray()->one();
- $list['orderStatistics'] = $total;
- return $this->success('success', $list);
- }
- return $this->render('destory', []);
- }
- }
|