package data_manage

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

// ChartFramework 图库框架表
type ChartFramework struct {
	ChartFrameworkId int       `orm:"column(chart_framework_id);pk"`
	FrameworkCode    string    `description:"框架唯一编码"`
	FrameworkName    string    `description:"框架名称"`
	FrameworkImg     string    `description:"框架图片"`
	FrameworkContent string    `description:"框架内容"`
	IsPublic         int       `description:"是否公开：0-私有；1-公开"`
	PublicTime       time.Time `description:"公开时间"`
	Sort             int       `description:"排序"`
	AdminId          int       `description:"创建人ID"`
	AdminName        string    `description:"创建人姓名"`
	CreateTime       time.Time `description:"创建时间"`
	ModifyTime       time.Time `description:"更新时间"`
}

func (m *ChartFramework) TableName() string {
	return "chart_framework"
}

func (m *ChartFramework) PrimaryId() string {
	return ChartFrameworkColumns.ChartFrameworkId
}

var ChartFrameworkColumns = struct {
	ChartFrameworkId string
	FrameworkCode    string
	FrameworkName    string
	FrameworkImg     string
	FrameworkContent string
	IsPublic         string
	PublicTime       string
	Sort             string
	AdminId          string
	AdminName        string
	CreateTime       string
	ModifyTime       string
}{
	ChartFrameworkId: "chart_framework_id",
	FrameworkCode:    "framework_code",
	FrameworkName:    "framework_name",
	FrameworkImg:     "framework_img",
	FrameworkContent: "framework_content",
	IsPublic:         "is_public",
	PublicTime:       "public_time",
	Sort:             "sort",
	AdminId:          "admin_id",
	AdminName:        "admin_name",
	CreateTime:       "create_time",
	ModifyTime:       "modify_time",
}

func (m *ChartFramework) Create() (err error) {
	o := orm.NewOrmUsingDB("data")
	id, err := o.Insert(m)
	if err != nil {
		return
	}
	m.ChartFrameworkId = int(id)
	return
}

func (m *ChartFramework) CreateMulti(items []*ChartFramework) (err error) {
	if len(items) == 0 {
		return
	}
	o := orm.NewOrmUsingDB("data")
	_, err = o.InsertMulti(len(items), items)
	return
}

func (m *ChartFramework) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Update(m, cols...)
	return
}

func (m *ChartFramework) Del() (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	_, err = o.Raw(sql, m.ChartFrameworkId).Exec()
	return
}

func (m *ChartFramework) GetItemById(id int) (item *ChartFramework, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	err = o.Raw(sql, id).QueryRow(&item)
	return
}

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

func (m *ChartFramework) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func (m *ChartFramework) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ChartFramework, err error) {
	o := orm.NewOrmUsingDB("data")
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	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
}

func (m *ChartFramework) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*ChartFramework, err error) {
	o := orm.NewOrmUsingDB("data")
	fields := strings.Join(fieldArr, ",")
	if len(fieldArr) == 0 {
		fields = `*`
	}
	order := `ORDER BY create_time DESC`
	if orderRule != "" {
		order = ` ORDER BY ` + orderRule
	}
	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

func (m *ChartFramework) CreateFrameworkAndNodes(item *ChartFramework, nodes []*ChartFrameworkNode) (err error) {
	o := orm.NewOrmUsingDB("data")
	tx, e := o.Begin()
	if e != nil {
		err = fmt.Errorf("orm begin err: %s", e.Error())
		return
	}
	defer func() {
		if err != nil {
			_ = tx.Rollback()
			return
		}
		_ = tx.Commit()
	}()

	id, e := tx.Insert(item)
	if e != nil {
		err = fmt.Errorf("insert framework err: %s", e.Error())
		return
	}
	newId := int(id)
	item.ChartFrameworkId = newId

	if len(nodes) > 0 {
		for _, n := range nodes {
			n.ChartFrameworkId = newId
		}
		_, e = tx.InsertMulti(len(nodes), nodes)
		if e != nil {
			err = fmt.Errorf("insert multi nodes err: %s", e.Error())
			return
		}
	}
	return
}

func (m *ChartFramework) EditFrameworkAndNodes(item *ChartFramework, updateCols []string, nodes []*ChartFrameworkNode) (err error) {
	o := orm.NewOrmUsingDB("data")
	tx, e := o.Begin()
	if e != nil {
		err = fmt.Errorf("orm begin err: %s", e.Error())
		return
	}
	defer func() {
		if err != nil {
			_ = tx.Rollback()
			return
		}
		_ = tx.Commit()
	}()

	_, e = tx.Update(item, updateCols...)
	if e != nil {
		err = fmt.Errorf("framework update err: %s", e.Error())
		return
	}

	sql := `DELETE FROM chart_framework_node WHERE chart_framework_id = ?`
	_, e = tx.Raw(sql, item.ChartFrameworkId).Exec()
	if e != nil {
		err = fmt.Errorf("clear nodes err: %s", e.Error())
		return
	}
	if len(nodes) > 0 {
		_, e = tx.InsertMulti(len(nodes), nodes)
		if e != nil {
			err = fmt.Errorf("insert multi nodes err: %s", e.Error())
			return
		}
	}
	return
}

func (m *ChartFramework) RemoveFrameworkAndNodes(frameworkId int) (err error) {
	o := orm.NewOrmUsingDB("data")
	tx, e := o.Begin()
	if e != nil {
		err = fmt.Errorf("orm begin err: %s", e.Error())
		return
	}
	defer func() {
		if err != nil {
			_ = tx.Rollback()
			return
		}
		_ = tx.Commit()
	}()

	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
	_, e = tx.Raw(sql, frameworkId).Exec()
	if e != nil {
		err = fmt.Errorf("delete framework err: %s", e.Error())
		return
	}

	sql = `DELETE FROM chart_framework_node WHERE chart_framework_id = ?`
	_, e = tx.Raw(sql, frameworkId).Exec()
	if e != nil {
		err = fmt.Errorf("clear nodes err: %s", e.Error())
		return
	}
	return
}

// ChartFrameworkAddReq 图库框架新增请求体
type ChartFrameworkAddReq struct {
	FrameworkName    string                  `description:"框架名称"`
	FrameworkImg     string                  `description:"框架图片"`
	FrameworkContent string                  `description:"框架内容"`
	Nodes            []ChartFrameworkNodeReq `description:"框架节点"`
}

// ChartFrameworkNodeReq 图库框架节点请求体
type ChartFrameworkNodeReq struct {
	MyChartClassifyId int    `description:"我的图表分类ID"`
	NodeId            string `description:"节点ID"`
	NodeName          string `description:"节点名称"`
}

// ChartFrameworkEditReq 图库框架编辑请求体
type ChartFrameworkEditReq struct {
	ChartFrameworkId int `description:"图库框架ID"`
	ChartFrameworkAddReq
}

// ChartFrameworkRemoveReq 图库框架编辑请求体
type ChartFrameworkRemoveReq struct {
	ChartFrameworkId int `description:"图库框架ID"`
}

// ChartFrameworkRenameReq 图库框架重命名请求体
type ChartFrameworkRenameReq struct {
	ChartFrameworkId int    `description:"图库框架ID"`
	FrameworkName    string `description:"框架名称"`
}

// ChartFrameworkEditPublicReq 图库框架编辑公开请求体
type ChartFrameworkEditPublicReq struct {
	ChartFrameworkId int `description:"图库框架ID"`
	IsPublic         int `description:"0-隐藏公开; 1-公开"`
}

// ChartFrameworkMoveReq 图库框架移动排序请求体
type ChartFrameworkMoveReq struct {
	ChartFrameworkId     int `description:"图库框架ID"`
	PrevChartFrameworkId int `description:"上一个框架ID"`
	NextChartFrameworkId int `description:"下一个框架ID"`
}

// UpdateChartFrameworkSort 更新我的图库框架排序
func UpdateChartFrameworkSort(adminId, frameworkId, current int, updates string) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := fmt.Sprintf(`UPDATE chart_framework SET sort = %s WHERE admin_id = ? and sort > ?`, updates)
	if frameworkId > 0 {
		sql += ` OR (chart_framework_id > ` + fmt.Sprint(frameworkId) + ` AND sort = ` + fmt.Sprint(current) + `)`
	}
	_, err = o.Raw(sql, adminId, current).Exec()
	return
}

// GetFirstChartFramework 获取我的图库框架排首位的数据
func GetFirstChartFramework(adminId int) (item *ChartFramework, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM chart_framework WHERE admin_id = ? ORDER BY sort ASC,chart_framework_id ASC LIMIT 1`
	err = o.Raw(sql, adminId).QueryRow(&item)
	return
}

// ChartFrameworkItem 图库框架表信息
type ChartFrameworkItem struct {
	ChartFrameworkId int                       `description:"框架ID"`
	FrameworkCode    string                    `description:"框架唯一编码"`
	FrameworkName    string                    `description:"框架名称"`
	FrameworkImg     string                    `description:"框架图片"`
	FrameworkContent string                    `description:"框架内容"`
	IsPublic         int                       `description:"是否公开：0-私有；1-公开"`
	PublicTime       string                    `description:"公开时间"`
	Sort             int                       `description:"排序"`
	AdminId          int                       `description:"创建人ID"`
	AdminName        string                    `description:"创建人姓名"`
	CreateTime       string                    `description:"创建时间"`
	ModifyTime       string                    `description:"更新时间"`
	Nodes            []*ChartFrameworkNodeItem `description:"框架节点"`
}

// FormatChartFramework2Item 格式化框架信息
func FormatChartFramework2Item(origin *ChartFramework, nodes []*ChartFrameworkNodeItem) (item *ChartFrameworkItem) {
	if origin == nil {
		return
	}
	item = new(ChartFrameworkItem)
	item.ChartFrameworkId = origin.ChartFrameworkId
	item.FrameworkCode = origin.FrameworkCode
	item.FrameworkName = origin.FrameworkName
	item.FrameworkImg = origin.FrameworkImg
	item.FrameworkContent = origin.FrameworkContent
	item.IsPublic = origin.IsPublic
	item.PublicTime = utils.TimeTransferString(utils.FormatDateTime, origin.PublicTime)
	item.Sort = origin.Sort
	item.AdminId = origin.AdminId
	item.AdminName = origin.AdminName
	item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, origin.CreateTime)
	item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, origin.ModifyTime)
	item.Nodes = nodes
	return
}

// ChartFrameworkPublicMenuItem 公开框架目录
type ChartFrameworkPublicMenuItem struct {
	AdminId    int                   `description:"创建人ID"`
	MenuName   string                `description:"目录名称"`
	Frameworks []*ChartFrameworkItem `description:"框架列表"`
}
