DefaultController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace addons\ScoreBonus\backend\controllers;
  3. use common\models\user\User;
  4. use Yii;
  5. use addons\ScoreBonus\common\enums\ScoreBonusEnums;
  6. use common\controllers\AddonsController;
  7. use addons\ScoreBonus\common\models\ScoreBonusModel;
  8. use addons\ScoreBonus\common\models\ScoreBonusCapitalModel;
  9. use addons\ScoreBonus\common\models\ScoreBonusCapitalPool;
  10. use addons\ScoreBonus\common\models\ScoreBonusRewardLogModel;
  11. use addons\ScoreBonus\common\models\ScoreBonusUserWeight;
  12. use addons\ScoreBonus\common\models\ScoreBonusUserWeightLog;
  13. use common\models\user\UserWallet;
  14. use common\enums\StatusEnum;
  15. use common\helpers\csv\CsvExportHelper;
  16. use common\enums\DownloadEnum;
  17. /**
  18. * 默认控制器
  19. *
  20. * Class DefaultController
  21. * @package addons\ScoreBonus\backend\controllers
  22. */
  23. class DefaultController extends BaseController
  24. {
  25. /**
  26. * 首页
  27. *
  28. * @return string
  29. */
  30. // 获取结算明细
  31. public function actionIndex()
  32. {
  33. if (Yii::$app->request->isAjax) {
  34. if (Yii::$app->request->isGet) {
  35. $get = Yii::$app->request->get();
  36. $res = Yii::$app->services->validate->validate($get, [
  37. [['orderBy', 'num_text'], 'string'],
  38. [['status', 'user_id'], 'number'],
  39. [['start', 'end'], 'datetime', 'format' => 'php:Y-m-d H:i:s'],
  40. ]);
  41. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  42. $where = [
  43. ['mall_id' => $this->mall_id],
  44. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  45. ];
  46. if (isset($get['status'])) $where[] = ['status' => $get['status']];
  47. if (!empty($get['start'])) $where[] = ['>=', 'created_at', strtotime($get['start'])];
  48. if (!empty($get['end'])) $where[] = ['<=', 'created_at', strtotime($get['end'])];
  49. if (!empty($get['num_text'])) $where[] = ['like', 'num_text', $get['num_text']];
  50. $order = 'id desc';
  51. if (!empty($get['orderBy'])) $order = $get['orderBy'];
  52. // $list_params = compact('where', 'order');
  53. $list_params = array('where' => $where, 'order' => $order);
  54. $list = ScoreBonusModel::listPage($list_params, false, true);
  55. if (!empty($list['list'])) {
  56. foreach ($list['list'] as $key => $value) {
  57. if ($value['num'] < 10) {
  58. $value['num'] = '0' . $value['num'];
  59. }
  60. $list['list'][$key]['num'] = $value['date_time'].$value['num'].'期';
  61. }
  62. }
  63. $config = Yii::$app->services->setting->addons->get($this->mall_id, ScoreBonusEnums::ADDONS_NAME, 'basic',true);
  64. //获取结算周期枚举
  65. $list['bonus_circle'] = ScoreBonusEnums::getSettleCircleMap();
  66. $capital_pool = ScoreBonusCapitalPool::getOne([
  67. ['mall_id' => $this->mall_id],
  68. ], true);
  69. //本期分红池总额
  70. $statistics_data['bonus_price'] = $capital_pool['bonus_price'];//本期分红池总额
  71. //当前总积分额
  72. /*因为现在添加了【权重占比分配】选项。 所以这里的积分统计不能只按照【积分占比分配】来统计。*/
  73. if (!empty($config['settle_method'])) {
  74. $statistics_data['total_score'] = ScoreBonusUserWeight::find()
  75. ->alias('w')
  76. ->joinWith(['userWallet uw' => function($query) {
  77. $query->andWhere(['>', 'uw.score', 0]);
  78. }])
  79. ->where(['w.status' => 1, 'w.mall_id' => $this->mall_id])
  80. ->andWhere(['>', 'weight', 0])
  81. ->sum('uw.score');
  82. } else {
  83. $statistics_data['total_score'] = UserWallet::find()->where(['and', ['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED], ['>=', 'score', $config['user_score']]])->sum('score');
  84. }
  85. //已结算分红总额
  86. $statistics_data['total_bonus_price'] = $capital_pool['total_price'];
  87. $list['statistics_data'] = $statistics_data;
  88. $list['export_list'] = ScoreBonusModel::fieldsList();
  89. return $this->success('操作成功',$list);
  90. }
  91. if (\Yii::$app->request->post('flag') == 'EXPORT') {
  92. $post = \Yii::$app->request->post();
  93. $post['mall_id'] = $this->mall_id;
  94. $filename = '结算明细-' . date('YmdHis') . '_' . rand(10000, 99999);
  95. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_SCORE_BONUS, ScoreBonusModel::class, 'exportHandle', $post);
  96. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  97. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  98. }
  99. }
  100. return $this->render($this->action->id);
  101. }
  102. //获取用户结算明细
  103. public function actionUserSettleData()
  104. {
  105. if (Yii::$app->request->isAjax) {
  106. if (Yii::$app->request->isGet) {
  107. $get = Yii::$app->request->get();
  108. $res = Yii::$app->services->validate->validate($get, [
  109. [['orderBy'], 'string'],
  110. [['user_id', 'bid'], 'number'],
  111. ]);
  112. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  113. $where = [
  114. ['mall_id' => $this->mall_id],
  115. ['bid' => $get['bid']]
  116. ];
  117. if (!empty($get['user_id'])) $where[] = ['user_id' => $get['user_id']];
  118. $orderBy = 'id desc';
  119. if (!empty($get['orderBy'])) $orderBy = $get['orderBy'];
  120. $list_params = array('where' => $where, 'order' => $orderBy, 'with'=>['user']);
  121. $list = ScoreBonusRewardLogModel::listPage($list_params, false, true);
  122. //获取本期数据
  123. $score_bonus_data = ScoreBonusModel::findOne(['mall_id' => $this->mall_id, 'id' => $get['bid']]);
  124. if ($score_bonus_data->num < 10) $score_bonus_data->num = '0' . $score_bonus_data->num;
  125. $circly_num = $score_bonus_data->date_time . $score_bonus_data->num .'期明细';
  126. $list['titleText'] = $circly_num;
  127. $list['bonus_circle'] = ScoreBonusEnums::getSettleCircleMap();
  128. $list['export_list'] = ScoreBonusRewardLogModel::fieldsList();
  129. return $this->success('操作成功',$list);
  130. }
  131. if (\Yii::$app->request->post('flag') == 'EXPORT') {
  132. $post = \Yii::$app->request->post();
  133. $post['mall_id'] = $this->mall_id;
  134. $score_bonus_data = ScoreBonusModel::findOne(['mall_id' => $this->mall_id, 'id' => $post['bid']]);
  135. if ($score_bonus_data->num < 10) $score_bonus_data->num = '0' . $score_bonus_data->num;
  136. $circly_num = $score_bonus_data->date_time . $score_bonus_data->num .'期用户结算明细';
  137. $filename = $circly_num.'-' . date('YmdHis') . '_' . rand(10000, 99999);
  138. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id, $filename, DownloadEnum::TYPE_SCORE_BONUS, ScoreBonusRewardLogModel::class, 'exportHandle', $post);
  139. if ($res === false) return $this->error('加入导出任务失败' . CsvExportHelper::getStaticError());
  140. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  141. }
  142. }
  143. return $this->render($this->action->id);
  144. }
  145. public function actionTestExport()
  146. {
  147. $params = [
  148. 'flag' => 'EXPORT',
  149. 'fields' => [
  150. 'user_id',
  151. 'nickname',
  152. 'commission',
  153. 'score',
  154. 'total_score',
  155. 'status',
  156. 'created_at',
  157. 'bonus_circle',
  158. ],
  159. 'mall_id' => 5,
  160. 'bid' => 20
  161. ];
  162. $data = [
  163. 'params' => json_encode($params),
  164. 'filename' => '2024062602期用户结算明细-20240627102225_85862',
  165. 'type' => 66,
  166. 'handler_class' => ScoreBonusRewardLogModel::class,
  167. 'method' => 'exportHandle'
  168. ];
  169. $data = json_decode(json_encode($data));
  170. $res = ScoreBonusRewardLogModel::exportHandle($data);
  171. print_r($res);exit;
  172. }
  173. public function actionUserList()
  174. {
  175. if (Yii::$app->request->isAjax) {
  176. if (Yii::$app->request->isGet) {
  177. $params = \Yii::$app->request->get();
  178. $where = [];
  179. $where[] = ['=', 'u.mall_id', $this->mall_id];
  180. $where[] = ['in', 'u.status', [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  181. // 根据ID
  182. if (!empty($params['user_id'])) $where[] = ['=', 'u.id', $params['user_id']];
  183. $select = 'u.id,u.id as user_id,nickname,mobile';
  184. $order = 'user_id desc';
  185. $limit = $params['limit'] ?? 10;
  186. $params = array(
  187. 'alias' => 'u',
  188. 'select' => $select,
  189. 'where' => $where,
  190. 'order' => $order,
  191. 'limit' => $limit
  192. );
  193. $list = User::listPage($params, false, true);
  194. if ($list['list']) {
  195. $user_ids = array_column($list['list'], 'id');
  196. $user_wallet_info = UserWallet::find()
  197. ->select('user_id,score')
  198. ->where([
  199. 'user_id' => $user_ids,
  200. 'status' => StatusEnum::ENABLED
  201. ])
  202. ->indexBy('user_id')
  203. ->asArray()
  204. ->all();
  205. $user_weight_info = ScoreBonusUserWeight::find()
  206. ->select('user_id,weight,total_income')
  207. ->where([
  208. 'user_id' => $user_ids,
  209. 'status' => StatusEnum::ENABLED
  210. ])
  211. ->indexBy('user_id')
  212. ->asArray()
  213. ->all();
  214. foreach ($list['list'] as &$item) {
  215. $item['score'] = $user_wallet_info[$item['id']]['score'] ?? '0';
  216. $item['weight'] = $user_weight_info[$item['id']]['weight'] ?? '0';
  217. $item['total_income'] = $user_weight_info[$item['id']]['total_income'] ?? '0';
  218. }
  219. }
  220. return $this->success('操作成功', $list);
  221. }
  222. }
  223. return $this->render($this->action->id);
  224. }
  225. public function actionWeightList()
  226. {
  227. if (Yii::$app->request->isAjax) {
  228. if (Yii::$app->request->isGet) {
  229. $get = \Yii::$app->request->get();
  230. $where[] = ['=', 'mall_id', $this->mall_id];
  231. $where[] = ['=', 'status', StatusEnum::ENABLED];
  232. if (!empty($get['user_id'])) {
  233. $where[] = ['=', 'user_id', $get['user_id']];
  234. }
  235. $params['order'] = 'id desc';
  236. $params['where'] = $where;
  237. $params['with'] = ['user'];
  238. $params['limit'] = 10;
  239. $list = ScoreBonusUserWeightLog::listPage($params, false);
  240. return $this->success('操作成功', $list);
  241. }
  242. }
  243. return $this->render($this->action->id);
  244. }
  245. }