base_from_yongyi_classify.go 8.5 KB

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