base_from_smm_classify.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/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. }
  20. // AddBaseFromSmmClassify 添加SMM原始数据分类
  21. func AddBaseFromSmmClassify(item *BaseFromSmmClassify) (lastId int64, err error) {
  22. o := orm.NewOrmUsingDB("data")
  23. lastId, err = o.Insert(item)
  24. return
  25. }
  26. // GetBaseFromSmmClassifyCount 获取分类名称的个数
  27. func GetBaseFromSmmClassifyCount(classifyName string, parentId int) (count int, err error) {
  28. o := orm.NewOrmUsingDB("data")
  29. sql := `SELECT COUNT(1) AS count FROM base_from_smm_classify WHERE classify_name=? AND parent_id=? `
  30. err = o.Raw(sql, classifyName, parentId).QueryRow(&count)
  31. return
  32. }
  33. // GetBaseFromSmmClassifyById 通过分类id的获取分类信息
  34. func GetBaseFromSmmClassifyById(classifyId int) (item *BaseFromSmmClassify, err error) {
  35. o := orm.NewOrmUsingDB("data")
  36. sql := `SELECT * FROM base_from_smm_classify WHERE classify_id=? `
  37. err = o.Raw(sql, classifyId).QueryRow(&item)
  38. return
  39. }
  40. // EditBaseFromSmmClassify 修改SMM原始数据分类
  41. func EditBaseFromSmmClassify(classifyId int, classifyName string) (err error) {
  42. o := orm.NewOrmUsingDB("data")
  43. sql := `UPDATE base_from_smm_classify SET classify_name=?,modify_time=NOW() WHERE classify_id=? `
  44. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  45. return
  46. }
  47. // UpdateBaseFromSmmClassifySort 修改SMM原始数据分类的排序
  48. func UpdateBaseFromSmmClassifySort(classifyId int) (err error) {
  49. o := orm.NewOrmUsingDB("data")
  50. sql := `UPDATE base_from_smm_classify SET sort=classify_id, modify_time=NOW() WHERE classify_id=? `
  51. _, err = o.Raw(sql, classifyId).Exec()
  52. return
  53. }
  54. type BaseFromSmmClassifyItems struct {
  55. ClassifyId int `description:"分类ID"`
  56. BaseFromSmmIndexId int `description:"指标类型ID"`
  57. BaseFromSmmIndexCode 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 []*BaseFromSmmClassifyItems `description:"下级"`
  66. }
  67. type BaseFromSmmClassifyNameItems struct {
  68. ClassifyId int `description:"分类ID"`
  69. ClassifyName string `description:"分类名称"`
  70. ParentId int `description:"父级id"`
  71. Children []*BaseFromSmmClassifyNameItems `description:"下级"`
  72. }
  73. type BaseFromSmmClassifyResp struct {
  74. List []*BaseFromSmmClassifyItems
  75. }
  76. type BaseFromSmmClassifyNameResp struct {
  77. List []*BaseFromSmmClassifyNameItems
  78. }
  79. type BaseFromSmmClassifyItemsButton struct {
  80. AddButton bool `description:"是否可添加"`
  81. OpButton bool `description:"是否可编辑"`
  82. DeleteButton bool `description:"是否可删除"`
  83. MoveButton bool `description:"是否可移动"`
  84. }
  85. // GetBaseFromSmmClassifyByParentId 根据上级id获取当下的分类列表数据
  86. func GetBaseFromSmmClassifyByParentId(parentId int) (items []*BaseFromSmmClassifyItems, err error) {
  87. o := orm.NewOrmUsingDB("data")
  88. sql := ` SELECT * FROM base_from_smm_classify WHERE parent_id=? order by sort asc,classify_id asc`
  89. _, err = o.Raw(sql, parentId).QueryRows(&items)
  90. return
  91. }
  92. // GetBaseFromSmmClassifyByIds 根据id获取当下的分类列表数据
  93. func GetBaseFromSmmClassifyByIds(classifyIds []int) (items []*BaseFromSmmClassifyItems, err error) {
  94. o := orm.NewOrmUsingDB("data")
  95. 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`
  96. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  97. return
  98. }
  99. // GetAllBaseFromSmmClassify 获取所有的分类列表数据
  100. func GetAllBaseFromSmmClassify() (items []*BaseFromSmmClassifyItems, err error) {
  101. o := orm.NewOrmUsingDB("data")
  102. sql := ` SELECT * FROM base_from_smm_classify WHERE parent_id <> 0 order by sort asc,classify_id asc`
  103. _, err = o.Raw(sql).QueryRows(&items)
  104. return
  105. }
  106. type DeleteBaseFromSmmClassifyReq struct {
  107. ClassifyId int `description:"分类id"`
  108. EdbInfoId int `description:"指标id"`
  109. }
  110. type BaseFromSmmClassifyListResp struct {
  111. AllNodes []*BaseFromSmmClassifyItems
  112. CanOpClassify bool `description:"是否允许操作分类"`
  113. }
  114. type BaseFromSmmClassifySimplify struct {
  115. ClassifyId int `description:"分类id"`
  116. ClassifyName string `description:"分类名称"`
  117. ParentId int
  118. }
  119. // GetFirstBaseFromSmmClassify 获取当前分类下,且排序数相同 的排序第一条的数据
  120. func GetFirstBaseFromSmmClassify() (item *BaseFromSmmClassify, err error) {
  121. o := orm.NewOrmUsingDB("data")
  122. sql := ` SELECT * FROM base_from_smm_classify order by sort asc,classify_id asc limit 1`
  123. err = o.Raw(sql).QueryRow(&item)
  124. return
  125. }
  126. // UpdateBaseFromSmmClassifySortByClassifyId 根据分类id更新排序
  127. func UpdateBaseFromSmmClassifySortByClassifyId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  128. o := orm.NewOrmUsingDB("data")
  129. sql := ` update base_from_smm_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. // MoveUpSmmIndexClassifyBySort 往上移动
  137. func MoveUpSmmIndexClassifyBySort(parentId, nextSort, currentSort int) (err error) {
  138. o := orm.NewOrmUsingDB("data")
  139. sql := `update base_from_smm_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. // MoveDownSmmIndexClassifyBySort 往下移动
  144. func MoveDownSmmIndexClassifyBySort(parentId, prevSort, currentSort int) (err error) {
  145. o := orm.NewOrmUsingDB("data")
  146. sql := `update base_from_smm_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. // GetSmmIndexClassifyMinSort 获取最小不等于0的排序
  151. func GetSmmIndexClassifyMinSort(parentId int) (sort int, err error) {
  152. o := orm.NewOrmUsingDB("data")
  153. sql := `select min(sort) from base_from_smm_classify where parent_id=? and sort <> 0 `
  154. err = o.Raw(sql, parentId).QueryRow(&sort)
  155. return
  156. }
  157. // Update 更新分类基础信息
  158. func (BaseFromSmmClassify *BaseFromSmmClassify) Update(cols []string) (err error) {
  159. o := orm.NewOrmUsingDB("data")
  160. _, err = o.Update(BaseFromSmmClassify, cols...)
  161. return
  162. }
  163. type AddSmmClassifyResp struct {
  164. ClassifyId int
  165. }
  166. // DeleteSmmClassifyByClassifyId 根据分类id删除对应的指标分类
  167. func DeleteSmmClassifyByClassifyId(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_smm_classify WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  175. _, err = o.Raw(sql, classifyIdList).Exec()
  176. return
  177. }
  178. // AddSmmClassifyMulti 批量新增SMM类别
  179. func AddSmmClassifyMulti(list []*BaseFromSmmClassify) (err error) {
  180. o := orm.NewOrmUsingDB("data")
  181. _, err = o.InsertMulti(1, list)
  182. return
  183. }
  184. // InitSmmClassifySort 初始化sort值
  185. func InitSmmClassifySort() (err error) {
  186. o := orm.NewOrmUsingDB("data")
  187. sql := `UPDATE base_from_smm_classify
  188. SET modify_time=NOW(), sort = classify_id`
  189. _, err = o.Raw(sql).Exec()
  190. return
  191. }
  192. // InitSmmIndexClassifyId 历史数据的classifyId值
  193. func InitSmmIndexClassifyId() (err error) {
  194. o := orm.NewOrmUsingDB("data")
  195. sql := `UPDATE base_from_smm_index 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_smm_classify c1
  202. LEFT JOIN base_from_smm_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. }