AchievementController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace addons\WriteOff\backend\controllers;
  3. use Yii;
  4. use common\controllers\AddonsController;
  5. use addons\WriteOff\common\models\WriteOffAwardLog;
  6. use addons\WriteOff\common\models\WriteOffCommissionJob;
  7. use addons\WriteOff\common\models\WriteOffPromoteOrderLog;
  8. use common\models\user\User;
  9. use common\enums\StatusEnum;
  10. use common\models\order\OrderDetail;
  11. use addons\WriteOff\common\enums\WriteOffEnum;
  12. use common\helpers\csv\CsvExportHelper;
  13. use common\enums\DownloadEnum;
  14. class AchievementController extends BaseController
  15. {
  16. public $enableCsrfValidation = false;
  17. /**
  18. * 首页
  19. *
  20. * @return string
  21. */
  22. public function actionIndex()
  23. {
  24. if (\Yii::$app->request->isAjax) {
  25. if (\Yii::$app->request->isGet) {
  26. $get = \Yii::$app->request->get();
  27. $where[] = ['=', 'mall_id', $this->mall_id];
  28. $where[] = ['=', 'status', StatusEnum::ENABLED];
  29. if (!empty($get['date_start']) && !empty($get['date_end'])) {
  30. $where[] = ['between', 'created_at', strtotime($get['date_start']), strtotime($get['date_end'])];
  31. }
  32. $maxMoney = $get['max_money'] > $get['min_money'] ? $get['max_money'] : $get['min_money'];
  33. $minMoney = $get['max_money'] > $get['min_money'] ? $get['min_money'] : $get['max_money'];
  34. if (!empty($get['max_money']) && !empty($get['min_money'])) {
  35. $where[] = ['between', 'award_money', $minMoney, $maxMoney];
  36. } elseif (!empty($this->max_money)) {
  37. $where[] = ['<=', 'award_money', $get['max_money']];
  38. } elseif (!empty($this->min_money)) {
  39. $where[] = ['>=', 'award_money', $get['min_money']];
  40. }
  41. $params['order'] = 'id desc';
  42. $params['where'] = $where;
  43. $params['limit'] = 10;
  44. $list = WriteOffAwardLog::listPage($params, false);
  45. $sum = WriteOffAwardLog::find()->where(['mall_id'=>$this->mall_id])->select('sum(award_money) as money')->asArray()->one();
  46. $count = WriteOffCommissionJob::find()->where(['mall_id'=>$this->mall_id])->count();
  47. $returnData['sum'] = [
  48. 'award_money' => $sum['money'] ?? 0,
  49. 'grant_num' => $count,
  50. ];
  51. if(!empty($list['list'])){
  52. $user_id_arr = array_column($list['list'],'user_id');
  53. $user_arr = User::lists([
  54. 'where'=>[
  55. ['id'=>$user_id_arr]
  56. ],
  57. 'index'=>'id',
  58. ]);
  59. foreach($list['list'] as &$item){
  60. if(isset($user_arr[$item['user_id']])){
  61. $item['user'] = $user_arr[$item['user_id']];
  62. }
  63. }
  64. }
  65. $returnData["list"] = $list;
  66. return $this->success('success', $returnData);
  67. }
  68. }
  69. return $this->render('index', []);
  70. }
  71. /**
  72. * 详情
  73. *
  74. * @return string
  75. */
  76. public function actionDetail()
  77. {
  78. if (\Yii::$app->request->isAjax) {
  79. if (\Yii::$app->request->isPost) {
  80. $data = \Yii::$app->request->post();
  81. $filename = '业绩详情'.date('Y-m-d',$data['created_at']).'-' . date('YmdHis');
  82. $data['mall_id'] = $this->mall_id;
  83. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id,$filename,DownloadEnum::TYPE_ASSETS,WriteOffPromoteOrderLog::class,'exportAssets',$data);
  84. if ($res === false) return $this->error('加入导出任务失败'.CsvExportHelper::getStaticError());
  85. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  86. }
  87. if (\Yii::$app->request->isGet) {
  88. $get = \Yii::$app->request->get();
  89. $where[] = ['=', 'mall_id', $this->mall_id];
  90. $where[] = ['=','user_id',$get['user_id']];
  91. $where[] = ['=', 'status', StatusEnum::ENABLED];
  92. // 获取配置
  93. $config = Yii::$app->services->setting->addons->get($this->mall_id, WriteOffEnum::ADDONS_NAME, 'basic');
  94. $compute_period = $config['compute_period'];
  95. $start_day = 0;
  96. $end_day = 0;
  97. switch ($config['compute_period']) {
  98. case 0:
  99. if (date('d') != 1) $continue = true;// 非当月1号直接退出
  100. $start_day = date("Ym01", strtotime("-1 month",$get['created_at']));
  101. $end_day = date('Ymd', strtotime(date('Y-m', $get['created_at']) . '-01 00:00:00') - 86400);
  102. break;
  103. }
  104. $start_day = strtotime($start_day);
  105. $end_day = strtotime(date('Y-m-d 23:59:59',strtotime($end_day)));
  106. $where[] = ['between', 'created_at', $start_day, $end_day];
  107. if (!empty($get['date_start']) && !empty($get['date_end'])) {
  108. $where[] = ['between', 'created_at', strtotime($get['date_start']), strtotime($get['date_end'])];
  109. }
  110. if(!empty($get['order_no'])){
  111. $where[] = ['=','order_no',$get['order_no']];
  112. }
  113. // 根据搜索手机号查询用户id
  114. if (!empty($get['keyword'])) {
  115. $user_ids = User::find()->select("id")
  116. ->where(['mall_id' => $this->mall_id])
  117. ->andWhere([
  118. 'or',
  119. ['=', 'id', $get['keyword']],
  120. ['like', 'mobile', $get['keyword'] . '%', false],
  121. ['like', 'nickname', $get['keyword'] . '%', false]
  122. ])
  123. ->asArray()
  124. ->column();
  125. $where[] = ['in', 'buy_user_id', $user_ids];
  126. }
  127. $params['order'] = 'id desc';
  128. $params['where'] = $where;
  129. $params['limit'] = 10;
  130. $list = WriteOffPromoteOrderLog::listPage($params, false);
  131. if(!empty($list['list'])){
  132. $user_id_arr = array_column($list['list'],'buy_user_id');
  133. $user_arr = User::lists([
  134. 'where'=>[
  135. ['id'=>$user_id_arr]
  136. ],
  137. 'index'=>'id',
  138. ]);
  139. $order_detail_ids = array_column($list['list'],'order_detail_id');
  140. $order_detail_list = OrderDetail::lists([
  141. 'where'=>[
  142. ['id'=>$order_detail_ids]
  143. ],
  144. 'index'=>'id',
  145. ]);
  146. foreach($list['list'] as &$item){
  147. if(isset($user_arr[$item['buy_user_id']])){
  148. $item['user'] = $user_arr[$item['buy_user_id']];
  149. }
  150. if(!empty($order_detail_list[$item['order_detail_id']])){
  151. $item['goods_name'] = $order_detail_list[$item['order_detail_id']]['goods_name'];
  152. }
  153. }
  154. }
  155. $list['export_list'] = WriteOffPromoteOrderLog::exportFieldsList();
  156. return $this->success('success', $list);
  157. }
  158. }
  159. return $this->render('detail', []);
  160. }
  161. }