199 lines
6.1 KiB
PHP
199 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\statistics;
|
|
|
|
use app\admin\model\Admin;
|
|
use app\admin\model\Order;
|
|
use app\common\controller\Backend;
|
|
use think\exception\DbException;
|
|
use think\response\Json;
|
|
use function Symfony\Component\Clock\now;
|
|
|
|
/**
|
|
* 订单列管理
|
|
*
|
|
* @icon fa fa-circle-o
|
|
*/
|
|
class Dispatcher extends Backend
|
|
{
|
|
|
|
/**
|
|
* Staorder模型对象
|
|
* @var \app\admin\model\Order
|
|
*/
|
|
protected $model = null;
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->model = new \app\admin\model\Order();
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
|
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
|
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
|
*/
|
|
|
|
|
|
/**
|
|
* 查看
|
|
*
|
|
* @return string|Json
|
|
* @throws \think\Exception
|
|
* @throws DbException
|
|
*/
|
|
public function index()
|
|
{
|
|
//$this->chart();
|
|
$today = now()->format('Y-m-d');
|
|
$today_end = now()->format('Y-m-d');
|
|
|
|
|
|
//设置过滤方法
|
|
$this->request->filter(['strip_tags', 'trim']);
|
|
if (false === $this->request->isAjax()) {
|
|
$this->assign('daterange',$today.' - '.$today_end);
|
|
|
|
$this->assignconfig('default_daterange',now()->format('Y-m-d 00:00:00').' - '.now()->format('Y-m-d 23:59:59'));
|
|
|
|
return $this->view->fetch();
|
|
}
|
|
|
|
$filter = $this->request->param('filter');
|
|
$filter = json_decode($filter,true);
|
|
|
|
if(!empty($filter['daterange'])){
|
|
$arr = explode(' - ',$filter['daterange']);
|
|
if(trim($arr[0])){
|
|
$filter['start_time'] = trim($arr[0]);
|
|
}
|
|
if(trim($arr[1])){
|
|
$filter['end_time'] = trim($arr[1]);
|
|
}
|
|
}
|
|
|
|
$list = $this->chart($filter,false);
|
|
|
|
$result = array("total" => $list->total(), "rows" => $list->items());
|
|
|
|
return json($result);
|
|
}
|
|
|
|
|
|
public function chartData()
|
|
{
|
|
$filter = $this->request->post();
|
|
|
|
if(!empty($filter['daterange'])){
|
|
$arr = explode(' - ',$filter['daterange']);
|
|
if(trim($arr[0])){
|
|
$filter['start_time'] = trim($arr[0]);
|
|
}
|
|
if(trim($arr[1])){
|
|
$filter['end_time'] = trim($arr[1]).' 23:59:59';
|
|
}
|
|
}
|
|
|
|
$data = $this->chart($filter,true);
|
|
|
|
|
|
$newData = [
|
|
['派单员','总业绩','转化率','利润率','变现值']
|
|
];
|
|
foreach ($data as $datum){
|
|
$newData[] = [
|
|
$datum['admin_user'],
|
|
$datum['performance'],
|
|
$datum['trans_rate'],
|
|
$datum['performance_rate'],
|
|
$datum['cash_value'],
|
|
];
|
|
}
|
|
return $newData;
|
|
}
|
|
|
|
|
|
/**
|
|
* @throws DbException
|
|
*/
|
|
public function orderSubSql()
|
|
{
|
|
$orderValid = implode(',',$this->model->tabStatus(Order::TAB_VALID));
|
|
|
|
//"COUNT(CASE WHEN status IN (".$orderValid.") THEN 1 END) AS ing_num",
|
|
$fields = [
|
|
'dispatch_admin_id',
|
|
"COUNT(CASE WHEN status = 60 THEN 1 END) AS finish_num", //完成数
|
|
"COUNT(CASE WHEN status IN (".$orderValid.") THEN 1 END) AS count_num", //总订单数 (排除取消 和草稿)
|
|
"SUM(CASE WHEN status = 60 THEN total END) AS total", //成效额
|
|
"SUM(CASE WHEN status = 60 THEN performance END) AS performance", //业绩
|
|
|
|
"SUM(CASE WHEN status = 60 THEN (cost + material_cost) END) AS cost_total", //总成本
|
|
|
|
"SUM(CASE WHEN status = 60 THEN (refund_amount + worker_refund_amount) END) AS refund_total", //退款总数
|
|
"COUNT(CASE WHEN refund_amount > 0 OR worker_refund_amount > 0 THEN 1 END) AS refund_count", //退款订单数量
|
|
"AVG(CASE WHEN status > 10 THEN UNIX_TIMESTAMP(dispatch_time) - UNIX_TIMESTAMP(create_time) END) AS avg_time_diff", //派单时效
|
|
// "SUM(CASE WHEN status = 60 THEN (field1 + field2) END) AS performance",
|
|
];
|
|
|
|
return $this->model->field($fields)->group('worker_id')->buildSql();
|
|
|
|
/* $newData = [];
|
|
|
|
if(!empty($data)){
|
|
foreach ($data as &$datum){
|
|
//利润率 = 总业绩/总成效额
|
|
$datum->performance_rate = $this->_calc($datum->performance,$datum->total,4,true);
|
|
//转化率 = 完单数 / 总订单数
|
|
$datum->trans_rate = $this->_calc($datum->finish_num,$datum->count_num,4,true);
|
|
//变现值 = 总业绩 / 总订单数
|
|
$datum->cash_value = $this->_calc($datum->performance,$datum->count_num,2);
|
|
//客单利润 = 总利润 / 完单数
|
|
$datum->performance_avg = $this->_calc($datum->performance,$datum->finish_num,2);
|
|
//客单价 = 总成效额 / 完单数
|
|
$datum->total_avg = $this->_calc($datum->total,$datum->finish_num,2);
|
|
|
|
if(!empty($datum->dispatch_admin_id)){
|
|
$datum->admin_user = Admin::where($datum->dispatch_admin_id)->value('nickname')??'系统';
|
|
}else{
|
|
$datum->admin_user = '系统';
|
|
}
|
|
$datum->avg_time_diff = $this->_calc($datum->avg_time_diff,3600*24,2);
|
|
|
|
$datum->id = $datum->dispatch_admin_id;
|
|
$newData[] = $datum->toArray();
|
|
}
|
|
}
|
|
|
|
if($getAll){
|
|
return $newData;
|
|
}else{
|
|
return $data;
|
|
}*/
|
|
//dump($newData);exit;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $a
|
|
* @param $b
|
|
* @param int $scale
|
|
* @return int|string
|
|
*/
|
|
private function _calc($a, $b, int $scale=4, $is_percent=false): int|string
|
|
{
|
|
$val = $b > 0 ? bcdiv($a,$b,$scale) : 0;
|
|
|
|
if($is_percent){
|
|
|
|
return bcmul($val,100,2);
|
|
}
|
|
return $val;
|
|
}
|
|
|
|
}
|