123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617 |
- <?php
- namespace Share\Src;
- use Dever;
- class User
- {
- const INFO = 'set/info:applet';
- const SHARE = 'comment/share:applet';
- const USER = 'passport/user:applet';
- const NEWS = 'content/news:applet';
- const COURSE = 'content/course:applet';
- const MEETING = 'content/meeting:applet';
- const SERVICE = 'content/service:applet';
- const ACTIVE = 'activity/active:applet';
- const REFLUX = 'comment/share_reflux:applet';
- const SHARELOG = 'comment/share_log:applet';
- const SHAREGROUP = 'comment/share_group:applet';
- /**
- * 小程序列表
- *
- * @return mixed
- */
- public function applet()
- {
- $info = Dever::db(self::INFO);
- $data = $info->state();
- return $data;
- }
- /**
- * 导入excel
- *
- * @return mixed
- */
- public function import()
- {
- $file = Dever::data() . 'user.xlsx';
- $reader = new \PHPExcel_Reader_Excel2007();
- if(!$reader->canRead($file)){
- $reader = new \PHPExcel_Reader_Excel5();
- if(!$reader->canRead($file)){
- echo 'no Excel';
- return ;
- }
- }
-
- $xls = $reader->load($file);
- $currentSheet = $xls->getSheet(0);
- $column = $currentSheet->getHighestColumn();
- $row = $currentSheet->getHighestRow();
- $group = $name = $wechat = array();
- for ($curRow = 5;$curRow <= $row; $curRow++) {
- for ($curColumn = 'A';$curColumn<= $column; $curColumn++) {
- $value = $currentSheet->getCellByColumnAndRow(ord($curColumn) - 65,$curRow)->getValue();
- if ($curColumn == 'B') {
- //分组
- if ($value) {
- $group_id = Dever::upinto('share/group', array('name' => $value), array('name' => $value));
- }
-
- } elseif ($curColumn == 'C') {
- //姓名
- if ($value && isset($group_id) && $group_id) {
- Dever::upinto('share/user', array('name' => $value), array('group_id' => $group_id, 'name' => $value));
- }
- } elseif ($curColumn == 'D') {
- //微信
- $wechat[$value] = $value;
- }
- }
- }
- }
- /**
- * 导出成excel
- *
- * @return mixed
- */
- public function out()
- {
- $applet = $this->applet();
- $group = Dever::db('share/group')->state();
- $user = Dever::db('share/user')->getAll();
- $day = Dever::input('day');
- if (!$day) {
- $day = date('Y-m-d', time()-3600*24);
- }
- $start = Dever::maketime(date('Y-m-01 00:00:00', Dever::maketime($day)));
- $end = Dever::maketime(date('Y-m-t 23:59:59', Dever::maketime($day)));
-
- $log = Dever::db('share/log')->state();
- $i = 0;
- foreach ($user as $k => $v) {
- if (!$v['wechat_name']) {
- unset($user[$k]);
- continue;
- }
-
- $user[$k]['group'] = $group[$v['group_id']]['name'];
- $user[$k]['info'] = Dever::db('share/info')->one(array('share_day' => $day, 'user_id' => $v['id']));
- if (!$user[$k]['info']) {
- $user[$k]['info']['share_article_num'] = 0;
- $user[$k]['applet'] = '未知';
- } else {
- $user[$k]['applet'] = array();
- if ($user[$k]['info']['applet']) {
- $temp = explode(',', $user[$k]['info']['applet']);
- foreach ($temp as $k1 => $v1) {
- $user[$k]['applet'][$k1] = $applet[$v1]['name'];
- }
- }
-
- $user[$k]['applet'] = implode("\r\n", $user[$k]['applet']);
- }
- $user[$k]['log'] = Dever::db('share/log')->state(array('share_day' => $day, 'user_id' => $v['id']));
- $total = Dever::db('share/log')->month(array('start' => $start, 'end' => $end, 'user_id' => $v['id']));
- $user[$k]['total'] = 0;
- if ($total) {
- $user[$k]['total'] = $total['num'];
- }
- if (!$user[$k]['total']) {
- $user[$k]['total'] = 0;
- }
- $user[$k]['status'] = '已达标';
- if ($user[$k]['info']['share_article_num'] < 3) {
- $user[$k]['status'] = '未达标';
- }
- if ($user[$k]['name'] && $user[$k]['info']) {
- $data[$i] = array
- (
- $i+1,
- $user[$k]['group'],
- $user[$k]['name'],
- $user[$k]['wechat_name'],
- $user[$k]['applet'],
- $user[$k]['info']['share_article_num'],
- $user[$k]['total'],
- $user[$k]['status'],
- );
- $i++;
- }
- }
- $header = array
- (
- '序号', '业务板块', '姓名', '微信昵称', '负责小程序名称', '分享文章数', '月总回流', '是否达标'
- );
- $file = $day . '_小程序推广';
- if (Dever::input('test')) {
- print_r($header);
- print_r($data);die;
- }
-
- Dever::excelExport($data, $header, $file, $sheet = 0, $sheetName = $day);
- return;
- }
- public function test()
- {
- $name = '姓名';
- $header = array
- (
- $name,'手机号'
- );
- $data = array
- (
- array
- (
- '于斌', '15810090845'
- ),
- array
- (
- '3', '4'
- ),
- );
- Dever::excelExport($data, $header, $filename = 'test');
- }
- /**
- * 获取数据
- *
- * @return mixed
- */
- public function data()
- {
- $user = Dever::db('share/user')->state();
- foreach ($user as $k => $v) {
- if ($v['wechat_name']) {
- $user[$k] = $this->userData($v);
- }
- }
- return true;
- }
- public function userData($data)
- {
- if ($data['uid']) {
- $where = array
- (
- 'option' => array
- (
- 'id' => array('yes', 'in'),
- ),
- 'id' => explode(',', $data['uid']),
- );
- $user = Dever::db(self::USER)->state($where);
- } else {
- $user = Dever::db(self::USER)->state(array('name' => $data['wechat_name']));
- }
- $ids = array();
- if ($user) {
- $ids = array_keys($user);
- }
-
- if ($ids) {
- $user = array();
- $option = array
- (
- 'share_id' => 'yes',
- 'uid' => array('yes', 'in'),
- 'start' => array('yes-cdate', '>='),
- 'end' => array('yes-cdate', '<='),
- );
- //获取昨天数据
- $time = time() - (3600 * 24);
- $set = Dever::input('set');
- if ($set) {
- $time = Dever::maketime($set);
- }
- $day = date('Y-m-d', $time);
- $day_prev = date('Y-m-d', strtotime("$day -1 day"));//前天
- $start = date('Y-m-d 00:00:00', $time);
- $end = date('Y-m-d 23:59:59', $time);
-
- $day_start = Dever::maketime($start);
- $day_end = Dever::maketime($end);
- $where = array
- (
- 'option' => $option,
- 'uid' => $ids,
- 'start' => $day_start,
- 'end' => $day_end
- );
- $share = Dever::db(self::SHARE)->state($where);
- if ($share) {
- $user['applet'] = array();
- //$user['applet'] = $data['applet'];
- $user['user_id'] = $data['id'];
- $user['share_day'] = $day;
- $user['share_num'] = 0;
- //$user['share_reflux_num'] = Dever::db(self::REFLUX)->total(array('source_uid' => $user['id']));
- //获取月度回流
- unset($where['option']['uid']);
- $where['option']['source_uid'] = array('yes', 'in');
- $where['source_uid'] = $ids;
- //$user['share_reflux_num'] = Dever::db(self::REFLUX)->total($where);
- $user['share_group_num'] = $user['share_reflux_num'] = 0;
- $article = array();
- foreach ($share as $k => $v) {
- $info = $this->shareData($v, $where, $article);
- if ($info) {
- $user['applet'][$info['applet']['id']] = $info['applet']['id'];
- $article[$info['source_table'] . '_' . $info['id']] = 1;
- $user['share'][$k] = $info;
- $user['share_num'] += $user['share'][$k]['num'];
- $user['share_group_num'] += $user['share'][$k]['group_num'];
- $user['share_reflux_num'] += $user['share'][$k]['reflux_num'];
- }
- }
- $user['applet'] = implode(',', $user['applet']);
- $user['share_article_num'] = count($article);
- $this->updateInfo($user, $day_prev);
- }
- }
- return $user;
- }
- public function shareData($data, $where)
- {
- $result = array();
- $table = false;
- switch ($data['source_table']) {
- case 1:
- $table = self::NEWS;
- break;
- case 2:
- $table = self::COURSE;
- break;
- case 3:
- $table = self::MEETING;
- break;
- case 5:
- $table = self::SERVICE;
- break;
- case 8:
- $table = self::ACTIVE;
- break;
- }
- if ($table && $data['source_id'] > 0) {
- $info = Dever::db($table)->one($data['source_id']);
- if ($info) {
- $result['applet'] = Dever::db(self::INFO)->one($data['info_id']);
- $result['source_table'] = $data['source_table'];
- $result['id'] = $info['id'];
- $result['name'] = $info['name'];
- $result['time'] = date('Y-m-d H:i:s', $data['cdate']);
- $result['cdate'] = $data['cdate'];
- $result['num'] = $data['num'];
- $result['group_num'] = $data['group_num'];
- $where['share_id'] = $data['id'];
- $result['reflux_num'] = Dever::db(self::REFLUX)->total($where);
- unset($where['option']['source_uid']);
- $result['num'] = Dever::db(self::SHARELOG)->total($where);
- $result['group_num'] = Dever::db(self::SHAREGROUP)->total($where);
- if ($result['num'] <= 0) {
- $result['reflux_num'] = 0;
- }
- if ($result['group_num'] > $result['num']) {
- $result['num'] = $result['group_num'];
- }
- }
- }
- return $result;
- }
- public function updateInfo($user, $day_prev)
- {
- # 获取上个月的数据 减去上个月的数据
- /*
- if ($user['share_day'] != $day_prev) {
- $where = array();
- $where['option_user_id'] = $user['user_id'];
- $where['option_share_day'] = $day_prev;
- $prev = Dever::db('share/info')->one($where);
- if ($prev) {
- $user['share_num'] = $user['share_num'] - $prev['share_num'];
- $user['share_group_num'] = $user['share_group_num'] - $prev['share_group_num'];
- }
- }*/
- $update = array();
- $update['user_id'] = $user['user_id'];
- $update['applet'] = $user['applet'];
- $update['share_day'] = $user['share_day'];
- $update['share_num'] = $user['share_num'];
- $update['share_reflux_num'] = $user['share_reflux_num'];
- $update['share_article_num'] = $user['share_article_num'];
- $update['share_group_num'] = $user['share_group_num'];
- $update['status'] = 1;
- if ($update['share_article_num'] >= 3) {
- $update['status'] = 2;
- }
- $where = array();
- $where['option_user_id'] = $update['user_id'];
- $where['option_share_day'] = $update['share_day'];
- $info = Dever::db('share/info')->one($where);
- if (!$info) {
- $id = Dever::db('share/info')->insert($update);
- } else {
- $id = $info['id'];
- $update['where_id'] = $id;
- Dever::db('share/info')->update($update);
- }
- if ($id > 0 && isset($user['share'])) {
- $user['share_num'] = 0;
- $user['share_group_num'] = 0;
- foreach ($user['share'] as $k => $v) {
- $result = $this->updateLog($k, $v, $id, $update['user_id'], $user['share_day'], $day_prev);
- if (isset($result['share_num'])) {
- $update['share_num'] += $result['share_num'];
- }
- if (isset($result['share_group_num'])) {
- $update['share_group_num'] += $result['share_group_num'];
- }
- }
- if ($user['share_num'] > 0) {
- $update = array();
- $update['share_num'] = $user['share_num'];
- $update['share_group_num'] = $user['share_group_num'];
- $update['where_id'] = $id;
- Dever::db('share/info')->update($update);
- }
- }
- }
- public function updateLog($share_id, $data, $info_id, $user_id, $day, $day_prev)
- {
- $prev = array();
- /*
- if ($day != $day_prev) {
- $where = array();
- $where['option_share_id'] = $share_id;
- $where['option_user_id'] = $user_id;
- $where['option_info_id'] = $info_id;
- $where['option_source_id'] = $data['id'];
- $where['option_share_day'] = $day_prev;
- $where['option_applet'] = $data['applet']['id'];
- $prev = Dever::db('share/log')->one($where);
- if ($prev) {
- $data['num'] = $data['num'] - $prev['share_num'];
- $data['group_num'] = $data['group_num'] - $prev['share_group_num'];
- }
- }
- */
- $update = array();
- $update['user_id'] = $user_id;
- $update['info_id'] = $info_id;
- $update['share_id'] = $share_id;
- $update['share_day'] = $day;
- $update['applet'] = $data['applet']['id'];
- $update['name'] = $data['name'];
- $update['source_id'] = $data['id'];
- $update['source_table'] = $data['source_table'];
- $update['share_num'] = $data['num'];
- $update['sdate'] = $data['cdate'];
- $update['share_group_num'] = $data['group_num'];
- $update['share_reflux_num'] = $data['reflux_num'];
- $where = array();
- $where['option_share_id'] = $update['share_id'];
- $where['option_user_id'] = $update['user_id'];
- $where['option_info_id'] = $update['info_id'];
- $where['option_source_id'] = $update['source_id'];
- $where['option_share_day'] = $update['share_day'];
- $where['option_applet'] = $update['applet'];
- $info = Dever::db('share/log')->one($where);
- if (!$info) {
- Dever::db('share/log')->insert($update);
- } else {
- $update['where_id'] = $info['id'];
- Dever::db('share/log')->update($update);
- }
- if ($prev) {
- return array
- (
- 'share_num' => $data['num'],
- 'share_group_num' => $data['group_num'],
- );
- }
- return array();
- }
- /*
- public function userMonthData($data)
- {
- $user = Dever::db(self::USER)->one(array('name' => $data['wechat_name']));
- if ($user) {
- $option = array
- (
- 'share_id' => 'yes',
- 'uid' => 'yes',
- 'start' => array('yes-cdate', '>='),
- 'end' => array('yes-cdate', '<='),
- );
- //获取昨天数据
- $time = time() - (3600 * 24);
- $set = Dever::input('set');
- if ($set) {
- $time = Dever::maketime($set);
- }
- $month = date('Y-m-d', $time);
- $month_end = date('Y-m-t', $time);
- $month_cur = date('Y-m-d', $time);
- $month_prev = date('Y-m', strtotime("$month -1 month"));//上个月
- $start = date('Y-m-01 00:00:00', $time);
- if ($month_end == $month_cur || $set) {
- $user['status'] = 1;//本月已经到月底
- $end = date('Y-m-t 23:59:59', $time);
- } else {
- $user['status'] = 2;//本月还未到月底
- $end = date('Y-m-d 23:59:59', $time);
- }
-
- $month_start = Dever::maketime($start);
- $month_end = Dever::maketime($end);
- $where = array
- (
- 'option' => $option,
- 'uid' => $user['id'],
- 'start' => $month_start,
- 'end' => $month_end
- );
- $share = Dever::db(self::SHARE)->state($where);
- if ($share) {
- $user['applet'] = $data['applet'];
- $user['user_id'] = $data['id'];
- $user['share_month'] = $month;
- $user['share_month_se'] = $start . '-' . $end;
- $user['share_num'] = 0;
- //$user['share_reflux_num'] = Dever::db(self::REFLUX)->total(array('source_uid' => $user['id']));
- //获取月度回流
- unset($where['option']['uid']);
- $where['option']['source_uid'] = $user['id'];
- $user['share_month_reflux_num'] = Dever::db(self::REFLUX)->total($where);
- $user['share_group_num'] = 0;
- foreach ($share as $k => $v) {
- $info = $this->shareData($v, $where);
- if ($info && $info['num'] > 0) {
- $user['share'][$k] = $info;
- $user['share_num'] += $user['share'][$k]['num'];
- $user['share_group_num'] += $user['share'][$k]['group_num'];
- }
- }
- $this->updateInfo($user, $month_prev);
- }
- }
- return $user;
- }
- public function updateInfo($user, $month_prev)
- {
- # 获取上个月的数据 减去上个月的数据
- if ($user['share_month'] != $month_prev) {
- $where = array();
- $where['option_user_id'] = $user['user_id'];
- $where['option_share_month'] = $month_prev;
- $prev = Dever::db('share/info')->one($where);
- if ($prev) {
- $user['share_num'] = $user['share_num'] - $prev['share_num'];
- $user['share_group_num'] = $user['share_group_num'] - $prev['share_group_num'];
- }
- }
- $update = array();
- $update['user_id'] = $user['user_id'];
- $update['applet'] = $user['applet'];
- $update['share_month'] = $user['share_month'];
- $update['share_month_se'] = $user['share_month_se'];
- $update['share_num'] = $user['share_num'];
- $update['share_month_reflux_num'] = $user['share_month_reflux_num'];
- $update['share_group_num'] = $user['share_group_num'];
- $update['status'] = $user['status'];
- $where = array();
- $where['option_user_id'] = $update['user_id'];
- $where['option_share_month'] = $update['share_month'];
- $info = Dever::db('share/info')->one($where);
- if (!$info) {
- $id = Dever::db('share/info')->insert($update);
- } else {
- $id = $info['id'];
- $update['where_id'] = $id;
- Dever::db('share/info')->update($update);
- }
- if ($id > 0 && $user['share']) {
- foreach ($user['share'] as $k => $v) {
- $this->updateLog($k, $v, $id, $update['user_id'], $user['share_month']);
- }
- }
- }
- public function updateLog($share_id, $data, $info_id, $user_id, $month)
- {
- $update = array();
- $update['user_id'] = $user_id;
- $update['info_id'] = $info_id;
- $update['share_id'] = $share_id;
- $update['share_month'] = $month;
- $update['applet'] = $data['applet']['id'];
- $update['name'] = $data['name'];
- $update['source_id'] = $data['id'];
- $update['source_table'] = $data['source_table'];
- $update['share_num'] = $data['num'];
- $update['sdate'] = $data['cdate'];
- $update['share_group_num'] = $data['group_num'];
- $update['share_reflux_num'] = $data['month_reflux_num'];
- $where = array();
- $where['option_share_id'] = $update['share_id'];
- $where['option_user_id'] = $update['user_id'];
- $where['option_info_id'] = $update['info_id'];
- $where['option_source_id'] = $update['source_id'];
- $where['option_share_month'] = $update['share_month'];
- $where['option_applet'] = $update['applet'];
- $info = Dever::db('share/log')->one($where);
- if (!$info) {
- Dever::db('share/log')->insert($update);
- } else {
- $update['where_id'] = $info['id'];
- Dever::db('share/log')->update($update);
- }
- }*/
- }
|