base_from_sci_classify.go 8.2 KB

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