package future_good

import (
	"encoding/json"
	"eta/eta_api/models"
	"eta/eta_api/models/data_manage"
	"eta/eta_api/models/data_manage/future_good"
	"eta/eta_api/models/data_manage/future_good/request"
	"eta/eta_api/models/data_manage/future_good/response"
	"eta/eta_api/models/system"
	"eta/eta_api/services/data"
	future_goodServ "eta/eta_api/services/data/future_good"
	"eta/eta_api/utils"
	"fmt"
	"sort"
	"strconv"
	"strings"
	"time"
)

// ProfitChartInfoAdd
// @Title 新增图表接口
// @Description 新增图表接口
// @Param	request	body data_manage.AddChartInfoReq true "type json string"
// @Success 200 {object} data_manage.AddChartInfoResp
// @router /chart_info/profit/add [post]
func (this *FutureGoodChartInfoController) ProfitChartInfoAdd() {
	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
	}
	deleteCache := true
	cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
	defer func() {
		if deleteCache {
			utils.Rc.Delete(cacheKey)
		}
	}()
	if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
		deleteCache = false
		br.Msg = "系统处理中，请稍后重试！"
		br.ErrMsg = "系统处理中，请稍后重试！" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
		return
	}
	var req request.AddChartInfoReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	req.ChartName = strings.Trim(req.ChartName, " ")
	if req.ChartName == "" {
		br.Msg = "请填写图表名称！"
		return
	}

	if req.ChartClassifyId <= 0 {
		br.Msg = "分类参数错误！"
		return
	}

	chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "分类不存在"
			br.ErrMsg = "分类不存在"
			return
		}
		br.Msg = "获取分类信息失败"
		br.ErrMsg = "获取分类信息失败，Err:" + err.Error()
		return
	}
	if chartClassify == nil {
		br.Msg = "分类不存在"
		br.ErrMsg = "分类不存在"
		return
	}

	//额外的配置信息
	extraReq := req.Extra
	if len(extraReq.DateList) <= 0 {
		br.Msg = "请选择日期"
		br.IsSendEmail = false
		return
	}
	if extraReq.BaseEdbInfoId <= 0 {
		br.Msg = "请选择ETA指标"
		br.IsSendEmail = false
		return
	}
	if len(extraReq.FutureGoodEdbInfoIdList) <= 0 {
		br.Msg = "请选择期货商品指标"
		br.IsSendEmail = false
		return
	}
	extraConfByte, err := json.Marshal(extraReq)
	if err != nil {
		br.Msg = "商品利润曲线图信息异常"
		br.ErrMsg = "商品利润曲线图信息异常,Err:" + err.Error()
		return
	}
	extraConf := string(extraConfByte)

	var edbInfoIdArr []int
	// ETA指标
	edbInfo, err := data_manage.GetEdbInfoById(extraReq.BaseEdbInfoId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "指标不存在！"
			br.ErrMsg = "指标不存在，edbInfoId:" + strconv.Itoa(extraReq.BaseEdbInfoId)
			return
		} else {
			br.Msg = "获取指标信息失败！"
			br.ErrMsg = "获取图表的指标信息失败，Err:" + err.Error()
			return
		}
	}
	if edbInfo == nil {
		br.Msg = "指标已被删除，请重新选择！"
		br.ErrMsg = "指标不存在，ChartInfoId:" + strconv.Itoa(extraReq.BaseEdbInfoId)
		return
	} else {
		if edbInfo.EdbInfoId <= 0 {
			br.Msg = "指标已被删除，请重新选择！"
			br.ErrMsg = "指标不存在，ChartInfoId:" + strconv.Itoa(extraReq.BaseEdbInfoId)
			return
		}
	}

	edbInfoIdArr = append(edbInfoIdArr, extraReq.BaseEdbInfoId)
	edbInfo.EdbNameSource = edbInfo.EdbName

	// 期货商品指标（主力合约）
	futureGoodEdbInfoMap := make(map[int]*future_good.FutureGoodEdbInfo)
	zlFutureGoodEdbInfoList := make([]*future_good.FutureGoodEdbInfo, 0)
	for _, v := range extraReq.FutureGoodEdbInfoIdList {
		if _, ok := futureGoodEdbInfoMap[v.EdbInfoId]; ok {
			continue
		}

		edbInfoId := v.EdbInfoId
		futureGoodEdbInfo, tmpErr := future_good.GetFutureGoodEdbInfo(edbInfoId)
		if tmpErr != nil {
			if tmpErr.Error() == utils.ErrNoRow() {
				br.Msg = "期货商品指标不存在！"
				br.ErrMsg = "期货商品指标不存在，指标id:" + strconv.Itoa(edbInfoId)
				return
			} else {
				br.Msg = "获取指标信息失败！"
				br.ErrMsg = "获取图表的指标信息失败，Err:" + tmpErr.Error()
				return
			}
		}

		if futureGoodEdbInfo == nil {
			br.Msg = "期货商品指标已被删除，请重新选择！"
			br.ErrMsg = "期货商品指标不存在，指标id:" + strconv.Itoa(edbInfoId)
			return
		} else {
			if futureGoodEdbInfo.FutureGoodEdbInfoId <= 0 {
				br.Msg = "期货商品指标已被删除，请重新选择！"
				br.ErrMsg = "期货商品指标不存在，指标id:" + strconv.Itoa(edbInfoId)
				return
			}
		}
		edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
		futureGoodEdbInfoMap[edbInfoId] = futureGoodEdbInfo
		zlFutureGoodEdbInfo, err := future_good.GetFutureGoodEdbInfo(v.EdbInfoId)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取图表，指标信息失败，Err:" + err.Error()
			return
		}
		zlFutureGoodEdbInfoList = append(zlFutureGoodEdbInfoList, zlFutureGoodEdbInfo)
	}

	sort.Ints(edbInfoIdArr)
	var edbInfoIdArrStr []string
	for _, v := range edbInfoIdArr {
		edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
	}
	edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")

	disableVal := data.CheckIsDisableChart(edbInfoIdArr)

	chartInfo := new(data_manage.ChartInfo)
	chartInfo.ChartName = req.ChartName
	chartInfo.EdbInfoIds = edbInfoIdStr
	chartInfo.ChartClassifyId = req.ChartClassifyId
	chartInfo.SysUserId = sysUser.AdminId
	chartInfo.SysUserRealName = sysUser.RealName
	chartInfo.CreateTime = time.Now()
	chartInfo.ModifyTime = time.Now()
	chartInfo.IsSetName = 0
	timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
	chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
	if req.DateType > 0 {
		chartInfo.DateType = req.DateType
	} else {
		chartInfo.DateType = 3
	}
	if req.ChartType == 0 {
		req.ChartType = 1
	}
	chartInfo.ChartType = req.ChartType
	if req.Calendar == "" {
		chartInfo.Calendar = "公历"
	} else {
		chartInfo.Calendar = req.Calendar
	}
	chartInfo.StartDate = req.StartDate
	chartInfo.EndDate = req.EndDate
	chartInfo.SeasonStartDate = req.SeasonStartDate
	chartInfo.SeasonEndDate = req.SeasonEndDate
	chartInfo.LeftMin = req.LeftMin
	chartInfo.LeftMax = req.LeftMax
	chartInfo.RightMin = req.RightMin
	chartInfo.RightMax = req.RightMax
	chartInfo.Disabled = disableVal
	chartInfo.ExtraConfig = extraConf
	chartInfo.Source = utils.CHART_SOURCE_FUTURE_GOOD_PROFIT

	// 关联指标
	mapList := make([]*data_manage.ChartEdbMapping, 0)
	{
		mapList = append(mapList, &data_manage.ChartEdbMapping{
			ChartEdbMappingId: 0,
			EdbInfoId:         extraReq.BaseEdbInfoId,
			CreateTime:        time.Now(),
			ModifyTime:        time.Now(),
			UniqueCode:        utils.MD5(utils.CHART_PREFIX + "_" + strconv.FormatInt(time.Now().UnixNano(), 10) + "_" + strconv.Itoa(extraReq.BaseEdbInfoId)),
			//MaxData:           0,
			//MinData:           0,
			IsOrder:     true,
			IsAxis:      1,
			EdbInfoType: 1,
			//LeadValue:         0,
			//LeadUnit:          "",
			//ChartStyle:        "",
			//ChartColor:        "",
			//PredictChartColor: "",
			//ChartWidth:        0,
			Source: 1,
		})

		for _, v := range futureGoodEdbInfoMap {
			mapList = append(mapList, &data_manage.ChartEdbMapping{
				ChartEdbMappingId: 0,
				EdbInfoId:         v.FutureGoodEdbInfoId,
				CreateTime:        time.Now(),
				ModifyTime:        time.Now(),
				UniqueCode:        utils.MD5(utils.CHART_PREFIX + "_" + strconv.FormatInt(time.Now().UnixNano(), 10) + "_" + strconv.Itoa(v.FutureGoodEdbInfoId)),
				//MaxData:           0,
				//MinData:           0,
				IsOrder:     true,
				IsAxis:      1,
				EdbInfoType: 1,
				//LeadValue:         0,
				//LeadUnit:          "",
				//ChartStyle:        "",
				//ChartColor:        "",
				//PredictChartColor: "",
				//ChartWidth:        0,
				Source: 2,
			})
		}
	}

	// 获取图表中的指标数据
	_, _, _, xDataList, yDataList, err := future_goodServ.GetProfitChartEdbData(edbInfo, zlFutureGoodEdbInfoList, extraReq.DateList, extraReq.CalculateFormula, extraReq.FutureGoodEdbInfoIdList)
	if err != nil {
		br.Msg = "保存失败"
		br.ErrMsg = "保存商品利润失败，指标信息失败，Err:" + err.Error()
		return
	}

	xDataListByte, err := json.Marshal(xDataList)
	if err != nil {
		br.Msg = "保存失败"
		br.ErrMsg = "保存商品利润失败，转换X轴数据失败，Err:" + err.Error()
		return
	}
	yDataListByte, err := json.Marshal(yDataList)
	if err != nil {
		br.Msg = "保存失败"
		br.ErrMsg = "保存商品利润失败，转换Y轴数据失败，Err:" + err.Error()
		return
	}

	err = future_good.CreateChartInfoFutureGoodProfitAndEdb(chartInfo, mapList, &future_good.ChartInfoFutureGoodProfit{
		ChartInfoId:  0,
		ProfitName:   zlFutureGoodEdbInfoList[0].FutureGoodEdbName + "盘面利润",
		ProfitNameEn: extraReq.ProfitNameEn,
		XValue:       string(xDataListByte),
		YValue:       string(yDataListByte),
		CreateTime:   time.Now(),
		ModifyTime:   time.Now(),
	})
	if err != nil {
		br.Msg = "保存失败"
		br.ErrMsg = "保存商品利润失败，Err:" + err.Error()
		return
	}

	resp := new(data_manage.AddChartInfoResp)
	resp.ChartInfoId = chartInfo.ChartInfoId
	resp.UniqueCode = chartInfo.UniqueCode
	resp.ChartType = req.ChartType

	//添加es数据
	go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)

	//新增操作日志
	{
		chartLog := new(data_manage.ChartInfoLog)
		chartLog.ChartInfoId = chartInfo.ChartInfoId
		chartLog.ChartName = req.ChartName
		chartLog.ChartClassifyId = req.ChartClassifyId
		chartLog.SysUserId = sysUser.AdminId
		chartLog.SysUserRealName = sysUser.RealName
		chartLog.UniqueCode = chartInfo.UniqueCode
		chartLog.CreateTime = time.Now()
		chartLog.Content = string(this.Ctx.Input.RequestBody)
		chartLog.Status = "新增商品利润图表"
		chartLog.Method = this.Ctx.Input.URI()
		go data_manage.AddChartInfoLog(chartLog)
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "保存成功"
	br.Data = resp
	br.IsAddLog = true
}

// ProfitChartInfoEdit
// @Title 编辑图表接口
// @Description 编辑图表接口
// @Param	request	body data_manage.EditChartInfoReq true "type json string"
// @Success Ret=200 保存成功
// @router /chart_info/profit/edit [post]
func (this *FutureGoodChartInfoController) ProfitChartInfoEdit() {
	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
	}

	var req request.EditChartInfoReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	req.ChartName = strings.Trim(req.ChartName, " ")
	if req.ChartClassifyId <= 0 {
		br.Msg = "分类参数错误！"
		return
	}

	chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "分类不存在"
			br.ErrMsg = "分类不存在"
			return
		}
		br.Msg = "获取分类信息失败"
		br.ErrMsg = "获取分类信息失败，Err:" + err.Error()
		return
	}
	if chartClassify == nil {
		br.Msg = "分类不存在"
		br.ErrMsg = "分类不存在"
		return
	}
	chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "图表已被删除，请刷新页面"
			br.ErrMsg = "图表已被删除，请刷新页面"
			return
		}
		br.Msg = "获取图表信息失败"
		br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
		return
	}

	if chartItem.Source != utils.CHART_SOURCE_FUTURE_GOOD_PROFIT {
		br.Msg = "该图不是商品利润曲线图！"
		br.IsSendEmail = false
		return
	}

	//图表操作权限
	ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId)
	if !ok {
		br.Msg = "没有该图表的操作权限"
		br.ErrMsg = "没有该图表的操作权限"
		return
	}

	// 查找商品利润图表的扩展信息
	chartInfoFutureGoodProfit := new(future_good.ChartInfoFutureGoodProfit)
	if e := chartInfoFutureGoodProfit.GetItemById(chartItem.ChartInfoId); e != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取基础相关性图表信息失败, Err: " + e.Error()
		return
	}

	//额外的配置信息
	extraReq := req.Extra
	if len(extraReq.DateList) <= 0 {
		br.Msg = "请选择日期"
		br.IsSendEmail = false
		return
	}
	if extraReq.BaseEdbInfoId <= 0 {
		br.Msg = "请选择ETA指标"
		br.IsSendEmail = false
		return
	}
	if len(extraReq.FutureGoodEdbInfoIdList) <= 0 {
		br.Msg = "请选择期货商品指标"
		br.IsSendEmail = false
		return
	}
	extraConfByte, err := json.Marshal(extraReq)
	if err != nil {
		br.Msg = "商品利润曲线图信息异常"
		br.ErrMsg = "商品利润曲线图信息异常,Err:" + err.Error()
		return
	}
	extraConf := string(extraConfByte)

	var edbInfoIdArr []int
	// ETA指标
	edbInfo, err := data_manage.GetEdbInfoById(extraReq.BaseEdbInfoId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "指标不存在！"
			br.ErrMsg = "指标不存在，edbInfoId:" + strconv.Itoa(extraReq.BaseEdbInfoId)
			return
		} else {
			br.Msg = "获取指标信息失败！"
			br.ErrMsg = "获取图表的指标信息失败，Err:" + err.Error()
			return
		}
	}
	if edbInfo == nil {
		br.Msg = "指标已被删除，请重新选择！"
		br.ErrMsg = "指标不存在，ChartInfoId:" + strconv.Itoa(extraReq.BaseEdbInfoId)
		return
	} else {
		if edbInfo.EdbInfoId <= 0 {
			br.Msg = "指标已被删除，请重新选择！"
			br.ErrMsg = "指标不存在，ChartInfoId:" + strconv.Itoa(extraReq.BaseEdbInfoId)
			return
		}
	}

	edbInfoIdArr = append(edbInfoIdArr, extraReq.BaseEdbInfoId)
	edbInfo.EdbNameSource = edbInfo.EdbName

	// 期货商品指标（主力合约）
	futureGoodEdbInfoMap := make(map[int]*future_good.FutureGoodEdbInfo)
	zlFutureGoodEdbInfoList := make([]*future_good.FutureGoodEdbInfo, 0)
	for _, v := range extraReq.FutureGoodEdbInfoIdList {
		if _, ok := futureGoodEdbInfoMap[v.EdbInfoId]; ok {
			continue
		}

		edbInfoId := v.EdbInfoId
		futureGoodEdbInfo, tmpErr := future_good.GetFutureGoodEdbInfo(edbInfoId)
		if tmpErr != nil {
			if tmpErr.Error() == utils.ErrNoRow() {
				br.Msg = "期货商品指标不存在！"
				br.ErrMsg = "期货商品指标不存在，指标id:" + strconv.Itoa(edbInfoId)
				return
			} else {
				br.Msg = "获取指标信息失败！"
				br.ErrMsg = "获取图表的指标信息失败，Err:" + tmpErr.Error()
				return
			}
		}

		if futureGoodEdbInfo == nil {
			br.Msg = "期货商品指标已被删除，请重新选择！"
			br.ErrMsg = "期货商品指标不存在，指标id:" + strconv.Itoa(edbInfoId)
			return
		} else {
			if futureGoodEdbInfo.FutureGoodEdbInfoId <= 0 {
				br.Msg = "期货商品指标已被删除，请重新选择！"
				br.ErrMsg = "期货商品指标不存在，指标id:" + strconv.Itoa(edbInfoId)
				return
			}
		}
		edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
		futureGoodEdbInfoMap[edbInfoId] = futureGoodEdbInfo
		zlFutureGoodEdbInfo, err := future_good.GetFutureGoodEdbInfo(v.EdbInfoId)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取图表，指标信息失败，Err:" + err.Error()
			return
		}
		zlFutureGoodEdbInfoList = append(zlFutureGoodEdbInfoList, zlFutureGoodEdbInfo)
	}

	sort.Ints(edbInfoIdArr)
	var edbInfoIdArrStr []string
	for _, v := range edbInfoIdArr {
		edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
	}
	edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")

	// 图表启用与否
	disableVal := data.CheckIsDisableChart(edbInfoIdArr)

	chartItem.ChartName = req.ChartName
	chartItem.EdbInfoIds = edbInfoIdStr
	chartItem.ChartClassifyId = req.ChartClassifyId
	chartItem.ModifyTime = time.Now()
	chartItem.Disabled = disableVal
	chartItem.LeftMin = req.LeftMin
	chartItem.LeftMax = req.LeftMax
	chartItem.ExtraConfig = extraConf
	chartItemUpdateCol := []string{"ChartName", "EdbInfoIds", "ChartClassifyId", "ModifyTime", "Disabled", "LeftMin", "LeftMax", "ExtraConfig"}

	// 关联指标
	mapList := make([]*data_manage.ChartEdbMapping, 0)
	{
		mapList = append(mapList, &data_manage.ChartEdbMapping{
			ChartEdbMappingId: 0,
			EdbInfoId:         extraReq.BaseEdbInfoId,
			CreateTime:        time.Now(),
			ModifyTime:        time.Now(),
			UniqueCode:        utils.MD5(utils.CHART_PREFIX + "_" + strconv.FormatInt(time.Now().UnixNano(), 10) + "_" + strconv.Itoa(extraReq.BaseEdbInfoId)),
			//MaxData:           0,
			//MinData:           0,
			IsOrder:     true,
			IsAxis:      1,
			EdbInfoType: 1,
			//LeadValue:         0,
			//LeadUnit:          "",
			//ChartStyle:        "",
			//ChartColor:        "",
			//PredictChartColor: "",
			//ChartWidth:        0,
			Source: 1,
		})

		for _, v := range futureGoodEdbInfoMap {
			mapList = append(mapList, &data_manage.ChartEdbMapping{
				ChartEdbMappingId: 0,
				EdbInfoId:         v.FutureGoodEdbInfoId,
				CreateTime:        time.Now(),
				ModifyTime:        time.Now(),
				UniqueCode:        utils.MD5(utils.CHART_PREFIX + "_" + strconv.FormatInt(time.Now().UnixNano(), 10) + "_" + strconv.Itoa(v.FutureGoodEdbInfoId)),
				//MaxData:           0,
				//MinData:           0,
				IsOrder:     true,
				IsAxis:      1,
				EdbInfoType: 1,
				//LeadValue:         0,
				//LeadUnit:          "",
				//ChartStyle:        "",
				//ChartColor:        "",
				//PredictChartColor: "",
				//ChartWidth:        0,
				Source: 2,
			})
		}
	}

	// 获取图表中的指标数据
	_, _, _, xDataList, yDataList, err := future_goodServ.GetProfitChartEdbData(edbInfo, zlFutureGoodEdbInfoList, extraReq.DateList, extraReq.CalculateFormula, extraReq.FutureGoodEdbInfoIdList)
	if err != nil {
		br.Msg = "保存失败"
		br.ErrMsg = "保存商品利润失败，指标信息失败，Err:" + err.Error()
		return
	}
	xDataListByte, err := json.Marshal(xDataList)
	if err != nil {
		br.Msg = "保存失败"
		br.ErrMsg = "保存商品利润失败，转换X轴数据失败，Err:" + err.Error()
		return
	}
	yDataListByte, err := json.Marshal(yDataList)
	if err != nil {
		br.Msg = "保存失败"
		br.ErrMsg = "保存商品利润失败，转换Y轴数据失败，Err:" + err.Error()
		return
	}

	extraUpdateCol := make([]string, 0)
	chartInfoFutureGoodProfit.XValue = string(xDataListByte)
	chartInfoFutureGoodProfit.YValue = string(yDataListByte)
	chartInfoFutureGoodProfit.ProfitName = zlFutureGoodEdbInfoList[0].FutureGoodEdbName + "盘面利润"
	chartInfoFutureGoodProfit.ModifyTime = time.Now()
	extraUpdateCol = []string{"XValue", "YValue", "ProfitName", "ModifyTime"}
	if extraReq.ProfitNameEn != `` {
		chartInfoFutureGoodProfit.ProfitNameEn = extraReq.ProfitNameEn
		extraUpdateCol = append(extraUpdateCol, "ProfitNameEn")
	}
	err = future_good.EditFutureGoodChartInfoAndMapping(chartItem, mapList, chartInfoFutureGoodProfit, chartItemUpdateCol, extraUpdateCol)
	if err != nil {
		br.Msg = "保存失败"
		br.ErrMsg = "保存商品利润失败，Err:" + err.Error()
		return
	}

	if err != nil {
		br.Msg = "保存失败"
		br.ErrMsg = "保存失败,Err:" + err.Error()
		return
	}
	resp := new(data_manage.AddChartInfoResp)
	resp.ChartInfoId = chartItem.ChartInfoId
	resp.UniqueCode = chartItem.UniqueCode
	resp.ChartType = req.ChartType

	//添加es数据
	go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
	//修改my eta es数据
	go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)

	//新增操作日志
	{
		chartLog := new(data_manage.ChartInfoLog)
		chartLog.ChartName = chartItem.ChartName
		chartLog.ChartInfoId = req.ChartInfoId
		chartLog.ChartClassifyId = chartItem.ChartClassifyId
		chartLog.SysUserId = sysUser.AdminId
		chartLog.SysUserRealName = sysUser.RealName
		chartLog.UniqueCode = chartItem.UniqueCode
		chartLog.CreateTime = time.Now()
		chartLog.Content = string(this.Ctx.Input.RequestBody)
		chartLog.Status = "编辑商品利润图表"
		chartLog.Method = this.Ctx.Input.URL()
		go data_manage.AddChartInfoLog(chartLog)
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "保存成功"
	br.Data = resp
	br.IsAddLog = true
}

// copyProfitChartInfo 复制商品利润曲线图
func copyProfitChartInfo(oldChartInfo *data_manage.ChartInfo, chartClassifyId int, chartName string, sysUser *system.Admin, br *models.BaseResponse) (chartInfo *data_manage.ChartInfo) {
	chartInfoId := oldChartInfo.ChartInfoId

	//校验图表名称是否重复
	chartName = strings.Trim(chartName, " ")
	if chartName == "" {
		br.Msg = "请填写图表名称！"
		return
	}
	if chartClassifyId <= 0 {
		br.Msg = "分类参数错误！"
		return
	}
	chartClassify, err := data_manage.GetChartClassifyById(chartClassifyId)
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			br.Msg = "分类不存在"
			br.ErrMsg = "分类不存在"
			return
		}
		br.Msg = "获取分类信息失败"
		br.ErrMsg = "获取分类信息失败，Err:" + err.Error()
		return
	}
	if chartClassify == nil {
		br.Msg = "分类不存在"
		br.ErrMsg = "分类不存在"
		return
	}
	//count, err := data_manage.ChartInfoExist("", edbInfoIdStr)
	//if err != nil && err.Error() != utils.ErrNoRow() {
	//	br.Msg = "保存失败"
	//	br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
	//	return
	//}
	//if count > 0 {
	//	br.Msg = "所选指标已存在相同指标"
	//	return
	//}
	//判断图表是否存在
	var condition string
	var pars []interface{}
	condition += " AND chart_classify_id=? "
	pars = append(pars, chartClassifyId)

	condition += " AND chart_name=? AND source = ? "
	pars = append(pars, chartName, utils.CHART_SOURCE_FUTURE_GOOD_PROFIT)

	count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
	if err != nil {
		br.Msg = "判断图表名称是否存在失败"
		br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
		return
	}

	if count > 0 {
		br.Msg = "图表已存在，请重新填写"
		br.IsSendEmail = false
		return
	}

	//获取原图表关联的指标信息列表
	edbMappingList, err := data_manage.GetChartEdbMappingListV2(chartInfoId)
	if err != nil {
		br.Msg = "获取图表关联的指标信息失败！"
		br.ErrMsg = "获取图表关联的指标信息失败！" + err.Error()
		return
	}

	//添加图表
	timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
	chartInfo = &data_manage.ChartInfo{
		ChartInfoId:     0,
		ChartName:       chartName,
		ChartClassifyId: chartClassifyId,
		SysUserId:       sysUser.AdminId,
		SysUserRealName: sysUser.RealName,
		UniqueCode:      utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
		CreateTime:      time.Now(),
		ModifyTime:      time.Now(),
		DateType:        oldChartInfo.DateType,
		StartDate:       oldChartInfo.StartDate,
		EndDate:         oldChartInfo.EndDate,
		IsSetName:       oldChartInfo.IsSetName,
		EdbInfoIds:      oldChartInfo.EdbInfoIds,
		ChartType:       oldChartInfo.ChartType,
		Calendar:        oldChartInfo.Calendar,
		SeasonStartDate: oldChartInfo.SeasonStartDate,
		SeasonEndDate:   oldChartInfo.SeasonEndDate,
		ChartImage:      oldChartInfo.ChartImage,
		BarConfig:       oldChartInfo.BarConfig,
		ExtraConfig:     oldChartInfo.ExtraConfig,
		//Sort:     sort,
		LeftMin:  oldChartInfo.LeftMin,
		LeftMax:  oldChartInfo.LeftMax,
		RightMin: oldChartInfo.RightMin,
		RightMax: oldChartInfo.RightMax,
		Disabled: oldChartInfo.Disabled,
		Source:   oldChartInfo.Source,
	}

	// 添加图表与指标的关联关系
	mapList := make([]*data_manage.ChartEdbMapping, 0)
	{
		for _, v := range edbMappingList {
			mapItem := &data_manage.ChartEdbMapping{
				//ChartEdbMappingId: 0,
				//ChartInfoId: chartInfo.ChartInfoId,
				EdbInfoId:   v.EdbInfoId,
				CreateTime:  time.Now(),
				ModifyTime:  time.Now(),
				UniqueCode:  utils.MD5(utils.CHART_PREFIX + "_" + strconv.Itoa(v.EdbInfoId) + strconv.FormatInt(time.Now().UnixNano(), 10)),
				MaxData:     v.MaxData,
				MinData:     v.MinData,
				IsOrder:     v.IsOrder,
				IsAxis:      v.IsAxis,
				EdbInfoType: v.EdbInfoType,
				LeadValue:   v.LeadValue,
				LeadUnit:    v.LeadUnit,
				ChartStyle:  v.ChartStyle,
				ChartColor:  v.ChartColor,
				ChartWidth:  v.ChartWidth,
				Source:      v.Source,
			}
			mapList = append(mapList, mapItem)
		}
	}

	// 查找商品利润图表的扩展信息
	oldChartInfoFutureGoodProfit := new(future_good.ChartInfoFutureGoodProfit)
	if e := oldChartInfoFutureGoodProfit.GetItemById(oldChartInfo.ChartInfoId); e != nil {
		br.Msg = "复制失败"
		br.ErrMsg = "获取基础相关性图表信息失败, Err: " + e.Error()
		return
	}

	// 保存
	err = future_good.CreateChartInfoFutureGoodProfitAndEdb(chartInfo, mapList, &future_good.ChartInfoFutureGoodProfit{
		ChartInfoId:  0,
		ProfitName:   oldChartInfoFutureGoodProfit.ProfitName,
		ProfitNameEn: oldChartInfoFutureGoodProfit.ProfitNameEn,
		XValue:       oldChartInfoFutureGoodProfit.XValue,
		YValue:       oldChartInfoFutureGoodProfit.YValue,
		CreateTime:   time.Now(),
		ModifyTime:   time.Now(),
	})
	if err != nil {
		br.Msg = "复制失败"
		br.ErrMsg = "保存商品利润图表失败，Err:" + err.Error()
		return
	}

	//添加es数据
	go data.EsAddOrEditChartInfo(chartInfo.ChartInfoId)
	//修改my eta es数据
	go data.EsAddOrEditMyChartInfoByChartInfoId(chartInfo.ChartInfoId)

	return
}

// PreviewProfitChartInfo
// @Title 图表-获取预览的商品利润曲线图数据
// @Description 图表-获取指标详情接口
// @Param   EdbInfoId   query   int  true       "指标ID"
// @Param   DateType   query   int  true       "日期类型:1:00年至今,2:10年至今,3:15年至今，4：年初至今，5:自定义时间,6:自定义起始时间至今，7：18年至今,8:19年至今,9:20年至今"
// @Param   StartDate   query   string  true       "自定义开始日期"
// @Param   EndDate   query   string  true       "自定义结束日期"
// @Param   EdbInfoType  query int  true     "1:标准指标,0:领先指标，默认为标准指标"
// @Param   LeadValue    query   int  true       "领先值"
// @Param   LeadUnit     query   string  true        "领先单位:天，月，季，周"
// @Success 200 {object} data_manage.ChartEdbInfoDetailResp
// @router /chart_info/profit/preview [post]
func (this *FutureGoodChartInfoController) PreviewProfitChartInfo() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

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

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

	if len(req.FutureGoodEdbInfoIdList) <= 0 {
		br.Msg = "请选择指标"
		return
	}
	if len(req.DateList) <= 0 {
		br.Msg = "请选择日期"
		return
	}
	chartInfo := new(data_manage.ChartInfoView)

	//var edbInfoMapping, futureGoodEdbInfoMapping *data_manage.ChartEdbInfoMapping
	baseEdbInfo, err := data_manage.GetEdbInfoById(req.BaseEdbInfoId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取图表，指标信息失败，Err:" + err.Error()
		return
	}
	// 商品数据库指标
	futureGoodEdbInfoMap := make(map[int]*future_good.FutureGoodEdbInfo)
	zlFutureGoodEdbInfoList := make([]*future_good.FutureGoodEdbInfo, 0)
	for _, v := range req.FutureGoodEdbInfoIdList {
		if _, ok := futureGoodEdbInfoMap[v.EdbInfoId]; ok {
			continue
		}

		zlFutureGoodEdbInfo, err := future_good.GetFutureGoodEdbInfo(v.EdbInfoId)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取图表，指标信息失败，Err:" + err.Error()
			return
		}

		futureGoodEdbInfoMap[v.EdbInfoId] = zlFutureGoodEdbInfo
		zlFutureGoodEdbInfoList = append(zlFutureGoodEdbInfoList, zlFutureGoodEdbInfo)
	}

	if baseEdbInfo == nil {
		br.Msg = "请选择ETA指标"
		br.ErrMsg = "请选择ETA指标"
		br.IsSendEmail = false
		return
	}
	if len(zlFutureGoodEdbInfoList) <= 0 {
		br.Msg = "请选择商品指标"
		br.ErrMsg = "请选择商品指标"
		br.IsSendEmail = false
		return
	}

	// 获取图表中的指标数据
	barConfigEdbInfoIdList, _, _, xDataList, yDataList, err := future_goodServ.GetProfitChartEdbData(baseEdbInfo, zlFutureGoodEdbInfoList, req.DateList, req.CalculateFormula, req.FutureGoodEdbInfoIdList)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取图表，指标信息失败，Err:" + err.Error()
		return
	}

	edbList := make([]*data_manage.ChartEdbInfoMapping, 0)
	edbList = append(edbList, &data_manage.ChartEdbInfoMapping{
		EdbInfoId:      baseEdbInfo.EdbInfoId,
		SourceName:     baseEdbInfo.SourceName,
		Source:         baseEdbInfo.Source,
		EdbCode:        baseEdbInfo.EdbCode,
		EdbName:        baseEdbInfo.EdbName,
		EdbAliasName:   baseEdbInfo.EdbName,
		EdbNameEn:      baseEdbInfo.EdbNameEn,
		EdbAliasNameEn: baseEdbInfo.EdbNameEn,
		EdbType:        baseEdbInfo.EdbType,
		Frequency:      baseEdbInfo.Frequency,
		FrequencyEn:    data.GetFrequencyEn(baseEdbInfo.Frequency),
		Unit:           baseEdbInfo.Unit,
		UnitEn:         baseEdbInfo.UnitEn,
		StartDate:      baseEdbInfo.StartDate,
		EndDate:        baseEdbInfo.EndDate,
		ModifyTime:     baseEdbInfo.ModifyTime.Format(utils.FormatDateTime),
		MappingSource:  1,
	})

	warnEdbList := make([]string, 0)
	for _, v := range edbList {
		if v.IsNullData {
			warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
		}
		for _, reqEdb := range barConfigEdbInfoIdList {
			if v.EdbInfoId == reqEdb.EdbInfoId {
				v.EdbAliasName = reqEdb.Name
				v.EdbAliasNameEn = reqEdb.NameEn
			}
		}
	}
	if len(warnEdbList) > 0 {
		chartInfo.WarnMsg = `图表引用指标异常，异常指标：` + strings.Join(warnEdbList, "，")
	}

	//图表操作权限
	chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId)
	chartInfo.Button.IsEdit = chartInfo.IsEdit
	chartInfo.Button.IsCopy = true
	//判断是否需要展示英文标识
	chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)

	resp := new(data_manage.ChartInfoDetailResp)
	resp.ChartInfo = chartInfo
	resp.EdbInfoList = edbList
	resp.DataResp = data_manage.ProfitFutureGoodChartResp{
		YDataList:    yDataList,
		XDataList:    xDataList,
		ProfitName:   zlFutureGoodEdbInfoList[0].FutureGoodEdbName + "盘面利润",
		ProfitNameEn: req.ProfitNameEn,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// getFutureGoodProfitChartInfo 获取商品利润曲线的数据
func getFutureGoodProfitChartInfo(chartInfo *data_manage.ChartInfoView, sysUser *system.Admin, br *models.BaseResponse) {
	chartInfoId := chartInfo.ChartInfoId

	// 商品利润曲线图的一些配置
	var extraConf request.ChartInfoReq
	err := json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConf)
	if err != nil {
		br.Msg = "商品利润曲线图配置异常"
		br.ErrMsg = "商品利润曲线图配置异常"
		return
	}

	edbList := make([]*data_manage.ChartEdbInfoMapping, 0)
	edbInfoMapping, err := data_manage.GetEtaEdbChartEdbMapping(chartInfoId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取商品利润图表，基础指标信息失败，Err:" + err.Error()
		return
	}
	edbList = append(edbList, edbInfoMapping)
	futureGoodEdbInfoMappingList, err := data_manage.GetFutureGoodEdbChartEdbMappingList(chartInfoId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取商品利润图表，商品指标信息失败，Err:" + err.Error()
		return
	}
	edbList = append(edbList, futureGoodEdbInfoMappingList...)
	if len(edbList) <= 0 {
		br.Msg = "商品利润曲线图表指标异常"
		br.ErrMsg = "商品利润曲线图表指标异常"
		return
	}

	xDataList := make([]data_manage.XData, 0)
	yDataList := make([]data_manage.YData, 0)

	// 查找商品利润图表的扩展信息
	chartInfoFutureGoodProfit := new(future_good.ChartInfoFutureGoodProfit)
	if err = chartInfoFutureGoodProfit.GetItemById(chartInfo.ChartInfoId); err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取基础相关性图表信息失败, Err: " + err.Error()
		return
	}

	err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.XValue), &xDataList)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "转换X轴数据失败, Err: " + err.Error()
		return
	}

	err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.YValue), &yDataList)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "转换Y轴数据失败, Err: " + err.Error()
		return
	}

	warnEdbList := make([]string, 0)
	baseEdbInfo := edbList[0] //现货指标

	for _, v := range edbList {
		if v.IsNullData {
			warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
		}
	}
	if len(warnEdbList) > 0 {
		chartInfo.WarnMsg = `图表引用指标异常，异常指标：` + strings.Join(warnEdbList, "，")
	}
	if chartInfoId > 0 && chartInfo != nil {
		//判断是否加入我的图库
		{
			var myChartCondition string
			var myChartPars []interface{}
			myChartCondition += ` AND a.admin_id=? `
			myChartPars = append(myChartPars, sysUser.AdminId)
			myChartCondition += ` AND a.chart_info_id=? `
			myChartPars = append(myChartPars, chartInfo.ChartInfoId)

			myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
			if err != nil && err.Error() != utils.ErrNoRow() {
				br.Msg = "获取失败"
				br.ErrMsg = "获取我的图表信息失败，GetMyChartByCondition，Err:" + err.Error()
				return
			}
			if myChartList != nil && len(myChartList) > 0 {
				chartInfo.IsAdd = true
				chartInfo.MyChartId = myChartList[0].MyChartId
				chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
			}
		}
	}

	//图表操作权限
	chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId)
	chartInfo.Button.IsEdit = chartInfo.IsEdit
	chartInfo.Button.IsCopy = true
	//判断是否需要展示英文标识
	chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList[0:1], chartInfo.Source, chartInfo.ChartType)
	chartInfo.UnitEn = baseEdbInfo.UnitEn

	resp := new(data_manage.ChartInfoDetailResp)
	resp.ChartInfo = chartInfo
	resp.EdbInfoList = edbList

	resp.DataResp = response.ProfitFutureGoodChartResp{
		YDataList:    yDataList,
		XDataList:    xDataList,
		ProfitName:   chartInfoFutureGoodProfit.ProfitName,
		ProfitNameEn: chartInfoFutureGoodProfit.ProfitNameEn,
		Extra:        extraConf,
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// GetFutureGoodProfitChartInfoDetailFromUniqueCode 根据编码获取图表详情
func GetFutureGoodProfitChartInfoDetailFromUniqueCode(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
	resp = new(data_manage.ChartInfoDetailFromUniqueCodeResp)

	adminId := sysUser.AdminId

	//判断是否存在缓存，如果存在缓存，那么直接从缓存中获取
	key := data.GetChartInfoDataKey(chartInfo.ChartInfoId)
	if utils.Re == nil && isCache {
		if utils.Re == nil && utils.Rc.IsExist(key) {
			if chartData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
				err := json.Unmarshal(chartData, &resp)
				if err == nil && resp != nil {
					// 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
					var myCond string
					var myPars []interface{}
					myCond += ` AND a.admin_id=? `
					myPars = append(myPars, adminId)
					myCond += ` AND a.chart_info_id=? `
					myPars = append(myPars, chartInfo.ChartInfoId)
					myList, err := data_manage.GetMyChartByCondition(myCond, myPars)
					if err != nil && err.Error() != utils.ErrNoRow() {
						msg = "获取失败"
						errMsg = "获取我的图表信息失败，GetMyChartByCondition，Err:" + err.Error()
						return
					}
					resp.ChartInfo.IsAdd = false
					resp.ChartInfo.MyChartId = 0
					resp.ChartInfo.MyChartClassifyId = ""
					if myList != nil && len(myList) > 0 {
						resp.ChartInfo.IsAdd = true
						resp.ChartInfo.MyChartId = myList[0].MyChartId
						resp.ChartInfo.MyChartClassifyId = myList[0].MyChartClassifyId
					}

					isOk = true
					fmt.Println("source redis")
					return
				}
			}
		}
	}

	chartInfoId := chartInfo.ChartInfoId

	// 商品利润曲线图的一些配置
	var extraConf request.ChartInfoReq
	err := json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConf)
	if err != nil {
		msg = "商品利润曲线图配置异常"
		errMsg = "商品利润曲线图配置异常，Err:" + err.Error()
		return
	}

	edbList := make([]*data_manage.ChartEdbInfoMapping, 0)
	edbInfoMapping, err := data_manage.GetEtaEdbChartEdbMapping(chartInfoId)
	if err != nil {
		msg = "获取失败"
		errMsg = "获取商品利润图表，基础指标信息失败，Err:" + err.Error()
		return
	}
	edbList = append(edbList, edbInfoMapping)
	futureGoodEdbInfoMappingList, err := data_manage.GetFutureGoodEdbChartEdbMappingList(chartInfoId)
	if err != nil {
		msg = "获取失败"
		errMsg = "获取商品利润图表，商品指标信息失败，Err:" + err.Error()
		return
	}
	edbList = append(edbList, futureGoodEdbInfoMappingList...)
	if len(edbList) <= 0 {
		msg = "商品利润曲线图表指标异常"
		errMsg = "商品利润曲线图表指标异常"
		return
	}

	xDataList := make([]data_manage.XData, 0)
	yDataList := make([]data_manage.YData, 0)

	// 查找商品利润图表的扩展信息
	chartInfoFutureGoodProfit := new(future_good.ChartInfoFutureGoodProfit)
	if err = chartInfoFutureGoodProfit.GetItemById(chartInfo.ChartInfoId); err != nil {
		msg = "获取失败"
		errMsg = "获取基础相关性图表信息失败, Err: " + err.Error()
		return
	}

	err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.XValue), &xDataList)
	if err != nil {
		msg = "获取失败"
		errMsg = "转换X轴数据失败, Err: " + err.Error()
		return
	}

	err = json.Unmarshal([]byte(chartInfoFutureGoodProfit.YValue), &yDataList)
	if err != nil {
		msg = "获取失败"
		errMsg = "转换Y轴数据失败, Err: " + err.Error()
		return
	}

	warnEdbList := make([]string, 0)
	baseEdbInfo := edbList[0] //现货指标

	for _, v := range edbList {
		if v.IsNullData {
			warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
		}
	}
	if len(warnEdbList) > 0 {
		chartInfo.WarnMsg = `图表引用指标异常，异常指标：` + strings.Join(warnEdbList, "，")
	}
	if chartInfoId > 0 && chartInfo != nil {
		//判断是否加入我的图库
		{
			var myChartCondition string
			var myChartPars []interface{}
			myChartCondition += ` AND a.admin_id=? `
			myChartPars = append(myChartPars, sysUser.AdminId)
			myChartCondition += ` AND a.chart_info_id=? `
			myChartPars = append(myChartPars, chartInfo.ChartInfoId)

			myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
			if err != nil && err.Error() != utils.ErrNoRow() {
				msg = "获取失败"
				errMsg = "获取我的图表信息失败，GetMyChartByCondition，Err: " + err.Error()
				return
			}
			if myChartList != nil && len(myChartList) > 0 {
				chartInfo.IsAdd = true
				chartInfo.MyChartId = myChartList[0].MyChartId
				chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
			}
		}
	}

	//图表操作权限
	chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId)
	chartInfo.Button.IsEdit = chartInfo.IsEdit
	chartInfo.Button.IsCopy = true
	//判断是否需要展示英文标识
	chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList[0:1], chartInfo.Source, chartInfo.ChartType)
	chartInfo.UnitEn = baseEdbInfo.UnitEn

	resp.ChartInfo = chartInfo
	resp.EdbInfoList = edbList
	resp.DataResp = response.ProfitFutureGoodChartResp{
		YDataList:    yDataList,
		XDataList:    xDataList,
		ProfitName:   chartInfoFutureGoodProfit.ProfitName,
		ProfitNameEn: chartInfoFutureGoodProfit.ProfitNameEn,
		Extra:        extraConf,
	}
	resp.Status = true

	// 将数据加入缓存
	if utils.Re == nil {
		data, _ := json.Marshal(resp)
		utils.Rc.Put(key, data, 2*time.Hour)
	}
	isOk = true

	return
}
