云服务器 199 / 年,新老同享,开发者力荐特惠渠道
阿里云推广

thinkphp3.2.3的excel导入数据事务处理代码片段

  • 内容
  • 评论
  • 相关

1.功能商品名称不能重复,包含excel里面不能重复,同时也不能和已经导入的商品名字重复。(没有通过数据库唯一约束实现)

2.循环里面一条一条处理,一旦有一条数据处理失败则回滚,都成功才会最终提交。(mysql数据库引擎需要是innodb)

3.如果多行数据有问题,失败问题原因都会展示。(错误累计展示,方便对excel修改,再次导入)

代码如下:


private function save_import_new($data) {
	$basekeynum = $_SESSION["accountinfo"]["basekeynum"];
	if (empty($data)) {
		$rt["sta"] = 0;
		$rt["msg"] = "导入为空";
		echo json_encode($rt);
		die;
	}
	$error_flag="0";
	$error_msg="";
	foreach ($data as $key => $value) {
		$goods_id =$value["A"];
		$goods_name =trim($value["B"]);
		$cate_name =trim($value["C"]);
		$goods_img = $value["D"];
		$goods_des = $value["E"];
		$goods_sort = $value["F"];
		$goods_money = $value["G"];
		$is_show = $value["H"];
		if($goods_name=='') {
			$error_flag=1;
			$error_msg.= "第" . $key . "行商品名称为空!<br/>";
		}
		if($cate_name=='') {
			$error_flag=1;
			$error_msg.= "第" . $key . "行分类名称为空!<br/>";
		}
		$goodsinfo = M("goods")->where("goods_name='$goods_name' and basekeynum='$basekeynum' ")->find();
		if (!empty($goodsinfo)) {
			$error_flag=1;
			$error_msg.= "第" . $key . "行商品名称和系统里面重复<br/>";
		}
	}
	if($error_flag=='1') {
		$rt["sta"] = 0;
		$rt["msg"] = $error_msg;
		echo json_encode($rt);
		die;
	}
	//开启事务处理机制
	$flag = true;
	$Model = M();
	$Model->startTrans();
	foreach ($data as $key => $value) {
		$goods_id =$value["A"];
		$goods_name =trim($value["B"]);
		$cate_name =trim($value["C"]);
		$goods_img = $value["D"];
		$goods_des = $value["E"];
		$goods_sort = $value["F"];
		$goods_money = $value["G"];
		$is_show = $value["H"];
		$goodsinfo = $Model->table('zs_goods')->where("goods_name='$goods_name' and basekeynum='$basekeynum' ")->find();
		if (!empty($goodsinfo)) {
			$flag = false;
			$error_msg.= "第" . $key . "行商品名称(".$goods_name.")和excel里面其他行重复<br/>";
		}
		//先查询分类是否存在,不存在则新增
		$cate_info =$Model->table('zs_goods_cate')->where("cate_name='$cate_name' and basekeynum='$basekeynum' ")->find();
		if (!empty($cate_info)) {
			$cate_id=$cate_info['cate_id'];
		} else {
			//新增
			$addcate['cate_name']=$cate_name;
			$addcate['cate_sort']=0;
			$addcate['add_time']=time();
			$addcate['status']=1;
			$addcate['basekeynum']=$basekeynum;
			$cate_id=$Model->table('zs_goods_cate')->add( $addcate);
			if(!$cate_id) {
				$flag = false;
				$error_msg.= "第" . $key . "行分类名称".$cate_name."新增失败<br/>";
			}
		}
		//新增商品
		$add["goods_name"] = $goods_name;
		$add["cate_id"] = $cate_id;
		$add["goods_img"] = $goods_img;
		$add["goods_des"] = $goods_des;
		$add["goods_sort"] = $goods_sort;
		$add["goods_money"] = $goods_money;
		$add["is_show"] = $is_show;
		$add["goods_add_time"] = time();
		$add["basekeynum"] = $basekeynum;
		$goods_id=$Model->table('zs_goods')->add($add);
		if(!$goods_id) {
			$flag = false;
			$error_msg.= "第" . $key . "行商品名称".$goods_name."新增失败<br/>";
		}
	}
	if($flag==true) {
		$Model->commit();
		$rt["sta"] = 1;
		$rt["msg"] = "导入成功";
		echo json_encode($rt);
		die;
	} else {
		$Model->rollback();
		$rt["sta"] = 0;
		$rt["msg"] = "导入失败,失败原因".$error_msg;
		echo json_encode($rt);
		die;
	}
}


本文标签:

版权声明:若无特殊注明,本文皆为《菜鸟站长》原创,转载请保留文章出处。

本文链接:thinkphp3.2.3的excel导入数据事务处理代码片段 - https://wlphp.com/?post=290

发表评论

电子邮件地址不会被公开。 必填项已用*标注