package edb_info

import (
	"github.com/gin-gonic/gin"
	"hongze/hongze_yb/controller/response"
	"hongze/hongze_yb/models/request"
	"hongze/hongze_yb/models/tables/edb_data"
	edbInfoModel "hongze/hongze_yb/models/tables/edb_info"
	"hongze/hongze_yb/utils"
	"strconv"
)

// EdbData 指标数据
func EdbData(c *gin.Context) {
	var req request.EdbInfoDataReq
	if err := c.Bind(&req); err != nil {
		response.Fail("参数有误", c)
		return
	}
	if req.EdbInfoId <= 0 {
		response.Fail("参数错误", c)
		return
	}
	if req.DateType <= 0 {
		req.DateType = 3
	}
	if req.EdbInfoType <= 0 {
		req.EdbInfoType = 1
	}

	edbInfo, e := edbInfoModel.GetEdbInfoById(req.EdbInfoId)
	if e != nil {
		if e == utils.ErrNoRow {
			response.FailMsg("获取失败", "指标不存在，EdbInfoId:"+strconv.Itoa(req.EdbInfoId), c)
			return
		}
		response.FailMsg("获取失败", "获取指标信息失败，Err:"+e.Error(), c)
		return
	}

	dataList := make([]*edb_data.EdbDataList, 0)
	startDate, endDate := utils.GetDateByDateTypeV2(req.DateType, req.StartDate, req.EndDate, req.StartYear, edbInfo.LatestDate)
	dataList, e = edb_data.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, req.EdbInfoId, startDate, endDate)
	if e != nil {
		response.FailMsg("获取失败", "获取指标数据失败, Err: "+e.Error(), c)
		return
	}

	type EdbInfoDataFullClassifyResp struct {
		EdbInfo  *edbInfoModel.EdbInfo
		DataList []*edb_data.EdbDataList
	}
	resp := new(EdbInfoDataFullClassifyResp)
	resp.EdbInfo = edbInfo
	resp.DataList = dataList
	response.OkData("获取成功", resp, c)
}
