base_from_usda_fas_classify.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. // BaseFromUsdaFasClassify UsdaFas原始数据分类表
  9. type BaseFromUsdaFasClassify 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. // AddBaseFromUsdaFasClassify 添加UsdaFas原始数据分类
  21. func AddBaseFromUsdaFasClassify(item *BaseFromUsdaFasClassify) (lastId int64, err error) {
  22. o := orm.NewOrmUsingDB("data")
  23. lastId, err = o.Insert(item)
  24. return
  25. }
  26. // GetBaseFromUsdaFasClassifyCount 获取分类名称的个数
  27. func GetBaseFromUsdaFasClassifyCount(classifyName string, parentId int) (count int, err error) {
  28. o := orm.NewOrmUsingDB("data")
  29. sql := `SELECT COUNT(1) AS count FROM base_from_usda_fas_classify WHERE classify_name=? AND parent_id=? `
  30. err = o.Raw(sql, classifyName, parentId).QueryRow(&count)
  31. return
  32. }
  33. // GetBaseFromUsdaFasClassifyById 通过分类id的获取分类信息
  34. func GetBaseFromUsdaFasClassifyById(classifyId int) (item *BaseFromUsdaFasClassify, err error) {
  35. o := orm.NewOrmUsingDB("data")
  36. sql := `SELECT * FROM base_from_usda_fas_classify WHERE classify_id=? `
  37. err = o.Raw(sql, classifyId).QueryRow(&item)
  38. return
  39. }
  40. // GetBaseFromUsdaFasClassifyById 通过分类id的获取分类信息
  41. func GetBaseFromUsdaFasClassifyByIds(classifyIds []int) (items []*BaseFromUsdaFasClassify, err error) {
  42. if len(classifyIds) == 0 {
  43. return
  44. }
  45. o := orm.NewOrmUsingDB("data")
  46. sql := `SELECT * FROM base_from_usda_fas_classify WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) `
  47. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  48. return
  49. }
  50. // EditBaseFromUsdaFasClassify 修改UsdaFas原始数据分类
  51. func EditBaseFromUsdaFasClassify(classifyId int, classifyName string) (err error) {
  52. o := orm.NewOrmUsingDB("data")
  53. sql := `UPDATE base_from_usda_fas_classify SET classify_name=?,modify_time=NOW() WHERE classify_id=? `
  54. _, err = o.Raw(sql, classifyName, classifyId).Exec()
  55. return
  56. }
  57. // UpdateBaseFromUsdaFasClassifySort 修改UsdaFas原始数据分类的排序
  58. func UpdateBaseFromUsdaFasClassifySort(classifyId int) (err error) {
  59. o := orm.NewOrmUsingDB("data")
  60. sql := `UPDATE base_from_usda_fas_classify SET sort=classify_id, modify_time=NOW() WHERE classify_id=? `
  61. _, err = o.Raw(sql, classifyId).Exec()
  62. return
  63. }
  64. type BaseFromUsdaFasClassifyItems struct {
  65. ClassifyId int `description:"分类ID"`
  66. BaseFromUsdaFasIndexId int `description:"指标类型ID"`
  67. IndexCode string `description:"指标唯一编码"`
  68. ClassifyName string `description:"分类名称"`
  69. ClassifyNameEn string `description:"分类名称"`
  70. UniqueCode string `description:"分类唯一编码"`
  71. ParentId int `description:"父级id"`
  72. Level int `description:"层级"`
  73. Sort int `description:"排序字段,越小越靠前,默认值:10"`
  74. Children []*BaseFromUsdaFasClassifyItems
  75. }
  76. type BaseFromUsdaFasClassifyNameItems struct {
  77. ClassifyId int `description:"分类ID"`
  78. ClassifyName string `description:"分类名称"`
  79. ParentId int `description:"父级id"`
  80. }
  81. type BaseFromUsdaFasClassifyResp struct {
  82. List []*BaseFromUsdaFasClassifyItems
  83. }
  84. type BaseFromUsdaFasClassifyNameResp struct {
  85. List []*BaseFromUsdaFasClassifyNameItems
  86. }
  87. type BaseFromUsdaFasClassifyItemsButton struct {
  88. AddButton bool `description:"是否可添加"`
  89. OpButton bool `description:"是否可编辑"`
  90. DeleteButton bool `description:"是否可删除"`
  91. MoveButton bool `description:"是否可移动"`
  92. }
  93. // GetBaseFromUsdaFasClassifyByParentId 根据上级id获取当下的分类列表数据
  94. func GetBaseFromUsdaFasClassifyByParentId(parentId int) (items []*BaseFromUsdaFasClassifyItems, err error) {
  95. o := orm.NewOrmUsingDB("data")
  96. sql := ` SELECT * FROM base_from_usda_fas_classify WHERE parent_id=? order by sort asc,classify_id asc`
  97. _, err = o.Raw(sql, parentId).QueryRows(&items)
  98. return
  99. }
  100. // GetAllBaseFromUsdaFasClassify 获取所有的分类列表数据
  101. func GetAllBaseFromUsdaFasClassify() (items []*BaseFromUsdaFasClassifyItems, err error) {
  102. o := orm.NewOrmUsingDB("data")
  103. sql := ` SELECT * FROM base_from_usda_fas_classify order by parent_id asc, sort asc,classify_id asc`
  104. _, err = o.Raw(sql).QueryRows(&items)
  105. return
  106. }
  107. type DeleteBaseFromUsdaFasClassifyReq struct {
  108. ClassifyId int `description:"分类id"`
  109. EdbInfoId int `description:"指标id"`
  110. }
  111. type BaseFromUsdaFasClassifyListResp struct {
  112. AllNodes []*BaseFromUsdaFasClassifyItems
  113. CanOpClassify bool `description:"是否允许操作分类"`
  114. }
  115. type BaseFromUsdaFasClassifySimplify struct {
  116. ClassifyId int `description:"分类id"`
  117. ClassifyName string `description:"分类名称"`
  118. ParentId int
  119. }
  120. // GetFirstBaseFromUsdaFasClassify 获取当前分类下,且排序数相同 的排序第一条的数据
  121. func GetFirstBaseFromUsdaFasClassify() (item *BaseFromUsdaFasClassify, err error) {
  122. o := orm.NewOrmUsingDB("data")
  123. sql := ` SELECT * FROM base_from_usda_fas_classify order by sort asc,classify_id asc limit 1`
  124. err = o.Raw(sql).QueryRow(&item)
  125. return
  126. }
  127. // UpdateBaseFromUsdaFasClassifySortByClassifyId 根据分类id更新排序
  128. func UpdateBaseFromUsdaFasClassifySortByClassifyId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  129. o := orm.NewOrmUsingDB("data")
  130. sql := ` update base_from_usda_fas_classify set sort = ` + updateSort + ` WHERE parent_id=? AND sort > ? `
  131. if classifyId > 0 {
  132. sql += ` or ( classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  133. }
  134. _, err = o.Raw(sql, parentId, nowSort).Exec()
  135. return
  136. }
  137. // MoveUpUsdaFasIndexClassifyBySort 往上移动
  138. func MoveUpUsdaFasIndexClassifyBySort(parentId, nextSort, currentSort int) (err error) {
  139. o := orm.NewOrmUsingDB("data")
  140. sql := `update base_from_usda_fas_classify set sort = sort + 1 where parent_id=? and sort >= ? and sort< ?`
  141. _, err = o.Raw(sql, parentId, nextSort, currentSort).Exec()
  142. return
  143. }
  144. // MoveDownUsdaFasIndexClassifyBySort 往下移动
  145. func MoveDownUsdaFasIndexClassifyBySort(parentId, prevSort, currentSort int) (err error) {
  146. o := orm.NewOrmUsingDB("data")
  147. sql := `update base_from_usda_fas_classify set sort = sort - 1 where parent_id=? and sort <= ? and sort> ? `
  148. _, err = o.Raw(sql, parentId, prevSort, currentSort).Exec()
  149. return
  150. }
  151. // GetUsdaFasIndexClassifyMinSort 获取最小不等于0的排序
  152. func GetUsdaFasIndexClassifyMinSort(parentId int) (sort int, err error) {
  153. o := orm.NewOrmUsingDB("data")
  154. sql := `select min(sort) from base_from_usda_fas_classify where parent_id=? and sort <> 0 `
  155. err = o.Raw(sql, parentId).QueryRow(&sort)
  156. return
  157. }
  158. // Update 更新分类基础信息
  159. func (BaseFromUsdaFasClassify *BaseFromUsdaFasClassify) Update(cols []string) (err error) {
  160. o := orm.NewOrmUsingDB("data")
  161. _, err = o.Update(BaseFromUsdaFasClassify, cols...)
  162. return
  163. }
  164. type AddUsdaFasClassifyResp struct {
  165. ClassifyId int
  166. }
  167. // DeleteUsdaFasClassifyByClassifyId 根据分类id删除对应的指标分类
  168. func DeleteUsdaFasClassifyByClassifyId(classifyIdList []int) (err error) {
  169. o := orm.NewOrmUsingDB("data")
  170. num := len(classifyIdList)
  171. if num <= 0 {
  172. return
  173. }
  174. //删除分类
  175. sql := `DELETE FROM base_from_usda_fas_classify WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  176. _, err = o.Raw(sql, classifyIdList).Exec()
  177. return
  178. }
  179. // AddUsdaFasClassifyMulti 批量新增SMM类别
  180. func AddUsdaFasClassifyMulti(list []*BaseFromUsdaFasClassify) (err error) {
  181. o := orm.NewOrmUsingDB("data")
  182. _, err = o.InsertMulti(1, list)
  183. return
  184. }
  185. // InitUsdaFasClassifySort 初始化sort值
  186. func InitUsdaFasClassifySort() (err error) {
  187. o := orm.NewOrmUsingDB("data")
  188. sql := `UPDATE base_from_usda_fas_classify
  189. SET modify_time=NOW(), sort = classify_id`
  190. _, err = o.Raw(sql).Exec()
  191. return
  192. }
  193. // InitUsdaFasIndexClassifyId 历史数据的classifyId值
  194. func InitUsdaFasIndexClassifyId() (err error) {
  195. o := orm.NewOrmUsingDB("data")
  196. sql := `UPDATE base_from_UsdaFasindex s
  197. LEFT JOIN (
  198. SELECT
  199. c1.classify_id,
  200. CONCAT( c2.classify_name, c1.classify_name ) AS type_name
  201. FROM
  202. base_from_usda_fas_classify c1
  203. LEFT JOIN base_from_usda_fas_classify c2 ON c1.parent_id = c2.classify_id
  204. ) AS t ON CONCAT( s.type_2, s.type_3 ) = t.type_name
  205. SET s.classify_id = t.classify_id, s.modify_time=NOW() where s.type_2 <>""`
  206. _, err = o.Raw(sql).Exec()
  207. return
  208. }