package data_manage

import (
	"eta/eta_api/models"
	"eta/eta_api/models/data_manage"
	"eta/eta_api/utils"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/paging"
	"github.com/tealeg/xlsx"
	"os"
	"path/filepath"
	"strings"
	"time"
)

// YongyiClassify
// @Title 涌益咨询数据分类
// @Description 涌益咨询数据分类接口
// @Success 200 {object} data_manage.BaseFromYongyiClassify
// @router /yongyi/classify [get]
func (this *EdbInfoController) YongyiClassify() {
	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
	}

	classifyAll, err := data_manage.GetAllBaseFromYongyiClassify()
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	//组装一级分类
	rootMap := make(map[int][]*data_manage.BaseFromYongyiClassifyItems)
	list := make([]*data_manage.BaseFromYongyiClassifyItems, 0)
	for _, classify := range classifyAll {
		if classify.ParentId == 0 {
			if _, ok := rootMap[classify.ClassifyId]; !ok {
				rootMap[classify.ClassifyId] = make([]*data_manage.BaseFromYongyiClassifyItems, 0)
				list = append(list, classify)
			}
		} else {
			child, ok := rootMap[classify.ParentId]
			if ok {
				child = append(child, classify)
				rootMap[classify.ParentId] = child
			}
		}
	}

	for k, v := range list {
		child, ok := rootMap[v.ClassifyId]
		if ok {
			list[k].Children = child
		}
	}
	//组装二级分类
	var ret data_manage.BaseFromYongyiClassifyResp
	ret.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = ret
}

// YongyiIndexData
// @Title 获取钢联数据
// @Description 获取钢联数据接口
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码，从1开始"
// @Param   ClassifyId   query   string  true       "分类id"
// @Success 200 {object} data_manage.LzFrequency
// @router /yongyi/index/data [get]
func (this *EdbInfoController) YongyiIndexData() {
	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
	}

	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	var startSize int

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

	classifyId, _ := this.GetInt("ClassifyId")
	if classifyId < 0 {
		br.Msg = "请选择分类"
		br.ErrMsg = "请选择分类"
		return
	}

	//获取指标
	var condition string
	var pars []interface{}

	if classifyId >= 0 {
		condition += ` AND classify_id=? `
		pars = append(pars, classifyId)
	}

	yongyiList, err := data_manage.GetYongyiIndex(condition, pars)
	if err != nil {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	resultList := make([]*data_manage.BaseFromYongyiIndexList, 0)
	for _, v := range yongyiList {
		product := new(data_manage.BaseFromYongyiIndexList)
		product.YongyiIndexId = v.YongyiIndexId
		product.Unit = v.Unit
		product.IndexCode = v.IndexCode
		product.IndexName = v.IndexName
		product.Frequency = v.Frequency
		product.ModifyTime = v.ModifyTime

		total, err := data_manage.GetYongyiIndexDataCount(v.IndexCode)
		page := paging.GetPaging(currentIndex, pageSize, total)
		dataList, err := data_manage.GetYongyiIndexData(v.IndexCode, startSize, pageSize)
		if err != nil {
			br.Msg = "获取数据失败"
			br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
			return
		}
		if dataList == nil {
			dataList = make([]*data_manage.BaseFromYongyiData, 0)
		}
		product.DataList = dataList
		product.Paging = page
		resultList = append(resultList, product)
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resultList
}

// YongyiSearchList
// @Title Yongyi模糊搜索
// @Description Yongyi模糊搜索
// @Param   Keyword   query   string  ture       "关键字搜索"
// @Success 200 {object} models.BaseResponse
// @router /yongyi/search_list [get]
func (this *EdbInfoController) YongyiSearchList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请重新登录"
		return
	}

	list := make([]*data_manage.BaseFromYongyiIndexSearchItem, 0)
	var err error
	//关键字
	keyword := this.GetString("Keyword")
	if keyword != "" {
		keyWordArr := strings.Split(keyword, " ")

		if len(keyWordArr) > 0 {
			condition := ""
			for _, v := range keyWordArr {
				condition += ` AND CONCAT(index_name,index_code) LIKE '%` + v + `%'`
			}
			list, err = data_manage.GetYongyiItemList(condition)
			if err != nil {
				br.ErrMsg = "获取失败，Err:" + err.Error()
				br.Msg = "获取失败"
				return
			}
		}

	} else {
		// todo es 模糊搜索
		list, err = data_manage.GetYongyiItemList("")
		if err != nil {
			br.ErrMsg = "获取失败，Err:" + err.Error()
			br.Msg = "获取失败"
			return
		}
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = list
}

// YongyiSingleData
// @Title 获取Yongyi数据
// @Description 获取Yongyi单条数据接口
// @Param   IndexCode   query   string  true       "指标唯一编码"
// @Success 200 {object} models.BaseResponse
// @router /yongyi/single_data [get]
func (this *EdbInfoController) YongyiSingleData() {
	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
	}
	indexCode := this.GetString("IndexCode")
	indexInfo, err := data_manage.GetBaseFromYongyiIndexByIndexCode(indexCode)
	if err != nil {
		br.Msg = "获取指标信息失败"
		br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
		return
	}
	dataTmpList, err := data_manage.GetYongyiIndexDataByCode(indexCode)
	if err != nil {
		br.Msg = "获取数据失败"
		br.ErrMsg = "获取数据失败,Err:" + err.Error()
		return
	}
	var ret data_manage.YongyiSingleDataResp
	var dataList []*data_manage.YongyiSingleData

	ret.ClassifyId = indexInfo.ClassifyId
	ret.YongyiIndexId = indexInfo.YongyiIndexId
	ret.IndexCode = indexInfo.IndexCode
	ret.IndexName = indexInfo.IndexName
	ret.Frequency = indexInfo.Frequency
	ret.CreateTime = indexInfo.CreateTime.Format(utils.FormatDateTime)
	ret.ModifyTime = indexInfo.ModifyTime.Format(utils.FormatDateTime)
	ret.Unit = indexInfo.Unit
	for _, v := range dataTmpList {
		tmp := &data_manage.YongyiSingleData{
			Value:    v.Value,
			DataTime: v.DataTime,
		}
		dataList = append(dataList, tmp)
	}
	ret.Data = dataList

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = ret
}

// ExportYongyiList
// @Title 导出Yongyi数据
// @Description 导出Yongyi数据
// @Param   IndexName   query   string  false       "名称关键词"
// @Param   IndexCode   query   string  false       "指标唯一编码"
// @Param   ClassifyId   query   string  true       "分类"
// @Success 200  导出成功
// @router /yongyi/export [get]
func (this *EdbInfoController) ExportYongyiList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请重新登录"
		return
	}

	indexCode := this.GetString("IndexCode") //指标唯一编码
	classifyId, _ := this.GetInt("ClassifyId")
	//secNameList := make([]*models.EdbdataExportList, 0)

	dir, _ := os.Executable()
	exPath := filepath.Dir(dir)

	downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
	xlsxFile := xlsx.NewFile()

	var pars []interface{}
	condition := ""
	if classifyId > 0 {
		//获取指标
		condition += " AND classify_id=?"
		pars = append(pars, classifyId)
	}
	if indexCode != "" {
		//获取指标
		condition += " AND index_code=?"
		pars = append(pars, indexCode)
	}
	indexList, err := data_manage.GetYongyiIndex(condition, pars)
	if err != nil {
		fmt.Println("获取数据失败，Err:" + err.Error())
		return
	}
	if len(indexList) <= 0 {
		fmt.Println("indexList 为空")
		br.Ret = 200
		br.Success = true
		br.Msg = "success"
		return
	}
	sheetNew := new(xlsx.Sheet)
	// todo 分类名称
	sheetNew, err = xlsxFile.AddSheet("涌益咨询")
	//sheetNew.SetColWidth()
	//获取指标数据
	windRow := sheetNew.AddRow()
	secNameRow := sheetNew.AddRow()
	indexCodeRow := sheetNew.AddRow()
	frequencyRow := sheetNew.AddRow()
	unitRow := sheetNew.AddRow()
	lastModifyDateRow := sheetNew.AddRow()
	//获取分类下指标最大数据量
	var dataMax int
	setRowIndex := 6
	indexCodeList := make([]string, 0)
	for _, v := range indexList {
		indexCodeList = append(indexCodeList, v.IndexCode)
	}
	dataListMap := make(map[string][]*data_manage.BaseFromYongyiData)
	if len(indexList) > 0 {
		allDataList, e := data_manage.GetYongyiIndexDataByCodes(indexCodeList)
		if e != nil {
			br.Msg = "获取数据失败"
			br.ErrMsg = "获取数据失败,Err:" + e.Error()
			return
		}
		for _, v := range allDataList {
			dataListMap[v.IndexCode] = append(dataListMap[v.IndexCode], v)
		}
		for _, v := range dataListMap {
			if len(v) > dataMax {
				dataMax = len(v)
			}
		}
	}

	for k, sv := range indexList {
		//获取数据
		dataList, ok := dataListMap[sv.IndexCode]
		if !ok {
			continue
		}
		if len(dataList) > 0 {
			windRow.AddCell().SetValue("涌益咨询")
			secNameRow.AddCell().SetValue("指标名称")
			indexCodeRow.AddCell().SetValue("指标ID")
			frequencyRow.AddCell().SetValue("频率")
			unitRow.AddCell().SetValue("单位")
			lastModifyDateRow.AddCell().SetValue("更新时间")

			secNameRow.AddCell().SetValue(sv.IndexName)
			indexCodeRow.AddCell().SetValue(sv.IndexCode)
			frequencyRow.AddCell().SetValue(sv.Frequency)

			unitRow.AddCell().SetValue(sv.Unit)
			lastModifyDateRow.AddCell().SetValue(sv.ModifyTime)

			windRow.AddCell()
			windRow.AddCell()
			secNameRow.AddCell()
			indexCodeRow.AddCell()
			frequencyRow.AddCell()
			unitRow.AddCell()
			lastModifyDateRow.AddCell()
			min := k * 3
			sheetNew.SetColWidth(min, min, 15)

			if len(dataList) <= 0 {
				for n := 0; n < dataMax; n++ {
					rowIndex := setRowIndex + n
					row := sheetNew.Row(rowIndex)
					row.AddCell()
					row.AddCell()
					row.AddCell()
				}
			} else {
				endRowIndex := 0
				for rk, dv := range dataList {
					rowIndex := setRowIndex + rk
					row := sheetNew.Row(rowIndex)
					displayDate, _ := time.Parse(utils.FormatDate, dv.DataTime)
					displayDateCell := row.AddCell()
					style := new(xlsx.Style)
					style.ApplyAlignment = true
					style.Alignment.WrapText = true
					displayDateCell.SetStyle(style)
					displayDateCell.SetDate(displayDate)

					row.AddCell().SetValue(dv.Value)
					row.AddCell()
					endRowIndex = rowIndex
				}
				if len(dataList) < dataMax {
					dataLen := dataMax - len(dataList)
					for n := 0; n < dataLen; n++ {
						rowIndex := (endRowIndex + 1) + n
						row := sheetNew.Row(rowIndex)
						row.AddCell()
						row.AddCell()
						row.AddCell()
					}
				}
			}
		}
	}

	err = xlsxFile.Save(downLoadnFilePath)
	if err != nil {
		//有指标无数据时先导出一遍空表
		sheet, err := xlsxFile.AddSheet("无数据")
		if err != nil {
			br.Msg = "新增Sheet失败"
			br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
			return
		}
		rowSecName := sheet.AddRow()
		celSecName := rowSecName.AddCell()
		celSecName.SetValue("")
		err = xlsxFile.Save(downLoadnFilePath)
		if err != nil {
			br.Msg = "保存文件失败"
			br.ErrMsg = "保存文件失败"
			return
		}
	}
	fileName := `涌益咨询数据`
	if len(indexList) > 0 {
		fileName = indexList[0].IndexName
	}
	fileName += time.Now().Format("06.01.02") + `.xlsx` //文件名称
	this.Ctx.Output.Download(downLoadnFilePath, fileName)
	defer func() {
		os.Remove(downLoadnFilePath)
	}()
	br.Ret = 200
	br.Success = true
	br.Msg = "success"

}
