添加结算方法,统计5个月内的订单记录: ~~~ public function account() { $time = time(); $nickarray = $sumarray = array(); $activeuser = UserModel::where('status',1)->find(); $uid =$activeuser['uid']; $minap = date('Ym',strtotime("-5 month", $time)); if($uid){ $tpllist = TplModel::all(['uid'=>$uid]); //echo "<BR>==="; foreach($tpllist as $tpl){ $tid = $tpl['tid']; $pnick = $tpl['pnick']; $dnick = $tpl['dnick']; $onick1 = $tpl['onick1']; $onick2 = $tpl['onick2']; $prate = $tpl['prate']; $drate = $tpl['drate']; $orate1 = $tpl['orate1']; $orate2 = $tpl['orate2']; if($pnick!="" && !in_array($pnick, $nickarray)){ $nickarray[] = $pnick; $sumarray[$pnick] = array(); } if($dnick!="" && !in_array($dnick, $nickarray)){ $nickarray[] = $dnick; $sumarray[$dnick] = array(); } if($onick1!="" && !in_array($onick1, $nickarray)){ $nickarray[] = $onick1; $sumarray[$onick1] = array(); } if($onick2!="" && !in_array($onick2, $nickarray)){ $nickarray[] = $onick2; $sumarray[$onick2] = array(); } if($pnick!="" || $dnick!="" || $onick1!="" || $onick2!=""){ //$sumorder = OrderModel::field('ap,sum(\'damount\') as sda')->where('uid',$uid)->where('tid',$tid)->where('ap','>',$minap)->group('ap'); $sumorder = Db::name('tto_order')->field('*,sum(damount) as sda') ->where('uid',$uid) ->where('tid',$tid) ->where('ap','>',$minap) ->group('ap') ->select(); //print_r($sumorder); if(!empty($sumorder)){ foreach($sumorder as $data){ if($pnick!=""){ $sumarray[$pnick][$data['ap']] += $data['sda'] * $prate; } if($dnick!=""){ $sumarray[$dnick][$data['ap']] += $data['sda'] * $drate; } if($onick1!=""){ $sumarray[$onick1][$data['ap']] += $data['sda'] * $orate1; } if($onick2!=""){ $sumarray[$onick2][$data['ap']] += $data['sda'] * $orate2; } } } } } while (list($key, $val) = each($sumarray)){ while (list($k, $v) = each($val)){ $account = AcctModel::get(['uid'=> $uid,'ap'=> $k,'anick'=> $key]); if($account){ $account->amount = $v; if (false !== $account->save()) { echo '更新记录成功<br />'; } else { return $account->getError(); } }else{ $acct['uid'] = $uid; $acct['ap'] = $k; $acct['anick'] = $key; $acct['amount'] = $v; if ($result = AcctModel::create($acct)) { echo '记录[ ' . $result->anick . ':' . $result->aid . ' ]新增成功<br />'; } else { return '新增出错'; } } } } return $this->success('更新成功'); }else{ return $this->error('未选择用户'); } } ~~~ 方法中默认使用$sumarray[$pnick][$data['ap']]数组会报错,所以修改了警告文件: /thinkphp/library/think/Error.php ~~~ error_reporting(E_ALL&~E_NOTICE); ~~~ 没有找到在配置文件中修改警告级别的地方,暂时先这样。 方法中$sumorder没有使用模型获取,而是直接使用了数据库查询。嗯,是的,不会。没找到模型的使用方法。泪牛满面ing......