ScoreLogController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace addons\ScoreExpansion\backend\controllers;
  3. use addons\Redpack\common\enums\RedpackEnum;
  4. use addons\ScoreExpansion\backend\logic\ScoreImportLogic;
  5. use addons\ScoreExpansion\common\enums\ScoreExpansionEnum;
  6. use addons\ScoreExpansion\common\models\ScoreExpansionWallet;
  7. use addons\ScoreExpansion\common\models\ScoreExpansionWalletLog;
  8. use common\components\CsvExport;
  9. use common\enums\AddonsEnum;
  10. use common\enums\StatusEnum;
  11. use common\models\common\ImportFailLog;
  12. use common\models\common\ImportLog;
  13. use common\models\user\User;
  14. use Yii;
  15. class ScoreLogController extends BaseController
  16. {
  17. public $enableCsrfValidation = false;
  18. /**
  19. * 首页
  20. *
  21. * @return string
  22. */
  23. public function actionIndex()
  24. {
  25. try {
  26. $request = Yii::$app->request;
  27. if ($request->isAjax) {
  28. // 提交等级内容
  29. if ($request->isPost) {
  30. $params = Yii::$app->request->post();
  31. $res = Yii::$app->services->validate->validate($params, [
  32. [['page','user_id','user_name','mobile','date_start','date_end','from_type'], 'safe'],
  33. ]);
  34. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  35. $user_ids = [];
  36. $user_name_ids = [];
  37. $is_search = false;
  38. if(!empty($params['user_id'])) $where[] = ['=','user_id',$params['user_id']];
  39. if(!empty($params['mobile'])) {
  40. $user_ids[] = User::find()->select("id")->where(['mall_id' => $this->mall_id,'mobile' => $params['mobile']])->scalar();
  41. $is_search = true;
  42. }
  43. if(!empty($params['user_name'])) {
  44. $user_where[] = ['like','nickname',$params['user_name'] . '%', false];
  45. $select = 'id,nickname';
  46. $user_list = User::lists(['where' =>$user_where,'select' => $select],true);
  47. $user_name_ids = array_column($user_list,'id');
  48. $is_search = true;
  49. }
  50. if($is_search){
  51. $all_user_ids = array_merge($user_ids,$user_name_ids);
  52. if(!$all_user_ids){
  53. $all_user_ids = [-1];
  54. }
  55. $where[] = ['in','user_id',$all_user_ids];
  56. }
  57. if(!empty($params['from_type'])) $where[] = ['=','from_type',$params['from_type']];
  58. if($params['date_start'] && $params['date_end']) $where[] = ['between', 'created_at', strtotime($params['date_start']),strtotime($params['date_end'])];
  59. $where[] = ['=','mall_id',$this->mall_id];
  60. $where[] = ['>=','status',StatusEnum::DISABLED];
  61. $order = 'id desc';
  62. // $list = ScoreExpansionLog::listPage(['where' => $where,'order' => $order,'with' => ['user']],false);
  63. $list = ScoreExpansionWalletLog::listPage(['where' => $where,'order' => $order,'with' => ['user']],false);
  64. // echo '<pre>';print_r($where);die;
  65. $list['from_type'] = ScoreExpansionEnum::getFromType('', $this->mall_id);
  66. $list['redpack_type'] = ScoreExpansionEnum::getRedpackText('', $this->mall_id);
  67. $list['score_name'] = Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic.score_name') ?? '积分';
  68. return $this->success('操作成功',$list);
  69. }
  70. }
  71. } catch (\Exception $e) {
  72. return $this->error($e->getMessage(), []);
  73. }
  74. return $this->render($this->action->id);
  75. }
  76. /**
  77. * 积分统计
  78. * @Author: wu
  79. * @DateTime: 2023/2/21 0015 15:00
  80. * @Copyright: copyright (c) 2023 广东七件事集团
  81. * @return string|\yii\web\Response
  82. */
  83. public function actionTotal()
  84. {
  85. $integral_wallet_type = 'frozen_integral';
  86. $frozen_integral_add = ScoreExpansionWalletLog::find()->where(['mall_id' => $this->mall_id, 'integral_wallet_type' => $integral_wallet_type, 'change_type' => 'add'])->sum('change_num');
  87. $frozen_integral_sub = ScoreExpansionWalletLog::find()->where(['mall_id' => $this->mall_id, 'integral_wallet_type' => $integral_wallet_type, 'change_type' => 'sub'])->sum('change_num');
  88. $total_frozen_integral = ($frozen_integral_add ?? 0) - ($frozen_integral_sub ?? 0);
  89. $integral_wallet_type = 'integral';
  90. $integral_add = ScoreExpansionWalletLog::find()->where(['mall_id' => $this->mall_id, 'integral_wallet_type' => $integral_wallet_type, 'change_type' => 'add'])->sum('change_num');
  91. $integral_sub = ScoreExpansionWalletLog::find()->where(['mall_id' => $this->mall_id, 'integral_wallet_type' => $integral_wallet_type, 'change_type' => 'sub'])->sum('change_num');
  92. $total_integral = ($integral_add ?? 0) - ($integral_sub ?? 0);
  93. return $this->success('操作成功', ['total' => ['total_frozen_integral' => $total_frozen_integral, 'total_integral' => $total_integral]]);
  94. }
  95. public function actionGetImportLog()
  96. {
  97. $params = Yii::$app->request->get();
  98. $params['where'] = [
  99. ['mall_id' => $this->mall_id],
  100. ['source' => ScoreExpansionWallet::class]
  101. ];
  102. $params['order'] = 'id desc';
  103. $data = ImportLog::listPage($params, false, true);
  104. if (!empty($data['list'])) {
  105. foreach ($data['list'] as &$v) {
  106. $v['url'] = '';
  107. if ($v['fail_counts'])
  108. $v['url'] = Yii::$app->urlManager->createUrl(['score-expansion/score-log/export-fail', 'import_log_id' => $v['id']]);
  109. }
  110. }
  111. return $this->success('操作成功', $data);
  112. }
  113. /**
  114. * @return string|void
  115. */
  116. public function actionImport()
  117. {
  118. if (!$this->request->isAjax) {
  119. return $this->render($this->action->id);
  120. }
  121. if (!$this->request->isPost) {
  122. return;
  123. }
  124. set_time_limit(0);
  125. $params = Yii::$app->request->post();
  126. $res = Yii::$app->services->validate->validate($params, [
  127. [['current_num'], 'required'],
  128. [['file'], 'file', 'extensions' => ['csv']],
  129. [['file_path'], 'string'],
  130. [['sync'], 'safe'],
  131. ]);
  132. if ($res === false) {
  133. return $this->error(Yii::$app->services->validate->getErrorMessage());
  134. }
  135. $params['mall_id'] = $this->mall_id;
  136. $res = ScoreImportLogic::importSync($params);
  137. if ($res === false) {
  138. return $this->error(ScoreImportLogic::getStaticError());
  139. }
  140. if (empty($res['fail_counts'])) {
  141. return $this->success('操作成功', $res);
  142. }
  143. if (!empty($res['fail_counts'])) {
  144. return $this->error('导入失败' . $res['fail_counts'] . '条');
  145. }
  146. }
  147. /**
  148. * 导出失败日志
  149. */
  150. public function actionExportFail()
  151. {
  152. $params = Yii::$app->request->get();
  153. $params['head_list'] = ScoreExpansionEnum::HEAD_LIST;
  154. ImportFailLog::exportFailLog($params);
  155. }
  156. /**
  157. * 导入的模板
  158. */
  159. public function actionImportTemplate()
  160. {
  161. $csv = new CsvExport();
  162. $file_name = '积分批量导入模板-' . date('YmdHis', time());
  163. return $csv->export([], array_column(ScoreExpansionEnum::HEAD_LIST, 'field_name'), $file_name);
  164. }
  165. }