package data_manage

import (
	"eta/eta_mobile/controllers"
	"eta/eta_mobile/models"
	"eta/eta_mobile/models/data_manage"
	"eta/eta_mobile/models/data_manage/response"
	"eta/eta_mobile/services/excel"
	"eta/eta_mobile/utils"
	"github.com/rdlucklib/rdluck_tools/paging"
)

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

// List
// @Title ETA表格列表接口
// @Description ETA表格列表接口
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码，从1开始"
// @Param   ExcelClassifyId   query   int  true       "分类id"
// @Param   Keyword   query   string  true       "搜索关键词"
// @Param   AdminId   query   int  false       "创建人id"
// @Success 200 {object} response.ExcelListResp
// @router /excel_info/list [get]
func (this *ExcelInfoController) List() {
	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
	}

	excelClassifyId, _ := this.GetInt("ExcelClassifyId")

	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	keyword := this.GetString("Keyword")
	adminId, _ := this.GetInt("AdminId")

	var total int
	page := paging.GetPaging(currentIndex, pageSize, total)

	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = paging.StartIndex(currentIndex, pageSize)

	var condition string
	var pars []interface{}

	// 筛选分类
	if excelClassifyId > 0 {
		_, err := data_manage.GetExcelClassifyById(excelClassifyId)
		if err != nil && err.Error() != utils.ErrNoRow() {
			br.Msg = "获取表格信息失败"
			br.ErrMsg = "获取信息失败,GetExcelClassify,Err:" + err.Error()
			return
		}
		condition += " AND excel_classify_id = ? "
		pars = append(pars, excelClassifyId)
	}
	if keyword != "" {
		condition += ` AND  ( excel_name LIKE ? )`
		pars = append(pars, `%`+keyword+`%`)
	}
	if adminId > 0 {
		condition += " AND sys_user_id = ? "
		pars = append(pars, adminId)
	}
	//获取表格信息
	list, err := data_manage.GetNoContentExcelListByCondition(condition, pars, startSize, pageSize)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Success = true
		br.Msg = "获取表格信息失败"
		br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
		return
	}

	if list == nil || len(list) <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
		list = make([]*data_manage.MyExcelInfoList, 0)
	}
	// 总数据量
	dataCount, err := data_manage.GetExcelListCountByCondition(condition, pars)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取表格列表信息失败"
		br.ErrMsg = "获取表格列表数据总数失败,Err:" + err.Error()
		return
	}
	page = paging.GetPaging(currentIndex, pageSize, dataCount)

	resp := response.ExcelListResp{
		Paging: page,
		List:   list,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// 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
}
