base_from_baiinfo.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package data_manage
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. type BaseFromBaiinfo struct {
  9. BaseFromBaiinfoId int `orm:"column(base_from_baiinfo_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 AddBaseFromBaiinfo(item *BaseFromBaiinfo) (lastId int64, err error) {
  34. o := orm.NewOrmUsingDB("data")
  35. lastId, err = o.Insert(item)
  36. return
  37. }
  38. func GetBaseFromBaiinfoList() (list []*BaseFromBaiinfo, err error) {
  39. o := orm.NewOrmUsingDB("data")
  40. sql := `SELECT * FROM base_from_baiinfo `
  41. _, err = o.Raw(sql).QueryRows(&list)
  42. return
  43. }
  44. func GetBaseFromBaiinfoByBaiinfoCode(baiinfoCode string) (item *BaseFromBaiinfo, err error) {
  45. o := orm.NewOrmUsingDB("data")
  46. sql := `SELECT * FROM base_from_baiinfo WHERE interface=?`
  47. err = o.Raw(sql, baiinfoCode).QueryRow(&item)
  48. return
  49. }
  50. type BaseFromBaiinfoIndex struct {
  51. BaseFromBaiinfoIndexId int `orm:"column(base_from_baiinfo_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. }
  62. func AddBaseFromBaiinfoIndex(item *BaseFromBaiinfoIndex) (lastId int64, err error) {
  63. o := orm.NewOrmUsingDB("data")
  64. lastId, err = o.Insert(item)
  65. return
  66. }
  67. func GetBaseFromBaiinfoIndex() (list []*BaseFromBaiinfoIndex, err error) {
  68. o := orm.NewOrmUsingDB("data")
  69. sql := `SELECT * FROM base_from_baiinfo_index `
  70. _, err = o.Raw(sql).QueryRows(&list)
  71. return
  72. }
  73. type BaseFromBaiinfoData struct {
  74. BaiinfoDataId int `orm:"column(baiinfo_data_id);pk"`
  75. BaseFromBaiinfoIndexId int
  76. IndexCode string
  77. DataTime string
  78. Value string
  79. CreateTime time.Time
  80. ModifyTime time.Time
  81. DataTimestamp int64
  82. }
  83. func AddBaseFromBaiinfoData(item *BaseFromBaiinfoData) (lastId int64, err error) {
  84. o := orm.NewOrmUsingDB("data")
  85. lastId, err = o.Insert(item)
  86. return
  87. }
  88. func GetBaseFromBaiinfoDataAll() (list []*BaseFromBaiinfoData, err error) {
  89. o := orm.NewOrmUsingDB("data")
  90. sql := `SELECT * FROM base_from_baiinfo_data `
  91. _, err = o.Raw(sql).QueryRows(&list)
  92. return
  93. }
  94. func ModifyBaseFromBaiinfoData(baiinfoDataId int, value string) (err error) {
  95. o := orm.NewOrmUsingDB("data")
  96. sql := `UPDATE base_from_baiinfo_data SET value=?,modify_time=NOW() WHERE baiinfo_data_id=? `
  97. _, err = o.Raw(sql, value, baiinfoDataId).Exec()
  98. return
  99. }
  100. func GetBaseFromBaiinfoIndexByBaiinfoCode(baiinfoCode string) (list []*BaseFromBaiinfoIndex, err error) {
  101. o := orm.NewOrmUsingDB("data")
  102. sql := ` SELECT * FROM base_from_baiinfo_index WHERE interface=? `
  103. _, err = o.Raw(sql, baiinfoCode).QueryRows(&list)
  104. return
  105. }
  106. func GetBaseFromBaiinfoMaxOrMinDate(indexCode string) (min_date, max_date string, err error) {
  107. o := orm.NewOrmUsingDB("data")
  108. sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM base_from_baiinfo_data WHERE index_code=? `
  109. err = o.Raw(sql, indexCode).QueryRow(&min_date, &max_date)
  110. return
  111. }
  112. func ModifyBaseFromBaiinfoMinDateAndMaxDate(baseFromBaiinfoIndexId int, minDate, maxDate string) (err error) {
  113. o := orm.NewOrmUsingDB("data")
  114. sql := ` UPDATE base_from_baiinfo_index SET start_date=?,end_date=?,modify_time=NOW() WHERE base_from_baiinfo_index_id=? `
  115. _, err = o.Raw(sql, minDate, maxDate, baseFromBaiinfoIndexId).Exec()
  116. return
  117. }
  118. func GetBaseFromBaiinfoDataAllByIndexCode(indexCode string) (list []*BaseFromBaiinfoData, err error) {
  119. o := orm.NewOrmUsingDB("data")
  120. sql := `SELECT * FROM base_from_baiinfo_data WHERE index_code=? `
  121. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  122. return
  123. }
  124. func GetBaseFromBaiinfoIndexByIndexCode(indexCode string) (list *BaseFromBaiinfoIndex, err error) {
  125. o := orm.NewOrmUsingDB("data")
  126. sql := ` SELECT * FROM base_from_baiinfo_index WHERE index_code=? `
  127. err = o.Raw(sql, indexCode).QueryRow(&list)
  128. return
  129. }
  130. // GetBaseFromBaiinfoIndexCountByClassifyIds 获取分类下指标的个数
  131. func GetBaseFromBaiinfoIndexCountByClassifyIds(classifyIds []int) (count int, err error) {
  132. o := orm.NewOrmUsingDB("data")
  133. num := len(classifyIds)
  134. if num <= 0 {
  135. return
  136. }
  137. sql := `SELECT COUNT(1) AS count FROM base_from_baiinfo_index WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  138. err = o.Raw(sql, classifyIds).QueryRow(&count)
  139. return
  140. }
  141. // GetBaseFromBaiinfoIndexByClassifyId 获取根据分类查询指标
  142. func GetBaseFromBaiinfoIndexByClassifyId(classifyId int) (list []*BaseFromBaiinfoIndex, err error) {
  143. o := orm.NewOrmUsingDB("data")
  144. sql := `SELECT * FROM base_from_baiinfo_index WHERE classify_id =? ORDER BY sort ASC, base_from_baiinfo_index_id ASC `
  145. _, err = o.Raw(sql, classifyId).QueryRows(&list)
  146. return
  147. }
  148. //// GetBaiinfoIndexAll 用于分类展示
  149. //func GetBaiinfoIndexAll() (items []*BaseFromBaiinfoClassifyItems, err error) {
  150. // o := orm.NewOrmUsingDB("data")
  151. // sql := ` SELECT base_from_baiinfo_index_id, classify_id, index_name as classify_name, index_code as base_from_baiinfo_index_code
  152. // FROM base_from_baiinfo_index ORDER BY sort ASC, base_from_baiinfo_index_id ASC `
  153. // _, err = o.Raw(sql).QueryRows(&items)
  154. // return
  155. //}
  156. // GetBaseFromBaiinfoIndexByIndexId 根据指标id获取指标信息
  157. func GetBaseFromBaiinfoIndexByIndexId(indexId int) (item *BaseFromBaiinfoIndex, err error) {
  158. o := orm.NewOrmUsingDB("data")
  159. sql := `SELECT * FROM base_from_baiinfo_index WHERE base_from_baiinfo_index_id=? `
  160. err = o.Raw(sql, indexId).QueryRow(&item)
  161. return
  162. }
  163. // GetBaseFromBaiinfoIndexMaxSortByClassifyId 根据指标id获取指标信息
  164. func GetBaseFromBaiinfoIndexMaxSortByClassifyId(classifyId int) (sort int, err error) {
  165. o := orm.NewOrmUsingDB("data")
  166. sql := `SELECT max(sort) FROM base_from_baiinfo_index WHERE classify_id=? `
  167. err = o.Raw(sql, classifyId).QueryRow(&sort)
  168. return
  169. }
  170. // GetBaseFromBaiinfoIndexMinSortByClassifyId 获取最小不等于0的排序
  171. func GetBaseFromBaiinfoIndexMinSortByClassifyId(classifyId int) (sort int, err error) {
  172. o := orm.NewOrmUsingDB("data")
  173. sql := `SELECT min(sort) FROM base_from_baiinfo_index WHERE classify_id=? and sort <> 0 `
  174. err = o.Raw(sql, classifyId).QueryRow(&sort)
  175. return
  176. }
  177. type BaseFromBaiinfoIndexType struct {
  178. Type2 string `orm:"column(type_2)"`
  179. Type3 string `orm:"column(type_3)"`
  180. }
  181. // GetBaseFromBaiinfoIndexType 获取历史指标信息类型
  182. func GetBaseFromBaiinfoIndexType() (list []BaseFromBaiinfoIndexType, err error) {
  183. o := orm.NewOrmUsingDB("data")
  184. sql := `SELECT type_2, type_3 from base_from_baiinfo_index where type_2 <> "" GROUP BY type_2, type_3`
  185. _, err = o.Raw(sql).QueryRows(&list)
  186. return
  187. }
  188. // MoveBaseFromBaiinfoIndex 移动指标分类
  189. func MoveBaseFromBaiinfoIndex(chartInfoId, classifyId int) (err error) {
  190. o := orm.NewOrmUsingDB("data")
  191. sql := ` UPDATE base_from_baiinfo_index
  192. SET
  193. classify_id = ?, modify_time=NOW()
  194. WHERE base_from_baiinfo_index_id = ?`
  195. _, err = o.Raw(sql, classifyId, chartInfoId).Exec()
  196. return
  197. }
  198. // UpdateBaseFromBaiinfoIndexByClassifyId 根据指标id更新排序
  199. func UpdateBaseFromBaiinfoIndexByClassifyId(classifyId, nowSort, prevIndexInfoId int, updateSort string) (err error) {
  200. o := orm.NewOrmUsingDB("data")
  201. sql := ` update base_from_baiinfo_index set sort = ` + updateSort + ` WHERE classify_id=? AND `
  202. if prevIndexInfoId > 0 {
  203. sql += ` ( sort > ? or (base_from_baiinfo_index_id > ` + fmt.Sprint(prevIndexInfoId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
  204. }
  205. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  206. return
  207. }
  208. // MoveUpBaiinfoIndexBySort 往上移动
  209. func MoveUpBaiinfoIndexBySort(classifyId, nextSort, currentSort int) (err error) {
  210. o := orm.NewOrmUsingDB("data")
  211. sql := `update base_from_baiinfo_index set sort = sort + 1 where classify_id=? and sort >= ? and sort< ?`
  212. _, err = o.Raw(sql, classifyId, nextSort, currentSort).Exec()
  213. return
  214. }
  215. // MoveDownBaiinfoIndexBySort 往下移动
  216. func MoveDownBaiinfoIndexBySort(classifyId, prevSort, currentSort int) (err error) {
  217. o := orm.NewOrmUsingDB("data")
  218. sql := `update base_from_baiinfo_index set sort = sort - 1 where classify_id=? and sort <= ? and sort> ? `
  219. _, err = o.Raw(sql, classifyId, prevSort, currentSort).Exec()
  220. return
  221. }
  222. // GetFirstBaseFromBaiinfoIndexByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  223. func GetFirstBaseFromBaiinfoIndexByClassifyId(classifyId int) (item *BaseFromBaiinfoIndex, err error) {
  224. o := orm.NewOrmUsingDB("data")
  225. sql := ` SELECT * FROM base_from_baiinfo_index WHERE classify_id=? order by sort asc,base_from_baiinfo_index_id asc limit 1`
  226. err = o.Raw(sql, classifyId).QueryRow(&item)
  227. return
  228. }
  229. // Update 更新Baiinfo指标基础信息
  230. func (item *BaseFromBaiinfoIndex) Update(cols []string) (err error) {
  231. o := orm.NewOrmUsingDB("data")
  232. _, err = o.Update(item, cols...)
  233. return
  234. }
  235. // DeleteBaseFromBaiinfoIndexById 根据指标id删除对应的指标、指标数据
  236. func DeleteBaseFromBaiinfoIndexById(baiinfoIndexId int) (err error) {
  237. o := orm.NewOrmUsingDB("data")
  238. to, err := o.Begin()
  239. if err != nil {
  240. return
  241. }
  242. defer func() {
  243. if err != nil {
  244. _ = to.Rollback()
  245. } else {
  246. _ = to.Commit()
  247. }
  248. }()
  249. //删除指标
  250. sql := `DELETE FROM base_from_baiinfo_index WHERE base_from_baiinfo_index_id = ? `
  251. _, err = to.Raw(sql, baiinfoIndexId).Exec()
  252. if err != nil {
  253. return
  254. }
  255. //删除指标数据
  256. sql = `DELETE FROM base_from_baiinfo_data WHERE base_from_baiinfo_index_id = ? `
  257. _, err = to.Raw(sql, baiinfoIndexId).Exec()
  258. if err != nil {
  259. return
  260. }
  261. return
  262. }
  263. // EditBaiinfoIndexInfoResp 新增指标的返回
  264. type EditBaiinfoIndexInfoResp struct {
  265. BaseFromBaiinfoIndexId int `description:"指标ID"`
  266. IndexCode string `description:"指标code"`
  267. }
  268. //type BaiinfoSingleDataResp struct {
  269. // BaseFromBaiinfoIndexId int
  270. // ClassifyId int
  271. // Interface string
  272. // Name string
  273. // IndexCode string
  274. // IndexName string
  275. // Frequency string
  276. // Unit string
  277. // ApiStartTime string
  278. // ApiUpdateTime string
  279. // StartTime string
  280. // FinishTime string
  281. // CreateTime string
  282. // ModifyTime string
  283. // Data []*BaiinfoIndexData
  284. //}