CompanyStrength.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. <?php
  2. namespace app\sys\controller;
  3. use think\facade\Db;
  4. use think\facade\Request;
  5. use think\facade\View;
  6. use app\model\Employee;
  7. use app\model\Org;
  8. use app\model\CompanyStrength as CompanyStrengthModel;
  9. use app\model\CompanyStrengthCate;
  10. use app\model\Company;
  11. use app\model\ShareLog;
  12. use app\model\Footprints;
  13. use app\model\CustomerClue;
  14. use app\logics\MaterialLogic;
  15. class CompanyStrength
  16. {
  17. /**
  18. * 首页
  19. */
  20. public function index()
  21. {
  22. $root_id = request()->employee->root_id;
  23. $w[] = ['root_id','=',$root_id];
  24. $cate = CompanyStrengthCate::where($w)->where('pid','=',0)->order('id asc')->select();
  25. View::assign('cate',$cate);
  26. $eid = CompanyStrengthModel::where([['del','=',0],['root_id','=',request()->employee->root_id],['employee_id','>',0]])->group('employee_id')->column('employee_id');
  27. $w[] = ['id','in',$eid];
  28. $w[] = ['grant_id', '>', 0];
  29. $w[] = ['root_id', '=', request()->employee->root_id];
  30. // 运营人员列表
  31. $employee = Employee::where($w)->field(['id', 'opt_name as name'])->select()->toArray();
  32. View::assign('employee', $employee);
  33. return View::fetch();
  34. }
  35. /**
  36. * 公司实力列表
  37. */
  38. public function list()
  39. {
  40. $root_id = request()->employee->root_id;
  41. $param = Request::only(['page'=>1,'limit'=>10,'cate'=>'','title'=>'','employee_id'=>'','add_time','order']);
  42. $w[] = ['root_id','=',$root_id];
  43. $w[] = ['del','=',0];
  44. if ($param['cate']) {
  45. $w[] = ['cate','=',$param['cate']];
  46. }
  47. if ($param['title']) {
  48. $w[] = ['title','like','%'.$param['title'].'%'];
  49. }
  50. if (!empty($param['employee_id'])) {
  51. $w[] = ['employee_id', '=', $param['employee_id']];
  52. }
  53. //上传时间筛选
  54. if (!empty($param['add_time'])) {
  55. $newtime=explode(' - ', $param['add_time']);
  56. $start_time=$newtime[0].' 00:00:00';
  57. $end_time=$newtime[1].' 23:59:59';
  58. $w[] = ['addtime', 'between', [$start_time, $end_time]];
  59. }
  60. $order='id desc';
  61. if (!empty($param['order']) && in_array($param['order'], ['visit_due_time desc','visit_due_time asc','view_times asc','view_times desc','shared_times asc','shared_times desc'])) {
  62. $order = $param['order'];
  63. }
  64. $list = CompanyStrengthModel::with(['employee'=>function($query){
  65. $query->visible(['id','name','opt_name']);
  66. }])->where($w)->order($order)->page($param['page'],$param['limit'])->select()->toArray();
  67. $count = CompanyStrengthModel::where($w)->count();
  68. //增加浏览时长转发次数
  69. // if(!empty($list)){
  70. // $statismod=new MaterialLogic;
  71. // $selorder=!empty($param['order'])?$param['order']:'';
  72. // $list=$statismod->sel_case_share($list,'CompanyStrength',$selorder);
  73. // }
  74. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  75. }
  76. /**
  77. * 上下架
  78. */
  79. public function apipublish()
  80. {
  81. $param = Request::Only(['id'=>0]);
  82. $condition = ['id' => $param['id'], 'root_id' => request()->employee->root_id];
  83. $obj = CompanyStrengthModel::where($condition)->findOrEmpty();
  84. if ($obj->isEmpty()) return json(['code' => 1, 'msg' => '数据不存在']);
  85. $obj->publish = $obj->publish == 1 ? 0 : 1;
  86. $obj->save();
  87. $msg = $obj->publish ? '上架成功' : '下架成功';
  88. return json(['code' => 0, 'msg' => $msg]);
  89. }
  90. /**
  91. * 添加页面
  92. */
  93. public function add()
  94. {
  95. $root_id = request()->employee->root_id;
  96. $cate = CompanyStrengthCate::where(['pid' => 0, 'root_id' => $root_id])->column('*', 'id');
  97. $label = CompanyStrengthCate::where([['pid', '>', 0], ['root_id', '=', $root_id]])->select()->toArray();
  98. $joinlabel = [];
  99. foreach ($label as $val) {
  100. $joinlabel[$val['pid']][$val['id']] = $val['name'];
  101. }
  102. View::assign('cate',$cate);
  103. View::assign('label', json_encode($joinlabel));
  104. $adding = input('adding', 0);
  105. view::assign('adding', $adding);
  106. return View::fetch();
  107. }
  108. /*
  109. * 客户见证添加
  110. */
  111. public function adding()
  112. {
  113. $data = Request::only(['title', 'desc','label', 'cate', 'pics','difference','cover'=>'','video_type', 'cover_share_img']);
  114. //视频
  115. if ($data['difference']==1) {
  116. $data['pics'] = input('video_url','');
  117. //图片
  118. }else{
  119. $data['cover'] = $data['pics'] = input('covers','');
  120. }
  121. $cateType = CompanyStrengthCate::where(['root_id'=>request()->employee->root_id,'id'=>$data['cate']])->find();
  122. if(empty($cateType)) return json(['code' => 1, 'msg' => '分类数据不存在']);
  123. $data['cate'] = $cateType['name'];
  124. $data['root_id'] = request()->employee->root_id;
  125. $data['employee_id'] = request()->employee->id;
  126. $data['desc'] = isset($data['desc']) && $data['desc'] ? str_replace("\r\n", '', $data['desc']) : '';
  127. if (CompanyStrengthModel::create($data)) {
  128. return json(['code' => 0, 'msg' => '保存成功']);
  129. } else {
  130. return json(['code' => 1, 'msg' => '保存失败']);
  131. }
  132. }
  133. /**
  134. * 删除
  135. */
  136. public function del()
  137. {
  138. $param = Request::Only(['id'=>0]);
  139. $condition = ['id' => $param['id'], 'root_id' => request()->employee->root_id];
  140. $obj = CompanyStrengthModel::where($condition)->findOrEmpty();
  141. if ($obj->isEmpty()) return json(['code' => 1, 'msg' => '数据不存在']);
  142. $obj->del = 1;
  143. $obj->save();
  144. return json(['code' => 0, 'msg' => '删除成功']);
  145. }
  146. /*
  147. * 富文本图片
  148. */
  149. public function imgUpload()
  150. {
  151. $bindUrl = config('app.ali_oss_bindurl');
  152. $data = [
  153. 'src' => 'https://' . $bindUrl . '/' . Request::param('file'),
  154. 'title' => ''
  155. ];
  156. return json(['code' => 0, 'msg' => '', 'data' => $data]);
  157. }
  158. /*
  159. * 公司实力编辑
  160. */
  161. public function edit()
  162. {
  163. $id = input('id');
  164. $root_id = request()->employee->root_id;
  165. $evidence = CompanyStrengthModel::where(['id' => $id, 'root_id' => request()->employee->root_id])->find()->toArray();
  166. $cateType = CompanyStrengthCate::where(['root_id'=>request()->employee->root_id,'name'=>$evidence['cate']])->find();
  167. $evidence['cate'] = $cateType['id'];
  168. View::assign('evidence', $evidence);
  169. $ali_oss_bindurl = config('app.ali_oss_bindurl');
  170. View::assign('ali_oss_bindurl', $ali_oss_bindurl);
  171. //公司实力分类获取
  172. // $cates = CompanyStrengthCate::where([['root_id', '=', request()->employee->root_id],['pid','=',0]])->order('id asc')->select()->toArray();
  173. // View::assign('cates', $cates);
  174. $cate = CompanyStrengthCate::where(['pid' => 0, 'root_id' => $root_id])->column('*', 'id');
  175. $label = CompanyStrengthCate::where([['pid', '>', 0], ['root_id', '=', $root_id]])->select()->toArray();
  176. $joinlabel = [];
  177. foreach ($label as $val) {
  178. $joinlabel[$val['pid']][$val['id']] = $val['name'];
  179. }
  180. View::assign('cates',$cate);
  181. View::assign('checked', $joinlabel[$evidence['cate']] ?? []);
  182. View::assign('label', json_encode($joinlabel));
  183. return View::fetch();
  184. }
  185. /*
  186. * 编辑保存
  187. */
  188. public function editing()
  189. {
  190. $data = Request::only(['id','title','cate','label','evidence_image_exist','evidence_image','desc'=>'','difference','cover','video_type','cover_share_img']);
  191. $data['cover'] = !isset($data['cover']) ? input('covers','') : $data['cover'];
  192. //视频
  193. if ($data['difference']==1) {
  194. $url = input('video_url','');
  195. $arr = explode(',',$url);
  196. $data['pics'] = end($arr);
  197. $data['cover'] = input('ycover','') ?: input('cover','');
  198. }else{
  199. $data['cover'] = $data['pics'] = input('ycovers','') ?: input('covers','');
  200. }
  201. $cateType = CompanyStrengthCate::where(['root_id'=>request()->employee->root_id,'id'=>$data['cate']])->find();
  202. if(empty($cateType)) return json(['code' => 1, 'msg' => '分类数据不存在']);
  203. $data['cate'] = $cateType['name'];
  204. unset($data['evidence_image_exist']);
  205. unset($data['evidence_image']);
  206. $data['desc'] = isset($data['desc']) && $data['desc'] ? str_replace("\r\n", '', $data['desc']) : '';
  207. $evidence = CompanyStrengthModel::where(['id' => $data['id'], 'root_id' => request()->employee->root_id])->find();
  208. $evidence->save($data);
  209. return json(['code' => 0, 'msg' => '保存成功']);
  210. }
  211. /*
  212. * 公司实力分类管理
  213. */
  214. function cate()
  215. {
  216. return View::fetch();
  217. }
  218. /*
  219. * 公司实力分类列表
  220. */
  221. function cate_list()
  222. {
  223. $param = Request::only(['page'=>1,'limit'=>10]);
  224. $root_id = request()->employee->root_id;
  225. $where[] = ['root_id','=',$root_id];
  226. $where[] = ['pid','=',0];
  227. $list = CompanyStrengthCate::where($where)->page($param['page'],$param['limit'])->order(['show'=>'asc','id'=>'desc'])->select();
  228. $count = CompanyStrengthCate::where($where)->count();
  229. return json(['code' => 0, 'data' =>$list,'count'=>$count]);
  230. }
  231. /*
  232. * 添加分类
  233. */
  234. function cate_add()
  235. {
  236. $param = Request::only(['name'=>'']);
  237. $condition[] = ['name', '=', $param['name']];
  238. $condition[] = ['root_id', '=', request()->employee->root_id];
  239. $condition[] = ['pid', '=', 0];
  240. $info = CompanyStrengthCate::where($condition)->findOrEmpty();
  241. if (!$info->isEmpty()) {
  242. return json(['code' => 1, 'msg' => '名称已存在']);
  243. }
  244. $save = [
  245. 'root_id' => request()->employee->root_id,
  246. 'name' => trim($param['name']),
  247. 'pid' => 0
  248. ];
  249. CompanyStrengthCate::insertGetId($save);
  250. return json(['code' => 0, 'msg' => '保存成功', 'data' => '保存成功']);
  251. }
  252. /*
  253. * 编辑分类
  254. */
  255. function cate_edit()
  256. {
  257. $param = Request::only(['name'=>'','id'=>0]);
  258. $condition[] = ['id', '=', $param['id']];
  259. $condition[] = ['root_id', '=', request()->employee->root_id];
  260. $condition[] = ['pid', '=', 0];
  261. $info = CompanyStrengthCate::where($condition)->findOrEmpty();
  262. if ($info->isEmpty()) {
  263. return json(['code' => 1, 'msg' => '数据不存在']);
  264. }
  265. $w[] = [
  266. ['id','<>',$param['id']],
  267. ['name','=',$param['name']],
  268. ['root_id', '=', request()->employee->root_id],
  269. ['pid','=', 0],
  270. ];
  271. $find = CompanyStrengthCate::where($w)->findOrEmpty();
  272. if (!$find->isEmpty()) {
  273. return json(['code' => 1, 'msg' => '分类已存在']);
  274. }
  275. CompanyStrengthCate::where($condition)->update(['name'=>$param['name']]);
  276. return json(['code' => 0, 'msg' => '保存成功', 'data' => '保存成功']);
  277. }
  278. /*
  279. * 分类删除前关联的内容数量
  280. */
  281. public function with_cate_count()
  282. {
  283. $cate = Request::param('cate');
  284. $where = [
  285. ['root_id','=',request()->employee->root_id],
  286. ['del','=',0],
  287. ['cate','=',$cate]
  288. ];
  289. $count = CompanyStrengthModel::where($where)->count();
  290. return $count > 0 ? '该分类有'.$count.'个公司实力,删除后该内容将归为其它分类。' : '确定删除该分类吗?';
  291. }
  292. /*
  293. * 分类删除
  294. */
  295. public function delete_cate()
  296. {
  297. $cate = Request::param('cate');
  298. $cateType = CompanyStrengthCate::where(['root_id'=>request()->employee->root_id,'name'=>$cate])->find();
  299. if(empty($cateType)) return json(['code' => 1, 'msg' => '数据不存在']);
  300. $other_id = CompanyStrengthCate::where(['root_id'=>request()->employee->root_id , 'show'=>1])->find();
  301. if(empty($other_id)) CompanyStrengthCate::insert(['name'=>'其它','root_id'=>request()->employee->root_id,'show'=>1]);
  302. CompanyStrengthCate::where(['root_id'=>request()->employee->root_id , 'name'=>$cate,'pid' => 0])->delete();
  303. CompanyStrengthCate::where(['root_id'=>request()->employee->root_id , 'pid' => $cateType['id']])->delete();
  304. $where = [
  305. ['root_id','=',request()->employee->root_id],
  306. ['del','=',0],
  307. ['cate','=',$cate]
  308. ];
  309. CompanyStrengthModel::where($where)->update(['cate'=>'其它','label'=>0]);
  310. return json(['code' => 0, 'msg' => '删除成功']);
  311. }
  312. /*
  313. * 标签管理
  314. */
  315. function cate_labels()
  316. {
  317. return View::fetch();
  318. }
  319. /*
  320. * 新标签管理列表
  321. */
  322. function cate_list_labels()
  323. {
  324. $param = Request::param();
  325. $condition[] = ['root_id', '=', request()->employee->root_id];
  326. $condition[] = ['pid', '>', 0];
  327. $parents = CompanyStrengthCate::where([['pid', '=', 0]])->column('name', 'id');
  328. $list = CompanyStrengthCate::where($condition)->page($param['page'], $param['limit'])->order(['show'=>'asc','id'=>'desc'])->field('id,name,pid,show')->select()->toarray();
  329. foreach ($list as $k => $v) {
  330. $list[$k]['pname'] = isset($parents[$v['pid']]) ? $parents[$v['pid']] : '';
  331. }
  332. $count = CompanyStrengthCate::where($condition)->count();
  333. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  334. }
  335. /*
  336. * 新标签添加和编辑
  337. */
  338. function cate_labels_add()
  339. {
  340. if (request()->isAjax()) {
  341. $param = Request::param();
  342. if (!$param['id'] || !$param['name']) {
  343. return json(['code' => 1, 'msg' => '缺少参数', 'data']);
  344. }
  345. isset($param['label_id']) ? $condition[] = ['id', '<>', $param['label_id']] : '';
  346. $condition[] = ['name', '=', $param['name']];
  347. $condition[] = ['root_id', '=', request()->employee->root_id];
  348. $condition[] = ['pid', '=', $param['id']];
  349. $info = CompanyStrengthCate::where($condition)->find();
  350. if ($info) {
  351. return json(['code' => 1, 'msg' => '名称已存在']);
  352. }
  353. if(isset($param['label_id'])){
  354. CompanyStrengthCate::where(['root_id'=>request()->employee->root_id,'id'=>$param['label_id']])->update(['pid'=>$param['id'],'name'=>$param['name']]);
  355. }else{
  356. $model = new CompanyStrengthCate;
  357. $model->name = $param['name'];
  358. $model->pid = $param['id'];
  359. $model->root_id = request()->employee->root_id;
  360. $model->save();
  361. }
  362. return json(['code' => 0, 'msg' => '保存成功', 'data' => '']);
  363. }
  364. //编辑
  365. if(Request::param('id')){
  366. $data = Request::param();
  367. view::assign('data', $data);
  368. }
  369. $condition[] = ['root_id', '=', request()->employee->root_id];
  370. $condition[] = ['pid', '=', 0];
  371. $list = CompanyStrengthCate::where($condition)->order('id desc')->field('id,name')->select()->toArray();
  372. view::assign('list', $list);
  373. return view::fetch();
  374. }
  375. /*
  376. * 删除标签更新文章label
  377. */
  378. public function with_type_count()
  379. {
  380. return '确认删除吗?删除后公司实力标签将重置。';
  381. }
  382. /*
  383. * 标签删除
  384. */
  385. public function delete_type_or_label()
  386. {
  387. $param = Request::param();
  388. $cateType = CompanyStrengthCate::where(['root_id'=>request()->employee->root_id,'id'=>$param['id']])->find();
  389. if(empty($cateType)) return json(['code' => 1, 'msg' => '数据不存在']);
  390. Db::startTrans();
  391. try {
  392. //更新关联内容
  393. CompanyStrengthModel::where(['root_id'=>request()->employee->root_id,'label'=>$param['id']])->update(['label'=>0]);
  394. //删除分类
  395. $cateType->delete();
  396. Db::commit();
  397. return json(['code' => 0, 'msg' => '删除成功']);
  398. } catch (\Exception $e) {
  399. Db::rollback();
  400. return json(['code' => 1, 'msg' => '删除失败']);
  401. }
  402. }
  403. /*
  404. * 添加标签
  405. */
  406. public function addtype($value, $id = null)
  407. {
  408. $array = ['name' => $value];
  409. if (isset($id)) {
  410. $cateType = CompanyStrengthCate::where(['root_id'=>request()->employee->root_id,'name'=>$id])->find();
  411. if(empty($cateType)) return json(['code' => 1, 'msg' => '数据不存在']);
  412. $array['pid'] = $cateType['id'];
  413. }
  414. $array['root_id'] = request()->employee->root_id;
  415. $insertGetId = CompanyStrengthCate::insertGetId($array);
  416. $data = ['id' => $insertGetId, 'value' => $value];
  417. return json(['code' => 0, 'msg' => '添加成功', 'data' => $data]);
  418. }
  419. /*
  420. * 预览api
  421. */
  422. public function preview_read()
  423. {
  424. $id = input('id',0);
  425. $data = CompanyStrengthModel::where(['id' => $id, 'root_id' => request()->employee->root_id])->find()->toArray();
  426. //公司信息
  427. $where[] = ['root_id','=',request()->employee->root_id];
  428. $data['company_info'] = Company::where($where)->field('company_name,logo,company_address')->find();
  429. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
  430. }
  431. /**
  432. * 数据统计页面
  433. */
  434. public function data_statistics($type = 0, $org_id = 0, $date = '')
  435. {
  436. $root_id = request()->employee->root_id;
  437. if ($type) {
  438. if ($org_id) {
  439. $path = Org::where('id', $org_id)->value('path');
  440. $org_where[] = ['path', 'like', $path . '%'];
  441. $org_ids = Org::where($org_where)->column('id');
  442. $eids = Employee::where([['org_id', 'in', $org_ids], ['uid', '>', 0], ['state', '=', '在职']])->column('id');
  443. } else {
  444. $pid = Org::where([['path', 'like', $root_id . '-%']])->column('pid');
  445. $org_ids = Org::where([['path', 'like', $root_id . '-%'],['id','not in',$pid]])->column('id');
  446. $em_where[] = ['root_id', '=', $root_id];
  447. $em_where[] = ['uid', '>', 0];
  448. $em_where[] = ['state', 'in', ['在职', '离职']];
  449. $em_where[] = ['org_id','in',$org_ids];
  450. $eids = Employee::where($em_where)->column('id');
  451. }
  452. if ($date) $date = explode(' - ', $date);
  453. //案例总数
  454. $case_where[] = ['root_id', '=', $root_id];
  455. $case_where[] = ['del', '=', 0];
  456. if (isset($eids)) $case_where[] = ['employee_id', 'in', $eids];
  457. if ($date) $case_where[] = ['addtime', 'between', [trim($date[0]) . ' 00:00:00', trim($date[1]) . ' 23:59:59']];
  458. $case = CompanyStrengthModel::where($case_where)->column('view_times');
  459. $data['case_count'] = count($case);
  460. //浏览次数
  461. // $data['views'] = array_sum(array_filter($case));
  462. //浏览人数 //浏览时长
  463. $foot_where = [['pipe_type', '=', 'CompanyStrength'], ['employee_id', 'in', $eids]];
  464. if ($date) $foot_where[] = ['addtime', 'between', [trim($date[0]) . ' 00:00:00', trim($date[1]) . ' 23:59:59']];
  465. $footprint = Footprints::where($foot_where)->column('visit_due_time,uid');
  466. $data['times'] = array_sum(array_column($footprint,'visit_due_time'));
  467. $data['views'] = count(array_unique(array_column($footprint,'uid')));
  468. //转发次数
  469. $share_where = [['employee_id', 'in', $eids], ['type', '=', 'CompanyStrength']];
  470. if ($date) $share_where[] = ['share_time', 'between', [trim($date[0]) . ' 00:00:00', trim($date[1]) . ' 23:59:59']];
  471. $shares = ShareLog::where($share_where)->column('employee_id');
  472. $data['share_count'] = count($shares);
  473. //转发人数
  474. $data['share_group_count'] = count(array_unique($shares));
  475. //获得线索
  476. $clue_where = [['employee_id', 'in', $eids], ['pipe_type', '=', 'CompanyStrength']];
  477. if ($date) $clue_where[] = ['addtime', 'between', [trim($date[0]) . ' 00:00:00', trim($date[1]) . ' 23:59:59']];
  478. $data['clue'] = CustomerClue::where($clue_where)->count();
  479. return $data;
  480. }
  481. //父級部门
  482. $pid = Org::where([['path', 'like', $root_id . '-%']])->column('pid');
  483. $org = Org::where([['id', 'in', $pid]])->column('id,name');
  484. View::assign('org', $org);
  485. return View::fetch();
  486. }
  487. /**
  488. * 拓客案例数据统计列表
  489. */
  490. public function data_statistics_list()
  491. {
  492. $root_id = request()->employee->root_id;
  493. $param = Request()->only(['page' => 1, 'limit' => 10, 'date' => '', 'org_id' => 0]);
  494. //父級部门
  495. $pid = Org::where([['path', 'like', $root_id . '-%']])->column('pid');
  496. $where[] = ['id', 'not in', $pid];
  497. $where[] = ['path', 'like', $root_id . '-%'];
  498. if ($param['org_id']) {
  499. $path = Org::where('id', $param['org_id'])->value('path');
  500. $where[] = ['path', 'like', $path . '%'];
  501. }
  502. $date_where = $share_date_where = [];
  503. if ($param['date']) {
  504. $date = explode(' - ', $param['date']);
  505. $date_where[] = ['addtime', 'between', [trim($date[0]) . ' 00:00:00', trim($date[1]) . ' 23:59:59']];
  506. $share_date_where[] = ['share_time', 'between', [trim($date[0]) . ' 00:00:00', trim($date[1]) . ' 23:59:59']];
  507. }
  508. //基层部门
  509. $data = Org::with(['employee' => function ($query) {
  510. $query->where('state', '在职')->field('id,org_id');
  511. }])->where($where)->page($param['page'], $param['limit'])->order('id asc')->field('id,name')->select()->toArray();
  512. //业务员
  513. $em_where[] = ['root_id', '=', $root_id];
  514. $em_where[] = ['uid', '>', 0];
  515. $em_where[] = ['state', '=', '在职'];
  516. $em_where[] = ['org_id', 'in', array_column($data, 'id')];
  517. $eids = Employee::where($em_where)->column('id');
  518. //转发人数,转发次数
  519. $shares = ShareLog::where([['employee_id', 'in', $eids], ['type', '=', 'CompanyStrength']])->where($share_date_where)->column('employee_id');
  520. //浏览人数,浏览次数,浏览时长
  521. $footprint = Footprints::where([['employee_id', 'in', $eids], ['pipe_type', '=', 'CompanyStrength']])->where($date_where)->column('employee_id,uid,visit_due_time');
  522. $footprints = [];
  523. foreach ($footprint as $val) {
  524. //浏览人员
  525. $footprints[$val['employee_id']]['employee'][]=$val['uid'];
  526. //浏览时长
  527. $val['visit_due_time'] = $val['visit_due_time'] ? $val['visit_due_time'] : 0;
  528. isset($footprints[$val['employee_id']]['visit_due_time']) ? $footprints[$val['employee_id']]['visit_due_time']+=$val['visit_due_time'] : $footprints[$val['employee_id']]['visit_due_time']=$val['visit_due_time'];
  529. }
  530. //线索
  531. $clue = CustomerClue::where([['employee_id', 'in', $eids], ['pipe_type', '=', 'CompanyStrength']])->where($date_where)->group('employee_id')->column('count(id) count', 'employee_id');
  532. $res = [];
  533. foreach ($data as $k => $v) {
  534. $employee = array_intersect($shares, array_column($v['employee'], 'id'));
  535. $share_count = count($employee);
  536. $share_group_count = count(array_unique($employee));
  537. $look_person = []; //浏览人员
  538. $clue_count = 0; //线索数量
  539. $visit_due_time = 0; //浏览时长
  540. $employee_count = count($v['employee']); //部门人数
  541. foreach ($v['employee'] as $k2 => $v2) {
  542. if (isset($footprints[$v2['id']])) {
  543. $look_person = array_merge($footprints[$v2['id']]['employee'],$look_person);
  544. $visit_due_time += $footprints[$v2['id']]['visit_due_time'];
  545. }
  546. if (isset($clue[$v2['id']])) $clue_count += $clue[$v2['id']];
  547. }
  548. $res[] = [
  549. 'id' => $v['id'],
  550. 'org_name' => $v['name'],
  551. 'share_count' => $share_count,
  552. 'views' => count(array_unique($look_person)),
  553. 'clue' => $clue_count,
  554. 'visit_due_time' => $visit_due_time,
  555. 'share_group_count' => $share_group_count,
  556. 'no_share_count' => $employee_count - $share_group_count,
  557. ];
  558. }
  559. $count = Org::where($where)->count();
  560. $top_date = $this->data_statistics(1, $param['org_id'], $param['date']);
  561. return json(['code' => 0, 'data' => $res, 'count' => $count, 'top_data' => $top_date]);
  562. }
  563. /**
  564. * 数据部门详情列表
  565. */
  566. public function datastatistics_org()
  567. {
  568. $org_id = input('org_id', 0);
  569. View::assign('org_id', $org_id);
  570. return View::fetch();
  571. }
  572. /**
  573. * 数据部门详情列表
  574. */
  575. public function datastatistics_org_list()
  576. {
  577. $param = Request()->only(['org_id' => 0, 'page' => 1, 'limit' => 10, 'keyword' => '', 'date' => '']);
  578. $root_id = request()->employee->root_id;
  579. $em_where[] = ['root_id', '=', $root_id];
  580. $em_where[] = ['uid', '>', 0];
  581. $em_where[] = ['state', '=', '在职'];
  582. $em_where[] = ['org_id', '=', $param['org_id']];
  583. if ($param['keyword']) $em_where[] = ['name', 'like', '%' . trim($param['keyword']) . '%'];
  584. $res = Employee::where($em_where)->page($param['page'], $param['limit'])->column('id,name');
  585. $count = Employee::where($em_where)->count();
  586. $eids = array_column($res, 'id');
  587. $date_where = $share_date_where = [];
  588. if ($param['date']) {
  589. $date = explode(' - ', $param['date']);
  590. $date_where[] = ['addtime', 'between', [trim($date[0]) . ' 00:00:00', trim($date[1]) . ' 23:59:59']];
  591. $share_date_where[] = ['share_time', 'between', [trim($date[0]) . ' 00:00:00', trim($date[1]) . ' 23:59:59']];
  592. }
  593. //转发人数,转发次数
  594. $shares = ShareLog::where([['employee_id', 'in', $eids], ['type', '=', 'CompanyStrength']])->where($share_date_where)->column('employee_id');
  595. //浏览人数,浏览次数,浏览时长
  596. $footprint = Footprints::where([['employee_id', 'in', $eids], ['pipe_type', '=', 'CompanyStrength']])->where($date_where)->column('employee_id,uid,visit_due_time');
  597. $footprints = [];
  598. foreach ($footprint as $val) {
  599. //浏览人员
  600. $footprints[$val['employee_id']]['employee'][]=$val['uid'];
  601. //浏览时长
  602. $val['visit_due_time'] = $val['visit_due_time'] ? $val['visit_due_time'] : 0;
  603. isset($footprints[$val['employee_id']]['visit_due_time']) ? $footprints[$val['employee_id']]['visit_due_time']+=$val['visit_due_time'] : $footprints[$val['employee_id']]['visit_due_time']=$val['visit_due_time'];
  604. }
  605. //线索
  606. $clue = CustomerClue::where([['employee_id', 'in', $eids], ['pipe_type', '=', 'CompanyStrength']])->where($date_where)->group('employee_id')->column('count(id) count', 'employee_id');
  607. $data = [];
  608. foreach ($res as $k => $v) {
  609. $employee = array_intersect($shares, [$v['id']]);
  610. $data[] = [
  611. 'id' => $v['id'],
  612. 'name' => $v['name'],
  613. 'share_count' => count($employee),
  614. 'views' => isset($footprints[$v['id']]) ? count(array_unique($footprints[$v['id']]['employee'])) : 0,
  615. 'clue' => isset($clue[$v['id']]) ? $clue[$v['id']] : 0,
  616. 'visit_due_time' => isset($footprints[$v['id']]) ? $footprints[$v['id']]['visit_due_time'] : 0
  617. ];
  618. }
  619. return json(['code' => 0, 'data' => $data, 'count' => $count, 'page' => $param]);
  620. }
  621. /**
  622. * 数据统计人员统计详情页面
  623. */
  624. public function datastatistics_org_read()
  625. {
  626. $eid = input('eid', 0);
  627. View::assign('eid', $eid);
  628. return View::fetch();
  629. }
  630. /**
  631. * 数据统计人员统计详情页面
  632. */
  633. public function datastatistics_org_read_list()
  634. {
  635. $param = Request()->only(['eid' => 0, 'page' => 1, 'limit' => 10, 'keyword' => '', 'date' => '']);
  636. $root_id = request()->employee->root_id;
  637. $where[] = ['employee_id', '=', $param['eid']];
  638. $where[] = ['type', '=', 'CompanyStrength'];
  639. if ($param['keyword']) {
  640. $keyword_where[] = ['root_id', '=', $root_id];
  641. $keyword_where[] = ['title', 'like', '%' . trim($param['keyword']) . '%'];
  642. $mids = CompanyStrengthModel::where($keyword_where)->column('id');
  643. $where[] = ['data_id', 'in', $mids];
  644. }
  645. $ids = ShareLog::where($where)->group('data_id')->column('data_id');
  646. if (empty($ids)) return json(['code' => 0, 'data' => [], 'count' => 0]);
  647. $query[] = ['id', 'in', $ids];
  648. //标题
  649. $data = CompanyStrengthModel::where($query)->page($param['page'], $param['limit'])->column('title,id');
  650. $count = CompanyStrengthModel::where($query)->count();
  651. $ids = array_column($data, 'id');
  652. $share_where = $date_where = [];
  653. if ($param['date']) {
  654. $date = explode(' - ', $param['date']);
  655. $share_where[] = ['share_time', 'between', [trim($date[0]) . ' 00:00:00', trim($date[1]) . ' 23:59:59']];
  656. $date_where[] = ['addtime', 'between', [trim($date[0]) . ' 00:00:00', trim($date[1]) . ' 23:59:59']];
  657. }
  658. //分享次数
  659. $shares = ShareLog::where([['data_id', 'in', $ids], ['type', '=', 'CompanyStrength']])->where($share_where)->group('data_id')->column('count(id) count', 'data_id');
  660. //获得线索
  661. $clue = CustomerClue::where([['pipe_id', 'in', $ids], ['pipe_type', '=', 'CompanyStrength']])->where($date_where)->group('pipe_id')->column('count(id) count', 'pipe_id');
  662. //浏览时长,浏览次数
  663. $foot_where[] = ['pipe_type', '=', 'CompanyStrength'];
  664. $foot_where[] = ['employee_id', '=', $param['eid']];
  665. $where_ids = [];
  666. foreach ($ids as $v) {
  667. $where_ids[] = '{"id":' . $v . '%';
  668. }
  669. $foot_where[] = ['reg_info', 'like', $where_ids, 'OR'];
  670. $footprints = Footprints::where($foot_where)->where($date_where)->column('reg_info,visit_due_time');
  671. $views = [];
  672. $time = [];
  673. foreach ($footprints as $v2) {
  674. $json = json_decode($v2['reg_info'], true);
  675. $views[$json['id']][] = $json['id'];
  676. if ($v2['visit_due_time']) {
  677. isset($time[$json['id']]) ? $time[$json['id']] += $v2['visit_due_time'] : $time[$json['id']] = $v2['visit_due_time'];
  678. }
  679. }
  680. $res = [];
  681. foreach ($data as $k3 => $v3) {
  682. //浏览人数,浏览时长
  683. $res[] = [
  684. 'title' => $v3['title'],
  685. 'id' => $v3['id'],
  686. 'share_count' => isset($shares[$v3['id']]) ? $shares[$v3['id']] : 0,
  687. 'view_count' => isset($views[$v3['id']]) ? count($views[$v3['id']]) : 0,
  688. 'visit_due_time' => isset($time[$v3['id']]) ? $time[$v3['id']] : 0,
  689. 'clue' => isset($clue[$v3['id']]) ? $clue[$v3['id']] : 0
  690. ];
  691. }
  692. return json(['code' => 0, 'data' => $res, 'count' => $count]);
  693. }
  694. }