Video.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. <?php
  2. namespace app\sys\controller;
  3. use think\facade\View;
  4. use think\facade\Request;
  5. use think\facade\Db;
  6. use app\model\Video as VideoModel;
  7. use app\model\VideoType;
  8. use douyin\VideoCrawler;
  9. use app\model\Employee;
  10. use app\model\Company;
  11. use app\model\Org;
  12. use app\model\ShareLog;
  13. use app\model\Footprints;
  14. use app\model\CustomerClue;
  15. use app\logics\MaterialLogic;
  16. class Video extends Base
  17. {
  18. /**
  19. * 列表页面
  20. */
  21. public function index()
  22. {
  23. if (request()->isAjax()) return $this->list();
  24. // 类型获取
  25. $type = VideoType::where(['pid' => 0, 'type' => 'video', 'root_id' => request()->employee->root_id])->column('*', 'id');
  26. $label = VideoType::where([['pid', '>', 0], ['type', '=', 'video'], ['root_id', '=', request()->employee->root_id]])->select()->toArray();
  27. $joinlabel = [];
  28. foreach ($label as $val) {
  29. $joinlabel[$val['pid']][$val['id']] = $val['name'];
  30. }
  31. $eid = VideoModel::where([['delete_time', '=', 0], ['root_id', '=', request()->employee->root_id], ['employee_id', '>', 0]])->group('employee_id')->column('employee_id');
  32. $w[] = ['id', 'in', $eid];
  33. $w[] = ['grant_id', '>', 0];
  34. $w[] = ['root_id', '=', request()->employee->root_id];
  35. // 运营人员列表
  36. $employee = Employee::where($w)->field(['id', 'opt_name as name'])->select()->toArray();
  37. View::assign('employee', $employee);
  38. View::assign('type', $type);
  39. View::assign('label', json_encode($joinlabel));
  40. return View::fetch();
  41. }
  42. /**
  43. * 列表
  44. */
  45. public function list()
  46. {
  47. // 请求参数获取
  48. $param = Request::param();
  49. // 请求条件初始化
  50. $where = [
  51. ['delete_time', '=', 0],
  52. ['root_id', '=', request()->employee->root_id]
  53. ];
  54. // 查询条件付值
  55. if (!empty($param['title'])) {
  56. $where[] = ['title', 'like', '%' . $param['title'] . '%'];
  57. }
  58. if (!empty($param['type'])) {
  59. $where[] = ['type', '=', $param['type']];
  60. }
  61. if (!empty($param['label'])) {
  62. $where[] = ['label', '=', $param['label']];
  63. }
  64. if (!empty($param['employee_id'])) {
  65. $where[] = ['employee_id', '=', $param['employee_id']];
  66. }
  67. //上传时间筛选
  68. if (!empty($param['add_time'])) {
  69. $newtime=explode(' - ', $param['add_time']);
  70. $start_time=$newtime[0].' 00:00:00';
  71. $end_time=$newtime[1].' 23:59:59';
  72. $where[] = ['uploadtime', 'between', [$start_time, $end_time]];
  73. }
  74. $order='uploadtime desc';
  75. 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'])) {
  76. $order = $param['order'];
  77. }
  78. // 获取数据
  79. $list = VideoModel::with('employee')->where($where)->page($param['page'], $param['limit'])->order($order)->select()->toArray();
  80. $count = VideoModel::where($where)->count();
  81. //增加查询转发次数浏览时长
  82. // if(!empty($list)){
  83. // $statismod=new MaterialLogic;
  84. // $selorder=!empty($param['order'])?$param['order']:'';
  85. // $list=$statismod->sel_case_share($list,'Video',$selorder);
  86. // }
  87. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  88. }
  89. /**
  90. * 添加
  91. */
  92. public function add()
  93. {
  94. $type = (new VideoType)->where(['pid' => 0, 'type' => 'video', 'root_id' => request()->employee->root_id])->column('*', 'id');
  95. $label = (new VideoType)->where([['pid', '>', 0], ['type', '=', 'video'], ['root_id', '=', request()->employee->root_id]])->select()->toArray();
  96. $joinlabel = [];
  97. foreach ($label as $val) {
  98. $joinlabel[$val['pid']][$val['id']] = $val['name'];
  99. }
  100. View::assign('type', $type);
  101. View::assign('label', json_encode($joinlabel));
  102. $adding = input('adding', 0);
  103. view::assign('adding', $adding);
  104. return View::fetch();
  105. }
  106. /**
  107. * 添加视频保存
  108. */
  109. public function addSave()
  110. {
  111. $data = Request::only(['title', 'type', 'label', 'description', 'video_url', 'douyin_url', 'id', 'cover','video_type', 'cover_share_img']);
  112. // 如果是抓取到抖音文件,先将抓取到文件地址进行更改,后保存
  113. if (!empty($data['douyin_url'])) {
  114. $file = '.' . $data['douyin_url'];
  115. $path = 'video' . DIRECTORY_SEPARATOR . pathinfo($file, PATHINFO_BASENAME);
  116. $data['video_url'] = $path;
  117. }
  118. unset($data['douyin_url']);
  119. if (!empty($data['video_url'])) {
  120. $arr = explode(',', $data['video_url']);
  121. $data['video_url'] = end($arr);
  122. }
  123. if (!empty($data['id'])) {
  124. VideoModel::where(['id' => $data['id'], 'root_id' => request()->employee->root_id])->update($data);
  125. $msg = '编辑成功';
  126. } else {
  127. $data['employee_id'] = request()->employee->id;
  128. $data['root_id'] = request()->employee->root_id;
  129. VideoModel::create($data);
  130. $msg = '添加成功';
  131. }
  132. if (isset($file) && ossUpload($path, $file)) {
  133. unlink($file);
  134. }
  135. return json(['code' => 0, 'msg' => $msg]);
  136. }
  137. /**
  138. * 视频修改
  139. */
  140. public function edit($id)
  141. {
  142. $data = VideoModel::where(['id' => $id, 'root_id' => request()->employee->root_id])->find();
  143. $data['cover_url'] = $data->getData('cover');
  144. $data['video_urls'] = $data->video_url;
  145. $cover = $data->cover;
  146. $cover_share_img = $data->cover_share_img;
  147. $data = $data->getData();
  148. $data['cover'] = $cover;
  149. $data['cover_share_img'] = $cover_share_img;
  150. // 类型获取
  151. $type = VideoType::where(['pid' => 0, 'type' => 'video', 'root_id' => request()->employee->root_id])->column('*', 'id');
  152. $label = VideoType::where([['pid', '>', 0], ['type', '=', 'video'], ['root_id', '=', request()->employee->root_id]])->select()->toArray();
  153. $joinlabel = [];
  154. foreach ($label as $val) {
  155. $joinlabel[$val['pid']][$val['id']] = $val['name'];
  156. }
  157. View::assign('type', $type);
  158. View::assign('label', json_encode($joinlabel));
  159. // 参数设置
  160. View::assign('checked', $joinlabel[$data['type']] ?? []);
  161. View::assign('data', $data);
  162. return View::fetch();
  163. }
  164. /**
  165. * 视频删除
  166. */
  167. public function del($id)
  168. {
  169. VideoModel::where(['id' => $id, 'root_id' => request()->employee->root_id])->update(['delete_time' => time()]);
  170. return json(['code' => 0, 'msg' => '删除成功']);
  171. }
  172. /**
  173. * 抖音内容抓取
  174. */
  175. public function catch($link)
  176. {
  177. // 抓取内容
  178. preg_match('/https:\/\/[\w\/\.]*/', $link, $match);
  179. if (!isset($match[0])) return json(['code' => 1, 'msg' => '未检测到链接地址']);
  180. $url = parse_url($match[0]);
  181. if ($url['scheme'] . '://' . $url['host'] != 'https://v.douyin.com') return json(['code' => 1, 'msg' => '请输入有效的抖音链接']);
  182. $crawler = new VideoCrawler();
  183. $data = $crawler->crawByUrl($match[0]);
  184. if ($data == false) return json(['code' => 1, 'msg' => '抓取失败,请手动上传视频']);
  185. // 定义配置参数
  186. ini_set("user_agent", "Mozilla/4.0 (compatible; MSIE 5.00; Windows 98)");
  187. // 将视频文件下载
  188. $r = file_get_contents($data['play']);
  189. $filename = './upload' . DIRECTORY_SEPARATOR . uniqid() . '.mp4';
  190. file_put_contents($filename, $r);
  191. $data['video'] = ltrim($filename, '.');
  192. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
  193. }
  194. /*
  195. * 添加分类
  196. */
  197. public function addtype($value, $id = null)
  198. {
  199. $array = [
  200. 'name' => $value,
  201. 'root_id' => request()->employee->root_id,
  202. 'type' => 'video'
  203. ];
  204. if (!empty($id)) $array['pid'] = $id;
  205. $insertGetId = VideoType::insertGetId($array);
  206. $data = ['id' => $insertGetId, 'value' => $value];
  207. return json(['code' => 0, 'msg' => '添加成功', 'data' => $data]);
  208. }
  209. /**
  210. * 是否展示
  211. */
  212. public function publish($id)
  213. {
  214. $obj = VideoModel::where(['id' => $id, 'root_id' => request()->employee->root_id])->find();
  215. if (empty($obj)) return json(['code' => 1, 'msg' => '数据不存在']);
  216. $obj->publish = $obj->publish == 1 ? 0 : 1;
  217. $obj->save();
  218. return json(['code' => 0, 'msg' => '修改成功']);
  219. }
  220. /*
  221. * 分类管理
  222. */
  223. function cate_video()
  224. {
  225. return View::fetch();
  226. }
  227. /*
  228. * 分类管理列表
  229. */
  230. function cate_video_list()
  231. {
  232. $param = Request::param();
  233. $condition[] = ['root_id', '=', request()->employee->root_id];
  234. $condition[] = ['type', '=', 'video'];
  235. $condition[] = ['pid', '=', 0];
  236. $list = VideoType::where($condition)->page($param['page'], $param['limit'])->order(['show' => 'asc', 'id' => 'desc'])->field('id,name,show')->select()->toarray();
  237. $count = VideoType::where($condition)->count();
  238. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  239. }
  240. /*
  241. * 分类管理添加
  242. */
  243. function cate_video_add()
  244. {
  245. $param = Request::param();
  246. $condition[] = ['name', '=', $param['name']];
  247. $condition[] = ['root_id', '=', request()->employee->root_id];
  248. $condition[] = ['type', '=', 'video'];
  249. $data['name'] = $param['name'];
  250. $model = VideoType::where($condition);
  251. $models = new VideoType;
  252. $info = $model->find();
  253. if ($info) {
  254. return json(['code' => 1, 'msg' => '名称已存在']);
  255. }
  256. $models->name = $param['name'];
  257. $models->type = 'video';
  258. $models->root_id = request()->employee->root_id;
  259. $models->save();
  260. return json(['code' => 0, 'msg' => '保存成功', 'data' => '']);
  261. }
  262. /*
  263. * 分类管理编辑
  264. */
  265. function cate_video_edit()
  266. {
  267. $param = Request::param();
  268. $conditions = [['id', '=', $param['id']]];
  269. $condition[] = ['name', '=', $param['name']];
  270. $condition[] = ['root_id', '=', request()->employee->root_id];
  271. $condition[] = ['type', '=', 'video'];
  272. $data['name'] = $param['name'];
  273. $model = VideoType::where($condition);
  274. $models = VideoType::where($conditions);
  275. $info = $model->find();
  276. if ($info) {
  277. return json(['code' => 1, 'msg' => '名称已存在']);
  278. }
  279. $models->update($data);
  280. return json(['code' => 0, 'msg' => '保存成功', 'data' => '']);
  281. }
  282. /*
  283. * 删除标签或分类前,查询关联的内容数量
  284. */
  285. public function with_type_count()
  286. {
  287. $param = Request::param();
  288. if ($param['type'] == 'type') {
  289. $field = 'type';
  290. } else {
  291. $field = 'label';
  292. }
  293. $where = [
  294. ['root_id', '=', request()->employee->root_id],
  295. ['delete_time', '=', 0],
  296. [$field, '=', $param['id']]
  297. ];
  298. $count = VideoModel::where($where)->count();
  299. if ($count > 0) {
  300. $type = $field == 'type' ? '分类' : '标签';
  301. $msg = '该' . $type . '有' . $count . '个' . '拓客视频,删除后该内容将归为其它分类。';
  302. } else {
  303. $msg = '确定删除该分类吗?';
  304. }
  305. return $msg;
  306. }
  307. /*
  308. * 分类、标签删除
  309. */
  310. public function delete_type_or_label()
  311. {
  312. $param = Request::param();
  313. $videoType = VideoType::where(['root_id' => request()->employee->root_id, 'type' => 'video', 'id' => $param['id']])->find();
  314. if (empty($videoType)) return json(['code' => 1, 'msg' => '数据不存在']);
  315. Db::startTrans();
  316. try {
  317. if ($param['type'] == 'type') {
  318. //查询是否存在其它分类
  319. $type_id = VideoType::where(['root_id' => request()->employee->root_id, 'pid' => 0, 'type' => 'video', 'show' => 1])->value('id');
  320. if (empty($type_id)) $type_id = VideoType::insertGetId(['pid' => 0, 'name' => '其它', 'type' => 'video', 'root_id' => request()->employee->root_id, 'show' => 1]);
  321. //更新关联内容
  322. VideoType::where(['root_id' => request()->employee->root_id, 'pid' => $param['id'], 'type' => 'video'])->update(['pid' => $type_id]);
  323. VideoModel::where(['root_id' => request()->employee->root_id, 'type' => $param['id']])->update(['type' => $type_id]);
  324. $more_other_id = VideoType::where(['root_id' => request()->employee->root_id, 'pid' => $type_id, 'type' => 'video', 'show' => 1])->column('id');
  325. if (count($more_other_id) >= 2) {
  326. $first_id = $more_other_id[0];
  327. VideoModel::where([['root_id', '=', request()->employee->root_id], ['type', '=', $type_id], ['label', 'in', $more_other_id]])->update(['label' => $first_id]);
  328. unset($more_other_id[0]);
  329. VideoType::where([['root_id', '=', request()->employee->root_id], ['id', 'in', $more_other_id]])->delete();
  330. }
  331. //删除分类
  332. $videoType->delete();
  333. } else {
  334. $label_id = VideoType::where(['root_id' => request()->employee->root_id, 'pid' => $param['pid'], 'type' => 'video', 'show' => 1])->value('id');
  335. if (empty($label_id)) $label_id = VideoType::insertGetId(['pid' => $param['pid'], 'name' => '其它', 'type' => 'video', 'root_id' => request()->employee->root_id, 'show' => 1]);
  336. //更新关联内容
  337. VideoModel::where(['root_id' => request()->employee->root_id, 'label' => $param['id']])->update(['label' => $label_id]);
  338. //删除分类
  339. $videoType->delete();
  340. }
  341. Db::commit();
  342. return json(['code' => 0, 'msg' => '删除成功']);
  343. } catch (\Exception $e) {
  344. Db::rollback();
  345. return json(['code' => 1, 'msg' => '删除失败']);
  346. }
  347. }
  348. /*
  349. * 标签管理
  350. */
  351. function cate_video_labels()
  352. {
  353. return View::fetch();
  354. }
  355. /*
  356. * 新标签管理列表
  357. */
  358. function cate_video_list_labels()
  359. {
  360. $param = Request::param();
  361. $condition[] = ['root_id', '=', request()->employee->root_id];
  362. $condition[] = ['type', '=', 'video'];
  363. $condition[] = ['pid', '>', 0];
  364. $parents = VideoType::where([['pid', '=', 0]])->column('name', 'id');
  365. $list = VideoType::where($condition)->page($param['page'], $param['limit'])->order(['show' => 'asc', 'id' => 'desc'])->field('id,name,pid,show')->select()->toarray();
  366. foreach ($list as $k => $v) {
  367. $list[$k]['pname'] = isset($parents[$v['pid']]) ? $parents[$v['pid']] : '';
  368. }
  369. $count = VideoType::where($condition)->count();
  370. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  371. }
  372. /*
  373. * 新分类标签管理编辑
  374. */
  375. function cate_video_edit_labels()
  376. {
  377. $param = Request::param();
  378. $conditions = [['id', '=', $param['id']]];
  379. $condition[] = ['name', '=', $param['name']];
  380. $condition[] = ['root_id', '=', request()->employee->root_id];
  381. $condition[] = ['pid', '=', $param['pid']];
  382. $condition[] = ['type', '=', 'video'];
  383. $data['name'] = $param['name'];
  384. $model = VideoType::where($condition);
  385. $models = VideoType::where($conditions);
  386. $info = $model->find();
  387. if ($info) {
  388. return json(['code' => 1, 'msg' => '名称已存在']);
  389. }
  390. $models->update($data);
  391. return json(['code' => 0, 'msg' => '保存成功', 'data' => '']);
  392. }
  393. /*
  394. * 新标签添加
  395. */
  396. function cate_video_labels_add()
  397. {
  398. if (request()->isAjax()) {
  399. $param = Request::param();
  400. if (!$param['id'] || !$param['name']) {
  401. return json(['code' => 1, 'msg' => '缺少参数', 'data']);
  402. }
  403. isset($param['label_id']) ? $condition[] = ['id', '<>', $param['label_id']] : '';
  404. $condition[] = ['name', '=', $param['name']];
  405. $condition[] = ['root_id', '=', request()->employee->root_id];
  406. $condition[] = ['pid', '=', $param['id']];
  407. $condition[] = ['type', '=', 'video'];
  408. $info = VideoType::where($condition)->find();
  409. if ($info) {
  410. return json(['code' => 1, 'msg' => '名称已存在']);
  411. }
  412. if (isset($param['label_id'])) {
  413. VideoType::where(['root_id' => request()->employee->root_id, 'type' => 'video', 'id' => $param['label_id']])->update(['pid' => $param['id'], 'name' => $param['name']]);
  414. VideoModel::where(['root_id' => request()->employee->root_id, 'label' => $param['label_id']])->update(['type' => $param['id']]);
  415. } else {
  416. $model = new VideoType;
  417. $model->name = $param['name'];
  418. $model->pid = $param['id'];
  419. $model->type = 'video';
  420. $model->root_id = request()->employee->root_id;
  421. $model->save();
  422. }
  423. return json(['code' => 0, 'msg' => '保存成功', 'data' => '']);
  424. }
  425. //编辑
  426. if (Request::param('id')) {
  427. $data = Request::param();
  428. view::assign('data', $data);
  429. }
  430. $condition[] = ['root_id', '=', request()->employee->root_id];
  431. $condition[] = ['type', '=', 'video'];
  432. $condition[] = ['pid', '=', 0];
  433. $list = VideoType::where($condition)->order('id desc')->field('id,name')->select()->toArray();
  434. view::assign('list', $list);
  435. return view::fetch();
  436. }
  437. /**
  438. * 预览详情
  439. */
  440. public function preview_read()
  441. {
  442. $id = input('id', 0);
  443. $data = VideoModel::where(['id' => $id, 'root_id' => request()->employee->root_id])->find();
  444. //公司信息
  445. $where[] = ['root_id', '=', request()->employee->root_id];
  446. $data['company_info'] = Company::where($where)->field('company_name,logo,company_address')->find();
  447. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
  448. }
  449. /**
  450. * 文件上传
  451. */
  452. public function fileupload()
  453. {
  454. $ali_oss_bindurl = config('app.ali_oss_bindurl');
  455. $url = 'https://' . $ali_oss_bindurl . '/' . Request::param('file');
  456. return json(['code' => 0, 'data' => ['src' => $url]]);
  457. }
  458. //数据统计
  459. public function statistics_data_list()
  460. {
  461. $request = request();
  462. $param=$request->only(['page'=>1,'limit'=>10,'org_id','time']);
  463. if (!request()->isAjax()) {
  464. $list=orgSubIds(request()->employee->root_id);
  465. $xins=[];
  466. foreach($list as $key=>$val){
  467. if(Org::where([['pid','=',$val],['status','=',1]])->value('id')){
  468. $xins[]=$val;
  469. }
  470. }
  471. $orglist=Org::where([['id','in',$xins]])->field('id,name,pid')->select()->toArray();
  472. view::assign('orglist', $orglist);
  473. return view::fetch();
  474. }
  475. if(!empty($param['org_id'])){
  476. $list=orgSubIds($param['org_id']);
  477. }else{
  478. $list=orgSubIds(request()->employee->root_id);
  479. }
  480. $xin=[];
  481. foreach($list as $key=>$val){
  482. if(!Org::where([['pid','=',$val],['status','=',1]])->value('id')){
  483. $xin[]=$val;
  484. }
  485. }
  486. $newlist=Org::with(['employee' => function ($sql) {
  487. $sql->where([['state', '=', '在职'], ['uid', '<>', 0]])->field('id,org_id');
  488. }])->where([['id','in',$xin]])->field('id,name,pid')->page($param['page'],$param['limit'])->select()->toArray();
  489. $orgid = array_column($newlist, 'id');
  490. $empid = Employee::where([['org_id', 'in', $orgid], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  491. $clue_type = 'video';
  492. $share_type = 'Video';
  493. //转发人数
  494. if(!empty($param['time'])){
  495. $newtime=explode(' - ', $param['time']);
  496. $start_time=$newtime[0].' 00:00:00';
  497. $end_time=$newtime[1].' 23:59:59';
  498. $where[] = ['share_time', 'between', [$start_time, $end_time]];
  499. $clue_where[] = ['addtime', 'between', [$start_time, $end_time]];
  500. $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
  501. }
  502. $where[] = ['type', '=', $share_type];
  503. $where[] = ['employee_id', 'in', $empid];
  504. $clue_where[] = ['employee_id', 'in', $empid];
  505. $clue_where[] = ['pipe_type', '=', $clue_type];
  506. $foot_where[] = ['employee_id', 'in', $empid];
  507. $foot_where[] = ['pipe_type', '=', $clue_type];
  508. $shared_employee = ShareLog::where($where)->column('employee_id');
  509. $clue_count = CustomerClue::where($clue_where)->column('employee_id');
  510. $foot_data = Footprints::where($foot_where)->field('id,employee_id,uid,visit_due_time')->select()->toArray();
  511. $new = [];
  512. foreach ($foot_data as $key => $val) {
  513. $new[$val['employee_id']][] = $val;
  514. }
  515. foreach ($newlist as $key => $val) {
  516. $empids = array_column($val['employee'], 'id');
  517. $newlist[$key]['share_count'] = count(array_intersect($shared_employee, $empids));
  518. $yes_share_count = count(array_unique(array_intersect($shared_employee, $empids)));
  519. $newlist[$key]['share_employee_count'] = $yes_share_count;
  520. $newlist[$key]['clue_count'] = count(array_intersect($clue_count, $empids));
  521. $cont = $visit_due_time = $uid = 0;
  522. foreach ($new as $k => $v) {
  523. if (in_array($k, $empids)) {
  524. $cont += count($v);
  525. $visit_due_time += array_sum(array_column($v, 'visit_due_time'));
  526. $uid += count(array_unique(array_column($v, 'uid')));
  527. }
  528. }
  529. $newlist[$key]['footprints_count'] = $cont;
  530. $newlist[$key]['visit_long'] = $visit_due_time;
  531. $newlist[$key]['footprints_user_count'] = $uid;
  532. $un_share_employee = count($empids) - $yes_share_count;
  533. $newlist[$key]['un_share_employee_count'] = $un_share_employee;
  534. }
  535. $count = Org::where([['id', 'in', $xin]])->count();
  536. $all = $this->top_statistics_data($xin, !empty($param['time']) ? $param['time'] : 0);
  537. $data = ['list' => $newlist, 'all' => $all];
  538. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  539. }
  540. public function top_statistics_data($xin,$time=0)
  541. {
  542. $video_cont=VideoModel::where([['delete_time', '=', 0],['root_id', '=', request()->employee->root_id]])->count();
  543. $all=['video_cont'=>$video_cont,'share_employee_count'=>0,'share_count'=>0,'clue_count'=>0,'footprints_count'=>0,'visit_long'=>0,'footprints_user_count'=>0,'un_share_employee_count'=>0];
  544. $sub_employee_ids = Employee::where([['org_id', 'in', $xin], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  545. $clue_type = 'video';
  546. $share_type = 'Video';
  547. //转发人数
  548. if(!empty($time)){
  549. $newtime=explode(' - ', $time);
  550. $start_time=$newtime[0].' 00:00:00';
  551. $end_time=$newtime[1].' 23:59:59';
  552. $where[] = ['share_time', 'between', [$start_time, $end_time]];
  553. $clue_where[] = ['addtime', 'between', [$start_time, $end_time]];
  554. $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
  555. }
  556. $where[] = ['type', '=', $share_type];
  557. $where[] = ['employee_id', 'in', $sub_employee_ids];
  558. $shared_employee = ShareLog::where($where)->column('employee_id');
  559. // 转发次数
  560. $all['share_count'] = count($shared_employee);
  561. $all['share_employee_count']=count(array_values(array_unique($shared_employee)));
  562. // 获得线索
  563. $clue_where[] = ['employee_id', 'in', $sub_employee_ids];
  564. $clue_where[] = ['pipe_type', '=', $clue_type];
  565. $clue_count = CustomerClue::where($clue_where)->count();
  566. $all['clue_count']=$clue_count;
  567. // 浏览次数
  568. $foot_where[] = ['employee_id', 'in', $sub_employee_ids];
  569. $foot_where[] = ['pipe_type', '=', $clue_type];
  570. $foot_user_count = Footprints::where($foot_where)->field('id,uid,visit_due_time')->select()->toArray();
  571. $all['footprints_user_count']=count(array_unique(array_column($foot_user_count,'uid')));
  572. // 浏览时长
  573. $all['visit_long'] = array_sum(array_column($foot_user_count,'visit_due_time'));
  574. return $all;
  575. }
  576. //统计员工列表
  577. public function statistics_emplist()
  578. {
  579. $request = request();
  580. $param=$request->only(['page'=>1,'limit'=>10,'keyword','org_id','time']);
  581. if (!request()->isAjax()) {
  582. view::assign('org_id', $param['org_id']);
  583. return view::fetch();
  584. }
  585. $e_where[]=['org_id', '=', $param['org_id']];
  586. $e_where[]=['state', '=', '在职'];
  587. $e_where[]=['uid', '<>', 0];
  588. if(!empty($param['keyword'])){
  589. $e_where[]=['name','like','%'.$param['keyword'].'%'];
  590. }
  591. $employee_list = Employee::where($e_where)->field('id,name')->page($param['page'],$param['limit'])->select()->toArray();
  592. $clue_type = 'video';
  593. $share_type = 'Video';
  594. $empid=array_column($employee_list,'id');
  595. if(!empty($param['time'])){
  596. $newtime=explode(' - ', $param['time']);
  597. $start_time=$newtime[0].' 00:00:00';
  598. $end_time=$newtime[1].' 23:59:59';
  599. $where[] = ['share_time', 'between', [$start_time, $end_time]];
  600. $clue_where[] = ['addtime', 'between', [$start_time, $end_time]];
  601. $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
  602. }
  603. $where[]=['type', '=', $share_type];
  604. $where[]=['employee_id', 'in', $empid];
  605. $clue_where[]=['employee_id', 'in', $empid];
  606. $clue_where[]=['pipe_type', '=', $clue_type];
  607. $foot_where[] = ['employee_id', 'in', $empid];
  608. $foot_where[] = ['pipe_type', '=', $clue_type];
  609. $shared_employee = ShareLog::where($where)->column('employee_id');
  610. $clue_count = CustomerClue::where($clue_where)->column('employee_id');
  611. $foot_data = Footprints::where($foot_where)->field('id,employee_id,uid,visit_due_time')->select()->toArray();
  612. $new=[];
  613. foreach($foot_data as $key=>$val){
  614. $new[$val['employee_id']][]=$val;
  615. }
  616. foreach($employee_list as $key=>$val){
  617. $empids=[$val['id']];
  618. $employee_list[$key]['share_count']=count(array_intersect($shared_employee,$empids));
  619. $yes_share_count=count(array_unique(array_intersect($shared_employee,$empids)));
  620. $employee_list[$key]['share_employee_count']=$yes_share_count;
  621. $employee_list[$key]['clue_count']=count(array_intersect($clue_count,$empids));
  622. $cont=$visit_due_time=$uid=0;
  623. foreach($new as $k=>$v){
  624. if(in_array($k,$empids)){
  625. $cont+=count($v);
  626. $visit_due_time+=array_sum(array_column($v,'visit_due_time'));
  627. $uid+=count(array_unique(array_column($v,'uid')));
  628. }
  629. }
  630. $employee_list[$key]['footprints_count']=$cont;
  631. $employee_list[$key]['visit_long']=$visit_due_time;
  632. $employee_list[$key]['footprints_user_count']=$uid;
  633. $un_share_employee = count($empids) - $yes_share_count;
  634. $employee_list[$key]['un_share_employee_count'] = $un_share_employee;
  635. }
  636. $count=Employee::where($e_where)->count();
  637. return json(['code' => 0, 'msg' => '获取成功', 'data' => $employee_list,'count'=>$count]);
  638. }
  639. //统计员工转发记录详情列表
  640. public function emp_logdetail_list()
  641. {
  642. $request = request();
  643. $param=$request->only(['page'=>1,'limit'=>10,'keyword','emp_id','time']);
  644. if (!request()->isAjax()) {
  645. view::assign('emp_id', $param['emp_id']);
  646. return view::fetch();
  647. }
  648. $empid=$param['emp_id'];
  649. $share_list=$this->sel_emp_videolist($empid);
  650. if(!empty($param['keyword'])){
  651. $key_list=VideoModel::where([['root_id','=',request()->employee->root_id],['title','like','%'.$param['keyword'].'%']])->column('id');
  652. if(!empty($key_list)&&!empty($share_list)){
  653. $tm= array_intersect($key_list,$share_list);
  654. if(!empty($tm)){
  655. $share_list=$tm;
  656. }else{
  657. return json(['code' => 0, 'msg' => '获取成功', 'data' => [],'count'=>0]);
  658. }
  659. }else{
  660. return json(['code' => 0, 'msg' => '获取成功', 'data' => [],'count'=>0]);
  661. }
  662. }
  663. $list=VideoModel::where([['root_id','=',request()->employee->root_id],['id','in',$share_list]])->field('id,title')->page($param['page'],$param['limit'])->select()->toArray();
  664. foreach($list as $key=>$val){
  665. $clue_type = 'video';
  666. $share_type = 'Video';
  667. //转发人数
  668. if(!empty($param['time'])){
  669. $newtime=explode(' - ', $param['time']);
  670. $start_time=$newtime[0].' 00:00:00';
  671. $end_time=$newtime[1].' 23:59:59';
  672. $where[] = ['share_time', 'between', [$start_time, $end_time]];
  673. $clue_where[] = ['addtime', 'between', [$start_time, $end_time]];
  674. $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
  675. }
  676. $where[] = ['type', '=', $share_type];
  677. $where[] = ['employee_id', '=', $empid];
  678. $where[] = ['data_id','=',$val['id']];
  679. $list[$key]['share_count'] = ShareLog::where($where)->count();
  680. // 获得线索
  681. $clue_where[] = ['employee_id', '=', $empid];
  682. $clue_where[] = ['pipe_type', '=', $clue_type];
  683. $clue_where[] = ['pipe_id','=',$val['id']];
  684. $clue_count = CustomerClue::where($clue_where)->count();
  685. $list[$key]['clue_count'] = $clue_count;
  686. // 浏览次数
  687. $foot_where[] = ['employee_id', '=', $empid];
  688. $foot_where[] = ['pipe_type', '=', $clue_type];
  689. $foot_where[] = ['reg_info', 'like', '{"id":' . $val['id'] . ',%'];
  690. $foot_user_count = Footprints::where($foot_where)->field('id,uid')->group('uid')->select();
  691. $list[$key]['footprints_user_count'] = count($foot_user_count);
  692. // 浏览时长
  693. $visit_long = Footprints::where($foot_where)->sum('visit_due_time');
  694. $list[$key]['visit_long'] = $visit_long;
  695. $where = [];
  696. $clue_where = [];
  697. $foot_where = [];
  698. }
  699. $count=VideoModel::where([['root_id','=',request()->employee->root_id],['id','in',$share_list]])->count();
  700. //$list = array_slice($list, ($param['page'] - 1) * $param['limit'],$param['limit']);
  701. return json(['code' => 0, 'msg' => '获取成功', 'data' => $list,'count'=>$count]);
  702. }
  703. //计算所有关联员工的视频id
  704. public function sel_emp_videolist($empid)
  705. {
  706. $clue_type = 'video';
  707. $share_type = 'Video';
  708. $where[] = ['type', '=', $share_type];
  709. $where[] = ['employee_id', '=', $empid];
  710. $shared_id = ShareLog::where($where)->group('data_id')->column('data_id');
  711. $clue_where[] = ['employee_id', '=', $empid];
  712. $clue_where[] = ['pipe_type', '=', $clue_type];
  713. $clue_id = CustomerClue::where($clue_where)->group('pipe_id')->column('pipe_id');
  714. $foot_where[] = ['employee_id', '=', $empid];
  715. $foot_where[] = ['pipe_type', '=', $clue_type];
  716. $foot_list = Footprints::where($foot_where)->field('id,pipe_type,reg_info')->select();
  717. $foot_id=[];
  718. foreach($foot_list as $key=>$val){
  719. $m=$val->getData('reg_info');
  720. if(!empty($m)){
  721. $info=json_decode($m,true);
  722. $foot_id[]=$info['id'];
  723. }
  724. }
  725. $vdid=array_values(array_unique(array_merge($shared_id,$clue_id,$foot_id)));
  726. return $vdid;
  727. }
  728. }