Commit b0c2fb23 by liuyingkang

refactor(查询逻辑): 将ORM查询重构为原生SQL以提高性能

将多个文件中的ORM查询重构为原生SQL,减少数据库查询的复杂性并提高性能。主要修改包括将`DB::table`和`DB::select`替换为直接SQL语句,并优化了日期范围和分组条件的处理。
parent 5f87eaa0
......@@ -103,11 +103,11 @@ protected function grid()
$filter->panel();
//$filter->like('lnum', '律师编号')->width(3);
$filter->like('lname', '律师名称')->width(3);
// $filter->like('cnum', '合同编号')->width(3);
$filter->like('cnum', '合同编号')->width(3);
// $filter->equal('ctype', '合同类型')->select(ModelsCovenant::CTYPE)->width(3);
// $filter->like('principal', '委托人')->width(3);
// $filter->between('received_at', '收款日期')->date()->width(4);
// $filter->between('invoiced_at', '开票日期')->date()->width(4);
$filter->between('received_at', '收款日期')->date()->width(4);
$filter->between('invoiced_at', '开票日期')->date()->width(4);
});
// 添加导出按钮
......
......@@ -62,51 +62,50 @@ public function getList($param)
$received_at = $param['received_at'] ?? [];
$invoiced_at = $param['invoiced_at'] ?? [];
DB::enableQueryLog();
$query = DB::table("covenant_receive_payment as rp")
->select(DB::raw('rp.cid, c.cname as covenant_name, c.ctype as covenant_type, c.number as covenant_num, c.principal, l.name as lname,
sum(CASE WHEN rp.rtype = 2 THEN rp.invoiced_money ELSE 0 END) as invoice_amount,
sum(CASE WHEN rp.rtype = 1 THEN rp.received_amount ELSE 0 END) as receipt_money'))
->leftJoin('covenant as c', 'rp.cid', '=', 'c.id')
->leftJoin('lawyer as l', 'rp.lawyer_id', '=', 'l.id');
// 构建基础SQL
$sql = "SELECT rp.cid, c.cname as covenant_name, c.ctype as covenant_type, c.number as covenant_num, c.principal, l.name as lname,
SUM(CASE WHEN rp.rtype = 2 THEN rp.invoiced_money ELSE 0 END) as invoice_amount,
SUM(CASE WHEN rp.rtype = 1 THEN rp.received_amount ELSE 0 END) as receipt_money
FROM covenant_receive_payment as rp
LEFT JOIN covenant as c ON rp.cid = c.id
LEFT JOIN lawyer as l ON rp.lawyer_id = l.id
WHERE rp.deleted_at IS NULL";
// 筛选条件
if (!empty($search['lname'])) {
$query->where('l.name', 'like', '%'.$search['lname'].'%');
$sql .= " AND l.name LIKE '%".$search['lname']."%'";
}
if (!empty($search['cnum'])) {
$query->where('c.number', 'like', '%'.$search['cnum'].'%');
$sql .= " AND c.number LIKE '%".$search['cnum']."%'";
}
if (!empty($search['ctype'])) {
$query->where('c.ctype', $search['ctype']);
$sql .= " AND c.ctype = '".$search['ctype']."'";
}
if (!empty($search['principal'])) {
$query->where('c.principal', 'like', '%'.$search['principal'].'%');
$sql .= " AND c.principal LIKE '%".$search['principal']."%'";
}
// 日期筛选
if (!empty($received_at)) {
if (!empty($received_at['start'])) {
$query->where('rp.received_at', '>=', $received_at['start']);
$sql .= " AND rp.received_at >= '".$received_at['start']."'";
}
if (!empty($received_at['end'])) {
$query->where('rp.received_at', '<=', $received_at['end']);
$sql .= " AND rp.received_at <= '".$received_at['end']."'";
}
}
if (!empty($invoiced_at)) {
if (!empty($invoiced_at['start'])) {
$query->where('rp.invoiced_at', '>=', $invoiced_at['start']);
$sql .= " AND rp.invoiced_at >= '".$invoiced_at['start']."'";
}
if (!empty($invoiced_at['end'])) {
$query->where('rp.invoiced_at', '<=', $invoiced_at['end']);
$sql .= " AND rp.invoiced_at <= '".$invoiced_at['end']."'";
}
}
$records = $query->groupBy('rp.cid')->get();
$sql .= " GROUP BY rp.cid, c.cname, c.ctype, c.number, c.principal, l.name";
$records = DB::select($sql);
// 计算合计
$total_invoice_amount = 0;
......@@ -152,4 +151,4 @@ public function getList($param)
return $data;
}
}
\ No newline at end of file
}
\ No newline at end of file
......@@ -87,9 +87,11 @@ public function get(Grid\Model $model)
// 获取列表数据
public function getList(array $param)
{
$currentPage = max((int)($param['current_page'] ?? 1), 1); // 确保当前页码至少为1
$prePage = max((int)($param['per_page'] ?? 20), 1); // 确保每页至少显示1条记录
$offset_start = ($currentPage - 1) * $prePage; // 使用currentPage重新计算offset
$currentPage = max((int)($param['current_page'] ?? 1), 1);
$prePage = max((int)($param['per_page'] ?? 20), 1);
$offset_start = ($currentPage - 1) * $prePage;
// 获取筛选条件
$lnum = $param['search']['lnum'] ?? '';
$lname = $param['search']['lname'] ?? '';
$cnum = $param['search']['cnum'] ?? '';
......@@ -97,128 +99,116 @@ public function getList(array $param)
$principal = $param['search']['principal'] ?? '';
$invoiced_at = $param['search']['invoiced_at'] ?? [];
$received_at = $param['search']['received_at'] ?? [];
$list = [];
DB::enableQueryLog();
$note_monitor = DB::table("covenant_receive_payment as rp")
->select(DB::raw('rp.cid, c.cname,c.ctype,c.number as cnum,c.principal,c.principal_id,l.number as lnum,l.name as lname,sum(invoiced_money) as invoiced_money,sum(received_amount) as received_amount'))
//->select(['rp.cid', 'c.cname', 'c.ctype', 'c.number as cnum', 'c.principal', 'l.number as lnum', 'l.name as lname', 'sum(invoiced_money) as invoiced_money'])
->leftJoin('covenant as c', 'rp.cid', '=', 'c.id')
->leftJoin('lawyer as l', 'rp.lawyer_id', '=', 'l.id')
->whereNull('rp.deleted_at');
// 构建基础SQL
$sql = "SELECT rp.cid, c.cname, c.ctype, c.number as cnum, c.principal, c.principal_id,
l.number as lnum, l.name as lname,
SUM(invoiced_money) as invoiced_money,
SUM(received_amount) as received_amount
FROM covenant_receive_payment as rp
LEFT JOIN covenant as c ON rp.cid = c.id
LEFT JOIN lawyer as l ON rp.lawyer_id = l.id
WHERE rp.deleted_at IS NULL";
// 处理日期范围查询条件
if ($invoiced_at['start'] && $received_at['start']) {
$in_start = $invoiced_at['start'] ?? '';
$in_end = $invoiced_at['end'] ?? '';
$re_start = $received_at['start'] ?? '';
$re_end = $received_at['end'] ?? '';
if ($in_start && $re_start) {
if ($in_end && $re_end) {
$note_monitor = $note_monitor->whereBetween('rp.received_at', [$re_start, $re_end])
->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]);
$sql .= " AND (rp.received_at BETWEEN '$re_start' AND '$re_end 23:59:59'
OR rp.invoiced_at BETWEEN '$in_start' AND '$in_end 23:59:59')";
} else {
if ($in_end) {
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $re_start)
->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]);
$sql .= " AND (rp.received_at >= '$re_start'
OR rp.invoiced_at BETWEEN '$in_start' AND '$in_end 23:59:59')";
} elseif ($re_end) {
$note_monitor = $note_monitor->whereDate('rp.invoiced_at', '>', $in_start)
->orWhereBetween('rp.received_at', [$re_start, $re_end]);
$sql .= " AND (rp.invoiced_at >= '$in_start'
OR rp.received_at BETWEEN '$re_start' AND '$re_end 23:59:59')";
} else {
$note_monitor = $note_monitor->whereDate('rp.invoiced_at', '>', $in_start)
->orWhereDate('rp.received_at', '>', $re_start);
$sql .= " AND (rp.invoiced_at >= '$in_start'
OR rp.received_at >= '$re_start')";
}
}
}
} elseif ($invoiced_at['start'] && !$received_at['start']) { //开票日期
} elseif ($invoiced_at['start'] && !$received_at['start']) {
$start = $invoiced_at['start'] ?? '';
$end = $invoiced_at['end'] ?? '';
if ($start) {
if ($end) {
$note_monitor = $note_monitor->whereBetween('rp.received_at', [$start, $end]);
$sql .= " AND rp.received_at BETWEEN '$start' AND '$end 23:59:59'";
} else {
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $start);
$sql .= " AND rp.received_at >= '$start'";
}
}
//
} elseif ($received_at['start'] && !$invoiced_at['start']) { //收款日期
} elseif ($received_at['start'] && !$invoiced_at['start']) {
$start = $received_at['start'] ?? '';
$end = $received_at['end'] ?? '';
if ($start) {
if ($end) {
$note_monitor = $note_monitor->whereBetween('rp.received_at', [$start, $end]);
$sql .= " AND rp.received_at BETWEEN '$start' AND '$end 23:59:59'";
} else {
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $start);
$sql .= " AND rp.received_at >= '$start'";
}
}
}
// 添加分组条件
$sql .= " GROUP BY rp.cid, c.cname, c.ctype, c.number, c.principal, c.principal_id, l.number, l.name";
// 添加having条件
if ($lnum) {
$note_monitor = $note_monitor->having("lnum", $lnum);
$sql .= " HAVING lnum = '$lnum'";
}
if ($lname) {
$note_monitor = $note_monitor->having("lname", $lname);
$sql .= " HAVING lname = '$lname'";
}
if ($cnum) {
$note_monitor = $note_monitor->having("cnum", $cnum);
$sql .= " HAVING cnum = '$cnum'";
}
if ($ctype) {
$note_monitor = $note_monitor->having("ctype", $ctype);
$sql .= " HAVING ctype = '$ctype'";
}
if ($principal) {
$note_monitor = $note_monitor->having("principal", $principal);
}
// 计算列表总数
$count = 0;
try {
$cObj = clone $note_monitor; // 克隆查询构建器以避免影响后续查询
$count = $cObj->groupBy('rp.cid')->get()->count(); // 使用实际的记录数
} catch (\Exception $e) {
Log::add('Count error:', ['message' => $e->getMessage()]);
$count = 0;
$sql .= " HAVING principal = '$principal'";
}
// 获取列表
// 添加分页
$sql .= " LIMIT $prePage OFFSET $offset_start";
// 执行查询
$results = DB::select($sql);
// 处理结果
$data = [];
try {
$list = $note_monitor->groupBy('rp.cid')
->when($prePage > 0, function($query) use ($prePage, $offset_start) {
return $query->limit($prePage)->offset($offset_start);
})
->get();
// 获取并打印最后一个查询
$queries = DB::getQueryLog();
$lastQuery = end($queries);
Log::add('Last SQL Query:', [
'sql' => $lastQuery['query'],
'bindings' => $lastQuery['bindings'],
'time' => $lastQuery['time']
]);
foreach ($list as $key => $item) {
$tmp = [];
$tmp['lnum'] = $item->lnum;
$tmp['lname'] = $item->lname;
$tmp['covenant_num'] = $item->cnum;
$tmp['covenant_name'] = $item->cname;
$tmp['covenant_type'] = $item->ctype ? ModelsCovenant::CTYPE[$item->ctype] : '';
$tmp['principal'] = $item->principal;
$tmp['invoice_amount'] = number_format($item->invoiced_money, 2);
$tmp['receipt_money'] = number_format($item->received_amount, 2);
$receipt_noinvoice = $item->received_amount - $item->invoiced_money;
$tmp['receipt_noinvoice'] = $receipt_noinvoice > 0 ? number_format($receipt_noinvoice, 2) : '-';
$invoice_noreceipt = $item->invoiced_money - $item->received_amount;
$tmp['invoice_noreceipt'] = $invoice_noreceipt > 0 ? number_format($invoice_noreceipt, 2) : '-';
array_push($data, $tmp);
}
} catch (\Exception $e) {
Log::add('List error:', ['message' => $e->getMessage()]);
$data = [];
foreach ($results as $item) {
$tmp = [];
$tmp['lnum'] = $item->lnum;
$tmp['lname'] = $item->lname;
$tmp['covenant_num'] = $item->cnum;
$tmp['covenant_name'] = $item->cname;
$tmp['covenant_type'] = $item->ctype ? ModelsCovenant::CTYPE[$item->ctype] : '';
$tmp['principal'] = $item->principal;
$tmp['invoice_amount'] = number_format($item->invoiced_money, 2);
$tmp['receipt_money'] = number_format($item->received_amount, 2);
$receipt_noinvoice = $item->received_amount - $item->invoiced_money;
$tmp['receipt_noinvoice'] = $receipt_noinvoice > 0 ? number_format($receipt_noinvoice, 2) : '-';
$invoice_noreceipt = $item->invoiced_money - $item->received_amount;
$tmp['invoice_noreceipt'] = $invoice_noreceipt > 0 ? number_format($invoice_noreceipt, 2) : '-';
array_push($data, $tmp);
}
// 获取总数
$countSql = "SELECT COUNT(*) as total FROM ($sql) as subquery";
$total = DB::select($countSql)[0]->total;
return [
'total' => $count,
'total' => $total,
'subjects' => $data
];
}
......
......@@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Command\Log;
class CovenantReceivePayment extends Model
{
......@@ -487,34 +488,42 @@ public static function getTotalAccumulationFund($number, $received_at = [])
return $total_money;
}
//开票汇总表 总收款金额
public static function getTotalReceivedAmount($params, $received_at, $invoiced_at)
//开票汇总表 总收款金额【备份】
public static function getTotalReceivedAmountBak($params, $received_at, $invoiced_at)
{
// 获取筛选条件
$lnum = $params['lnum'] ?? '';
$lname = $params['lname'] ?? '';
$cnum = $params['cnum'] ?? '';
$ctype = $params['ctype'] ?? '';
$principal = $params['principal'] ?? '';
$lname = $params['lname'] ?? ''; // 律师姓名
// $lnum = $params['lnum'] ?? ''; // 律师编号
// $cnum = $params['cnum'] ?? ''; // 合同编号
// $ctype = $params['ctype'] ?? ''; // 合同类型
// $principal = $params['principal'] ?? ''; // 负责人
// 开启SQL日志记录
DB::enableQueryLog();
// 构建基础查询
$note_monitor = DB::table("covenant_receive_payment as rp")
->select(DB::raw('rp.cid, c.cname,c.ctype,c.number as cnum,c.principal,c.principal_id,l.number as lnum,l.name as lname,sum(invoiced_money) as invoiced_money,sum(received_amount) as received_amount'))
//->select(['rp.cid', 'c.cname', 'c.ctype', 'c.number as cnum', 'c.principal', 'l.number as lnum', 'l.name as lname', 'sum(invoiced_money) as invoiced_money'])
->leftJoin('covenant as c', 'rp.cid', '=', 'c.id')
->leftJoin('lawyer as l', 'rp.lawyer_id', '=', 'l.id')
->where('rp.rtype', 1);
->leftJoin('covenant as c', 'rp.cid', '=', 'c.id') // 关联合同表
->leftJoin('lawyer as l', 'rp.lawyer_id', '=', 'l.id') // 关联律师表
->where('rp.rtype', 1); // 过滤条件:收款类型为1,1: 收款 2:开票
// 处理日期范围查询条件
if ($invoiced_at['start'] && $received_at['start']) {
$in_start = $invoiced_at['start'] ?? '';
$in_end = $invoiced_at['end'] ?? '';
$re_start = $received_at['start'] ?? '';
$re_end = $received_at['end'] ?? '';
// 同时存在开票日期和收款日期范围
if ($in_start && $re_start) {
if ($in_end && $re_end) {
// 完整日期范围查询
$note_monitor = $note_monitor->whereBetween('rp.received_at', [$re_start, $re_end])
->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]);
} else {
// 处理部分日期范围
if ($in_end) {
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $re_start)
->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]);
......@@ -527,10 +536,10 @@ public static function getTotalReceivedAmount($params, $received_at, $invoiced_a
}
}
}
} elseif ($invoiced_at['start'] && !$received_at['start']) { //开票日期
} elseif ($invoiced_at['start'] && !$received_at['start']) { //开票日期
$start = $invoiced_at['start'] ?? '';
$end = $invoiced_at['end'] ?? '';
if ($start) {
if ($end) {
$note_monitor = $note_monitor->whereBetween('rp.received_at', [$start, $end]);
......@@ -538,11 +547,10 @@ public static function getTotalReceivedAmount($params, $received_at, $invoiced_a
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $start);
}
}
//
} elseif ($received_at['start'] && !$invoiced_at['start']) { //收款日期
} elseif ($received_at['start'] && !$invoiced_at['start']) { // 仅收款日期
$start = $received_at['start'] ?? '';
$end = $received_at['end'] ?? '';
if ($start) {
if ($end) {
$note_monitor = $note_monitor->whereBetween('rp.received_at', [$start, $end]);
......@@ -551,126 +559,141 @@ public static function getTotalReceivedAmount($params, $received_at, $invoiced_a
}
}
}
if ($lnum) {
$note_monitor = $note_monitor->having("lnum", $lnum);
}
if ($lname) {
$note_monitor = $note_monitor->having("lname", $lname);
}
if ($cnum) {
$note_monitor = $note_monitor->having("cnum", $cnum);
}
if ($ctype) {
$note_monitor = $note_monitor->having("ctype", $ctype);
}
if ($principal) {
$note_monitor = $note_monitor->having("principal", $principal);
}
// 计算列表总数
// 添加其他筛选条件
// if ($lnum) {
// $note_monitor = $note_monitor->having("lnum", $lnum);
// }
// if ($cnum) {
// $note_monitor = $note_monitor->having("cnum", $cnum);
// }
// if ($ctype) {
// $note_monitor = $note_monitor->having("ctype", $ctype);
// }
// if ($principal) {
// $note_monitor = $note_monitor->having("principal", $principal);
// }
// 执行查询并计算总金额
$total = 0;
$list = $note_monitor->groupBy('rp.cid')->get();
$list = $note_monitor->groupBy('c.cname')->get();
$queries = DB::getQueryLog();
Log::add('dubug', $queries); // 记录SQL日志
if ($list->toArray()) {
foreach ($list as $item) {
$total += $item->received_amount;
$total += $item->received_amount; // 累加收款金额
}
}
return $total;
}
//开票汇总表 总开票金额
//开票汇总表 总收款金额
public static function getTotalReceivedAmount($params, $received_at, $invoiced_at)
{
return self::getTotalMoney($params, $received_at, $invoiced_at,1);
}
//开票汇总表 总收款金额
public static function getTotalInvoicedMoney($params, $received_at, $invoiced_at)
{
// 获取筛选条件
$lnum = $params['lnum'] ?? '';
$lname = $params['lname'] ?? '';
$cnum = $params['cnum'] ?? '';
$ctype = $params['ctype'] ?? '';
$principal = $params['principal'] ?? '';
return self::getTotalMoney($params, $received_at, $invoiced_at,2);
}
DB::enableQueryLog();
$note_monitor = DB::table("covenant_receive_payment as rp")
->select(DB::raw('rp.cid, c.cname,c.ctype,c.number as cnum,c.principal,c.principal_id,l.number as lnum,l.name as lname,sum(invoiced_money) as invoiced_money,sum(received_amount) as received_amount'))
//->select(['rp.cid', 'c.cname', 'c.ctype', 'c.number as cnum', 'c.principal', 'l.number as lnum', 'l.name as lname', 'sum(invoiced_money) as invoiced_money'])
->leftJoin('covenant as c', 'rp.cid', '=', 'c.id')
->leftJoin('lawyer as l', 'rp.lawyer_id', '=', 'l.id')
->where('rp.rtype', 2);
//开票汇总表 总开票金额
public static function getTotalMoney($params, $received_at, $invoiced_at,$rtype)
{
// 获取筛选条件
$lname = $params['lname'] ?? ''; // 律师姓名
// 构建基础SQL
$sql = "SELECT rp.cid, c.cname, c.ctype, c.number as cnum, c.principal, c.principal_id,
l.number as lnum, l.name as lname,
SUM(invoiced_money) as invoiced_money,
SUM(received_amount) as received_amount
FROM covenant_receive_payment as rp
LEFT JOIN covenant as c ON rp.cid = c.id
LEFT JOIN lawyer as l ON rp.lawyer_id = l.id
WHERE rp.rtype = ".$rtype;
// 处理日期范围查询条件
if ($invoiced_at['start'] && $received_at['start']) {
$in_start = $invoiced_at['start'] ?? '';
$in_end = $invoiced_at['end'] ?? '';
$re_start = $received_at['start'] ?? '';
$re_end = $received_at['end'] ?? '';
if ($in_start && $re_start) {
if ($in_end && $re_end) {
$note_monitor = $note_monitor->whereBetween('rp.received_at', [$re_start, $re_end])
->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]);
$sql .= " AND (rp.received_at BETWEEN '$re_start' AND '$re_end 23:59:59'
OR rp.invoiced_at BETWEEN '$in_start' AND '$in_end 23:59:59')";
} else {
if ($in_end) {
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $re_start)
->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]);
$sql .= " AND (rp.received_at >= '$re_start'
OR rp.invoiced_at BETWEEN '$in_start' AND '$in_end 23:59:59')";
} elseif ($re_end) {
$note_monitor = $note_monitor->whereDate('rp.invoiced_at', '>', $in_start)
->orWhereBetween('rp.received_at', [$re_start, $re_end]);
$sql .= " AND (rp.invoiced_at >= '$in_start'
OR rp.received_at BETWEEN '$re_start' AND '$re_end 23:59:59')";
} else {
$note_monitor = $note_monitor->whereDate('rp.invoiced_at', '>', $in_start)
->orWhereDate('rp.received_at', '>', $re_start);
$sql .= " AND (rp.invoiced_at >= '$in_start'
OR rp.received_at >= '$re_start')";
}
}
}
} elseif ($invoiced_at['start'] && !$received_at['start']) { //开票日期
} elseif ($invoiced_at['start'] && !$received_at['start']) {
$start = $invoiced_at['start'] ?? '';
$end = $invoiced_at['end'] ?? '';
if ($start) {
if ($end) {
$note_monitor = $note_monitor->whereBetween('rp.received_at', [$start, $end]);
$sql .= " AND rp.received_at BETWEEN '$start' AND '$end 23:59:59'";
} else {
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $start);
$sql .= " AND rp.received_at >= '$start'";
}
}
//
} elseif ($received_at['start'] && !$invoiced_at['start']) { //收款日期
} elseif ($received_at['start'] && !$invoiced_at['start']) {
$start = $received_at['start'] ?? '';
$end = $received_at['end'] ?? '';
if ($start) {
if ($end) {
$note_monitor = $note_monitor->whereBetween('rp.received_at', [$start, $end]);
$sql .= " AND rp.received_at BETWEEN '$start' AND '$end 23:59:59'";
} else {
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $start);
$sql .= " AND rp.received_at >= '$start'";
}
}
}
if ($lnum) {
$note_monitor = $note_monitor->having("lnum", $lnum);
}
// 添加分组和having条件
$sql .= " GROUP BY rp.cid, c.cname, c.ctype, c.number, c.principal, c.principal_id, l.number, l.name";
if ($lname) {
$note_monitor = $note_monitor->having("lname", $lname);
}
if ($cnum) {
$note_monitor = $note_monitor->having("cnum", $cnum);
$sql .= " HAVING lname = '$lname'";
}
if ($ctype) {
$note_monitor = $note_monitor->having("ctype", $ctype);
}
if ($principal) {
$note_monitor = $note_monitor->having("principal", $principal);
}
// 计算列表总数
Log::add('dubug', $sql); // 记录SQL日志
// 执行查询
$results = DB::select($sql);
// 计算总金额
$total = 0;
$list = $note_monitor->groupBy('rp.cid')->get();
if ($list->toArray()) {
foreach ($list as $item) {
$total += $item->invoiced_money;
foreach ($results as $result) {
if ($rtype == 1) {
$total += $result->received_amount;
} else {
$total += $result->invoiced_money;
}
}
$queries = DB::getQueryLog();
return $total;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment