base_from_smm_classify.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. // BaseFromSmmClassify SMM原始数据分类表
  9. type BaseFromSmmClassify 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. // AddBaseFromSmmClassify 添加SMM原始数据分类
  22. func AddBaseFromSmmClassify(item *BaseFromSmmClassify) (lastId int64, err error) {
  23. o := orm.NewOrmUsingDB("data")
  24. lastId, err = o.Insert(item)
  25. return
  26. }
  27. // GetBaseFromSmmClassifyCount 获取分类名称的个数
  28. func GetBaseFromSmmClassifyCount(classifyName string, parentId int) (count int, err error) {
  29. o := orm.NewOrmUsingDB("data")
  30. sql := `SELECT COUNT(1) AS count FROM base_from_smm_classify WHERE classify_name=? AND parent_id=? `
  31. err = o.Raw(sql, classifyName, parentId).QueryRow(&count)
  32. return
  33. }
  34. // GetBaseFromSmmClassifyEnCount
  35. // @Description: 获取英文分类名称的个数
  36. // @author: Roc
  37. // @datetime 2024-04-16 16:49:03
  38. // @param classifyNameEn string
  39. // @param parentId int
  40. // @return count int
  41. // @return err error
  42. func GetBaseFromSmmClassifyEnCount(classifyNameEn string, parentId int) (count int, err error) {
  43. o := orm.NewOrmUsingDB("data")
  44. sql := `SELECT COUNT(1) AS count FROM base_from_smm_classify WHERE classify_name_en = ? AND parent_id = ? `
  45. err = o.Raw(sql, classifyNameEn, parentId).QueryRow(&count)
  46. return
  47. }
  48. // GetBaseFromSmmClassifyById 通过分类id的获取分类信息
  49. func GetBaseFromSmmClassifyById(classifyId int) (item *BaseFromSmmClassify, err error) {
  50. o := orm.NewOrmUsingDB("data")
  51. sql := `SELECT * FROM base_from_smm_classify WHERE classify_id=? `
  52. err = o.Raw(sql, classifyId).QueryRow(&item)
  53. return
  54. }
  55. // EditBaseFromSmmClassify 修改SMM原始数据分类
  56. func EditBaseFromSmmClassify(classifyId int, classifyName string) (err error) {
  57. o := orm.NewOrmUsingDB("data")
  58. sql := `UPDATE base_from_smm_classify SET classify_name=?,modify_time=NOW() WHERE classify_id=? `
  59. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  60. return
  61. }
  62. // EditBaseFromSmmClassifyEn
  63. // @Description: 修改SMM原始数据英文分类
  64. // @author: Roc
  65. // @datetime 2024-04-16 16:50:55
  66. // @param classifyId int
  67. // @param classifyNameEn string
  68. // @return err error
  69. func EditBaseFromSmmClassifyEn(classifyId int, classifyNameEn string) (err error) {
  70. o := orm.NewOrmUsingDB("data")
  71. sql := `UPDATE base_from_smm_classify SET classify_name_en = ?,modify_time=NOW() WHERE classify_id=? `
  72. _, err = o.Raw(sql, classifyNameEn, classifyId).Exec()
  73. return
  74. }
  75. // UpdateBaseFromSmmClassifySort 修改SMM原始数据分类的排序
  76. func UpdateBaseFromSmmClassifySort(classifyId int) (err error) {
  77. o := orm.NewOrmUsingDB("data")
  78. sql := `UPDATE base_from_smm_classify SET sort=classify_id, modify_time=NOW() WHERE classify_id=? `
  79. _, err = o.Raw(sql, classifyId).Exec()
  80. return
  81. }
  82. type BaseFromSmmClassifyItems struct {
  83. ClassifyId int `description:"分类ID"`
  84. BaseFromSmmIndexId int `description:"指标类型ID"`
  85. BaseFromSmmIndexCode 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 []*BaseFromSmmClassifyItems `description:"下级"`
  95. }
  96. type BaseFromSmmClassifyNameItems struct {
  97. ClassifyId int `description:"分类ID"`
  98. ClassifyName string `description:"分类名称"`
  99. ParentId int `description:"父级id"`
  100. Children []*BaseFromSmmClassifyNameItems `description:"下级"`
  101. }
  102. type BaseFromSmmClassifyResp struct {
  103. List []*BaseFromSmmClassifyItems
  104. }
  105. type BaseFromSmmClassifyNameResp struct {
  106. List []*BaseFromSmmClassifyNameItems
  107. }
  108. type BaseFromSmmClassifyItemsButton struct {
  109. AddButton bool `description:"是否可添加"`
  110. OpButton bool `description:"是否可编辑"`
  111. DeleteButton bool `description:"是否可删除"`
  112. MoveButton bool `description:"是否可移动"`
  113. }
  114. // GetBaseFromSmmClassifyByParentId 根据上级id获取当下的分类列表数据
  115. func GetBaseFromSmmClassifyByParentId(parentId int) (items []*BaseFromSmmClassifyItems, err error) {
  116. o := orm.NewOrmUsingDB("data")
  117. sql := ` SELECT * FROM base_from_smm_classify WHERE parent_id=? order by sort asc,classify_id asc`
  118. _, err = o.Raw(sql, parentId).QueryRows(&items)
  119. return
  120. }
  121. // GetBaseFromSmmClassifyByIds 根据id获取当下的分类列表数据
  122. func GetBaseFromSmmClassifyByIds(classifyIds []int) (items []*BaseFromSmmClassifyItems, err error) {
  123. o := orm.NewOrmUsingDB("data")
  124. sql := ` SELECT * FROM base_from_smm_classify WHERE parent_id > 0 and classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) order by sort asc, classify_id asc`
  125. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  126. return
  127. }
  128. // GetAllBaseFromSmmClassify 获取所有的分类列表数据
  129. func GetAllBaseFromSmmClassify() (items []*BaseFromSmmClassifyItems, err error) {
  130. o := orm.NewOrmUsingDB("data")
  131. sql := ` SELECT * FROM base_from_smm_classify WHERE parent_id <> 0 order by sort asc,classify_id asc`
  132. _, err = o.Raw(sql).QueryRows(&items)
  133. return
  134. }
  135. type DeleteBaseFromSmmClassifyReq struct {
  136. ClassifyId int `description:"分类id"`
  137. EdbInfoId int `description:"指标id"`
  138. }
  139. type BaseFromSmmClassifyListResp struct {
  140. AllNodes []*BaseFromSmmClassifyItems
  141. CanOpClassify bool `description:"是否允许操作分类"`
  142. }
  143. type BaseFromSmmClassifySimplify struct {
  144. ClassifyId int `description:"分类id"`
  145. ClassifyName string `description:"分类名称"`
  146. ParentId int
  147. }
  148. // GetFirstBaseFromSmmClassify 获取当前分类下,且排序数相同 的排序第一条的数据
  149. func GetFirstBaseFromSmmClassify() (item *BaseFromSmmClassify, err error) {
  150. o := orm.NewOrmUsingDB("data")
  151. sql := ` SELECT * FROM base_from_smm_classify order by sort asc,classify_id asc limit 1`
  152. err = o.Raw(sql).QueryRow(&item)
  153. return
  154. }
  155. // UpdateBaseFromSmmClassifySortByClassifyId 根据分类id更新排序
  156. func UpdateBaseFromSmmClassifySortByClassifyId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  157. o := orm.NewOrmUsingDB("data")
  158. sql := ` update base_from_smm_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? `
  159. if classifyId > 0 {
  160. sql += ` or ( classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  161. }
  162. _, err = o.Raw(sql, parentId, nowSort).Exec()
  163. return
  164. }
  165. // MoveUpSmmIndexClassifyBySort 往上移动
  166. func MoveUpSmmIndexClassifyBySort(parentId, nextSort, currentSort int) (err error) {
  167. o := orm.NewOrmUsingDB("data")
  168. sql := `update base_from_smm_classify set sort = sort + 1 where parent_id=? and sort >= ? and sort< ?`
  169. _, err = o.Raw(sql, parentId, nextSort, currentSort).Exec()
  170. return
  171. }
  172. // MoveDownSmmIndexClassifyBySort 往下移动
  173. func MoveDownSmmIndexClassifyBySort(parentId, prevSort, currentSort int) (err error) {
  174. o := orm.NewOrmUsingDB("data")
  175. sql := `update base_from_smm_classify set sort = sort - 1 where parent_id=? and sort <= ? and sort> ? `
  176. _, err = o.Raw(sql, parentId, prevSort, currentSort).Exec()
  177. return
  178. }
  179. // GetSmmIndexClassifyMinSort 获取最小不等于0的排序
  180. func GetSmmIndexClassifyMinSort(parentId int) (sort int, err error) {
  181. o := orm.NewOrmUsingDB("data")
  182. sql := `select min(sort) from base_from_smm_classify where parent_id=? and sort <> 0 `
  183. err = o.Raw(sql, parentId).QueryRow(&sort)
  184. return
  185. }
  186. // Update 更新分类基础信息
  187. func (BaseFromSmmClassify *BaseFromSmmClassify) Update(cols []string) (err error) {
  188. o := orm.NewOrmUsingDB("data")
  189. _, err = o.Update(BaseFromSmmClassify, cols...)
  190. return
  191. }
  192. type AddSmmClassifyResp struct {
  193. ClassifyId int
  194. }
  195. // DeleteSmmClassifyByClassifyId 根据分类id删除对应的指标分类
  196. func DeleteSmmClassifyByClassifyId(classifyIdList []int) (err error) {
  197. o := orm.NewOrmUsingDB("data")
  198. num := len(classifyIdList)
  199. if num <= 0 {
  200. return
  201. }
  202. //删除分类
  203. sql := `DELETE FROM base_from_smm_classify WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  204. _, err = o.Raw(sql, classifyIdList).Exec()
  205. return
  206. }
  207. // AddSmmClassifyMulti 批量新增SMM类别
  208. func AddSmmClassifyMulti(list []*BaseFromSmmClassify) (err error) {
  209. o := orm.NewOrmUsingDB("data")
  210. _, err = o.InsertMulti(1, list)
  211. return
  212. }
  213. // InitSmmClassifySort 初始化sort值
  214. func InitSmmClassifySort() (err error) {
  215. o := orm.NewOrmUsingDB("data")
  216. sql := `UPDATE base_from_smm_classify
  217. SET modify_time=NOW(), sort = classify_id`
  218. _, err = o.Raw(sql).Exec()
  219. return
  220. }
  221. // InitSmmIndexClassifyId 历史数据的classifyId值
  222. func InitSmmIndexClassifyId() (err error) {
  223. o := orm.NewOrmUsingDB("data")
  224. sql := `UPDATE base_from_smm_index s
  225. LEFT JOIN (
  226. SELECT
  227. c1.classify_id,
  228. CONCAT( c2.classify_name, c1.classify_name ) AS type_name
  229. FROM
  230. base_from_smm_classify c1
  231. LEFT JOIN base_from_smm_classify c2 ON c1.parent_id = c2.classify_id
  232. ) AS t ON CONCAT( s.type_2, s.type_3 ) = t.type_name
  233. SET s.classify_id = t.classify_id, s.modify_time=NOW() where s.type_2 <>""`
  234. _, err = o.Raw(sql).Exec()
  235. return
  236. }