package models import ( "errors" "eta_gn/eta_index_lib/global" "eta_gn/eta_index_lib/utils" "strconv" "time" ) type EdbClassify struct { ClassifyId int `gorm:"primaryKey;autoIncrement;column:classify_id"` ClassifyType uint8 `gorm:"column:classify_type" description:"分类类型,0:普通指标分类,1:预测指标分类"` ClassifyName string `gorm:"column:classify_name" description:"分类名称"` ParentId int `gorm:"column:parent_id" description:"父级id"` RootId int `gorm:"column:root_id" description:"顶级id"` HasData int `gorm:"column:has_data" description:"是否含有指标数据"` CreateTime time.Time `gorm:"column:create_time" description:"创建时间"` ModifyTime time.Time `gorm:"column:modify_time" description:"修改时间"` SysUserId int `gorm:"column:sys_user_id" description:"创建人id"` SysUserRealName string `gorm:"column:sys_user_real_name" description:"创建人姓名"` Level int `gorm:"column:level" description:"层级"` UniqueCode string `gorm:"column:unique_code" description:"唯一编码"` Sort int `gorm:"column:sort" description:"排序字段,越小越靠前,默认值:10"` } func AddEdbClassify(item *EdbClassify) (lastId int64, err error) { err = global.DEFAULT_DmSQL.Create(item).Error lastId = int64(item.ClassifyId) return } // Update 更新分类基础信息 func (edbClassify *EdbClassify) Update(cols []string) (err error) { err = global.DEFAULT_DmSQL.Model(edbClassify).Select(cols).Updates(edbClassify).Error return } func GetEdbClassifyById(classifyId int) (item *EdbClassify, err error) { sql := `SELECT * FROM edb_classify WHERE classify_id=? ` err = global.DEFAULT_DmSQL.Raw(sql, classifyId).First(&item).Error return } type EdbClassifySimplify struct { ClassifyId int `gorm:"primaryKey;autoIncrement;column:classify_id" description:"分类id"` ClassifyName string `description:"分类名称"` ParentId int } // 判断分类名称是否存在 func GetEdbClassifyCount(classifyName string, parentId int, classifyType uint8) (item *EdbClassify, err error) { sql := `SELECT * FROM edb_classify WHERE parent_id=? AND classify_name=? AND classify_type = ? ` err = global.DEFAULT_DmSQL.Raw(sql, parentId, classifyName, classifyType).First(&item).Error return } // GetEdbClassifyMaxSort 获取分类下最大的排序数 func GetEdbClassifyMaxSort(parentId int, classifyType uint8) (sort int, err error) { sql := `SELECT COALESCE(MAX(sort),0) AS sort FROM edb_classify WHERE parent_id=? AND classify_type=? ` err = global.DEFAULT_DmSQL.Raw(sql, parentId, classifyType).First(&sort).Error return } type AddEdbClassifyReq struct { ClassifyName string `description:"分类名称"` ParentId int `description:"父级id,第一级传0"` Level int `description:"层级,第一级传0,其余传上一级的层级"` ClassifyType uint8 `description:"分类类型,0:普通指标分类,1:预测指标分类"` } // AddEdbClassify 添加指标分类 func SaveEdbClassify(classifyName string, parentId, level int, classifyType uint8, sysUserId, sysUserName string) (classifyInfo *EdbClassify, err error, errMsg string) { edbClassify, err := GetEdbClassifyCount(classifyName, parentId, classifyType) var isAdd bool if err != nil { if err.Error() == utils.ErrNoRow() { isAdd = true } else { errMsg = `判断名称是否已存在失败` return } } if edbClassify != nil && edbClassify.ClassifyId > 0 { classifyInfo = edbClassify return edbClassify, nil, "" } else { isAdd = true } if isAdd { sysUserIdInt, _ := strconv.Atoi(sysUserId) rootId := 0 if parentId > 0 { parentClassify, tErr := GetEdbClassifyById(parentId) if tErr != nil { if tErr.Error() == utils.ErrNoRow() { errMsg = "父级分类不存在" err = errors.New(errMsg) return } errMsg = "获取失败" err = errors.New("获取分类信息失败,Err:" + tErr.Error()) return } rootId = parentClassify.RootId } timestamp := strconv.FormatInt(time.Now().UnixNano(), 10) classifyInfo = &EdbClassify{ //ClassifyId: 0, ClassifyType: classifyType, ClassifyName: classifyName, ParentId: parentId, RootId: rootId, HasData: 0, CreateTime: time.Now(), ModifyTime: time.Now(), SysUserId: sysUserIdInt, SysUserRealName: sysUserName, Level: level + 1, UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp), Sort: GetAddEdbMaxSortByClassifyId(parentId, classifyType), } var classifyId int64 classifyId, err = AddEdbClassify(classifyInfo) if err != nil { errMsg = "保存分类失败" return } classifyInfo.ClassifyId = int(classifyId) if parentId == 0 { //一级目录的rootId等于自己本身 classifyInfo.RootId = int(classifyId) err = classifyInfo.Update([]string{"RootId"}) if err != nil { errMsg = "更新分类失败" return } } } return }