base_from_baiinfo_classify.go 9.7 KB

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