User.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. <?php
  2. namespace Share\Src;
  3. use Dever;
  4. class User
  5. {
  6. const INFO = 'set/info:applet';
  7. const SHARE = 'comment/share:applet';
  8. const USER = 'passport/user:applet';
  9. const NEWS = 'content/news:applet';
  10. const COURSE = 'content/course:applet';
  11. const MEETING = 'content/meeting:applet';
  12. const SERVICE = 'content/service:applet';
  13. const ACTIVE = 'activity/active:applet';
  14. const REFLUX = 'comment/share_reflux:applet';
  15. const SHARELOG = 'comment/share_log:applet';
  16. const SHAREGROUP = 'comment/share_group:applet';
  17. /**
  18. * 小程序列表
  19. *
  20. * @return mixed
  21. */
  22. public function applet()
  23. {
  24. $info = Dever::db(self::INFO);
  25. $data = $info->state();
  26. return $data;
  27. }
  28. /**
  29. * 导入excel
  30. *
  31. * @return mixed
  32. */
  33. public function import()
  34. {
  35. $file = Dever::data() . 'user.xlsx';
  36. $reader = new \PHPExcel_Reader_Excel2007();
  37. if(!$reader->canRead($file)){
  38. $reader = new \PHPExcel_Reader_Excel5();
  39. if(!$reader->canRead($file)){
  40. echo 'no Excel';
  41. return ;
  42. }
  43. }
  44. $xls = $reader->load($file);
  45. $currentSheet = $xls->getSheet(0);
  46. $column = $currentSheet->getHighestColumn();
  47. $row = $currentSheet->getHighestRow();
  48. $group = $name = $wechat = array();
  49. for ($curRow = 5;$curRow <= $row; $curRow++) {
  50. for ($curColumn = 'A';$curColumn<= $column; $curColumn++) {
  51. $value = $currentSheet->getCellByColumnAndRow(ord($curColumn) - 65,$curRow)->getValue();
  52. if ($curColumn == 'B') {
  53. //分组
  54. if ($value) {
  55. $group_id = Dever::upinto('share/group', array('name' => $value), array('name' => $value));
  56. }
  57. } elseif ($curColumn == 'C') {
  58. //姓名
  59. if ($value && isset($group_id) && $group_id) {
  60. Dever::upinto('share/user', array('name' => $value), array('group_id' => $group_id, 'name' => $value));
  61. }
  62. } elseif ($curColumn == 'D') {
  63. //微信
  64. $wechat[$value] = $value;
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * 导出成excel
  71. *
  72. * @return mixed
  73. */
  74. public function out()
  75. {
  76. $applet = $this->applet();
  77. $group = Dever::db('share/group')->state();
  78. $user = Dever::db('share/user')->getAll();
  79. $day = Dever::input('day');
  80. if (!$day) {
  81. $day = date('Y-m-d', time()-3600*24);
  82. }
  83. $start = Dever::maketime(date('Y-m-01 00:00:00', Dever::maketime($day)));
  84. $end = Dever::maketime(date('Y-m-t 23:59:59', Dever::maketime($day)));
  85. $log = Dever::db('share/log')->state();
  86. $i = 0;
  87. foreach ($user as $k => $v) {
  88. if (!$v['wechat_name']) {
  89. unset($user[$k]);
  90. continue;
  91. }
  92. $user[$k]['group'] = $group[$v['group_id']]['name'];
  93. $user[$k]['info'] = Dever::db('share/info')->one(array('share_day' => $day, 'user_id' => $v['id']));
  94. if (!$user[$k]['info']) {
  95. $user[$k]['info']['share_article_num'] = 0;
  96. $user[$k]['applet'] = '未知';
  97. } else {
  98. $user[$k]['applet'] = array();
  99. if ($user[$k]['info']['applet']) {
  100. $temp = explode(',', $user[$k]['info']['applet']);
  101. foreach ($temp as $k1 => $v1) {
  102. $user[$k]['applet'][$k1] = $applet[$v1]['name'];
  103. }
  104. }
  105. $user[$k]['applet'] = implode("\r\n", $user[$k]['applet']);
  106. }
  107. $user[$k]['log'] = Dever::db('share/log')->state(array('share_day' => $day, 'user_id' => $v['id']));
  108. $total = Dever::db('share/log')->month(array('start' => $start, 'end' => $end, 'user_id' => $v['id']));
  109. $user[$k]['total'] = 0;
  110. if ($total) {
  111. $user[$k]['total'] = $total['num'];
  112. }
  113. if (!$user[$k]['total']) {
  114. $user[$k]['total'] = 0;
  115. }
  116. $user[$k]['status'] = '已达标';
  117. if ($user[$k]['info']['share_article_num'] < 3) {
  118. $user[$k]['status'] = '未达标';
  119. }
  120. if ($user[$k]['name'] && $user[$k]['info']) {
  121. $data[$i] = array
  122. (
  123. $i+1,
  124. $user[$k]['group'],
  125. $user[$k]['name'],
  126. $user[$k]['wechat_name'],
  127. $user[$k]['applet'],
  128. $user[$k]['info']['share_article_num'],
  129. $user[$k]['total'],
  130. $user[$k]['status'],
  131. );
  132. $i++;
  133. }
  134. }
  135. $header = array
  136. (
  137. '序号', '业务板块', '姓名', '微信昵称', '负责小程序名称', '分享文章数', '月总回流', '是否达标'
  138. );
  139. $file = $day . '_小程序推广';
  140. if (Dever::input('test')) {
  141. print_r($header);
  142. print_r($data);die;
  143. }
  144. Dever::excelExport($data, $header, $file, $sheet = 0, $sheetName = $day);
  145. return;
  146. }
  147. public function test()
  148. {
  149. $name = '姓名';
  150. $header = array
  151. (
  152. $name,'手机号'
  153. );
  154. $data = array
  155. (
  156. array
  157. (
  158. '于斌', '15810090845'
  159. ),
  160. array
  161. (
  162. '3', '4'
  163. ),
  164. );
  165. Dever::excelExport($data, $header, $filename = 'test');
  166. }
  167. /**
  168. * 获取数据
  169. *
  170. * @return mixed
  171. */
  172. public function data()
  173. {
  174. $user = Dever::db('share/user')->state();
  175. foreach ($user as $k => $v) {
  176. if ($v['wechat_name']) {
  177. $user[$k] = $this->userData($v);
  178. }
  179. }
  180. return true;
  181. }
  182. public function userData($data)
  183. {
  184. if ($data['uid']) {
  185. $where = array
  186. (
  187. 'option' => array
  188. (
  189. 'id' => array('yes', 'in'),
  190. ),
  191. 'id' => explode(',', $data['uid']),
  192. );
  193. $user = Dever::db(self::USER)->state($where);
  194. } else {
  195. $user = Dever::db(self::USER)->state(array('name' => $data['wechat_name']));
  196. }
  197. $ids = array();
  198. if ($user) {
  199. $ids = array_keys($user);
  200. }
  201. if ($ids) {
  202. $user = array();
  203. $option = array
  204. (
  205. 'share_id' => 'yes',
  206. 'uid' => array('yes', 'in'),
  207. 'start' => array('yes-cdate', '>='),
  208. 'end' => array('yes-cdate', '<='),
  209. );
  210. //获取昨天数据
  211. $time = time() - (3600 * 24);
  212. $set = Dever::input('set');
  213. if ($set) {
  214. $time = Dever::maketime($set);
  215. }
  216. $day = date('Y-m-d', $time);
  217. $day_prev = date('Y-m-d', strtotime("$day -1 day"));//前天
  218. $start = date('Y-m-d 00:00:00', $time);
  219. $end = date('Y-m-d 23:59:59', $time);
  220. $day_start = Dever::maketime($start);
  221. $day_end = Dever::maketime($end);
  222. $where = array
  223. (
  224. 'option' => $option,
  225. 'uid' => $ids,
  226. 'start' => $day_start,
  227. 'end' => $day_end
  228. );
  229. $share = Dever::db(self::SHARE)->state($where);
  230. if ($share) {
  231. $user['applet'] = array();
  232. //$user['applet'] = $data['applet'];
  233. $user['user_id'] = $data['id'];
  234. $user['share_day'] = $day;
  235. $user['share_num'] = 0;
  236. //$user['share_reflux_num'] = Dever::db(self::REFLUX)->total(array('source_uid' => $user['id']));
  237. //获取月度回流
  238. unset($where['option']['uid']);
  239. $where['option']['source_uid'] = array('yes', 'in');
  240. $where['source_uid'] = $ids;
  241. //$user['share_reflux_num'] = Dever::db(self::REFLUX)->total($where);
  242. $user['share_group_num'] = $user['share_reflux_num'] = 0;
  243. $article = array();
  244. foreach ($share as $k => $v) {
  245. $info = $this->shareData($v, $where, $article);
  246. if ($info) {
  247. $user['applet'][$info['applet']['id']] = $info['applet']['id'];
  248. $article[$info['source_table'] . '_' . $info['id']] = 1;
  249. $user['share'][$k] = $info;
  250. $user['share_num'] += $user['share'][$k]['num'];
  251. $user['share_group_num'] += $user['share'][$k]['group_num'];
  252. $user['share_reflux_num'] += $user['share'][$k]['reflux_num'];
  253. }
  254. }
  255. $user['applet'] = implode(',', $user['applet']);
  256. $user['share_article_num'] = count($article);
  257. $this->updateInfo($user, $day_prev);
  258. }
  259. }
  260. return $user;
  261. }
  262. public function shareData($data, $where)
  263. {
  264. $result = array();
  265. $table = false;
  266. switch ($data['source_table']) {
  267. case 1:
  268. $table = self::NEWS;
  269. break;
  270. case 2:
  271. $table = self::COURSE;
  272. break;
  273. case 3:
  274. $table = self::MEETING;
  275. break;
  276. case 5:
  277. $table = self::SERVICE;
  278. break;
  279. case 8:
  280. $table = self::ACTIVE;
  281. break;
  282. }
  283. if ($table && $data['source_id'] > 0) {
  284. $info = Dever::db($table)->one($data['source_id']);
  285. if ($info) {
  286. $result['applet'] = Dever::db(self::INFO)->one($data['info_id']);
  287. $result['source_table'] = $data['source_table'];
  288. $result['id'] = $info['id'];
  289. $result['name'] = $info['name'];
  290. $result['time'] = date('Y-m-d H:i:s', $data['cdate']);
  291. $result['cdate'] = $data['cdate'];
  292. $result['num'] = $data['num'];
  293. $result['group_num'] = $data['group_num'];
  294. $where['share_id'] = $data['id'];
  295. $result['reflux_num'] = Dever::db(self::REFLUX)->total($where);
  296. unset($where['option']['source_uid']);
  297. $result['num'] = Dever::db(self::SHARELOG)->total($where);
  298. $result['group_num'] = Dever::db(self::SHAREGROUP)->total($where);
  299. if ($result['num'] <= 0) {
  300. $result['reflux_num'] = 0;
  301. }
  302. if ($result['group_num'] > $result['num']) {
  303. $result['num'] = $result['group_num'];
  304. }
  305. }
  306. }
  307. return $result;
  308. }
  309. public function updateInfo($user, $day_prev)
  310. {
  311. # 获取上个月的数据 减去上个月的数据
  312. /*
  313. if ($user['share_day'] != $day_prev) {
  314. $where = array();
  315. $where['option_user_id'] = $user['user_id'];
  316. $where['option_share_day'] = $day_prev;
  317. $prev = Dever::db('share/info')->one($where);
  318. if ($prev) {
  319. $user['share_num'] = $user['share_num'] - $prev['share_num'];
  320. $user['share_group_num'] = $user['share_group_num'] - $prev['share_group_num'];
  321. }
  322. }*/
  323. $update = array();
  324. $update['user_id'] = $user['user_id'];
  325. $update['applet'] = $user['applet'];
  326. $update['share_day'] = $user['share_day'];
  327. $update['share_num'] = $user['share_num'];
  328. $update['share_reflux_num'] = $user['share_reflux_num'];
  329. $update['share_article_num'] = $user['share_article_num'];
  330. $update['share_group_num'] = $user['share_group_num'];
  331. $update['status'] = 1;
  332. if ($update['share_article_num'] >= 3) {
  333. $update['status'] = 2;
  334. }
  335. $where = array();
  336. $where['option_user_id'] = $update['user_id'];
  337. $where['option_share_day'] = $update['share_day'];
  338. $info = Dever::db('share/info')->one($where);
  339. if (!$info) {
  340. $id = Dever::db('share/info')->insert($update);
  341. } else {
  342. $id = $info['id'];
  343. $update['where_id'] = $id;
  344. Dever::db('share/info')->update($update);
  345. }
  346. if ($id > 0 && isset($user['share'])) {
  347. $user['share_num'] = 0;
  348. $user['share_group_num'] = 0;
  349. foreach ($user['share'] as $k => $v) {
  350. $result = $this->updateLog($k, $v, $id, $update['user_id'], $user['share_day'], $day_prev);
  351. if (isset($result['share_num'])) {
  352. $update['share_num'] += $result['share_num'];
  353. }
  354. if (isset($result['share_group_num'])) {
  355. $update['share_group_num'] += $result['share_group_num'];
  356. }
  357. }
  358. if ($user['share_num'] > 0) {
  359. $update = array();
  360. $update['share_num'] = $user['share_num'];
  361. $update['share_group_num'] = $user['share_group_num'];
  362. $update['where_id'] = $id;
  363. Dever::db('share/info')->update($update);
  364. }
  365. }
  366. }
  367. public function updateLog($share_id, $data, $info_id, $user_id, $day, $day_prev)
  368. {
  369. $prev = array();
  370. /*
  371. if ($day != $day_prev) {
  372. $where = array();
  373. $where['option_share_id'] = $share_id;
  374. $where['option_user_id'] = $user_id;
  375. $where['option_info_id'] = $info_id;
  376. $where['option_source_id'] = $data['id'];
  377. $where['option_share_day'] = $day_prev;
  378. $where['option_applet'] = $data['applet']['id'];
  379. $prev = Dever::db('share/log')->one($where);
  380. if ($prev) {
  381. $data['num'] = $data['num'] - $prev['share_num'];
  382. $data['group_num'] = $data['group_num'] - $prev['share_group_num'];
  383. }
  384. }
  385. */
  386. $update = array();
  387. $update['user_id'] = $user_id;
  388. $update['info_id'] = $info_id;
  389. $update['share_id'] = $share_id;
  390. $update['share_day'] = $day;
  391. $update['applet'] = $data['applet']['id'];
  392. $update['name'] = $data['name'];
  393. $update['source_id'] = $data['id'];
  394. $update['source_table'] = $data['source_table'];
  395. $update['share_num'] = $data['num'];
  396. $update['sdate'] = $data['cdate'];
  397. $update['share_group_num'] = $data['group_num'];
  398. $update['share_reflux_num'] = $data['reflux_num'];
  399. $where = array();
  400. $where['option_share_id'] = $update['share_id'];
  401. $where['option_user_id'] = $update['user_id'];
  402. $where['option_info_id'] = $update['info_id'];
  403. $where['option_source_id'] = $update['source_id'];
  404. $where['option_share_day'] = $update['share_day'];
  405. $where['option_applet'] = $update['applet'];
  406. $info = Dever::db('share/log')->one($where);
  407. if (!$info) {
  408. Dever::db('share/log')->insert($update);
  409. } else {
  410. $update['where_id'] = $info['id'];
  411. Dever::db('share/log')->update($update);
  412. }
  413. if ($prev) {
  414. return array
  415. (
  416. 'share_num' => $data['num'],
  417. 'share_group_num' => $data['group_num'],
  418. );
  419. }
  420. return array();
  421. }
  422. /*
  423. public function userMonthData($data)
  424. {
  425. $user = Dever::db(self::USER)->one(array('name' => $data['wechat_name']));
  426. if ($user) {
  427. $option = array
  428. (
  429. 'share_id' => 'yes',
  430. 'uid' => 'yes',
  431. 'start' => array('yes-cdate', '>='),
  432. 'end' => array('yes-cdate', '<='),
  433. );
  434. //获取昨天数据
  435. $time = time() - (3600 * 24);
  436. $set = Dever::input('set');
  437. if ($set) {
  438. $time = Dever::maketime($set);
  439. }
  440. $month = date('Y-m-d', $time);
  441. $month_end = date('Y-m-t', $time);
  442. $month_cur = date('Y-m-d', $time);
  443. $month_prev = date('Y-m', strtotime("$month -1 month"));//上个月
  444. $start = date('Y-m-01 00:00:00', $time);
  445. if ($month_end == $month_cur || $set) {
  446. $user['status'] = 1;//本月已经到月底
  447. $end = date('Y-m-t 23:59:59', $time);
  448. } else {
  449. $user['status'] = 2;//本月还未到月底
  450. $end = date('Y-m-d 23:59:59', $time);
  451. }
  452. $month_start = Dever::maketime($start);
  453. $month_end = Dever::maketime($end);
  454. $where = array
  455. (
  456. 'option' => $option,
  457. 'uid' => $user['id'],
  458. 'start' => $month_start,
  459. 'end' => $month_end
  460. );
  461. $share = Dever::db(self::SHARE)->state($where);
  462. if ($share) {
  463. $user['applet'] = $data['applet'];
  464. $user['user_id'] = $data['id'];
  465. $user['share_month'] = $month;
  466. $user['share_month_se'] = $start . '-' . $end;
  467. $user['share_num'] = 0;
  468. //$user['share_reflux_num'] = Dever::db(self::REFLUX)->total(array('source_uid' => $user['id']));
  469. //获取月度回流
  470. unset($where['option']['uid']);
  471. $where['option']['source_uid'] = $user['id'];
  472. $user['share_month_reflux_num'] = Dever::db(self::REFLUX)->total($where);
  473. $user['share_group_num'] = 0;
  474. foreach ($share as $k => $v) {
  475. $info = $this->shareData($v, $where);
  476. if ($info && $info['num'] > 0) {
  477. $user['share'][$k] = $info;
  478. $user['share_num'] += $user['share'][$k]['num'];
  479. $user['share_group_num'] += $user['share'][$k]['group_num'];
  480. }
  481. }
  482. $this->updateInfo($user, $month_prev);
  483. }
  484. }
  485. return $user;
  486. }
  487. public function updateInfo($user, $month_prev)
  488. {
  489. # 获取上个月的数据 减去上个月的数据
  490. if ($user['share_month'] != $month_prev) {
  491. $where = array();
  492. $where['option_user_id'] = $user['user_id'];
  493. $where['option_share_month'] = $month_prev;
  494. $prev = Dever::db('share/info')->one($where);
  495. if ($prev) {
  496. $user['share_num'] = $user['share_num'] - $prev['share_num'];
  497. $user['share_group_num'] = $user['share_group_num'] - $prev['share_group_num'];
  498. }
  499. }
  500. $update = array();
  501. $update['user_id'] = $user['user_id'];
  502. $update['applet'] = $user['applet'];
  503. $update['share_month'] = $user['share_month'];
  504. $update['share_month_se'] = $user['share_month_se'];
  505. $update['share_num'] = $user['share_num'];
  506. $update['share_month_reflux_num'] = $user['share_month_reflux_num'];
  507. $update['share_group_num'] = $user['share_group_num'];
  508. $update['status'] = $user['status'];
  509. $where = array();
  510. $where['option_user_id'] = $update['user_id'];
  511. $where['option_share_month'] = $update['share_month'];
  512. $info = Dever::db('share/info')->one($where);
  513. if (!$info) {
  514. $id = Dever::db('share/info')->insert($update);
  515. } else {
  516. $id = $info['id'];
  517. $update['where_id'] = $id;
  518. Dever::db('share/info')->update($update);
  519. }
  520. if ($id > 0 && $user['share']) {
  521. foreach ($user['share'] as $k => $v) {
  522. $this->updateLog($k, $v, $id, $update['user_id'], $user['share_month']);
  523. }
  524. }
  525. }
  526. public function updateLog($share_id, $data, $info_id, $user_id, $month)
  527. {
  528. $update = array();
  529. $update['user_id'] = $user_id;
  530. $update['info_id'] = $info_id;
  531. $update['share_id'] = $share_id;
  532. $update['share_month'] = $month;
  533. $update['applet'] = $data['applet']['id'];
  534. $update['name'] = $data['name'];
  535. $update['source_id'] = $data['id'];
  536. $update['source_table'] = $data['source_table'];
  537. $update['share_num'] = $data['num'];
  538. $update['sdate'] = $data['cdate'];
  539. $update['share_group_num'] = $data['group_num'];
  540. $update['share_reflux_num'] = $data['month_reflux_num'];
  541. $where = array();
  542. $where['option_share_id'] = $update['share_id'];
  543. $where['option_user_id'] = $update['user_id'];
  544. $where['option_info_id'] = $update['info_id'];
  545. $where['option_source_id'] = $update['source_id'];
  546. $where['option_share_month'] = $update['share_month'];
  547. $where['option_applet'] = $update['applet'];
  548. $info = Dever::db('share/log')->one($where);
  549. if (!$info) {
  550. Dever::db('share/log')->insert($update);
  551. } else {
  552. $update['where_id'] = $info['id'];
  553. Dever::db('share/log')->update($update);
  554. }
  555. }*/
  556. }