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;
......
...@@ -87,9 +87,11 @@ public function get(Grid\Model $model) ...@@ -87,9 +87,11 @@ public function get(Grid\Model $model)
// 获取列表数据 // 获取列表数据
public function getList(array $param) public function getList(array $param)
{ {
$currentPage = max((int)($param['current_page'] ?? 1), 1); // 确保当前页码至少为1 $currentPage = max((int)($param['current_page'] ?? 1), 1);
$prePage = max((int)($param['per_page'] ?? 20), 1); // 确保每页至少显示1条记录 $prePage = max((int)($param['per_page'] ?? 20), 1);
$offset_start = ($currentPage - 1) * $prePage; // 使用currentPage重新计算offset $offset_start = ($currentPage - 1) * $prePage;
// 获取筛选条件
$lnum = $param['search']['lnum'] ?? ''; $lnum = $param['search']['lnum'] ?? '';
$lname = $param['search']['lname'] ?? ''; $lname = $param['search']['lname'] ?? '';
$cnum = $param['search']['cnum'] ?? ''; $cnum = $param['search']['cnum'] ?? '';
...@@ -97,112 +99,100 @@ public function getList(array $param) ...@@ -97,112 +99,100 @@ public function getList(array $param)
$principal = $param['search']['principal'] ?? ''; $principal = $param['search']['principal'] ?? '';
$invoiced_at = $param['search']['invoiced_at'] ?? []; $invoiced_at = $param['search']['invoiced_at'] ?? [];
$received_at = $param['search']['received_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']) { if ($invoiced_at['start'] && $received_at['start']) {
$in_start = $invoiced_at['start'] ?? ''; $in_start = $invoiced_at['start'] ?? '';
$in_end = $invoiced_at['end'] ?? ''; $in_end = $invoiced_at['end'] ?? '';
$re_start = $received_at['start'] ?? ''; $re_start = $received_at['start'] ?? '';
$re_end = $received_at['end'] ?? ''; $re_end = $received_at['end'] ?? '';
if ($in_start && $re_start) { if ($in_start && $re_start) {
if ($in_end && $re_end) { if ($in_end && $re_end) {
$note_monitor = $note_monitor->whereBetween('rp.received_at', [$re_start, $re_end]) $sql .= " AND (rp.received_at BETWEEN '$re_start' AND '$re_end 23:59:59'
->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]); OR rp.invoiced_at BETWEEN '$in_start' AND '$in_end 23:59:59')";
} else { } else {
if ($in_end) { if ($in_end) {
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $re_start) $sql .= " AND (rp.received_at >= '$re_start'
->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]); OR rp.invoiced_at BETWEEN '$in_start' AND '$in_end 23:59:59')";
} elseif ($re_end) { } elseif ($re_end) {
$note_monitor = $note_monitor->whereDate('rp.invoiced_at', '>', $in_start) $sql .= " AND (rp.invoiced_at >= '$in_start'
->orWhereBetween('rp.received_at', [$re_start, $re_end]); OR rp.received_at BETWEEN '$re_start' AND '$re_end 23:59:59')";
} else { } else {
$note_monitor = $note_monitor->whereDate('rp.invoiced_at', '>', $in_start) $sql .= " AND (rp.invoiced_at >= '$in_start'
->orWhereDate('rp.received_at', '>', $re_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'] ?? ''; $start = $invoiced_at['start'] ?? '';
$end = $invoiced_at['end'] ?? ''; $end = $invoiced_at['end'] ?? '';
if ($start) { if ($start) {
if ($end) { 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 { } 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'] ?? ''; $start = $received_at['start'] ?? '';
$end = $received_at['end'] ?? ''; $end = $received_at['end'] ?? '';
if ($start) { if ($start) {
if ($end) { 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 { } 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) { if ($lnum) {
$note_monitor = $note_monitor->having("lnum", $lnum); $sql .= " HAVING lnum = '$lnum'";
} }
if ($lname) { if ($lname) {
$note_monitor = $note_monitor->having("lname", $lname); $sql .= " HAVING lname = '$lname'";
} }
if ($cnum) { if ($cnum) {
$note_monitor = $note_monitor->having("cnum", $cnum); $sql .= " HAVING cnum = '$cnum'";
} }
if ($ctype) { if ($ctype) {
$note_monitor = $note_monitor->having("ctype", $ctype); $sql .= " HAVING ctype = '$ctype'";
} }
if ($principal) { if ($principal) {
$note_monitor = $note_monitor->having("principal", $principal); $sql .= " 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;
} }
// 获取列表 // 添加分页
$data = []; $sql .= " LIMIT $prePage OFFSET $offset_start";
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) { // 执行查询
$results = DB::select($sql);
// 处理结果
$data = [];
foreach ($results as $item) {
$tmp = []; $tmp = [];
$tmp['lnum'] = $item->lnum; $tmp['lnum'] = $item->lnum;
$tmp['lname'] = $item->lname; $tmp['lname'] = $item->lname;
$tmp['covenant_num'] = $item->cnum; $tmp['covenant_num'] = $item->cnum;
$tmp['covenant_name'] = $item->cname; $tmp['covenant_name'] = $item->cname;
$tmp['covenant_type'] = $item->ctype ? ModelsCovenant::CTYPE[$item->ctype] : ''; $tmp['covenant_type'] = $item->ctype ? ModelsCovenant::CTYPE[$item->ctype] : '';
$tmp['principal'] = $item->principal; $tmp['principal'] = $item->principal;
$tmp['invoice_amount'] = number_format($item->invoiced_money, 2); $tmp['invoice_amount'] = number_format($item->invoiced_money, 2);
$tmp['receipt_money'] = number_format($item->received_amount, 2); $tmp['receipt_money'] = number_format($item->received_amount, 2);
...@@ -212,13 +202,13 @@ public function getList(array $param) ...@@ -212,13 +202,13 @@ public function getList(array $param)
$tmp['invoice_noreceipt'] = $invoice_noreceipt > 0 ? number_format($invoice_noreceipt, 2) : '-'; $tmp['invoice_noreceipt'] = $invoice_noreceipt > 0 ? number_format($invoice_noreceipt, 2) : '-';
array_push($data, $tmp); array_push($data, $tmp);
} }
} catch (\Exception $e) {
Log::add('List error:', ['message' => $e->getMessage()]); // 获取总数
$data = []; $countSql = "SELECT COUNT(*) as total FROM ($sql) as subquery";
} $total = DB::select($countSql)[0]->total;
return [ return [
'total' => $count, 'total' => $total,
'subjects' => $data 'subjects' => $data
]; ];
} }
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use App\Command\Log;
class CovenantReceivePayment extends Model class CovenantReceivePayment extends Model
{ {
...@@ -487,34 +488,42 @@ public static function getTotalAccumulationFund($number, $received_at = []) ...@@ -487,34 +488,42 @@ public static function getTotalAccumulationFund($number, $received_at = [])
return $total_money; 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'] ?? ''; // 律师姓名
$lname = $params['lname'] ?? ''; // $lnum = $params['lnum'] ?? ''; // 律师编号
$cnum = $params['cnum'] ?? ''; // $cnum = $params['cnum'] ?? ''; // 合同编号
$ctype = $params['ctype'] ?? ''; // $ctype = $params['ctype'] ?? ''; // 合同类型
$principal = $params['principal'] ?? ''; // $principal = $params['principal'] ?? ''; // 负责人
// 开启SQL日志记录
DB::enableQueryLog(); DB::enableQueryLog();
// 构建基础查询
$note_monitor = DB::table("covenant_receive_payment as rp") $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(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('covenant as c', 'rp.cid', '=', 'c.id') ->leftJoin('lawyer as l', 'rp.lawyer_id', '=', 'l.id') // 关联律师表
->leftJoin('lawyer as l', 'rp.lawyer_id', '=', 'l.id') ->where('rp.rtype', 1); // 过滤条件:收款类型为1,1: 收款 2:开票
->where('rp.rtype', 1);
// 处理日期范围查询条件
if ($invoiced_at['start'] && $received_at['start']) { if ($invoiced_at['start'] && $received_at['start']) {
$in_start = $invoiced_at['start'] ?? ''; $in_start = $invoiced_at['start'] ?? '';
$in_end = $invoiced_at['end'] ?? ''; $in_end = $invoiced_at['end'] ?? '';
$re_start = $received_at['start'] ?? ''; $re_start = $received_at['start'] ?? '';
$re_end = $received_at['end'] ?? ''; $re_end = $received_at['end'] ?? '';
// 同时存在开票日期和收款日期范围
if ($in_start && $re_start) { if ($in_start && $re_start) {
if ($in_end && $re_end) { if ($in_end && $re_end) {
// 完整日期范围查询
$note_monitor = $note_monitor->whereBetween('rp.received_at', [$re_start, $re_end]) $note_monitor = $note_monitor->whereBetween('rp.received_at', [$re_start, $re_end])
->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]); ->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]);
} else { } else {
// 处理部分日期范围
if ($in_end) { if ($in_end) {
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $re_start) $note_monitor = $note_monitor->whereDate('rp.received_at', '>', $re_start)
->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]); ->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]);
...@@ -527,7 +536,7 @@ public static function getTotalReceivedAmount($params, $received_at, $invoiced_a ...@@ -527,7 +536,7 @@ 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'] ?? ''; $start = $invoiced_at['start'] ?? '';
$end = $invoiced_at['end'] ?? ''; $end = $invoiced_at['end'] ?? '';
...@@ -538,8 +547,7 @@ public static function getTotalReceivedAmount($params, $received_at, $invoiced_a ...@@ -538,8 +547,7 @@ public static function getTotalReceivedAmount($params, $received_at, $invoiced_a
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $start); $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'] ?? ''; $start = $received_at['start'] ?? '';
$end = $received_at['end'] ?? ''; $end = $received_at['end'] ?? '';
...@@ -552,28 +560,32 @@ public static function getTotalReceivedAmount($params, $received_at, $invoiced_a ...@@ -552,28 +560,32 @@ public static function getTotalReceivedAmount($params, $received_at, $invoiced_a
} }
} }
if ($lnum) {
$note_monitor = $note_monitor->having("lnum", $lnum);
}
if ($lname) { if ($lname) {
$note_monitor = $note_monitor->having("lname", $lname); $note_monitor = $note_monitor->having("lname", $lname);
} }
if ($cnum) { // 添加其他筛选条件
$note_monitor = $note_monitor->having("cnum", $cnum); // if ($lnum) {
} // $note_monitor = $note_monitor->having("lnum", $lnum);
if ($ctype) { // }
$note_monitor = $note_monitor->having("ctype", $ctype); // if ($cnum) {
} // $note_monitor = $note_monitor->having("cnum", $cnum);
if ($principal) { // }
$note_monitor = $note_monitor->having("principal", $principal); // if ($ctype) {
} // $note_monitor = $note_monitor->having("ctype", $ctype);
// 计算列表总数 // }
// if ($principal) {
// $note_monitor = $note_monitor->having("principal", $principal);
// }
// 执行查询并计算总金额
$total = 0; $total = 0;
$list = $note_monitor->groupBy('rp.cid')->get(); $list = $note_monitor->groupBy('c.cname')->get();
$queries = DB::getQueryLog(); $queries = DB::getQueryLog();
Log::add('dubug', $queries); // 记录SQL日志
if ($list->toArray()) { if ($list->toArray()) {
foreach ($list as $item) { foreach ($list as $item) {
$total += $item->received_amount; $total += $item->received_amount; // 累加收款金额
} }
} }
...@@ -581,96 +593,107 @@ public static function getTotalReceivedAmount($params, $received_at, $invoiced_a ...@@ -581,96 +593,107 @@ public static function getTotalReceivedAmount($params, $received_at, $invoiced_a
} }
//开票汇总表 总开票金额 //开票汇总表 总收款金额
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) public static function getTotalInvoicedMoney($params, $received_at, $invoiced_at)
{ {
// 获取筛选条件 return self::getTotalMoney($params, $received_at, $invoiced_at,2);
$lnum = $params['lnum'] ?? ''; }
$lname = $params['lname'] ?? '';
$cnum = $params['cnum'] ?? '';
$ctype = $params['ctype'] ?? '';
$principal = $params['principal'] ?? '';
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']) { if ($invoiced_at['start'] && $received_at['start']) {
$in_start = $invoiced_at['start'] ?? ''; $in_start = $invoiced_at['start'] ?? '';
$in_end = $invoiced_at['end'] ?? ''; $in_end = $invoiced_at['end'] ?? '';
$re_start = $received_at['start'] ?? ''; $re_start = $received_at['start'] ?? '';
$re_end = $received_at['end'] ?? ''; $re_end = $received_at['end'] ?? '';
if ($in_start && $re_start) { if ($in_start && $re_start) {
if ($in_end && $re_end) { if ($in_end && $re_end) {
$note_monitor = $note_monitor->whereBetween('rp.received_at', [$re_start, $re_end]) $sql .= " AND (rp.received_at BETWEEN '$re_start' AND '$re_end 23:59:59'
->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]); OR rp.invoiced_at BETWEEN '$in_start' AND '$in_end 23:59:59')";
} else { } else {
if ($in_end) { if ($in_end) {
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $re_start) $sql .= " AND (rp.received_at >= '$re_start'
->orWhereBetween('rp.invoiced_at', [$in_start, $in_end]); OR rp.invoiced_at BETWEEN '$in_start' AND '$in_end 23:59:59')";
} elseif ($re_end) { } elseif ($re_end) {
$note_monitor = $note_monitor->whereDate('rp.invoiced_at', '>', $in_start) $sql .= " AND (rp.invoiced_at >= '$in_start'
->orWhereBetween('rp.received_at', [$re_start, $re_end]); OR rp.received_at BETWEEN '$re_start' AND '$re_end 23:59:59')";
} else { } else {
$note_monitor = $note_monitor->whereDate('rp.invoiced_at', '>', $in_start) $sql .= " AND (rp.invoiced_at >= '$in_start'
->orWhereDate('rp.received_at', '>', $re_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'] ?? ''; $start = $invoiced_at['start'] ?? '';
$end = $invoiced_at['end'] ?? ''; $end = $invoiced_at['end'] ?? '';
if ($start) { if ($start) {
if ($end) { 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 { } 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'] ?? ''; $start = $received_at['start'] ?? '';
$end = $received_at['end'] ?? ''; $end = $received_at['end'] ?? '';
if ($start) { if ($start) {
if ($end) { 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 { } else {
$note_monitor = $note_monitor->whereDate('rp.received_at', '>', $start); $sql .= " AND rp.received_at >= '$start'";
} }
} }
} }
if ($lnum) { // 添加分组和having条件
$note_monitor = $note_monitor->having("lnum", $lnum); $sql .= " GROUP BY rp.cid, c.cname, c.ctype, c.number, c.principal, c.principal_id, l.number, l.name";
}
if ($lname) { if ($lname) {
$note_monitor = $note_monitor->having("lname", $lname); $sql .= " 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); Log::add('dubug', $sql); // 记录SQL日志
}
// 计算列表总数 // 执行查询
$results = DB::select($sql);
// 计算总金额
$total = 0; $total = 0;
$list = $note_monitor->groupBy('rp.cid')->get(); foreach ($results as $result) {
if ($list->toArray()) { if ($rtype == 1) {
foreach ($list as $item) { $total += $result->received_amount;
$total += $item->invoiced_money; } else {
$total += $result->invoiced_money;
} }
} }
$queries = DB::getQueryLog();
return $total; 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