码迷,mamicode.com
首页 > 其他好文 > 详细

批量导入数据

时间:2018-06-04 14:28:37      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:red   for   rollback   标题   mda   回滚   val   where   ddr   

public function aeBatchImport(){
$contentType = $this->getParam(‘content_type‘, 1, ‘int‘);
$filename = $_FILES[‘data_file‘][‘name‘];
$ext = trim(strrchr($filename, ‘.‘),‘.‘);
// $ext = substr($filename,strpos($filename,‘.‘) + 1);
if($ext != ‘xls‘ && $ext != ‘xlsx‘) {
$this->error(‘文件类型错误‘);
}
$contents = autoimportExcel($_FILES[‘data_file‘][‘tmp_name‘],$ext);
$this->_checkContentHeader($contents[1], $contentType);
unset($contents[1]);
//效验数据
$dd = $data = array();
$AiyoExecuteModel = new \Common\Model\AiyoExecuteModel();

/*
* 查询所有的信息,循环嵌套对比
*/
foreach ($contents as $k=>$val) {
if (!trim($val[‘A‘]) && !trim($val[‘B‘]) && !trim($val[‘C‘]) && !trim($val[‘D‘]) && !trim($val[‘E‘]) && !trim($val[‘F‘]) && !trim($val[‘G‘]) && !trim($val[‘H‘]) && !trim($val[‘I‘]) && !trim($val[‘J‘])) {
unset($contents[$k]);
continue;
}
}
$contents_A = array_column($contents, ‘A‘);
//判断是否有重复的编号
$diff_cons = array_diff_assoc($contents_A, array_unique($contents_A));
if(!empty($diff_cons)){
$this->error(‘执行编号‘.implode(‘,‘, $diff_cons).‘重复‘);
}
//找出上传的编号的所有信息
$where[‘execute_no‘] = [‘in‘, $contents_A];
$executes = $AiyoExecuteModel->where($where)->field(‘execute_id,execute_no‘)->select();
$executes_A = array_column($executes, ‘execute_no‘);
foreach ($executes as $exe){
$executess[$exe[‘execute_id‘]] = $exe[‘execute_no‘];
}
switch ($contentType){
case 1:
foreach ($contents as $kk=>$con) {
if(!$con[‘A‘]){
$this->error(‘第‘.$kk.‘行执行编号不能为空‘);
}
if(!in_array($con[‘A‘], $executes_A)){
$this->error(‘第‘.$kk.‘行执行编号错误‘);
}
if($con[‘D‘] && strpos($con[‘D‘], ‘http‘)===false){
$this->error(‘第‘.$kk.‘行投放链接存在时必须带HTTP‘);
}
if($con[‘E‘] && strpos($con[‘E‘], ‘http‘)===false){
$this->error(‘第‘.$kk.‘行上线链接存在时必须带HTTP‘);
}
if(strlen($con[‘F‘]) > 9 || strlen($con[‘G‘]) > 9){
$this->error(‘第‘.$kk.‘行数字不能超过999999999‘);
}
$execute_id = array_search($con[‘A‘], $executess)?:‘‘;
$data[] = array(
‘execute_no‘ => $con[‘A‘],
‘talent_nickname‘ => $con[‘B‘]?:‘‘,
‘execute_title‘ => $con[‘C‘]?:‘‘,
‘tuwen_launch_url‘ => $con[‘D‘]?:‘‘,
‘online_url‘ => $con[‘E‘]?:‘‘,
‘view_num‘ => $con[‘F‘]?:‘‘,
‘zhibo_come_num‘ => $con[‘G‘]?:‘‘,
‘content_type‘ => $contentType,
‘execute_user_id‘ => $this->iUserId,
‘execute_id‘ => $execute_id
);
$dd[] = $con[‘A‘];
}
break;
case 2:
$hotManModel = new HotmanModel();
$hotManList = $hotManModel->field(‘hotman_id, hotman_nickname‘)->select();
foreach ($hotManList as $v){
$hotManLists[$v[‘hotman_id‘]] = $v[‘hotman_nickname‘];
}
foreach ($contents as $kk=>$con) {
if(!$con[‘A‘]){
$this->error(‘第‘.$kk.‘行执行编号不能为空‘);
}
if(!in_array($con[‘A‘], $executes_A)){
$this->error(‘第‘.$kk.‘行执行编号错误‘);
}
if($con[‘E‘] && strpos($con[‘E‘], ‘http‘)===false){
$this->error(‘第‘.$kk.‘行直播链接存在时必须带HTTP‘);
}
if(strlen($con[‘D‘]) > 9 || strlen($con[‘F‘]) > 9 || strlen($con[‘G‘]) > 9 || strlen($con[‘I‘]) > 9 || strlen($con[‘J‘]) > 9){
$this->error(‘第‘.$kk.‘行数字不能超过999999999‘);
}
//达人昵称存在时根据昵称查询达人ID
if($con[‘B‘]){
$hotman_id = array_search($con[‘B‘], $hotManLists);
if(!$hotman_id){
$this->error(‘第‘.$kk.‘行达人昵称不存在‘);
}
}else{
$hotman_id = 0;
}
$execute_id = array_search($con[‘A‘], $executess)?:‘‘;
$data[] = array(
‘execute_no‘ => $con[‘A‘],
‘talent_nickname‘ => $con[‘B‘]?:‘‘,
‘zhibo_set_play_time‘ => $con[‘C‘]?:‘‘,
‘zhibo_play_duration‘ => $con[‘D‘]?:‘‘,
‘online_url‘ => $con[‘E‘]?:‘‘,
‘view_num‘ => $con[‘F‘]?:‘‘,
‘zhibo_zan_num‘ => $con[‘G‘]?:‘‘,
‘execute_title‘ => $con[‘H‘]?:‘‘,
‘zhibo_come_num‘ => $con[‘I‘]?:‘‘,
‘zhibo_trade_num‘ => $con[‘J‘]?:‘‘,
‘content_type‘ => $contentType,
‘execute_user_id‘ => $this->iUserId,
‘execute_id‘ => $execute_id,
‘zhibo_hotman_id‘ => $hotman_id
);
$dd[] = $con[‘A‘];
}
break;
case 3:
foreach ($contents as $kk=>$con) {
if(!$con[‘A‘]){
$this->error(‘第‘.$kk.‘行执行编号不能为空‘);
}
if(!in_array($con[‘A‘], $executes_A)){
$this->error(‘第‘.$kk.‘行执行编号错误‘);
}
if($con[‘B‘] && strpos($con[‘B‘], ‘http‘)===false){
$this->error(‘第‘.$kk.‘行上线链接存在时必须带HTTP‘);
}
if($con[‘D‘] && strpos($con[‘D‘], ‘http‘)===false){
$this->error(‘第‘.$kk.‘行视频链接存在时必须带HTTP‘);
}
if(strlen($con[‘E‘]) > 9 || strlen($con[‘F‘]) > 9 || strlen($con[‘G‘]) > 9){
$this->error(‘第‘.$kk.‘行数字不能超过999999999‘);
}
$execute_id = array_search($con[‘A‘], $executess)?:‘‘;
$data[] = array(
‘execute_no‘ => $con[‘A‘],
‘online_url‘ => $con[‘B‘]?:‘‘,
‘execute_title‘ => $con[‘C‘]?:‘‘,
‘shipin_video_url_2‘=> $con[‘D‘]?:‘‘,
‘view_num‘ => $con[‘E‘]?:‘‘,
‘zhibo_come_num‘ => $con[‘F‘]?:‘‘,
‘capita_time‘ => $con[‘G‘]?:‘‘,
‘content_type‘ => $contentType,
‘execute_user_id‘ => $this->iUserId,
‘execute_id‘ => $execute_id
);
$dd[] = $con[‘A‘];
}
break;
}

//循环里面进行数据库操作
// switch ($contentType){
// case 1:
// foreach ($contents as $k=>&$val){
// if(!$val[‘A‘] && !$val[‘B‘] && !$val[‘C‘] && !$val[‘D‘]&& !$val[‘E‘] && !$val[‘F‘] && !$val[‘G‘]){
// unset($contents[$k]);continue;
// }
// if(!$val[‘A‘]){
// $this->error(‘第‘.$k.‘行执行编号不能为空‘);
// }
// if($val[‘D‘] && strpos($val[‘D‘], ‘http‘)===false){
// $this->error(‘第‘.$k.‘行投放链接存在时必须带HTTP‘);
// }
// if($val[‘E‘] && strpos($val[‘E‘], ‘http‘)===false){
// $this->error(‘第‘.$k.‘行上线链接存在时必须带HTTP‘);
// }
// if(strlen($val[‘F‘]) > 9 || strlen($val[‘G‘]) > 9){
// $this->error(‘第‘.$k.‘行数字不能超过999999999‘);
// }
// $fields = ‘execute_id,execute_no,talent_nickname,execute_title,tuwen_launch_url,online_url,view_num,zhibo_come_num‘;
// if($info = $AiyoExecuteModel->field($fields)->where([‘execute_no‘=>$val[‘A‘]])->find()){
// //查询当前执行编号下面的数据是否有值
// if(!empty($info[‘talent_nickname‘]) || !empty($info[‘execute_title‘]) || !empty($info[‘tuwen_launch_url‘]) || !empty($info[‘online_url‘]) || !empty($info[‘view_num‘]) || !empty($info[‘zhibo_come_num‘])){
// $dd[] = $val[‘A‘];
// }
// $ar = array(
// ‘execute_no‘ => $val[‘A‘],
// ‘talent_nickname‘ => $val[‘B‘]?:‘‘,
// ‘execute_title‘ => $val[‘C‘]?:‘‘,
// ‘tuwen_launch_url‘ => $val[‘D‘]?:‘‘,
// ‘online_url‘ => $val[‘E‘]?:‘‘,
// ‘view_num‘ => $val[‘F‘]?:‘‘,
// ‘zhibo_come_num‘ => $val[‘G‘]?:‘‘,
// ‘content_type‘ => $contentType,
// ‘execute_user_id‘ => $this->iUserId,
// ‘execute_id‘ => $info[‘execute_id‘],
// );
// $data[] = $ar;unset($ar);
// }else{
// $this->error(‘第‘.$k.‘行执行编号错误‘);
// }
// }
// break;
// case 2:
// $hotManModel = new HotmanModel();
// foreach ($contents as $k=>$val){
// if(!$val[‘A‘] && !$val[‘B‘] && !$val[‘C‘] && !$val[‘D‘]&& !$val[‘E‘] && !$val[‘F‘] && !$val[‘G‘]&& !$val[‘H‘] && !$val[‘I‘] && !$val[‘J‘]){
// unset($contents[$k]);continue;
// }
// if(!$val[‘A‘]){
// $this->error(‘第‘.$k.‘行执行编号不能为空‘);
// }
// if($val[‘E‘] && strpos($val[‘E‘], ‘http‘)===false){
// $this->error(‘第‘.$k.‘行直播链接存在时必须带HTTP‘);
// }
// if(strlen($val[‘D‘]) > 9 || strlen($val[‘F‘]) > 9 || strlen($val[‘G‘]) > 9 || strlen($val[‘I‘]) > 9 || strlen($val[‘J‘]) > 9){
// $this->error(‘第‘.$k.‘行数字不能超过999999999‘);
// }
// $fields = ‘execute_id,execute_no,talent_nickname,execute_title,online_url,view_num,zhibo_come_num,zhibo_set_play_time,zhibo_play_duration,zhibo_zan_num,zhibo_trade_num‘;
// if($info = $AiyoExecuteModel->field($fields)->where([‘execute_no‘=>$val[‘A‘]])->find()){
// //查询当前执行编号下面的数据是否有值
// if(!empty($info[‘talent_nickname‘]) || !empty($info[‘zhibo_set_play_time‘]) || !empty($info[‘zhibo_play_duration‘]) || !empty($info[‘online_url‘]) || !empty($info[‘view_num‘]) || !empty($info[‘zhibo_zan_num‘]) || !empty($info[‘execute_title‘]) || !empty($info[‘zhibo_come_num‘]) || !empty($info[‘zhibo_trade_num‘])){
// $dd[] = $val[‘A‘];
// }
// //根据昵称查询达人ID
// if($val[‘B‘]){
// $hotman_id = $hotManModel->where([‘hotman_nickname‘=>$val[‘B‘]])->getField(‘hotman_id‘);
// if(!$hotman_id){
// $this->error(‘第‘.$k.‘行达人昵称不存在‘);
// }
// }else{
// $hotman_id = 0;
// }
// $ar = array(
// ‘execute_no‘ => $val[‘A‘],
// ‘talent_nickname‘ => $val[‘B‘]?:‘‘,
// ‘zhibo_set_play_time‘ => $val[‘C‘]?:‘‘,
// ‘zhibo_play_duration‘ => $val[‘D‘]?:‘‘,
// ‘online_url‘ => $val[‘E‘]?:‘‘,
// ‘view_num‘ => $val[‘F‘]?:‘‘,
// ‘zhibo_zan_num‘ => $val[‘G‘]?:‘‘,
// ‘execute_title‘ => $val[‘H‘]?:‘‘,
// ‘zhibo_come_num‘ => $val[‘I‘]?:‘‘,
// ‘zhibo_trade_num‘ => $val[‘J‘]?:‘‘,
// ‘content_type‘ => $contentType,
// ‘execute_user_id‘ => $this->iUserId,
// ‘execute_id‘ => $info[‘execute_id‘],
// ‘zhibo_hotman_id‘ => $hotman_id
// );
// $data[] = $ar;unset($ar);
// }else{
// $this->error(‘第‘.$k.‘行执行编号错误‘);
// }
// }
// break;
// case 3:
// foreach ($contents as $k=>$val){
// if(!$val[‘A‘] && !$val[‘B‘] && !$val[‘C‘] && !$val[‘D‘]&& !$val[‘E‘] && !$val[‘F‘] && !$val[‘G‘]){
// unset($contents[$k]);continue;
// }
// if(!$val[‘A‘]){
// $this->error(‘第‘.$k.‘行执行编号不能为空‘);
// }
// if($val[‘B‘] && strpos($val[‘B‘], ‘http‘)===false){
// $this->error(‘第‘.$k.‘行上线链接存在时必须带HTTP‘);
// }
// if($val[‘D‘] && strpos($val[‘D‘], ‘http‘)===false){
// $this->error(‘第‘.$k.‘行视频链接存在时必须带HTTP‘);
// }
// if(strlen($val[‘E‘]) > 9 || strlen($val[‘F‘]) > 9 || strlen($val[‘G‘]) > 9){
// $this->error(‘第‘.$k.‘行数字不能超过999999999‘);
// }
// $fields = ‘execute_id,execute_no,execute_title,online_url,view_num,zhibo_come_num,shipin_video_url_2,capita_time‘;
// if($info = $AiyoExecuteModel->field($fields)->where([‘execute_no‘=>$val[‘A‘]])->find()){
// //查询当前执行编号下面的数据是否有值
// if(!empty($info[‘online_url‘]) || !empty($info[‘execute_title‘]) || !empty($info[‘shipin_video_url_2‘]) || !empty($info[‘view_num‘]) || !empty($info[‘zhibo_come_num‘]) || !empty($info[‘capita_time‘])){
// $dd[] = $val[‘A‘];
// }
//
// $ar = array(
// ‘execute_no‘ => $val[‘A‘],
// ‘online_url‘ => $val[‘B‘]?:‘‘,
// ‘execute_title‘ => $val[‘C‘]?:‘‘,
// ‘shipin_video_url_2‘=> $val[‘D‘]?:‘‘,
// ‘view_num‘ => $val[‘E‘]?:‘‘,
// ‘zhibo_come_num‘ => $val[‘F‘]?:‘‘,
// ‘capita_time‘ => $val[‘G‘]?:‘‘,
// ‘content_type‘ => $contentType,
// ‘execute_user_id‘ =>$this->iUserId,
// ‘execute_id‘ =>$info[‘execute_id‘]
// );
// $data[] = $ar;unset($ar);
// }else{
// $this->error(‘第‘.$k.‘行执行编号错误‘);
// }
// }
// break;
// }
if(count($data) <= 0){
$this->error(‘文件内容不能为空‘);
}
$list[‘arr‘] = $data;
//数据库中其他字段存在值的execute_no集合
if(!empty($dd)){
$list[‘str‘] = trim(implode(‘,‘, $dd), ‘,‘);
}else{
$list[‘str‘] = ‘‘;
}
$this->returnList($list);
}

//效验表格头
private function _checkContentHeader($fields, $contentType) {
$return = array();
switch($contentType){
case 1: //图文
$header = array(
‘A‘=>array(‘执行任务编号‘, ‘execute_no‘),
‘B‘=>array(‘达人昵称‘, ‘talent_nickname‘),
‘C‘=>array(‘标题‘, ‘execute_title‘),
‘D‘=>array(‘投放链接‘, ‘tuwen_launch_url‘),
‘E‘=>array(‘上线链接‘, ‘online_url‘),
‘F‘=>array(‘阅读量‘, ‘view_num‘),
‘G‘=>array(‘进店数‘, ‘zhibo_come_num‘),
);
break;
case 2: //直播
$header = array(
‘A‘=>array(‘执行任务编号‘, ‘execute_no‘),
‘B‘=>array(‘达人昵称‘, ‘talent_nickname‘),
‘C‘=>array(‘直播时间‘, ‘zhibo_set_play_time‘),
‘D‘=>array(‘直播时长‘, ‘zhibo_play_duration‘),
‘E‘=>array(‘直播链接‘, ‘online_url‘),
‘F‘=>array(‘观看量‘, ‘view_num‘),
‘G‘=>array(‘点赞数‘, ‘zhibo_zan_num‘),
‘H‘=>array(‘标题‘, ‘execute_title‘),
‘I‘=>array(‘进店数‘, ‘zhibo_come_num‘),
‘J‘=>array(‘成交笔数‘, ‘zhibo_trade_num‘),
);
break;
case 3: //短视频
$header = array(
‘A‘=>array(‘执行任务编号‘, ‘execute_no‘),
‘B‘=>array(‘上线链接‘, ‘online_url‘),
‘C‘=>array(‘标题‘, ‘execute_title‘),
‘D‘=>array(‘视频链接‘, ‘shipin_video_url_2‘),
‘E‘=>array(‘阅读量‘, ‘view_num‘),
‘F‘=>array(‘进店数‘, ‘zhibo_come_num‘),
‘G‘=>array(‘人均时长‘, ‘capita_time‘),
);
break;
}
foreach($header as $key => $value) {
if (false === strpos($fields[$key],$value[0])){
$errors[] = array(‘line‘ => ‘null‘, ‘error‘ => "文件模板不正确.[{$key}]");
}
$return[$value[1]] = $key;
}
if ($errors){
$this->error(‘上传失败‘, 0, $errors);
}

return $return;
}
//处理上传表格数据
public function disposeData(){
$dat = $_POST[‘data‘];
$data = json_decode($dat, true);
$AiyoExecuteModel = new \Common\Model\AiyoExecuteModel();
$AiyoExecuteModel->startTrans(); //开启事务
$num = 0;
foreach ($data as $key=>$item){
$item[‘create_time‘] = date(‘Y-m-d H:i:s‘, time());
if($AiyoExecuteModel->save($item)){
$num += 1;
}else{
$AiyoExecuteModel->rollback(); //数据回滚
$this->error($AiyoExecuteModel->getError());
}
}
if(count($data) == $num){
$AiyoExecuteModel->commit(); //数据提交
$this->success("执行内容上传成功");
}else{
$AiyoExecuteModel->rollback(); //数据回滚
$this->error("数据有误,请检查后重新上传");
}
}
/**
* 导入excel文件
* @param string $filename
*/
function autoimportExcel($filename,$ext) {
$arr = array();


//导入PHPExcel类库,因为PHPExcel没有用命名空间,只能inport导入
import("Org.Util.PHPExcel");

//创建PHPExcel对象,注意,不能少了\
$PHPExcel = new \PHPExcel();
//如果excel文件后缀名为.xls,导入这个类
if($ext == ‘xls‘) {
import("Org.Util.PHPExcel.Reader.Excel5");
$PHPReader = new \PHPExcel_Reader_Excel5();
}
else {
//如果excel文件后缀名为.xlsx,导入这下类
import("Org.Util.PHPExcel.Reader.Excel2007");
$PHPReader = new \PHPExcel_Reader_Excel2007();
}

//载入文件
$PHPExcel = $PHPReader->load($filename);
//获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
$currentSheet = $PHPExcel->getSheet(0);
//获取总列数
$allColumn = $currentSheet->getHighestColumn();
//获取总行数
$allRow = $currentSheet->getHighestRow();
//循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
for($currentRow = 1; $currentRow <= $allRow; $currentRow++) {
//从哪列开始,A表示第一列
for($currentColumn = ‘A‘; $currentColumn <= $allColumn; $currentColumn++) {
//数据坐标
$address = $currentColumn . $currentRow;
//读取到的数据,保存到数组$arr中
$arr[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
$cell = $currentSheet->getCell($address);

// http://www.osakac.ac.jp/labs/koeda/tmp/phpexcel/Documentation/API/PHPExcel_Shared/PHPExcel_Shared_Date.html
if(PHPExcel_Shared_Date::isDateTime($cell)) {
$time = $currentSheet->getCell($address)->getValue();
//财务上传对账,日期为零
if(CONTROLLER_NAME == ‘PayLog‘){
if(is_float($time)){
$arr[$currentRow][$currentColumn] = gmdate("Y-m-d H:i:s",PHPExcel_Shared_Date::ExcelToPHP($time));
}
else {
$arr[$currentRow][$currentColumn] = $time;
}
} else {
if($time) {
$arr[$currentRow][$currentColumn] = gmdate("Y-m-d H:i:s",PHPExcel_Shared_Date::ExcelToPHP($time));
}
else {
$arr[$currentRow][$currentColumn] = $time;
}
}

// $arr[$currentRow][$currentColumn]=gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($currentSheet->getCell($address)->getValue()));
}
else {
$arr[$currentRow][$currentColumn] = $currentSheet->getCell($address)->getValue();
if(is_object($arr[$currentRow][$currentColumn])) {
$arr[$currentRow][$currentColumn] = $arr[$currentRow][$currentColumn]->__toString();
}
}
}
}
return $arr;
}







批量导入数据

标签:red   for   rollback   标题   mda   回滚   val   where   ddr   

原文地址:https://www.cnblogs.com/liujiyun/p/9132568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!