diff --git a/deescloud.exe b/deescloud.exe index 3fc4d27..ef032c3 100644 Binary files a/deescloud.exe and b/deescloud.exe differ diff --git a/main.go b/main.go index 1cf82d6..c3921bf 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "io" "math" "net/http" + "path/filepath" "sort" "encoding/json" @@ -1548,6 +1549,8 @@ func main() { http.HandleFunc("/deescloud/getQualityData", getQualityData) http.HandleFunc("/deescloud/upload", upload) http.HandleFunc("/deescloud/download_ds_file", download_ds_file) + http.HandleFunc("/deescloud/delete_ds_file", delete_ds_file) + http.HandleFunc("/deescloud/saveLinkData", saveLinkData) //监听服务 fmt.Println("listen server ...") @@ -3453,8 +3456,8 @@ func get_tool_warning_pdf(response http.ResponseWriter, request *http.Request) { fmt.Println("99", data) filename = export_tool_warning_pdf(req.OpUser, baseinfo, tw, data) + fmt.Println("filename", filename) resp.FileId = fmt.Sprintf(`%v.pdf`, filename) - jdata, _ := json.Marshal(resp) fmt.Fprintf(response, string(jdata)) @@ -3491,13 +3494,12 @@ func get_tool_mess_pdf(response http.ResponseWriter, request *http.Request) { if err != nil { fmt.Printf("!!! 调用出错: %v\n", err) } else { - jsonData, marshalErr := json.MarshalIndent(respData, "", " ") + _, marshalErr := json.MarshalIndent(respData, "", " ") if marshalErr != nil { // 将原始响应内容添加到切片 allRespData = append(allRespData, respData) fmt.Println("原始响应内容:", respData) } else { - fmt.Println(string(jsonData)) // 成功序列化也添加到切片 allRespData = append(allRespData, respData) } @@ -3508,7 +3510,8 @@ func get_tool_mess_pdf(response http.ResponseWriter, request *http.Request) { fmt.Println(string(fullData)) filename := GenerateInstrumentPDF(fullData, warnData) - resp.FileId = fmt.Sprintf(`%v.pdf`, filename) + fmt.Println("文件名------------------------:", filename) + resp.FileId = filename jdata, _ := json.Marshal(resp) fmt.Fprintf(response, string(jdata)) @@ -3519,8 +3522,6 @@ func get_tool_mess_pdf(response http.ResponseWriter, request *http.Request) { } func GenerateInstrumentPDF(fullData []byte, resp []Tool_warning) (filename string) { - fmt.Println("Tool_warning---------------------", resp) - respJSON, err := json.Marshal(resp) if err != nil { logs.Error("Tool_warning数据JSON序列化错误:", err) @@ -3533,6 +3534,7 @@ func GenerateInstrumentPDF(fullData []byte, resp []Tool_warning) (filename strin logs.Error("JSON解析错误:", err) return "" } + // 初始化PDF pdf := gopdf.GoPdf{} pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4}) @@ -3550,155 +3552,154 @@ func GenerateInstrumentPDF(fullData []byte, resp []Tool_warning) (filename strin colGap := 15.0 // 列间距 cellPadding := 5.0 // 表格单元格内边距 pageHeight := gopdf.PageSizeA4.H - margin*2 + minBottomMargin := 10.0 // 最小底部间距 + y := margin - // 主标题样式 + // 样式函数 titleStyle := func() { pdf.SetFont("simfang", "", 18) pdf.SetTextColor(0, 0, 0) } - // 节标题样式 sectionStyle := func() { pdf.SetFont("simfang", "", 14) - pdf.SetTextColor(30, 30, 150) // 深蓝色 + pdf.SetTextColor(30, 30, 150) } - // 表格标题样式 tableHeaderStyle := func() { pdf.SetFont("simfang", "", 12) - pdf.SetTextColor(255, 255, 255) // 白色 - pdf.SetFillColor(57, 99, 156) // 深蓝背景 + pdf.SetTextColor(255, 255, 255) + pdf.SetFillColor(57, 99, 156) } - // 表格内容样式 tableCellStyle := func() { pdf.SetFont("simfang", "", 11) pdf.SetTextColor(0, 0, 0) - pdf.SetFillColor(255, 255, 255) // 白背景 + pdf.SetFillColor(255, 255, 255) } - // 生成PDF报告(只生成一次,不为每个仪器单独生成页面) - pdf.AddPage() - y := margin + // 辅助函数:检查是否需要换页 + checkPageBreak := func(requiredHeight float64) bool { + return y+requiredHeight > pageHeight-minBottomMargin + } - // 获取井名(从第一个仪器获取,假设所有仪器属于同一口井) + // 使用示例 + if checkPageBreak(lineHeight * 2) { + pdf.AddPage() + y = margin + } + + // 生成PDF报告 + pdf.AddPage() + + // 获取井名 var wellName string if len(instruments) > 0 { wellName = instruments[0].WellName } + // 主标题 titleStyle() pdf.SetX(margin) pdf.SetY(y) - // 居中显示井名 - pageWidth := gopdf.PageSizeA4.W - titleWidth := pageWidth - margin*2 + titleWidth := gopdf.PageSizeA4.W - margin*2 var titleOption gopdf.CellOption titleOption.Align = gopdf.Center pdf.CellWithOption(&gopdf.Rect{W: titleWidth, H: lineHeight}, wellName, titleOption) y += lineHeight * 1.8 + + // 风险提示部分 if len(resp) > 0 { - // 使用更醒目的标题样式 - pdf.SetFont("simfang", "B", 12) // 加粗并增大字号 - pdf.SetTextColor(0, 0, 0) // 深红色 + pdf.SetFont("simfang", "B", 12) + pdf.SetTextColor(0, 0, 0) pdf.SetX(margin) pdf.SetY(y) pdf.Cell(nil, "本口井风险提示") y += lineHeight * 1.5 - // 显示井号和报告时间 + reportTime := time.Now().Format("2006-01-02 15:04:05") pdf.SetFont("simfang", "", 12) - pdf.SetTextColor(0, 0, 0) // 黑色 + pdf.SetTextColor(0, 0, 0) pdf.SetX(margin) pdf.SetY(y) - reportTime := time.Now().Format("2006-01-02 15:04:05") pdf.Cell(nil, fmt.Sprintf("井号:%s\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t报告时间:%s", wellName, reportTime)) y += lineHeight * 1.2 - pdf.Line(margin, y, pageWidth-margin, y) + pdf.Line(margin, y, gopdf.PageSizeA4.W-margin, y) y += 10 - // 添加风险预警标题 - pdf.SetFont("simfang", "B", 14) - pdf.SetTextColor(220, 20, 60) // 深红色 + + pdf.SetFont("simfang", "B", 16) + pdf.SetTextColor(220, 20, 60) pdf.SetX(margin) pdf.SetY(y) pdf.Cell(nil, "风险预警:") y += lineHeight * 1.2 - // 显示异常信息内容 + for _, warning := range resp { - if y > pageHeight-lineHeight-15 { // 预留底部间距 + if checkPageBreak(lineHeight * 1.2) { pdf.AddPage() y = margin + 15 } - // 根据flag决定显示内容和样式 var contextText string if warning.Flag == 0 { contextText = "正常" - pdf.SetTextColor(0, 128, 0) // 绿色 + pdf.SetTextColor(0, 128, 0) } else { contextText = warning.Context - pdf.SetTextColor(200, 0, 0) // 红色 + pdf.SetTextColor(200, 0, 0) } - // 拼接异常信息描述 - warningText := fmt.Sprintf("%s %s %s", - warning.Series, - warning.Instrument, - contextText) - - pdf.SetFont("simfang", "", 13) // 增大内容字号 - pdf.SetX(margin) // 与上方内容对齐,移除左边距偏移 + warningText := fmt.Sprintf("%s %s %s", warning.Series, warning.Instrument, contextText) + pdf.SetFont("simfang", "", 13) + pdf.SetX(margin) pdf.SetY(y) pdf.Cell(&gopdf.Rect{W: gopdf.PageSizeA4.W - margin*2, H: lineHeight}, warningText) - y += lineHeight * 1.2 // 增加行间距 + y += lineHeight * 1.2 } - // ... existing code ... - - // 更新Y坐标到内容下方 y += 15 } - // 遍历所有仪器,显示基本信息、上井记录和维保记录 + // 遍历所有仪器 for _, inst := range instruments { pdf.SetLineWidth(1) - pdf.SetStrokeColor(0, 0, 0) // 黑色 - pdf.Line(margin, y, pageWidth-margin, y) + pdf.SetStrokeColor(0, 0, 0) + pdf.Line(margin, y, gopdf.PageSizeA4.W-margin, y) y += 15 - // 2. 基本信息区块(双列布局) + + // 1. 基本信息区块 sectionStyle() pdf.SetX(margin) pdf.SetY(y) pdf.Cell(nil, "▍ 基本信息 ("+inst.Series+" "+inst.Instrument+")") y += lineHeight + formattedTime := inst.Time if inst.Time != "" { - // 尝试解析时间 if t, err := time.Parse(time.RFC3339, inst.Time); err == nil { formattedTime = t.Format("2006-01-02") } else if t, err := time.Parse("2006-01-02T15:04:05Z", inst.Time); err == nil { formattedTime = t.Format("2006-01-02") } } - // 基本信息分组(两列) + basicInfo := []struct { Label string Value string }{ - {"固件版本号:", inst.Version}, {"出厂日期:", formattedTime}, + {"入库日期:", ""}, {"当前工作时间:", inst.WorkTime + "小时"}, {"累计工作时间:", inst.WorkTimes + "小时"}, {"所属部门:", inst.Dept}, + {"固件版本号:", inst.Version}, } - // 计算列宽(页面宽度减去边距后平分) colWidth := (gopdf.PageSizeA4.W - margin*2 - colGap) / 2 - labelWidth := 100.0 // 标签宽度固定为100像素 - valueWidth := colWidth - labelWidth // 值宽度为剩余部分 + labelWidth := 100.0 + valueWidth := colWidth - labelWidth - // 绘制双列信息 for i := 0; i < len(basicInfo); i += 2 { - if y > pageHeight-lineHeight*2 { + if checkPageBreak(lineHeight * 2) { pdf.AddPage() y = margin } @@ -3707,19 +3708,19 @@ func GenerateInstrumentPDF(fullData []byte, resp []Tool_warning) (filename strin pdf.SetX(margin) pdf.SetY(y) pdf.SetFont("simfang", "", 12) - pdf.SetTextColor(80, 80, 80) // 灰色标签 + pdf.SetTextColor(80, 80, 80) pdf.Cell(&gopdf.Rect{W: labelWidth, H: lineHeight}, basicInfo[i].Label) pdf.SetX(margin + labelWidth) - pdf.SetTextColor(0, 0, 0) // 黑色值 + pdf.SetTextColor(0, 0, 0) if basicInfo[i].Value == "" { - pdf.SetTextColor(150, 150, 150) // 灰色表示无数据 + pdf.SetTextColor(150, 150, 150) pdf.Cell(&gopdf.Rect{W: valueWidth, H: lineHeight}, "无数据") } else { pdf.Cell(&gopdf.Rect{W: valueWidth, H: lineHeight}, basicInfo[i].Value) } - // 第二列(如果有) + // 第二列 if i+1 < len(basicInfo) { pdf.SetX(margin + colWidth + colGap) pdf.SetY(y) @@ -3729,7 +3730,7 @@ func GenerateInstrumentPDF(fullData []byte, resp []Tool_warning) (filename strin pdf.SetX(margin + colWidth + colGap + labelWidth) pdf.SetTextColor(0, 0, 0) if basicInfo[i+1].Value == "" { - pdf.SetTextColor(150, 150, 150) // 灰色表示无数据 + pdf.SetTextColor(150, 150, 150) pdf.Cell(&gopdf.Rect{W: valueWidth, H: lineHeight}, "无数据") } else { pdf.Cell(&gopdf.Rect{W: valueWidth, H: lineHeight}, basicInfo[i+1].Value) @@ -3740,16 +3741,83 @@ func GenerateInstrumentPDF(fullData []byte, resp []Tool_warning) (filename strin } y += sectionGap + // 2. 质检记录 + sectionStyle() + pdf.SetX(margin) + pdf.SetY(y) + pdf.Cell(nil, "▍ 产品质检记录") + y += lineHeight + + if inst.QualityData.ProcessRecord == "" && inst.QualityData.ProcessInspection == "" && inst.QualityData.FinalInspection == "" { + if checkPageBreak(lineHeight) { + pdf.AddPage() + y = margin + } + pdf.SetFont("simfang", "", 12) + pdf.SetTextColor(150, 150, 150) + pdf.SetX(margin) + pdf.SetY(y) + pdf.Cell(nil, "暂无质检记录") + y += lineHeight + } else { + qcItems := []struct { + Label string + Value string + }{ + {"生产过程记录:", inst.QualityData.ProcessRecord}, + {"生产过程检验:", inst.QualityData.ProcessInspection}, + {"成品检验记录:", inst.QualityData.FinalInspection}, + } + + for _, item := range qcItems { + // 计算文本高度 + lines, _ := pdf.SplitText(item.Value, gopdf.PageSizeA4.W-margin*2-labelWidth) + itemHeight := lineHeight * float64(max(1, len(lines))) + + if checkPageBreak(itemHeight) { + pdf.AddPage() + y = margin + } + + // 标签 + pdf.SetX(margin) + pdf.SetY(y) + pdf.SetFont("simfang", "", 12) + pdf.SetTextColor(80, 80, 80) + pdf.Cell(&gopdf.Rect{W: labelWidth, H: lineHeight}, item.Label) + + // 值 + pdf.SetX(margin + labelWidth) + pdf.SetTextColor(0, 0, 0) + if item.Value == "" { + pdf.SetTextColor(150, 150, 150) + pdf.Cell(&gopdf.Rect{W: valueWidth, H: lineHeight}, "无数据") + y += lineHeight + } else { + for j, line := range lines { + if j > 0 { + y += lineHeight + pdf.SetX(margin + labelWidth) + } + pdf.SetY(y + float64(j)*lineHeight) + pdf.Cell(&gopdf.Rect{W: gopdf.PageSizeA4.W - margin*2 - labelWidth, H: lineHeight}, line) + } + y += lineHeight * float64(len(lines)) + } + } + } + y += sectionGap + // 3. 上井记录表格 sectionStyle() pdf.SetX(margin) pdf.SetY(y) - pdf.Cell(nil, "▍ 上井记录") + pdf.Cell(nil, "▍ 仪器施工汇总") y += lineHeight // 表格参数 - colWidths := []float64{100, 80, 80, 80, 100} - headers := []string{"井名", "入井状态", "工作井深", "最高温度", "最高压力"} + colWidths := []float64{80, 60, 60, 60, 60, 60, 50, 100} + headers := []string{"井名", "入井状态", "工作井深", "最高温度", "最高压力", "仪器状况", "上井人", "报告"} if len(inst.WorkMessList) == 0 { // 没有上井记录时显示提示信息 @@ -3765,64 +3833,47 @@ func GenerateInstrumentPDF(fullData []byte, resp []Tool_warning) (filename strin pdf.Cell(nil, "暂无上井记录") y += lineHeight } else { - // 绘制表头 - if y > pageHeight-lineHeight*2 { - pdf.AddPage() - y = margin - } + // 表头高度 + headerHeight := lineHeight + cellPadding*2 + // 只在第一页绘制表头 tableHeaderStyle() - currX := margin + startX := margin + currX := startX + for i, width := range colWidths { // 绘制单元格(填充+边框) - pdf.SetFillColor(57, 99, 156) // 深蓝背景 - pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "F") // 先绘制填充 + pdf.SetFillColor(57, 99, 156) + pdf.RectFromUpperLeftWithStyle(currX, y, width, headerHeight, "F") pdf.SetLineWidth(0.1) - pdf.SetStrokeColor(255, 255, 255) // 白色边框 - pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "D") // 再绘制边框 + pdf.SetStrokeColor(255, 255, 255) + pdf.RectFromUpperLeftWithStyle(currX, y, width, headerHeight, "D") - // 为每个单元格单独设置文本位置 - pdf.SetX(currX + cellPadding) + // 表头居中 + text := headers[i] + textWidth, _ := pdf.MeasureTextWidth(text) + textX := currX + (width-textWidth)/2 + pdf.SetX(textX) pdf.SetY(y + cellPadding) - // 使用CellWithOption确保文本在正确位置显示 - pdf.Cell(&gopdf.Rect{W: width - cellPadding*2, H: lineHeight}, headers[i]) + pdf.Cell(&gopdf.Rect{W: width, H: lineHeight}, text) + currX += width } - y += lineHeight + cellPadding*2 + y += headerHeight // 绘制表格内容 tableCellStyle() for _, work := range inst.WorkMessList { - if y > pageHeight-lineHeight { - pdf.AddPage() - y = margin - // 新页需要重绘表头 - tableHeaderStyle() - currX := margin - for i, width := range colWidths { - pdf.SetFillColor(57, 99, 156) // 深蓝背景 - pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "F") // 先绘制填充 - pdf.SetLineWidth(0.1) - pdf.SetStrokeColor(255, 255, 255) // 白色边框 - pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "D") // 再绘制边框 - - pdf.SetX(currX + cellPadding) - pdf.SetY(y + cellPadding) - pdf.Cell(&gopdf.Rect{W: width - cellPadding*2, H: lineHeight}, headers[i]) - currX += width - } - y += lineHeight + cellPadding*2 - tableCellStyle() - } - - // 绘制行内容 - currX := margin + // 准备行数据 rowData := []string{ work.WellName, work.SampleField, work.ValueInterval + "米", work.GlobalMax1 + "℃", work.GlobalMax2 + "MPa", + work.Content, + work.PersonnelList, + work.Link, } // 处理空数据 @@ -3832,16 +3883,47 @@ func GenerateInstrumentPDF(fullData []byte, resp []Tool_warning) (filename strin } } + // 计算最大行数 + maxLines := 1 + cellLines := make([][]string, len(colWidths)) for i, width := range colWidths { + lines, _ := pdf.SplitText(rowData[i], width-cellPadding*2) + cellLines[i] = lines + if len(lines) > maxLines { + maxLines = len(lines) + } + } + + // 计算行高 + rowHeight := lineHeight*float64(maxLines) + cellPadding*2 + + // 检查是否需要换页(只考虑行高度) + if y+rowHeight > pageHeight-5 { // 保留5mm底部间距 + pdf.AddPage() + y = margin + // 注意:换页后不再绘制表头 + } + + // 绘制行内容 + currX = startX + for i, width := range colWidths { + // 绘制单元格边框 pdf.SetLineWidth(0.1) - pdf.SetStrokeColor(0, 0, 0) // 黑色边框 - pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "D") - pdf.SetX(currX + cellPadding) - pdf.SetY(y + cellPadding) - pdf.Cell(&gopdf.Rect{W: width - cellPadding*2, H: lineHeight}, rowData[i]) + pdf.SetStrokeColor(0, 0, 0) + pdf.RectFromUpperLeftWithStyle(currX, y, width, rowHeight, "D") + + // 绘制多行文本 + textY := y + cellPadding + for _, line := range cellLines[i] { + pdf.SetX(currX + cellPadding) + pdf.SetY(textY) + pdf.Cell(&gopdf.Rect{W: width - cellPadding*2, H: lineHeight}, line) + textY += lineHeight + } + currX += width } - y += lineHeight + cellPadding*2 + y += rowHeight } } y += sectionGap @@ -3854,108 +3936,54 @@ func GenerateInstrumentPDF(fullData []byte, resp []Tool_warning) (filename strin y += lineHeight if len(inst.GetLcmRespData) == 0 { - // 没有维保记录时显示提示信息 - if y > pageHeight-lineHeight*2 { + // 无数据提示(保持不变) + if y > pageHeight-lineHeight { pdf.AddPage() y = margin } - pdf.SetFont("simfang", "", 12) - pdf.SetTextColor(150, 150, 150) // 灰色表示无数据 + pdf.SetTextColor(150, 150, 150) pdf.SetX(margin) pdf.SetY(y) pdf.Cell(nil, "暂无维保记录") y += lineHeight } else { // 表格参数 - tableWidth := gopdf.PageSizeA4.W - margin*2 - // 确保最后一列有最小宽度 - minContentWidth := 200.0 - lastColWidth := tableWidth - 280 // 120+80+80=280 - if lastColWidth < minContentWidth { - lastColWidth = minContentWidth - tableWidth = 280 + lastColWidth // 重新计算表格总宽度 - } - - colWidths := []float64{120, 80, 80, lastColWidth} - headers := []string{"维保时间", "维保等级", "维保人员", "维保内容"} - - // 绘制表头 - if y > pageHeight-lineHeight*2 { - pdf.AddPage() - y = margin - } - - tableHeaderStyle() + colWidths := []float64{120, 80, 80, 140, 120} + headers := []string{"维保时间", "维保等级", "维保人员", "维保内容", "报告"} + headerHeight := lineHeight + cellPadding*2 startX := margin + + // 只在第一页绘制表头 + tableHeaderStyle() currX := startX - - // 绘制表头单元格 for i, width := range colWidths { - // 绘制单元格(填充+边框) - pdf.SetFillColor(57, 99, 156) // 深蓝背景 - pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "F") // 先绘制填充 + pdf.SetFillColor(57, 99, 156) + pdf.RectFromUpperLeftWithStyle(currX, y, width, headerHeight, "F") pdf.SetLineWidth(0.1) - pdf.SetStrokeColor(255, 255, 255) // 白色边框 - pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "D") // 再绘制边框 + pdf.SetStrokeColor(255, 255, 255) + pdf.RectFromUpperLeftWithStyle(currX, y, width, headerHeight, "D") - // 设置文本位置 - pdf.SetX(currX + cellPadding) + text := headers[i] + textWidth, _ := pdf.MeasureTextWidth(text) + textX := currX + (width-textWidth)/2 + pdf.SetX(textX) pdf.SetY(y + cellPadding) - - // 确保文本区域宽度不为负 - textWidth := width - cellPadding*2 - if textWidth < 10 { - textWidth = 10 - } - - // 绘制文本 - textRect := &gopdf.Rect{ - W: textWidth, - H: lineHeight, - } - pdf.Cell(textRect, headers[i]) - + pdf.Cell(&gopdf.Rect{W: width, H: lineHeight}, text) currX += width } - y += lineHeight + cellPadding*2 + y += headerHeight // 绘制表格内容 tableCellStyle() for _, repair := range inst.GetLcmRespData { - if y > pageHeight-lineHeight { - pdf.AddPage() - y = margin - // 新页需要重绘表头 - tableHeaderStyle() - currX = startX - for i, width := range colWidths { - pdf.SetFillColor(57, 99, 156) // 深蓝背景 - pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "F") // 先绘制填充 - pdf.SetLineWidth(0.1) - pdf.SetStrokeColor(255, 255, 255) // 白色边框 - pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "D") // 再绘制边框 - - pdf.SetX(currX + cellPadding) - pdf.SetY(y + cellPadding) - textWidth := width - cellPadding*2 - if textWidth < 10 { - textWidth = 10 - } - textRect := &gopdf.Rect{W: textWidth, H: lineHeight} - pdf.Cell(textRect, headers[i]) - currX += width - } - y += lineHeight + cellPadding*2 - tableCellStyle() - } - - currX = startX + // 准备行数据 rowData := []string{ repair.Time, repair.Repair_level, repair.Applicanter, repair.Note, + repair.Link, } // 处理空数据 @@ -3965,318 +3993,49 @@ func GenerateInstrumentPDF(fullData []byte, resp []Tool_warning) (filename strin } } - // 绘制内容单元格 + // 计算最大行数 + maxLines := 1 + cellLines := make([][]string, len(colWidths)) + for i, width := range colWidths { + lines, _ := pdf.SplitText(rowData[i], width-cellPadding*2) + cellLines[i] = lines + if len(lines) > maxLines { + maxLines = len(lines) + } + } + rowHeight := lineHeight*float64(maxLines) + cellPadding*2 + + // 检查是否需要换页(只考虑行高度) + if y+rowHeight > pageHeight-5 { // 保留5mm底部间距 + pdf.AddPage() + y = margin + // 注意:换页后不再绘制表头 + } + + // 绘制行 + currX = startX for i, width := range colWidths { - // 绘制单元格边框 pdf.SetLineWidth(0.1) - pdf.SetStrokeColor(0, 0, 0) // 黑色边框 - pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "D") + pdf.SetStrokeColor(0, 0, 0) + pdf.RectFromUpperLeftWithStyle(currX, y, width, rowHeight, "D") - // 设置文本位置 - pdf.SetX(currX + cellPadding) - pdf.SetY(y + cellPadding) - - // 确保文本区域宽度不为负 - textWidth := width - cellPadding*2 - if textWidth < 10 { - textWidth = 10 + textY := y + cellPadding + for _, line := range cellLines[i] { + pdf.SetX(currX + cellPadding) + pdf.SetY(textY) + pdf.Cell(&gopdf.Rect{W: width - cellPadding*2, H: lineHeight}, line) + textY += lineHeight } - - // 绘制文本 - textRect := &gopdf.Rect{ - W: textWidth, - H: lineHeight, - } - pdf.Cell(textRect, rowData[i]) - currX += width } - y += lineHeight + cellPadding*2 + y += rowHeight } } y += sectionGap + + // ... 后面的代码保持不变 ... + } - // ... existing code ... - - // for _, inst := range instruments { - // pdf.SetLineWidth(1) - // pdf.SetStrokeColor(0, 0, 0) // 黑色 - // pdf.Line(margin, y, pageWidth-margin, y) - // y += 15 - // // 2. 基本信息区块(双列布局) - // sectionStyle() - // pdf.SetX(margin) - // pdf.SetY(y) - // pdf.Cell(nil, "▍ 基本信息 ("+inst.Series+" "+inst.Instrument+")") - // y += lineHeight - - // // 基本信息分组(两列) - // basicInfo := []struct { - // Label string - // Value string - // }{ - // {"井名称:", inst.WellName}, - // {"版本号:", inst.Version}, - // {"系列号:", inst.Series}, - // {"仪器号:", inst.Instrument}, - // {"累计工作时间:", inst.WorkTime + "小时"}, - // {"所属部门:", inst.Dept}, - // {"设备状态:", inst.Content}, - // } - - // // 计算列宽(页面宽度减去边距后平分) - // colWidth := (gopdf.PageSizeA4.W - margin*2 - colGap) / 2 - - // // 绘制双列信息 - // for i := 0; i < len(basicInfo); i += 2 { - // if y > pageHeight-lineHeight*2 { - // pdf.AddPage() - // y = margin - // } - - // // 第一列 - // pdf.SetX(margin) - // pdf.SetY(y) - // pdf.SetFont("simfang", "", 12) - // pdf.SetTextColor(80, 80, 80) // 灰色标签 - // pdf.Cell(nil, basicInfo[i].Label) - - // pdf.SetX(margin + 80) // 标签固定宽度 - // pdf.SetTextColor(0, 0, 0) // 黑色值 - // pdf.Cell(nil, basicInfo[i].Value) - - // // 第二列(如果有) - // if i+1 < len(basicInfo) { - // pdf.SetX(margin + colWidth + colGap) - // pdf.SetY(y) - // pdf.SetTextColor(80, 80, 80) - // pdf.Cell(nil, basicInfo[i+1].Label) - - // pdf.SetX(margin + colWidth + colGap + 80) - // pdf.SetTextColor(0, 0, 0) - // pdf.Cell(nil, basicInfo[i+1].Value) - // } - - // y += lineHeight - // } - // y += sectionGap - - // // 3. 上井记录表格 - // if len(inst.WorkMessList) > 0 { - // sectionStyle() - // pdf.SetX(margin) - // pdf.SetY(y) - // pdf.Cell(nil, "▍ 上井记录") - // y += lineHeight - - // // 表格参数 - // colWidths := []float64{100, 80, 80, 80, 100} - // headers := []string{"井名", "入井状态", "工作井深", "最高温度", "最高压力"} - - // // 绘制表头 - // if y > pageHeight-lineHeight*2 { - // pdf.AddPage() - // y = margin - // } - - // tableHeaderStyle() - // currX := margin - // for i, width := range colWidths { - // // 绘制单元格(填充+边框) - // pdf.SetFillColor(57, 99, 156) // 深蓝背景 - // pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "F") // 先绘制填充 - // pdf.SetLineWidth(0.1) - // pdf.SetStrokeColor(255, 255, 255) // 白色边框 - // pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "D") // 再绘制边框 - - // // 为每个单元格单独设置文本位置 - // pdf.SetX(currX + cellPadding) - // pdf.SetY(y + cellPadding) - // // 使用CellWithOption确保文本在正确位置显示 - // pdf.Cell(&gopdf.Rect{W: width - cellPadding*2, H: lineHeight}, headers[i]) - // currX += width - // } - // y += lineHeight + cellPadding*2 - - // // 绘制表格内容 - // tableCellStyle() - // for _, work := range inst.WorkMessList { - // if y > pageHeight-lineHeight { - // pdf.AddPage() - // y = margin - // // 新页需要重绘表头 - // tableHeaderStyle() - // currX := margin - // for i, width := range colWidths { - // pdf.SetFillColor(57, 99, 156) // 深蓝背景 - // pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "F") // 先绘制填充 - // pdf.SetLineWidth(0.1) - // pdf.SetStrokeColor(255, 255, 255) // 白色边框 - // pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "D") // 再绘制边框 - - // pdf.SetX(currX + cellPadding) - // pdf.SetY(y + cellPadding) - // pdf.Cell(&gopdf.Rect{W: width - cellPadding*2, H: lineHeight}, headers[i]) - // currX += width - // } - // y += lineHeight + cellPadding*2 - // tableCellStyle() - // } - - // // 绘制行内容 - // currX := margin - // rowData := []string{ - // work.WellName, - // work.SampleField, - // work.ValueInterval + "米", - // work.GlobalMax1 + "℃", - // work.GlobalMax2 + "MPa", - // } - - // for i, width := range colWidths { - // pdf.SetLineWidth(0.1) - // pdf.SetStrokeColor(0, 0, 0) // 黑色边框 - // pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "D") - // pdf.SetX(currX + cellPadding) - // pdf.SetY(y + cellPadding) - // pdf.Cell(&gopdf.Rect{W: width - cellPadding*2, H: lineHeight}, rowData[i]) - // currX += width - // } - // y += lineHeight + cellPadding*2 - // } - // y += sectionGap - // } - - // // 4. 维保记录表格 - // if len(inst.GetLcmRespData) > 0 { - // sectionStyle() - // pdf.SetX(margin) - // pdf.SetY(y) - // pdf.Cell(nil, "▍ 维保记录") - // y += lineHeight - - // // 表格参数 - // tableWidth := gopdf.PageSizeA4.W - margin*2 - // // 确保最后一列有最小宽度 - // minContentWidth := 200.0 - // lastColWidth := tableWidth - 280 // 120+80+80=280 - // if lastColWidth < minContentWidth { - // lastColWidth = minContentWidth - // tableWidth = 280 + lastColWidth // 重新计算表格总宽度 - // } - - // colWidths := []float64{120, 80, 80, lastColWidth} - // headers := []string{"维保时间", "维保等级", "维保人员", "维保内容"} - - // // 绘制表头 - // if y > pageHeight-lineHeight*2 { - // pdf.AddPage() - // y = margin - // } - - // tableHeaderStyle() - // startX := margin - // currX := startX - - // // 绘制表头单元格 - // for i, width := range colWidths { - // // 绘制单元格(填充+边框) - // pdf.SetFillColor(57, 99, 156) // 深蓝背景 - // pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "F") // 先绘制填充 - // pdf.SetLineWidth(0.1) - // pdf.SetStrokeColor(255, 255, 255) // 白色边框 - // pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "D") // 再绘制边框 - - // // 设置文本位置 - // pdf.SetX(currX + cellPadding) - // pdf.SetY(y + cellPadding) - - // // 确保文本区域宽度不为负 - // textWidth := width - cellPadding*2 - // if textWidth < 10 { - // textWidth = 10 - // } - - // // 绘制文本 - // textRect := &gopdf.Rect{ - // W: textWidth, - // H: lineHeight, - // } - // pdf.Cell(textRect, headers[i]) - - // currX += width - // } - // y += lineHeight + cellPadding*2 - - // // 绘制表格内容 - // tableCellStyle() - // for _, repair := range inst.GetLcmRespData { - // if y > pageHeight-lineHeight { - // pdf.AddPage() - // y = margin - // // 新页需要重绘表头 - // tableHeaderStyle() - // currX = startX - // for i, width := range colWidths { - // pdf.SetFillColor(57, 99, 156) // 深蓝背景 - // pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "F") // 先绘制填充 - // pdf.SetLineWidth(0.1) - // pdf.SetStrokeColor(255, 255, 255) // 白色边框 - // pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "D") // 再绘制边框 - - // pdf.SetX(currX + cellPadding) - // pdf.SetY(y + cellPadding) - // textWidth := width - cellPadding*2 - // if textWidth < 10 { - // textWidth = 10 - // } - // textRect := &gopdf.Rect{W: textWidth, H: lineHeight} - // pdf.Cell(textRect, headers[i]) - // currX += width - // } - // y += lineHeight + cellPadding*2 - // tableCellStyle() - // } - - // currX = startX - // rowData := []string{ - // repair.Time, - // repair.Repair_level, - // repair.Applicanter, - // repair.Note, - // } - - // // 绘制内容单元格 - // for i, width := range colWidths { - // // 绘制单元格边框 - // pdf.SetLineWidth(0.1) - // pdf.SetStrokeColor(0, 0, 0) // 黑色边框 - // pdf.RectFromUpperLeftWithStyle(currX, y, width, lineHeight+cellPadding*2, "D") - - // // 设置文本位置 - // pdf.SetX(currX + cellPadding) - // pdf.SetY(y + cellPadding) - - // // 确保文本区域宽度不为负 - // textWidth := width - cellPadding*2 - // if textWidth < 10 { - // textWidth = 10 - // } - - // // 绘制文本 - // textRect := &gopdf.Rect{ - // W: textWidth, - // H: lineHeight, - // } - // pdf.Cell(textRect, rowData[i]) - - // currX += width - // } - // y += lineHeight + cellPadding*2 - // } - // y += sectionGap - // } - // } // 页码 pdf.SetFont("simfang", "", 10) @@ -4296,6 +4055,14 @@ func GenerateInstrumentPDF(fullData []byte, resp []Tool_warning) (filename strin return strconv.FormatInt(beginTime, 10) + ".pdf" } +// 辅助函数:获取最大值 +func max(a, b int) int { + if a > b { + return a + } + return b +} + func leftOneData(wellName string, IsMemory *bool) []VibrationStatData { var db string var cmdSql string @@ -5566,13 +5333,41 @@ func export_tool_warning_pdf(opuser string, baseinfo get_srr_base_resp, data []T // } func get_tool_warning_info(user string, wellname string) (resp []Tool_warning) { + logs.Info("get_tool_warning_infof方法--------------------------------", user) ps_info := get_ps_info(user) - + var db string + wellSql := fmt.Sprintf(`SELECT WELLName from WellInformation where WELLNameSource = '%s'`, wellname) + wellRow := sqlConn.QueryRow(wellSql) + wellRow.Scan(&db) + fmt.Println("db----------", db) // 第一个查询:获取仪器组合 - sel_sql := `select DISTINCT [2327], [2328] from REPAIRANDMENT where [2321] = @wellname AND [2327] IS NOT NULL` - rows, err := sqlConn.Query(sel_sql, sql.Named("wellname", wellname)) + if strings.HasPrefix(db, "LH2020-") || + strings.HasPrefix(db, "LH2021-") || + strings.HasPrefix(db, "LH2022-") || + strings.HasPrefix(db, "LH2023-") || + strings.HasPrefix(db, "LH2024-") { + conn, err := getOdbcConn("master") + if err != nil { + logs.Info("链接master库失败", err.Error()) + fmt.Println("链接master库失败", err.Error()) + } + defer conn.Close() + cmdSql := fmt.Sprintf("ALTER DATABASE [%s] SET ONLINE", db) + _, err = conn.Exec(cmdSql) + } + conn, err := getOdbcConn(db) + + if err != nil { + logs.Info("Connecting Error", err.Error()) + } + defer conn.Close() + sel_sql := fmt.Sprintf(`select DISTINCT [2327], [2328] from REPAIRANDMENT where [2321] = '%s' AND [2327] IS NOT NULL`, wellname) + logs.Info("第一个查询:获取仪器组合--------", sel_sql) + fmt.Println("第一个查询:获取仪器组合--------", sel_sql) + rows, err := conn.Query(sel_sql) if err != nil { fmt.Println("仪器组合查询错误:", err.Error()) + logs.Info("仪器组合查询错误:", err.Error()) return } defer rows.Close() @@ -5593,6 +5388,7 @@ func get_tool_warning_info(user string, wellname string) (resp []Tool_warning) { err = rows.Scan(&seriesPtr, &instrumentPtr) if err != nil { fmt.Println("仪器组合行扫描错误:", err.Error()) + logs.Info("仪器组合行扫描错误:", err.Error()) continue } @@ -5604,22 +5400,26 @@ func get_tool_warning_info(user string, wellname string) (resp []Tool_warning) { if err = rows.Err(); err != nil { fmt.Println("仪器组合遍历错误:", err.Error()) + logs.Info("仪器组合遍历错误:", err.Error()) } // 第二个查询:获取警告信息 - warningSql := `SELECT [time], [err_level], [context] + warningSql := fmt.Sprintf(`SELECT [time], [err_level], [context] FROM [analysis_tool_warning] - WHERE [wellname] = @wellname - ORDER BY [time] DESC` - warningRows, err := sqlConn.Query(warningSql, sql.Named("wellname", wellname)) + WHERE [wellname] = '%s' + ORDER BY [time] DESC`, wellname) + fmt.Println("第二个查询:获取警告信息warningSql:-----------------", warningSql) + logs.Info("第二个查询:获取警告信息warningSql:-----------------", warningSql) + warningRows, err := sqlConn.Query(warningSql) if err != nil { fmt.Println("警告信息查询错误:", err.Error()) + logs.Info("警告信息查询错误:", err.Error()) return } defer warningRows.Close() fmt.Println("警告信息:-----------------", warningRows) - + logs.Info("警告信息:-----------------", warningRows) // 存储所有警告记录 var warnings []Tool_warning for warningRows.Next() { @@ -5633,6 +5433,7 @@ func get_tool_warning_info(user string, wellname string) (resp []Tool_warning) { err = warningRows.Scan(&timePtr, &errLevelPtr, &contextPtr) if err != nil { fmt.Println("警告信息行扫描错误:", err.Error()) + logs.Info("警告信息行扫描错误:", err.Error()) continue } @@ -5641,9 +5442,11 @@ func get_tool_warning_info(user string, wellname string) (resp []Tool_warning) { w.Context = contextPtr.String warnings = append(warnings, w) } + fmt.Println("warnings----------------------:", warnings) if err = warningRows.Err(); err != nil { fmt.Println("警告信息遍历错误:", err.Error()) + logs.Info("警告信息遍历错误:", err.Error()) } // 处理结果:检查每个仪器组合是否出现在警告记录中 @@ -5653,20 +5456,23 @@ func get_tool_warning_info(user string, wellname string) (resp []Tool_warning) { Series: pair.Series, Instrument: pair.Instrument, Flag: 0, // 默认flag=0 + Context: "正常", } // 构建要查找的字符串格式:系列号 + 空格 + 仪器号 searchStr := pair.Series + " " + pair.Instrument + fmt.Println("searchStr----------------------:", searchStr) // 在警告记录中查找匹配的context for _, warn := range warnings { + fmt.Println("warn.Context----------------------:", warn.Context) if strings.Contains(warn.Context, searchStr) { // 找到匹配项,使用警告记录的信息 v.Time = warn.Time v.Err_level = warn.Err_level v.Context = warn.Context v.Flag = 1 // 设置flag=1 - + fmt.Println("warn.Context----------------------:", warn.Context) // 处理时间格式 if len(v.Time) >= 19 { originalTime := v.Time @@ -5679,7 +5485,7 @@ func get_tool_warning_info(user string, wellname string) (resp []Tool_warning) { } else { fmt.Printf("时间格式异常,长度不足19: %s\n", v.Time) } - + fmt.Println("ps_info.R1----------", ps_info.R1) // 压力单位转换 if ps_info.R1 == "2" { if strings.Contains(v.Context, "MPa") { @@ -5748,10 +5554,11 @@ func get_tool_warning_info(user string, wellname string) (resp []Tool_warning) { } fmt.Printf("总共处理 %d 条仪器组合\n", len(data)) - + logs.Info("总共处理 %d 条仪器组合\n", len(data)) if len(data) > 0 { resp = data fmt.Printf("成功返回 %d 条警告信息\n", len(data)) + logs.Info("成功返回 %d 条警告信息\n", len(data)) } else { logs.Info("未查询到任何仪器组合") resp = []Tool_warning{} @@ -5842,7 +5649,6 @@ func get_tool_warning_en(response http.ResponseWriter, request *http.Request) { } func get_tool_warning(response http.ResponseWriter, request *http.Request) { - beginTime := time.Now().UnixNano() logs.Info("get_tool_warning recv req begin", time.Now().Format("2006-01-02 15:04:05")) @@ -20134,13 +19940,33 @@ func get_well_history(response http.ResponseWriter, request *http.Request) { } ifWhere = true*/ + // if ifWhere { + // selSql = fmt.Sprintf("select top %d [ID],[WELLNameSource],[stats],[CreateTableTime],COALESCE([Operator],''),[rt_flag],COALESCE([ClOUDACCOUNT],''),[tool_status_warning] from [WellInformation] where [ID] not in (select top %d [ID] from [WellInformation] where %s order by [CreateTableTime],[tool_status_warning] desc) and %s order by [CreateTableTime] DESC,[tool_status_warning] desc", req.Count, (req.Index-1)*req.Count, whereSql, whereSql) + // } else { + // selSql = fmt.Sprintf("select top %d [ID],[WELLNameSource],[stats],[CreateTableTime],COALESCE([Operator],''),[rt_flag],COALESCE([ClOUDACCOUNT],''),[tool_status_warning] from [WellInformation] where [ID] not in (select top %d [ID] from [WellInformation] order by [CreateTableTime],[tool_status_warning] desc) order by [CreateTableTime] DESC,[tool_status_warning] desc", req.Count, (req.Index-1)*req.Count) + // } + if ifWhere { - selSql = fmt.Sprintf("select top %d [ID],[WELLNameSource],[stats],[CreateTableTime],COALESCE([Operator],''),[rt_flag],COALESCE([ClOUDACCOUNT],''),[tool_status_warning] from [WellInformation] where [ID] not in (select top %d [ID] from [WellInformation] where %s order by [CreateTableTime],[tool_status_warning] desc) and %s order by [CreateTableTime] DESC,[tool_status_warning] desc", req.Count, (req.Index-1)*req.Count, whereSql, whereSql) + selSql = fmt.Sprintf(` + SELECT [ID],[WELLNameSource],[stats],[CreateTableTime], + COALESCE([Operator],''),[rt_flag], + COALESCE([ClOUDACCOUNT],''),[tool_status_warning] + FROM [WellInformation] + WHERE %s + ORDER BY [CreateTableTime] DESC, [tool_status_warning] DESC + OFFSET %d ROWS FETCH NEXT %d ROWS ONLY`, + whereSql, (req.Index-1)*req.Count, req.Count) } else { - selSql = fmt.Sprintf("select top %d [ID],[WELLNameSource],[stats],[CreateTableTime],COALESCE([Operator],''),[rt_flag],COALESCE([ClOUDACCOUNT],''),[tool_status_warning] from [WellInformation] where [ID] not in (select top %d [ID] from [WellInformation] order by [CreateTableTime],[tool_status_warning] desc) order by [CreateTableTime] DESC,[tool_status_warning] desc", req.Count, (req.Index-1)*req.Count) + selSql = fmt.Sprintf(` + SELECT [ID],[WELLNameSource],[stats],[CreateTableTime], + COALESCE([Operator],''),[rt_flag], + COALESCE([ClOUDACCOUNT],''),[tool_status_warning] + FROM [WellInformation] + ORDER BY [CreateTableTime] DESC, [tool_status_warning] DESC + OFFSET %d ROWS FETCH NEXT %d ROWS ONLY`, + (req.Index-1)*req.Count, req.Count) } - fmt.Println(selSql) if ifWhere { cntSql = fmt.Sprintf("select count([ID]) from [WellInformation] where %s", whereSql) } else { @@ -20152,7 +19978,7 @@ func get_well_history(response http.ResponseWriter, request *http.Request) { logs.Error("getWellEx1 count Query Error", err.Error()) return } - + fmt.Println(selSql) row, err := sqlConn.Query(selSql) if err != nil { logs.Error("getWellEx1 Query Error", err.Error()) @@ -20165,8 +19991,8 @@ func get_well_history(response http.ResponseWriter, request *http.Request) { var d wellExData var id int var wellstate int - var rt_flag int - var tsw int + var rt_flag sql.NullInt64 + var tsw sql.NullInt64 if err := row.Scan(&id, &d.WellNameSource, &wellstate, &d.Welltime, &d.Welloperator, &rt_flag, &d.Upload_user, &tsw); err == nil { @@ -20182,11 +20008,13 @@ func get_well_history(response http.ResponseWriter, request *http.Request) { d.Welloperator = strings.Trim(d.Welloperator, " ") - if rt_flag == 1 { - d.Rt_flag = true + if rt_flag.Valid { + if rt_flag.Int64 == 1 { + d.Rt_flag = true + } } - if tsw == 1 { + if tsw.Valid && tsw.Int64 == 1 { d.Tool_state_warning = true } @@ -20263,10 +20091,31 @@ func getWellEx1_en(response http.ResponseWriter, request *http.Request) { ifWhere = true } + // if ifWhere { + // selSql = fmt.Sprintf("select top %d [ID],[WELLNameSource],[stats],[CreateTableTime],COALESCE([Operator],''),[rt_flag] from [WellInformation] where [ID] not in (select top %d [ID] from [WellInformation] where %s order by [CreateTableTime] desc) and %s order by [CreateTableTime] desc", req.Count, (req.Index-1)*req.Count, whereSql, whereSql) + // } else { + // selSql = fmt.Sprintf("select top %d [ID],[WELLNameSource],[stats],[CreateTableTime],COALESCE([Operator],''),[rt_flag] from [WellInformation] where [ID] not in (select top %d [ID] from [WellInformation] order by [CreateTableTime] desc) order by [CreateTableTime] desc", req.Count, (req.Index-1)*req.Count) + // } + if ifWhere { - selSql = fmt.Sprintf("select top %d [ID],[WELLNameSource],[stats],[CreateTableTime],COALESCE([Operator],''),[rt_flag] from [WellInformation] where [ID] not in (select top %d [ID] from [WellInformation] where %s order by [CreateTableTime] desc) and %s order by [CreateTableTime] desc", req.Count, (req.Index-1)*req.Count, whereSql, whereSql) + selSql = fmt.Sprintf(` + SELECT [ID],[WELLNameSource],[stats],[CreateTableTime], + COALESCE([Operator],''),[rt_flag], + COALESCE([ClOUDACCOUNT],''),[tool_status_warning] + FROM [WellInformation] + WHERE %s + ORDER BY [CreateTableTime] DESC, [tool_status_warning] DESC + OFFSET %d ROWS FETCH NEXT %d ROWS ONLY`, + whereSql, (req.Index-1)*req.Count, req.Count) } else { - selSql = fmt.Sprintf("select top %d [ID],[WELLNameSource],[stats],[CreateTableTime],COALESCE([Operator],''),[rt_flag] from [WellInformation] where [ID] not in (select top %d [ID] from [WellInformation] order by [CreateTableTime] desc) order by [CreateTableTime] desc", req.Count, (req.Index-1)*req.Count) + selSql = fmt.Sprintf(` + SELECT [ID],[WELLNameSource],[stats],[CreateTableTime], + COALESCE([Operator],''),[rt_flag], + COALESCE([ClOUDACCOUNT],''),[tool_status_warning] + FROM [WellInformation] + ORDER BY [CreateTableTime] DESC, [tool_status_warning] DESC + OFFSET %d ROWS FETCH NEXT %d ROWS ONLY`, + (req.Index-1)*req.Count, req.Count) } if ifWhere { @@ -30024,9 +29873,8 @@ func getInstrumentMessUtil(req InstrumentMess) (*InstrumentMessRespdata, error) JOIN REPAIRANDMENT r ON wi.WELLNameSource = r.[2321] WHERE r.[2327] = '%s' AND r.[2328] = '%s'`, req.Series, req.Instrument) - - fmt.Println("查询仪器所在井------------", sqlWellData) - + fmt.Println("查询仪器所在的所有井------------", sqlWellData) + logs.Info("查询仪器所在的所有井------------", sqlWellData) wellRow, err := sqlConn.Query(sqlWellData) if err != nil { fmt.Println("查询错误:", err) @@ -30058,8 +29906,31 @@ func getInstrumentMessUtil(req InstrumentMess) (*InstrumentMessRespdata, error) AND [2328] = '%s' ORDER BY ID DESC`, req.WellName, req.Series, req.Instrument) + fmt.Println("查询维保表信息------------", sqlQuery) + logs.Info("查询维保表信息------------", sqlQuery) + var db string + wellSql := fmt.Sprintf(`SELECT WELLName from WellInformation where WELLNameSource = '%s'`, req.WellName) + dbData := sqlConn.QueryRow(wellSql) + dbData.Scan(&db) + fmt.Println("db----------", db) + logs.Info("db----------", db) + if strings.HasPrefix(db, "LH2020-") || + strings.HasPrefix(db, "LH2021-") || + strings.HasPrefix(db, "LH2022-") || + strings.HasPrefix(db, "LH2023-") || + strings.HasPrefix(db, "LH2024-") { + conn, err := getOdbcConn("master") + if err != nil { + logs.Info("链接master库失败", err.Error()) + fmt.Println("链接master库失败", err.Error()) + } + defer conn.Close() + cmdSql := fmt.Sprintf("ALTER DATABASE [%s] SET ONLINE", db) + _, err = conn.Exec(cmdSql) + } + conn, err := getOdbcConn(db) - row, err := sqlConn.Query(sqlQuery) + row, err := conn.Query(sqlQuery) if err != nil { fmt.Println("查询错误:", err) return nil, err @@ -30083,8 +29954,20 @@ func getInstrumentMessUtil(req InstrumentMess) (*InstrumentMessRespdata, error) return nil, err } } - fmt.Println("") - //上井情况汇总 + querySQL := fmt.Sprintf(`SELECT COALESCE(process_record, '') as process_record ,COALESCE(process_inspection, '') as process_inspection ,COALESCE(final_inspection, '') as final_inspection + FROM t_quality_inspection + WHERE well_name = '%s' AND series = '%s' AND instrument = '%s'`, + req.WellName, req.Series, req.Instrument) + var respData QualityDataResp + fmt.Println("querySQL-----------------", querySQL) + logs.Info("querySQL-----------------", querySQL) + err = sqlConn.QueryRow(querySQL).Scan( + &respData.ProcessRecord, + &respData.ProcessInspection, + &respData.FinalInspection, + ) + resp.QualityData = respData + //仪器施工汇总 resp.WorkMessList = workMess(wellInfos, req.Flag, req.Content) //维保情况 resp.GetLcmRespData = getMaintenanceMess(req.Series, req.Instrument, req.Opuser) @@ -30092,11 +29975,31 @@ func getInstrumentMessUtil(req InstrumentMess) (*InstrumentMessRespdata, error) resp.Dept = get_DeptMess(req.Series, req.Instrument) //仪器工作状态 resp.Content = req.Content + + // if strings.HasPrefix(db, "LH2020-") || + // strings.HasPrefix(db, "LH2021-") || + // strings.HasPrefix(db, "LH2022-") || + // strings.HasPrefix(db, "LH2023-") || + // strings.HasPrefix(db, "LH2024-") || + // strings.HasPrefix(db, "LH2025-") { + // logs.Info("开始关库,数据库名: %s", db) + // cmdSql := fmt.Sprintf("ALTER DATABASE [%s] SET OFFLINE", db) + // _, err = conn.Exec(cmdSql) + // if err != nil { + // logs.Error("关闭数据库失败, 数据库名: %s, 错误信息: %v", db, err) + // fmt.Printf("关闭数据库失败, 数据库名: %s, 错误信息: %v\n", db, err) + // } else { + // logs.Info("关闭数据库成功, 数据库名: %s", db) + // fmt.Printf("关闭数据库成功, 数据库名: %s\n", db) + // } + // } + return resp, nil } func workMess(wellInfos []get_vibration_resp, flag string, content string) []WellMessRespdata { + fmt.Println("仪器施工汇总开始------------------------------------", wellInfos) var results []WellMessRespdata if len(wellInfos) == 0 { fmt.Println("wellInfos 是空的,没有数据可打印") @@ -30106,14 +30009,14 @@ func workMess(wellInfos []get_vibration_resp, flag string, content string) []Wel var db string // 循环打印每个 WellNameSource - fmt.Println("开始打印 WellNameSource 列表:") workMessSql := `SELECT MAX(id) AS id, [5200] AS WellName, - (SELECT TOP 1 [5203] - FROM [dbo].[EQUIPMENTRECORDS] t2 - WHERE t2.[5200] = t1.[5200] - ORDER BY t2.ID DESC) AS SampleField, + ( + SELECT TOP 1 [5203] + FROM [EQUIPMENTRECORDS] + ORDER BY ID DESC + ) AS SampleField, CONVERT(VARCHAR(50), MIN([5217])) + '-' + CONVERT(VARCHAR(50), MAX([5217])) AS ValueInterval, MAX([5220]) AS GlobalMax1, MAX([5221]) AS GlobalMax2, @@ -30127,8 +30030,10 @@ func workMess(wellInfos []get_vibration_resp, flag string, content string) []Wel for _, well := range wellInfos { qsql := workMessSql + fmt.Sprintf(` WHERE [5200] = '%s' GROUP BY [5200]`, well.WellNameSource) - db = well.Wellname - // 这些库被设置为了离线状态,需要先设置为启用状态 + wellSql := fmt.Sprintf(`SELECT WELLName from WellInformation where WELLNameSource = '%s'`, well.WellNameSource) + + wellRow := sqlConn.QueryRow(wellSql) + wellRow.Scan(&db) if strings.HasPrefix(db, "LH2020-") || strings.HasPrefix(db, "LH2021-") || strings.HasPrefix(db, "LH2022-") || @@ -30136,6 +30041,7 @@ func workMess(wellInfos []get_vibration_resp, flag string, content string) []Wel strings.HasPrefix(db, "LH2024-") { conn, err := getOdbcConn("master") if err != nil { + logs.Info("链接master库失败", err.Error()) fmt.Println("链接master库失败", err.Error()) } defer conn.Close() @@ -30143,42 +30049,62 @@ func workMess(wellInfos []get_vibration_resp, flag string, content string) []Wel _, err = conn.Exec(cmdSql) } + // 第一个查询:获取仪器组合 conn, err := getOdbcConn(db) if err != nil { fmt.Println("查询失败", err.Error()) continue } defer conn.Close() - + fmt.Println("qsql--------------------", qsql) + logs.Info("qsql--------------------", qsql) rows, err := conn.Query(qsql) if err != nil { fmt.Println("查询工作状态出错", err.Error()) + logs.Info("查询工作状态出错", err.Error()) continue } - defer rows.Close() - fmt.Println("查询数据库------------------", qsql) + var itemCount int for rows.Next() { + itemCount++ var item WellMessRespdata var id, wellName, sampleField, valueInterval, personnelList, globalMax1, globalMax2 string //先扫描工作数据 if err := rows.Scan(&id, &wellName, &sampleField, &valueInterval, &globalMax1, &globalMax2, &personnelList); err != nil { - fmt.Println("扫描数据失败", db, err.Error()) continue } - //查询上传文件数据 - queryUploadSql := `SELECT id, file_name, create_user, create_time, well_file_id, type + queryUploadSql := fmt.Sprintf(`SELECT id, file_name, create_user, create_time, well_file_id, type,real_file_name FROM t_file - WHERE type='tool_detail' AND well_file_id=@wellFileId` + WHERE type='tool_detail_instrument' AND well_file_id='%s' AND file_name IS NOT NULL`, id) - uploadRows, err := sqlConn.Query(queryUploadSql, sql.Named("wellFileId", id)) + uploadRows, err := sqlConn.Query(queryUploadSql) if err != nil { fmt.Printf("查询上传数据失败: %v\n", err) + logs.Info("查询上传数据失败: %v\n", err) continue } + //查询上传链接 + row, err := sqlConn.Query(fmt.Sprintf(`SELECT DISTINCT link FROM t_file + WHERE type='tool_detail_instrument' AND well_file_id='%s'`, id)) + if err != nil { + fmt.Printf("查询链接失败: %v\n", err) + logs.Info("查询链接失败: %v\n", err) + } + // 处理查询结果并打印 + var link string + if row.Next() { + err := row.Scan(&link) + if err != nil { + fmt.Printf("扫描链接行失败: %v\n", err) + } + } else { + fmt.Println("未查询到链接") + } + row.Close() var uploadData []uploadRes for uploadRows.Next() { var res uploadRes @@ -30189,6 +30115,7 @@ func workMess(wellInfos []get_vibration_resp, flag string, content string) []Wel &res.CreateTime, &res.WellFileId, &res.Types, + &res.RealFileName, ); err != nil { fmt.Printf("扫描上传数据行失败: %v\n", err) continue @@ -30200,7 +30127,10 @@ func workMess(wellInfos []get_vibration_resp, flag string, content string) []Wel // 3. 填充数据 fmt.Printf("扫描到的值 - ID: %s, WellName: %s, SampleField: %s, ValueInterval: %s, GlobalMax1: %s, GlobalMax2: %s, PersonnelList: %s\n", id, wellName, sampleField, valueInterval, globalMax1, globalMax2, personnelList) - + // 去掉personnelList开头的第一个逗号 + if len(personnelList) > 0 && personnelList[0] == ',' { + personnelList = personnelList[1:] + } item.ID = id item.WellName = wellName item.SampleField = sampleField @@ -30209,7 +30139,7 @@ func workMess(wellInfos []get_vibration_resp, flag string, content string) []Wel item.GlobalMax2 = globalMax2 item.PersonnelList = personnelList item.Data = uploadData // 添加上传数据 - + item.Link = link //添加上传链接 if flag == "0" { item.Content = "正常" } else { @@ -30218,13 +30148,21 @@ func workMess(wellInfos []get_vibration_resp, flag string, content string) []Wel item.Flag = flag results = append(results, item) + logs.Info("results---------", results) + fmt.Println("results---------", results) } + if itemCount == 0 { + fmt.Println("未查询到数据") + logs.Info("未查询到数据11111111111") + } + } + fmt.Println("仪器施工汇总结束------------------------------------") return results } func getMaintenanceMess(series string, instrument string, opuser string) []GetLcmRespData { - + logs.Info("维保情况开始------------------------------------") var result []GetLcmRespData ps_info := get_ps_info(opuser) @@ -30258,10 +30196,12 @@ func getMaintenanceMess(series string, instrument string, opuser string) []GetLc } selSql += " order by time desc" + fmt.Println("查询维保情况sql=====", selSql) + logs.Info("查询维保情况sql=====", selSql) conn, err := getOdbcConn("WellNameInformation") rows, err := conn.Query(selSql) if err != nil { - logs.Error("getLcm Query Error", err.Error()) + logs.Error("查询维保情况 Query Error", err.Error()) return result } defer rows.Close() @@ -30273,6 +30213,62 @@ func getMaintenanceMess(series string, instrument string, opuser string) []GetLc continue } + queryUploadSql := fmt.Sprintf(`SELECT id, file_name, create_user, create_time, well_file_id, type ,real_file_name + FROM t_file + WHERE type='tool_detail_repair' AND file_name IS NOT NULL AND well_file_id= '%s'`, d.ID) + + uploadRows, err := sqlConn.Query(queryUploadSql, sql.Named("wellFileId", d.ID)) + if err != nil { + fmt.Printf("查询上传数据失败: %v\n", err) + logs.Info("查询上传数据失败: %v\n", err) + continue + } + + var uploadData []uploadRes + for uploadRows.Next() { + var res uploadRes + if err := uploadRows.Scan( + &res.ID, + &res.FileName, + &res.CreateUser, + &res.CreateTime, + &res.WellFileId, + &res.Types, + &res.RealFileName, + ); err != nil { + fmt.Printf("扫描上传数据行失败: %v\n", err) + continue + } + uploadData = append(uploadData, res) + } + if len(uploadData) == 0 { + fmt.Println("uploadData 是空的,没有查询到文件数据") + } else { + fmt.Printf("uploadData 内容: %+v\n", uploadData) + } + uploadRows.Close() + + //查询上传链接 + queryLinkUploadSql := fmt.Sprintf(`SELECT DISTINCT link FROM t_file + WHERE type='tool_detail_repair' AND well_file_id= '%s'`, d.ID) + row, err := sqlConn.Query(queryLinkUploadSql) + + if err != nil { + fmt.Printf("查询链接失败: %v\n", err) + } + var link string + if row.Next() { + err := row.Scan(&link) + if err != nil { + fmt.Printf("扫描链接行失败: %v\n", err) + } else { + fmt.Printf("查询到的链接: %s\n", link) + } + } else { + fmt.Println("未查询到链接") + } + row.Close() + // 处理时间格式 if len(d.Time) >= 19 { d.Time = d.Time[:10] + " " + d.Time[11:19] @@ -30288,10 +30284,11 @@ func getMaintenanceMess(series string, instrument string, opuser string) []GetLc } else { d.IfShowOp = false } - + d.Data = uploadData + d.Link = link result = append(result, d) } - + logs.Info("维保情况结束------------------------------------") return result } @@ -30348,6 +30345,90 @@ func get_DeptMess(series string, instrument string) string { } } +func saveLinkData(response http.ResponseWriter, request *http.Request) { + beginTime := time.Now().UnixNano() + fmt.Println("saveLinkData recv req begin", time.Now().Format("2006-01-02 15:04:05")) + + // 设置响应头为JSON格式 + response.Header().Set("Content-Type", "application/json") + + // 读取并解析请求体 + reqdata, err := ioutil.ReadAll(request.Body) + if err != nil { + fmt.Println("读取请求体失败:", err) + response.WriteHeader(http.StatusBadRequest) + response.Write([]byte(`{"code": 1, "msg": "无效的请求数据"}`)) + return + } + + var req LinkDataReq + if err := json.Unmarshal(reqdata, &req); err != nil { + fmt.Println("解析JSON失败:", err) + response.WriteHeader(http.StatusBadRequest) + response.Write([]byte(`{"code": 1, "msg": "无效的JSON数据"}`)) + return + } + + // 检查必填字段 + if req.WellName == "" || req.Series == "" || req.Instrument == "" { + response.WriteHeader(http.StatusBadRequest) + response.Write([]byte(`{"code": 1, "msg": "缺少必要参数"}`)) + return + } + querySQL := fmt.Sprintf(`SELECT id + FROM t_file + WHERE well_name = '%s' AND series = '%s' AND instrument = '%s'`, + req.WellName, req.Series, req.Instrument) + row := sqlConn.QueryRow(querySQL, + sql.Named("wellName", req.WellName), + sql.Named("series", req.Series), + sql.Named("instrument", req.Instrument)) + + var id string + err = row.Scan(&id) + + // 根据查询结果执行插入或更新操作 + if err != nil { + if err == sql.ErrNoRows { + fmt.Println("记录不存在,执行插入") + // 记录不存在,执行插入 + err = insertFileRecord(req.ID, "", "", req.OpUser, req.Type, + req.Series, req.Instrument, req.WellName, req.Link) + + } else { + // 查询出错 + fmt.Println("查询数据失败:", err) + response.WriteHeader(http.StatusInternalServerError) + response.Write([]byte(`{"code": 1, "msg": "数据库查询失败"}`)) + return + } + } else { + fmt.Println("记录存在,执行更新") + // 记录存在,执行更新 + err = updateFileRecord(id, req.Link, "", req.OpUser, req.Type, + req.Series, req.Instrument, req.WellName, req.Link) + } + + // 处理操作结果 + if err != nil { + fmt.Println("数据库操作失败:", err) + response.WriteHeader(http.StatusInternalServerError) + response.Write([]byte(`{"code": 1, "msg": "保存数据失败"}`)) + return + } + + // 返回成功响应 + successResponse := map[string]interface{}{ + "code": 0, + "msg": "数据保存成功", + } + jsonResponse, _ := json.Marshal(successResponse) + response.Write(jsonResponse) + + fmt.Println("saveLinkData recv req end", time.Now().Format("2006-01-02 15:04:05"), + "cost:", (time.Now().UnixNano()-beginTime)/1e6, "ms") +} + func addQualityData(response http.ResponseWriter, request *http.Request) { beginTime := time.Now().UnixNano() fmt.Println("addQualityData recv req begin", time.Now().Format("2006-01-02 15:04:05")) @@ -30379,8 +30460,6 @@ func addQualityData(response http.ResponseWriter, request *http.Request) { WHERE well_name = '%s' AND series = '%s' AND instrument = '%s'`, req.WellName, req.Series, req.Instrument) - fmt.Println("querySQL----", querySQL) - row := sqlConn.QueryRow(querySQL) // 使用QueryRow获取单行结果 var fieldValue sql.NullString // 使用NullString处理可能为NULL的情况 @@ -30450,8 +30529,6 @@ func addQualityData(response http.ResponseWriter, request *http.Request) { } func getQualityData(response http.ResponseWriter, request *http.Request) { - beginTime := time.Now().UnixNano() - fmt.Println("getQualityData recv req begin", time.Now().Format("2006-01-02 15:04:05")) // 读取并解析请求体 reqdata, err := ioutil.ReadAll(request.Body) @@ -30480,7 +30557,6 @@ func getQualityData(response http.ResponseWriter, request *http.Request) { FROM t_quality_inspection WHERE well_name = '%s' AND series = '%s' AND instrument = '%s'`, req.WellName, req.Series, req.Instrument) - fmt.Println("querySQL----", querySQL) var resp QualityDataResp err = sqlConn.QueryRow(querySQL).Scan( &resp.ProcessRecord, @@ -30502,8 +30578,6 @@ func getQualityData(response http.ResponseWriter, request *http.Request) { } } - fmt.Println("查询质量检验数据成功:", resp) - // 构造响应 responseData := map[string]interface{}{ "code": 0, @@ -30521,7 +30595,6 @@ func getQualityData(response http.ResponseWriter, request *http.Request) { response.Header().Set("Content-Type", "application/json") response.Write(jsonResponse) - fmt.Println("getQualityData recv req end", time.Now().Format("2006-01-02 15:04:05"), "cost:", (time.Now().UnixNano()-beginTime)/1e6, "ms") } func upload(response http.ResponseWriter, request *http.Request) { @@ -30552,6 +30625,8 @@ func upload(response http.ResponseWriter, request *http.Request) { wellName := request.FormValue("wellName") opuser := request.FormValue("opuser") types := request.FormValue("type") + series := request.FormValue("series") + instrument := request.FormValue("instrument") // 创建保存文件的目录(如果不存在) saveDir := "./upload" @@ -30562,10 +30637,22 @@ func upload(response http.ResponseWriter, request *http.Request) { // 构造文件保存路径 filename := fmt.Sprintf("%s/%s", saveDir, handler.Filename) + // 获取文件扩展名(如".txt") + ext := filepath.Ext(handler.Filename) + + // 获取不带扩展名的文件名(包含路径) + nameWithoutExt := filename[:len(filename)-len(ext)] + + // 生成时间戳 + timestamp := time.Now().Format("20060102-150405") + + // 组合新文件名:原文件名_时间戳.扩展名 + realFileName := fmt.Sprintf("%s_%s%s", nameWithoutExt, timestamp, ext) + fmt.Println("realFileName------", realFileName) // 创建文件 - f, err := os.Create(filename) + f, err := os.Create(realFileName) if err != nil { - logs.Error("创建文件失败:", err) + fmt.Println("创建文件失败:", err) response.WriteHeader(http.StatusInternalServerError) response.Write([]byte(`{"code": 1, "msg": "创建文件失败"}`)) return @@ -30581,39 +30668,158 @@ func upload(response http.ResponseWriter, request *http.Request) { return } + querySQL := fmt.Sprintf(`SELECT DISTINCT link + FROM t_file + WHERE well_name = '%s' AND series = '%s' AND instrument = '%s'`, + wellName, series, instrument) + rowD := sqlConn.QueryRow(querySQL, + sql.Named("wellName", wellName), + sql.Named("series", series), + sql.Named("instrument", instrument)) + + fmt.Println("查询link的值----------", querySQL) + var LinkQuery string + err = rowD.Scan(&LinkQuery) + + //不存在记录,则插入 + err = insertFileRecord(id, filename, realFileName, opuser, types, series, instrument, wellName, LinkQuery) + if err != nil { + fmt.Println("插入文件记录失败:", err) + } + + //查询最新的数据ID + queryIdSql := fmt.Sprintf(`SELECT id FROM t_file WHERE real_file_name= '%s'`, realFileName) + row, err := sqlConn.Query(queryIdSql) + + if err != nil { + fmt.Printf("查询链接失败: %v\n", err) + } + var ids string + if row.Next() { + err := row.Scan(&ids) + if err != nil { + fmt.Printf("扫描链接行失败: %v\n", err) + } else { + fmt.Printf("查询到的链接: %s\n", ids) + } + } else { + fmt.Println("未查询到链接") + } + row.Close() + // 返回成功响应 responseData := map[string]interface{}{ "code": 0, "msg": "文件上传成功", "data": map[string]string{ - "id": id, - "wellName": wellName, - "opuser": opuser, - "type": types, - "filename": filename, - "fileSize": fmt.Sprintf("%d", handler.Size), + "id": ids, + "wellName": wellName, + "opuser": opuser, + "type": types, + "filename": filename, + "realFileName": realFileName, + "fileSize": fmt.Sprintf("%d", handler.Size), }, } + jsonResponse, _ := json.Marshal(responseData) + response.Write(jsonResponse) + fmt.Println("upload recv req end", time.Now().Format("2006-01-02 15:04:05"), "cost:", (time.Now().UnixNano()-beginTime)/1e6, "ms") +} + +// insertFileRecord 插入文件记录到数据库的公共方法 +func insertFileRecord(id string, filename string, realFileName string, opuser string, types string, series string, instrument string, wellName string, link string) error { + + fmt.Printf("insertFileRecord 参数:\n") + fmt.Printf("id: %s\n", id) + fmt.Printf("filename: %s\n", filename) + fmt.Printf("realFileName: %s\n", realFileName) + fmt.Printf("opuser: %s\n", opuser) + fmt.Printf("types: %s\n", types) + fmt.Printf("series: %s\n", series) + fmt.Printf("instrument: %s\n", instrument) + fmt.Printf("wellName: %s\n", wellName) + fmt.Printf("link: %s\n", link) insert_sql := fmt.Sprintf(`INSERT INTO [t_file] ([well_file_id] ,[file_name] ,[create_user] ,[type] - ,[create_time]) + ,[create_time] + ,[series] + ,[instrument] + ,[well_name] + ,[link] + ,[real_file_name]) VALUES ('%s' ,'%s' - ,'%s','%s','%s')`, id, filename, opuser, types, time.Now().Format("2006-01-02 15:04:05")) - _, err = sqlConn.Exec(insert_sql) - if err != nil { - fmt.Println(fmt.Sprintf(`%s err:%s`, insert_sql, err.Error())) + ,'%s','%s','%s','%s','%s','%s','%s','%s')`, + id, filename, opuser, types, time.Now().Format("2006-01-02 15:04:05"), series, instrument, wellName, link, realFileName) + + _, err := sqlConn.Exec(insert_sql) + fmt.Println("insert_sql------------------", insert_sql) + return err +} + +func updateFileRecord(id, filename, realFileName, opuser, types, series, instrument, wellName, link string) error { + // 构建基础SQL + updateSQL := "UPDATE [t_file] SET " + + // 使用切片收集需要更新的字段和值 + var setFields []string + var args []interface{} + + // 只需要修改link字段 + if link != "" { + setFields = append(setFields, "[link] = @p1") + args = append(args, link) } - jsonResponse, _ := json.Marshal(responseData) - response.Write(jsonResponse) + // 如果没有需要更新的字段,直接返回 + if len(setFields) == 0 { + return nil + } - fmt.Println("upload recv req end", time.Now().Format("2006-01-02 15:04:05"), "cost:", (time.Now().UnixNano()-beginTime)/1e6, "ms") + // 添加更新时间 + setFields = append(setFields, "[create_time] = @p"+strconv.Itoa(len(args)+1)) + args = append(args, time.Now().Format("2006-01-02 15:04:05")) + + // 构建完整的SQL语句 + updateSQL += strings.Join(setFields, ", ") + + // 构建WHERE条件(使用type, series, instrument, well_name) + whereConditions := []string{} + if types != "" { + whereConditions = append(whereConditions, "[type] = @p"+strconv.Itoa(len(args)+1)) + args = append(args, types) + } + if series != "" { + whereConditions = append(whereConditions, "[series] = @p"+strconv.Itoa(len(args)+1)) + args = append(args, series) + } + if instrument != "" { + whereConditions = append(whereConditions, "[instrument] = @p"+strconv.Itoa(len(args)+1)) + args = append(args, instrument) + } + if wellName != "" { + whereConditions = append(whereConditions, "[well_name] = @p"+strconv.Itoa(len(args)+1)) + args = append(args, wellName) + } + + // 如果没有WHERE条件,返回错误(避免更新全表) + if len(whereConditions) == 0 { + return fmt.Errorf("至少需要一个WHERE条件") + } + + updateSQL += " WHERE " + strings.Join(whereConditions, " AND ") + + fmt.Println("执行的更新语句为----", updateSQL) + fmt.Println("参数为----", args) + + // 执行更新 + _, err := sqlConn.Exec(updateSQL, args...) + return err } func download_ds_file(rw http.ResponseWriter, r *http.Request) { @@ -30638,6 +30844,58 @@ func download_ds_file(rw http.ResponseWriter, r *http.Request) { } +func delete_ds_file(response http.ResponseWriter, request *http.Request) { + fmt.Println("删除文件") + // 读取请求体 + reqdata, err := ioutil.ReadAll(request.Body) + if err != nil { + http.Error(response, "Failed to read request body", http.StatusBadRequest) + return + } + + // 解析JSON请求 + var req download_file_req + if err := json.Unmarshal(reqdata, &req); err != nil { + http.Error(response, "解析失败", http.StatusBadRequest) + return + } + + // 检查文件名是否为空 + if req.ID == "" { // 假设download_file_req结构中有Id字段 + http.Error(response, "id为空", http.StatusBadRequest) + return + } + fmt.Println("req.Filename--------------", req.Filename) + fmt.Println("req.RealFileName--------------", req.RealFileName) + delete_sql := fmt.Sprintf(`DELETE FROM [t_file] WHERE [id] = '%s'`, req.ID) + sqlConn.Exec(delete_sql) + fmt.Println("delete_sql--------------", delete_sql) + filename := req.Filename + prefixes := []string{"./upload/", "upload/", "/upload/"} + for _, prefix := range prefixes { + filename = strings.TrimPrefix(filename, prefix) + } + base_file_path := modelePath + "/upload/" + filepath := base_file_path + filename + fmt.Println("filename--------------", filename) + fmt.Println("base_file_path--------------", base_file_path) + fmt.Println("filepath--------------", filepath) + // 删除文件 + err = os.Remove(filepath) + if err != nil { + if os.IsNotExist(err) { + http.Error(response, "File not found", http.StatusNotFound) + } else { + http.Error(response, "Failed to delete file", http.StatusInternalServerError) + } + return + } + + // 返回成功响应 + response.WriteHeader(http.StatusOK) + response.Write([]byte(`{"status": "success", "message": "File deleted successfully"}`)) +} + // 跨域测试 func test(response http.ResponseWriter, request *http.Request) { fmt.Println("recv test req") diff --git a/req.go b/req.go index 6c853b3..829f423 100644 --- a/req.go +++ b/req.go @@ -114,9 +114,11 @@ type get_up_off_well_info_req struct { type get_well_files_req get_srr_base_req type download_file_req struct { - OpUser string `json:"opuser"` - OpUserUuid string `json:"opuser_uuid"` - Filename string `json:"filename"` + OpUser string `json:"opuser"` + OpUserUuid string `json:"opuser_uuid"` + Filename string `json:"filename"` + RealFileName string `json:"realFileName"` + ID string `json:"id"` } type add_dev_manage_req struct { @@ -676,3 +678,14 @@ type QualityData struct { WellName string `json:"wellName"` Value string `json:"value"` } + +type LinkDataReq struct { + ID string `json:"id"` + Instrument string `json:"instrument"` + Link string `json:"link"` + OpUser string `json:"opuser"` + Opuser_uuid string `json:"opuser_uuid"` + Series string `json:"series"` + WellName string `json:"wellName"` + Type string `json:"type"` +} diff --git a/res.go b/res.go index 86cbbce..cc37e3b 100644 --- a/res.go +++ b/res.go @@ -235,15 +235,17 @@ type GetLcmResp struct { } type GetLcmRespData struct { - ID string `json:"id"` - Series string `json:"series"` - InstrumentID string `json:"instrument_id"` - Repair_cnt string `json:"repair_cnt"` - Repair_level string `json:"repair_level"` - Applicanter string `json:"applicanter"` - Note string `json:"note"` - Time string `json:"time"` - IfShowOp bool `json:"if_show_op"` + ID string `json:"id"` + Series string `json:"series"` + InstrumentID string `json:"instrument_id"` + Repair_cnt string `json:"repair_cnt"` + Repair_level string `json:"repair_level"` + Applicanter string `json:"applicanter"` + Note string `json:"note"` + Time string `json:"time"` + IfShowOp bool `json:"if_show_op"` + Link string `json:"link"` + Data []uploadRes `json:"data"` } type getWellAnalysisResultResp struct { @@ -756,6 +758,7 @@ type InstrumentMessRespdata struct { WorkMessList []WellMessRespdata `json:"data"` //上井情况汇总 GetLcmRespData []GetLcmRespData `json:"data1"` //维保情况 GetToolMess []Tool_warning `json:"data2"` //异常信息 + QualityData QualityDataResp `json:"data3"` //质检数据 } // 上井情况汇总 @@ -769,6 +772,7 @@ type WellMessRespdata struct { PersonnelList string `json:"personnelList"` //累计工作时间 Content string `json:"content"` //仪器工作情况 Flag string `json:"flag"` //仪器工作情况标识 + Link string `json:"link"` //文档链接 Data []uploadRes `json:"data"` } @@ -781,9 +785,9 @@ type QualityDataResp struct { type uploadRes struct { ID string `json:"id"` FileName string `json:"fileName"` - RealFileName string `json:"realFileName"` CreateUser string `json:"createUser"` CreateTime string `json:"createTime"` WellFileId string `json:"wellFileId"` Types string `json:"type"` + RealFileName string `json:"realFileName"` }