base_from_ths_hf_classify.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strings"
  7. "time"
  8. )
  9. // BaseFromThsHfClassify 同花顺高频数据-分类
  10. type BaseFromThsHfClassify struct {
  11. BaseFromThsHfClassifyId int `orm:"column(base_from_ths_hf_classify_id);pk"`
  12. ClassifyName string `description:"分类名称"`
  13. ClassifyNameEn string `description:"英文分类名称"`
  14. ParentId int `description:"父级ID"`
  15. SysUserId int `description:"创建人ID"`
  16. SysUserRealName string `description:"创建人姓名"`
  17. Level int `description:"层级"`
  18. Sort int `description:"排序"`
  19. RootId int `description:"顶级分类ID"`
  20. LevelPath string `description:"层级路径"`
  21. UniqueCode string `description:"唯一编码"`
  22. CreateTime time.Time `description:"创建时间"`
  23. ModifyTime time.Time `description:"修改时间"`
  24. }
  25. func (m *BaseFromThsHfClassify) TableName() string {
  26. return "base_from_ths_hf_classify"
  27. }
  28. type BaseFromThsHfClassifyCols struct {
  29. PrimaryId string
  30. ClassifyName string
  31. ClassifyNameEn string
  32. ParentId string
  33. SysUserId string
  34. SysUserRealName string
  35. Level string
  36. Sort string
  37. RootId string
  38. LevelPath string
  39. UniqueCode string
  40. CreateTime string
  41. ModifyTime string
  42. }
  43. func (m *BaseFromThsHfClassify) Cols() BaseFromThsHfClassifyCols {
  44. return BaseFromThsHfClassifyCols{
  45. PrimaryId: "base_from_ths_hf_classify_id",
  46. ClassifyName: "classify_name",
  47. ClassifyNameEn: "classify_name_en",
  48. ParentId: "parent_id",
  49. SysUserId: "sys_user_id",
  50. SysUserRealName: "sys_user_real_name",
  51. Level: "level",
  52. Sort: "sort",
  53. RootId: "root_id",
  54. LevelPath: "level_path",
  55. UniqueCode: "unique_code",
  56. CreateTime: "create_time",
  57. ModifyTime: "modify_time",
  58. }
  59. }
  60. func (m *BaseFromThsHfClassify) Create() (err error) {
  61. o := orm.NewOrmUsingDB("data")
  62. id, err := o.Insert(m)
  63. if err != nil {
  64. return
  65. }
  66. m.BaseFromThsHfClassifyId = int(id)
  67. return
  68. }
  69. func (m *BaseFromThsHfClassify) CreateMulti(items []*BaseFromThsHfClassify) (err error) {
  70. if len(items) == 0 {
  71. return
  72. }
  73. o := orm.NewOrmUsingDB("data")
  74. _, err = o.InsertMulti(len(items), items)
  75. return
  76. }
  77. func (m *BaseFromThsHfClassify) Update(cols []string) (err error) {
  78. o := orm.NewOrmUsingDB("data")
  79. _, err = o.Update(m, cols...)
  80. return
  81. }
  82. func (m *BaseFromThsHfClassify) Remove() (err error) {
  83. o := orm.NewOrmUsingDB("data")
  84. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  85. _, err = o.Raw(sql, m.BaseFromThsHfClassifyId).Exec()
  86. return
  87. }
  88. func (m *BaseFromThsHfClassify) MultiRemove(ids []int) (err error) {
  89. if len(ids) == 0 {
  90. return
  91. }
  92. o := orm.NewOrmUsingDB("data")
  93. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  94. _, err = o.Raw(sql, ids).Exec()
  95. return
  96. }
  97. func (m *BaseFromThsHfClassify) RemoveByCondition(condition string, pars []interface{}) (err error) {
  98. if condition == "" {
  99. return
  100. }
  101. o := orm.NewOrmUsingDB("data")
  102. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
  103. _, err = o.Raw(sql, pars).Exec()
  104. return
  105. }
  106. func (m *BaseFromThsHfClassify) GetItemById(id int) (item *BaseFromThsHfClassify, err error) {
  107. o := orm.NewOrmUsingDB("data")
  108. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  109. err = o.Raw(sql, id).QueryRow(&item)
  110. return
  111. }
  112. func (m *BaseFromThsHfClassify) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *BaseFromThsHfClassify, err error) {
  113. o := orm.NewOrmUsingDB("data")
  114. order := ``
  115. if orderRule != "" {
  116. order = ` ORDER BY ` + orderRule
  117. }
  118. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  119. err = o.Raw(sql, pars).QueryRow(&item)
  120. return
  121. }
  122. func (m *BaseFromThsHfClassify) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  123. o := orm.NewOrmUsingDB("data")
  124. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  125. err = o.Raw(sql, pars).QueryRow(&count)
  126. return
  127. }
  128. func (m *BaseFromThsHfClassify) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BaseFromThsHfClassify, err error) {
  129. o := orm.NewOrmUsingDB("data")
  130. fields := strings.Join(fieldArr, ",")
  131. if len(fieldArr) == 0 {
  132. fields = `*`
  133. }
  134. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  135. if orderRule != "" {
  136. order = ` ORDER BY ` + orderRule
  137. }
  138. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  139. _, err = o.Raw(sql, pars).QueryRows(&items)
  140. return
  141. }
  142. func (m *BaseFromThsHfClassify) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BaseFromThsHfClassify, err error) {
  143. o := orm.NewOrmUsingDB("data")
  144. fields := strings.Join(fieldArr, ",")
  145. if len(fieldArr) == 0 {
  146. fields = `*`
  147. }
  148. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  149. if orderRule != "" {
  150. order = ` ORDER BY ` + orderRule
  151. }
  152. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  153. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  154. return
  155. }
  156. // BaseFromThsHfClassifyItem 同花顺高频数据信息
  157. type BaseFromThsHfClassifyItem struct {
  158. ClassifyId int `description:"分类ID"`
  159. ClassifyName string `description:"分类名称"`
  160. ClassifyNameEn string `description:"英文分类名称"`
  161. ParentId int `description:"父级ID"`
  162. Level int `description:"层级"`
  163. Sort int `description:"排序"`
  164. LevelPath string `description:"层级路径"`
  165. UniqueCode string `description:"唯一编码"`
  166. Children []*BaseFromThsHfClassifyItem `description:"子分类"`
  167. }
  168. func (m *BaseFromThsHfClassify) Format2Item() (item *BaseFromThsHfClassifyItem) {
  169. item = new(BaseFromThsHfClassifyItem)
  170. item.ClassifyId = m.BaseFromThsHfClassifyId
  171. item.ClassifyName = m.ClassifyName
  172. item.ClassifyNameEn = m.ClassifyNameEn
  173. item.ParentId = m.ParentId
  174. item.Level = m.Level
  175. item.Sort = m.Sort
  176. item.LevelPath = m.LevelPath
  177. item.UniqueCode = m.UniqueCode
  178. item.Children = make([]*BaseFromThsHfClassifyItem, 0)
  179. return
  180. }
  181. type ThsHfClassifyAddReq struct {
  182. ClassifyName string `description:"分类名称"`
  183. ParentId int `description:"父级ID, 第一级传0"`
  184. Level int `description:"层级, 第一级传0, 其余传上一级的层级"`
  185. }
  186. type ThsHfClassifyEditReq struct {
  187. ClassifyId int `description:"分类ID"`
  188. ClassifyName string `description:"分类名称"`
  189. }
  190. func (m *BaseFromThsHfClassify) GetSortMax(parentId int) (sort int, err error) {
  191. o := orm.NewOrmUsingDB("data")
  192. sql := fmt.Sprintf(`SELECT Max(%s) FROM %s WHERE %s = ?`, m.Cols().Sort, m.TableName(), m.Cols().ParentId)
  193. err = o.Raw(sql, parentId).QueryRow(&sort)
  194. return
  195. }
  196. type ThsHfClassifyRemoveReq struct {
  197. ClassifyId int `description:"分类ID"`
  198. }
  199. type BaseFromThsHfClassifyListItem struct {
  200. ItemType int `description:"类型: 0-分类; 1-指标"`
  201. ClassifyId int `description:"分类ID"`
  202. ClassifyName string `description:"分类名称"`
  203. ClassifyNameEn string `description:"英文分类名称"`
  204. IndexId int `description:"指标ID"`
  205. IndexCode string `description:"指标编码"`
  206. IndexName string `description:"指标名称"`
  207. ParentId int `description:"父级ID"`
  208. Level int `description:"层级"`
  209. Sort int `description:"排序"`
  210. UniqueCode string `description:"唯一编码, 指标的话用indexCode"`
  211. Children []*BaseFromThsHfClassifyListItem `description:"子分类"`
  212. }
  213. type BaseFromThsHfClassifyMoveReq struct {
  214. ClassifyId int `description:"分类ID"`
  215. ParentClassifyId int `description:"父级分类ID"`
  216. PrevClassifyId int `description:"上一个兄弟节点分类ID"`
  217. NextClassifyId int `description:"下一个兄弟节点分类ID"`
  218. ItemId int `description:"指标ID, 如果指标ID有值,则移动对象为指标,否则认为移动对象为分类"`
  219. PrevItemId int `description:"上一个指标ID"`
  220. NextItemId int `description:"下一个指标ID"`
  221. }
  222. func GetThsHfClassifyById(classifyId int) (item *BaseFromThsHfClassify, err error) {
  223. o := orm.NewOrmUsingDB("data")
  224. sql := `SELECT * FROM base_from_ths_hf_classify WHERE base_from_ths_hf_classify_id = ?`
  225. err = o.Raw(sql, classifyId).QueryRow(&item)
  226. return
  227. }
  228. func GetThsHfClassifyByRootIdLevel(rootId int, orderStr string) (items []*BaseFromThsHfClassify, err error) {
  229. o := orm.NewOrmUsingDB("data")
  230. sql := ` SELECT * FROM base_from_ths_hf_classify WHERE root_id = ? `
  231. if orderStr != "" {
  232. sql += orderStr
  233. } else {
  234. sql += ` order by level desc, sort asc, base_from_ths_hf_classify_id asc`
  235. }
  236. _, err = o.Raw(sql, rootId).QueryRows(&items)
  237. return
  238. }
  239. // UpdateThsHfClassifySortByParentId 根据父类id更新排序
  240. func UpdateThsHfClassifySortByParentId(parentId, classifyId, nowSort int, updateSort string) (err error) {
  241. o := orm.NewOrmUsingDB("data")
  242. sql := ` update base_from_ths_hf_classify set sort = ` + updateSort + ` WHERE parent_id = ? AND sort > ? `
  243. if classifyId > 0 {
  244. sql += ` or ( base_from_ths_hf_classify_id > ` + fmt.Sprint(classifyId) + ` and sort = ` + fmt.Sprint(nowSort) + `)`
  245. }
  246. _, err = o.Raw(sql, parentId, nowSort).Exec()
  247. return
  248. }
  249. // GetFirstThsHfClassifyByParentId 获取当前父级分类下,且排序数相同 的排序第一条的数据
  250. func GetFirstThsHfClassifyByParentId(parentId int) (item *BaseFromThsHfClassify, err error) {
  251. o := orm.NewOrmUsingDB("data")
  252. sql := ` SELECT * FROM base_from_ths_hf_classify WHERE parent_id = ? order by sort asc,base_from_ths_hf_classify_id asc limit 1`
  253. err = o.Raw(sql, parentId).QueryRow(&item)
  254. return
  255. }
  256. func UpdateThsHfClassifyChildByParentClassifyId(classifyIds []int, rootId int, levelStep int) (err error) {
  257. o := orm.NewOrmUsingDB("data")
  258. var pars []interface{}
  259. pars = append(pars, rootId, levelStep)
  260. pars = append(pars, classifyIds)
  261. // 更新相关联的二级分类的parentId,和classify_name_second
  262. sql := `update base_from_ths_hf_classify SET root_id = ?, level = level+? where base_from_ths_hf_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)`
  263. _, err = o.Raw(sql, pars).Exec()
  264. if err != nil {
  265. return
  266. }
  267. return
  268. }