123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858 |
- <?php
- namespace app\sys\controller;
- use app\model\PreformanceTasks as PreformanceTasksModel;
- use app\logics\EmployeeLogic;
- use app\logics\OrgLogic;
- use app\model\ExamEmpResult;
- use app\model\ExamPaper;
- use app\model\ExamPaperQuestion;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use think\facade\View;
- use think\facade\Request;
- use app\model\ExamQuestion;
- use app\model\Setting;
- use app\model\Employee;
- use app\model\Org;
- use app\model\CustomerVisitLog;
- use app\model\Customer;
- use app\model\PreformanceTasksOrg;
- use app\model\TrainCredits;
- class PreformanceTasks
- {
- //业绩任务首页
- public function index(){
- $w[] = ['root_id','=',request()->employee->root_id];
- $w[] = ['del','=',0];
- $date = date('Y-m-d H:i:s');
- $data['all_count'] = $data['ing_count'] = $data['wks_count'] = $data['pass_count'] = $data['no_count'] = 0;
- $data['grawth'] = '';
- //排序 进行中0,逾期1,已完成2,未开始3
- //是否指派签单任务
- $m = 'IF(is_money=1,IF(ok_deposit>=deposit&&ok_money>=money,2,1),IF(ok_deposit>deposit,2,1))';
- //已通过
- $pass = 'IF(is_deposit=1,'.$m.',IF(ok_money>=money,2,1))';
- //进行中
- $ing = 'IF(end_date>=now(),0,'.$pass.')';
- //未开始
- $no = 'IF(start_date>now(),3,'.$ing.') as orders';
- $row = PreformanceTasksModel::where($w)->field('*,'.$no)->select()->toArray();
- foreach ($row as $v) {
- if ($v['orders']==0) {
- $data['ing_count']+=1;
- }elseif ($v['orders']==1) {
- $data['no_count']+=1;
- }elseif ($v['orders']==2) {
- $data['pass_count']+=1;
- }elseif ($v['orders']==3) {
- $data['wks_count']+=1;
- }
- }
- $data['all_count'] = count($row);//任务总数
- $data['grawth'] = $data['all_count']==0 ? '0%' : round($data['pass_count']/$data['all_count']*100,2).'%';//完成率
- View::assign('data',$data);
-
- $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->field('id,name,level')->order('path asc')->select()->toArray();
- view::assign('org', $org);
- return View::fetch();
- }
- //获取所有任务阶段内的所有交定签单
- public function orders(){
- $param = Request::only(['page'=>1,'limit'=>10,'type'=>1,'org_id'=>0,'keyword'=>'','start_date'=>'','end_date'=>'']);
- $deposit = CustomerVisitLog::changeState('已交定', 'chaos');
- $money = CustomerVisitLog::changeState('已签单', 'chaos');
- if ($param['org_id']) {
- $org = [$param['org_id']];
- }else{
- $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->column('id');
- }
- $or = [];
- if ($param['keyword']) {
- //业务员
- $em[] = ['root_id','=',request()->employee->root_id];
- $em[] = ['uid','>',0];
- $em[] = ['name','like','%'.$param['keyword'].'%'];
- $employee_id = Employee::where($em)->column('id');
- $or[] = ['employee_id','in',$employee_id];
- //客户姓名
- $customer_id = Customer::where([['org_id','in',$org],['name','like','%'.$param['keyword'].'%']])->column('id');
- $or[] = ['customer_id','in',$customer_id];
- //任务名称
- $pw[] = ['title','like','%'.$param['keyword'].'%'];
- $pw[] = ['root_id','=',request()->employee->root_id];
- $pw[] = ['del','=',0];
- $pw[] = ['customer_visit_log_id','<>',''];
- $pw[] = ['customer_visit_log_id','not null',''];
- $vid = PreformanceTasksModel::where($pw)->column('customer_visit_log_id');
- $or[] = ['id','in',explode(',',implode(',',$vid))];
- }
- if ($param['start_date']) $qw[] = ['addtime','>=',$param['start_date'].' 00:00:00'];
- if ($param['end_date']) $qw[] = ['addtime','<=',$param['end_date'].' 23:59:59'];
- //所有交定签单
- $qw[] = ['org_id','in',$org];
- $qw[] = ['state','in',array_merge($deposit,$money)];
- $qw[] = ['employee_id','>',0];
- $list = CustomerVisitLog::where($qw)->where(function($query) use ($or){
- $query->whereOr($or);
- })->page($param['page'],$param['limit'])->order('id desc')->select();
- $like = "%".implode("%,%",array_column($list->toArray(),'id'))."%";
- $qw1[] = ['customer_visit_log_id','like',explode(',',$like),'or'];
- $qw1[] = ['root_id','=',request()->employee->root_id];
- $qw1[] = ['del','=',0];
- if($param['keyword']) $qw1[] = ['title','like','%'.$param['keyword'].'%'];
- $tasks = PreformanceTasksModel::where($qw1)->select();
- $data = [];
- foreach ($list as $k => $v) {
- $date = date('Y-m-d',strtotime($v->addtime));
- $row = [
- 'id'=>$v->id,
- 'employee_name' => $v->employee->name,
- 'org_name' => $v->employee->org->name,
- 'org_id' => $v->employee->org->id,
- 'type' => (strpos($v->state,'定')!==false || $v->state==2) ? '成功收定' : '成功签单',
- 'customer_name' => $v->customer ? ($v->customer->name ?: '未知') : '',
- 'deposit_money' => $v->moeny ?: '',
- 'signed_money' => $v->money ?: '',
- 'show_money' => $v->money ? $v->money.'元' : '0元',
- 'date' => $date,
- 'time' => date('H:i:s',strtotime($v->addtime)),
- 'datetime' => $v->addtime,
- 'img' => isset($v->customer->employee->user->headimgurl) ? $v->customer->employee->user->headimgurl : '/static/img/default.png',
- 'tasks' => []
- ];
- $task = [];
- //所属任务,时间符合并且属于任务指派部门
- foreach ($tasks as $k2 => $v2) {
- $vids = explode(',',$v2['customer_visit_log_id']);
- if (in_array($v->id,$vids)) {
- //任务存在交定和签单
- if ($row['type']=='成功收定') {
- $task[] = $v2->title.':'.$v2->ok_deposit.'/'.$v2->deposit.'(单)';
- }elseif ($row['type']=='成功签单') {
- $task[] = $v2->title.':'.$v2->ok_money.'/'.$v2->money.'(元)';
- }
- }
- }
- if ($task) {
- foreach ($task as $k3 => $v3) {
- $row['tasks'] = [$v3];
- $data[$date][] = $row;
- }
- }else{
- $data[$date][] = $row;
- }
- unset($row);
- }
- $count = CustomerVisitLog::where($qw)->where(function($query) use ($or){
- $query->whereOr($or);
- })->count();
- //最新动态时间
- $new_time = CustomerVisitLog::where($qw)->order('id desc')->value('addtime');
- return json(['code' => 0, 'data' => $data, 'count' => $count,'time'=>$new_time, 'msg' => '获取成功']);
- }
- //获取所有任务
- public function tasks(){
- $param = Request::only(['page'=>1,'limit'=>10,'keyword'=>'']);
- $root_id = request()->employee->root_id;
- $w[] = ['root_id','=',$root_id];
- $w[] = ['del','=',0];
- if ($param['keyword']) $w[] = ['title','like','%'.trim($param['keyword']).'%'];
- //排序 进行中0,逾期1,已完成2,未开始3
- $orders = [0=>'进行中',1=>'已逾期',2=>'已完成',3=>'未开始'];
- //是否指派签单任务
- $m = 'IF(is_money=1,IF(ok_deposit>=deposit&&ok_money>=money,2,1),IF(ok_deposit>deposit,2,1))';
- //已通过
- $pass = 'IF(is_deposit=1,'.$m.',IF(ok_money>=money,2,1))';
- //进行中
- $ing = 'IF(end_date>=now(),0,'.$pass.')';
- //未开始
- $no = 'IF(start_date>now(),3,'.$ing.') as orders';
- $row = PreformanceTasksModel::where($w)->order('orders asc')->page($param['page'],$param['limit'])->field('*,'.$no)->select();
- $date = date('Y-m-d H:i:s');
- $color = ['未开始'=>'background:#249EFB;','进行中'=>'background:#FABA59;','已完成'=>'background:grey;','已逾期'=>'background:red;'];
- foreach ($row as $k => $v) {
- $v->start_dates = date('Y-m-d',strtotime($v->start_date));
- $v->end_dates = date('Y-m-d',strtotime($v->end_date));
- //状态
- $v->state = $orders[$v->orders];
- $v->color = $color[$v->state];
- }
- $count = PreformanceTasksModel::where($w)->count();
- return json(['code' => 0, 'data' => $row, 'count' => $count,'msg' => '获取成功']);
- }
-
-
-
- public function add(){
-
- return View::fetch();
- }
- //数据统计页面
- public function statistical(){
- $param = Request::only(['id'=>0,'org_id'=>0]);
- $data = $this->data_statistics($param['id'],$param['org_id']);
- View::assign('data',$data);
- View::assign('id',$param['id']);
- View::assign('org_id',$param['org_id']);
- return View::fetch();
- }
- //数据统计页面列表
- public function statistical_list(){
- $param = Request::only(['id'=>0,'org_id'=>0,'page'=>1,'limit'=>10,'keyword'=>'']);
- $root_id = request()->employee->root_id;
- $w0[] = ['id','=',$param['id']];
- $w0[] = ['root_id','=',$root_id];
- $w0[] = ['del','=',0];
- $w0[] = ['customer_visit_log_id','<>',''];
- $w0[] = ['customer_visit_log_id','not null',''];
- $info = PreformanceTasksModel::where($w0)->findOrEmpty();
- if ($info->isEmpty()) return json(['code' => 0, 'data' =>[], 'count' =>0,'msg' => '获取成功']);
- $w[] = ['performance_tasks_id','=',$param['id']];
- $w[] = ['root_id','=',$root_id];
- $w[] = ['org_id','=',$param['org_id']];
- $w[] = ['del','=',0];
- $row = PreformanceTasksOrg::where($w)->findOrEmpty();
- if ($row->isEmpty()) return json(['code' => 0, 'data' =>[], 'count' =>0,'msg' => '获取成功']);
- $vid = $info->customer_visit_log_id;
- $eid = CustomerVisitLog::where([['org_id','=',$param['org_id']],['id','in',explode(',',$vid)]])->group('employee_id')->column('employee_id');
- $wor[] = ['id','in',$eid];
- $wor[] = ['org_id','=',$param['org_id']];
- $w1[] = ['root_id','=',$root_id];
- $w1[] = ['state','in',['在职','离职']];
- $w1[] = ['uid','>',0];
- if($param['keyword']) $w1[] = ['name','like','%'.trim($param['keyword']).'%'];
- $list = Employee::where($w1)->where(function($query) use ($wor){
- $query->whereOr($wor);
- })->field('id,name')->page($param['page'],$param['limit'])->select()->toArray();
- if($info->is_deposit==1){
- //交定数量查询
- $w3[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
- $w3[] = ['id','in',explode(',',$vid)];
- $w3[] = ['org_id','=',$param['org_id']];
- $deposit = CustomerVisitLog::where($w3)->field('employee_id,customer_id')->group('employee_id,customer_id')->select()->toArray();
- $eid = [];
- foreach ($deposit as $k => $v) {
- $eid[$v['employee_id']] = isset($eid[$v['employee_id']]) ? $eid[$v['employee_id']]+=1 : 1;
- }
- }
- if($info->is_money==1){
- //签单金额查询
- $w33[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
- $w33[] = ['id','in',explode(',',$vid)];
- $w33[] = ['org_id','=',$param['org_id']];
- $money = CustomerVisitLog::where($w33)->field('sum(money) num,employee_id')->group('employee_id')->order('num desc')->column('sum(money) num','employee_id');
- $eidm = [];
- foreach ($money as $k5 => $v5) {
- $eidm[$k5] = $v5;
- }
- }
- if($info->is_credit == 1){
- $w66[] = ['id', 'in', explode(',', $vid)];
- $credit = TrainCredits::where($w66)->field('sum(credits) num,employee_id')->group('employee_id')->order('num desc')->column('sum(credits) num', 'employee_id');
- }
- foreach ($list as $k6 => $v6) {
- $list[$k6]['deposit'] = isset($eid[$v6['id']]) ? $eid[$v6['id']] : 0;
- $list[$k6]['money'] = isset($eidm[$v6['id']]) ? $eidm[$v6['id']] : 0;
- $list[$k6]['credit'] = isset($credit[$v6['id']]) ? $credit[$v6['id']] : 0;
- }
- $count = Employee::where($w1)->where(function($query) use ($wor){
- $query->whereOr($wor);
- })->count();
- return json(['code' => 0, 'data' =>$list, 'count' =>$count,'msg' => '获取成功']);
- }
- //任务详情页面
- public function details(){
- $id = input('id',0);
- View::assign('id',$id);
- return View::fetch();
- }
- //任务详情列表
- public function details_list(){
- $param = Request::only(['page'=>1,'limit'=>10,'id'=>0]);
- $root_id = request()->employee->root_id;
- $w[] = ['performance_tasks_id','=',$param['id']];
- $w[] = ['root_id','=',$root_id];
- $list = PreformanceTasksOrg::with(['org'=>function($query){
- $query->visible(['id','name'])->bind(['name']);
- }])->where($w)->page($param['page'],$param['limit'])->select();
- $count = PreformanceTasksOrg::where($w)->count();
- return json(['code' => 0, 'data' => $list, 'count' => $count,'msg' => '获取成功']);
- }
- //交定排行
- public function deposit(){
- $param = Request::only(['id'=>0]);
- View::assign('id',$param['id']);
- $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->field('id,name,level')->order('path asc')->select()->toArray();
- view::assign('org', $org);
- return View::fetch();
- }
- //0交定 1签单排行数据
- public function deposit_list(){
- $param = Request::only(['id'=>0 ,'org_id'=>0,'type'=>0,'page'=>1,'limit'=>10,'keyword'=>'']);
- $root_id = request()->employee->root_id;
- $w0[] = ['id','=',$param['id']];
- $w0[] = ['root_id','=',$root_id];
- $w0[] = ['del','=',0];
- $w0[] = $param['type']==0 ? ['is_deposit','=',1] : ['is_money','=',1];
- $w0[] = ['customer_visit_log_id','<>',''];
- $w0[] = ['customer_visit_log_id','not null',''];
- $info = PreformanceTasksModel::with(['preformanceTasksOrg'=>function($query){
- $query->visible(['org_id','performance_tasks_id,ok_deposit,ok_money'])->where('del',0);
- }])->where($w0)->findOrEmpty();
- if ($info->isEmpty() || $info->preformanceTasksOrg->isEmpty()) return json(['code' => 0, 'data' =>[], 'count' =>0,'msg' => '获取成功']);
- $org_ids = $param['org_id'] ? [$param['org_id']] : array_column($info->preformanceTasksOrg->toArray(),'org_id');
- //如果有任务期间有切换部门的情况也要计算排名
- $vid = $info->customer_visit_log_id;
- $eid = CustomerVisitLog::where([['id','in',explode(',',$vid)],['org_id','in',$org_ids]])->group('employee_id')->column('employee_id');
- $wor[] = ['id','in',$eid];
- $wor[] = ['org_id','in',$org_ids];
- $w1[] = ['root_id','=',$root_id];
- $w1[] = ['state','in',['在职','离职']];
- if ($param['keyword']) $w1[] = ['name','like','%'.trim($param['keyword']).'%'];
- $employee = Employee::where($w1)->where(function($query) use ($wor){
- $query->whereOr($wor);
- })->field('id,name,org_id')->select()->toArray();
- if ($param['type']==0) {
- //交定,一个业务员在两个部门交定同一个客户算两个交定业绩sb
- $w22[] = ['id','in',explode(',',$vid)];
- $w22[] = ['state', 'in',CustomerVisitLog::changeState('已交定', 'chaos')];
- $w22[] = ['org_id','in',$org_ids];
- // $deposit = CustomerVisitLog::where($w22)->field('employee_id,customer_id')->group('employee_id,customer_id')->select()->toArray();
- // $eidcc = [];
- // foreach ($deposit as $k => $v) {
- // $eidcc[$v['employee_id']] = !isset($eidcc[$v['employee_id']]) ? 1 : $eidcc[$v['employee_id']]+=1;
- // }
- $eidcc = CustomerVisitLog::where($w22)->group('employee_id')->column('count(id) count','employee_id');
- }else{
- $w3[] = ['id','in',explode(',',$vid)];
- $w3[] = ['state', 'in',CustomerVisitLog::changeState('已签单', 'chaos')];
- $w3[] = ['org_id','in',$org_ids];
- // $money = CustomerVisitLog::where($w3)->field('sum(money) num,employee_id')->group('employee_id')->order('num desc')->column('sum(money) num','employee_id');
- // $eidcc = [];
- // foreach ($money as $kk => $vv) {
- // $eidcc[$kk] = $vv;
- // }
- $eidcc = CustomerVisitLog::where($w3)->field('sum(money) num,employee_id')->group('employee_id')->order('num desc')->column('sum(money) num','employee_id');
- }
- foreach ($employee as $k2 => $v2) {
- $employee[$k2]['value'] = isset($eidcc[$v2['id']]) ? $eidcc[$v2['id']] : 0;
- }
- $count = count($employee);
- array_multisort(array_column($employee,'value'), SORT_DESC, $employee);
- foreach ($employee as $k6 => $v6) {
- $employee[$k6]['ranking'] = $k6+1;
- }
- $list = array_slice($employee,($param['page']-1)*$param['limit'],$param['limit']);
- $orgs = Org::where([['id','in',array_column($list,'org_id')]])->column('name','id');
- foreach ($list as $k7 => $v7) {
- $list[$k7]['org_name'] = $orgs[$v7['org_id']];
- }
- return json(['code' => 0, 'data'=>$list,'count'=>$count,'msg' => '获取成功']);
- }
- //签单排行
- public function money(){
- $param = Request::only(['id'=>0]);
- View::assign('id',$param['id']);
- $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->field('id,name,level')->order('path asc')->select()->toArray();
- view::assign('org', $org);
- return View::fetch();
- }
- //保存任务
- public function add_data(){
- $param = Request::only(['title'=>'','start_date'=>'','end_date'=>'','deposit'=>0,'money'=>0,'is_deposit'=>0,'is_money'=>0,'','time'=>'','credit'=>0,'is_credit'=>0]);
- $time = explode(' - ',$param['time']);
- $param['start_date'] = $time[0].' '.date('H:i:s');
- $param['end_date'] = $time[1].' 23:59:59';
- unset($param['time']);
- $param['root_id'] = request()->employee->root_id;
- PreformanceTasksModel::insertGetId($param);
- return json(['code' => 0, 'data' =>'保存成功','msg' => '保存成功']);
- }
- //编辑页面
- public function edit(){
- $param = Request::only(['id'=>0]);
- View::assign('id',$param['id']);
- $root_id = request()->employee->root_id;
- $w[] = ['id','=',$param['id']];
- $w[] = ['root_id','=',$root_id];
- $row = PreformanceTasksModel::where($w)->findOrEmpty();
- if($row->isEmpty()) return json(['code' => 1, 'data' =>'数据不存在','msg' => '数据不存在']);
- $row->time = date('Y-m-d',strtotime($row->start_date)).' - '.date('Y-m-d',strtotime($row->end_date));
- View::assign('data',$row);
- return View::fetch();
- }
- //编辑任务
- public function edit_data(){
- $param = Request::only(['id'=>0,'title'=>'','start_date'=>'','end_date'=>'','deposit'=>0,'money'=>0,'is_deposit'=>0,'is_money'=>0,'','time'=>'','credit'=>0,'is_credit'=>0]);
- if($param['start_date']<$param['end_date']) return json(['code' => 1, 'data' =>'开始时间要小于结束时间','msg' => '开始时间要小于结束时间']);
- $time = explode(' - ',$param['time']);
- $param['start_date'] = $time[0].' '.date('H:i:s');
- $param['end_date'] = $time[1].' 23:59:59';
- unset($param['time']);
- $root_id = request()->employee->root_id;
- $w[] = ['id','=',$param['id']];
- $w[] = ['root_id','=',$root_id];
- $row = PreformanceTasksModel::where($w)->findOrEmpty();
- if($row->isEmpty()) return json(['code' => 1, 'data' =>'数据不存在','msg' => '数据不存在']);
- $date = date('Y-m-d H:i:s');
- if($row->end_date < $date) return json(['code' => 1, 'data' =>'任务已结束无法编辑','msg' => '任务已结束无法编辑']);
- //查询已经分配的任务
- $w1[] = ['root_id','=',$root_id];
- $w1[] = ['performance_tasks_id','=',$row->id];
- $w1[] = ['del','=',0];
- $deposit = PreformanceTasksOrg::where($w1)->sum('deposit');
- if ($deposit>0 && !$param['is_deposit']) {
- return json(['code' => 1, 'data' =>'已经指派交定任务无法取消','msg' => '已经指派交定任务无法取消']);
- }
- if ($deposit>0 && $param['deposit'] < $deposit) {
- return json(['code' => 1, 'data' =>'交定目标不能小于已经指派的数量','msg' => '交定目标不能小于已经指派的数量']);
- }
- $money = PreformanceTasksOrg::where($w1)->sum('money');
- if ($money>0 && !$param['is_money']) {
- return json(['code' => 1, 'data' =>'已经指派签单任务无法取消','msg' => '已经指派签单任务无法取消']);
- }
- if ($money>0 && $param['money'] < $money) {
- return json(['code' => 1, 'data' =>'签单金额不能小于已经指派的金额','msg' => '签单金额不能小于已经指派的金额']);
- }
- unset($param['id']);
- $row->save($param);
- return json(['code' => 0, 'data' =>'编辑成功','msg' => '编辑成功']);
- }
- //指派页面
- public function assign(){
- $id = input('id',0);
- View::assign('id',$id);
- View::assign('type','');
- $root_id = request()->employee->root_id;
- $w[] = ['performance_tasks_id','=',$id];
- $w[] = ['root_id','=',$root_id];
- $w[] = ['del','=',0];
- $row = preformanceTasksOrg::with(['org'=>function($query){
- $query->visible(['id,name'])->bind(['name']);
- }])->where($w)->field(['id','root_id','performance_tasks_id','org_id','deposit','money','credit'])->select()->toArray();
- $w1[] = ['org_id','in',array_column($row,'org_id')];
- $w1[] = ['state','=','在职'];
- $w1[] = ['root_id','=',$root_id];
- $ec = Employee::where($w1)->group('org_id')->column('count(id) count','org_id');
- $sum['deposit'] = $sum['money'] = $sum['credit'] = 0;
- foreach ($row as $k => $v) {
- unset($v->org);
- $row[$k]['count'] = isset($ec[$v['org_id']]) ? $ec[$v['org_id']] : 0;
- $sum['deposit'] += $v['deposit'];
- $sum['money'] += $v['money'];
- $sum['credit'] += $v['credit'];
- }
- $count = count($row);
- View::assign('count',$count);
- View::assign('data',$row);
-
- $w11[] = ['id','=',$id];
- $w11[] = ['root_id','=',$root_id];
- $w11[] = ['del','=',0];
- $info = PreformanceTasksModel::where($w11)->find();
- $sum['deposit'] = $info->deposit-$sum['deposit'];
- $sum['money'] = $info->money-$sum['money'];
- $sum['credit'] = $info->credit-$sum['credit'];
- View::assign('sum',$sum);
- return View::fetch();
- }
- //指派
- public function assign_org(){
- $param = Request::only(['id'=>0,'title'=>'','start_date'=>'','end_date'=>'','deposit'=>0,'money'=>0,'is_deposit'=>0,'is_money'=>0,'']);
- if($param['start_date']<$param['end_date']) return json(['code' => 1, 'data' =>'开始时间要小于结束时间','msg' => '开始时间要小于结束时间']);
- $root_id = request()->employee->root_id;
- $w[] = ['id','=',$param['id']];
- $w[] = ['root_id','=',$root_id];
- $row = PreformanceTasksModel::where($w)->findOrEmpty();
- if($row->isEmpty()) return json(['code' => 1, 'data' =>'数据不存在','msg' => '数据不存在']);
- unset($param['id']);
- $row->save($param);
- return json(['code' => 0, 'data' =>'编辑成功','msg' => '编辑成功']);
- }
- /*
- * 指派组织
- */
- public function get_person()
- {
- $id = input('id',0);//任务id
- $root_id = request()->employee->root_id;
- $keyword = input('keyword','');
- $where = [
- ['path', 'like', $root_id . '-%'],
- ['status', '=', 1]
- ];
- if ($keyword) {
- // $where[] = ['name','like','%'.trim($keyword).'%'];
- }
- $count = Employee::where([['root_id','=',$root_id],['state','like','%在职%'],['uid','>',0]])->group('org_id')->column('count(*) count','org_id');
- $info = PreformanceTasksOrg::where(['root_id'=>request()->employee->root_id , 'performance_tasks_id'=>$id,'del'=>0])->column('org_id');
- $allnodes = Org::where($where)->field('id,pid,name title,level,org_type,info,path')->order('level asc, id asc')->select()->toarray();
- foreach ($allnodes as $k => $v) {
- $allnodes[$k]['count'] = isset($count[$v['id']]) ? $count[$v['id']] : 0;//本部门人数
- $allnodes[$k]['all_count'] = 0;//包含子部门总数
- $allnodes[$k]['org_class'] = 'org_'.$v['id'];
- $allnodes[$k]['per_class'] = 'per_org_'.$v['id'];
- $allnodes[$k]['type'] = 'org';
- foreach ($allnodes as $k2 => $v2) {
- if (strpos($v2['path'],$v['path']) !== false) {
- $allnodes[$k]['all_count'] = isset($count[$v2['id']]) ? $count[$v2['id']]+$allnodes[$k]['all_count'] : $allnodes[$k]['all_count'];
- }
- }
- $allnodes[$k]['y_title'] = $allnodes[$k]['title'];
- $allnodes[$k]['title'] = $allnodes[$k]['title'].' ('.$allnodes[$k]['count'].'人)';
- $allnodes[$k]['selected'] = in_array($v['id'],$info);
- }
- $tree = $this->tree($allnodes,0);
- return json(['code' => 0, 'data' => $tree, 'checkOrg' =>'']);
- }
- public function tree($data,$pid = 0)
- {
- $new_arr = [];
- foreach($data as $k => $v){
- if($v['pid'] == $pid) {
- $children = $this->tree($data, $v['id']);
- $v['children'] = $children;
- if (empty($v['children'])) $v['disabled']=true;
- $new_arr[] =$v;
- }
- }
- return $new_arr;
- }
- //删除任务
- public function del_tasks()
- {
- $id = input('id',0);
- $root_id = request()->employee->root_id;
- $w[] = ['id','=',$id];
- $w[] = ['root_id','=',$root_id];
- $row = PreformanceTasksModel::where($w)->findOrEmpty();
- if($row->isEmpty()) return json(['code' => 1, 'data' =>'数据不存在','msg' => '数据不存在']);
- $row->del = 1;
- $row->save();
- return json(['code' => 0, 'data' =>'删除成功','msg' => '删除成功']);
- }
- //获取部门子部门数量
- public function get_cid(){
- $id = input('id',0);
- $root_id = request()->employee->root_id;
- $w[] = ['id','=',$id];
- $w[] = ['path','like',$root_id.'-%'];
- $row = Org::where($w)->findOrEmpty();
- if ($row->isEmpty()) return json(['code' => 0, 'data' =>[],'msg' =>'']);
- $w1[] = ['path','like',$row->path.'%'];
- $w1[] = ['id','=',$id];
- $list = Org::withCount(['employee'=>function($query){
- $query->where([['state','=','在职'],['uid','>',0]]);
- }])->where($w1)->field(['id','name'])->select();
- return json(['code' => 0, 'data' =>$list,'msg' =>'']);
- }
- //数据统计api,,,任务id,任务指派的部门id
- public function data_statistics($id,$org_id=0){
- //实际交定,实际签单,平均交定,平均签单
- //交定最多人,签单最多人,签单最低人,交定最低人
- $data = [
- 'ok_deposit' => 0,
- 'ok_money' => 0,
- 'avg_deposit' => 0,
- 'avg_money' => 0,
- 'deposit_top' => '',
- 'money_top' => '',
- 'deposit_down' => '',
- 'money_down' => '',
- 'ok_credit' =>0,
- 'avg_credit' =>0
- ];
- //数据不存在
- $root_id = request()->employee->root_id;
- $w[] = ['id','=',$id];
- $w[] = ['root_id','=',$root_id];
- $w[] = ['del','=',0];
- $w[] = ['customer_visit_log_id','<>',''];
- $w[] = ['customer_visit_log_id','not null',''];
- $row = PreformanceTasksModel::where($w)->findOrEmpty();
- if ($row->isEmpty()) return $data;
- $w1[] = ['root_id','=',$root_id];
- $w1[] = ['performance_tasks_id','=',$id];
- $w1[] = ['del','=',0];
- $w1[] = ['org_id','=',$org_id];
- $org = PreformanceTasksOrg::where($w1)->findOrEmpty();
- if ($org->isEmpty()) return $data;
- //实际交定,实际签单
- $data['ok_deposit'] = $org->ok_deposit;//数量
- $data['ok_money'] = $org->ok_money;
- $data['ok_credit'] = $org->ok_credit;
- $vid = $row->customer_visit_log_id;
- //部门员工人数
- //如果有任务期间有切换部门的情况也要计算排名
- $eid = CustomerVisitLog::where([['org_id','=',$org_id],['id','in',explode(',',$vid)]])->group('employee_id')->column('employee_id');
- $wor[] = ['id','in',$eid];
- $wor[] = ['org_id','=',$org_id];
- $w2[] = ['root_id','=',$root_id];
- $w2[] = ['state','in',['在职','离职']];
- $eid = Employee::where($w2)->where(function($query) use ($wor){
- $query->whereOr($wor);
- })->column('name','id');
- $eids = array_keys($eid);
- //平均交定,平均签单
- $data['avg_deposit'] = count($eids) ? round($org->ok_deposit/count($eids),1) : 0;
- $data['avg_money'] = count($eids) ? round($org->ok_money/count($eids),1) : 0;
- $data['avg_credit'] = count($eids) ? round($org->ok_credit/count($eids),1) : 0;
- if($row->is_deposit==1){
- //交定
- $w22[] = ['id','in',explode(',',$vid)];
- $w22[] = ['state', 'in',CustomerVisitLog::changeState('已交定', 'chaos')];
- $w22[] = ['org_id','=',$org_id];
- //一个客户可以不同的人交定多次
- $deposit = CustomerVisitLog::where($w22)->field('employee_id,customer_id')->group('employee_id,customer_id')->select()->toArray();
- $eidcc = [];
- foreach ($deposit as $k => $v) {
- $eidcc[$v['employee_id']] = !isset($eidcc[$v['employee_id']]) ? 1 : $eidcc[$v['employee_id']]+=1;
- }
- $eidc = [];
- foreach ($eids as $v2) {
- $eidc[] = [
- 'id'=>$v2,
- 'count'=>isset($eidcc[$v2]) ? $eidcc[$v2] : 0
- ];
- }
- array_multisort(array_column($eidc,'count'), SORT_DESC, $eidc);
- $top = isset($eidc[0]['count']) ? $eidc[0]['count'] : 0;
- $down = isset(end($eidc)['count']) ? end($eidc)['count'] : 0;
- foreach ($eidc as $k3 => $v3) {
- if ($top==$v3['count']) $data['deposit_top'] .= $eid[$v3['id']].',';//所有第一名
- if ($down==$v3['count']) $data['deposit_down'] .= $eid[$v3['id']].',';//所有最后一名
- }
- }
- if($row->is_money==1){
- $w3[] = ['id','in',explode(',',$vid)];
- $w3[] = ['state', 'in',CustomerVisitLog::changeState('已签单', 'chaos')];
- $w3[] = ['org_id','=',$org_id];
- $money = CustomerVisitLog::where($w3)->field('sum(money) num,employee_id')->group('employee_id')->order('num desc')->column('sum(money) num','employee_id');
- $eidm = [];
- foreach ($eids as $v5) {
- $eidm[] = [
- 'id'=>$v5,
- 'num' => isset($money[$v5]) ? (int)$money[$v5] : 0
- ];
- }
- array_multisort(array_column($eidm,'num'), SORT_DESC, $eidm);
- $top = isset($eidm[0]['num']) ? (int)$eidm[0]['num'] : 0;
- $down = isset(end($eidm)['num']) ? (int)end($eidm)['num'] : 0;
- foreach ($eidm as $k4 => $v4) {
- if ($top==$v4['num']) $data['money_top'] .= $eid[$v4['id']].',';//所有第一名
- if ($down==$v4['num']) $data['money_down'] .= $eid[$v4['id']].',';//所有最后一名
- }
- }
- return $data;
- }
- //保存指派
- public function save()
- {
- $param = input('arr','');
- if(!is_array($param)) return json(['code' => 1, 'data' =>'请选择部门','msg' =>'请选择部门']);
- $id = input('id',0);
- //数据不存在
- $root_id = request()->employee->root_id;
- $w[] = ['id','=',$id];
- $w[] = ['root_id','=',$root_id];
- $w[] = ['del','=',0];
- $row = PreformanceTasksModel::where($w)->findOrEmpty();
- if ($row->isEmpty()) return json(['code' => 1, 'data' =>'任务不存在','msg' =>'任务不存在']);
- $deposit = array_sum(array_column($param,'deposit'));
- if ($row->deposit<$deposit) return json(['code' => 1, 'data' =>'交定总数不能大于任务设置总数','msg' =>'交定总数不能大于任务设置总数']);
- $money = array_sum(array_column($param,'money'));
- if ($row->money<$money) return json(['code' => 1, 'data' =>'签单金额不能大于任务设置金额','msg' =>'签单金额不能大于任务设置金额']);
- $dw[] = ['root_id','=',$root_id];
- $dw[] = ['performance_tasks_id','=',$id];
- PreformanceTasksOrg::where($dw)->delete();
- foreach ($param as $k => $v) {
- if (!$v['deposit'] && !$v['money'] && !$v['credit']) {
- continue;
- }
- $w1 = [];
- $w1[] = ['performance_tasks_id','=',$id];
- $w1[] = ['root_id','=',$root_id];
- $w1[] = ['del','=',0];
- $w1[] = ['org_id','=',$v['org_id']];
- $check = PreformanceTasksOrg::where($w1)->findOrEmpty();
- if ($check->isEmpty()) {
- $save = [
- 'root_id' => $root_id,
- 'performance_tasks_id' => $id,
- 'del' => 0,
- 'deposit' => $v['deposit'],
- 'money' => $v['money'],
- 'org_id' => $v['org_id'],
- 'is_deposit' => $row->is_deposit,
- 'is_money' => $row->is_money,
- 'credit' =>$v['credit'],
- 'is_credit' => $row->is_credit
- ];
- $pid = PreformanceTasksOrg::insertGetId($save);
- unset($save);
-
- }else{
- $check->deposit = $v['deposit'];
- $check->money = $v['money'];
- $check->credit = $v['credit'];
- $check->is_deposit = $row->is_deposit;
- $check->is_money = $row->is_money;
- $check->is_credit = $row->is_credit;
- $check->save();
- }
- unset($w1);
- }
- return json(['code' => 0, 'data' =>'指派成功','msg' =>'指派成功']);
- }
- }
|