package eta_forum

import (
	"eta/eta_mobile/models/data_manage"
	"eta/eta_mobile/services/alarm_msg"
	"eta/eta_mobile/utils"
	"fmt"
	"github.com/rdlucklib/rdluck_tools/paging"
	"time"
)

type UserChartListReq struct {
	Keyword         string `description:"搜索关键词"`
	PageSize        int
	CurrentIndex    int
	UserMobile      string `description:"用户手机号"`
	BusinessCode    string `description:"商户号"`
	UserTelAreaCode string `description:"手机号区号"`
}

type UserChartListResp struct {
	Ret         int
	Msg         string
	ErrMsg      string
	ErrCode     string
	Data        *UserChartListRespItem
	Success     bool `description:"true 执行成功，false 执行失败"`
	IsSendEmail bool `json:"-" description:"true 发送邮件，false 不发送邮件"`
	IsAddLog    bool `json:"-" description:"true 新增操作日志，false 不新增操作日志" `
}

type UserChartListRespItem struct {
	ChartInfoList []*data_manage.ChartInfo
	Paging        *paging.PagingItem `description:"分页数据"`
}

type UserCollectChartClassifyListResp struct {
	Ret         int
	Msg         string
	ErrMsg      string
	ErrCode     string
	Data        *UserCollectChartClassifyListItem
	Success     bool `description:"true 执行成功，false 执行失败"`
	IsSendEmail bool `json:"-" description:"true 发送邮件，false 不发送邮件"`
	IsAddLog    bool `json:"-" description:"true 新增操作日志，false 不新增操作日志" `
}

type UserCollectChartClassifyListItem struct {
	List     []*ChartCollectClassifyItem
	Language string `description:"指标的展示语言，CN：中文，EN：英文"`
}

// ChartCollectClassifyItem 我的图表分类信息
type ChartCollectClassifyItem struct {
	CollectClassifyId int    `description:"分类ID"`
	ClassifyName      string `description:"分类名称"`
	UserId            int    `description:"创建人id"`
	ChartNum          int    `description:"分类下的图表数量"`
}

type UserCollectChartListResp struct {
	Ret         int
	Msg         string
	ErrMsg      string
	ErrCode     string
	Data        *UserCollectChartListRespItem
	Success     bool `description:"true 执行成功，false 执行失败"`
	IsSendEmail bool `json:"-" description:"true 发送邮件，false 不发送邮件"`
	IsAddLog    bool `json:"-" description:"true 新增操作日志，false 不新增操作日志" `
}
type UserCollectChartListRespItem struct {
	Paging *paging.PagingItem
	List   []*ChartCollectView
}

type ChartCollectView struct {
	ChartCollectId    int
	ChartInfoId       int       `description:"图表id"`
	UserId            int       `description:"用户id"`
	CreateTime        time.Time `description:"创建时间"`
	CollectTime       time.Time `description:"收藏时间"`
	CollectClassifyId int
	ChartName         string `description:"来源名称"`
	ChartNameEn       string `description:"英文图表名称"`
	ChartImage        string `description:"图表图片"`
	UniqueCode        string `description:"图表唯一编码"`
}

type ChartFromUniqueCodeResp struct {
	Ret         int
	Msg         string
	ErrMsg      string
	ErrCode     string
	Data        *ChartFromUniqueCodeRespItem
	Success     bool `description:"true 执行成功，false 执行失败"`
	IsSendEmail bool `json:"-" description:"true 发送邮件，false 不发送邮件"`
	IsAddLog    bool `json:"-" description:"true 新增操作日志，false 不新增操作日志" `
}

type ChartFromUniqueCodeRespItem struct {
	ChartInfo   *data_manage.ChartInfoView
	EdbInfoList []*data_manage.ChartEdbInfoMapping
	XEdbIdValue []int               `description:"柱方图的x轴数据，指标id"`
	YDataList   []data_manage.YData `description:"柱方图的y轴数据"`
	XDataList   []data_manage.XData `description:"商品价格曲线的X轴数据"`
	//BarChartInfo BarChartInfoReq `description:"柱方图的配置"`
	//CorrelationChartInfo *CorrelationInfo `description:"相关性图表信息"`
	DataResp  interface{} `description:"图表数据,根据图的类型而定的,没有确定的数据格式"`
	WaterMark string      `description:"水印"`
}

// GetUserChartList 查询社区中对用户可见的图表列表
func GetUserChartList(businessCode, userMobile, telAreaCode, keyword string, currentIndex, pageSize int) (resp UserChartListRespItem, err error, errMsg string) {
	defer func() {
		if err != nil {
			msg := fmt.Sprintf("查询社区中对用户可见的图表列表 GetUserChartList：Err:%v,ErrMsg:%s", err, errMsg)
			utils.FileLog.Info(msg)
			go alarm_msg.SendAlarmMsg(msg, 3)
		}
	}()
	urlQuery := fmt.Sprintf("BusinessCode=%s&UserMobile=%s&UserTelAreaCode=%s&Keyword=%s&CurrentIndex=%d&PageSize=%d", businessCode, userMobile, telAreaCode, keyword, currentIndex, pageSize)
	result, err := GetUserChartListLib(urlQuery)
	if err != nil {
		errMsg = "查询失败"
		err = fmt.Errorf("查询失败,Err:" + err.Error())
		return
	}
	if result.Ret != 200 {
		errMsg = "查询失败"
		err = fmt.Errorf(result.Msg + result.ErrMsg)
		return
	}
	resp.Paging = result.Data.Paging
	resp.ChartInfoList = result.Data.ChartInfoList

	return
}

// GetUserCollectChartClassifyList 查询社区中用户收藏的分类列表
func GetUserCollectChartClassifyList(businessCode, userMobile, telAreaCode string) (resp UserCollectChartClassifyListItem, err error, errMsg string) {
	defer func() {
		if err != nil {
			msg := fmt.Sprintf("查询社区中用户收藏的分类列表 GetUserCollectChartClassifyList：Err:%v,ErrMsg:%s", err, errMsg)
			utils.FileLog.Info(msg)
			go alarm_msg.SendAlarmMsg(msg, 3)
		}
	}()
	urlQuery := fmt.Sprintf("BusinessCode=%s&UserMobile=%s&UserTelAreaCode=%s", businessCode, userMobile, telAreaCode)
	result, err := getUserCollectChartClassifyLib(urlQuery)
	if err != nil {
		errMsg = "查询失败"
		err = fmt.Errorf("查询失败,Err:" + err.Error())
		return
	}
	if result.Ret != 200 {
		errMsg = "查询失败"
		err = fmt.Errorf(result.Msg + result.ErrMsg)
		return
	}
	resp.List = result.Data.List
	resp.Language = result.Data.Language

	return
}

// GetUserCollectChartList 查询社区中对用户收藏的图表列表
func GetUserCollectChartList(businessCode, userMobile, telAreaCode, keyword, collectClassifyIds string, currentIndex, pageSize int) (resp UserCollectChartListRespItem, err error, errMsg string) {
	defer func() {
		if err != nil {
			msg := fmt.Sprintf("查询社区中对用户收藏的图表列表 GetUserCollectChartList：Err:%v,ErrMsg:%s", err, errMsg)
			utils.FileLog.Info(msg)
			go alarm_msg.SendAlarmMsg(msg, 3)
		}
	}()
	urlQuery := fmt.Sprintf("BusinessCode=%s&UserMobile=%s&UserTelAreaCode=%s&Keyword=%s&CollectClassifyIds=%s&CurrentIndex=%d&PageSize=%d", businessCode, userMobile, telAreaCode, keyword, collectClassifyIds, currentIndex, pageSize)
	result, err := getUserCollectChartListLib(urlQuery)
	if err != nil {
		errMsg = "查询失败"
		err = fmt.Errorf("查询失败,Err:" + err.Error())
		return
	}
	if result.Ret != 200 {
		errMsg = "查询失败"
		err = fmt.Errorf(result.Msg + result.ErrMsg)
		return
	}
	resp.Paging = result.Data.Paging
	resp.List = result.Data.List

	return
}

// GeChartFromUniqueCode 社区中根据唯一编码查询图表
func GeChartFromUniqueCode(uniqueCode string, isCache bool) (resp ChartFromUniqueCodeRespItem, err error, errMsg string) {
	defer func() {
		if err != nil {
			msg := fmt.Sprintf("查询社区中对用户可见的图表列表 GeChartFromUniqueCode：Err:%v,ErrMsg:%s", err, errMsg)
			utils.FileLog.Info(msg)
			go alarm_msg.SendAlarmMsg(msg, 3)
		}
	}()
	urlQuery := fmt.Sprintf("UniqueCode=%s&isCache=%v", uniqueCode, isCache)
	result, err := getChartFromUniqueCodeLib(urlQuery)
	if err != nil {
		errMsg = "查询失败"
		err = fmt.Errorf("查询失败,Err:" + err.Error())
		return
	}
	if result.Ret != 200 {
		errMsg = "查询失败"
		err = fmt.Errorf(result.Msg + result.ErrMsg)
		return
	}
	resp = *result.Data

	return
}
