Commit b0c2fb23 by liuyingkang

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

将多个文件中的ORM查询重构为原生SQL,减少数据库查询的复杂性并提高性能。主要修改包括将`DB::table`和`DB::select`替换为直接SQL语句,并优化了日期范围和分组条件的处理。
parent 5f87eaa0
...@@ -103,11 +103,11 @@ protected function grid() ...@@ -103,11 +103,11 @@ protected function grid()
$filter->panel(); $filter->panel();
//$filter->like('lnum', '律师编号')->width(3); //$filter->like('lnum', '律师编号')->width(3);
$filter->like('lname', '律师名称')->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->equal('ctype', '合同类型')->select(ModelsCovenant::CTYPE)->width(3);
// $filter->like('principal', '委托人')->width(3); // $filter->like('principal', '委托人')->width(3);
// $filter->between('received_at', '收款日期')->date()->width(4); $filter->between('received_at', '收款日期')->date()->width(4);
// $filter->between('invoiced_at', '开票日期')->date()->width(4); $filter->between('invoiced_at', '开票日期')->date()->width(4);
}); });
// 添加导出按钮 // 添加导出按钮
......
...@@ -62,51 +62,50 @@ public function getList($param) ...@@ -62,51 +62,50 @@ public function getList($param)
$received_at = $param['received_at'] ?? []; $received_at = $param['received_at'] ?? [];
$invoiced_at = $param['invoiced_at'] ?? []; $invoiced_at = $param['invoiced_at'] ?? [];
DB::enableQueryLog(); // 构建基础SQL
$query = DB::table("covenant_receive_payment as rp") $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,
->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 = 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
sum(CASE WHEN rp.rtype = 1 THEN rp.received_amount ELSE 0 END) as receipt_money')) FROM covenant_receive_payment as rp
->leftJoin('covenant as c', 'rp.cid', '=', 'c.id') LEFT JOIN covenant as c ON rp.cid = c.id
->leftJoin('lawyer as l', 'rp.lawyer_id', '=', 'l.id'); LEFT JOIN lawyer as l ON rp.lawyer_id = l.id
WHERE rp.deleted_at IS NULL";
// 筛选条件 // 筛选条件
if (!empty($search['lname'])) { if (!empty($search['lname'])) {
$query->where('l.name', 'like', '%'.$search['lname'].'%'); $sql .= " AND l.name LIKE '%".$search['lname']."%'";
} }
if (!empty($search['cnum'])) { if (!empty($search['cnum'])) {
$query->where('c.number', 'like', '%'.$search['cnum'].'%'); $sql .= " AND c.number LIKE '%".$search['cnum']."%'";
} }
if (!empty($search['ctype'])) { if (!empty($search['ctype'])) {
$query->where('c.ctype', $search['ctype']); $sql .= " AND c.ctype = '".$search['ctype']."'";
} }
if (!empty($search['principal'])) { 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)) {
if (!empty($received_at['start'])) { 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'])) { 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)) {
if (!empty($invoiced_at['start'])) { 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'])) { 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; $total_invoice_amount = 0;
...@@ -152,4 +151,4 @@ public function getList($param) ...@@ -152,4 +151,4 @@ public function getList($param)
return $data; return $data;
} }
} }
\ No newline at end of file \ No newline at end of file
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