package future_good

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

// FutureGoodChartClassify future_good_chart表格分类
type FutureGoodChartClassify struct {
	FutureGoodChartClassifyId   int       `orm:"column(future_good_chart_classify_id);pk"`
	FutureGoodChartClassifyName string    `description:"分类名称"`
	ParentId                    int       `description:"父级id"`
	SysUserId                   int       `description:"创建人id"`
	SysUserRealName             string    `description:"创建人姓名"`
	Level                       int       `description:"层级"`
	UniqueCode                  string    `description:"唯一编码"`
	Sort                        int       `description:"排序字段，越小越靠前，默认值：10"`
	IsDelete                    int       `description:"排序字段，越小越靠前，默认值：10"`
	CreateTime                  time.Time `description:"创建时间"`
	ModifyTime                  time.Time `description:"修改时间"`
}

// AddFutureGoodChartClassify 添加future_good_chart分类
func AddFutureGoodChartClassify(item *FutureGoodChartClassify) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("data")
	lastId, err = o.Insert(item)
	return
}

// GetFutureGoodChartClassifyCount 获取同级分类下存在同名分类的数量
func GetFutureGoodChartClassifyCount(FutureGoodChartClassifyName string, parentId int) (count int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT COUNT(1) AS count FROM future_good_chart_classify WHERE parent_id=? AND future_good_chart_classify_name=? AND is_delete=0 `
	err = o.Raw(sql, parentId, FutureGoodChartClassifyName).QueryRow(&count)
	return
}

func GetFutureGoodChartClassifyById(classifyId int) (item *FutureGoodChartClassify, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM future_good_chart_classify WHERE future_good_chart_classify_id=? AND is_delete=0 `
	err = o.Raw(sql, classifyId).QueryRow(&item)
	return
}

func GetFutureGoodChartClassifyByParentId(parentId int) (items []*FutureGoodChartClassifyItems, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM future_good_chart_classify WHERE parent_id=? AND is_delete=0 order by sort asc,future_good_chart_classify_id asc`
	_, err = o.Raw(sql, parentId).QueryRows(&items)
	return
}

func GetFutureGoodChartClassifyAll() (items []*FutureGoodChartClassifyItems, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM future_good_chart_classify WHERE parent_id<>0 AND is_delete=0 order by sort asc,future_good_chart_classify_id asc`
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

type FutureGoodChartClassifyItems struct {
	FutureGoodChartClassifyId   int `description:"分类id"`
	FutureGoodChartInfoId       int `description:"表格id"`
	FutureGoodChartClassifyName string
	ParentId                    int
	Level                       int    `description:"层级"`
	Sort                        int    `description:"排序字段，越小越靠前，默认值：10"`
	UniqueCode                  string `description:"唯一编码"`
	SysUserId                   int    `description:"创建人id"`
	SysUserRealName             string `description:"创建人姓名"`
	StartDate                   string `description:"自定义开始日期"`
	Children                    []*FutureGoodChartClassifyItems
}

func GetFutureGoodChartClassifyByCondition(condition string, pars []interface{}) (item *FutureGoodChartClassify, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM future_good_chart_classify WHERE 1=1 AND is_delete=0 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&item)
	return
}

// GetNextFutureGoodChartClassifyByCondition 获取下一个分类
func GetNextFutureGoodChartClassifyByCondition(condition string, pars []interface{}) (item *FutureGoodChartClassify, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM future_good_chart_classify WHERE 1=1 AND is_delete=0 `
	if condition != "" {
		sql += condition
	}
	sql += " ORDER BY sort asc , create_time ASC LIMIT 1 "
	err = o.Raw(sql, pars).QueryRow(&item)
	return
}

// GetFirstFutureGoodChartClassifyByParentId 获取当前父级图表分类下的排序第一条的数据
func GetFirstFutureGoodChartClassifyByParentId(parentId int) (item *FutureGoodChartClassify, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT * FROM future_good_chart_classify WHERE parent_id=? AND is_delete=0 order by sort asc,future_good_chart_classify_id asc limit 1`
	err = o.Raw(sql, parentId).QueryRow(&item)
	return
}

// UpdateFutureGoodChartClassifySortByParentId 根据图表父类id更新排序
func UpdateFutureGoodChartClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` update future_good_chart_classify set sort = ` + updateSort + ` WHERE parent_id=? and sort > ? AND is_delete=0 `
	if classifyId > 0 {
		sql += ` or ( future_good_chart_classify_id > ` + fmt.Sprint(classifyId) + ` and sort= ` + fmt.Sprint(nowSort) + `)`
	}
	_, err = o.Raw(sql, parentId, nowSort).Exec()
	return
}

// Update 更新图表分类基础信息
func (FutureGoodChartClassify *FutureGoodChartClassify) Update(cols []string) (err error) {
	o := orm.NewOrmUsingDB("data")
	_, err = o.Update(FutureGoodChartClassify, cols...)
	return
}

// GetFutureGoodChartClassifyMaxSort 获取图表分类下最大的排序数
func GetFutureGoodChartClassifyMaxSort(parentId int) (sort int, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT Max(sort) AS sort FROM future_good_chart_classify WHERE parent_id=? AND is_delete=0 `
	err = o.Raw(sql, parentId).QueryRow(&sort)
	return
}

type FutureGoodChartClassifyView struct {
	FutureGoodChartClassifyId   int    `orm:"column(future_good_chart_classify_id);pk"`
	FutureGoodChartClassifyName string `description:"分类名称"`
	ParentId                    int    `description:"父级id"`
}

func GetFutureGoodChartClassifyViewById(classifyId int) (item *FutureGoodChartClassifyView, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := `SELECT * FROM future_good_chart_classify WHERE future_good_chart_classify_id=? AND is_delete=0 `
	err = o.Raw(sql, classifyId).QueryRow(&item)
	return
}

//用于分类展示
func GetChartInfoAll() (items []*FutureGoodChartClassifyItems, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT chart_info_id,chart_classify_id,chart_name AS chart_classify_name,
             unique_code,sys_user_id,sys_user_real_name,date_type,start_date,end_date,chart_type,calendar,season_start_date,season_end_date
            FROM chart_info ORDER BY sort asc,create_time ASC `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// GetNoContentFutureGoodInfoAll 获取不含content的表格列表 用于分类展示
func GetNoContentFutureGoodInfoAll() (items []FutureGoodChartClassifyItems, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT excel_info_id,excel_classify_id,excel_name AS excel_classify_name,
             unique_code,sys_user_id,sys_user_real_name
            FROM excel_info where is_delete=0 ORDER BY sort asc,create_time desc `
	_, err = o.Raw(sql).QueryRows(&items)
	return
}

// GetFutureGoodChartInfoCountByClassifyId 根据分类id获取名下表格数量
func GetFutureGoodChartInfoCountByClassifyId(classifyId int) (total int64, err error) {
	o := orm.NewOrmUsingDB("data")
	sql := ` SELECT count(1) total FROM excel_info WHERE excel_classify_id = ? AND is_delete=0 `
	err = o.Raw(sql, classifyId).QueryRow(&total)
	return
}
