我们知道TP5有insetall函数可以批量插入,但是如果一次插入10000条数据呢 同时插入肯定会卡死,所以我们分批插入
$house = Db_One('house')->where('b_id','eq',$data['scope_id'])->select();
$add_list = [];
//遍历进行生成操作
foreach($house as $key=>$val){
//组合数据
$add['title'] = $data['title'];
$add['b_id'] = $val['b_id'];
$add['u_id'] = $val['u_id'];
$add['h_id'] = $val['title'];
$add['bill_id'] = $class['id'];
//根据type判断使用对应的单位------------------转换思路 代入式,值是确定的情况下使用
$bill_unit = config('config.bill_unit');
$bill_type = config('config.bill_type');
$details_name = $bill_type[$class['type']]['value'];
$count = $bill_type[$class['type']]['name'];//12
$args = $bill_unit[$class['unit']]['name'];//area对应的面积值,读书
$unit = $bill_unit[$class['unit']]['value'];//单位 元/平方
$money = $val["$args"]*$class['price']*$count.'元';
$add['origin_money'] = $money;
$add['money'] = $add['origin_money'];
$add['pay_time'] = strtotime($data['pay_time']);
$add['score_starttime'] = strtotime($data['score_starttime']);
$add['score_endtime'] = strtotime($data['score_endtime']);
$add['bill_details'] = date("Y").'年'.$details_name.':'.$val["$args"].'*'.$class['price'].$unit.'*'.$count.'='.$money;
$add_list[] = $add;
}
//批量插入数据标签:批次,bill,add,class,插入,tp5,type,id,unit From: https://www.cnblogs.com/2019piggy/p/17202286.html
$num = 100;//每次导入条数
$limit = ceil(count($add_list)/$num);
for($i=1;$i<=$limit;$i++){
$offset = ($i-1)*$num;
$data_pill = array_slice($add_list,$offset,$num);
if(!Db_one('bill')->insertAll($add_list)){
$this->error = '批量添加数据失败';
return false;
}
}