base_from_smm.go 14 KB

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