package data_manage import ( "eta_gn/eta_api/global" "eta_gn/eta_api/utils" "fmt" "strings" "time" ) // ChartCollectClassify 图表收藏分类 type ChartCollectClassify struct { ChartCollectClassifyId int `gorm:"primaryKey;autoIncrement;column:chart_collect_classify_id;type:int(10) unsigned;not null"` ClassifyName string `gorm:"column:classify_name;type:varchar(255);not null;default:''"` // 分类名称 ParentId int `gorm:"column:parent_id;type:int(10) unsigned;not null;default:0"` // 父级Id SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人Id SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(128);not null"` // 创建人姓名 UniqueCode string `gorm:"unique;column:unique_code;type:varchar(64);not null;default:''"` // 唯一编码 Level int `gorm:"column:level;type:int(10) unsigned;not null;default:0"` // 层级 Sort int `gorm:"column:sort;type:int(10) unsigned;not null;default:0"` // 排序 RootId int `gorm:"column:root_id;type:int(10) unsigned;not null;default:0"` // 顶级Id LevelPath string `gorm:"column:level_path;type:varchar(255);not null;default:''"` // 层级路径,逗号分隔 CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间 ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 修改时间 } func (m *ChartCollectClassify) TableName() string { return "chart_collect_classify" } type ChartCollectClassifyCols struct { PrimaryId string ClassifyName string ParentId string SysUserId string SysUserRealName string UniqueCode string Level string Sort string RootId string LevelPath string CreateTime string ModifyTime string } func (m *ChartCollectClassify) Cols() ChartCollectClassifyCols { return ChartCollectClassifyCols{ PrimaryId: "chart_collect_classify_id", ClassifyName: "classify_name", ParentId: "parent_id", SysUserId: "sys_user_id", SysUserRealName: "sys_user_real_name", UniqueCode: "unique_code", Level: "level", Sort: "sort", RootId: "root_id", LevelPath: "level_path", CreateTime: "create_time", ModifyTime: "modify_time", } } func (m *ChartCollectClassify) Create() (err error) { err = global.DmSQL["data"].Create(m).Error return } func (m *ChartCollectClassify) CreateMulti(items []*ChartCollectClassify) (err error) { if len(items) == 0 { return } err = global.DmSQL["data"].CreateInBatches(items, utils.MultiAddNum).Error return } func (m *ChartCollectClassify) Update(cols []string) (err error) { err = global.DmSQL["data"].Select(cols).Updates(m).Error return } func (m *ChartCollectClassify) Remove() (err error) { sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId) err = global.DmSQL["data"].Exec(sql, m.ChartCollectClassifyId).Error return } func (m *ChartCollectClassify) MultiRemove(ids []int) (err error) { if len(ids) == 0 { return } sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids))) err = global.DmSQL["data"].Exec(sql, ids).Error return } func (m *ChartCollectClassify) RemoveByCondition(condition string, pars []interface{}) (err error) { if condition == "" { return } sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition) err = global.DmSQL["data"].Exec(sql, pars...).Error return } func (m *ChartCollectClassify) GetItemById(id int) (item *ChartCollectClassify, err error) { sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId) err = global.DmSQL["data"].Raw(sql, id).First(&item).Error return } func (m *ChartCollectClassify) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *ChartCollectClassify, err error) { order := `` if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order) err = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error return } func (m *ChartCollectClassify) GetCountByCondition(condition string, pars []interface{}) (count int, err error) { sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition) err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error return } func (m *ChartCollectClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*ChartCollectClassify, err error) { fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime) if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order) err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error return } func (m *ChartCollectClassify) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*ChartCollectClassify, err error) { fields := strings.Join(fieldArr, ",") if len(fieldArr) == 0 { fields = `*` } order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime) if orderRule != "" { order = ` ORDER BY ` + orderRule } sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order) pars = append(pars, startSize, pageSize) err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error return } func (m *ChartCollectClassify) GetSortMax(parentId int) (sort int, err error) { sql := fmt.Sprintf(`SELECT COALESCE(MAX(%s), 0) FROM %s WHERE %s = ?`, m.Cols().Sort, m.TableName(), m.Cols().ParentId) err = global.DmSQL["data"].Raw(sql, parentId).Scan(&sort).Error return } // ChartCollectClassifyItem 图表收藏分类 type ChartCollectClassifyItem struct { ClassifyId int `description:"收藏ID"` ClassifyName string `description:"图表收藏分类ID"` ParentId int `description:"图表ID"` Level int `description:"图表ID"` Sort int `description:"图表ID"` LevelPath string `description:"层级路径"` UniqueCode string `description:"唯一编码"` Children []*ChartCollectClassifyItem `description:"子分类"` } func (m *ChartCollectClassify) Format2Item() (item *ChartCollectClassifyItem) { item = new(ChartCollectClassifyItem) item.ClassifyId = m.ChartCollectClassifyId item.ClassifyName = m.ClassifyName item.ParentId = m.ParentId item.Level = m.Level item.Sort = m.Sort item.LevelPath = m.LevelPath item.UniqueCode = m.UniqueCode //item.Children = make([]*ChartCollectClassifyItem, 0) return } // ChartCollectClassifyListItem 图表收藏分类列表 type ChartCollectClassifyListItem struct { NodeType int `description:"类型: 1-分类; 2-图表"` ClassifyId int `description:"分类ID"` ClassifyName string `description:"分类名称"` ChartInfoId int `description:"图表ID"` ChartCode string `description:"图表编码"` ChartName string `description:"图表名称"` ParentId int `description:"父级ID"` Level int `description:"层级"` Sort int `description:"排序"` UniqueCode string `description:"唯一编码, 图表的话用indexCode"` Children []*ChartCollectClassifyListItem `description:"子分类"` } // ChartCollectClassifyAddReq 新增分类 type ChartCollectClassifyAddReq struct { ClassifyName string `description:"分类名称"` ParentId int `description:"父级ID"` Level int `description:"层级"` } // ChartCollectClassifyEditReq 编辑分类 type ChartCollectClassifyEditReq struct { ClassifyId int `description:"分类ID"` ClassifyName string `description:"分类名称"` } // ChartCollectClassifyRemoveReq 删除分类 type ChartCollectClassifyRemoveReq struct { ClassifyId int `description:"分类ID"` } // RemoveClassifyAndCollect 移除分类及收藏 func (m *ChartCollectClassify) RemoveClassifyAndCollect(classifyIds []int) (err error) { if len(classifyIds) == 0 { return } tx := global.DmSQL["data"].Begin() defer func() { if err != nil { _ = tx.Rollback() return } _ = tx.Commit() }() sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(classifyIds))) e := tx.Exec(sql, classifyIds).Error if e != nil { err = fmt.Errorf("remove classify err: %v", e) return } collectOb := new(ChartCollect) sql = fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, collectOb.TableName(), collectOb.Cols().ChartCollectClassifyId, utils.GetOrmInReplace(len(classifyIds))) e = tx.Exec(sql, classifyIds).Error if e != nil { err = fmt.Errorf("remove collect err: %v", e) return } return } // ChartCollectClassifyMoveReq 移动分类 type ChartCollectClassifyMoveReq struct { ClassifyId int `description:"分类ID"` PrevClassifyId int `description:"上一个兄弟节点分类ID"` NextClassifyId int `description:"下一个兄弟节点分类ID"` } // GetChartCollectClassifyById 获取分类 func GetChartCollectClassifyById(adminId, classifyId int) (item *ChartCollectClassify, err error) { sql := `SELECT * FROM chart_collect_classify WHERE sys_user_id = ? AND chart_collect_classify_id = ? ` err = global.DmSQL["data"].Raw(sql, adminId, classifyId).First(&item).Error return } // GetFirstChartCollectClassifyByAdminId 获取当前账号下,排序第一条的分类数据 func GetFirstChartCollectClassifyByAdminId(adminId int) (item *ChartCollectClassify, err error) { sql := `SELECT * FROM chart_collect_classify WHERE sys_user_id = ? ORDER BY sort ASC, chart_collect_classify_id ASC LIMIT 1` err = global.DmSQL["data"].Raw(sql, adminId).First(&item).Error return } // UpdateChartCollectClassifySortByClassifyId 更新排序 func UpdateChartCollectClassifySortByClassifyId(adminId, classifyId, nowSort int, updateSort string) (err error) { sql := fmt.Sprintf(`UPDATE chart_collect_classify SET sort = %d WHERE sys_user_id = ? and sort > ? `, updateSort) if classifyId > 0 { sql += ` OR ( chart_collect_classify_id > ` + fmt.Sprint(classifyId) + ` AND sort = ` + fmt.Sprint(nowSort) + `)` } err = global.DmSQL["data"].Exec(sql, adminId, nowSort).Error return }