package models

import (
	"eta/eta_index_lib/utils"
	"github.com/beego/beego/v2/client/orm"
	"strconv"
	"time"
)

type BaseFromMysteelChemicalClassify struct {
	BaseFromMysteelChemicalClassifyId int       `orm:"column(base_from_mysteel_chemical_classify_id);pk"`
	ClassifyName                      string    `description:"分类名称"`
	ParentId                          int       `description:"父级id"`
	SysUserId                         int       `description:"创建人id"`
	SysUserRealName                   string    `description:"创建人姓名"`
	Level                             int       `description:"层级"`
	Sort                              int       `description:"排序字段，越小越靠前，默认值：10"`
	ModifyTime                        time.Time `description:"修改时间"`
	CreateTime                        time.Time `description:"创建时间"`
}

// 判断分类名称是否存在
func GetMysteelClassify(classifyName string, parentId int) (item *BaseFromMysteelChemicalClassify, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM base_from_mysteel_chemical_classify WHERE parent_id=? AND classify_name=? `
	err = o.Raw(sql, parentId, classifyName).QueryRow(&item)
	return
}

// GetEdbClassifyMaxSort 获取分类下最大的排序数
func GetMysteelClassifyMaxSort(parentId int) (sort int, err error) {
	o := orm.NewOrm()
	sql := `SELECT Max(sort) AS sort FROM base_from_mysteel_chemical_classify WHERE parent_id=? `
	err = o.Raw(sql, parentId).QueryRow(&sort)
	return
}

func AddMysteelClassify(item *BaseFromMysteelChemicalClassify) (lastId int64, err error) {
	o := orm.NewOrm()
	lastId, err = o.Insert(item)
	return
}

type AddMysteelClassifyReq struct {
	ClassifyName    string `description:"分类名称"`
	ParentId        int    `description:"父级id,第一级传0"`
	Level           int    `description:"层级,第一级传0，其余传上一级的层级"`
	SysUserId       int    `description:"操作人id"`
	SysUserRealName string `description:"操作人真实名称"`
}

// AddEdbClassify 添加指标分类
func SaveMysteelClassify(classifyName string, parentId, level int, sysUserId, sysUserName string) (classifyInfo *BaseFromMysteelChemicalClassify, err error, errMsg string) {
	edbClassify, err := GetMysteelClassify(classifyName, parentId)

	var isAdd bool
	if err != nil {
		if err.Error() == utils.ErrNoRow() {
			isAdd = true
		} else {
			errMsg = `判断名称是否已存在失败`
			return
		}
	}

	if edbClassify != nil && edbClassify.BaseFromMysteelChemicalClassifyId > 0 {
		classifyInfo = edbClassify
		return edbClassify, nil, ""
	} else {
		isAdd = true
	}
	if isAdd {
		sysUserIdInt, _ := strconv.Atoi(sysUserId)
		//获取该层级下最大的排序数
		maxSort, err := GetMysteelClassifyMaxSort(parentId)
		classifyInfo = &BaseFromMysteelChemicalClassify{
			ClassifyName:    classifyName,
			ParentId:        parentId,
			CreateTime:      time.Now(),
			ModifyTime:      time.Now(),
			SysUserId:       sysUserIdInt,
			SysUserRealName: sysUserName,
			Level:           level + 1,
			Sort:            maxSort,
		}
		classifyId, err := AddMysteelClassify(classifyInfo)
		if err != nil {
			errMsg = "保存分类失败"
		}
		classifyInfo.BaseFromMysteelChemicalClassifyId = int(classifyId)
	}
	return
}
