base_from_smm.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. type BaseFromSmm struct {
  9. BaseFromSmmId int `orm:"column(base_from_smm_id);pk"`
  10. Aid int
  11. Name string
  12. Interface string
  13. ApiUpdate int
  14. ApiUpdateType string
  15. ApiType int
  16. ApiTypeAll string
  17. Type1 string `orm:"column(type_1)"`
  18. Type2 string `orm:"column(type_2)"`
  19. Type3 string `orm:"column(type_3)"`
  20. ApiStartTime string
  21. ApiUpdateTime string
  22. StartTime string
  23. FinishTime string
  24. AuthModule string
  25. AuthLang string
  26. ApiLabel string
  27. Enable string
  28. EditPerson string
  29. EditTime string
  30. AuthDur int
  31. AuthDurType string
  32. }
  33. func AddBaseFromSmm(item *BaseFromSmm) (lastId int64, err error) {
  34. o := orm.NewOrmUsingDB("data")
  35. lastId, err = o.Insert(item)
  36. return
  37. }
  38. func GetBaseFromSmmList() (list []*BaseFromSmm, err error) {
  39. o := orm.NewOrmUsingDB("data")
  40. sql := `SELECT * FROM base_from_smm `
  41. _, err = o.Raw(sql).QueryRows(&list)
  42. return
  43. }
  44. func GetBaseFromSmmBySmmCode(smmCode string) (item *BaseFromSmm, err error) {
  45. o := orm.NewOrmUsingDB("data")
  46. sql := `SELECT * FROM base_from_smm WHERE interface=?`
  47. err = o.Raw(sql, smmCode).QueryRow(&item)
  48. return
  49. }
  50. type BaseFromSmmIndex struct {
  51. BaseFromSmmIndexId int `orm:"column(base_from_smm_index_id);pk"`
  52. ClassifyId int
  53. Interface string
  54. Name string
  55. IndexCode string
  56. IndexName string
  57. Type1 string `orm:"column(type_1)"`
  58. Type2 string `orm:"column(type_2)"`
  59. Type3 string `orm:"column(type_3)"`
  60. Frequency string
  61. Unit string
  62. ApiStartTime string
  63. ApiUpdateTime string
  64. StartTime string
  65. FinishTime string
  66. Sort int
  67. CreateTime time.Time
  68. ModifyTime time.Time
  69. }
  70. func AddBaseFromSmmIndex(item *BaseFromSmmIndex) (lastId int64, err error) {
  71. o := orm.NewOrmUsingDB("data")
  72. lastId, err = o.Insert(item)
  73. return
  74. }
  75. func GetBaseFromSmmIndex() (list []*BaseFromSmmIndex, err error) {
  76. o := orm.NewOrmUsingDB("data")
  77. sql := `SELECT * FROM base_from_smm_index `
  78. _, err = o.Raw(sql).QueryRows(&list)
  79. return
  80. }
  81. type BaseFromSmmData struct {
  82. SmmDataId int `orm:"column(smm_data_id);pk"`
  83. BaseFromSmmIndexId int
  84. IndexCode string
  85. DataTime string
  86. Value string
  87. CreateTime time.Time
  88. ModifyTime time.Time
  89. DataTimestamp int64
  90. }
  91. func AddBaseFromSmmData(item *BaseFromSmmData) (lastId int64, err error) {
  92. o := orm.NewOrmUsingDB("data")
  93. lastId, err = o.Insert(item)
  94. return
  95. }
  96. func GetBaseFromSmmDataAll() (list []*BaseFromSmmData, err error) {
  97. o := orm.NewOrmUsingDB("data")
  98. sql := `SELECT * FROM base_from_smm_data `
  99. _, err = o.Raw(sql).QueryRows(&list)
  100. return
  101. }
  102. func ModifyBaseFromSmmData(smmDataId int, value string) (err error) {
  103. o := orm.NewOrmUsingDB("data")
  104. sql := `UPDATE base_from_smm_data SET value=?,modify_time=NOW() WHERE smm_data_id=? `
  105. _, err = o.Raw(sql, value, smmDataId).Exec()
  106. return
  107. }
  108. func GetBaseFromSmmIndexBySmmCode(smmCode string) (list []*BaseFromSmmIndex, err error) {
  109. o := orm.NewOrmUsingDB("data")
  110. sql := ` SELECT * FROM base_from_smm_index WHERE interface=? `
  111. _, err = o.Raw(sql, smmCode).QueryRows(&list)
  112. return
  113. }
  114. func GetBaseFromSmmMaxOrMinDate(indexCode string) (min_date, max_date string, err error) {
  115. o := orm.NewOrmUsingDB("data")
  116. sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM base_from_smm_data WHERE index_code=? `
  117. err = o.Raw(sql, indexCode).QueryRow(&min_date, &max_date)
  118. return
  119. }
  120. func ModifyBaseFromSmmMinDateAndMaxDate(baseFromSmmIndexId int, minDate, maxDate string) (err error) {
  121. o := orm.NewOrmUsingDB("data")
  122. sql := ` UPDATE base_from_smm_index SET start_date=?,end_date=?,modify_time=NOW() WHERE base_from_smm_index_id=? `
  123. _, err = o.Raw(sql, minDate, maxDate, baseFromSmmIndexId).Exec()
  124. return
  125. }
  126. func GetBaseFromSmmDataAllByIndexCode(indexCode string) (list []*BaseFromSmmData, err error) {
  127. o := orm.NewOrmUsingDB("data")
  128. sql := `SELECT * FROM base_from_smm_data WHERE index_code=? `
  129. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  130. return
  131. }
  132. func GetBaseFromSmmIndexByIndexCode(indexCode string) (list *BaseFromSmmIndex, err error) {
  133. o := orm.NewOrmUsingDB("data")
  134. sql := ` SELECT * FROM base_from_smm_index WHERE index_code=? `
  135. err = o.Raw(sql, indexCode).QueryRow(&list)
  136. return
  137. }
  138. // GetBaseFromSmmIndexCountByClassifyIds 获取分类下指标的个数
  139. func GetBaseFromSmmIndexCountByClassifyIds(classifyIds []int) (count int, err error) {
  140. o := orm.NewOrmUsingDB("data")
  141. num := len(classifyIds)
  142. if num <= 0 {
  143. return
  144. }
  145. sql := `SELECT COUNT(1) AS count FROM base_from_smm_index WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  146. err = o.Raw(sql, classifyIds).QueryRow(&count)
  147. return
  148. }
  149. // GetBaseFromSmmIndexByClassifyId 获取根据分类查询指标
  150. func GetBaseFromSmmIndexByClassifyId(classifyId int) (list []*BaseFromSmmIndex, err error) {
  151. o := orm.NewOrmUsingDB("data")
  152. sql := `SELECT * FROM base_from_smm_index WHERE classify_id =? ORDER BY sort ASC, base_from_smm_index_id ASC `
  153. _, err = o.Raw(sql, classifyId).QueryRows(&list)
  154. return
  155. }
  156. // GetSmmIndexAll 用于分类展示
  157. func GetSmmIndexAll() (items []*BaseFromSmmClassifyItems, err error) {
  158. o := orm.NewOrmUsingDB("data")
  159. sql := ` SELECT base_from_smm_index_id, classify_id, index_name as classify_name, index_code as base_from_smm_index_code
  160. FROM base_from_smm_index ORDER BY sort ASC, base_from_smm_index_id ASC `
  161. _, err = o.Raw(sql).QueryRows(&items)
  162. return
  163. }
  164. // GetBaseFromSmmIndexByIndexId 根据指标id获取指标信息
  165. func GetBaseFromSmmIndexByIndexId(indexId int) (item *BaseFromSmmIndex, err error) {
  166. o := orm.NewOrmUsingDB("data")
  167. sql := `SELECT * FROM base_from_smm_index WHERE base_from_smm_index_id=? `
  168. err = o.Raw(sql, indexId).QueryRow(&item)
  169. return
  170. }
  171. // GetBaseFromSmmIndexMaxSortByClassifyId 根据指标id获取指标信息
  172. func GetBaseFromSmmIndexMaxSortByClassifyId(classifyId int) (sort int, err error) {
  173. o := orm.NewOrmUsingDB("data")
  174. sql := `SELECT max(sort) FROM base_from_smm_index WHERE classify_id=? `
  175. err = o.Raw(sql, classifyId).QueryRow(&sort)
  176. return
  177. }
  178. // GetBaseFromSmmIndexMinSortByClassifyId 获取最小不等于0的排序
  179. func GetBaseFromSmmIndexMinSortByClassifyId(classifyId int) (sort int, err error) {
  180. o := orm.NewOrmUsingDB("data")
  181. sql := `SELECT min(sort) FROM base_from_smm_index WHERE classify_id=? and sort <> 0 `
  182. err = o.Raw(sql, classifyId).QueryRow(&sort)
  183. return
  184. }
  185. type BaseFromSmmIndexType struct {
  186. Type2 string `orm:"column(type_2)"`
  187. Type3 string `orm:"column(type_3)"`
  188. }
  189. // GetBaseFromSmmIndexType 获取历史指标信息类型
  190. func GetBaseFromSmmIndexType() (list []BaseFromSmmIndexType, err error) {
  191. o := orm.NewOrmUsingDB("data")
  192. sql := `SELECT type_2, type_3 from base_from_smm_index where type_2 <> "" GROUP BY type_2, type_3`
  193. _, err = o.Raw(sql).QueryRows(&list)
  194. return
  195. }
  196. // MoveBaseFromSmmIndex 移动指标分类
  197. func MoveBaseFromSmmIndex(chartInfoId, classifyId int) (err error) {
  198. o := orm.NewOrmUsingDB("data")
  199. sql := ` UPDATE base_from_smm_index
  200. SET
  201. classify_id = ?, modify_time=NOW()
  202. WHERE base_from_smm_index_id = ?`
  203. _, err = o.Raw(sql, classifyId, chartInfoId).Exec()
  204. return
  205. }
  206. // UpdateBaseFromSmmIndexByClassifyId 根据指标id更新排序
  207. func UpdateBaseFromSmmIndexByClassifyId(classifyId, nowSort, prevIndexInfoId int, updateSort string) (err error) {
  208. o := orm.NewOrmUsingDB("data")
  209. sql := ` update base_from_smm_index set sort = ` + updateSort + ` WHERE classify_id=? AND `
  210. if prevIndexInfoId > 0 {
  211. sql += ` ( sort > ? or (base_from_smm_index_id > ` + fmt.Sprint(prevIndexInfoId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
  212. }
  213. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  214. return
  215. }
  216. // MoveUpSmmIndexBySort 往上移动
  217. func MoveUpSmmIndexBySort(classifyId, nextSort, currentSort int) (err error) {
  218. o := orm.NewOrmUsingDB("data")
  219. sql := `update base_from_smm_index set sort = sort + 1 where classify_id=? and sort >= ? and sort< ?`
  220. _, err = o.Raw(sql, classifyId, nextSort, currentSort).Exec()
  221. return
  222. }
  223. // MoveDownSmmIndexBySort 往下移动
  224. func MoveDownSmmIndexBySort(classifyId, prevSort, currentSort int) (err error) {
  225. o := orm.NewOrmUsingDB("data")
  226. sql := `update base_from_smm_index set sort = sort - 1 where classify_id=? and sort <= ? and sort> ? `
  227. _, err = o.Raw(sql, classifyId, prevSort, currentSort).Exec()
  228. return
  229. }
  230. // GetFirstBaseFromSmmIndexByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  231. func GetFirstBaseFromSmmIndexByClassifyId(classifyId int) (item *BaseFromSmmIndex, err error) {
  232. o := orm.NewOrmUsingDB("data")
  233. sql := ` SELECT * FROM base_from_smm_index WHERE classify_id=? order by sort asc,base_from_smm_index_id asc limit 1`
  234. err = o.Raw(sql, classifyId).QueryRow(&item)
  235. return
  236. }
  237. // Update 更新Smm指标基础信息
  238. func (item *BaseFromSmmIndex) Update(cols []string) (err error) {
  239. o := orm.NewOrmUsingDB("data")
  240. _, err = o.Update(item, cols...)
  241. return
  242. }
  243. // DeleteBaseFromSmmIndexById 根据指标id删除对应的指标、指标数据
  244. func DeleteBaseFromSmmIndexById(smmIndexId int) (err error) {
  245. o := orm.NewOrmUsingDB("data")
  246. to, err := o.Begin()
  247. if err != nil {
  248. return
  249. }
  250. defer func() {
  251. if err != nil {
  252. _ = to.Rollback()
  253. } else {
  254. _ = to.Commit()
  255. }
  256. }()
  257. //删除指标
  258. sql := `DELETE FROM base_from_smm_index WHERE base_from_smm_index_id = ? `
  259. _, err = to.Raw(sql, smmIndexId).Exec()
  260. if err != nil {
  261. return
  262. }
  263. //删除指标数据
  264. sql = `DELETE FROM base_from_smm_data WHERE base_from_smm_index_id = ? `
  265. _, err = to.Raw(sql, smmIndexId).Exec()
  266. if err != nil {
  267. return
  268. }
  269. return
  270. }
  271. // EditSmmIndexInfoResp 新增指标的返回
  272. type EditSmmIndexInfoResp struct {
  273. BaseFromSmmIndexId int `description:"指标ID"`
  274. IndexCode string `description:"指标code"`
  275. }
  276. type SmmSingleDataResp struct {
  277. BaseFromSmmIndexId int
  278. ClassifyId int
  279. Interface string
  280. Name string
  281. IndexCode string
  282. IndexName string
  283. Frequency string
  284. Unit string
  285. ApiStartTime string
  286. ApiUpdateTime string
  287. StartTime string
  288. FinishTime string
  289. CreateTime string
  290. ModifyTime string
  291. Data []*SmmIndexData
  292. }