base_from_sci.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. type BaseFromSci struct {
  9. BaseFromSciId int `orm:"column(base_from_sci_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 AddBaseFromSci(item *BaseFromSci) (lastId int64, err error) {
  34. o := orm.NewOrmUsingDB("data")
  35. lastId, err = o.Insert(item)
  36. return
  37. }
  38. func GetBaseFromSciList() (list []*BaseFromSci, err error) {
  39. o := orm.NewOrmUsingDB("data")
  40. sql := `SELECT * FROM base_from_sci `
  41. _, err = o.Raw(sql).QueryRows(&list)
  42. return
  43. }
  44. func GetBaseFromSciBySciCode(sciCode string) (item *BaseFromSci, err error) {
  45. o := orm.NewOrmUsingDB("data")
  46. sql := `SELECT * FROM base_from_sci WHERE interface=?`
  47. err = o.Raw(sql, sciCode).QueryRow(&item)
  48. return
  49. }
  50. type BaseFromSciIndex struct {
  51. BaseFromSciIndexId int `orm:"column(base_from_sci_index_id);pk"`
  52. ClassifyId int
  53. Interface string
  54. IndexCode string
  55. IndexName string
  56. Frequency string
  57. Unit string
  58. Sort int
  59. CreateTime time.Time
  60. ModifyTime time.Time
  61. FilePath string
  62. TerminalCode string `description:"终端编码"`
  63. }
  64. func AddBaseFromSciIndex(item *BaseFromSciIndex) (lastId int64, err error) {
  65. o := orm.NewOrmUsingDB("data")
  66. lastId, err = o.Insert(item)
  67. return
  68. }
  69. func GetBaseFromSciIndex() (list []*BaseFromSciIndex, err error) {
  70. o := orm.NewOrmUsingDB("data")
  71. sql := `SELECT * FROM base_from_sci_index `
  72. _, err = o.Raw(sql).QueryRows(&list)
  73. return
  74. }
  75. type BaseFromSciData struct {
  76. SciDataId int `orm:"column(sci_data_id);pk"`
  77. BaseFromSciIndexId int
  78. IndexCode string
  79. DataTime string
  80. Value string
  81. CreateTime time.Time
  82. ModifyTime time.Time
  83. DataTimestamp int64
  84. }
  85. func AddBaseFromSciData(item *BaseFromSciData) (lastId int64, err error) {
  86. o := orm.NewOrmUsingDB("data")
  87. lastId, err = o.Insert(item)
  88. return
  89. }
  90. func GetBaseFromSciDataAll() (list []*BaseFromSciData, err error) {
  91. o := orm.NewOrmUsingDB("data")
  92. sql := `SELECT * FROM base_from_sci_data `
  93. _, err = o.Raw(sql).QueryRows(&list)
  94. return
  95. }
  96. func ModifyBaseFromSciData(sciDataId int, value string) (err error) {
  97. o := orm.NewOrmUsingDB("data")
  98. sql := `UPDATE base_from_sci_data SET value=?,modify_time=NOW() WHERE sci_data_id=? `
  99. _, err = o.Raw(sql, value, sciDataId).Exec()
  100. return
  101. }
  102. func GetBaseFromSciIndexBySciCode(sciCode string) (list []*BaseFromSciIndex, err error) {
  103. o := orm.NewOrmUsingDB("data")
  104. sql := ` SELECT * FROM base_from_sci_index WHERE interface=? `
  105. _, err = o.Raw(sql, sciCode).QueryRows(&list)
  106. return
  107. }
  108. func GetBaseFromSciMaxOrMinDate(indexCode string) (min_date, max_date string, err error) {
  109. o := orm.NewOrmUsingDB("data")
  110. sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM base_from_sci_data WHERE index_code=? `
  111. err = o.Raw(sql, indexCode).QueryRow(&min_date, &max_date)
  112. return
  113. }
  114. func ModifyBaseFromSciMinDateAndMaxDate(baseFromSciIndexId int, minDate, maxDate string) (err error) {
  115. o := orm.NewOrmUsingDB("data")
  116. sql := ` UPDATE base_from_sci_index SET start_date=?,end_date=?,modify_time=NOW() WHERE base_from_sci_index_id=? `
  117. _, err = o.Raw(sql, minDate, maxDate, baseFromSciIndexId).Exec()
  118. return
  119. }
  120. func GetBaseFromSciDataAllByIndexCode(indexCode string) (list []*BaseFromSciData, err error) {
  121. o := orm.NewOrmUsingDB("data")
  122. sql := `SELECT * FROM base_from_sci_data WHERE index_code=? `
  123. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  124. return
  125. }
  126. func GetBaseFromSciIndexByIndexCode(indexCode string) (list *BaseFromSciIndex, err error) {
  127. o := orm.NewOrmUsingDB("data")
  128. sql := ` SELECT * FROM base_from_sci_index WHERE index_code=? `
  129. err = o.Raw(sql, indexCode).QueryRow(&list)
  130. return
  131. }
  132. // GetBaseFromSciIndexCountByClassifyIds 获取分类下指标的个数
  133. func GetBaseFromSciIndexCountByClassifyIds(classifyIds []int) (count int, err error) {
  134. o := orm.NewOrmUsingDB("data")
  135. num := len(classifyIds)
  136. if num <= 0 {
  137. return
  138. }
  139. sql := `SELECT COUNT(1) AS count FROM base_from_sci_index WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  140. err = o.Raw(sql, classifyIds).QueryRow(&count)
  141. return
  142. }
  143. // GetBaseFromSciIndexByClassifyId 获取根据分类查询指标
  144. func GetBaseFromSciIndexByClassifyId(classifyId int) (list []*BaseFromSciIndex, err error) {
  145. o := orm.NewOrmUsingDB("data")
  146. sql := `SELECT * FROM base_from_sci_index WHERE classify_id =? ORDER BY sort ASC, base_from_sci_index_id ASC `
  147. _, err = o.Raw(sql, classifyId).QueryRows(&list)
  148. return
  149. }
  150. // GetSciIndexAll 用于分类展示
  151. func GetSciIndexAll() (items []*BaseFromSciClassifyItems, err error) {
  152. o := orm.NewOrmUsingDB("data")
  153. sql := ` SELECT base_from_sci_index_id, classify_id, index_name as classify_name, index_code as base_from_sci_index_code
  154. FROM base_from_sci_index ORDER BY sort ASC, base_from_sci_index_id ASC `
  155. _, err = o.Raw(sql).QueryRows(&items)
  156. return
  157. }
  158. // GetBaseFromSciIndexByIndexId 根据指标id获取指标信息
  159. func GetBaseFromSciIndexByIndexId(indexId int) (item *BaseFromSciIndex, err error) {
  160. o := orm.NewOrmUsingDB("data")
  161. sql := `SELECT * FROM base_from_sci_index WHERE base_from_sci_index_id=? `
  162. err = o.Raw(sql, indexId).QueryRow(&item)
  163. return
  164. }
  165. // GetBaseFromSciIndexMaxSortByClassifyId 根据指标id获取指标信息
  166. func GetBaseFromSciIndexMaxSortByClassifyId(classifyId int) (sort int, err error) {
  167. o := orm.NewOrmUsingDB("data")
  168. sql := `SELECT max(sort) FROM base_from_sci_index WHERE classify_id=? `
  169. err = o.Raw(sql, classifyId).QueryRow(&sort)
  170. return
  171. }
  172. // GetBaseFromSciIndexMinSortByClassifyId 获取最小不等于0的排序
  173. func GetBaseFromSciIndexMinSortByClassifyId(classifyId int) (sort int, err error) {
  174. o := orm.NewOrmUsingDB("data")
  175. sql := `SELECT min(sort) FROM base_from_sci_index WHERE classify_id=? and sort <> 0 `
  176. err = o.Raw(sql, classifyId).QueryRow(&sort)
  177. return
  178. }
  179. type BaseFromSciIndexType struct {
  180. Type2 string `orm:"column(type_2)"`
  181. Type3 string `orm:"column(type_3)"`
  182. }
  183. // GetBaseFromSciIndexType 获取历史指标信息类型
  184. func GetBaseFromSciIndexType() (list []BaseFromSciIndexType, err error) {
  185. o := orm.NewOrmUsingDB("data")
  186. sql := `SELECT type_2, type_3 from base_from_sci_index where type_2 <> "" GROUP BY type_2, type_3`
  187. _, err = o.Raw(sql).QueryRows(&list)
  188. return
  189. }
  190. // MoveBaseFromSciIndex 移动指标分类
  191. func MoveBaseFromSciIndex(chartInfoId, classifyId int) (err error) {
  192. o := orm.NewOrmUsingDB("data")
  193. sql := ` UPDATE base_from_sci_index
  194. SET
  195. classify_id = ?, modify_time=NOW()
  196. WHERE base_from_sci_index_id = ?`
  197. _, err = o.Raw(sql, classifyId, chartInfoId).Exec()
  198. return
  199. }
  200. // UpdateBaseFromSciIndexByClassifyId 根据指标id更新排序
  201. func UpdateBaseFromSciIndexByClassifyId(classifyId, nowSort, prevIndexInfoId int, updateSort string) (err error) {
  202. o := orm.NewOrmUsingDB("data")
  203. sql := ` update base_from_sci_index set sort = ` + updateSort + ` WHERE classify_id=? AND `
  204. if prevIndexInfoId > 0 {
  205. sql += ` ( sort > ? or (base_from_sci_index_id > ` + fmt.Sprint(prevIndexInfoId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
  206. }
  207. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  208. return
  209. }
  210. // MoveUpSciIndexBySort 往上移动
  211. func MoveUpSciIndexBySort(classifyId, nextSort, currentSort int) (err error) {
  212. o := orm.NewOrmUsingDB("data")
  213. sql := `update base_from_sci_index set sort = sort + 1 where classify_id=? and sort >= ? and sort< ?`
  214. _, err = o.Raw(sql, classifyId, nextSort, currentSort).Exec()
  215. return
  216. }
  217. // MoveDownSciIndexBySort 往下移动
  218. func MoveDownSciIndexBySort(classifyId, prevSort, currentSort int) (err error) {
  219. o := orm.NewOrmUsingDB("data")
  220. sql := `update base_from_sci_index set sort = sort - 1 where classify_id=? and sort <= ? and sort> ? `
  221. _, err = o.Raw(sql, classifyId, prevSort, currentSort).Exec()
  222. return
  223. }
  224. // GetFirstBaseFromSciIndexByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  225. func GetFirstBaseFromSciIndexByClassifyId(classifyId int) (item *BaseFromSciIndex, err error) {
  226. o := orm.NewOrmUsingDB("data")
  227. sql := ` SELECT * FROM base_from_sci_index WHERE classify_id=? order by sort asc,base_from_sci_index_id asc limit 1`
  228. err = o.Raw(sql, classifyId).QueryRow(&item)
  229. return
  230. }
  231. // Update 更新Sci指标基础信息
  232. func (item *BaseFromSciIndex) Update(cols []string) (err error) {
  233. o := orm.NewOrmUsingDB("data")
  234. _, err = o.Update(item, cols...)
  235. return
  236. }
  237. // DeleteBaseFromSciIndexById 根据指标id删除对应的指标、指标数据
  238. func DeleteBaseFromSciIndexById(sciIndexId int) (err error) {
  239. o := orm.NewOrmUsingDB("data")
  240. to, err := o.Begin()
  241. if err != nil {
  242. return
  243. }
  244. defer func() {
  245. if err != nil {
  246. _ = to.Rollback()
  247. } else {
  248. _ = to.Commit()
  249. }
  250. }()
  251. //删除指标
  252. sql := `DELETE FROM base_from_sci_index WHERE base_from_sci_index_id = ? `
  253. _, err = to.Raw(sql, sciIndexId).Exec()
  254. if err != nil {
  255. return
  256. }
  257. //删除指标数据
  258. sql = `DELETE FROM base_from_sci_data WHERE base_from_sci_index_id = ? `
  259. _, err = to.Raw(sql, sciIndexId).Exec()
  260. if err != nil {
  261. return
  262. }
  263. return
  264. }
  265. // EditSciIndexInfoResp 新增指标的返回
  266. type EditSciIndexInfoResp struct {
  267. BaseFromSciIndexId int `description:"指标ID"`
  268. IndexCode string `description:"指标code"`
  269. }
  270. type SciSingleDataResp struct {
  271. BaseFromSciIndexId int
  272. ClassifyId int
  273. Interface string
  274. Name string
  275. IndexCode string
  276. IndexName string
  277. Frequency string
  278. Unit string
  279. ApiStartTime string
  280. ApiUpdateTime string
  281. StartTime string
  282. FinishTime string
  283. CreateTime string
  284. ModifyTime string
  285. Data []*SciIndexData
  286. }