Commit 97056f91 by liuyingkang

refactor: 优化日志记录和SQL查询条件

- 注释掉不必要的日志记录以减少日志冗余
- 在SQL查询中添加 `deleted_at IS NULL` 条件以排除已删除记录
- 在日志记录中添加换行符以提高可读性
parent 2a9d3c9e
......@@ -154,7 +154,7 @@ public function export(Request $request)
{
$filename = $request->get('filename');
$param = json_decode($request->get('param'), true);
Log::add('debug2', '导出数据:'.json_encode($param));
//Log::add('debug2', '导出数据:'.json_encode($param));
// 如果参数中没有时间,再从GET请求中获取
if (empty($param['received_at']['start'])) {
......
......@@ -63,17 +63,19 @@ public function getList($param)
$received_at = $param['received_at'] ?? [];
$invoiced_at = $param['invoiced_at'] ?? [];
Log::add('debug3', '导出数据:'.json_encode($param));
//Log::add('debug3', '导出数据:'.json_encode($param));
// 构建基础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
LEFT JOIN covenant as c ON rp.cid = c.id AND c.deleted_at IS NULL
LEFT JOIN lawyer as l ON rp.lawyer_id = l.id AND l.deleted_at IS NULL
WHERE rp.deleted_at IS NULL";
// 筛选条件
if (!empty($search['lname'])) {
$sql .= " AND l.name LIKE '%".$search['lname']."%'";
......@@ -140,7 +142,7 @@ public function getList($param)
$records = DB::select($sql);
Log::add('debug4', '导出数据:'.$sql);
Log::add('debug-导出数据:', '导出数据:'.$sql);
// 计算合计
$total_invoice_amount = 0;
......
......@@ -106,8 +106,8 @@ public function getList(array $param)
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
LEFT JOIN covenant as c ON rp.cid = c.id AND c.deleted_at IS NULL
LEFT JOIN lawyer as l ON rp.lawyer_id = l.id AND l.deleted_at IS NULL
WHERE rp.deleted_at IS NULL";
// 处理日期范围查询条件
......@@ -180,7 +180,9 @@ public function getList(array $param)
// 添加分页
$sql .= " LIMIT $prePage OFFSET $offset_start";
Log::add('debug-list:', 'sql:'.$sql);
// 执行查询
$results = DB::select($sql);
......
......@@ -2,10 +2,10 @@
namespace App\Command;
class Log{
static public function add(string $logKey, mixed $logInfo) :void{
//判断当天的日志文件是否存在
$day = date('Y-m-d');
// 判断当天的日志文件是否存在
$day = date('Y-m-d'); // 在日期前添加换行符
$logFileName = 'runLog_'.$day.'.log';
$logPath = storage_path("logs/".$logFileName);
file_put_contents($logPath,date('【 H:i:s 】').$logKey.': '.print_r($logInfo,true),FILE_APPEND);
file_put_contents($logPath,"\n" . date('【 H:i:s 】').$logKey.': '.print_r($logInfo,true),FILE_APPEND);
}
}
......@@ -612,6 +612,8 @@ public static function getTotalMoney($params, $received_at, $invoiced_at,$rtype)
{
// 获取筛选条件
$lname = $params['lname'] ?? ''; // 律师姓名
Log::add('dubug-params', $params); // 记录SQL日志
// 构建基础SQL
$sql = "SELECT rp.cid, c.cname, c.ctype, c.number as cnum, c.principal, c.principal_id,
......@@ -619,9 +621,9 @@ public static function getTotalMoney($params, $received_at, $invoiced_at,$rtype)
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;
LEFT JOIN covenant as c ON rp.cid = c.id AND c.deleted_at IS NULL
LEFT JOIN lawyer as l ON rp.lawyer_id = l.id AND l.deleted_at IS NULL
WHERE rp.rtype = ".$rtype." AND rp.deleted_at IS NULL";
// 处理日期范围查询条件
if ($invoiced_at['start'] && $received_at['start']) {
......@@ -678,7 +680,7 @@ public static function getTotalMoney($params, $received_at, $invoiced_at,$rtype)
$sql .= " HAVING lname = '$lname'";
}
Log::add('dubug', $sql); // 记录SQL日志
Log::add('dubug-sql', $sql); // 记录SQL日志
// 执行查询
$results = DB::select($sql);
......
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