ziwen 1 year ago
parent
commit
6321f6a60b

+ 70 - 1
controllers/meeting_probabilities.go

@@ -1,7 +1,15 @@
 package controllers
 
 import (
+	"encoding/json"
 	"eta/eta_api/models"
+	"eta/eta_api/models/data_manage/request"
+	"eta/eta_api/utils"
+	"fmt"
+	"github.com/tealeg/xlsx"
+	"os"
+	"path/filepath"
+	"time"
 )
 
 // MeetingProbabilitiesController 加息概率
@@ -30,16 +38,77 @@ func (c *MeetingProbabilitiesController) Detail() {
 	}
 
 	date := c.GetString("DateTime")
+	isExport, _ := c.GetInt("IsExport")
 
 	// 获取数据详情bu
 	excelDetail, err := models.GetMeetingInfoById(date)
 	if err != nil {
+		if err.Error() == utils.ErrNoRow() {
+			br.Ret = 200
+			br.Msg = "该日期无数据"
+			br.Msg = "该日期无数据,Err:" + err.Error()
+			return
+		}
 		br.ErrMsg = err.Error()
 		return
 	}
 
+	if isExport == 1 {
+		sheet := new(request.ISheet)
+		err = json.Unmarshal([]byte(excelDetail.Content), &sheet)
+
+		MeetingProbabilitiesExport(c,br,sheet)
+		return
+	}
+
+
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "获取成功"
 	br.Data = excelDetail
-}
+}
+
+// MeetingProbabilitiesExport 导出Excel
+func MeetingProbabilitiesExport(this *MeetingProbabilitiesController, br *models.BaseResponse, sheetReq *request.ISheet) {
+	sysUser := this.SysUser
+	if sysUser == nil {
+		br.Msg = "请重新登录"
+		return
+	}
+
+	dir, _ := os.Executable()
+	exPath := filepath.Dir(dir)
+	downloadPath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
+	xlsxFile := xlsx.NewFile()
+	sheet, e := xlsxFile.AddSheet("数据报表")
+	if e != nil {
+		br.Msg = "新增Sheet失败"
+		br.ErrMsg = "新增Sheet失败, Err: " + e.Error()
+		return
+	}
+
+
+	for _, v := range sheetReq.Data {
+		addRow := sheet.AddRow()
+		for _, s := range v {
+			if s != nil {
+				addRow.AddCell().SetString(s.M)
+			}
+		}
+	}
+
+	if e = xlsxFile.Save(downloadPath); e != nil {
+		br.Msg = "导出失败"
+		br.ErrMsg = "保存文件失败"
+		return
+	}
+	fileName := fmt.Sprint("MeetingProbabilities", time.Now().Format("2006.01.02"), ".xlsx")
+	this.Ctx.Output.Download(downloadPath, fileName)
+	defer func() {
+		_ = os.Remove(downloadPath)
+	}()
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "success"
+}
+

+ 34 - 15
models/data_manage/request/meeting_probabilities.go

@@ -1,21 +1,29 @@
 package request
 
 type ISheet struct {
-	ScrollTop                int                 `json:"scrollTop"`
-	ScrollLeft               int                 `json:"scrollLeft"`
-	Index                    string              `json:"index"`
-	Status                   int                 `json:"status"`
-	JfGirdSelectSave         []struct{}          `json:"jfgird_select_save"`
-	LuckySheetSelectSave     []struct{}          `json:"luckysheet_select_save"`
-	Data                     [][]interface{}     `json:"data"`
-	Config                   map[string]struct{} `json:"config"`
-	VisibleDataRow           []int               `json:"visibledatarow"`
-	VisibleDataColumn        []int               `json:"visibledatacolumn"`
-	ChWidth                  int                 `json:"ch_width"`
-	RhHeight                 int                 `json:"rh_height"`
-	LuckySheetSelectionRange []int               `json:"luckysheet_selection_range"`
-	ZoomRatio                int                 `json:"zoomRatio"`
-	CellData                 []ISheetCellData    `json:"celldata"`
+	ScrollTop            int        `json:"scrollTop"`
+	ScrollLeft           int        `json:"scrollLeft"`
+	Index                string     `json:"index"`
+	Status               int        `json:"status"`
+	JfGirdSelectSave     []struct{} `json:"jfgird_select_save"`
+	LuckySheetSelectSave []struct{} `json:"luckysheet_select_save"`
+	Data                 [][]*struct {
+		Ct struct {
+			Fa string `json:"fa"`
+			T  string `json:"t"`
+		} `json:"ct"`
+		M string  `json:"m"`
+		V float64 `json:"v"`
+	} `json:"data"`
+	Config map[string]struct {
+	} `json:"config"`
+	VisibleDataRow           []int            `json:"visibledatarow"`
+	VisibleDataColumn        []int            `json:"visibledatacolumn"`
+	ChWidth                  int              `json:"ch_width"`
+	RhHeight                 int              `json:"rh_height"`
+	LuckySheetSelectionRange []int            `json:"luckysheet_selection_range"`
+	ZoomRatio                int              `json:"zoomRatio"`
+	CellData                 []ISheetCellData `json:"celldata"`
 }
 type ISheetData struct {
 	M  string       `json:"m"`
@@ -33,3 +41,14 @@ type ISheetCellData struct {
 	C int        `json:"c"`
 	V ISheetData `json:"v"`
 }
+
+type SheetData struct {
+	Data [][]*struct {
+		Ct struct {
+			Fa string `json:"fa"`
+			T  string `json:"t"`
+		} `json:"ct"`
+		M string  `json:"m"`
+		V float64 `json:"v"`
+	} `json:"data"`
+}

+ 5 - 3
models/meeting_probabilities.go

@@ -10,7 +10,6 @@ type MeetingProbabilities struct {
 	MeetingInfoId int    `orm:"column(meeting_info_id);pk"`
 	DateTime      string // 数据日期
 	Content       string // 表格内容
-	FileUrl       string // excel下载地址
 	ExcelImage    string // 表格图片
 	IsDelete      int    // 是否删除,0:未删除,1:已删除
 	CreateTime    time.Time
@@ -23,9 +22,12 @@ func GetMeetingInfoById(dateTime string) (item *MeetingProbabilities, err error)
 	sql := ``
 	if dateTime == "" {
 		sql = ` SELECT * FROM meeting_probabilities WHERE is_delete=0 ORDER BY create_time DESC LIMIT 1 `
+		err = o.Raw(sql).QueryRow(&item)
+		return
 	} else {
 		sql = ` SELECT * FROM meeting_probabilities WHERE date_time=? AND is_delete=0 `
+		err = o.Raw(sql, dateTime).QueryRow(&item)
+		return
 	}
-	err = o.Raw(sql, dateTime).QueryRow(&item)
-	return
 }
+