base_from_baiinfo_classify.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "eta/eta_api/utils"
  6. "time"
  7. )
  8. // BaseFromBaiinfoClassify SMM原始数据分类表
  9. type BaseFromBaiinfoClassify struct {
  10. ClassifyId int `orm:"column(classify_id);pk"`
  11. ClassifyName string `description:"分类名称"`
  12. ParentId int `description:"父级id"`
  13. SysUserId int `description:"创建人id"`
  14. SysUserRealName string `description:"创建人姓名"`
  15. Level int `description:"层级"`
  16. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  17. ModifyTime time.Time `description:"修改时间"`
  18. CreateTime time.Time `description:"创建时间"`
  19. }
  20. // AddBaseFromBaiinfoClassify 添加SMM原始数据分类
  21. func AddBaseFromBaiinfoClassify(item *BaseFromBaiinfoClassify) (lastId int64, err error) {
  22. o := orm.NewOrmUsingDB("data")
  23. lastId, err = o.Insert(item)
  24. return
  25. }
  26. // GetBaseFromBaiinfoClassifyCount 获取分类名称的个数
  27. func GetBaseFromBaiinfoClassifyCount(classifyName string, parentId int) (count int, err error) {
  28. o := orm.NewOrmUsingDB("data")
  29. sql := `SELECT COUNT(1) AS count FROM base_from_baiinfo_classify WHERE classify_name=? AND parent_id=? `
  30. err = o.Raw(sql, classifyName, parentId).QueryRow(&count)
  31. return
  32. }
  33. // GetBaseFromBaiinfoClassifyById 通过分类id的获取分类信息
  34. func GetBaseFromBaiinfoClassifyById(classifyId int) (item *BaseFromBaiinfoClassify, err error) {
  35. o := orm.NewOrmUsingDB("data")
  36. sql := `SELECT * FROM base_from_baiinfo_classify WHERE classify_id=? `
  37. err = o.Raw(sql, classifyId).QueryRow(&item)
  38. return
  39. }
  40. // EditBaseFromBaiinfoClassify 修改SMM原始数据分类
  41. func EditBaseFromBaiinfoClassify(classifyId int, classifyName string) (err error) {
  42. o := orm.NewOrmUsingDB("data")
  43. sql := `UPDATE base_from_baiinfo_classify SET classify_name=?,modify_time=NOW() WHERE classify_id=? `
  44. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  45. return
  46. }
  47. // UpdateBaseFromBaiinfoClassifySort 修改SMM原始数据分类的排序
  48. func UpdateBaseFromBaiinfoClassifySort(classifyId int) (err error) {
  49. o := orm.NewOrmUsingDB("data")
  50. sql := `UPDATE base_from_baiinfo_classify SET sort=classify_id, modify_time=NOW() WHERE classify_id=? `
  51. _, err = o.Raw(sql, classifyId).Exec()
  52. return
  53. }
  54. type BaseFromBaiinfoClassifyItems struct {
  55. ClassifyId int `description:"分类ID"`
  56. BaseFromBaiinfoIndexId int `description:"指标类型ID"`
  57. BaseFromBaiinfoIndexCode string `description:"指标唯一编码"`
  58. ClassifyName string `description:"分类名称"`
  59. ParentId int `description:"父级id"`
  60. Level int `description:"层级"`
  61. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  62. //UniqueCode string `description:"唯一编码"`
  63. //ModifyTime time.Time `description:"修改时间"`
  64. //CreateTime time.Time `description:"创建时间"`
  65. Children []*BaseFromBaiinfoClassifyItems `description:"下级"`
  66. }
  67. type BaseFromBaiinfoClassifyNameItems struct {
  68. ClassifyId int `description:"分类ID"`
  69. ClassifyName string `description:"分类名称"`
  70. ParentId int `description:"父级id"`
  71. Children []*BaseFromBaiinfoClassifyNameItems `description:"下级"`
  72. }
  73. type BaseFromBaiinfoClassifyResp struct {
  74. List []*BaseFromBaiinfoClassifyItems
  75. }
  76. type BaseFromBaiinfoClassifyNameResp struct {
  77. List []*BaseFromBaiinfoClassifyNameItems
  78. }
  79. type BaseFromBaiinfoClassifyItemsButton struct {
  80. AddButton bool `description:"是否可添加"`
  81. OpButton bool `description:"是否可编辑"`
  82. DeleteButton bool `description:"是否可删除"`
  83. MoveButton bool `description:"是否可移动"`
  84. }
  85. // GetBaseFromBaiinfoClassifyByParentId 根据上级id获取当下的分类列表数据
  86. func GetBaseFromBaiinfoClassifyByParentId(parentId int) (items []*BaseFromBaiinfoClassifyItems, err error) {
  87. o := orm.NewOrmUsingDB("data")
  88. sql := ` SELECT * FROM base_from_baiinfo_classify WHERE parent_id=? order by sort asc,classify_id asc`
  89. _, err = o.Raw(sql, parentId).QueryRows(&items)
  90. return
  91. }
  92. // GetAllBaseFromBaiinfoClassify 获取所有的分类列表数据
  93. func GetAllBaseFromBaiinfoClassify() (items []*BaseFromBaiinfoClassifyItems, err error) {
  94. o := orm.NewOrmUsingDB("data")
  95. sql := ` SELECT * FROM base_from_baiinfo_classify WHERE parent_id <> 0 order by sort asc,classify_id asc`
  96. _, err = o.Raw(sql).QueryRows(&items)
  97. return
  98. }
  99. type DeleteBaseFromBaiinfoClassifyReq struct {
  100. ClassifyId int `description:"分类id"`
  101. EdbInfoId int `description:"指标id"`
  102. }
  103. type BaseFromBaiinfoClassifyListResp struct {
  104. AllNodes []*BaseFromBaiinfoClassifyItems
  105. CanOpClassify bool `description:"是否允许操作分类"`
  106. }
  107. type BaseFromBaiinfoClassifySimplify struct {
  108. ClassifyId int `description:"分类id"`
  109. ClassifyName string `description:"分类名称"`
  110. ParentId int
  111. }
  112. // GetFirstBaseFromBaiinfoClassify 获取当前分类下,且排序数相同 的排序第一条的数据
  113. func GetFirstBaseFromBaiinfoClassify() (item *BaseFromBaiinfoClassify, err error) {
  114. o := orm.NewOrmUsingDB("data")
  115. sql := ` SELECT * FROM base_from_baiinfo_classify order by sort asc,classify_id asc limit 1`
  116. err = o.Raw(sql).QueryRow(&item)
  117. return
  118. }
  119. // UpdateBaseFromBaiinfoClassifySortByClassifyId 根据分类id更新排序
  120. func UpdateBaseFromBaiinfoClassifySortByClassifyId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  121. o := orm.NewOrmUsingDB("data")
  122. sql := ` update base_from_baiinfo_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? `
  123. if classifyId > 0 {
  124. sql += ` or ( classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  125. }
  126. _, err = o.Raw(sql, parentId, nowSort).Exec()
  127. return
  128. }
  129. // MoveUpBaiinfoIndexClassifyBySort 往上移动
  130. func MoveUpBaiinfoIndexClassifyBySort(parentId, nextSort, currentSort int) (err error) {
  131. o := orm.NewOrmUsingDB("data")
  132. sql := `update base_from_baiinfo_classify set sort = sort + 1 where parent_id=? and sort >= ? and sort< ?`
  133. _, err = o.Raw(sql, parentId, nextSort, currentSort).Exec()
  134. return
  135. }
  136. // MoveDownBaiinfoIndexClassifyBySort 往下移动
  137. func MoveDownBaiinfoIndexClassifyBySort(parentId, prevSort, currentSort int) (err error) {
  138. o := orm.NewOrmUsingDB("data")
  139. sql := `update base_from_baiinfo_classify set sort = sort - 1 where parent_id=? and sort <= ? and sort> ? `
  140. _, err = o.Raw(sql, parentId, prevSort, currentSort).Exec()
  141. return
  142. }
  143. // GetBaiinfoIndexClassifyMinSort 获取最小不等于0的排序
  144. func GetBaiinfoIndexClassifyMinSort(parentId int) (sort int, err error) {
  145. o := orm.NewOrmUsingDB("data")
  146. sql := `select min(sort) from base_from_baiinfo_classify where parent_id=? and sort <> 0 `
  147. err = o.Raw(sql, parentId).QueryRow(&sort)
  148. return
  149. }
  150. // Update 更新分类基础信息
  151. func (BaseFromBaiinfoClassify *BaseFromBaiinfoClassify) Update(cols []string) (err error) {
  152. o := orm.NewOrmUsingDB("data")
  153. _, err = o.Update(BaseFromBaiinfoClassify, cols...)
  154. return
  155. }
  156. type AddBaiinfoClassifyResp struct {
  157. ClassifyId int
  158. }
  159. // DeleteBaiinfoClassifyByClassifyId 根据分类id删除对应的指标分类
  160. func DeleteBaiinfoClassifyByClassifyId(classifyIdList []int) (err error) {
  161. o := orm.NewOrmUsingDB("data")
  162. num := len(classifyIdList)
  163. if num <= 0 {
  164. return
  165. }
  166. //删除分类
  167. sql := `DELETE FROM base_from_baiinfo_classify WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  168. _, err = o.Raw(sql, classifyIdList).Exec()
  169. return
  170. }
  171. // AddBaiinfoClassifyMulti 批量新增SMM类别
  172. func AddBaiinfoClassifyMulti(list []*BaseFromBaiinfoClassify) (err error) {
  173. o := orm.NewOrmUsingDB("data")
  174. _, err = o.InsertMulti(1, list)
  175. return
  176. }
  177. // InitBaiinfoClassifySort 初始化sort值
  178. func InitBaiinfoClassifySort() (err error) {
  179. o := orm.NewOrmUsingDB("data")
  180. sql := `UPDATE base_from_baiinfo_classify
  181. SET modify_time=NOW(), sort = classify_id`
  182. _, err = o.Raw(sql).Exec()
  183. return
  184. }
  185. // InitBaiinfoIndexClassifyId 历史数据的classifyId值
  186. func InitBaiinfoIndexClassifyId() (err error) {
  187. o := orm.NewOrmUsingDB("data")
  188. sql := `UPDATE base_from_baiinfo_index s
  189. LEFT JOIN (
  190. SELECT
  191. c1.classify_id,
  192. CONCAT( c2.classify_name, c1.classify_name ) AS type_name
  193. FROM
  194. base_from_baiinfo_classify c1
  195. LEFT JOIN base_from_baiinfo_classify c2 ON c1.parent_id = c2.classify_id
  196. ) AS t ON CONCAT( s.type_2, s.type_3 ) = t.type_name
  197. SET s.classify_id = t.classify_id, s.modify_time=NOW() where s.type_2 <>""`
  198. _, err = o.Raw(sql).Exec()
  199. return
  200. }