diff --git a/application/admin/controller/Order.php b/application/admin/controller/Order.php index 89567e2..8211365 100644 --- a/application/admin/controller/Order.php +++ b/application/admin/controller/Order.php @@ -390,6 +390,7 @@ class Order extends Backend $order->status = \app\admin\model\Order::STATUS_DISPATCHED; $order->dispatch_time = date('Y-m-d H:i:s'); // $order->dispatch_admin_id = $this->auth->id; + $order->worker_id = $worker_id; $order->save(); //日志 diff --git a/application/admin/controller/orders/Dispatch.php b/application/admin/controller/orders/Dispatch.php index 2e1cc12..1d7799b 100644 --- a/application/admin/controller/orders/Dispatch.php +++ b/application/admin/controller/orders/Dispatch.php @@ -155,6 +155,7 @@ class Dispatch extends Backend $order->status = Order::STATUS_DISPATCHED; $order->dispatch_time = date('Y-m-d H:i:s'); $order->dispatch_admin_id = $this->auth->id; + $order->worker_id = $worker->id; $order->save(); //order log diff --git a/application/admin/controller/statistics/Dispatcher.php b/application/admin/controller/statistics/Dispatcher.php index 1793ac0..447e51a 100644 --- a/application/admin/controller/statistics/Dispatcher.php +++ b/application/admin/controller/statistics/Dispatcher.php @@ -116,12 +116,12 @@ class Dispatcher extends Backend return $newData; } - //图表统计 - public function chart($filter,$getAll=false){ - - - + /** + * @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", @@ -140,39 +140,9 @@ class Dispatcher extends Backend // "SUM(CASE WHEN status = 60 THEN (field1 + field2) END) AS performance", ]; - $builder = $this->model - ->field($fields); - //->where('dispatch_admin_id','>',0); + return $this->model->field($fields)->group('worker_id')->buildSql(); - 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 = []; + /* $newData = []; if(!empty($data)){ foreach ($data as &$datum){ @@ -203,7 +173,7 @@ class Dispatcher extends Backend return $newData; }else{ return $data; - } + }*/ //dump($newData);exit; } @@ -211,10 +181,11 @@ class Dispatcher extends Backend /** * @param $a * @param $b - * @param $scale + * @param int $scale * @return int|string */ - private function _calc($a,$b,$scale=4,$is_percent=false){ + private function _calc($a, $b, int $scale=4, $is_percent=false): int|string + { $val = $b > 0 ? bcdiv($a,$b,$scale) : 0; if($is_percent){ diff --git a/application/admin/controller/statistics/Worker.php b/application/admin/controller/statistics/Worker.php new file mode 100644 index 0000000..3c2097a --- /dev/null +++ b/application/admin/controller/statistics/Worker.php @@ -0,0 +1,191 @@ +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()) { + 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; + } + + + +} diff --git a/application/admin/lang/zh-cn/statistics/worker.php b/application/admin/lang/zh-cn/statistics/worker.php new file mode 100644 index 0000000..b165c44 --- /dev/null +++ b/application/admin/lang/zh-cn/statistics/worker.php @@ -0,0 +1,22 @@ + 'ID', + 'Type' => '1 自营 2 非自营', + 'Name' => '师傅姓名', + 'Tel' => '师傅电话', + 'Status' => '师傅状态', + 'Status 1' => '激活', + 'Set status to 1' => '设为激活', + 'Status 0' => '冻结', + 'Set status to 0' => '设为冻结', + 'Area_id' => '所在地区', + 'Lng' => '经度', + 'Lat' => '纬度', + 'Location_update_time' => '位置更新时间', + 'Deposit_amount' => '保证金金额', + 'Star' => '信用(5星制)', + 'Create_time' => '创建时间', + 'Update_time' => '更新时间', + 'Deletetime' => '删除时间' +]; diff --git a/application/admin/validate/Sworker.php b/application/admin/validate/Sworker.php new file mode 100644 index 0000000..26fcc3c --- /dev/null +++ b/application/admin/validate/Sworker.php @@ -0,0 +1,27 @@ + [], + 'edit' => [], + ]; + +} diff --git a/application/admin/view/statistics/dispatcher/add.html b/application/admin/view/statistics/dispatcher/add.html deleted file mode 100644 index b49bd11..0000000 --- a/application/admin/view/statistics/dispatcher/add.html +++ /dev/null @@ -1,313 +0,0 @@ -
diff --git a/application/admin/view/statistics/dispatcher/edit.html b/application/admin/view/statistics/dispatcher/edit.html deleted file mode 100644 index 6b3b94c..0000000 --- a/application/admin/view/statistics/dispatcher/edit.html +++ /dev/null @@ -1,313 +0,0 @@ - diff --git a/application/admin/view/statistics/worker/index.html b/application/admin/view/statistics/worker/index.html new file mode 100644 index 0000000..e7f96ee --- /dev/null +++ b/application/admin/view/statistics/worker/index.html @@ -0,0 +1,26 @@ +