base_from_sci.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hz_crm_api/utils"
  6. "time"
  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. }
  62. func AddBaseFromSciIndex(item *BaseFromSciIndex) (lastId int64, err error) {
  63. o := orm.NewOrmUsingDB("data")
  64. lastId, err = o.Insert(item)
  65. return
  66. }
  67. func GetBaseFromSciIndex() (list []*BaseFromSciIndex, err error) {
  68. o := orm.NewOrmUsingDB("data")
  69. sql := `SELECT * FROM base_from_sci_index `
  70. _, err = o.Raw(sql).QueryRows(&list)
  71. return
  72. }
  73. type BaseFromSciData struct {
  74. SciDataId int `orm:"column(sci_data_id);pk"`
  75. BaseFromSciIndexId int
  76. IndexCode string
  77. DataTime string
  78. Value string
  79. CreateTime time.Time
  80. ModifyTime time.Time
  81. DataTimestamp int64
  82. }
  83. func AddBaseFromSciData(item *BaseFromSciData) (lastId int64, err error) {
  84. o := orm.NewOrmUsingDB("data")
  85. lastId, err = o.Insert(item)
  86. return
  87. }
  88. func GetBaseFromSciDataAll() (list []*BaseFromSciData, err error) {
  89. o := orm.NewOrmUsingDB("data")
  90. sql := `SELECT * FROM base_from_sci_data `
  91. _, err = o.Raw(sql).QueryRows(&list)
  92. return
  93. }
  94. func ModifyBaseFromSciData(sciDataId int, value string) (err error) {
  95. o := orm.NewOrmUsingDB("data")
  96. sql := `UPDATE base_from_sci_data SET value=?,modify_time=NOW() WHERE sci_data_id=? `
  97. _, err = o.Raw(sql, value, sciDataId).Exec()
  98. return
  99. }
  100. func GetBaseFromSciIndexBySciCode(sciCode string) (list []*BaseFromSciIndex, err error) {
  101. o := orm.NewOrmUsingDB("data")
  102. sql := ` SELECT * FROM base_from_sci_index WHERE interface=? `
  103. _, err = o.Raw(sql, sciCode).QueryRows(&list)
  104. return
  105. }
  106. func GetBaseFromSciMaxOrMinDate(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_sci_data WHERE index_code=? `
  109. err = o.Raw(sql, indexCode).QueryRow(&min_date, &max_date)
  110. return
  111. }
  112. func ModifyBaseFromSciMinDateAndMaxDate(baseFromSciIndexId int, minDate, maxDate string) (err error) {
  113. o := orm.NewOrmUsingDB("data")
  114. sql := ` UPDATE base_from_sci_index SET start_date=?,end_date=?,modify_time=NOW() WHERE base_from_sci_index_id=? `
  115. _, err = o.Raw(sql, minDate, maxDate, baseFromSciIndexId).Exec()
  116. return
  117. }
  118. func GetBaseFromSciDataAllByIndexCode(indexCode string) (list []*BaseFromSciData, err error) {
  119. o := orm.NewOrmUsingDB("data")
  120. sql := `SELECT * FROM base_from_sci_data WHERE index_code=? `
  121. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  122. return
  123. }
  124. func GetBaseFromSciIndexByIndexCode(indexCode string) (list *BaseFromSciIndex, err error) {
  125. o := orm.NewOrmUsingDB("data")
  126. sql := ` SELECT * FROM base_from_sci_index WHERE index_code=? `
  127. err = o.Raw(sql, indexCode).QueryRow(&list)
  128. return
  129. }
  130. // GetBaseFromSciIndexCountByClassifyIds 获取分类下指标的个数
  131. func GetBaseFromSciIndexCountByClassifyIds(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_sci_index WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  138. err = o.Raw(sql, classifyIds).QueryRow(&count)
  139. return
  140. }
  141. // GetBaseFromSciIndexByClassifyId 获取根据分类查询指标
  142. func GetBaseFromSciIndexByClassifyId(classifyId int) (list []*BaseFromSciIndex, err error) {
  143. o := orm.NewOrmUsingDB("data")
  144. sql := `SELECT * FROM base_from_sci_index WHERE classify_id =? ORDER BY sort ASC, base_from_sci_index_id ASC `
  145. _, err = o.Raw(sql, classifyId).QueryRows(&list)
  146. return
  147. }
  148. // GetSciIndexAll 用于分类展示
  149. func GetSciIndexAll() (items []*BaseFromSciClassifyItems, err error) {
  150. o := orm.NewOrmUsingDB("data")
  151. sql := ` SELECT base_from_sci_index_id, classify_id, index_name as classify_name, index_code as base_from_sci_index_code
  152. FROM base_from_sci_index ORDER BY sort ASC, base_from_sci_index_id ASC `
  153. _, err = o.Raw(sql).QueryRows(&items)
  154. return
  155. }
  156. // GetBaseFromSciIndexByIndexId 根据指标id获取指标信息
  157. func GetBaseFromSciIndexByIndexId(indexId int) (item *BaseFromSciIndex, err error) {
  158. o := orm.NewOrmUsingDB("data")
  159. sql := `SELECT * FROM base_from_sci_index WHERE base_from_sci_index_id=? `
  160. err = o.Raw(sql, indexId).QueryRow(&item)
  161. return
  162. }
  163. // GetBaseFromSciIndexMaxSortByClassifyId 根据指标id获取指标信息
  164. func GetBaseFromSciIndexMaxSortByClassifyId(classifyId int) (sort int, err error) {
  165. o := orm.NewOrmUsingDB("data")
  166. sql := `SELECT max(sort) FROM base_from_sci_index WHERE classify_id=? `
  167. err = o.Raw(sql, classifyId).QueryRow(&sort)
  168. return
  169. }
  170. // GetBaseFromSciIndexMinSortByClassifyId 获取最小不等于0的排序
  171. func GetBaseFromSciIndexMinSortByClassifyId(classifyId int) (sort int, err error) {
  172. o := orm.NewOrmUsingDB("data")
  173. sql := `SELECT min(sort) FROM base_from_sci_index WHERE classify_id=? and sort <> 0 `
  174. err = o.Raw(sql, classifyId).QueryRow(&sort)
  175. return
  176. }
  177. type BaseFromSciIndexType struct {
  178. Type2 string `orm:"column(type_2)"`
  179. Type3 string `orm:"column(type_3)"`
  180. }
  181. // GetBaseFromSciIndexType 获取历史指标信息类型
  182. func GetBaseFromSciIndexType() (list []BaseFromSciIndexType, err error) {
  183. o := orm.NewOrmUsingDB("data")
  184. sql := `SELECT type_2, type_3 from base_from_sci_index where type_2 <> "" GROUP BY type_2, type_3`
  185. _, err = o.Raw(sql).QueryRows(&list)
  186. return
  187. }
  188. // MoveBaseFromSciIndex 移动指标分类
  189. func MoveBaseFromSciIndex(chartInfoId, classifyId int) (err error) {
  190. o := orm.NewOrmUsingDB("data")
  191. sql := ` UPDATE base_from_sci_index
  192. SET
  193. classify_id = ?, modify_time=NOW()
  194. WHERE base_from_sci_index_id = ?`
  195. _, err = o.Raw(sql, classifyId, chartInfoId).Exec()
  196. return
  197. }
  198. // UpdateBaseFromSciIndexByClassifyId 根据指标id更新排序
  199. func UpdateBaseFromSciIndexByClassifyId(classifyId, nowSort, prevIndexInfoId int, updateSort string) (err error) {
  200. o := orm.NewOrmUsingDB("data")
  201. sql := ` update base_from_sci_index set sort = ` + updateSort + ` WHERE classify_id=? AND `
  202. if prevIndexInfoId > 0 {
  203. sql += ` ( sort > ? or (base_from_sci_index_id > ` + fmt.Sprint(prevIndexInfoId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
  204. }
  205. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  206. return
  207. }
  208. // MoveUpSciIndexBySort 往上移动
  209. func MoveUpSciIndexBySort(classifyId, nextSort, currentSort int) (err error) {
  210. o := orm.NewOrmUsingDB("data")
  211. sql := `update base_from_sci_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. // MoveDownSciIndexBySort 往下移动
  216. func MoveDownSciIndexBySort(classifyId, prevSort, currentSort int) (err error) {
  217. o := orm.NewOrmUsingDB("data")
  218. sql := `update base_from_sci_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. // GetFirstBaseFromSciIndexByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  223. func GetFirstBaseFromSciIndexByClassifyId(classifyId int) (item *BaseFromSciIndex, err error) {
  224. o := orm.NewOrmUsingDB("data")
  225. sql := ` SELECT * FROM base_from_sci_index WHERE classify_id=? order by sort asc,base_from_sci_index_id asc limit 1`
  226. err = o.Raw(sql, classifyId).QueryRow(&item)
  227. return
  228. }
  229. // Update 更新Sci指标基础信息
  230. func (item *BaseFromSciIndex) Update(cols []string) (err error) {
  231. o := orm.NewOrmUsingDB("data")
  232. _, err = o.Update(item, cols...)
  233. return
  234. }
  235. // DeleteBaseFromSciIndexById 根据指标id删除对应的指标、指标数据
  236. func DeleteBaseFromSciIndexById(sciIndexId 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_sci_index WHERE base_from_sci_index_id = ? `
  251. _, err = to.Raw(sql, sciIndexId).Exec()
  252. if err != nil {
  253. return
  254. }
  255. //删除指标数据
  256. sql = `DELETE FROM base_from_sci_data WHERE base_from_sci_index_id = ? `
  257. _, err = to.Raw(sql, sciIndexId).Exec()
  258. if err != nil {
  259. return
  260. }
  261. return
  262. }
  263. // EditSciIndexInfoResp 新增指标的返回
  264. type EditSciIndexInfoResp struct {
  265. BaseFromSciIndexId int `description:"指标ID"`
  266. IndexCode string `description:"指标code"`
  267. }
  268. type SciSingleDataResp struct {
  269. BaseFromSciIndexId 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 []*SciIndexData
  284. }