allocatr/application/admin/controller/statistics/Worker.php
2025-04-25 16:37:07 +08:00

197 lines
6.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\admin\controller\statistics;
use app\admin\model\Admin;
use app\admin\model\Order;
use app\admin\model\OrderDispatch;
use app\common\controller\Backend;
use think\Exception;
use think\exception\DbException;
use think\response\Json;
/**
* 师傅列管理
*
* @icon fa fa-circle-o
*/
class Worker extends Backend
{
/**
* Sworker模型对象
* @var \app\admin\model\Worker
*/
protected $model = null;
/**
* @var Order
*/
private Order $OrderModel;
/**
* @var OrderDispatch
*/
private OrderDispatch $DispatchModel;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\Worker();
$this->OrderModel = new Order();
$this->DispatchModel = new OrderDispatch();
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*
* @return string|Json
* @throws Exception
* @throws DbException
*/
public function index()
{
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if (false === $this->request->isAjax()) {
$appkey = config('map.baidu_app_key');
$this->assign('mapkey',$appkey);
return $this->view->fetch();
}
//如果发送的来源是 Selectpage则转发到 Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
[$where, $sort, $order, $offset, $limit] = $this->buildparams();
$list = $this->model
->where($where)
->order($sort, $order)
->paginate($limit);
$result = ['total' => $list->total(), 'rows' => $list->items()];
return json($result);
}
//图表统计
public function chart($filter,$getAll=false){
$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",
];
$builder = $this->model
->field($fields);
//->where('dispatch_admin_id','>',0);
if(!empty($filter['admin_id'])){
$builder->where('dispatch_admin_id',$filter['admin_id']);
}
if(!empty($filter['start_time']) && !empty($filter['end_time'])){
$time_by = $filter['time_by'] ??1;
if($time_by == 1){ //按派单时间
$time_field = 'dispatch_time';
}else{ //按录单时间
$time_field = 'create_time';
}
$builder->whereBetween($time_field,[$filter['start_time'],$filter['end_time']]);
}
//城市
if(!empty($filter['area_id'])){
$builder->where('area_id',$filter['area_id']);
}
//项目
if(!empty($filter['item_id'])){
$builder->where('item_id',$filter['item_id']);
}
if($getAll){
$data = $builder->group('dispatch_admin_id')->limit(50)->select();
}else{
$data = $builder->group('dispatch_admin_id')->paginate();
}
$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 $scale
* @return int|string
*/
private function _calc($a,$b,$scale=4,$is_percent=false){
$val = $b > 0 ? bcdiv($a,$b,$scale) : 0;
if($is_percent){
return bcmul($val,100,2);
}
return $val;
}
}