base_from_smm.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "time"
  8. )
  9. type BaseFromSmm struct {
  10. BaseFromSmmId int `orm:"column(base_from_smm_id);pk"`
  11. Aid int
  12. Name string
  13. Interface string
  14. ApiUpdate int
  15. ApiUpdateType string
  16. ApiType int
  17. ApiTypeAll string
  18. Type1 string `orm:"column(type_1)"`
  19. Type2 string `orm:"column(type_2)"`
  20. Type3 string `orm:"column(type_3)"`
  21. ApiStartTime string
  22. ApiUpdateTime string
  23. StartTime string
  24. FinishTime string
  25. AuthModule string
  26. AuthLang string
  27. ApiLabel string
  28. Enable string
  29. EditPerson string
  30. EditTime string
  31. AuthDur int
  32. AuthDurType string
  33. }
  34. func AddBaseFromSmm(item *BaseFromSmm) (lastId int64, err error) {
  35. o := orm.NewOrmUsingDB("data")
  36. lastId, err = o.Insert(item)
  37. return
  38. }
  39. func GetBaseFromSmmList() (list []*BaseFromSmm, err error) {
  40. o := orm.NewOrmUsingDB("data")
  41. sql := `SELECT * FROM base_from_smm `
  42. _, err = o.Raw(sql).QueryRows(&list)
  43. return
  44. }
  45. func GetBaseFromSmmBySmmCode(smmCode string) (item *BaseFromSmm, err error) {
  46. o := orm.NewOrmUsingDB("data")
  47. sql := `SELECT * FROM base_from_smm WHERE interface=?`
  48. err = o.Raw(sql, smmCode).QueryRow(&item)
  49. return
  50. }
  51. type BaseFromSmmIndex struct {
  52. BaseFromSmmIndexId int `orm:"column(base_from_smm_index_id);pk"`
  53. ClassifyId int
  54. Interface string
  55. Name string
  56. IndexCode string
  57. IndexName string
  58. Type1 string `orm:"column(type_1)"`
  59. Type2 string `orm:"column(type_2)"`
  60. Type3 string `orm:"column(type_3)"`
  61. Frequency string
  62. Unit string
  63. ApiStartTime string
  64. ApiUpdateTime string
  65. StartTime string
  66. FinishTime string
  67. Sort int
  68. CreateTime time.Time
  69. ModifyTime time.Time
  70. DataState string
  71. ReleaseTime string
  72. StartDate string
  73. EndDate string
  74. }
  75. func AddBaseFromSmmIndex(item *BaseFromSmmIndex) (lastId int64, err error) {
  76. o := orm.NewOrmUsingDB("data")
  77. lastId, err = o.Insert(item)
  78. return
  79. }
  80. func GetBaseFromSmmIndex() (list []*BaseFromSmmIndex, err error) {
  81. o := orm.NewOrmUsingDB("data")
  82. sql := `SELECT * FROM base_from_smm_index `
  83. _, err = o.Raw(sql).QueryRows(&list)
  84. return
  85. }
  86. type BaseFromSmmData struct {
  87. SmmDataId int `orm:"column(smm_data_id);pk"`
  88. BaseFromSmmIndexId int
  89. IndexCode string
  90. DataTime string
  91. Value string
  92. CreateTime time.Time
  93. ModifyTime time.Time
  94. DataTimestamp int64
  95. }
  96. func AddBaseFromSmmData(item *BaseFromSmmData) (lastId int64, err error) {
  97. o := orm.NewOrmUsingDB("data")
  98. lastId, err = o.Insert(item)
  99. return
  100. }
  101. func GetBaseFromSmmDataAll() (list []*BaseFromSmmData, err error) {
  102. o := orm.NewOrmUsingDB("data")
  103. sql := `SELECT * FROM base_from_smm_data `
  104. _, err = o.Raw(sql).QueryRows(&list)
  105. return
  106. }
  107. func ModifyBaseFromSmmData(smmDataId int, value string) (err error) {
  108. o := orm.NewOrmUsingDB("data")
  109. sql := `UPDATE base_from_smm_data SET value=?,modify_time=NOW() WHERE smm_data_id=? `
  110. _, err = o.Raw(sql, value, smmDataId).Exec()
  111. return
  112. }
  113. func GetBaseFromSmmIndexBySmmCode(smmCode string) (list []*BaseFromSmmIndex, err error) {
  114. o := orm.NewOrmUsingDB("data")
  115. sql := ` SELECT * FROM base_from_smm_index WHERE interface=? `
  116. _, err = o.Raw(sql, smmCode).QueryRows(&list)
  117. return
  118. }
  119. func GetBaseFromSmmMaxOrMinDate(indexCode string) (min_date, max_date string, err error) {
  120. o := orm.NewOrmUsingDB("data")
  121. sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date FROM base_from_smm_data WHERE index_code=? `
  122. err = o.Raw(sql, indexCode).QueryRow(&min_date, &max_date)
  123. return
  124. }
  125. func ModifyBaseFromSmmMinDateAndMaxDate(baseFromSmmIndexId int, minDate, maxDate string) (err error) {
  126. o := orm.NewOrmUsingDB("data")
  127. sql := ` UPDATE base_from_smm_index SET start_date=?,end_date=?,modify_time=NOW() WHERE base_from_smm_index_id=? `
  128. _, err = o.Raw(sql, minDate, maxDate, baseFromSmmIndexId).Exec()
  129. return
  130. }
  131. func GetBaseFromSmmDataAllByIndexCode(indexCode string) (list []*BaseFromSmmData, err error) {
  132. o := orm.NewOrmUsingDB("data")
  133. sql := `SELECT * FROM base_from_smm_data WHERE index_code=? `
  134. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  135. return
  136. }
  137. func GetBaseFromSmmIndexByIndexCode(indexCode string) (list *BaseFromSmmIndex, err error) {
  138. o := orm.NewOrmUsingDB("data")
  139. sql := ` SELECT * FROM base_from_smm_index WHERE index_code=? `
  140. err = o.Raw(sql, indexCode).QueryRow(&list)
  141. return
  142. }
  143. // GetBaseFromSmmIndexCountByClassifyIds 获取分类下指标的个数
  144. func GetBaseFromSmmIndexCountByClassifyIds(classifyIds []int) (count int, err error) {
  145. o := orm.NewOrmUsingDB("data")
  146. num := len(classifyIds)
  147. if num <= 0 {
  148. return
  149. }
  150. sql := `SELECT COUNT(1) AS count FROM base_from_smm_index WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  151. err = o.Raw(sql, classifyIds).QueryRow(&count)
  152. return
  153. }
  154. // GetBaseFromSmmIndexByClassifyId 获取根据分类查询指标
  155. func GetBaseFromSmmIndexByClassifyId(classifyId int) (list []*BaseFromSmmIndex, err error) {
  156. o := orm.NewOrmUsingDB("data")
  157. sql := `SELECT * FROM base_from_smm_index WHERE classify_id =? ORDER BY sort ASC, base_from_smm_index_id ASC `
  158. _, err = o.Raw(sql, classifyId).QueryRows(&list)
  159. return
  160. }
  161. // GetSmmIndexAll 用于分类展示
  162. func GetSmmIndexAll() (items []*BaseFromSmmClassifyItems, err error) {
  163. o := orm.NewOrmUsingDB("data")
  164. sql := ` SELECT base_from_smm_index_id, classify_id, index_name as classify_name, index_code as base_from_smm_index_code
  165. FROM base_from_smm_index ORDER BY sort ASC, base_from_smm_index_id ASC `
  166. _, err = o.Raw(sql).QueryRows(&items)
  167. return
  168. }
  169. // GetBaseFromSmmIndexByIndexId 根据指标id获取指标信息
  170. func GetBaseFromSmmIndexByIndexId(indexId int) (item *BaseFromSmmIndex, err error) {
  171. o := orm.NewOrmUsingDB("data")
  172. sql := `SELECT * FROM base_from_smm_index WHERE base_from_smm_index_id=? `
  173. err = o.Raw(sql, indexId).QueryRow(&item)
  174. return
  175. }
  176. // GetBaseFromSmmIndexMaxSortByClassifyId 根据指标id获取指标信息
  177. func GetBaseFromSmmIndexMaxSortByClassifyId(classifyId int) (sort int, err error) {
  178. o := orm.NewOrmUsingDB("data")
  179. sql := `SELECT max(sort) FROM base_from_smm_index WHERE classify_id=? `
  180. err = o.Raw(sql, classifyId).QueryRow(&sort)
  181. return
  182. }
  183. // GetBaseFromSmmIndexMinSortByClassifyId 获取最小不等于0的排序
  184. func GetBaseFromSmmIndexMinSortByClassifyId(classifyId int) (sort int, err error) {
  185. o := orm.NewOrmUsingDB("data")
  186. sql := `SELECT min(sort) FROM base_from_smm_index WHERE classify_id=? and sort <> 0 `
  187. err = o.Raw(sql, classifyId).QueryRow(&sort)
  188. return
  189. }
  190. type BaseFromSmmIndexType struct {
  191. Type2 string `orm:"column(type_2)"`
  192. Type3 string `orm:"column(type_3)"`
  193. }
  194. // GetBaseFromSmmIndexType 获取历史指标信息类型
  195. func GetBaseFromSmmIndexType() (list []BaseFromSmmIndexType, err error) {
  196. o := orm.NewOrmUsingDB("data")
  197. sql := `SELECT type_2, type_3 from base_from_smm_index where type_2 <> "" GROUP BY type_2, type_3`
  198. _, err = o.Raw(sql).QueryRows(&list)
  199. return
  200. }
  201. // MoveBaseFromSmmIndex 移动指标分类
  202. func MoveBaseFromSmmIndex(chartInfoId, classifyId int) (err error) {
  203. o := orm.NewOrmUsingDB("data")
  204. sql := ` UPDATE base_from_smm_index
  205. SET
  206. classify_id = ?, modify_time=NOW()
  207. WHERE base_from_smm_index_id = ?`
  208. _, err = o.Raw(sql, classifyId, chartInfoId).Exec()
  209. return
  210. }
  211. // UpdateBaseFromSmmIndexByClassifyId 根据指标id更新排序
  212. func UpdateBaseFromSmmIndexByClassifyId(classifyId, nowSort, prevIndexInfoId int, updateSort string) (err error) {
  213. o := orm.NewOrmUsingDB("data")
  214. sql := ` update base_from_smm_index set sort = ` + updateSort + ` WHERE classify_id=? AND `
  215. if prevIndexInfoId > 0 {
  216. sql += ` ( sort > ? or (base_from_smm_index_id > ` + fmt.Sprint(prevIndexInfoId) + ` and sort = ` + fmt.Sprint(nowSort) + `))`
  217. }
  218. _, err = o.Raw(sql, classifyId, nowSort).Exec()
  219. return
  220. }
  221. // MoveUpSmmIndexBySort 往上移动
  222. func MoveUpSmmIndexBySort(classifyId, nextSort, currentSort int) (err error) {
  223. o := orm.NewOrmUsingDB("data")
  224. sql := `update base_from_smm_index set sort = sort + 1 where classify_id=? and sort >= ? and sort< ?`
  225. _, err = o.Raw(sql, classifyId, nextSort, currentSort).Exec()
  226. return
  227. }
  228. // MoveDownSmmIndexBySort 往下移动
  229. func MoveDownSmmIndexBySort(classifyId, prevSort, currentSort int) (err error) {
  230. o := orm.NewOrmUsingDB("data")
  231. sql := `update base_from_smm_index set sort = sort - 1 where classify_id=? and sort <= ? and sort> ? `
  232. _, err = o.Raw(sql, classifyId, prevSort, currentSort).Exec()
  233. return
  234. }
  235. // GetFirstBaseFromSmmIndexByClassifyId 获取当前分类下,且排序数相同 的排序第一条的数据
  236. func GetFirstBaseFromSmmIndexByClassifyId(classifyId int) (item *BaseFromSmmIndex, err error) {
  237. o := orm.NewOrmUsingDB("data")
  238. sql := ` SELECT * FROM base_from_smm_index WHERE classify_id=? order by sort asc,base_from_smm_index_id asc limit 1`
  239. err = o.Raw(sql, classifyId).QueryRow(&item)
  240. return
  241. }
  242. // Update 更新Smm指标基础信息
  243. func (item *BaseFromSmmIndex) Update(cols []string) (err error) {
  244. o := orm.NewOrmUsingDB("data")
  245. _, err = o.Update(item, cols...)
  246. return
  247. }
  248. // DeleteBaseFromSmmIndexById 根据指标id删除对应的指标、指标数据
  249. func DeleteBaseFromSmmIndexById(smmIndexId int) (err error) {
  250. o := orm.NewOrmUsingDB("data")
  251. to, err := o.Begin()
  252. if err != nil {
  253. return
  254. }
  255. defer func() {
  256. if err != nil {
  257. _ = to.Rollback()
  258. } else {
  259. _ = to.Commit()
  260. }
  261. }()
  262. //删除指标
  263. sql := `DELETE FROM base_from_smm_index WHERE base_from_smm_index_id = ? `
  264. _, err = to.Raw(sql, smmIndexId).Exec()
  265. if err != nil {
  266. return
  267. }
  268. //删除指标数据
  269. sql = `DELETE FROM base_from_smm_data WHERE base_from_smm_index_id = ? `
  270. _, err = to.Raw(sql, smmIndexId).Exec()
  271. if err != nil {
  272. return
  273. }
  274. return
  275. }
  276. // EditSmmIndexInfoResp 新增指标的返回
  277. type EditSmmIndexInfoResp struct {
  278. BaseFromSmmIndexId int `description:"指标ID"`
  279. IndexCode string `description:"指标code"`
  280. }
  281. type SmmSingleDataResp struct {
  282. BaseFromSmmIndexId int
  283. ClassifyId int
  284. Interface string
  285. Name string
  286. IndexCode string
  287. IndexName string
  288. Frequency string
  289. Unit string
  290. ApiStartTime string
  291. ApiUpdateTime string
  292. StartTime string
  293. FinishTime string
  294. CreateTime string
  295. ModifyTime string
  296. Data []*SmmIndexData
  297. }
  298. func GetSmmIndexDataList(condition, sortStr string, pars []interface{}, startSize, pageSize int) (items []*BaseFromSmmIndexItem, err error) {
  299. sql := `select * FROM base_from_smm_index
  300. WHERE 1=1 `
  301. if condition != "" {
  302. sql += condition
  303. }
  304. sql += ` ORDER BY ` + sortStr
  305. sql += ` limit ?,? `
  306. o := orm.NewOrmUsingDB("data")
  307. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  308. return
  309. }
  310. func GetSmmIndexDataListCount(condition string, pars []interface{}) (total int, err error) {
  311. sql := `select count(1) FROM base_from_smm_index
  312. WHERE 1=1 `
  313. if condition != "" {
  314. sql += condition
  315. }
  316. o := orm.NewOrmUsingDB("data")
  317. err = o.Raw(sql, pars).QueryRow(&total)
  318. return
  319. }
  320. type BaseFromSmmIndexListResp struct {
  321. List []*BaseFromSmmIndexItem
  322. Paging *paging.PagingItem `description:"分页数据"`
  323. }
  324. type BaseFromSmmIndexItem struct {
  325. BaseFromSmmIndexId int `orm:"column(base_from_smm_index_id);pk"`
  326. ClassifyId int
  327. Interface string
  328. Name string
  329. IndexCode string
  330. IndexName string
  331. Type1 string `orm:"column(type_1)"`
  332. Type2 string `orm:"column(type_2)"`
  333. Type3 string `orm:"column(type_3)"`
  334. Frequency string
  335. Unit string
  336. ApiStartTime string
  337. ApiUpdateTime string
  338. StartTime string
  339. FinishTime string
  340. Sort int
  341. CreateTime string
  342. ModifyTime string
  343. DataState string
  344. ReleaseTime string
  345. StartDate string
  346. EndDate string
  347. TypeAll string
  348. }
  349. type BaseFromSmmIndexTypeList struct {
  350. Type1 string `orm:"column(type_1)"`
  351. Type2 string `orm:"column(type_2)"`
  352. Type3 string `orm:"column(type_3)"`
  353. }
  354. // GetBaseFromSmmIndexTypeList
  355. func GetBaseFromSmmIndexTypeList() (list []BaseFromSmmIndexTypeList, err error) {
  356. o := orm.NewOrmUsingDB("data")
  357. sql := `SELECT type_1,type_2, type_3 from base_from_smm_index GROUP BY type_1,type_2,type_3 `
  358. _, err = o.Raw(sql).QueryRows(&list)
  359. return
  360. }
  361. type TypeListRespItem struct {
  362. Type string
  363. Child []TypeListRespItem
  364. }
  365. func GetSmmIndexDataListNoPage(condition string, pars []interface{}) (items []*BaseFromSmmIndexItem, err error) {
  366. sql := `select * FROM base_from_smm_index
  367. WHERE 1=1 `
  368. if condition != "" {
  369. sql += condition
  370. }
  371. o := orm.NewOrmUsingDB("data")
  372. _, err = o.Raw(sql, pars).QueryRows(&items)
  373. return
  374. }
  375. func GetBaseFromSmmIndexByIndexCodes(indexCodes string) (list []*BaseFromSmmIndex, err error) {
  376. o := orm.NewOrmUsingDB("data")
  377. sql := ` SELECT * FROM base_from_smm_index WHERE index_code IN (` + indexCodes + `)`
  378. _, err = o.Raw(sql).QueryRows(&list)
  379. return
  380. }
  381. // SmmIndexListReq
  382. type SmmIndexListReq struct {
  383. Types []string `description:"分类"`
  384. Frequency string `description:"频度"`
  385. DataState string `description:"数据状态"`
  386. Keyword string `description:"关键字"`
  387. IndexCodes string `description:"指标代码"`
  388. SortType string `description:"排序类型"`
  389. SortParam string `description:"排序参数"`
  390. PageSize int
  391. CurrentIndex int
  392. }
  393. // SmmIndexExistCheckReq
  394. type SmmIndexExistCheckReq struct {
  395. Types []string `description:"分类"`
  396. Frequency string `description:"频度"`
  397. DataState string `description:"数据状态"`
  398. EdbCode string `description:"指标代码"`
  399. SelectAll bool `description:"是否全选"`
  400. Keyword string `description:"关键字"`
  401. }