Files
xk-api-yii/admin/models/forms/statistics/ServiceDataForm.php

171 lines
5.6 KiB
PHP

<?php
namespace admin\models\forms\statistics;
use admin\exceptions\ApiException;
use admin\models\forms\export\CommonExport;
use common\models\Goods;
use common\models\GoodsWarehouse;
use common\models\oldDb\ImMessageSession;
use common\models\OrderDetail;
use yii\base\Model;
use yii\data\ActiveDataProvider;
class ServiceDataForm extends Model
{
public $date_start;
public $date_end;
public $page;
public $limit;
public $is_export;
public function rules()
{
return [
[['page', 'limit'], 'integer'],
[['page',], 'default', 'value' => 1],
[['limit'], 'default', 'value' => 20],
[['date_start', 'date_end'], 'trim'],
['is_export','default','value'=> 0]
];
}
public function init()
{
parent::init(); // TODO: Change the autogenerated stub
}
public function head_search()
{
if (!$this->validate()) {
throw new ApiException(implode(",",$this->getFirstErrors()));
}
if(!$this->date_start){
$this->date_start = date('Y-m').'-01';
}
if(!$this->date_end){
$this->date_end = date('Y-m-t');
}
$query = ImMessageSession::find()->alias('i')->where([
'i.mall_id' => \Yii::$app->mallId,
'i.type' => 0,
])->andWhere(['<>','i.imuser_id',0])->joinWith(['imUser u'=>function($q){
$q->andWhere(['not', ['u.id' => null]]);
}]);
if ($this->date_start) {
$query->andWhere(['>=', 'i.created_at', strtotime($this->date_start . ' 00:00:00')]);
}
if ($this->date_end) {
$query->andWhere(['<=', 'i.created_at', strtotime($this->date_end . ' 23:59:59')]);
}
$query->select("count(i.user_id) as `times`,count(distinct(i.user_id)) as `peoples`");
$data = $query->all();
$query2 = ImMessageSession::find()->alias('i')->where([
'i.mall_id' => \Yii::$app->mallId,
'i.type' => 0,
])->andWhere(['<>','i.imuser_id',0])->joinWith(['imUser u'=>function($q){
$q->andWhere(['not', ['u.id' => null]]);
}]);
if ($this->date_start) {
$query2->andWhere(['>=', 'i.created_at', strtotime($this->date_start . ' 00:00:00')]);
}
if ($this->date_end) {
$query2->andWhere(['<=', 'i.created_at', strtotime($this->date_end . ' 23:59:59')]);
}
$query2->select("FROM_UNIXTIME(i.`created_at`, '%Y-%m-%d') AS `time`,count(i.user_id) as `times`,count(distinct(i.user_id)) as `peoples`");
$list= $query2->groupBy('time')->orderBy('time')->asArray()->all();
$day = floor((strtotime($this->date_end) - strtotime($this->date_start)) / 86400) + 1;
for ($i = 0; $i < $day; $i++) {
$date = date('Y-m-d', strtotime("-$i day",strtotime($this->date_end)));
$bool = false;
foreach ($list as $item) {
if ($date == $item['time']) {
$bool = true;
$arr[$i]['created_at'] = $item['time'];
$arr[$i]['times'] = $item['times'];
$arr[$i]['peoples'] = $item['peoples'];
}
}
if (!$bool) {
$arr[$i]['created_at'] = $date;
$arr[$i]['times'] = 0;
$arr[$i]['peoples'] = 0;
}
}
$list = !empty($arr) ? array_reverse($arr) : [];
return [
'data' => $data[0],
'table' => $list
];
}
public function bottom_query()
{
if(!$this->date_start){
$this->date_start = date('Y-m').'-01';
}
if(!$this->date_end){
$this->date_end = date('Y-m-t');
}
$query = ImMessageSession::find()->alias('i')->where([
'i.mall_id' => \Yii::$app->mallId,
'i.type' => 0,
])->andWhere(['<>','i.imuser_id',0]);
if ($this->date_start) {
$query->andWhere(['>=', 'i.created_at', strtotime($this->date_start . ' 00:00:00')]);
}
if ($this->date_end) {
$query->andWhere(['<=', 'i.created_at', strtotime($this->date_end . ' 23:59:59')]);
}
$query->joinWith(['imUser u'=>function($q){
$q->andWhere(['not', ['u.id' => null]]);
}])->select(['i.imuser_id,COUNT(i.user_id) AS `times`,COUNT(distinct(i.user_id)) AS `peoples`'])->groupBy('i.imuser_id')->orderBy('i.imuser_id desc');
return $query;
}
public function bottom_search()
{
if (!$this->validate()) {
throw new ApiException(implode(",",$this->getFirstErrors()));
}
$query = $this->bottom_query();
if ($this->is_export) {
$fields = [
'imuser_name','times','peoples'
];
$queueId = CommonExport::handle([
'export_class' => 'backend\\models\\forms\\export\\ServiceExport',
'params' => [
'fieldsKeyList' => $fields,
],
'model_class' => 'backend\\models\\forms\\statistics\\ServiceDataForm',
'model_params' => $this->attributes,
'function_name' => 'bottom_query'
]);
return [
'queue_id' => $queueId
];
}
$data = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'defaultPageSize' => $this->limit,
'params' => [
'page' => $this->page
]
]
]);
return $data;
}
}