edb_collect_classify.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package data_manage
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  6. "strings"
  7. "time"
  8. )
  9. // EdbCollectClassify 指标收藏分类
  10. type EdbCollectClassify struct {
  11. EdbCollectClassifyId int `gorm:"primaryKey;autoIncrement;column:edb_collect_classify_id;type:int(10) unsigned;not null"`
  12. ClassifyName string `gorm:"column:classify_name;type:varchar(255);not null;default:''"` // 分类名称
  13. ParentId int `gorm:"column:parent_id;type:int(10) unsigned;not null;default:0"` // 父级Id
  14. SysUserId int `gorm:"column:sys_user_id;type:int(10) unsigned;not null;default:0"` // 创建人Id
  15. SysUserRealName string `gorm:"column:sys_user_real_name;type:varchar(128);not null"` // 创建人姓名
  16. UniqueCode string `gorm:"unique;column:unique_code;type:varchar(64);not null;default:''"` // 唯一编码
  17. Level int `gorm:"column:level;type:int(10) unsigned;not null;default:0"` // 层级
  18. Sort int `gorm:"column:sort;type:int(10) unsigned;not null;default:0"` // 排序
  19. RootId int `gorm:"column:root_id;type:int(10) unsigned;not null;default:0"` // 顶级Id
  20. LevelPath string `gorm:"column:level_path;type:varchar(255);not null;default:''"` // 层级路径,逗号分隔
  21. CreateTime time.Time `gorm:"column:create_time;type:datetime"` // 创建时间
  22. ModifyTime time.Time `gorm:"column:modify_time;type:datetime"` // 修改时间
  23. }
  24. func (m *EdbCollectClassify) TableName() string {
  25. return "edb_collect_classify"
  26. }
  27. type EdbCollectClassifyCols struct {
  28. PrimaryId string
  29. ClassifyName string
  30. ParentId string
  31. SysUserId string
  32. SysUserRealName string
  33. UniqueCode string
  34. Level string
  35. Sort string
  36. RootId string
  37. LevelPath string
  38. CreateTime string
  39. ModifyTime string
  40. }
  41. func (m *EdbCollectClassify) Cols() EdbCollectClassifyCols {
  42. return EdbCollectClassifyCols{
  43. PrimaryId: "edb_collect_classify_id",
  44. ClassifyName: "classify_name",
  45. ParentId: "parent_id",
  46. SysUserId: "sys_user_id",
  47. SysUserRealName: "sys_user_real_name",
  48. UniqueCode: "unique_code",
  49. Level: "level",
  50. Sort: "sort",
  51. RootId: "root_id",
  52. LevelPath: "level_path",
  53. CreateTime: "create_time",
  54. ModifyTime: "modify_time",
  55. }
  56. }
  57. func (m *EdbCollectClassify) Create() (err error) {
  58. err = global.DmSQL["data"].Create(m).Error
  59. return
  60. }
  61. func (m *EdbCollectClassify) CreateMulti(items []*EdbCollectClassify) (err error) {
  62. if len(items) == 0 {
  63. return
  64. }
  65. err = global.DmSQL["data"].CreateInBatches(items, utils.MultiAddNum).Error
  66. return
  67. }
  68. func (m *EdbCollectClassify) Update(cols []string) (err error) {
  69. err = global.DmSQL["data"].Select(cols).Updates(m).Error
  70. return
  71. }
  72. func (m *EdbCollectClassify) Remove() (err error) {
  73. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  74. err = global.DmSQL["data"].Exec(sql, m.EdbCollectClassifyId).Error
  75. return
  76. }
  77. func (m *EdbCollectClassify) MultiRemove(ids []int) (err error) {
  78. if len(ids) == 0 {
  79. return
  80. }
  81. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  82. err = global.DmSQL["data"].Exec(sql, ids).Error
  83. return
  84. }
  85. func (m *EdbCollectClassify) RemoveByCondition(condition string, pars []interface{}) (err error) {
  86. if condition == "" {
  87. return
  88. }
  89. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  90. err = global.DmSQL["data"].Exec(sql, pars...).Error
  91. return
  92. }
  93. func (m *EdbCollectClassify) GetItemById(id int) (item *EdbCollectClassify, err error) {
  94. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  95. err = global.DmSQL["data"].Raw(sql, id).First(&item).Error
  96. return
  97. }
  98. func (m *EdbCollectClassify) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *EdbCollectClassify, err error) {
  99. order := ``
  100. if orderRule != "" {
  101. order = ` ORDER BY ` + orderRule
  102. }
  103. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  104. err = global.DmSQL["data"].Raw(sql, pars...).First(&item).Error
  105. return
  106. }
  107. func (m *EdbCollectClassify) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  108. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  109. err = global.DmSQL["data"].Raw(sql, pars...).Scan(&count).Error
  110. return
  111. }
  112. func (m *EdbCollectClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EdbCollectClassify, err error) {
  113. fields := strings.Join(fieldArr, ",")
  114. if len(fieldArr) == 0 {
  115. fields = `*`
  116. }
  117. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  118. if orderRule != "" {
  119. order = ` ORDER BY ` + orderRule
  120. }
  121. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  122. err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  123. return
  124. }
  125. func (m *EdbCollectClassify) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EdbCollectClassify, err error) {
  126. fields := strings.Join(fieldArr, ",")
  127. if len(fieldArr) == 0 {
  128. fields = `*`
  129. }
  130. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  131. if orderRule != "" {
  132. order = ` ORDER BY ` + orderRule
  133. }
  134. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  135. pars = append(pars, startSize, pageSize)
  136. err = global.DmSQL["data"].Raw(sql, pars...).Find(&items).Error
  137. return
  138. }
  139. func (m *EdbCollectClassify) GetSortMax(parentId int) (sort int, err error) {
  140. sql := fmt.Sprintf(`SELECT COALESCE(MAX(%s), 0) FROM %s WHERE %s = ?`, m.Cols().Sort, m.TableName(), m.Cols().ParentId)
  141. err = global.DmSQL["data"].Raw(sql, parentId).Scan(&sort).Error
  142. return
  143. }
  144. // EdbCollectClassifyItem 指标收藏分类
  145. type EdbCollectClassifyItem struct {
  146. ClassifyId int `description:"收藏ID"`
  147. ClassifyName string `description:"指标收藏分类ID"`
  148. ParentId int `description:"指标ID"`
  149. Level int `description:"指标ID"`
  150. Sort int `description:"指标ID"`
  151. LevelPath string `description:"层级路径"`
  152. UniqueCode string `description:"唯一编码"`
  153. Children []*EdbCollectClassifyItem `description:"子分类"`
  154. }
  155. func (m *EdbCollectClassify) Format2Item() (item *EdbCollectClassifyItem) {
  156. item = new(EdbCollectClassifyItem)
  157. item.ClassifyId = m.EdbCollectClassifyId
  158. item.ClassifyName = m.ClassifyName
  159. item.ParentId = m.ParentId
  160. item.Level = m.Level
  161. item.Sort = m.Sort
  162. item.LevelPath = m.LevelPath
  163. item.UniqueCode = m.UniqueCode
  164. //item.Children = make([]*EdbCollectClassifyItem, 0)
  165. return
  166. }
  167. // EdbCollectClassifyListItem 指标收藏分类列表
  168. type EdbCollectClassifyListItem struct {
  169. NodeType int `description:"类型: 1-分类; 2-指标"`
  170. ClassifyId int `description:"分类ID"`
  171. ClassifyName string `description:"分类名称"`
  172. EdbInfoId int `description:"指标ID"`
  173. EdbCode string `description:"指标编码"`
  174. EdbName string `description:"指标名称"`
  175. ParentId int `description:"父级ID"`
  176. Level int `description:"层级"`
  177. Sort int `description:"排序"`
  178. UniqueCode string `description:"唯一编码, 指标的话用indexCode"`
  179. Children []*EdbCollectClassifyListItem `description:"子分类"`
  180. }
  181. // EdbCollectClassifyAddReq 新增分类
  182. type EdbCollectClassifyAddReq struct {
  183. ClassifyName string `description:"分类名称"`
  184. ParentId int `description:"父级ID"`
  185. Level int `description:"层级"`
  186. }
  187. // EdbCollectClassifyEditReq 编辑分类
  188. type EdbCollectClassifyEditReq struct {
  189. ClassifyId int `description:"分类ID"`
  190. ClassifyName string `description:"分类名称"`
  191. }
  192. // EdbCollectClassifyRemoveReq 删除分类
  193. type EdbCollectClassifyRemoveReq struct {
  194. ClassifyId int `description:"分类ID"`
  195. }
  196. // RemoveClassifyAndCollect 移除分类及收藏
  197. func (m *EdbCollectClassify) RemoveClassifyAndCollect(classifyIds []int) (err error) {
  198. if len(classifyIds) == 0 {
  199. return
  200. }
  201. tx := global.DmSQL["data"].Begin()
  202. defer func() {
  203. if err != nil {
  204. _ = tx.Rollback()
  205. return
  206. }
  207. _ = tx.Commit()
  208. }()
  209. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(classifyIds)))
  210. e := tx.Exec(sql, classifyIds).Error
  211. if e != nil {
  212. err = fmt.Errorf("remove classify err: %v", e)
  213. return
  214. }
  215. collectOb := new(EdbCollect)
  216. sql = fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, collectOb.TableName(), collectOb.Cols().EdbCollectClassifyId, utils.GetOrmInReplace(len(classifyIds)))
  217. e = tx.Exec(sql, classifyIds).Error
  218. if e != nil {
  219. err = fmt.Errorf("remove collect err: %v", e)
  220. return
  221. }
  222. return
  223. }
  224. // EdbCollectClassifyMoveReq 移动分类
  225. type EdbCollectClassifyMoveReq struct {
  226. ClassifyId int `description:"分类ID"`
  227. PrevClassifyId int `description:"上一个兄弟节点分类ID"`
  228. NextClassifyId int `description:"下一个兄弟节点分类ID"`
  229. }
  230. // GetEdbCollectClassifyById 获取分类
  231. func GetEdbCollectClassifyById(adminId, classifyId int) (item *EdbCollectClassify, err error) {
  232. sql := `SELECT * FROM edb_collect_classify WHERE sys_user_id = ? AND edb_collect_classify_id = ? `
  233. err = global.DmSQL["data"].Raw(sql, adminId, classifyId).First(&item).Error
  234. return
  235. }
  236. // GetFirstEdbCollectClassifyByAdminId 获取当前账号下,排序第一条的分类数据
  237. func GetFirstEdbCollectClassifyByAdminId(adminId int) (item *EdbCollectClassify, err error) {
  238. sql := `SELECT * FROM edb_collect_classify WHERE sys_user_id = ? ORDER BY sort ASC, edb_collect_classify_id ASC LIMIT 1`
  239. err = global.DmSQL["data"].Raw(sql, adminId).First(&item).Error
  240. return
  241. }
  242. // UpdateEdbCollectClassifySortByClassifyId 更新排序
  243. func UpdateEdbCollectClassifySortByClassifyId(adminId, classifyId, nowSort int, updateSort string) (err error) {
  244. sql := fmt.Sprintf(`UPDATE edb_collect_classify SET sort = %d WHERE sys_user_id = ? and sort > ? `, updateSort)
  245. if classifyId > 0 {
  246. sql += ` OR ( edb_collect_classify_id > ` + fmt.Sprint(classifyId) + ` AND sort = ` + fmt.Sprint(nowSort) + `)`
  247. }
  248. err = global.DmSQL["data"].Exec(sql, adminId, nowSort).Error
  249. return
  250. }