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); } }*/ }