编辑add方法:
~~~
public function add()
{
$activeuser = UserModel::where('status',1)->find();
$this->assign('activeuser', $activeuser['username']);
return $this->fetch();
}
~~~
模板文件:
/apps/index/view/order/add.html
~~~
{layout name="layout" /}
<div class="container">
<div id="templateadd" class="formgrid">
<div class="sorting">
<div class="ui8-select">
<span class="active-option">当前账户:<em>{$activeuser}</em></span>
</div>
</div>
<form action="http://127.0.0.1/tp5/public/index.php/index/order/upload" enctype="multipart/form-data" method="post">
<table class="formbox" cellpadding="1" cellspacing="0">
<tbody>
<tr>
<td><input type="file" name="csv" /></td>
</tr>
</tbody>
</table>
<table class="submitbox" cellpadding="1" cellspacing="0">
<tbody>
<tr>
<td><button type="submit" tabindex="100" class="pn pnc btn btn-small btn-green"><strong>上传</strong></button></td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
~~~
打开浏览器
http://127.0.0.1/tp5/public/index.php/index/order/add
页面输出类似
![](https://box.kancloud.cn/2016-07-28_579970f6eb1de.png)
上传订单报表
订单报表是CSV格式的外部文件
内容类似
~~~
"账期","分成用户nick","订单号","卖家NICK","订单金额","订购开始时间","订购结束时间","本月分摊金额","分成比例","合作方分成金额","备注
"
"201606","test222","107362570424","test22222","90.00","2016-06-20 00:00:00","2016-09-20 00:00:00","11.00","70%","7.70","null"
"201606","test222","307497150468","test2222234","15.00","2016-06-26 00:00:00","2016-09-26 00:00:00","0.83","70%","0.59","null"
"201606","test222","106388930044","test22222343","15.00","2016-04-07 00:00:00","2016-07-07 00:00:00","5.01","70%","3.51","null"
"201606","test222","105865170194","test222222","15.00","2016-03-06 00:00:00","2016-06-06 00:00:00","0.84","70%","0.59","null"
"201606","test222","106244970042","test2222245","15.00","2016-03-31 00:00:00","2016-06-30 00:00:00","4.84","70%","3.39","null"
~~~
编辑upload方法处理上传文件:
~~~
public function upload()
{
$file = request()->file('csv');
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads','data.csv');
if($info){
$datasrc = array();
while($array = $file->fgetcsv()) {
$datasrc[] = $array;
}
$sum_record = count($datasrc);
if($sum_record > 1){
Cache::set('name',$datasrc,36000);
$this->redirect('http://127.0.0.1/tp5/public/index.php/index/order/save/step/1');
}
}else{
echo $file->getError();
}
}
~~~
编辑save方法保存订单:
~~~
public function save($step = 1)
{
$activeuser = UserModel::where('status',1)->find();
$tpl = TplModel::where(['uid'=> $activeuser['uid'],'ttype'=> 1])->find();
$datasrc = Cache::get('name');
$sum_data = count($datasrc);
$endpoint = min(($step+100),$sum_data);
if($step < $sum_data){
for($i=$step;$i<$endpoint;$i++)
{
echo "STEP".$i." ";
if(is_numeric($datasrc[$i][2])){
$order = OrderModel::get(['uid'=> $activeuser['uid'],'ap'=>$datasrc[$i][0],'onum'=> $datasrc[$i][2]]);
if($order){
continue;
}else{
$neworder = new OrderModel;
$neworder->uid = $activeuser['uid'];
$neworder->ap = intval($datasrc[$i][0]);
$neworder->onum = $datasrc[$i][2];
$neworder->snick = $datasrc[$i][1];
$neworder->bnick = $datasrc[$i][3];
$neworder->stime = $datasrc[$i][5];
$neworder->etime = $datasrc[$i][6];
$neworder->oamount = floatval($datasrc[$i][4]);
if($tpl and $neworder->oamount >= 90){
$neworder->tid = $tpl['tid'];
}
$neworder->pamount = floatval($datasrc[$i][7]);
$neworder->damount = floatval($datasrc[$i][9]);
$neworder->trate = floatval($datasrc[$i][8])/100;
$neworder->remark = $datasrc[$i][10];
if($neworder->save()){
echo '订单'.$neworder->oid.'新增成功!';
}
}
}
}
$step = $step+100;
return $this->success('STEP'.$step,'http://127.0.0.1/tp5/public/index.php/index/order/save/step/'.$step,'',1);
}else{
return $this->success('录入成功','http://127.0.0.1/tp5/public/index.php/index/order/');
}
}
~~~