package future_good

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strings"
	"time"
)

// ChartInfoFutureGoodProfit 商品利润图表-扩展信息
type ChartInfoFutureGoodProfit struct {
	ChartInfoId  int       `orm:"column(chart_info_id);pk" description:"商品利润图表ID(chart_info表source=5的)"`
	ProfitName   string    `description:"利润名称"`
	ProfitNameEn string    `description:"利润英文名称"`
	XValue       string    `description:"X轴数据值"`
	YValue       string    `description:"Y轴数据值"`
	CreateTime   time.Time `description:"创建时间"`
	ModifyTime   time.Time `description:"更新时间"`
}

type CorrelationInfo struct {
	LeadValue       int    `description:"领先值"`
	LeadUnit        string `description:"领先单位"`
	CalculateValue  int    `description:"计算窗口"`
	CalculateUnit   string `description:"计算频度"`
	StartDate       string `description:"开始日期"`
	EndDate         string `description:"结束日期"`
	EdbInfoIdFirst  int    `description:"A指标ID"`
	EdbInfoIdSecond int    `description:"B指标ID"`
	PeriodData      string `description:"X轴-期数数据"`
	CorrelationData string `description:"Y轴-商品利润系数"`
}

func (m *ChartInfoFutureGoodProfit) TableName() string {
	return "chart_info_future_good_profit"
}

func (m *ChartInfoFutureGoodProfit) Create() (err error) {
	o := orm.NewOrm()
	_, err = o.Insert(m)
	if err != nil {
		return
	}
	//m.CorrelationChartInfoId = int(id)
	return
}

func (m *ChartInfoFutureGoodProfit) Update(cols []string) (err error) {
	o := orm.NewOrm()
	_, err = o.Update(m, cols...)
	return
}

func (m *ChartInfoFutureGoodProfit) Delete() (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`DELETE FROM %s WHERE chart_info_id = ? LIMIT 1`, m.TableName())
	_, err = o.Raw(sql, m.ChartInfoId).Exec()
	return
}

func (m *ChartInfoFutureGoodProfit) GetItemById(id int) (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE chart_info_id = ? LIMIT 1`, m.TableName())
	err = o.Raw(sql, id).QueryRow(&m)
	return
}

func (m *ChartInfoFutureGoodProfit) GetItemByCondition(condition string, pars []interface{}) (err error) {
	o := orm.NewOrm()
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&m)
	return
}

func (m *ChartInfoFutureGoodProfit) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ChartInfoFutureGoodProfit, err error) {
	o := orm.NewOrm()
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := ``
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}
