HiBeiController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace addons\HiGo\backend\controllers;
  3. use addons\HiGo\common\enums\HiGoEnum;
  4. use addons\HiGo\common\logic\HiBeiLogic;
  5. use addons\HiGo\common\logic\WalletLogic;
  6. use addons\HiGo\common\models\HigoHibeiLog;
  7. use addons\HiGo\common\models\HigoMall;
  8. use addons\HiGo\common\models\HigoMerchantRewardLog;
  9. use addons\HiGo\common\models\HigoWallet;
  10. use addons\HiGo\common\models\HigoWalletLog;
  11. use common\enums\StatusEnum;
  12. use common\models\user\User;
  13. use Yii;
  14. use Exception;
  15. /**
  16. * 默认控制器
  17. *
  18. * Class HiBeiController
  19. * @package addons\HiGo\backend\controllers
  20. */
  21. class HiBeiController extends BaseController
  22. {
  23. public $enableCsrfValidation = false;
  24. /**
  25. * 首页
  26. *
  27. * @return string
  28. */
  29. public function actionIndex()
  30. {
  31. if (Yii::$app->request->isAjax) {
  32. $get = Yii::$app->request->get();
  33. $user_id = $get['user_id'] ?? 0;
  34. $keyword = $get['keyword'] ?? '';
  35. $remark = $get['remark'] ?? '';
  36. $type = $get['type'] ?? '';
  37. $from_type = $get['from_type'] ?? '';
  38. $filter = [];
  39. if ($user_id > 0) {
  40. $filter[] = ['user_id' => $user_id];
  41. }
  42. if (!empty($keyword)) {
  43. $search_user_ids = User::find()->where([
  44. 'or', ['like', 'nickname', $keyword], ['like', 'mobile', $keyword]
  45. ])->select(['id'])->asArray()->all();
  46. $search_user_ids = array_column($search_user_ids, 'id');
  47. if ($search_user_ids) {
  48. $filter[] = ['user_id' => $search_user_ids];
  49. } else {
  50. $filter[] = ['user_id' => []];
  51. }
  52. }
  53. if (!empty($remark)) {
  54. $filter[] = ['like', 'desc', $remark];
  55. }
  56. if (!empty($type)) {
  57. $filter[] = ['change_type' => $type];
  58. }
  59. if (!empty($from_type)) {
  60. $filter[] = ['from_type' => $from_type];
  61. }
  62. $params = [
  63. 'where' => array_merge([
  64. ['mall_id' => $this->mall_id],
  65. ['wallet_type' => HiGoEnum::WALLET_TYPE_HI_BEI]
  66. ], $filter),
  67. 'with' => ['user'],
  68. 'order' => 'id DESC'
  69. ];
  70. $list = HigoWalletLog::listPage($params);
  71. if ($list === false) {
  72. return $this->error(HigoWalletLog::getStaticError());
  73. }
  74. $from_type_map = HiGoEnum::getFromTypeMap($this->mall_id);
  75. foreach ($list['list'] as &$item) {
  76. if($item['addon_name'] == HiGoEnum::ADDONS_NAME){
  77. $item['from_type'] = $from_type_map[$item['from_type']] ?? '';
  78. }else{
  79. $item['from_type'] = HiGoEnum::getAddonsFromTypeMap($item['addon_name'],$item['from_type']);
  80. }
  81. $item['before_num'] = $item['change_type'] == 'add' ? ($item['after_num'] - $item['change_num']) : ($item['after_num'] + $item['change_num']);
  82. }
  83. unset($item);
  84. $higo_mall = HigoMall::getOne([['mall_id' => $this->mall_id]], true);
  85. $total = HigoMerchantRewardLog::find()->where(['mall_id' => $this->mall_id])
  86. ->select('sum(profit_price) as total_profit_price,sum(profit) as total_profit')->asArray()->one();
  87. $list['orderStatistics'] = [
  88. 'total_send_hi_bei' => $higo_mall['hi_bei'] ?? 0,
  89. 'total_profit' => $total['total_profit_price'] ?? 0,
  90. 'total_deduct_hi_bei' => $total['total_profit'] ?? 0
  91. ];
  92. $list['from_type_config'] = HiGoEnum::getFromTypeMap();
  93. return $this->success('success', $list);
  94. }
  95. return $this->render('index', []);
  96. }
  97. public function actionRecharge()
  98. {
  99. if (Yii::$app->request->isAjax) {
  100. if (Yii::$app->request->isPost) {
  101. $params = Yii::$app->request->post();
  102. $from_type = [
  103. HiGoEnum::WALLET_TYPE_HI_VALUE => HiGoEnum::FROM_TYPE_ADMIN_HI_VALUE,
  104. HiGoEnum::WALLET_TYPE_HI_BEI => HiGoEnum::FROM_TYPE_ADMIN_HI_BEI
  105. ];
  106. $rules = [
  107. [['user_id', 'price', 'wallet_type'], 'required'],
  108. [['price'], 'number'],
  109. [['remark'], 'string'],
  110. ['wallet_type', 'in', 'range' => array_keys($from_type)]
  111. ];
  112. $res = Yii::$app->services->validate->validate($params, $rules);
  113. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  114. try {
  115. $higo_wallet = HigoWallet::findOne(['mall_id' => $this->mall_id, 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED]);
  116. $name = WalletLogic::getWalletTypeMsg($this->mall_id, $params['wallet_type']);
  117. if ($params['price'] < 0) {
  118. $wallet_value = $higo_wallet ? $higo_wallet->{$params['wallet_type']} : 0;
  119. $after_num = bcsub($wallet_value, abs($params['price']), 10);
  120. if (bccomp($after_num, 0, 10) < 0) {
  121. throw new Exception('用户' . $name . '不足');
  122. }
  123. } else {
  124. if (!HiBeiLogic::checkAddHibei($this->mall_id, $params['price'])) {
  125. throw new Exception('系统嗨呗不足');
  126. }
  127. }
  128. $params['remark'] = !empty($params['remark']) ? trim($params['remark']) : '系统后台' . ($params['price'] > 0 ? '充值' : '扣除') . strval(abs($params['price'])) . $name;
  129. $wallet_log = [
  130. 'mall_id' => $this->mall_id,
  131. 'user_id' => $params['user_id'],
  132. 'change_num' => $params['price'],
  133. 'desc' => $params['remark'],
  134. 'source_table' => HigoWallet::tableName(),
  135. 'source_table_id' => $higo_wallet ? $higo_wallet->id : 0,
  136. 'from_type' => $from_type[$params['wallet_type']],
  137. 'wallet_type' => $params['wallet_type'],
  138. 'is_send' => true
  139. ];
  140. $result = WalletLogic::handleInQueue($wallet_log);
  141. if ($result === false) {
  142. throw new Exception('系统繁忙,请稍后再试');
  143. }
  144. return $this->success('success');
  145. } catch (Exception $e) {
  146. return $this->error($e->getMessage());
  147. }
  148. }
  149. return $this->error('非法的请求');
  150. }
  151. }
  152. /**
  153. * 嗨呗销毁记录
  154. * @return mixed|string|void
  155. */
  156. public function actionDestory()
  157. {
  158. if (Yii::$app->request->isAjax) {
  159. $get = Yii::$app->request->get();
  160. $params = [
  161. 'where' => [
  162. ['mall_id' => $this->mall_id],
  163. ['from_type' => [HigoHibeiLog::FROM_TYPE_DESTROY,HigoHibeiLog::FROM_TYPE_TRANSFER_DESTROY]],
  164. ['change_type' =>'sub'],
  165. ],
  166. 'order' => 'id DESC'
  167. ];
  168. $list = HigoHibeiLog::listPage($params);
  169. if ($list === false) {
  170. return $this->error(HigoHibeiLog::getStaticError());
  171. }
  172. // foreach ($list['list'] as &$item) {
  173. //
  174. // }
  175. // unset($item);
  176. //计算总数量
  177. $total = HigoHibeiLog::find()->where(['mall_id' => $this->mall_id,'from_type' => [HigoHibeiLog::FROM_TYPE_DESTROY,HigoHibeiLog::FROM_TYPE_TRANSFER_DESTROY],'change_type' =>'sub'])
  178. ->select('sum(change_num) as total')->asArray()->one();
  179. $list['orderStatistics'] = $total;
  180. return $this->success('success', $list);
  181. }
  182. return $this->render('destory', []);
  183. }
  184. }