|
|
@@ -1,8 +1,16 @@
|
|
|
package limsdrillcollar
|
|
|
|
|
|
import (
|
|
|
+ "dashoo.cn/backend/api/business/limsdataentry"
|
|
|
+ "dashoo.cn/utils"
|
|
|
+ "github.com/360EntSecGroup-Skylar/excelize"
|
|
|
"github.com/go-xorm/xorm"
|
|
|
. "dashoo.cn/backend/api/mydb"
|
|
|
+ "os"
|
|
|
+ "strconv"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+ . "github.com/linxGnu/goseaweedfs"
|
|
|
)
|
|
|
|
|
|
type LimsDrillCollarService struct {
|
|
|
@@ -14,3 +22,145 @@ func GetLimsDrillCollarService(xormEngine *xorm.Engine) *LimsDrillCollarService
|
|
|
s.DBE = xormEngine
|
|
|
return s
|
|
|
}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 数据录入导出
|
|
|
+ */
|
|
|
+func (s *LimsDrillCollarService) DocDataToExcel(entrustMainEntity limsdataentry.DataEntryEntrustModel, dataentrymainlist limsdataentry.LimsDateEntry, tablename string, seqStr string, retUrl string) (reportUrl string, err error) {
|
|
|
+ var dataentrylist []LimsReportDrillCollar
|
|
|
+ s.GetEntitysByWhere(tablename, " DataEntryId='"+utils.ToStr(dataentrymainlist.Id)+"'", &dataentrylist)
|
|
|
+ //模板下载到服务器
|
|
|
+ _dir := utils.Cfg.MustValue("file", "tmplateDir") + utils.ToStr(dataentrymainlist.Id)
|
|
|
+ fileurl := strings.Split(retUrl, "|")
|
|
|
+ utils.DownloadFile("http://"+fileurl[0], fileurl[1], _dir)
|
|
|
+
|
|
|
+ xlsx, err := excelize.OpenFile(utils.Cfg.MustValue("file", "tmplateDir") + utils.ToStr(dataentrymainlist.Id) + "/" + fileurl[1])
|
|
|
+ if err != nil {
|
|
|
+ utils.LogError(err)
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ tmpDocName := utils.ToStr(dataentrymainlist.Id) + "_" + strconv.Itoa(time.Now().Nanosecond()) + ".xlsx"
|
|
|
+ //提前生成文件在服务器的存储路径
|
|
|
+ _pdir := utils.Cfg.MustValue("file", "docDir") + utils.ToStr(dataentrymainlist.Id) + "/"
|
|
|
+ exist, _ := utils.PathExists(_pdir)
|
|
|
+ var dirErr error
|
|
|
+ if !exist {
|
|
|
+ dirErr = os.Mkdir(_pdir, os.ModePerm)
|
|
|
+ }
|
|
|
+ if dirErr != nil {
|
|
|
+ utils.LogError(err)
|
|
|
+ return "", dirErr
|
|
|
+ }
|
|
|
+ excelrow := 16
|
|
|
+ var copysheetssnum = 1
|
|
|
+ if (len(dataentrylist) > excelrow) {
|
|
|
+ if (len(dataentrylist)%excelrow > 0) {
|
|
|
+ copysheetssnum = len(dataentrylist)/excelrow + 1
|
|
|
+ } else {
|
|
|
+ copysheetssnum = len(dataentrylist) / excelrow
|
|
|
+ }
|
|
|
+ sheetIdx := 3
|
|
|
+ for i := 1; i < copysheetssnum; i++ {
|
|
|
+ xlsx.NewSheet(1+sheetIdx, "Sheet"+strconv.Itoa(1+sheetIdx))
|
|
|
+ err = xlsx.CopySheet(1, 1+sheetIdx)
|
|
|
+ xlsx.NewSheet(2+sheetIdx, "Sheet"+strconv.Itoa(2+sheetIdx))
|
|
|
+ err = xlsx.CopySheet(2, 2+sheetIdx)
|
|
|
+ xlsx.NewSheet(3+sheetIdx, "Sheet"+strconv.Itoa(3+sheetIdx))
|
|
|
+ // index := xlsx.NewSheet("Sheet" + strconv.Itoa(sheetIdx))
|
|
|
+ err = xlsx.CopySheet(3, 3+sheetIdx)
|
|
|
+ sheetIdx += 3
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sheetIdx1 := 1
|
|
|
+ sheetIdx2 := 2
|
|
|
+ sheetIdx3 := 3
|
|
|
+ startIdx := 0
|
|
|
+ for i := 0; i < copysheetssnum; i++ {
|
|
|
+ step := 0
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "C5", dataentrymainlist.Product)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "L5", dataentrymainlist.Spec)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "C7", dataentrymainlist.SamplingBase)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "L7", dataentrymainlist.Spec)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "C43", dataentrymainlist.Appearance)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "C45", dataentrymainlist.Remark)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "C5", dataentrymainlist.Product)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "G5", dataentrymainlist.Spec)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "C7", dataentrymainlist.SamplingBase)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "G7", dataentrymainlist.Spec)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "C43", dataentrymainlist.Appearance)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "C45", dataentrymainlist.Remark)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "C5", dataentrymainlist.Product)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "K5", dataentrymainlist.Spec)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "C7", dataentrymainlist.SamplingBase)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "K7", dataentrymainlist.Spec)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "C43", dataentrymainlist.Appearance)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "C45", dataentrymainlist.Remark)
|
|
|
+
|
|
|
+
|
|
|
+ for ; startIdx < (len(dataentrylist)); startIdx++ {
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "A"+strconv.Itoa(11+step), dataentrylist[startIdx].SampleNumber)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "B"+strconv.Itoa(11+step), dataentrylist[startIdx].CloseDistanceOut)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "C"+strconv.Itoa(11+step), dataentrylist[startIdx].CloseDistanceIn)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "D"+strconv.Itoa(11+step), dataentrylist[startIdx].TaperToleranceOut1)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "E"+strconv.Itoa(11+step), dataentrylist[startIdx].TaperToleranceOut2 )
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "F"+strconv.Itoa(11+step), dataentrylist[startIdx].TaperToleranceIn1)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "G"+strconv.Itoa(11+step), dataentrylist[startIdx].TaperToleranceIn2)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "H"+strconv.Itoa(11+step), dataentrylist[startIdx].PitchToleranceOut1)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "I"+strconv.Itoa(11+step), dataentrylist[startIdx].PitchToleranceOut2)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "J"+strconv.Itoa(11+step), dataentrylist[startIdx].PitchToleranceIn1)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "K"+strconv.Itoa(11+step), dataentrylist[startIdx].PitchToleranceIn2)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "L"+strconv.Itoa(11+step), dataentrylist[startIdx].ShoulderSurface)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx1), "M"+strconv.Itoa(11+step), dataentrylist[startIdx].EndFace)
|
|
|
+
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "A"+strconv.Itoa(11+step), dataentrylist[startIdx].SampleNumber)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "B"+strconv.Itoa(11+step), dataentrylist[startIdx].MajorAxis)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "C"+strconv.Itoa(11+step), dataentrylist[startIdx].ShortAxis)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "D"+strconv.Itoa(11+step), dataentrylist[startIdx].OutDiaDif)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "E"+strconv.Itoa(11+step), dataentrylist[startIdx].DF)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "F"+strconv.Itoa(11+step), dataentrylist[startIdx].QC)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "G"+strconv.Itoa(11+step), dataentrylist[startIdx].DRG)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx2), "H"+strconv.Itoa(11+step), dataentrylist[startIdx].LPC)
|
|
|
+
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "A"+strconv.Itoa(11+step), dataentrylist[startIdx].SampleNumber)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "B"+strconv.Itoa(11+step), dataentrylist[startIdx].WallDif1)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "C"+strconv.Itoa(11+step), dataentrylist[startIdx].ExDiameterM1)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "D"+strconv.Itoa(11+step), dataentrylist[startIdx].WallDifference1)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "E"+strconv.Itoa(11+step), dataentrylist[startIdx].WallDif2)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "F"+strconv.Itoa(11+step), dataentrylist[startIdx].ExDiameterM2)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "G"+strconv.Itoa(11+step), dataentrylist[startIdx].WallDifference2)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "H"+strconv.Itoa(11+step), dataentrylist[startIdx].WallDif3)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "I"+strconv.Itoa(11+step), dataentrylist[startIdx].ExDiameterM3)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "J"+strconv.Itoa(11+step), dataentrylist[startIdx].WallDifference3)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "K"+strconv.Itoa(11+step), dataentrylist[startIdx].InterDiameter)
|
|
|
+ xlsx.SetCellValue("Sheet"+strconv.Itoa(sheetIdx3), "L"+strconv.Itoa(11+step), dataentrylist[startIdx].TubeLength)
|
|
|
+
|
|
|
+ step+=2
|
|
|
+ if (startIdx+1)%excelrow == 0 {
|
|
|
+ sheetIdx1 = sheetIdx1 + 3
|
|
|
+ sheetIdx2 = sheetIdx2 + 3
|
|
|
+ sheetIdx3 = sheetIdx3 + 3
|
|
|
+ startIdx++
|
|
|
+ step = 0
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //4. 将文件保存到服务器相关目录上
|
|
|
+ xlsx.SaveAs(utils.Cfg.MustValue("file", "docDir") + utils.ToStr(dataentrymainlist.Id) + "/" + tmpDocName)
|
|
|
+
|
|
|
+ //5. 上传文件到文件服务器
|
|
|
+ DocFilePath := utils.Cfg.MustValue("file", "docDir") + utils.ToStr(dataentrymainlist.Id) + "/" + tmpDocName
|
|
|
+ var sw *Seaweed
|
|
|
+ var filer []string
|
|
|
+ if _filer := os.Getenv("GOSWFS_FILER_URL"); _filer != "" {
|
|
|
+ filer = []string{_filer}
|
|
|
+ }
|
|
|
+ sw = NewSeaweed("http", utils.Cfg.MustValue("file", "upFileHost"), filer, 2*1024*1024, 5*time.Minute)
|
|
|
+ _, _, fID, err := sw.UploadFile(DocFilePath, "", "")
|
|
|
+ retDocUrl := utils.Cfg.MustValue("file", "downFileHost") + "/" + fID
|
|
|
+ //6. 删除本地文件,释放空间
|
|
|
+ os.Remove(DocFilePath)
|
|
|
+ //7. 保存住报告URL(由controller层调用生成或修改生成报告记录)
|
|
|
+ return retDocUrl, err
|
|
|
+}
|