package data_manage

import (
	"eta/eta_mobile/utils"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

// EditFutureGoodChartEnInfoAndEdbEnInfo 更改商品价格曲线英文信息
func EditFutureGoodChartEnInfoAndEdbEnInfo(chartInfoId int, chartNameEn string, edbInfoId int, edbNameEn, edbUnitEn string) (err error) {
	o := orm.NewOrmUsingDB("data")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	sql := ` UPDATE  chart_info
			SET
			  chart_name_en =?,
			  modify_time = NOW()
			WHERE chart_info_id = ?`

	_, err = to.Raw(sql, chartNameEn, chartInfoId).Exec()
	if err != nil {
		fmt.Println("UPDATE  chart_info Err:", err.Error())
		return err
	}
	var count int
	csql := `SELECT COUNT(1) AS count FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = 1 `
	err = to.Raw(csql, chartInfoId, edbInfoId).QueryRow(&count)
	if err != nil {
		fmt.Println("QueryRow Err:", err.Error())
		return err
	}
	if count > 0 {
		msql := ` UPDATE  edb_info
			SET edb_name_en = ?,
			  unit_en = ?,
			  modify_time = NOW()
			WHERE edb_info_id = ? `
		_, err = to.Raw(msql, edbNameEn, edbUnitEn, edbInfoId).Exec()
		if err != nil {
			fmt.Println("edb_info Err:" + err.Error())
			return err
		}
	}

	return
}

type EditFutureGoodChartInfoBaseReq struct {
	ChartInfoId int    `description:"图表ID"`
	ChartName   string `description:"图表名称（根据当前语言版本不同而不同）"`
	/*	EdbName        string `description:"指标名称（根据当前语言版本不同而不同）"`
		Unit           string `description:"指标单位（根据当前语言版本不同而不同）"`*/
	ProfitName       string                      `description:"利润名称（根据当前语言版本不同而不同）"`
	FutureGoodName   string                      `description:"期货合约名称（根据当前语言版本不同而不同）"`
	ChartEdbInfoList []*EditChartInfoEdbBaseItem `description:"指标及配置信息"`
	XDataList        []string                    `description:"商品价格曲线的X轴数据"`
}

// EditBaseFutureGoodChartInfoAndEdbEnInfo
// @Description:  编辑期货商品基础的图表信息及指标信息
// @author: Roc
// @datetime 2024-04-23 13:58:23
// @param chartInfo ChartInfo
// @param chartName string
// @param edbInfoId int
// @param edbName string
// @param edbUnit string
// @param lang string
// @return err error
func EditBaseFutureGoodChartInfoAndEdbEnInfo(chartInfo *ChartInfo, req *EditFutureGoodChartInfoBaseReq, lang string) (err error) {
	o := orm.NewOrmUsingDB("data")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	// 修改图表信息
	updateChartCols := make([]string, 0)
	switch lang {
	case utils.EnLangVersion:
		chartInfo.ChartNameEn = req.ChartName
		updateChartCols = append(updateChartCols, "ChartNameEn")
	default:
		chartInfo.ChartName = req.ChartName
		updateChartCols = append(updateChartCols, "ChartName")
	}
	chartInfo.ModifyTime = time.Now()
	updateChartCols = append(updateChartCols, "ModifyTime", "BarConfig")
	_, err = to.Update(chartInfo, updateChartCols...)
	if err != nil {
		fmt.Println("UPDATE  chart_info Err:", err.Error())
		return err
	}
	for _, v := range req.ChartEdbInfoList {
		msql := ` UPDATE edb_info SET modify_time = NOW()  `
		pars := make([]interface{}, 0)
		switch lang {
		case utils.EnLangVersion:
			if v.EdbName != `` {
				msql += ` ,edb_name_en = ? `
				pars = append(pars, v.EdbName)
			}

			if v.Unit != `` {
				msql += ` ,unit_en = ? `
				pars = append(pars, v.Unit)
			}

		default:

			if v.EdbName != `` {
				msql += ` ,edb_name = ? `
				pars = append(pars, v.EdbName)
			}

			if v.Unit != `` {
				msql += ` ,unit = ? `
				pars = append(pars, v.Unit)
			}
		}
		msql += ` WHERE edb_info_id = ? `
		pars = append(pars, v.EdbInfoId)
		_, err = to.Raw(msql, pars...).Exec()
		if err != nil {
			fmt.Println("edb_info Err:" + err.Error())
			return err
		}
	}
	return
}

// EditFutureGoodProfitChartEnInfoAndEdbEnInfo 更改商品利润曲线英文信息
func EditFutureGoodProfitChartEnInfoAndEdbEnInfo(chartInfoId int, chartNameEn string, edbInfoId int, edbNameEn, edbUnitEn, profitNameEn string) (err error) {
	o := orm.NewOrmUsingDB("data")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	sql := ` UPDATE  chart_info
			SET chart_name_en =?,modify_time = NOW()
			WHERE chart_info_id = ?`

	_, err = to.Raw(sql, chartNameEn, chartInfoId).Exec()
	if err != nil {
		fmt.Println("UPDATE  chart_info Err:", err.Error())
		return
	}

	// 更改指标英文信息
	sql = ` UPDATE  edb_info
			SET edb_name_en = ?,unit_en = ?,modify_time = NOW()
			WHERE edb_info_id = ? `
	_, err = to.Raw(sql, edbNameEn, edbUnitEn, edbInfoId).Exec()
	if err != nil {
		fmt.Println("edb_info Err:" + err.Error())
		return
	}

	// 更改指标英文信息
	sql = ` UPDATE  chart_info_future_good_profit
			SET profit_name_en = ?,modify_time = NOW()
			WHERE chart_info_id = ? `
	_, err = to.Raw(sql, profitNameEn, chartInfoId).Exec()
	if err != nil {
		fmt.Println("chart_info_future_good_profit Err:" + err.Error())
		return
	}

	return
}

// EditBaseFutureGoodProfitChartInfoAndEdbEnInfo
// @Description: 更改商品利润曲线基础信息
// @author: Roc
// @datetime 2024-04-23 14:36:45
// @param chartInfo *ChartInfo
// @param chartName string
// @param edbInfoId int
// @param edbName string
// @param edbUnit string
// @param profitName string
// @param lang string
// @return err error
func EditBaseFutureGoodProfitChartInfoAndEdbEnInfo(chartInfo *ChartInfo, req *EditFutureGoodChartInfoBaseReq, lang string) (err error) {
	o := orm.NewOrmUsingDB("data")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	// 修改图表信息
	updateChartCols := make([]string, 0)
	switch lang {
	case utils.EnLangVersion:
		chartInfo.ChartNameEn = req.ChartName
		updateChartCols = append(updateChartCols, "ChartNameEn")
	default:
		chartInfo.ChartName = req.ChartName
		updateChartCols = append(updateChartCols, "ChartName")
	}
	chartInfo.ModifyTime = time.Now()
	updateChartCols = append(updateChartCols, "ModifyTime", "ExtraConfig")
	_, err = to.Update(chartInfo, updateChartCols...)
	if err != nil {
		fmt.Println("UPDATE  chart_info Err:", err.Error())
		return err
	}
	var sql string
	// 更改指标英文信息
	for _, v := range req.ChartEdbInfoList {
		msql := ` UPDATE edb_info SET modify_time = NOW()  `
		pars := make([]interface{}, 0)
		switch lang {
		case utils.EnLangVersion:
			if v.EdbName != `` {
				msql += ` ,edb_name_en = ? `
				pars = append(pars, v.EdbName)
			}

			if v.Unit != `` {
				msql += ` ,unit_en = ? `
				pars = append(pars, v.Unit)
			}

		default:

			if v.EdbName != `` {
				msql += ` ,edb_name = ? `
				pars = append(pars, v.EdbName)
			}

			if v.Unit != `` {
				msql += ` ,unit = ? `
				pars = append(pars, v.Unit)
			}
		}
		msql += ` WHERE edb_info_id = ? `
		pars = append(pars, v.EdbInfoId)
		_, err = to.Raw(msql, pars...).Exec()
		if err != nil {
			fmt.Println("edb_info Err:" + err.Error())
			return err
		}
	}

	// 更改指标英文信息

	switch lang {
	case utils.EnLangVersion:
		sql = ` UPDATE  chart_info_future_good_profit SET profit_name_en = ?,modify_time = NOW() WHERE chart_info_id = ? `
	default:
		sql = ` UPDATE  chart_info_future_good_profit SET profit_name = ?,modify_time = NOW() WHERE chart_info_id = ? `
	}
	_, err = to.Raw(sql, req.ProfitName, chartInfo.ChartInfoId).Exec()
	if err != nil {
		fmt.Println("chart_info_future_good_profit Err:" + err.Error())
		return
	}

	return
}
