package chart

import (
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"hongze/hongze_yb/controller/response"
	"hongze/hongze_yb/global"
	"hongze/hongze_yb/models/response/chart_info"
	chartEdbMappingModel "hongze/hongze_yb/models/tables/chart_edb_mapping"
	chartInfoModel "hongze/hongze_yb/models/tables/chart_info"
	"hongze/hongze_yb/models/tables/chart_info_log"
	edbDataModel "hongze/hongze_yb/models/tables/edb_data"
	edbInfoModel "hongze/hongze_yb/models/tables/edb_info"
	"hongze/hongze_yb/services/alarm_msg"
	"hongze/hongze_yb/services/chart"
	edbDataService "hongze/hongze_yb/services/edb_data"
	"hongze/hongze_yb/services/user"
	"hongze/hongze_yb/utils"
	"io/ioutil"
	"sort"
	"strconv"
	"strings"
	"time"
)

// GetChartInfoDetail 获取图表详情
// @Tags 图库模块
// @Summary  获取图表详情
// @Description 获取图表详情
// @Security ApiKeyAuth
// @Param Authorization	header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
// @Accept  json
// @Product json
// @Param DateType query string false "时间段：1-00年至今; 2-10年至今; 3-15年至今; 4-21年至今; 5-指定区间; 6-指定年月至今; 7-18年至今; 8-19年至今; 9-20年至今"
// @Param ClassifyId query string false "图表分类ID"
// @Success 200 {object} chart_info.ChartInfoDetailResp
// @failure 400 {string} string "图表详情获取失败"
// @Router /my_chart/getChartInfoDetail [get]
func GetChartInfoDetail(c *gin.Context) {
	// 图表ID
	reqChartInfoId := c.DefaultQuery("ChartInfoId", "")
	if reqChartInfoId == "" {
		response.Fail("参数有误:图表ID", c)
		return
	}
	chartInfoId, _ := strconv.Atoi(reqChartInfoId)

	startDate := c.DefaultQuery("StartDate", "")
	endDate := c.DefaultQuery("EndDate", "")
	// 图表样式类型
	reqChartType := c.DefaultQuery("ChartType", "")
	chartType, _ := strconv.Atoi(reqChartType)
	// 季节性图表时间
	reqSeasonStartDate := c.DefaultQuery("SeasonStartDate", "")
	reqSeasonEndDate := c.DefaultQuery("SeasonEndDate", "")
	// 指标ID
	edbInfoId := c.DefaultQuery("EdbInfoId", "")
	// 公历/农历
	reqCalendar := c.DefaultQuery("Calendar", "")

	// 获取图表信息
	var err error
	chartInfo := new(chartInfoModel.ChartInfoView)
	chartInfo, err = chartInfoModel.GetChartInfoViewById(chartInfoId)
	if err != nil {
		if err == utils.ErrNoRow {
			response.Fail("图表不存在，请刷新页面", c)
			return
		}
		response.FailMsg("获取失败", "获取图表信息失败, Err:"+err.Error(), c)
		return
	}
	chartType = chartInfo.ChartType
	calendar := chartInfo.Calendar
	if reqCalendar != "" {
		calendar = reqCalendar
	}

	// 时段筛选
	reqDateType := c.DefaultQuery("DateType", "")
	dateType := chartInfo.DateType
	if reqDateType != "" {
		dateType, _ = strconv.Atoi(reqDateType)
	}
	if dateType <= 0 {
		dateType = 3 // 默认同后台15年至今
	}

	switch dateType {
	case 1:
		startDate = "2000-01-01"
	case 2:
		startDate = "2010-01-01"
	case 3:
		startDate = "2015-01-01"
	case 4:
		startDate = "2021-01-01"
	case 5:
		if startDate == "" && chartInfo.StartDate != "" {
			startDate = chartInfo.StartDate
			endDate = chartInfo.EndDate
		} else {
			startDate = startDate + "-01"
			endDate = endDate + "-01"
		}
	case 6:
		if startDate == "" && chartInfo.StartDate != "" {
			startDate = chartInfo.StartDate
		} else {
			startDate = startDate + "-01"
		}
		//兼容日期数据为2022-09的错误
		if strings.Count(startDate, "-") == 1 {
			startDate = startDate + "-01"
		}
	case 7:
		startDate = "2018-01-01"
	case 8:
		startDate = "2019-01-01"
	case 9:
		startDate = "2020-01-01"
	case 11:
		startDate = "2022-01-01"
	}

	if chartType == 2 {
		// 季节性图表
		var seasonStartDate, seasonEndDate string
		if reqSeasonStartDate == "" {
			seasonStartDate = chartInfo.SeasonStartDate
		} else {
			seasonStartDate = reqSeasonStartDate
		}
		if reqSeasonEndDate == "" {
			seasonEndDate = chartInfo.SeasonEndDate
		} else {
			seasonEndDate = reqSeasonEndDate
		}
		if seasonStartDate != "" {
			startDate = seasonStartDate + "-01-01"
		} else {
			fivePre := time.Now().AddDate(-4, 0, 0).Year()
			startDate = strconv.Itoa(fivePre) + "-01-01"
		}
		if seasonEndDate != "" {
			endDate = seasonEndDate + "-12-31"
		} else {
			endDate = time.Now().Format(utils.FormatDate)
		}
	}

	// 获取图表指标映射
	mappingList := make([]*chartEdbMappingModel.ChartEdbInfoMapping, 0)
	if chartInfoId > 0 {
		mappingList, err = chartEdbMappingModel.GetMappingListByChartInfoId(chartInfoId)
		if err != nil {
			response.FailMsg("获取失败", "获取图表指标信息失败4001, Err:"+err.Error(), c)
			return
		}
	} else {
		if edbInfoId != "" {
			mappingList, err = chartEdbMappingModel.GetMappingListByEdbInfoId(edbInfoId)
			if err != nil {
				response.FailMsg("获取失败", "获取图表指标信息失败4002, Err:"+err.Error(), c)
				return
			}
		}
	}

	// 指标列表
	sourceArr := make([]string, 0)
	edbList := make([]*chartEdbMappingModel.ChartEdbInfoMappingList, 0)
	for _, v := range mappingList {
		item := new(chartEdbMappingModel.ChartEdbInfoMappingList)
		item.EdbInfoId = v.EdbInfoId
		item.SourceName = v.SourceName
		item.Source = v.Source
		item.EdbCode = v.EdbCode
		item.EdbName = v.EdbName
		item.Frequency = v.Frequency
		if v.Unit != "无" {
			item.Unit = v.Unit
		}
		item.StartDate = v.StartDate
		item.EndDate = v.EndDate
		item.ModifyTime = v.ModifyTime

		if !utils.InArray(v.Source, utils.SystemSourceList) { //来源于系统的指标，都展示为弘则研究
			if !utils.InArray(v.SourceName, sourceArr) {
				sourceArr = append(sourceArr, v.SourceName)
			}
		}
		if chartInfoId <= 0 {
			item.IsAxis = 1
			item.LeadValue = 0
			item.LeadUnit = ""
			item.ChartEdbMappingId = 0
			item.ChartInfoId = 0
			item.IsOrder = false
			item.EdbInfoType = 1
			item.ChartStyle = ""
			item.ChartColor = ""
			item.ChartWidth = 0
			item.MaxData = v.MaxValue
			item.MinData = v.MinValue
		} else {
			item.IsAxis = v.IsAxis
			item.EdbInfoType = v.EdbInfoType
			item.LeadValue = v.LeadValue
			item.LeadUnit = v.LeadUnit
			item.ChartEdbMappingId = v.ChartEdbMappingId
			item.ChartInfoId = v.ChartInfoId
			item.ChartStyle = v.ChartStyle
			item.ChartColor = v.ChartColor
			item.ChartWidth = v.ChartWidth
			item.IsOrder = v.IsOrder
			item.MaxData = v.MaxData
			item.MinData = v.MinData
		}
		item.LatestValue = v.LatestValue
		item.LatestDate = v.LatestDate
		item.UniqueCode = v.UniqueCode
		item.ChartStyle = v.ChartStyle

		var startDateReal string
		var diffSeconds int64
		if chartType == 2 { //季节性图表
			startDateReal = startDate
		} else {
			if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 {
				// 领先指标
				var startTimeRealTemp time.Time
				startDateParse, _ := time.Parse(utils.FormatDate, startDate)
				switch v.LeadUnit {
				case "天":
					startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
				case "月":
					startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
				case "季":
					startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
				case "周":
					startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
				case "年":
					startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
				}
				if startTimeRealTemp.Before(startDateParse) {
					startDateReal = startTimeRealTemp.Format(utils.FormatDate)
					diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
				} else {
					startDateReal = startDate
					diffSeconds = 0
				}
			} else {
				startDateReal = startDate
			}
		}
		//fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
		calendarPreYear := 0
		if calendar == "农历" {
			newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
			if err != nil {
				response.FailMsg("获取失败", "农历时间转换有误4001, Err:"+err.Error(), c)
				//fmt.Println("time.Parse:" + err.Error())
				return
			}
			calendarPreYear = newStartDateReal.Year() - 1
			newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
			startDateReal = newStartDateReal.Format(utils.FormatDate)
		}
		dataList := make([]*edbDataModel.EdbDataList, 0)
		fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
		dataList, err = edbDataModel.GetEdbDataList(v.Source, v.EdbInfoId, startDateReal, endDate)
		if err != nil {
			response.FailMsg("获取失败", "获取图表指标信息失败4003, Err:"+err.Error(), c)
			return
		}
		if diffSeconds != 0 && v.EdbInfoType == 0 {
			dataListLen := len(dataList)
			for i := 0; i < dataListLen; i++ {
				dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
			}
		}

		if chartType == 2 {
			if calendar == "农历" {
				if len(dataList) <= 0 {
					result := new(edbDataModel.EdbDataResult)
					item.DataList = result
				} else {
					result, err := edbDataService.AddCalculateQuarter(dataList)
					if err != nil {
						response.FailMsg("获取失败", "获取农历数据失败4002, Err:"+err.Error(), c)
						return
					}
					if result.List[0].Year != calendarPreYear {
						itemList := make([]*edbDataModel.EdbDataList, 0)
						items := new(edbDataModel.EdbDataItems)
						//items.Year = calendarPreYear
						items.Items = itemList

						newResult := new(edbDataModel.EdbDataResult)
						newResult.List = append(newResult.List, items)
						newResult.List = append(newResult.List, result.List...)
						item.DataList = newResult
					} else {
						item.DataList = result
					}
				}

			} else {
				currentYear := time.Now().Year()
				quarterDataList := make([]*edbDataModel.QuarterData, 0)
				quarterMap := make(map[int][]*edbDataModel.EdbDataList)
				var quarterArr []int
				for _, v := range dataList {
					itemDate, err := time.Parse(utils.FormatDate, v.DataTime)
					if err != nil {
						response.FailMsg("获取失败", "季度指标日期转换失败, Err:"+err.Error()+";DataTime:"+v.DataTime, c)
						return
					}
					year := itemDate.Year()
					newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
					timestamp := newItemDate.UnixNano() / 1e6
					v.DataTimestamp = timestamp
					if findVal, ok := quarterMap[year]; !ok {
						quarterArr = append(quarterArr, year)
						findVal = append(findVal, v)
						quarterMap[year] = findVal
					} else {
						findVal = append(findVal, v)
						quarterMap[year] = findVal
					}
				}
				for _, v := range quarterArr {
					itemList := quarterMap[v]
					quarterItem := new(edbDataModel.QuarterData)
					quarterItem.Year = v
					quarterItem.DataList = itemList
					quarterDataList = append(quarterDataList, quarterItem)
				}
				item.DataList = quarterDataList
			}
		} else {
			item.DataList = dataList
		}
		edbList = append(edbList, item)
	}
	sourceArr = append(sourceArr, "弘则研究")
	chartInfo.ChartSource = strings.Join(sourceArr, ",")

	// 访问记录-仅普通用户记录
	userInfo := user.GetInfoByClaims(c)
	ok, _, _ := user.GetAdminByUserInfo(userInfo)
	if !ok {
		reqMyChartClassifyId := c.DefaultQuery("MyChartClassifyId", "")
		myChartClassifyId, _ := strconv.Atoi(reqMyChartClassifyId)
		go chart.SaveChartVisitLog(userInfo, chartInfo, myChartClassifyId)
	}

	resp := new(chart_info.ChartInfoDetailResp)
	resp.ChartInfo = chartInfo
	resp.EdbInfoList = edbList
	response.OkData("获取成功", resp, c)
}

// RefreshChartInfo 刷新图表信息
// @Tags 图库模块
// @Summary  刷新图表信息
// @Description 刷新图表信息
// @Security ApiKeyAuth
// @Param Authorization	header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
// @Accept  json
// @Product json
// @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
// @Success 200 {string} string "操作成功"
// @failure 400 {string} string "操作失败"
// @Router /my_chart/refreshChartInfo [post]
func RefreshChartInfo(c *gin.Context) {
	// 参数校验
	var req chartInfoModel.RefreshChartInfoReq
	if c.ShouldBind(&req) != nil {
		response.Fail("参数异常", c)
		return
	}
	chartInfoId := req.ChartInfoId
	if chartInfoId == 0 {
		response.Fail("参数有误", c)
		return
	}

	userInfo := user.GetInfoByClaims(c)
	ok, _, err := user.GetAdminByUserInfo(userInfo)
	if err != nil {
		response.FailMsg("刷新失败", "RefreshChartInfo-获取系统用户信息失败"+err.Error(), c)
		return
	}
	if !ok {
		// 普通用户刷新频率限制-每个用户/图/天/2次
		cacheKey := utils.HZ_CHART_LIB_DETAIL + "YB_REFRESH_LIMIT_" + strconv.Itoa(chartInfoId) + "_" + strconv.Itoa(int(userInfo.UserID))
		fmt.Println("refreshCacheKey:", cacheKey)
		countUserRefresh, _ := global.Redis.Get(context.TODO(), cacheKey).Int()
		if countUserRefresh >= 2 {
			response.Ok("目前已是最新数据", c)
			return
		}
		countUserRefresh += 1
		now := time.Now()
		today := time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, time.Local)
		sub := today.Sub(now)
		_ = global.Redis.SetEX(context.TODO(), cacheKey, countUserRefresh, sub)
	}

	// 图表信息校验
	chartInfo, err := chartInfoModel.GetChartInfoById(chartInfoId)
	if err != nil {
		if err == utils.ErrNoRow {
			response.Fail("图表已被删除，无需刷新", c)
			return
		}
		response.FailMsg("刷新失败", "刷新失败, Err:"+err.Error(), c)
		return
	}

	// 刷新图表
	if err = chart.RefreshChart(chartInfo.ChartInfoId); err != nil {
		errContent := fmt.Sprint("ErrMsg: 刷新图表关联指标信息失败, " + err.Error())
		if global.CONFIG.Serve.RunMode == "release" {
			go alarm_msg.SendAlarmMsg("刷新图表报错"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+errContent, 3)
			//go services.SendEmail("弘则研报小程序-release-刷新图表报错", errContent, utils.EmailSendToUsers)
		} else {
			global.LOG.Info(errContent)
		}
	}

	//清除图表缓存
	{
		key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
		_ = global.Redis.Del(context.TODO(), key)
	}

	response.OkData("刷新成功", "", c)
}

// EditChartInfo 编辑图表信息
// @Tags 图库模块
// @Summary  编辑图表信息
// @Description 编辑图表信息
// @Security ApiKeyAuth
// @Param Authorization	header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
// @Accept  json
// @Product json
// @Param data body chartInfoModel.SaveChartInfoReq true "请求参数"
// @Success 200 {string} string "操作成功"
// @failure 400 {string} string "操作失败"
// @Router /my_chart/editChartInfo [post]
func EditChartInfo(c *gin.Context) {
	// 参数校验
	var req chartInfoModel.SaveChartInfoReq
	if c.ShouldBind(&req) != nil {
		response.Fail("参数异常", c)
		return
	}

	if req.ChartInfoId > 0 && len(req.ChartEdbInfoList) <= 0 {
		response.Fail("参数异常", c)
		return
	}

	// 操作权限校验
	userInfo := user.GetInfoByClaims(c)
	ok, adminInfo, err := user.GetAdminByUserInfo(userInfo)
	if err != nil {
		response.Fail("操作人信息有误", c)
		return
	}
	if !ok {
		response.Fail("非内部人员无权进行操作", c)
		return
	}

	// 图表信息校验
	chartItem, err := chartInfoModel.GetChartInfoById(req.ChartInfoId)
	if err != nil {
		if err == utils.ErrNoRow {
			response.Fail("图表已被删除，请刷新页面！", c)
			return
		}
		response.FailMsg("操作失败", "获取图表信息失败, Err:"+err.Error(), c)
		return
	}
	if chartItem.ChartType == 2 && len(req.ChartEdbInfoList) > 1 {
		response.Fail("您选择的图表样式为季节性图表，只支持单指标画图！", c)
		return
	}
	if req.Calendar == "" {
		req.Calendar = "公历"
	}

	// 指标信息
	var edbInfoIdArr []int
	edbList := make([]*edbInfoModel.EdbInfo, 0)

	for _, v := range req.ChartEdbInfoList {
		edbInfo, err := edbInfoModel.GetEdbInfoById(v.EdbInfoId)
		if err != nil {
			if err == utils.ErrNoRow {
				response.FailMsg("操作失败", "图表不存在，ChartInfoId:"+strconv.Itoa(v.EdbInfoId), c)
				return
			}
			response.FailMsg("操作失败", "获取图表的指标信息失败，Err:"+err.Error(), c)
			return
		}
		if edbInfo == nil {
			response.FailMsg("操作失败", "指标不存在，ChartInfoId:"+strconv.Itoa(v.EdbInfoId), c)
			return
		}
		edbInfoIdArr = append(edbInfoIdArr, v.EdbInfoId)
		edbInfo.EdbNameSource = edbInfo.EdbName
		edbList = append(edbList, edbInfo)
	}
	sort.Ints(edbInfoIdArr)
	var edbInfoIdArrStr []string
	for _, v := range edbInfoIdArr {
		edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
	}
	edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")

	err = chart.ModifyChartInfoAndMapping(edbInfoIdStr, &req, chartItem.ChartType)
	if err != nil {
		response.Fail("图表保存失败, Err:"+err.Error(), c)
		return
	}

	// 清除图表缓存
	cacheKey := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
	_ = global.Redis.Del(context.TODO(), cacheKey)

	// 新增操作日志
	{
		chartLog := new(chart_info_log.ChartInfoLog)
		chartLog.ChartName = chartItem.ChartName
		chartLog.ChartInfoId = req.ChartInfoId
		chartLog.ChartClassifyId = chartItem.ChartClassifyId
		chartLog.SysUserId = int(adminInfo.AdminID)
		chartLog.SysUserRealName = adminInfo.RealName
		chartLog.UniqueCode = chartItem.UniqueCode
		chartLog.CreateTime = time.Now()
		bodyBytes, _ := ioutil.ReadAll(c.Request.Body)
		chartLog.Content = string(bodyBytes)
		chartLog.Status = "修改配置项"
		chartLog.Method = c.Request.URL.String()
		go chartLog.Create()
	}

	response.OkData("操作成功", "", c)
}
