package excel

import (
	"encoding/json"
	"eta/eta_mobile/models"
	"eta/eta_mobile/models/data_manage"
	"eta/eta_mobile/models/data_manage/excel/request"
	"eta/eta_mobile/services/data"
	"eta/eta_mobile/services/data/data_manage_permission"
	excel2 "eta/eta_mobile/services/data/excel"
	"eta/eta_mobile/utils"
	"fmt"
	"strconv"
	"strings"
	"time"
)

// GetSystemDate
// @Title 获取系统日期（包含计算日期）
// @Description 获取系统日期（包含计算日期）
// @Param	request	body request.MixedTableCellDataReq true "type json string"
// @router /excel_info/get_system_date [post]
func (c *ExcelInfoController) GetSystemDate() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()

	sysUser := c.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	var req request.MixedTableCellDataReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	var config request.EdbDateConf
	err = json.Unmarshal([]byte(req.Value), &config)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	date, _, err, errMsg := excel2.HandleDate(req.DataTimeType, req.Value)
	if err != nil {
		br.Msg = "获取系统日期失败"
		if errMsg != `` {
			br.Msg = errMsg
		}
		br.ErrMsg = "获取系统日期失败,Err:" + err.Error()
		return
	}
	if req.DataTimeType == request.EdbDateDT {
		edbInfo, err := data_manage.GetEdbInfoById(config.EdbInfoId)
		if err != nil {
			br.Msg = "获取指标信息失败！"
			br.ErrMsg = "获取指标信息失败，Err:" + err.Error()
			return
		}

		dataList := make([]*data_manage.EdbDataList, 0)
		switch edbInfo.EdbInfoType {
		case 0:
			dataList, _ = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
		case 1:
			_, dataList, _, _, _, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
		default:
			br.Msg = "指标类型异常！"
			br.ErrMsg = "指标类型异常，Err:" + strconv.Itoa(edbInfo.EdbInfoType)
			return
		}

		if req.DataTime == `` { //选择前移几期数
			date, err = excel2.GetEdbDateByMoveForward(req.Value, dataList)
			if err != nil {
				br.Msg = "查询指标前移日期失败！"
				br.ErrMsg = "查询指标前移日期失败，Err:" + err.Error()
				return
			}
		} else {
			date = req.DataTime //选择表格中的日期
		}
		if date != "" {
			// 开始做日期变换
			date, err = excel2.HandleMixTableDateChange(date, req.Value)
			if err != nil {
				br.Msg = "日期变换失败！"
				br.ErrMsg = "日期变换失败，Err:" + err.Error()
				return
			}
		}
	}

	type resp struct {
		Date string
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取系统日期成功"
	br.Data = resp{
		Date: date,
	}
}

// CalculateData
// @Title 公式计算（混合表格）
// @Description 公式计算（混合表格）
// @Param	request	body request.CalculateConf true "type json string"
// @router /excel_info/mixed/calculate [post]
func (c *ExcelInfoController) CalculateData() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()

	sysUser := c.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	requestBody := string(c.Ctx.Input.RequestBody)
	var req request.CalculateConf
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
	if err != nil {
		br.Msg = "获取指标信息失败！"
		br.ErrMsg = "获取指标信息失败，Err:" + err.Error()
		return
	}

	dataList := make([]*data_manage.EdbDataList, 0)
	switch edbInfo.EdbInfoType {
	case 0:
		dataList, _ = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, ``, ``)
	case 1:
		_, dataList, _, _, _, _ = data.GetPredictDataListByPredictEdbInfoId(edbInfo.EdbInfoId, ``, ``, false)
	default:
		br.Msg = "指标类型异常！"
		br.ErrMsg = "指标类型异常，Err:" + strconv.Itoa(edbInfo.EdbInfoType)
		return
	}

	//获取所有数据，计算所有数据
	//获取部分数据，计算部分数据
	// BaseCalculate 数据计算的结构体
	type BaseCalculate struct {
		DataList      []*data_manage.EdbDataList
		Frequency     string `description:"需要转换的频度"`
		Formula       interface{}
		Calendar      string `description:"公历/农历"`
		MoveType      int    `description:"移动方式:1:领先(默认),2:滞后"`
		MoveFrequency string `description:"移动频度"`
		FromFrequency string `description:"来源的频度"`
		Source        int    `description:"1：累计值转月；2：累计值转季；3：同比值；4：同差值；5：N数值移动平均数计算；6：环比值；7：环差值；8：升频；9：降频；10：时间移位；11：超季节性；12：年化；13：累计值；14：累计值年初至今；15：指数修匀；16：日均值"`
	}

	req2 := &BaseCalculate{
		DataList:      dataList,
		Frequency:     req.Frequency,
		Formula:       req.Formula,
		Calendar:      req.Calendar,
		MoveType:      req.MoveType,
		MoveFrequency: req.MoveFrequency,
		FromFrequency: edbInfo.Frequency,
		Source:        req.Source,
	}

	// 调用指标库去更新
	reqJson, tmpErr := json.Marshal(req2)
	if tmpErr != nil {
		err = tmpErr
		return
	}
	respItem, tmpErr := data.BaseCalculate(string(reqJson), c.Lang)
	if tmpErr != nil {
		br.Msg = "计算指标失败"
		br.ErrMsg = tmpErr.Error()
		return
	}
	if respItem.Ret != 200 {
		br.Msg = respItem.Msg
		br.ErrMsg = respItem.ErrMsg
		return
	}

	// 数据处理成需要返回的样式
	num := 5
	lenDate := len(respItem.Data.DateList)
	if lenDate < 5 {
		num = lenDate
	}

	dataListResp := make([]*data_manage.EdbDataList, 0)

	var newDate string
	var showValue string
	if req.DataTime == `` { //选择前移几期数
		newDate, err = excel2.GetEdbDateByMoveForwardByDateList(requestBody, respItem.Data.DateList)
		if err != nil {
			return
		}
	} else {
		newDate = req.DataTime //选择表格中的日期
		if strings.Count(newDate, "-") == 1 {
			newDate = newDate + "-01"
			// 查找这个月早的时间作为起始时间
			for _, v := range respItem.Data.DateList {
				if v >= newDate {
					newDate = v
				}
			}
		}
	}
	// 开始做日期变换
	newDate, err = excel2.HandleMixTableDateChange(newDate, requestBody)
	if err != nil {
		return
	}
	if req.DataTime == `` {
		canAdd := false
		for i := 1; i <= lenDate; i++ {
			date := respItem.Data.DateList[lenDate-i]
			val, ok := respItem.Data.DataMap[date]
			if !ok {
				continue
			}
			if date == newDate {
				showValue = utils.FormatMixTableDataShowValue(val)
				canAdd = true
			}
			if canAdd && len(dataListResp) <= num {
				dataListResp = append(dataListResp, &data_manage.EdbDataList{
					Value:    val,
					DataTime: date,
				})
			}
		}
	} else {
		// todo 如果选择了表格中的日期应该如何处理
		if val, ok := respItem.Data.DataMap[newDate]; ok {
			showValue = utils.FormatMixTableDataShowValue(val)
			for i, tmpDate := range respItem.Data.DateList {
				if tmpDate == newDate {
					if i+3 <= lenDate {
						t1Date := respItem.Data.DateList[i+2]
						if tmpVal, ok2 := respItem.Data.DataMap[t1Date]; ok2 {
							// 当前日期
							dataListResp = append(dataListResp, &data_manage.EdbDataList{
								Value:    tmpVal,
								DataTime: t1Date,
							})
						}
					}

					if i+2 <= lenDate {
						t1Date := respItem.Data.DateList[i+1]
						if tmpVal, ok2 := respItem.Data.DataMap[t1Date]; ok2 {
							// 当前日期
							dataListResp = append(dataListResp, &data_manage.EdbDataList{
								Value:    tmpVal,
								DataTime: t1Date,
							})
						}
					}

					// 当前日期
					dataListResp = append(dataListResp, &data_manage.EdbDataList{
						Value:    val,
						DataTime: newDate,
					})
					if i >= 1 {
						t1Date := respItem.Data.DateList[i-1]
						if tmpVal, ok2 := respItem.Data.DataMap[t1Date]; ok2 {
							// 当前日期
							dataListResp = append(dataListResp, &data_manage.EdbDataList{
								Value:    tmpVal,
								DataTime: t1Date,
							})
						}
					}
					if i >= 2 {
						t1Date := respItem.Data.DateList[i-2]
						if tmpVal, ok2 := respItem.Data.DataMap[t1Date]; ok2 {
							// 当前日期
							dataListResp = append(dataListResp, &data_manage.EdbDataList{
								Value:    tmpVal,
								DataTime: t1Date,
							})
						}
					}
				}
			}
		}
	}
	resp := data_manage.BeforeAndAfterDateDataResp{
		List:      dataListResp,
		Date:      newDate,
		ShowValue: showValue,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "计算成功"
	br.Data = resp
}

// GetMixDateCalculate
// @Title 获取混合表格日期计算
// @Description 获取混合表格日期计算
// @Param	request	body request.MixedTableCellDataReq true "type json string"
// @router /excel_info/mixed/date_calculate [post]
func (c *ExcelInfoController) GetMixDateCalculate() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()

	sysUser := c.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}

	var req request.MixedDateCalculateReq
	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	valMap := make(map[string]int)
	for _, v := range req.DateList {
		// 查找单元格数据
		// 如果不是基础计算单元格,直接返回
		_, err = time.ParseInLocation(utils.FormatDate, v.Date, time.Local)
		if err != nil {
			br.Msg = "日期计算失败！"
			br.ErrMsg = fmt.Sprintf("%s 的单元格非日期类型, Err: %s", v.Date, err.Error())
			return
		}
		// todo 把日期转换成excel里的天数
		realDiffDay := utils.GetDaysDiff1900(v.Date)

		valMap[strings.ToUpper(v.Tag)] = realDiffDay
	}

	// 计算
	val, errMsg, err := excel2.DateCalculateFormula(valMap, strings.ToUpper(req.Formula))
	if err != nil {
		br.Msg = errMsg
		br.ErrMsg = err.Error()
		return
	}

	showValue := utils.FormatMixTableDataShowValue(val)

	type resp struct {
		ShowValue string
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.Data = resp{
		ShowValue: showValue,
	}
}

// GetBaseEdbInfo
// @Title 获取指标的基本信息
// @Description 获取指标的基本信息
// @Param	EdbInfoIds	string  "指标ID用英文逗号分割"
// @router /excel_info/base_edb_info [get]
func (c *ExcelInfoController) GetBaseEdbInfo() {
	br := new(models.BaseResponse).Init()
	defer func() {
		c.Data["json"] = br
		c.ServeJSON()
	}()

	sysUser := c.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	edbInfoIds := c.GetString("EdbInfoIds")
	if edbInfoIds == "" {
		br.Msg = "请输入指标ID"
		return
	}
	ids := strings.Split(edbInfoIds, ",")
	edbIds := make([]int, 0)
	for _, v := range ids {
		id, err := strconv.Atoi(v)
		if err != nil {
			br.Msg = "指标ID格式错误"
			return
		}
		edbIds = append(edbIds, id)
	}
	edbInfoList, err := data_manage.GetEdbInfoByIdList(edbIds)
	if err != nil {
		br.Msg = "获取指标信息失败！"
		br.ErrMsg = "获取指标信息失败，Err:" + err.Error()
		return
	}

	list := make([]*data_manage.BaseEdbNameItem, 0)

	if len(edbInfoList) > 0 {
		classifyIdList := make([]int, 0)
		for _, v := range edbInfoList {
			classifyIdList = append(classifyIdList, v.ClassifyId)
		}
		// 指标权限
		{
			// 当前的分类
			classifyMap := make(map[int]*data_manage.EdbClassify)
			classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
			if err != nil {
				if err != nil {
					br.Msg = "获取失败"
					br.ErrMsg = "获取分类列表失败，Err:" + err.Error()
					return
				}
			}

			for _, v := range classifyList {
				classifyMap[v.ClassifyId] = v
			}

			// 获取所有有权限的指标和分类
			permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(c.SysUser.AdminId, 0, 0)
			if err != nil {
				br.Msg = "获取失败"
				br.ErrMsg = "获取所有有权限的指标和分类失败，Err:" + err.Error()
				return
			}

			for _, v := range edbInfoList {
				tmp := new(data_manage.BaseEdbNameItem)

				// 指标权限
				if currClassify, ok := classifyMap[v.ClassifyId]; ok {
					tmp.HaveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(v.IsJoinPermission, currClassify.IsJoinPermission, v.EdbInfoId, v.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
				}

				tmp.EdbInfoId = v.EdbInfoId
				tmp.EdbInfoType = v.EdbInfoType
				tmp.EdbCode = v.EdbCode
				tmp.EdbName = v.EdbName
				tmp.EdbNameEn = v.EdbNameEn
				tmp.Source = v.Source
				tmp.SourceName = v.SourceName
				tmp.Frequency = v.Frequency
				tmp.Unit = v.Unit
				tmp.UnitEn = v.UnitEn
				list = append(list, tmp)
			}
		}

	}

	resp := data_manage.BaseEdbInfoResp{
		List: list,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "查询成功"
	br.Data = resp
}
