package data_manage

import (
	"hongze/hongze_ETA_mobile_api/controllers"
	"hongze/hongze_ETA_mobile_api/models"
	"hongze/hongze_ETA_mobile_api/models/data_manage"
	"hongze/hongze_ETA_mobile_api/models/data_manage/response"
	"hongze/hongze_ETA_mobile_api/services/excel"
	"hongze/hongze_ETA_mobile_api/utils"
)

// ExcelInfoController ETA表格管理
type ExcelInfoController struct {
	controllers.BaseAuthController
}



// GetExcelTableData
// @Title 获取excel表格的table数据
// @Description 获取excel表格的table数据接口
// @Param   UniqueCode   query   string  true       "表格code"
// @Success 200 {object} response.ExcelTableDetailResp
// @router /excel_info/table_data [get]
func (this *ExcelInfoController) GetExcelTableData() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	uniqueCode := this.GetString("UniqueCode")

	var err error
	if uniqueCode == `` {
		br.Msg = "请选择表格"
		br.ErrMsg = "UniqueCode未传"
		br.IsSendEmail = false
		return
	}
	//获取eta表格信息
	excelInfo, err := data_manage.GetExcelInfoByUniqueCode(uniqueCode)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取ETA表格信息失败，Err:" + err.Error()
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "ETA表格被删除，请刷新页面"
			br.ErrMsg = "ETA表格被删除，请刷新页面，Err:" + err.Error()
			br.IsSendEmail = false
		}
		return
	}

	luckySheetData, err := excel.GetLuckySheetData(excelInfo.Content)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取excel数据失败，Err:" + err.Error()
		return
	}
	tableData, err := luckySheetData.GetTableDataByLuckySheetDataStr()
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "转换成table失败，Err:" + err.Error()
		return
	}

	tableData = excel.HandleTableCell(tableData)

	resp := response.ExcelTableDetailResp{
		UniqueCode: excelInfo.UniqueCode,
		ExcelImage: excelInfo.ExcelImage,
		ExcelName:  excelInfo.ExcelName,
		TableInfo:  tableData,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}


