187 lines
6.2 KiB
PHP
187 lines
6.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @author Any
|
|
* @description KISS
|
|
* @date 2021年10月4日
|
|
* @version 1.0.0
|
|
*
|
|
* _____LOG_____
|
|
*
|
|
*/
|
|
namespace app\models\common\integral;
|
|
|
|
use app\models\integral\Integral;
|
|
use app\models\integral\IntegralLog;
|
|
use app\models\User;
|
|
use app\models\Model;
|
|
use app\models\UniqueOrderNo;
|
|
use yii\data\Pagination;
|
|
use app\components\SiteHelper;
|
|
|
|
|
|
class CommonIntegralLogListForm extends Model
|
|
{
|
|
public $keywords;
|
|
public $limit;
|
|
public $page;
|
|
|
|
public $user_id;
|
|
public $cx_mch_id;
|
|
public $scene;
|
|
public $type;
|
|
public $order_type;
|
|
|
|
public $start_time;
|
|
public $end_time;
|
|
|
|
public $integral_id;
|
|
|
|
public $year;
|
|
public $month;
|
|
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
[['keywords', 'year', 'month'], 'trim'],
|
|
[['keywords', 'year', 'month'], 'string'],
|
|
[['limit', 'page', 'user_id', 'cx_mch_id', 'scene', 'type', 'order_type', 'integral_id'], 'integer'],
|
|
[['page'], 'default', 'value' => 1],
|
|
[['limit'], 'default', 'value' => 20],
|
|
[['start_time', 'end_time',], 'safe'],
|
|
[['cx_mch_id'], 'required'],
|
|
[['user_id',], 'required', 'on' => 'user'],
|
|
[['user_id', 'integral_id'], 'required', 'on' => 'detail'],
|
|
];
|
|
}
|
|
|
|
public function search()
|
|
{
|
|
if(!$this->validate()){
|
|
return $this->getModelError();
|
|
}
|
|
$query = IntegralLog::find()->alias('bl')
|
|
->leftJoin(['u' => User::tableName()], 'u.id=bl.user_id')
|
|
->where([
|
|
'bl.cx_mch_id' => $this->cx_mch_id,
|
|
'bl.scene' => $this->scene,
|
|
])
|
|
->andWhere([
|
|
'OR',
|
|
['LIKE', 'bl.desc', $this->keywords],
|
|
['LIKE', 'bl.order_no', $this->keywords],
|
|
['LIKE', 'u.username', $this->keywords],
|
|
['LIKE', 'u.nickname', $this->keywords],
|
|
['LIKE', 'u.real_name', $this->keywords],
|
|
])
|
|
->andFilterWhere([
|
|
'bl.user_id' => $this->user_id,
|
|
'bl.type' => $this->type,
|
|
'bl.order_type' => $this->order_type,
|
|
'bl.id' => $this->integral_id,
|
|
]);
|
|
$query = $this->switchTime($query);
|
|
$query = $query->select('u.nickname,u.avatar_url,u.real_name,bl.id,bl.cx_mch_id,bl.user_id,bl.type,bl.scene,bl.integral,bl.before_account_integral,bl.after_account_integral,bl.desc,bl.order_type,bl.order_no,bl.ext,bl.created_at');
|
|
$count_query = clone $query;
|
|
$count = $count_query->count();
|
|
$pagination = new Pagination(['pageSize' => $this->limit, 'totalCount' => $count, 'page' => $this->page - 1]);
|
|
$list = $query->offset($pagination->offset)->limit($pagination->limit)->orderBy(['bl.created_at' => SORT_DESC])->asArray()->all();
|
|
$bop_list = [];
|
|
foreach ($list as $index => $item){
|
|
$item['created_at_cn'] = date('Y-m-d H:i:s',$item['created_at']);
|
|
$item['created_date'] = date('Ym',$item['created_at']);
|
|
$item['avatar_url'] = SiteHelper::getFullUrl($item['avatar_url']);
|
|
$item['type_cn'] = $item['type'] == Integral::TYPE_INCOME ? '收入': '支出';
|
|
$item['order_type_cn'] = UniqueOrderNo::getOrderType($item['order_type']);
|
|
$list[$index] = $item;
|
|
if($this->scenario == 'user' && !isset($bop_list[$item['created_date']])){
|
|
$bop_list[$item['created_date']] = $this->getMonthBop($item['created_date']);
|
|
}
|
|
}
|
|
//是否已经全部加载
|
|
$end_flag = $this->page > $pagination->pageCount ? true : false;
|
|
return [
|
|
'code' => 0,
|
|
'msg' => 'ok',
|
|
'data' => $list,
|
|
'count' => $count,
|
|
'page_size' => $this->limit,
|
|
'page_count' => $pagination->pageCount,
|
|
'page_no' => $this->page,
|
|
'end_flag' => $end_flag,
|
|
'bop_list' => $bop_list
|
|
];
|
|
}
|
|
|
|
public function switchTime($query)
|
|
{
|
|
if(!empty($this->start_time)){
|
|
$query = $query->andWhere([
|
|
'>',
|
|
'bl.created_at',
|
|
strtotime($this->start_time)
|
|
]);
|
|
}
|
|
if(!empty($this->end_time)){
|
|
$query = $query->andWhere([
|
|
'<',
|
|
'bl.created_at',
|
|
strtotime($this->end_time)
|
|
]);
|
|
}
|
|
if(!empty($this->year)){
|
|
$date = $this->year;
|
|
$date .= !empty($this->month) ? $this->month : "";
|
|
$query = $query->andWhere([
|
|
'LIKE',
|
|
'FROM_UNIXTIME(bl.created_at,"%Y%m")',
|
|
$date
|
|
]);
|
|
}
|
|
return $query;
|
|
}
|
|
|
|
public function getMonthBop($date)
|
|
{
|
|
$query = IntegralLog::find()->alias('bl')
|
|
->leftJoin(['u' => User::tableName()], 'u.id=bl.user_id')
|
|
->where([
|
|
'bl.cx_mch_id' => $this->cx_mch_id,
|
|
'bl.scene' => $this->scene,
|
|
])
|
|
->andWhere([
|
|
'OR',
|
|
['LIKE', 'bl.desc', $this->keywords],
|
|
['LIKE', 'bl.order_no', $this->keywords],
|
|
['LIKE', 'u.username', $this->keywords],
|
|
['LIKE', 'u.nickname', $this->keywords],
|
|
['LIKE', 'u.real_name', $this->keywords],
|
|
])
|
|
->andFilterWhere([
|
|
'bl.user_id' => $this->user_id,
|
|
'bl.order_type' => $this->order_type,
|
|
])
|
|
->andWhere([
|
|
'LIKE',
|
|
'FROM_UNIXTIME(bl.created_at,"%Y%m")',
|
|
$date
|
|
]);
|
|
$income_query = clone $query;
|
|
$pay_query = clone $query;
|
|
$total_income = $income_query->andWhere(['bl.type' => Integral::TYPE_INCOME])->sum('integral');
|
|
$total_income = $total_income ? $total_income : 0;
|
|
$total_pay = $pay_query->andWhere(['bl.type' => Integral::TYPE_PAY])->sum('integral');
|
|
$total_pay = $total_pay ? $total_pay : 0;
|
|
$data = [
|
|
'date' => $date,
|
|
'total_income' => $total_income,
|
|
'total_pay' => $total_pay
|
|
];
|
|
return $data;
|
|
}
|
|
}
|
|
|
|
|
|
|