CreditsLogController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace addons\Member\merchant\controllers;
  3. use Yii;
  4. use common\models\base\SearchModel;
  5. use common\models\member\CreditsLog;
  6. use common\enums\StatusEnum;
  7. use common\enums\MemberTypeEnum;
  8. use common\enums\CreditsLogTypeEnum;
  9. use common\helpers\ExcelHelper;
  10. /**
  11. * Class CreditsLogController
  12. * @package addons\Member\merchant\controllers
  13. * @author jianyan74 <751393839@qq.com>
  14. */
  15. class CreditsLogController extends BaseController
  16. {
  17. /**
  18. * 消费日志
  19. *
  20. * @return string
  21. * @throws \yii\web\NotFoundHttpException
  22. */
  23. public function actionConsume()
  24. {
  25. list($dataProvider, $searchModel, $startTime, $endTime) = $this->getData(CreditsLogTypeEnum::CONSUME_MONEY);
  26. return $this->render('credit', [
  27. 'dataProvider' => $dataProvider,
  28. 'searchModel' => $searchModel,
  29. 'startTime' => $startTime,
  30. 'endTime' => $endTime,
  31. 'action' => $this->action->id,
  32. 'type' => CreditsLogTypeEnum::CONSUME_MONEY,
  33. 'title' => '消费日志'
  34. ]);
  35. }
  36. /**
  37. * 余额日志
  38. *
  39. * @return string
  40. * @throws \yii\web\NotFoundHttpException
  41. */
  42. public function actionMoney()
  43. {
  44. list($dataProvider, $searchModel, $startTime, $endTime) = $this->getData(CreditsLogTypeEnum::USER_MONEY);
  45. return $this->render('credit', [
  46. 'dataProvider' => $dataProvider,
  47. 'searchModel' => $searchModel,
  48. 'startTime' => $startTime,
  49. 'endTime' => $endTime,
  50. 'action' => $this->action->id,
  51. 'type' => CreditsLogTypeEnum::USER_MONEY,
  52. 'title' => '余额日志'
  53. ]);
  54. }
  55. /**
  56. * 积分日志
  57. *
  58. * @return string
  59. * @throws \yii\web\NotFoundHttpException
  60. */
  61. public function actionIntegral()
  62. {
  63. list($dataProvider, $searchModel, $startTime, $endTime) = $this->getData(CreditsLogTypeEnum::USER_INTEGRAL);
  64. return $this->render('credit', [
  65. 'dataProvider' => $dataProvider,
  66. 'searchModel' => $searchModel,
  67. 'startTime' => $startTime,
  68. 'endTime' => $endTime,
  69. 'action' => $this->action->id,
  70. 'type' => CreditsLogTypeEnum::USER_INTEGRAL,
  71. 'title' => '积分日志'
  72. ]);
  73. }
  74. /**
  75. * 积分日志
  76. *
  77. * @return string
  78. * @throws \yii\web\NotFoundHttpException
  79. */
  80. public function actionGrowth()
  81. {
  82. list($dataProvider, $searchModel, $startTime, $endTime) = $this->getData(CreditsLogTypeEnum::USER_GROWTH);
  83. return $this->render('credit', [
  84. 'dataProvider' => $dataProvider,
  85. 'searchModel' => $searchModel,
  86. 'startTime' => $startTime,
  87. 'endTime' => $endTime,
  88. 'action' => $this->action->id,
  89. 'type' => CreditsLogTypeEnum::USER_GROWTH,
  90. 'title' => '成长值日志'
  91. ]);
  92. }
  93. /**
  94. * @param $type
  95. * @return array
  96. * @throws \yii\web\NotFoundHttpException
  97. */
  98. protected function getData($type)
  99. {
  100. $startTime = Yii::$app->request->get('start_time');
  101. $endTime = Yii::$app->request->get('end_time');
  102. $searchModel = new SearchModel([
  103. 'model' => CreditsLog::class,
  104. 'scenario' => 'default',
  105. 'partialMatchAttributes' => [], // 模糊查询
  106. 'defaultOrder' => [
  107. 'id' => SORT_DESC
  108. ],
  109. 'pageSize' => $this->pageSize
  110. ]);
  111. $dataProvider = $searchModel
  112. ->search(Yii::$app->request->queryParams);
  113. $dataProvider->query
  114. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  115. ->andWhere(['type' => $type, 'member_type' => MemberTypeEnum::MEMBER])
  116. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  117. ->andFilterWhere(['between', 'created_at', !empty($startTime) ? strtotime($startTime) : '', !empty($endTime) ? strtotime($endTime) : ''])
  118. ->with(['member']);
  119. return [$dataProvider, $searchModel, $startTime, $endTime];
  120. }
  121. /**
  122. * @return bool
  123. * @throws \PhpOffice\PhpSpreadsheet\Exception
  124. * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  125. */
  126. public function actionExport()
  127. {
  128. $data = Yii::$app->request->get('SearchModel');
  129. $startTime = Yii::$app->request->get('start_time');
  130. $endTime = Yii::$app->request->get('end_time');
  131. $type = Yii::$app->request->get('type');
  132. $list = CreditsLog::find()
  133. ->where(['type' => $type, 'status' => StatusEnum::ENABLED])
  134. ->andFilterWhere(['merchant_id' => Yii::$app->services->merchant->getId()])
  135. ->andFilterWhere(['member_id' => $data['member_id']])
  136. ->andFilterWhere(['num' => $data['num']])
  137. ->andFilterWhere(['new_num' => $data['new_num']])
  138. ->andFilterWhere(['like', 'remark', $data['remark']])
  139. ->andFilterWhere(['between', 'created_at', !empty($startTime) ? strtotime($startTime) : '', !empty($endTime) ? strtotime($endTime) : ''])
  140. ->with(['baseMember'])
  141. ->orderBy('id desc')
  142. ->asArray()
  143. ->all();
  144. $header = [
  145. ['ID', 'id'],
  146. ['会员ID', 'baseMember.id'],
  147. ['会员', 'baseMember.nickname'],
  148. ['变动数量', 'num'],
  149. ['变动后数量', 'new_num'],
  150. ['备注', 'remark'],
  151. ['创建时间', 'created_at', 'date', 'Y-m-d H:i:s'],
  152. ];
  153. return ExcelHelper::exportData($list, $header, CreditsLogTypeEnum::getValue($type) . '_' . date('YmdHis'));
  154. }
  155. }
粤ICP备19079148号