Article.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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\Article as ArticleModel;
  7. use app\model\VideoType;
  8. use app\model\Employee;
  9. use think\exception\HttpException;
  10. use wx\offiaccount\Crawler;
  11. use app\model\Company;
  12. use app\model\Org;
  13. use app\model\ShareLog;
  14. use app\model\Footprints;
  15. use app\model\CustomerClue;
  16. use app\logics\MaterialLogic;
  17. class Article
  18. {
  19. /**
  20. * 文章列表页面
  21. */
  22. public function index()
  23. {
  24. // 类型获取
  25. $type = VideoType::where(['pid' => 0, 'type' => 'article', 'root_id' => request()->employee->root_id])->column('*', 'id');
  26. $label = VideoType::where([['pid', '>', 0], ['type', '=', 'article'], ['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 = ArticleModel::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. if (!empty($param['title'])) {
  55. $where[] = ['title', 'like', '%' . $param['title'] . '%'];
  56. }
  57. if (!empty($param['type'])) {
  58. $where[] = ['type', '=', $param['type']];
  59. }
  60. if (!empty($param['employee_id'])) {
  61. $where[] = ['employee_id', '=', $param['employee_id']];
  62. }
  63. //上传时间筛选
  64. if (!empty($param['add_time'])) {
  65. $newtime=explode(' - ', $param['add_time']);
  66. $start_time=$newtime[0].' 00:00:00';
  67. $end_time=$newtime[1].' 23:59:59';
  68. $where[] = ['uploadtime', 'between', [$start_time, $end_time]];
  69. }
  70. $order='id desc,hot desc,star_num desc,got_customers_num desc';
  71. 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'])) {
  72. $order = $param['order'];
  73. }
  74. $data = $count = ArticleModel::field('id,cover_img,title,type,label,publish,uploadtime,employee_id,hot,visit_due_time,view_times,shared_times')->with(['employee'])->where($where);
  75. if (!empty($param['label'])) {
  76. $data = $count = $data->whereRaw("FIND_IN_SET(" . $param['label'] . " , label)");
  77. }
  78. // 获取数据
  79. $list = $data->page($param['page'], $param['limit'])->order($order)->select()->toArray();
  80. $count = $count->count();
  81. //增加查询转发次数浏览时长
  82. // if(!empty($list)){
  83. // $statismod=new MaterialLogic;
  84. // $selorder=!empty($param['order'])?$param['order']:'';
  85. // $list=$statismod->sel_case_share($list,'Article',$selorder);
  86. // }
  87. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  88. }
  89. /**
  90. * 文章添加视图
  91. */
  92. public function add()
  93. {
  94. // 类型获取
  95. $type = VideoType::where(['pid' => 0, 'type' => 'article', 'root_id' => request()->employee->root_id])->column('*', 'id');
  96. $label = VideoType::where([['pid', '>', 0], ['type', '=', 'article'], ['root_id', '=', request()->employee->root_id]])->select()->toArray();
  97. $joinlabel = [];
  98. foreach ($label as $val) {
  99. $joinlabel[$val['pid']][$val['id']] = $val['name'];
  100. }
  101. View::assign('type', $type);
  102. View::assign('label', json_encode($joinlabel));
  103. $adding = input('adding',0);
  104. view::assign('adding',$adding);
  105. return View::fetch();
  106. }
  107. /**
  108. * 文章修改视图
  109. */
  110. public function edit($id)
  111. {
  112. $data = ArticleModel::where([['id', '=', $id], ['root_id', '=', request()->employee->root_id]])->find();
  113. if (empty($data)) throw new HttpException(404, '数据不存在');
  114. $data = $data->getData();
  115. $data['cover_img'] = empty($data['cover_img']) ?: 'https://' . config('app.ali_oss_bindurl') . '/' . $data['cover_img'];
  116. $data['cover_share_img'] = empty($data['cover_share_img']) ?'': 'https://' . config('app.ali_oss_bindurl') . '/' . $data['cover_share_img'];
  117. // 类型获取
  118. $type = VideoType::where(['pid' => 0, 'type' => 'article', 'root_id' => request()->employee->root_id])->column('*', 'id');
  119. $label = VideoType::where([['pid', '>', 0], ['type', '=', 'article'], ['root_id', '=', request()->employee->root_id]])->select()->toArray();
  120. $joinlabel = [];
  121. foreach ($label as $val) {
  122. $joinlabel[$val['pid']][$val['id']] = $val['name'];
  123. }
  124. View::assign('data', $data);
  125. View::assign('type', $type);
  126. View::assign('checked', $joinlabel[$data['type']] ?? []);
  127. View::assign('label', json_encode($joinlabel));
  128. return View::fetch();
  129. }
  130. /**
  131. * 文章修改视图
  132. */
  133. public function preview_read()
  134. {
  135. $id = input('id',0);
  136. $data = ArticleModel::where([['id', '=', $id], ['root_id', '=', request()->employee->root_id]])->find();
  137. $data = $data->getData();
  138. $data['cover_img'] = empty($data['cover_img']) ?: 'https://' . config('app.ali_oss_bindurl') . '/' . $data['cover_img'];
  139. //公司信息
  140. $where[] = ['root_id','=',request()->employee->root_id];
  141. $data['company_info'] = Company::where($where)->field('company_name,logo,company_address')->find();
  142. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data]);
  143. }
  144. /**
  145. * 添加文章保存
  146. */
  147. public function save()
  148. {
  149. $data = Request::only(['id', 'cover_img', 'title', 'content', 'type', 'label'=>'', 'cover_share_img']);
  150. if (empty($data['id'])) {
  151. $data['root_id'] = request()->employee->root_id;
  152. $data['employee_id'] = request()->employee->id;
  153. ArticleModel::create($data);
  154. } else {
  155. // $data['employee_id'] = request()->employee->id;
  156. ArticleModel::where(['id' => $data['id'], 'root_id' => request()->employee->root_id])->save($data);
  157. }
  158. return json(['code' => 0, 'msg' => '操作成功']);
  159. }
  160. /**
  161. * 文章删除
  162. */
  163. public function del($id)
  164. {
  165. $rs = ArticleModel::update(['delete_time' => time()], ['id' => $id, 'root_id' => request()->employee->root_id]);
  166. if (!$rs) {
  167. return json(['code' => 1, 'msg' => '删除失败']);
  168. }
  169. return json(['code' => 0, 'msg' => '删除成功']);
  170. }
  171. /*
  172. * 添加分类
  173. */
  174. public function addtype($value, $id = null)
  175. {
  176. $array = ['name' => $value, 'type' => 'article'];
  177. if (isset($id)) {
  178. $array['pid'] = $id;
  179. }
  180. $array['root_id'] = request()->employee->root_id;
  181. $insertGetId = VideoType::insertGetId($array);
  182. $data = ['id' => $insertGetId, 'value' => $value];
  183. return json(['code' => 0, 'msg' => '添加成功', 'data' => $data]);
  184. }
  185. /**
  186. * 是否展示
  187. */
  188. public function publish($id)
  189. {
  190. $obj = ArticleModel::where(['id' => $id, 'root_id' => request()->employee->root_id])->find();
  191. if (empty($obj)) return json(['code' => 1, 'msg' => '文章不存在']);
  192. $obj->publish = $obj->publish == 1 ? 0 : 1;
  193. $obj->save();
  194. return json(['code' => 0, 'msg' => '修改成功']);
  195. }
  196. public function fileupload()
  197. {
  198. $ali_oss_bindurl = config('app.ali_oss_bindurl');
  199. $url = 'https://' . $ali_oss_bindurl . '/' . Request::param('file');
  200. return json(['code' => 0, 'data' => ['src' => $url]]);
  201. }
  202. public function filedelete()
  203. {
  204. $param = request()->param();
  205. foreach ($param as $path) {
  206. $path = str_replace('https://' . config('app.ali_oss_bindurl') . '/', '', $path);
  207. trace($path);
  208. filedelete($path);
  209. }
  210. return json(['code' => 0, 'msg' => '删除成功']);
  211. }
  212. /**
  213. * 网址内容抓取
  214. */
  215. public function catch($link)
  216. {
  217. $crawler = new Crawler();
  218. $data = $crawler->crawByUrl($link);
  219. View::assign('data', $data);
  220. return json(['code' => 0, 'data' => $data]);
  221. }
  222. /*
  223. * 分类管理
  224. */
  225. function cate_article()
  226. {
  227. return View::fetch();
  228. }
  229. /*
  230. * 分类管理列表
  231. */
  232. function cate_article_list()
  233. {
  234. $param = Request::param();
  235. $condition[] = ['root_id', '=', request()->employee->root_id];
  236. $condition[] = ['type', '=', 'article'];
  237. $condition[] = ['pid', '=', 0];
  238. $list = VideoType::where($condition)->page($param['page'], $param['limit'])->order(['show'=>'asc','id'=>'desc'])->field('id,name,show')->select()->toarray();
  239. $count = VideoType::where($condition)->count();
  240. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  241. }
  242. /*
  243. * 分类管理添加
  244. */
  245. function cate_article_add()
  246. {
  247. $param = Request::param();
  248. $condition[] = ['name', '=', $param['name']];
  249. $condition[] = ['root_id', '=', request()->employee->root_id];
  250. $condition[] = ['type', '=', 'article'];
  251. $data['name'] = $param['name'];
  252. $model = VideoType::where($condition);
  253. $models = new VideoType;
  254. $info = $model->find();
  255. if ($info) {
  256. return json(['code' => 1, 'msg' => '名称已存在']);
  257. }
  258. $models->name = $param['name'];
  259. $models->type = 'article';
  260. $models->root_id = request()->employee->root_id;
  261. $models->save();
  262. return json(['code' => 0, 'msg' => '保存成功', 'data' => '']);
  263. }
  264. /*
  265. * 分类管理编辑
  266. */
  267. function cate_article_edit()
  268. {
  269. $param = Request::param();
  270. $conditions[] = ['id', '=', $param['id']];
  271. $conditions[] = ['root_id', '=', request()->employee->root_id];
  272. $condition[] = ['name', '=', $param['name']];
  273. $condition[] = ['root_id', '=', request()->employee->root_id];
  274. $condition[] = ['type', '=', 'article'];
  275. $data['name'] = $param['name'];
  276. $model = VideoType::where($condition);
  277. $models = VideoType::where($conditions);
  278. $info = $model->find();
  279. if ($info) {
  280. return json(['code' => 1, 'msg' => '名称已存在']);
  281. }
  282. $models->update($data);
  283. return json(['code' => 0, 'msg' => '保存成功', 'data' => '']);
  284. }
  285. /*
  286. * 分类管理
  287. */
  288. function cate_article_label()
  289. {
  290. $id = input('id');
  291. view::assign('id', $id);
  292. return View::fetch();
  293. }
  294. /*
  295. * 分类管理列表
  296. */
  297. function cate_article_list_label()
  298. {
  299. $param = Request::param();
  300. $condition[] = ['root_id', '=', request()->employee->root_id];
  301. $condition[] = ['type', '=', 'article'];
  302. $condition[] = ['pid', '=', $param['id']];
  303. $list = VideoType::where($condition)->page($param['page'], $param['limit'])->order('id desc')->field('id,name')->select()->toArray();
  304. $count = VideoType::where($condition)->count();
  305. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  306. }
  307. /*
  308. * 分类管理添加
  309. */
  310. function cate_article_add_label()
  311. {
  312. $param = Request::param();
  313. $condition[] = ['name', '=', $param['name']];
  314. $condition[] = ['root_id', '=', request()->employee->root_id];
  315. $condition[] = ['type', '=', 'article'];
  316. $condition[] = ['pid', '=', $param['pid']];
  317. $condition[] = ['type', '=', 'article'];
  318. $data['name'] = $param['name'];
  319. $model = VideoType::where($condition);
  320. $models = new VideoType;
  321. $info = $model->find();
  322. if ($info) {
  323. return json(['code' => 1, 'msg' => '名称已存在']);
  324. }
  325. $models->name = $param['name'];
  326. $models->type = 'article';
  327. $models->pid = $param['pid'];
  328. $models->root_id = request()->employee->root_id;
  329. $models->save();
  330. return json(['code' => 0, 'msg' => '保存成功', 'data' => '']);
  331. }
  332. /*
  333. * 分类管理编辑
  334. */
  335. function cate_article_edit_label()
  336. {
  337. $param = Request::param();
  338. $conditions[] = ['id', '=', $param['id']];
  339. $conditions[] = ['root_id', '=', request()->employee->root_id];
  340. $condition[] = ['name', '=', $param['name']];
  341. $condition[] = ['root_id', '=', request()->employee->root_id];
  342. $condition[] = ['pid', '=', $param['pid']];
  343. $condition[] = ['type', '=', 'article'];
  344. $data['name'] = $param['name'];
  345. $model = VideoType::where($condition);
  346. $models = VideoType::where($conditions);
  347. $info = $model->find();
  348. if ($info) {
  349. return json(['code' => 1, 'msg' => '名称已存在']);
  350. }
  351. $models->update($data);
  352. return json(['code' => 0, 'msg' => '保存成功', 'data' => '']);
  353. }
  354. /*
  355. * 标签管理
  356. */
  357. function cate_article_labels()
  358. {
  359. return View::fetch();
  360. }
  361. /*
  362. * 新标签管理列表
  363. */
  364. function cate_article_list_labels()
  365. {
  366. $param = Request::param();
  367. $condition[] = ['root_id', '=', request()->employee->root_id];
  368. $condition[] = ['type', '=', 'article'];
  369. $condition[] = ['pid', '>', 0];
  370. $parents = VideoType::where([['pid', '=', 0]])->column('name', 'id');
  371. $list = VideoType::where($condition)->page($param['page'], $param['limit'])->order(['show'=>'asc','id'=>'desc'])->field('id,name,pid,show')->select()->toarray();
  372. foreach ($list as $k => $v) {
  373. $list[$k]['pname'] = isset($parents[$v['pid']]) ? $parents[$v['pid']] : '';
  374. }
  375. $count = VideoType::where($condition)->count();
  376. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  377. }
  378. /*
  379. * 新分类标签管理编辑
  380. */
  381. function cate_article_edit_labels()
  382. {
  383. $param = Request::param();
  384. $conditions[] = ['id', '=', $param['id']];
  385. $conditions[] = ['root_id', '=', request()->employee->root_id];
  386. $condition[] = ['name', '=', $param['name']];
  387. $condition[] = ['root_id', '=', request()->employee->root_id];
  388. $condition[] = ['pid', '=', $param['pid']];
  389. $condition[] = ['type', '=', 'article'];
  390. $data['name'] = $param['name'];
  391. $model = VideoType::where($condition);
  392. $models = VideoType::where($conditions);
  393. $info = $model->find();
  394. if ($info) {
  395. return json(['code' => 1, 'msg' => '名称已存在']);
  396. }
  397. $models->update($data);
  398. return json(['code' => 0, 'msg' => '保存成功', 'data' => '']);
  399. }
  400. /*
  401. * 新标签添加
  402. */
  403. function cate_article_labels_add()
  404. {
  405. if (request()->isAjax()) {
  406. $param = Request::param();
  407. if (!$param['id'] || !$param['name']) {
  408. return json(['code' => 1, 'msg' => '缺少参数', 'data']);
  409. }
  410. isset($param['label_id']) ? $condition[] = ['id', '<>', $param['label_id']] : '';
  411. $condition[] = ['name', '=', $param['name']];
  412. $condition[] = ['root_id', '=', request()->employee->root_id];
  413. $condition[] = ['pid', '=', $param['id']];
  414. $condition[] = ['type', '=', 'article'];
  415. $info = VideoType::where($condition)->find();
  416. if ($info) {
  417. return json(['code' => 1, 'msg' => '名称已存在']);
  418. }
  419. if(isset($param['label_id']))
  420. {
  421. VideoType::where(['root_id'=>request()->employee->root_id,'type'=>'article','id'=>$param['label_id']])->update(['pid'=>$param['id'],'name'=>$param['name']]);
  422. ArticleModel::where(['root_id'=>request()->employee->root_id])->whereRaw("FIND_IN_SET(" . $param['label_id'] . " , label)")->update(['type'=>$param['id']]);
  423. //print_r(ArticleModel::where(['root_id'=>request()->employee->root_id])->whereRaw("FIND_IN_SET(" . $param['label_id'] . " , label)")->select()->toArray());exit;
  424. }else{
  425. $model = new VideoType;
  426. $model->name = $param['name'];
  427. $model->pid = $param['id'];
  428. $model->type = 'article';
  429. $model->root_id = request()->employee->root_id;
  430. $model->save();
  431. }
  432. return json(['code' => 0, 'msg' => '保存成功', 'data' => '']);
  433. }
  434. //编辑
  435. if(Request::param('id')){
  436. $data = Request::param();
  437. view::assign('data', $data);
  438. }
  439. $condition[] = ['root_id', '=', request()->employee->root_id];
  440. $condition[] = ['type', '=', 'article'];
  441. $condition[] = ['pid', '=', 0];
  442. $list = VideoType::where($condition)->order('id desc')->field('id,name')->select()->toArray();
  443. view::assign('list', $list);
  444. return view::fetch();
  445. }
  446. /*
  447. * 文章标签分类列表
  448. */
  449. public function label_list()
  450. {
  451. $id = request()->param('id');
  452. $zid = request()->param('zid'); //文章id
  453. $lid = request()->param('lid'); //需要选中的标签id
  454. $data = [];
  455. if ($id) {
  456. $types = VideoType::where([['pid', '=', $id]])->select()->toArray();
  457. $zids = [];
  458. if ($zid) {
  459. $zids = ArticleModel::where([['id', '=', $zid]])->value('label');
  460. $zids = $zids ? explode(',', $zids) : [];
  461. }
  462. foreach ($types as $k => $v) {
  463. $data[] = [
  464. 'name' => $v['name'],
  465. 'value' => $v['id'],
  466. 'selected' => (in_array($v['id'], $zids) || $v['id']==$lid) ? true : false
  467. ];
  468. }
  469. }
  470. return json($data);
  471. }
  472. /*
  473. * 删除标签或分类前,查询关联的内容数量
  474. */
  475. public function with_type_count()
  476. {
  477. $param = Request::param();
  478. if($param['type'] == 'type')
  479. {
  480. $field = 'type';
  481. }else{
  482. $field = 'label';
  483. }
  484. $where = [
  485. ['root_id','=',request()->employee->root_id],
  486. ['delete_time','=',0],
  487. [$field,'=',$param['id']]
  488. ];
  489. $count = ArticleModel::where($where)->count();
  490. if($count > 0)
  491. {
  492. $type = $field == 'type' ? '分类' : '标签' ;
  493. $msg = '该'.$type.'有'.$count.'个'.'拓客图文,删除后该内容将归为其它分类。';
  494. }else{
  495. $msg = '确定删除该分类吗?';
  496. }
  497. return $msg;
  498. }
  499. /*
  500. * 分类、标签删除
  501. */
  502. public function delete_type_or_label()
  503. {
  504. $param = Request::param();
  505. $articleType = VideoType::where(['root_id'=>request()->employee->root_id,'type'=>'article','id'=>$param['id']])->find();
  506. if(empty($articleType)) return json(['code' => 1, 'msg' => '数据不存在']);
  507. Db::startTrans();
  508. try {
  509. if($param['type'] == 'type')
  510. {
  511. //查询是否存在其它分类
  512. $type_id = VideoType::where(['root_id'=>request()->employee->root_id,'pid'=>0,'type'=>'article','show'=>1])->value('id');
  513. if(empty($type_id)) $type_id = VideoType::insertGetId(['pid'=>0,'name'=>'其它','type'=>'article','root_id'=>request()->employee->root_id,'show'=>1]);
  514. //更新关联内容
  515. VideoType::where(['root_id'=>request()->employee->root_id,'pid'=>$param['id'],'type'=>'article'])->update(['pid'=>$type_id]);
  516. ArticleModel::where(['root_id'=>request()->employee->root_id,'type'=>$param['id']])->update(['type'=>$type_id]);
  517. $more_other_id = VideoType::where(['root_id'=>request()->employee->root_id,'pid'=>$type_id,'type'=>'article','show'=>1])->column('id');
  518. if(count($more_other_id) >= 2)
  519. {
  520. $first_id = $more_other_id[0];
  521. ArticleModel::where([['root_id','=',request()->employee->root_id],['type','=',$type_id],['label','in',$more_other_id]])->update(['label'=>$first_id]);
  522. unset($more_other_id[0]);
  523. VideoType::where([['root_id','=',request()->employee->root_id],['id','in',$more_other_id]])->delete();
  524. }
  525. //删除分类
  526. $articleType->delete();
  527. }else{
  528. $label_id = VideoType::where(['root_id'=>request()->employee->root_id,'pid'=>$param['pid'],'type'=>'article','show'=>1])->value('id');
  529. if(empty($label_id)) $label_id = VideoType::insertGetId(['pid'=>$param['pid'],'name'=>'其它','type'=>'article','root_id'=>request()->employee->root_id,'show'=>1]);
  530. //更新关联内容
  531. ArticleModel::where(['root_id'=>request()->employee->root_id,'label'=>$param['id']])->update(['label'=>$label_id]);
  532. //删除分类
  533. $articleType->delete();
  534. }
  535. Db::commit();
  536. return json(['code' => 0, 'msg' => '删除成功']);
  537. } catch (\Exception $e) {
  538. Db::rollback();
  539. return json(['code' => 1, 'msg' => '删除失败']);
  540. }
  541. }
  542. //数据统计
  543. public function statistics_data_list()
  544. {
  545. $request = request();
  546. $param=$request->only(['page'=>1,'limit'=>10,'org_id','time']);
  547. if (!request()->isAjax()) {
  548. $list=orgSubIds(request()->employee->root_id);
  549. $xins=[];
  550. foreach($list as $key=>$val){
  551. if(Org::where([['pid','=',$val],['status','=',1]])->value('id')){
  552. $xins[]=$val;
  553. }
  554. }
  555. $orglist=Org::where([['id','in',$xins]])->field('id,name,pid')->select()->toArray();
  556. view::assign('orglist', $orglist);
  557. return view::fetch();
  558. }
  559. if(!empty($param['org_id'])){
  560. $list=orgSubIds($param['org_id']);
  561. }else{
  562. $list=orgSubIds(request()->employee->root_id);
  563. }
  564. $xin=[];
  565. foreach($list as $key=>$val){
  566. if(!Org::where([['pid','=',$val],['status','=',1]])->value('id')){
  567. $xin[]=$val;
  568. }
  569. }
  570. $newlist = Org::with(['employee' => function ($sql) {
  571. $sql->where([['state', '=', '在职'], ['uid', '<>', 0]])->field('id,org_id');
  572. }])->where([['id', 'in', $xin]])->field('id,name,pid')->page($param['page'], $param['limit'])->select()->toArray();
  573. $clue_type = 'article';
  574. $share_type = 'Article';
  575. $orgid = array_column($newlist, 'id');
  576. $empid = Employee::where([['org_id', 'in', $orgid], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  577. //转发人数
  578. if (!empty($param['time'])) {
  579. $newtime = explode(' - ', $param['time']);
  580. $start_time = $newtime[0] . ' 00:00:00';
  581. $end_time = $newtime[1] . ' 23:59:59';
  582. $where[] = ['share_time', 'between', [$start_time, $end_time]];
  583. $clue_where[] = ['addtime', 'between', [$start_time, $end_time]];
  584. $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
  585. }
  586. $where[] = ['type', '=', $share_type];
  587. $where[] = ['employee_id', 'in', $empid];
  588. $clue_where[] = ['employee_id', 'in', $empid];
  589. $clue_where[] = ['pipe_type', '=', $clue_type];
  590. $foot_where[] = ['employee_id', 'in', $empid];
  591. $foot_where[] = ['pipe_type', '=', $clue_type];
  592. $shared_employee = ShareLog::where($where)->column('employee_id');
  593. $clue_count = CustomerClue::where($clue_where)->column('employee_id');
  594. $foot_data = Footprints::where($foot_where)->field('id,employee_id,uid,visit_due_time')->select()->toArray();
  595. $new = [];
  596. foreach ($foot_data as $key => $val) {
  597. $new[$val['employee_id']][] = $val;
  598. }
  599. foreach ($newlist as $key => $val) {
  600. $empids = array_column($val['employee'], 'id');
  601. $newlist[$key]['share_count'] = count(array_intersect($shared_employee, $empids));
  602. $yes_share_count = count(array_unique(array_intersect($shared_employee, $empids)));
  603. $newlist[$key]['share_employee_count'] = $yes_share_count;
  604. $newlist[$key]['clue_count'] = count(array_intersect($clue_count, $empids));
  605. $cont = $visit_due_time = $uid = 0;
  606. foreach ($new as $k => $v) {
  607. if (in_array($k, $empids)) {
  608. $cont += count($v);
  609. $visit_due_time += array_sum(array_column($v, 'visit_due_time'));
  610. $uid += count(array_unique(array_column($v, 'uid')));
  611. }
  612. }
  613. $newlist[$key]['footprints_count'] = $cont;
  614. $newlist[$key]['visit_long'] = $visit_due_time;
  615. $newlist[$key]['footprints_user_count'] = $uid;
  616. $un_share_employee = count($empids) - $yes_share_count;
  617. $newlist[$key]['un_share_employee_count'] = $un_share_employee;
  618. }
  619. $count = Org::where([['id', 'in', $xin]])->count();
  620. $all = $this->top_statistics_data($xin, !empty($param['time']) ? $param['time'] : 0);
  621. $data = ['list' => $newlist, 'all' => $all];
  622. return json(['code' => 0, 'msg' => '获取成功', 'data' => $data, 'count' => $count]);
  623. }
  624. public function top_statistics_data($xin,$time=0)
  625. {
  626. $article_cont=ArticleModel::where([['delete_time', '=', 0],['root_id', '=', request()->employee->root_id]])->count();
  627. $all=['article_cont'=>$article_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];
  628. $sub_employee_ids = Employee::where([['org_id', 'in', $xin], ['state', '=', '在职'], ['uid', '<>', 0]])->column('id');
  629. $clue_type = 'article';
  630. $share_type = 'Article';
  631. //转发人数
  632. if(!empty($time)){
  633. $newtime=explode(' - ', $time);
  634. $start_time=$newtime[0].' 00:00:00';
  635. $end_time=$newtime[1].' 23:59:59';
  636. $where[] = ['share_time', 'between', [$start_time, $end_time]];
  637. $clue_where[] = ['addtime', 'between', [$start_time, $end_time]];
  638. $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
  639. }
  640. $where[] = ['type', '=', $share_type];
  641. $where[] = ['employee_id', 'in', $sub_employee_ids];
  642. $shared_employee = ShareLog::where($where)->column('employee_id');
  643. // 转发次数
  644. $all['share_count'] = count($shared_employee);
  645. $all['share_employee_count']=count(array_values(array_unique($shared_employee)));
  646. // 获得线索
  647. $clue_where[] = ['employee_id', 'in', $sub_employee_ids];
  648. $clue_where[] = ['pipe_type', '=', $clue_type];
  649. $clue_count = CustomerClue::where($clue_where)->count();
  650. $all['clue_count']=$clue_count;
  651. // 浏览次数
  652. $foot_where[] = ['employee_id', 'in', $sub_employee_ids];
  653. $foot_where[] = ['pipe_type', '=', $clue_type];
  654. $foot_user_count = Footprints::where($foot_where)->field('id,uid,visit_due_time')->select()->toArray();
  655. $all['footprints_user_count']=count(array_unique(array_column($foot_user_count,'uid')));
  656. // 浏览时长
  657. $all['visit_long'] = array_sum(array_column($foot_user_count,'visit_due_time'));
  658. return $all;
  659. }
  660. //统计员工列表
  661. public function statistics_emplist()
  662. {
  663. $request = request();
  664. $param=$request->only(['page'=>1,'limit'=>10,'keyword','org_id','time']);
  665. if (!request()->isAjax()) {
  666. view::assign('org_id', $param['org_id']);
  667. return view::fetch();
  668. }
  669. $e_where[]=['org_id', '=', $param['org_id']];
  670. $e_where[]=['state', '=', '在职'];
  671. $e_where[]=['uid', '<>', 0];
  672. if(!empty($param['keyword'])){
  673. $e_where[]=['name','like','%'.$param['keyword'].'%'];
  674. }
  675. $employee_list = Employee::where($e_where)->field('id,name')->page($param['page'],$param['limit'])->select()->toArray();
  676. $clue_type = 'article';
  677. $share_type = 'Article';
  678. $empid=array_column($employee_list,'id');
  679. if(!empty($param['time'])){
  680. $newtime=explode(' - ', $param['time']);
  681. $start_time=$newtime[0].' 00:00:00';
  682. $end_time=$newtime[1].' 23:59:59';
  683. $where[] = ['share_time', 'between', [$start_time, $end_time]];
  684. $clue_where[] = ['addtime', 'between', [$start_time, $end_time]];
  685. $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
  686. }
  687. $where[]=['type', '=', $share_type];
  688. $where[]=['employee_id', 'in', $empid];
  689. $clue_where[]=['employee_id', 'in', $empid];
  690. $clue_where[]=['pipe_type', '=', $clue_type];
  691. $foot_where[] = ['employee_id', 'in', $empid];
  692. $foot_where[] = ['pipe_type', '=', $clue_type];
  693. $shared_employee = ShareLog::where($where)->column('employee_id');
  694. $clue_count = CustomerClue::where($clue_where)->column('employee_id');
  695. $foot_data = Footprints::where($foot_where)->field('id,employee_id,uid,visit_due_time')->select()->toArray();
  696. $new=[];
  697. foreach($foot_data as $key=>$val){
  698. $new[$val['employee_id']][]=$val;
  699. }
  700. foreach($employee_list as $key=>$val){
  701. $empids=[$val['id']];
  702. $employee_list[$key]['share_count']=count(array_intersect($shared_employee,$empids));
  703. $yes_share_count=count(array_unique(array_intersect($shared_employee,$empids)));
  704. $employee_list[$key]['share_employee_count']=$yes_share_count;
  705. $employee_list[$key]['clue_count']=count(array_intersect($clue_count,$empids));
  706. $cont=$visit_due_time=$uid=0;
  707. foreach($new as $k=>$v){
  708. if(in_array($k,$empids)){
  709. $cont+=count($v);
  710. $visit_due_time+=array_sum(array_column($v,'visit_due_time'));
  711. $uid+=count(array_unique(array_column($v,'uid')));
  712. }
  713. }
  714. $employee_list[$key]['footprints_count']=$cont;
  715. $employee_list[$key]['visit_long']=$visit_due_time;
  716. $employee_list[$key]['footprints_user_count']=$uid;
  717. $un_share_employee = count($empids) - $yes_share_count;
  718. $employee_list[$key]['un_share_employee_count'] = $un_share_employee;
  719. }
  720. $count=Employee::where($e_where)->count();
  721. return json(['code' => 0, 'msg' => '获取成功', 'data' => $employee_list,'count'=>$count]);
  722. }
  723. //统计员工转发记录详情列表
  724. public function emp_logdetail_list()
  725. {
  726. $request = request();
  727. $param=$request->only(['page'=>1,'limit'=>10,'keyword','emp_id','time']);
  728. if (!request()->isAjax()) {
  729. view::assign('emp_id', $param['emp_id']);
  730. return view::fetch();
  731. }
  732. $empid=$param['emp_id'];
  733. $share_list=$this->sel_emp_videolist($empid);
  734. if(!empty($param['keyword'])){
  735. $key_list=ArticleModel::where([['root_id','=',request()->employee->root_id],['title','like','%'.$param['keyword'].'%']])->column('id');
  736. if(!empty($key_list)&&!empty($share_list)){
  737. $tm= array_intersect($key_list,$share_list);
  738. if(!empty($tm)){
  739. $share_list=$tm;
  740. }else{
  741. return json(['code' => 0, 'msg' => '获取成功', 'data' => [],'count'=>0]);
  742. }
  743. }else{
  744. return json(['code' => 0, 'msg' => '获取成功', 'data' => [],'count'=>0]);
  745. }
  746. }
  747. $list=ArticleModel::where([['root_id','=',request()->employee->root_id],['id','in',$share_list]])->field('id,title')->page($param['page'],$param['limit'])->select()->toArray();
  748. $clue_type = 'article';
  749. $share_type = 'Article';
  750. foreach($list as $key=>$val){
  751. //转发人数
  752. if(!empty($param['time'])){
  753. $newtime=explode(' - ', $param['time']);
  754. $start_time=$newtime[0].' 00:00:00';
  755. $end_time=$newtime[1].' 23:59:59';
  756. $where[] = ['share_time', 'between', [$start_time, $end_time]];
  757. $clue_where[] = ['addtime', 'between', [$start_time, $end_time]];
  758. $foot_where[] = ['addtime', 'between', [$start_time, $end_time]];
  759. }
  760. $where[] = ['type', '=', $share_type];
  761. $where[] = ['employee_id', '=', $empid];
  762. $where[] = ['data_id','=',$val['id']];
  763. $list[$key]['share_count'] = ShareLog::where($where)->count();
  764. // 获得线索
  765. $clue_where[] = ['employee_id', '=', $empid];
  766. $clue_where[] = ['pipe_type', '=', $clue_type];
  767. $clue_where[] = ['pipe_id','=',$val['id']];
  768. $clue_count = CustomerClue::where($clue_where)->count();
  769. $list[$key]['clue_count'] = $clue_count;
  770. // 浏览次数
  771. $foot_where[] = ['employee_id', '=', $empid];
  772. $foot_where[] = ['pipe_type', '=', $clue_type];
  773. $foot_where[] = ['reg_info', 'like', '{"id":' . $val['id'] . ',%'];
  774. $foot_user_count = Footprints::where($foot_where)->field('id,uid')->group('uid')->select();
  775. $list[$key]['footprints_user_count'] = count($foot_user_count);
  776. // 浏览时长
  777. $visit_long = Footprints::where($foot_where)->sum('visit_due_time');
  778. $list[$key]['visit_long'] = $visit_long;
  779. $where = [];
  780. $clue_where = [];
  781. $foot_where = [];
  782. }
  783. $count=ArticleModel::where([['root_id','=',request()->employee->root_id],['id','in',$share_list]])->count();
  784. //$list = array_slice($list, ($param['page'] - 1) * $param['limit'],$param['limit']);
  785. return json(['code' => 0, 'msg' => '获取成功', 'data' => $list,'count'=>$count]);
  786. }
  787. //计算所有关联员工的id
  788. public function sel_emp_videolist($empid)
  789. {
  790. $clue_type = 'article';
  791. $share_type = 'Article';
  792. $where[] = ['type', '=', $share_type];
  793. $where[] = ['employee_id', '=', $empid];
  794. $shared_id = ShareLog::where($where)->group('data_id')->column('data_id');
  795. $clue_where[] = ['employee_id', '=', $empid];
  796. $clue_where[] = ['pipe_type', '=', $clue_type];
  797. $clue_id = CustomerClue::where($clue_where)->group('pipe_id')->column('pipe_id');
  798. $foot_where[] = ['employee_id', '=', $empid];
  799. $foot_where[] = ['pipe_type', '=', $clue_type];
  800. $foot_list = Footprints::where($foot_where)->field('id,pipe_type,reg_info')->select();
  801. $foot_id=[];
  802. foreach($foot_list as $key=>$val){
  803. $m=$val->getData('reg_info');
  804. if(!empty($m)){
  805. $info=json_decode($m,true);
  806. $foot_id[]=$info['id'];
  807. }
  808. }
  809. $vdid=array_values(array_unique(array_merge($shared_id,$clue_id,$foot_id)));
  810. return $vdid;
  811. }
  812. }