base_from_yongyi_classify.go 8.0 KB

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