PreformanceTasks.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. <?php
  2. namespace app\sys\controller;
  3. use app\model\PreformanceTasks as PreformanceTasksModel;
  4. use app\logics\EmployeeLogic;
  5. use app\logics\OrgLogic;
  6. use app\model\ExamEmpResult;
  7. use app\model\ExamPaper;
  8. use app\model\ExamPaperQuestion;
  9. use PhpOffice\PhpSpreadsheet\IOFactory;
  10. use think\facade\View;
  11. use think\facade\Request;
  12. use app\model\ExamQuestion;
  13. use app\model\Setting;
  14. use app\model\Employee;
  15. use app\model\Org;
  16. use app\model\CustomerVisitLog;
  17. use app\model\Customer;
  18. use app\model\PreformanceTasksOrg;
  19. use app\model\TrainCredits;
  20. class PreformanceTasks
  21. {
  22. //业绩任务首页
  23. public function index(){
  24. $w[] = ['root_id','=',request()->employee->root_id];
  25. $w[] = ['del','=',0];
  26. $date = date('Y-m-d H:i:s');
  27. $data['all_count'] = $data['ing_count'] = $data['wks_count'] = $data['pass_count'] = $data['no_count'] = 0;
  28. $data['grawth'] = '';
  29. //排序 进行中0,逾期1,已完成2,未开始3
  30. //是否指派签单任务
  31. $m = 'IF(is_money=1,IF(ok_deposit>=deposit&&ok_money>=money,2,1),IF(ok_deposit>deposit,2,1))';
  32. //已通过
  33. $pass = 'IF(is_deposit=1,'.$m.',IF(ok_money>=money,2,1))';
  34. //进行中
  35. $ing = 'IF(end_date>=now(),0,'.$pass.')';
  36. //未开始
  37. $no = 'IF(start_date>now(),3,'.$ing.') as orders';
  38. $row = PreformanceTasksModel::where($w)->field('*,'.$no)->select()->toArray();
  39. foreach ($row as $v) {
  40. if ($v['orders']==0) {
  41. $data['ing_count']+=1;
  42. }elseif ($v['orders']==1) {
  43. $data['no_count']+=1;
  44. }elseif ($v['orders']==2) {
  45. $data['pass_count']+=1;
  46. }elseif ($v['orders']==3) {
  47. $data['wks_count']+=1;
  48. }
  49. }
  50. $data['all_count'] = count($row);//任务总数
  51. $data['grawth'] = $data['all_count']==0 ? '0%' : round($data['pass_count']/$data['all_count']*100,2).'%';//完成率
  52. View::assign('data',$data);
  53. $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->field('id,name,level')->order('path asc')->select()->toArray();
  54. view::assign('org', $org);
  55. return View::fetch();
  56. }
  57. //获取所有任务阶段内的所有交定签单
  58. public function orders(){
  59. $param = Request::only(['page'=>1,'limit'=>10,'type'=>1,'org_id'=>0,'keyword'=>'','start_date'=>'','end_date'=>'']);
  60. $deposit = CustomerVisitLog::changeState('已交定', 'chaos');
  61. $money = CustomerVisitLog::changeState('已签单', 'chaos');
  62. if ($param['org_id']) {
  63. $org = [$param['org_id']];
  64. }else{
  65. $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->column('id');
  66. }
  67. $or = [];
  68. if ($param['keyword']) {
  69. //业务员
  70. $em[] = ['root_id','=',request()->employee->root_id];
  71. $em[] = ['uid','>',0];
  72. $em[] = ['name','like','%'.$param['keyword'].'%'];
  73. $employee_id = Employee::where($em)->column('id');
  74. $or[] = ['employee_id','in',$employee_id];
  75. //客户姓名
  76. $customer_id = Customer::where([['org_id','in',$org],['name','like','%'.$param['keyword'].'%']])->column('id');
  77. $or[] = ['customer_id','in',$customer_id];
  78. //任务名称
  79. $pw[] = ['title','like','%'.$param['keyword'].'%'];
  80. $pw[] = ['root_id','=',request()->employee->root_id];
  81. $pw[] = ['del','=',0];
  82. $pw[] = ['customer_visit_log_id','<>',''];
  83. $pw[] = ['customer_visit_log_id','not null',''];
  84. $vid = PreformanceTasksModel::where($pw)->column('customer_visit_log_id');
  85. $or[] = ['id','in',explode(',',implode(',',$vid))];
  86. }
  87. if ($param['start_date']) $qw[] = ['addtime','>=',$param['start_date'].' 00:00:00'];
  88. if ($param['end_date']) $qw[] = ['addtime','<=',$param['end_date'].' 23:59:59'];
  89. //所有交定签单
  90. $qw[] = ['org_id','in',$org];
  91. $qw[] = ['state','in',array_merge($deposit,$money)];
  92. $qw[] = ['employee_id','>',0];
  93. $list = CustomerVisitLog::where($qw)->where(function($query) use ($or){
  94. $query->whereOr($or);
  95. })->page($param['page'],$param['limit'])->order('id desc')->select();
  96. $like = "%".implode("%,%",array_column($list->toArray(),'id'))."%";
  97. $qw1[] = ['customer_visit_log_id','like',explode(',',$like),'or'];
  98. $qw1[] = ['root_id','=',request()->employee->root_id];
  99. $qw1[] = ['del','=',0];
  100. if($param['keyword']) $qw1[] = ['title','like','%'.$param['keyword'].'%'];
  101. $tasks = PreformanceTasksModel::where($qw1)->select();
  102. $data = [];
  103. foreach ($list as $k => $v) {
  104. $date = date('Y-m-d',strtotime($v->addtime));
  105. $row = [
  106. 'id'=>$v->id,
  107. 'employee_name' => $v->employee->name,
  108. 'org_name' => $v->employee->org->name,
  109. 'org_id' => $v->employee->org->id,
  110. 'type' => (strpos($v->state,'定')!==false || $v->state==2) ? '成功收定' : '成功签单',
  111. 'customer_name' => $v->customer ? ($v->customer->name ?: '未知') : '',
  112. 'deposit_money' => $v->moeny ?: '',
  113. 'signed_money' => $v->money ?: '',
  114. 'show_money' => $v->money ? $v->money.'元' : '0元',
  115. 'date' => $date,
  116. 'time' => date('H:i:s',strtotime($v->addtime)),
  117. 'datetime' => $v->addtime,
  118. 'img' => isset($v->customer->employee->user->headimgurl) ? $v->customer->employee->user->headimgurl : '/static/img/default.png',
  119. 'tasks' => []
  120. ];
  121. $task = [];
  122. //所属任务,时间符合并且属于任务指派部门
  123. foreach ($tasks as $k2 => $v2) {
  124. $vids = explode(',',$v2['customer_visit_log_id']);
  125. if (in_array($v->id,$vids)) {
  126. //任务存在交定和签单
  127. if ($row['type']=='成功收定') {
  128. $task[] = $v2->title.':'.$v2->ok_deposit.'/'.$v2->deposit.'(单)';
  129. }elseif ($row['type']=='成功签单') {
  130. $task[] = $v2->title.':'.$v2->ok_money.'/'.$v2->money.'(元)';
  131. }
  132. }
  133. }
  134. if ($task) {
  135. foreach ($task as $k3 => $v3) {
  136. $row['tasks'] = [$v3];
  137. $data[$date][] = $row;
  138. }
  139. }else{
  140. $data[$date][] = $row;
  141. }
  142. unset($row);
  143. }
  144. $count = CustomerVisitLog::where($qw)->where(function($query) use ($or){
  145. $query->whereOr($or);
  146. })->count();
  147. //最新动态时间
  148. $new_time = CustomerVisitLog::where($qw)->order('id desc')->value('addtime');
  149. return json(['code' => 0, 'data' => $data, 'count' => $count,'time'=>$new_time, 'msg' => '获取成功']);
  150. }
  151. //获取所有任务
  152. public function tasks(){
  153. $param = Request::only(['page'=>1,'limit'=>10,'keyword'=>'']);
  154. $root_id = request()->employee->root_id;
  155. $w[] = ['root_id','=',$root_id];
  156. $w[] = ['del','=',0];
  157. if ($param['keyword']) $w[] = ['title','like','%'.trim($param['keyword']).'%'];
  158. //排序 进行中0,逾期1,已完成2,未开始3
  159. $orders = [0=>'进行中',1=>'已逾期',2=>'已完成',3=>'未开始'];
  160. //是否指派签单任务
  161. $m = 'IF(is_money=1,IF(ok_deposit>=deposit&&ok_money>=money,2,1),IF(ok_deposit>deposit,2,1))';
  162. //已通过
  163. $pass = 'IF(is_deposit=1,'.$m.',IF(ok_money>=money,2,1))';
  164. //进行中
  165. $ing = 'IF(end_date>=now(),0,'.$pass.')';
  166. //未开始
  167. $no = 'IF(start_date>now(),3,'.$ing.') as orders';
  168. $row = PreformanceTasksModel::where($w)->order('orders asc')->page($param['page'],$param['limit'])->field('*,'.$no)->select();
  169. $date = date('Y-m-d H:i:s');
  170. $color = ['未开始'=>'background:#249EFB;','进行中'=>'background:#FABA59;','已完成'=>'background:grey;','已逾期'=>'background:red;'];
  171. foreach ($row as $k => $v) {
  172. $v->start_dates = date('Y-m-d',strtotime($v->start_date));
  173. $v->end_dates = date('Y-m-d',strtotime($v->end_date));
  174. //状态
  175. $v->state = $orders[$v->orders];
  176. $v->color = $color[$v->state];
  177. }
  178. $count = PreformanceTasksModel::where($w)->count();
  179. return json(['code' => 0, 'data' => $row, 'count' => $count,'msg' => '获取成功']);
  180. }
  181. public function add(){
  182. return View::fetch();
  183. }
  184. //数据统计页面
  185. public function statistical(){
  186. $param = Request::only(['id'=>0,'org_id'=>0]);
  187. $data = $this->data_statistics($param['id'],$param['org_id']);
  188. View::assign('data',$data);
  189. View::assign('id',$param['id']);
  190. View::assign('org_id',$param['org_id']);
  191. return View::fetch();
  192. }
  193. //数据统计页面列表
  194. public function statistical_list(){
  195. $param = Request::only(['id'=>0,'org_id'=>0,'page'=>1,'limit'=>10,'keyword'=>'']);
  196. $root_id = request()->employee->root_id;
  197. $w0[] = ['id','=',$param['id']];
  198. $w0[] = ['root_id','=',$root_id];
  199. $w0[] = ['del','=',0];
  200. $w0[] = ['customer_visit_log_id','<>',''];
  201. $w0[] = ['customer_visit_log_id','not null',''];
  202. $info = PreformanceTasksModel::where($w0)->findOrEmpty();
  203. if ($info->isEmpty()) return json(['code' => 0, 'data' =>[], 'count' =>0,'msg' => '获取成功']);
  204. $w[] = ['performance_tasks_id','=',$param['id']];
  205. $w[] = ['root_id','=',$root_id];
  206. $w[] = ['org_id','=',$param['org_id']];
  207. $w[] = ['del','=',0];
  208. $row = PreformanceTasksOrg::where($w)->findOrEmpty();
  209. if ($row->isEmpty()) return json(['code' => 0, 'data' =>[], 'count' =>0,'msg' => '获取成功']);
  210. $vid = $info->customer_visit_log_id;
  211. $eid = CustomerVisitLog::where([['org_id','=',$param['org_id']],['id','in',explode(',',$vid)]])->group('employee_id')->column('employee_id');
  212. $wor[] = ['id','in',$eid];
  213. $wor[] = ['org_id','=',$param['org_id']];
  214. $w1[] = ['root_id','=',$root_id];
  215. $w1[] = ['state','in',['在职','离职']];
  216. $w1[] = ['uid','>',0];
  217. if($param['keyword']) $w1[] = ['name','like','%'.trim($param['keyword']).'%'];
  218. $list = Employee::where($w1)->where(function($query) use ($wor){
  219. $query->whereOr($wor);
  220. })->field('id,name')->page($param['page'],$param['limit'])->select()->toArray();
  221. if($info->is_deposit==1){
  222. //交定数量查询
  223. $w3[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
  224. $w3[] = ['id','in',explode(',',$vid)];
  225. $w3[] = ['org_id','=',$param['org_id']];
  226. $deposit = CustomerVisitLog::where($w3)->field('employee_id,customer_id')->group('employee_id,customer_id')->select()->toArray();
  227. $eid = [];
  228. foreach ($deposit as $k => $v) {
  229. $eid[$v['employee_id']] = isset($eid[$v['employee_id']]) ? $eid[$v['employee_id']]+=1 : 1;
  230. }
  231. }
  232. if($info->is_money==1){
  233. //签单金额查询
  234. $w33[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
  235. $w33[] = ['id','in',explode(',',$vid)];
  236. $w33[] = ['org_id','=',$param['org_id']];
  237. $money = CustomerVisitLog::where($w33)->field('sum(money) num,employee_id')->group('employee_id')->order('num desc')->column('sum(money) num','employee_id');
  238. $eidm = [];
  239. foreach ($money as $k5 => $v5) {
  240. $eidm[$k5] = $v5;
  241. }
  242. }
  243. if($info->is_credit == 1){
  244. $w66[] = ['id', 'in', explode(',', $vid)];
  245. $credit = TrainCredits::where($w66)->field('sum(credits) num,employee_id')->group('employee_id')->order('num desc')->column('sum(credits) num', 'employee_id');
  246. }
  247. foreach ($list as $k6 => $v6) {
  248. $list[$k6]['deposit'] = isset($eid[$v6['id']]) ? $eid[$v6['id']] : 0;
  249. $list[$k6]['money'] = isset($eidm[$v6['id']]) ? $eidm[$v6['id']] : 0;
  250. $list[$k6]['credit'] = isset($credit[$v6['id']]) ? $credit[$v6['id']] : 0;
  251. }
  252. $count = Employee::where($w1)->where(function($query) use ($wor){
  253. $query->whereOr($wor);
  254. })->count();
  255. return json(['code' => 0, 'data' =>$list, 'count' =>$count,'msg' => '获取成功']);
  256. }
  257. //任务详情页面
  258. public function details(){
  259. $id = input('id',0);
  260. View::assign('id',$id);
  261. return View::fetch();
  262. }
  263. //任务详情列表
  264. public function details_list(){
  265. $param = Request::only(['page'=>1,'limit'=>10,'id'=>0]);
  266. $root_id = request()->employee->root_id;
  267. $w[] = ['performance_tasks_id','=',$param['id']];
  268. $w[] = ['root_id','=',$root_id];
  269. $list = PreformanceTasksOrg::with(['org'=>function($query){
  270. $query->visible(['id','name'])->bind(['name']);
  271. }])->where($w)->page($param['page'],$param['limit'])->select();
  272. $count = PreformanceTasksOrg::where($w)->count();
  273. return json(['code' => 0, 'data' => $list, 'count' => $count,'msg' => '获取成功']);
  274. }
  275. //交定排行
  276. public function deposit(){
  277. $param = Request::only(['id'=>0]);
  278. View::assign('id',$param['id']);
  279. $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->field('id,name,level')->order('path asc')->select()->toArray();
  280. view::assign('org', $org);
  281. return View::fetch();
  282. }
  283. //0交定 1签单排行数据
  284. public function deposit_list(){
  285. $param = Request::only(['id'=>0 ,'org_id'=>0,'type'=>0,'page'=>1,'limit'=>10,'keyword'=>'']);
  286. $root_id = request()->employee->root_id;
  287. $w0[] = ['id','=',$param['id']];
  288. $w0[] = ['root_id','=',$root_id];
  289. $w0[] = ['del','=',0];
  290. $w0[] = $param['type']==0 ? ['is_deposit','=',1] : ['is_money','=',1];
  291. $w0[] = ['customer_visit_log_id','<>',''];
  292. $w0[] = ['customer_visit_log_id','not null',''];
  293. $info = PreformanceTasksModel::with(['preformanceTasksOrg'=>function($query){
  294. $query->visible(['org_id','performance_tasks_id,ok_deposit,ok_money'])->where('del',0);
  295. }])->where($w0)->findOrEmpty();
  296. if ($info->isEmpty() || $info->preformanceTasksOrg->isEmpty()) return json(['code' => 0, 'data' =>[], 'count' =>0,'msg' => '获取成功']);
  297. $org_ids = $param['org_id'] ? [$param['org_id']] : array_column($info->preformanceTasksOrg->toArray(),'org_id');
  298. //如果有任务期间有切换部门的情况也要计算排名
  299. $vid = $info->customer_visit_log_id;
  300. $eid = CustomerVisitLog::where([['id','in',explode(',',$vid)],['org_id','in',$org_ids]])->group('employee_id')->column('employee_id');
  301. $wor[] = ['id','in',$eid];
  302. $wor[] = ['org_id','in',$org_ids];
  303. $w1[] = ['root_id','=',$root_id];
  304. $w1[] = ['state','in',['在职','离职']];
  305. if ($param['keyword']) $w1[] = ['name','like','%'.trim($param['keyword']).'%'];
  306. $employee = Employee::where($w1)->where(function($query) use ($wor){
  307. $query->whereOr($wor);
  308. })->field('id,name,org_id')->select()->toArray();
  309. if ($param['type']==0) {
  310. //交定,一个业务员在两个部门交定同一个客户算两个交定业绩sb
  311. $w22[] = ['id','in',explode(',',$vid)];
  312. $w22[] = ['state', 'in',CustomerVisitLog::changeState('已交定', 'chaos')];
  313. $w22[] = ['org_id','in',$org_ids];
  314. // $deposit = CustomerVisitLog::where($w22)->field('employee_id,customer_id')->group('employee_id,customer_id')->select()->toArray();
  315. // $eidcc = [];
  316. // foreach ($deposit as $k => $v) {
  317. // $eidcc[$v['employee_id']] = !isset($eidcc[$v['employee_id']]) ? 1 : $eidcc[$v['employee_id']]+=1;
  318. // }
  319. $eidcc = CustomerVisitLog::where($w22)->group('employee_id')->column('count(id) count','employee_id');
  320. }else{
  321. $w3[] = ['id','in',explode(',',$vid)];
  322. $w3[] = ['state', 'in',CustomerVisitLog::changeState('已签单', 'chaos')];
  323. $w3[] = ['org_id','in',$org_ids];
  324. // $money = CustomerVisitLog::where($w3)->field('sum(money) num,employee_id')->group('employee_id')->order('num desc')->column('sum(money) num','employee_id');
  325. // $eidcc = [];
  326. // foreach ($money as $kk => $vv) {
  327. // $eidcc[$kk] = $vv;
  328. // }
  329. $eidcc = CustomerVisitLog::where($w3)->field('sum(money) num,employee_id')->group('employee_id')->order('num desc')->column('sum(money) num','employee_id');
  330. }
  331. foreach ($employee as $k2 => $v2) {
  332. $employee[$k2]['value'] = isset($eidcc[$v2['id']]) ? $eidcc[$v2['id']] : 0;
  333. }
  334. $count = count($employee);
  335. array_multisort(array_column($employee,'value'), SORT_DESC, $employee);
  336. foreach ($employee as $k6 => $v6) {
  337. $employee[$k6]['ranking'] = $k6+1;
  338. }
  339. $list = array_slice($employee,($param['page']-1)*$param['limit'],$param['limit']);
  340. $orgs = Org::where([['id','in',array_column($list,'org_id')]])->column('name','id');
  341. foreach ($list as $k7 => $v7) {
  342. $list[$k7]['org_name'] = $orgs[$v7['org_id']];
  343. }
  344. return json(['code' => 0, 'data'=>$list,'count'=>$count,'msg' => '获取成功']);
  345. }
  346. //签单排行
  347. public function money(){
  348. $param = Request::only(['id'=>0]);
  349. View::assign('id',$param['id']);
  350. $org = Org::where([['path', 'like', request()->employee->root_id . '-%']])->field('id,name,level')->order('path asc')->select()->toArray();
  351. view::assign('org', $org);
  352. return View::fetch();
  353. }
  354. //保存任务
  355. public function add_data(){
  356. $param = Request::only(['title'=>'','start_date'=>'','end_date'=>'','deposit'=>0,'money'=>0,'is_deposit'=>0,'is_money'=>0,'','time'=>'','credit'=>0,'is_credit'=>0]);
  357. $time = explode(' - ',$param['time']);
  358. $param['start_date'] = $time[0].' '.date('H:i:s');
  359. $param['end_date'] = $time[1].' 23:59:59';
  360. unset($param['time']);
  361. $param['root_id'] = request()->employee->root_id;
  362. PreformanceTasksModel::insertGetId($param);
  363. return json(['code' => 0, 'data' =>'保存成功','msg' => '保存成功']);
  364. }
  365. //编辑页面
  366. public function edit(){
  367. $param = Request::only(['id'=>0]);
  368. View::assign('id',$param['id']);
  369. $root_id = request()->employee->root_id;
  370. $w[] = ['id','=',$param['id']];
  371. $w[] = ['root_id','=',$root_id];
  372. $row = PreformanceTasksModel::where($w)->findOrEmpty();
  373. if($row->isEmpty()) return json(['code' => 1, 'data' =>'数据不存在','msg' => '数据不存在']);
  374. $row->time = date('Y-m-d',strtotime($row->start_date)).' - '.date('Y-m-d',strtotime($row->end_date));
  375. View::assign('data',$row);
  376. return View::fetch();
  377. }
  378. //编辑任务
  379. public function edit_data(){
  380. $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]);
  381. if($param['start_date']<$param['end_date']) return json(['code' => 1, 'data' =>'开始时间要小于结束时间','msg' => '开始时间要小于结束时间']);
  382. $time = explode(' - ',$param['time']);
  383. $param['start_date'] = $time[0].' '.date('H:i:s');
  384. $param['end_date'] = $time[1].' 23:59:59';
  385. unset($param['time']);
  386. $root_id = request()->employee->root_id;
  387. $w[] = ['id','=',$param['id']];
  388. $w[] = ['root_id','=',$root_id];
  389. $row = PreformanceTasksModel::where($w)->findOrEmpty();
  390. if($row->isEmpty()) return json(['code' => 1, 'data' =>'数据不存在','msg' => '数据不存在']);
  391. $date = date('Y-m-d H:i:s');
  392. if($row->end_date < $date) return json(['code' => 1, 'data' =>'任务已结束无法编辑','msg' => '任务已结束无法编辑']);
  393. //查询已经分配的任务
  394. $w1[] = ['root_id','=',$root_id];
  395. $w1[] = ['performance_tasks_id','=',$row->id];
  396. $w1[] = ['del','=',0];
  397. $deposit = PreformanceTasksOrg::where($w1)->sum('deposit');
  398. if ($deposit>0 && !$param['is_deposit']) {
  399. return json(['code' => 1, 'data' =>'已经指派交定任务无法取消','msg' => '已经指派交定任务无法取消']);
  400. }
  401. if ($deposit>0 && $param['deposit'] < $deposit) {
  402. return json(['code' => 1, 'data' =>'交定目标不能小于已经指派的数量','msg' => '交定目标不能小于已经指派的数量']);
  403. }
  404. $money = PreformanceTasksOrg::where($w1)->sum('money');
  405. if ($money>0 && !$param['is_money']) {
  406. return json(['code' => 1, 'data' =>'已经指派签单任务无法取消','msg' => '已经指派签单任务无法取消']);
  407. }
  408. if ($money>0 && $param['money'] < $money) {
  409. return json(['code' => 1, 'data' =>'签单金额不能小于已经指派的金额','msg' => '签单金额不能小于已经指派的金额']);
  410. }
  411. unset($param['id']);
  412. $row->save($param);
  413. return json(['code' => 0, 'data' =>'编辑成功','msg' => '编辑成功']);
  414. }
  415. //指派页面
  416. public function assign(){
  417. $id = input('id',0);
  418. View::assign('id',$id);
  419. View::assign('type','');
  420. $root_id = request()->employee->root_id;
  421. $w[] = ['performance_tasks_id','=',$id];
  422. $w[] = ['root_id','=',$root_id];
  423. $w[] = ['del','=',0];
  424. $row = preformanceTasksOrg::with(['org'=>function($query){
  425. $query->visible(['id,name'])->bind(['name']);
  426. }])->where($w)->field(['id','root_id','performance_tasks_id','org_id','deposit','money','credit'])->select()->toArray();
  427. $w1[] = ['org_id','in',array_column($row,'org_id')];
  428. $w1[] = ['state','=','在职'];
  429. $w1[] = ['root_id','=',$root_id];
  430. $ec = Employee::where($w1)->group('org_id')->column('count(id) count','org_id');
  431. $sum['deposit'] = $sum['money'] = $sum['credit'] = 0;
  432. foreach ($row as $k => $v) {
  433. unset($v->org);
  434. $row[$k]['count'] = isset($ec[$v['org_id']]) ? $ec[$v['org_id']] : 0;
  435. $sum['deposit'] += $v['deposit'];
  436. $sum['money'] += $v['money'];
  437. $sum['credit'] += $v['credit'];
  438. }
  439. $count = count($row);
  440. View::assign('count',$count);
  441. View::assign('data',$row);
  442. $w11[] = ['id','=',$id];
  443. $w11[] = ['root_id','=',$root_id];
  444. $w11[] = ['del','=',0];
  445. $info = PreformanceTasksModel::where($w11)->find();
  446. $sum['deposit'] = $info->deposit-$sum['deposit'];
  447. $sum['money'] = $info->money-$sum['money'];
  448. $sum['credit'] = $info->credit-$sum['credit'];
  449. View::assign('sum',$sum);
  450. return View::fetch();
  451. }
  452. //指派
  453. public function assign_org(){
  454. $param = Request::only(['id'=>0,'title'=>'','start_date'=>'','end_date'=>'','deposit'=>0,'money'=>0,'is_deposit'=>0,'is_money'=>0,'']);
  455. if($param['start_date']<$param['end_date']) return json(['code' => 1, 'data' =>'开始时间要小于结束时间','msg' => '开始时间要小于结束时间']);
  456. $root_id = request()->employee->root_id;
  457. $w[] = ['id','=',$param['id']];
  458. $w[] = ['root_id','=',$root_id];
  459. $row = PreformanceTasksModel::where($w)->findOrEmpty();
  460. if($row->isEmpty()) return json(['code' => 1, 'data' =>'数据不存在','msg' => '数据不存在']);
  461. unset($param['id']);
  462. $row->save($param);
  463. return json(['code' => 0, 'data' =>'编辑成功','msg' => '编辑成功']);
  464. }
  465. /*
  466. * 指派组织
  467. */
  468. public function get_person()
  469. {
  470. $id = input('id',0);//任务id
  471. $root_id = request()->employee->root_id;
  472. $keyword = input('keyword','');
  473. $where = [
  474. ['path', 'like', $root_id . '-%'],
  475. ['status', '=', 1]
  476. ];
  477. if ($keyword) {
  478. // $where[] = ['name','like','%'.trim($keyword).'%'];
  479. }
  480. $count = Employee::where([['root_id','=',$root_id],['state','like','%在职%'],['uid','>',0]])->group('org_id')->column('count(*) count','org_id');
  481. $info = PreformanceTasksOrg::where(['root_id'=>request()->employee->root_id , 'performance_tasks_id'=>$id,'del'=>0])->column('org_id');
  482. $allnodes = Org::where($where)->field('id,pid,name title,level,org_type,info,path')->order('level asc, id asc')->select()->toarray();
  483. foreach ($allnodes as $k => $v) {
  484. $allnodes[$k]['count'] = isset($count[$v['id']]) ? $count[$v['id']] : 0;//本部门人数
  485. $allnodes[$k]['all_count'] = 0;//包含子部门总数
  486. $allnodes[$k]['org_class'] = 'org_'.$v['id'];
  487. $allnodes[$k]['per_class'] = 'per_org_'.$v['id'];
  488. $allnodes[$k]['type'] = 'org';
  489. foreach ($allnodes as $k2 => $v2) {
  490. if (strpos($v2['path'],$v['path']) !== false) {
  491. $allnodes[$k]['all_count'] = isset($count[$v2['id']]) ? $count[$v2['id']]+$allnodes[$k]['all_count'] : $allnodes[$k]['all_count'];
  492. }
  493. }
  494. $allnodes[$k]['y_title'] = $allnodes[$k]['title'];
  495. $allnodes[$k]['title'] = $allnodes[$k]['title'].' ('.$allnodes[$k]['count'].'人)';
  496. $allnodes[$k]['selected'] = in_array($v['id'],$info);
  497. }
  498. $tree = $this->tree($allnodes,0);
  499. return json(['code' => 0, 'data' => $tree, 'checkOrg' =>'']);
  500. }
  501. public function tree($data,$pid = 0)
  502. {
  503. $new_arr = [];
  504. foreach($data as $k => $v){
  505. if($v['pid'] == $pid) {
  506. $children = $this->tree($data, $v['id']);
  507. $v['children'] = $children;
  508. if (empty($v['children'])) $v['disabled']=true;
  509. $new_arr[] =$v;
  510. }
  511. }
  512. return $new_arr;
  513. }
  514. //删除任务
  515. public function del_tasks()
  516. {
  517. $id = input('id',0);
  518. $root_id = request()->employee->root_id;
  519. $w[] = ['id','=',$id];
  520. $w[] = ['root_id','=',$root_id];
  521. $row = PreformanceTasksModel::where($w)->findOrEmpty();
  522. if($row->isEmpty()) return json(['code' => 1, 'data' =>'数据不存在','msg' => '数据不存在']);
  523. $row->del = 1;
  524. $row->save();
  525. return json(['code' => 0, 'data' =>'删除成功','msg' => '删除成功']);
  526. }
  527. //获取部门子部门数量
  528. public function get_cid(){
  529. $id = input('id',0);
  530. $root_id = request()->employee->root_id;
  531. $w[] = ['id','=',$id];
  532. $w[] = ['path','like',$root_id.'-%'];
  533. $row = Org::where($w)->findOrEmpty();
  534. if ($row->isEmpty()) return json(['code' => 0, 'data' =>[],'msg' =>'']);
  535. $w1[] = ['path','like',$row->path.'%'];
  536. $w1[] = ['id','=',$id];
  537. $list = Org::withCount(['employee'=>function($query){
  538. $query->where([['state','=','在职'],['uid','>',0]]);
  539. }])->where($w1)->field(['id','name'])->select();
  540. return json(['code' => 0, 'data' =>$list,'msg' =>'']);
  541. }
  542. //数据统计api,,,任务id,任务指派的部门id
  543. public function data_statistics($id,$org_id=0){
  544. //实际交定,实际签单,平均交定,平均签单
  545. //交定最多人,签单最多人,签单最低人,交定最低人
  546. $data = [
  547. 'ok_deposit' => 0,
  548. 'ok_money' => 0,
  549. 'avg_deposit' => 0,
  550. 'avg_money' => 0,
  551. 'deposit_top' => '',
  552. 'money_top' => '',
  553. 'deposit_down' => '',
  554. 'money_down' => '',
  555. 'ok_credit' =>0,
  556. 'avg_credit' =>0
  557. ];
  558. //数据不存在
  559. $root_id = request()->employee->root_id;
  560. $w[] = ['id','=',$id];
  561. $w[] = ['root_id','=',$root_id];
  562. $w[] = ['del','=',0];
  563. $w[] = ['customer_visit_log_id','<>',''];
  564. $w[] = ['customer_visit_log_id','not null',''];
  565. $row = PreformanceTasksModel::where($w)->findOrEmpty();
  566. if ($row->isEmpty()) return $data;
  567. $w1[] = ['root_id','=',$root_id];
  568. $w1[] = ['performance_tasks_id','=',$id];
  569. $w1[] = ['del','=',0];
  570. $w1[] = ['org_id','=',$org_id];
  571. $org = PreformanceTasksOrg::where($w1)->findOrEmpty();
  572. if ($org->isEmpty()) return $data;
  573. //实际交定,实际签单
  574. $data['ok_deposit'] = $org->ok_deposit;//数量
  575. $data['ok_money'] = $org->ok_money;
  576. $data['ok_credit'] = $org->ok_credit;
  577. $vid = $row->customer_visit_log_id;
  578. //部门员工人数
  579. //如果有任务期间有切换部门的情况也要计算排名
  580. $eid = CustomerVisitLog::where([['org_id','=',$org_id],['id','in',explode(',',$vid)]])->group('employee_id')->column('employee_id');
  581. $wor[] = ['id','in',$eid];
  582. $wor[] = ['org_id','=',$org_id];
  583. $w2[] = ['root_id','=',$root_id];
  584. $w2[] = ['state','in',['在职','离职']];
  585. $eid = Employee::where($w2)->where(function($query) use ($wor){
  586. $query->whereOr($wor);
  587. })->column('name','id');
  588. $eids = array_keys($eid);
  589. //平均交定,平均签单
  590. $data['avg_deposit'] = count($eids) ? round($org->ok_deposit/count($eids),1) : 0;
  591. $data['avg_money'] = count($eids) ? round($org->ok_money/count($eids),1) : 0;
  592. $data['avg_credit'] = count($eids) ? round($org->ok_credit/count($eids),1) : 0;
  593. if($row->is_deposit==1){
  594. //交定
  595. $w22[] = ['id','in',explode(',',$vid)];
  596. $w22[] = ['state', 'in',CustomerVisitLog::changeState('已交定', 'chaos')];
  597. $w22[] = ['org_id','=',$org_id];
  598. //一个客户可以不同的人交定多次
  599. $deposit = CustomerVisitLog::where($w22)->field('employee_id,customer_id')->group('employee_id,customer_id')->select()->toArray();
  600. $eidcc = [];
  601. foreach ($deposit as $k => $v) {
  602. $eidcc[$v['employee_id']] = !isset($eidcc[$v['employee_id']]) ? 1 : $eidcc[$v['employee_id']]+=1;
  603. }
  604. $eidc = [];
  605. foreach ($eids as $v2) {
  606. $eidc[] = [
  607. 'id'=>$v2,
  608. 'count'=>isset($eidcc[$v2]) ? $eidcc[$v2] : 0
  609. ];
  610. }
  611. array_multisort(array_column($eidc,'count'), SORT_DESC, $eidc);
  612. $top = isset($eidc[0]['count']) ? $eidc[0]['count'] : 0;
  613. $down = isset(end($eidc)['count']) ? end($eidc)['count'] : 0;
  614. foreach ($eidc as $k3 => $v3) {
  615. if ($top==$v3['count']) $data['deposit_top'] .= $eid[$v3['id']].',';//所有第一名
  616. if ($down==$v3['count']) $data['deposit_down'] .= $eid[$v3['id']].',';//所有最后一名
  617. }
  618. }
  619. if($row->is_money==1){
  620. $w3[] = ['id','in',explode(',',$vid)];
  621. $w3[] = ['state', 'in',CustomerVisitLog::changeState('已签单', 'chaos')];
  622. $w3[] = ['org_id','=',$org_id];
  623. $money = CustomerVisitLog::where($w3)->field('sum(money) num,employee_id')->group('employee_id')->order('num desc')->column('sum(money) num','employee_id');
  624. $eidm = [];
  625. foreach ($eids as $v5) {
  626. $eidm[] = [
  627. 'id'=>$v5,
  628. 'num' => isset($money[$v5]) ? (int)$money[$v5] : 0
  629. ];
  630. }
  631. array_multisort(array_column($eidm,'num'), SORT_DESC, $eidm);
  632. $top = isset($eidm[0]['num']) ? (int)$eidm[0]['num'] : 0;
  633. $down = isset(end($eidm)['num']) ? (int)end($eidm)['num'] : 0;
  634. foreach ($eidm as $k4 => $v4) {
  635. if ($top==$v4['num']) $data['money_top'] .= $eid[$v4['id']].',';//所有第一名
  636. if ($down==$v4['num']) $data['money_down'] .= $eid[$v4['id']].',';//所有最后一名
  637. }
  638. }
  639. return $data;
  640. }
  641. //保存指派
  642. public function save()
  643. {
  644. $param = input('arr','');
  645. if(!is_array($param)) return json(['code' => 1, 'data' =>'请选择部门','msg' =>'请选择部门']);
  646. $id = input('id',0);
  647. //数据不存在
  648. $root_id = request()->employee->root_id;
  649. $w[] = ['id','=',$id];
  650. $w[] = ['root_id','=',$root_id];
  651. $w[] = ['del','=',0];
  652. $row = PreformanceTasksModel::where($w)->findOrEmpty();
  653. if ($row->isEmpty()) return json(['code' => 1, 'data' =>'任务不存在','msg' =>'任务不存在']);
  654. $deposit = array_sum(array_column($param,'deposit'));
  655. if ($row->deposit<$deposit) return json(['code' => 1, 'data' =>'交定总数不能大于任务设置总数','msg' =>'交定总数不能大于任务设置总数']);
  656. $money = array_sum(array_column($param,'money'));
  657. if ($row->money<$money) return json(['code' => 1, 'data' =>'签单金额不能大于任务设置金额','msg' =>'签单金额不能大于任务设置金额']);
  658. $dw[] = ['root_id','=',$root_id];
  659. $dw[] = ['performance_tasks_id','=',$id];
  660. PreformanceTasksOrg::where($dw)->delete();
  661. foreach ($param as $k => $v) {
  662. if (!$v['deposit'] && !$v['money'] && !$v['credit']) {
  663. continue;
  664. }
  665. $w1 = [];
  666. $w1[] = ['performance_tasks_id','=',$id];
  667. $w1[] = ['root_id','=',$root_id];
  668. $w1[] = ['del','=',0];
  669. $w1[] = ['org_id','=',$v['org_id']];
  670. $check = PreformanceTasksOrg::where($w1)->findOrEmpty();
  671. if ($check->isEmpty()) {
  672. $save = [
  673. 'root_id' => $root_id,
  674. 'performance_tasks_id' => $id,
  675. 'del' => 0,
  676. 'deposit' => $v['deposit'],
  677. 'money' => $v['money'],
  678. 'org_id' => $v['org_id'],
  679. 'is_deposit' => $row->is_deposit,
  680. 'is_money' => $row->is_money,
  681. 'credit' =>$v['credit'],
  682. 'is_credit' => $row->is_credit
  683. ];
  684. $pid = PreformanceTasksOrg::insertGetId($save);
  685. unset($save);
  686. }else{
  687. $check->deposit = $v['deposit'];
  688. $check->money = $v['money'];
  689. $check->credit = $v['credit'];
  690. $check->is_deposit = $row->is_deposit;
  691. $check->is_money = $row->is_money;
  692. $check->is_credit = $row->is_credit;
  693. $check->save();
  694. }
  695. unset($w1);
  696. }
  697. return json(['code' => 0, 'data' =>'指派成功','msg' =>'指派成功']);
  698. }
  699. }