edb_collect_classify.go 10 KB

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