smm_data.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "gorm.io/gorm"
  7. "time"
  8. )
  9. type SmmClassify struct {
  10. TypeName string `orm:"column(type_name)" description:"分类名称"`
  11. TypeCode string `orm:"column(type_code)" description:"分类名称编码"`
  12. }
  13. func GetSmmClassify() (items []*SmmClassify, err error) {
  14. sql := `SELECT CONCAT(type_2,type_3) AS type_name,CONCAT(type_2,'#',type_3) type_code FROM base_from_smm_index GROUP BY CONCAT(type_2,type_3) ORDER BY CONCAT(type_2,type_3) ASC `
  15. o := global.DbMap[utils.DbNameIndex]
  16. err = o.Raw(sql).Find(&items).Error
  17. return
  18. }
  19. type SmmFrequency struct {
  20. Frequency string `description:"频度"`
  21. }
  22. func GetSmmFrequencyByClassifyId(classifyId int) (items []*GlFrequency, err error) {
  23. o := global.DbMap[utils.DbNameIndex]
  24. sql := ` SELECT frequency FROM base_from_smm_index WHERE classify_id = ? `
  25. sql += ` GROUP BY frequency ORDER BY frequency ASC `
  26. err = o.Raw(sql, classifyId).Find(&items).Error
  27. return
  28. }
  29. type SmmIndex struct {
  30. BaseFromSmmIndexId int `gorm:"column:base_from_smm_index_id;primaryKey"`
  31. Interface string
  32. Name string
  33. IndexCode string
  34. IndexName string
  35. Type1 string `gorm:"column:type_1"`
  36. Type2 string `gorm:"column:type_2"`
  37. Type3 string `gorm:"column:type_3"`
  38. Frequency string
  39. Unit string
  40. ApiStartTime string
  41. ApiUpdateTime string
  42. StartTime string
  43. FinishTime string
  44. CreateTime string
  45. ModifyTime string
  46. IsStop int `description:"是否停更:1:停更,0:未停更"`
  47. EndValue float64 `description:"指标的最新值"`
  48. }
  49. func (obj *SmmIndex) AfterFind(tx *gorm.DB) (err error) {
  50. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  51. if obj.CreateTime != "" {
  52. obj.CreateTime = utils.GormDateStrToDateTimeStr(obj.CreateTime)
  53. }
  54. if obj.ModifyTime != "" {
  55. obj.ModifyTime = utils.GormDateStrToDateTimeStr(obj.ModifyTime)
  56. }
  57. if obj.StartTime != "" {
  58. obj.StartTime = utils.GormDateStrToDateTimeStr(obj.StartTime)
  59. }
  60. if obj.FinishTime != "" {
  61. obj.FinishTime = utils.GormDateStrToDateTimeStr(obj.FinishTime)
  62. }
  63. if obj.ApiStartTime != "" {
  64. obj.ApiStartTime = utils.GormDateStrToDateTimeStr(obj.ApiStartTime)
  65. }
  66. if obj.ApiUpdateTime != "" {
  67. obj.ApiUpdateTime = utils.GormDateStrToDateTimeStr(obj.ApiUpdateTime)
  68. }
  69. }
  70. return
  71. }
  72. type SmmIndexItem struct {
  73. BaseFromSmmIndexId int `orm:"column(base_from_smm_index_id);pk" gorm:"primaryKey"`
  74. ClassifyId int
  75. ParentClassifyId int
  76. Interface string
  77. Name string
  78. IndexCode string
  79. IndexName string
  80. Type1 string `gorm:"column:type_1"`
  81. Type2 string `gorm:"column:type_2"`
  82. Type3 string `gorm:"column:type_3"`
  83. Frequency string
  84. Unit string
  85. ApiStartTime string
  86. ApiUpdateTime string
  87. StartTime string
  88. FinishTime string
  89. CreateTime string
  90. ModifyTime string
  91. IsStop int `description:"是否停更:1:停更,0:未停更"`
  92. EndValue float64 `description:"指标的最新值"`
  93. }
  94. func GetSmmIndex(condition string, pars []interface{}) (items []*SmmIndex, err error) {
  95. o := global.DbMap[utils.DbNameIndex]
  96. sql := ` SELECT * FROM base_from_smm_index WHERE 1=1 `
  97. if condition != "" {
  98. sql += condition
  99. }
  100. sql += `ORDER BY sort ASC, base_from_smm_index_id asc`
  101. err = o.Raw(sql, pars...).Find(&items).Error
  102. return
  103. }
  104. // GetSmmIndexById
  105. // @Description: 根据id获取指标信息
  106. // @author: Roc
  107. // @datetime 2024-01-10 14:25:26
  108. // @param basFromSmmIndexId int
  109. // @return item *SmmIndex
  110. // @return err error
  111. func GetSmmIndexById(basFromSmmIndexId int) (item *SmmIndex, err error) {
  112. o := global.DbMap[utils.DbNameIndex]
  113. sql := ` SELECT * FROM base_from_smm_index WHERE base_from_smm_index_id = ? `
  114. err = o.Raw(sql, basFromSmmIndexId).Find(&item).Error
  115. return
  116. }
  117. type SmmExportIndex struct {
  118. TypeName string
  119. IndexCode string
  120. IndexName string
  121. Type1 string `gorm:"column:type_1"`
  122. Type2 string `gorm:"column:type_2"`
  123. Type3 string `gorm:"column:type_3"`
  124. Frequency string
  125. Unit string
  126. ModifyTime string
  127. }
  128. func GetExportSmmIndex(typeCodes []string) (items []*SmmExportIndex, err error) {
  129. if len(typeCodes) == 0 {
  130. return
  131. }
  132. o := global.DbMap[utils.DbNameIndex]
  133. sql := ` SELECT *,CONCAT(type_2, "#", type_3) AS type_name FROM base_from_smm_index WHERE CONCAT(type_2, "#", type_3) IN (` + utils.GetOrmInReplace(len(typeCodes)) + `) ORDER BY frequency ASC,index_code ASC`
  134. err = o.Raw(sql, typeCodes).Find(&items).Error
  135. return
  136. }
  137. func GetSmmFrequency(classifyId int) (items []*string, err error) {
  138. sql := `SELECT DISTINCT frequency FROM base_from_smm_index WHERE classify_id=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  139. o := global.DbMap[utils.DbNameIndex]
  140. err = o.Raw(sql, classifyId).Find(&items).Error
  141. return
  142. }
  143. func GetSmmFrequencyByCode(code string) (items []*string, err error) {
  144. sql := `SELECT DISTINCT frequency FROM base_from_smm_index WHERE index_code=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  145. o := global.DbMap[utils.DbNameIndex]
  146. err = o.Raw(sql, code).Find(&items).Error
  147. return
  148. }
  149. type SmmIndexList struct {
  150. BaseFromSmmIndexId int `gorm:"column:base_from_smm_index_id);primaryKey"`
  151. Interface string
  152. Name string
  153. IndexCode string
  154. IndexName string
  155. Type1 string `gorm:"column:type_1"`
  156. Type2 string `gorm:"column:type_2"`
  157. Type3 string `gorm:"column:type_3"`
  158. Frequency string
  159. Unit string
  160. ApiStartTime string
  161. ApiUpdateTime string
  162. StartTime string
  163. FinishTime string
  164. ModifyTime string
  165. DataList []*SmmIndexData `gorm:"-"`
  166. Paging *paging.PagingItem `description:"分页数据" gorm:"-"`
  167. }
  168. type SmmIndexData struct {
  169. Value string `gorm:"column:value" description:"日期"`
  170. DataTime string `gorm:"column:data_time" description:"值"`
  171. }
  172. func (m *SmmIndexData) AfterFind(tx *gorm.DB) (err error) {
  173. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  174. if m.DataTime != "" {
  175. m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
  176. }
  177. }
  178. return
  179. }
  180. func GetSmmIndexData(indexCode string, startSize, pageSize int) (items []*SmmIndexData, err error) {
  181. o := global.DbMap[utils.DbNameIndex]
  182. sql := ` SELECT * FROM base_from_smm_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  183. err = o.Raw(sql, indexCode, startSize, pageSize).Find(&items).Error
  184. return
  185. }
  186. func GetSmmIndexDataCount(indexCode string) (count int, err error) {
  187. o := global.DbMap[utils.DbNameIndex]
  188. sql := ` SELECT COUNT(1) AS count FROM base_from_smm_data WHERE index_code=? `
  189. err = o.Raw(sql, indexCode).Scan(&count).Error
  190. return
  191. }
  192. // GetSmmItemList 模糊查询Smm数据库指标列表
  193. func GetSmmItemList(keyword string) (items []*SmmIndexItem, err error) {
  194. o := global.DbMap[utils.DbNameIndex]
  195. sql := "SELECT * FROM base_from_smm_index WHERE CONCAT(index_name,index_code) LIKE ? "
  196. err = o.Raw(sql, utils.GetLikeKeyword(keyword)).Find(&items).Error
  197. return
  198. }
  199. func GetSmmIndexDataByCode(indexCode string) (items []*SmmIndexData, err error) {
  200. o := global.DbMap[utils.DbNameIndex]
  201. sql := ` SELECT * FROM base_from_smm_data WHERE index_code=? ORDER BY data_time DESC `
  202. err = o.Raw(sql, indexCode).Find(&items).Error
  203. return
  204. }
  205. func GetSmmDataMaxCount(classifyId int) (count int, err error) {
  206. o := global.DbMap[utils.DbNameIndex]
  207. sql := `SELECT COALESCE(MAX(t.num), 0) AS count FROM (
  208. SELECT COUNT(1) AS num FROM base_from_smm_index AS a
  209. INNER JOIN base_from_smm_data AS b ON a.index_code=b.index_code
  210. WHERE a.classify_id=?
  211. GROUP BY a.base_from_smm_index_id
  212. )AS t `
  213. err = o.Raw(sql, classifyId).Scan(&count).Error
  214. return
  215. }
  216. type ExportSmmDataMaxCount struct {
  217. TypeName string
  218. Count int
  219. }
  220. func GetExportSmmDataMaxCount(typeCodes []string) (items []*ExportSmmDataMaxCount, err error) {
  221. if len(typeCodes) == 0 {
  222. return
  223. }
  224. o := global.DbMap[utils.DbNameIndex]
  225. sql := ` SELECT
  226. COALESCE(MAX(t.num), 0) AS count,
  227. t.type_name
  228. FROM
  229. (
  230. SELECT
  231. COUNT(1) AS num,
  232. CONCAT(a.type_2, "#", a.type_3) AS type_name
  233. FROM
  234. base_from_smm_index AS a
  235. INNER JOIN base_from_smm_data AS b ON a.index_code = b.index_code
  236. WHERE
  237. CONCAT(a.type_2, "#", a.type_3) IN (` + utils.GetOrmInReplace(len(typeCodes)) + `)
  238. GROUP BY
  239. a.base_from_smm_index_id
  240. ) AS t
  241. GROUP BY
  242. type_name `
  243. err = o.Raw(sql, typeCodes).Find(&items).Error
  244. return
  245. }
  246. type ExportSmmIndexData struct {
  247. Value string `orm:"column(value)" description:"日期"`
  248. DataTime string `orm:"column(data_time)" description:"值"`
  249. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  250. }
  251. func GetExportSmmIndexDataByCodes(indexCodes []string) (items []*ExportSmmIndexData, err error) {
  252. if len(indexCodes) == 0 {
  253. return
  254. }
  255. o := global.DbMap[utils.DbNameIndex]
  256. sql := ` SELECT index_code,data_time,value FROM base_from_smm_data WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `) ORDER BY data_time DESC `
  257. err = o.Raw(sql, indexCodes).Find(&items).Error
  258. return
  259. }
  260. // GetSmmBaseInfoList
  261. // @Description: 获取有色数据列表
  262. // @author: Roc
  263. // @datetime 2024-01-10 14:28:29
  264. // @param condition string
  265. // @param pars []interface{}
  266. // @param orderBy string
  267. // @param startSize int
  268. // @param pageSize int
  269. // @return total int
  270. // @return items []*BaseRefreshEdbInfo
  271. // @return err error
  272. func GetSmmBaseInfoList(condition string, pars []interface{}, orderBy string, startSize, pageSize int) (total int, items []*BaseRefreshEdbInfo, err error) {
  273. o := global.DbMap[utils.DbNameIndex]
  274. // 数量汇总
  275. totalSql := ` SELECT count(1) FROM base_from_smm_index WHERE 1=1 `
  276. if condition != "" {
  277. totalSql += condition
  278. }
  279. err = o.Raw(totalSql, pars...).Scan(&total).Error
  280. if err != nil {
  281. return
  282. }
  283. // 列表数据
  284. sql := ` SELECT base_from_smm_index_id as edb_info_id, classify_id,index_code,index_name,end_date,end_value,frequency,is_stop,terminal_code FROM base_from_smm_index WHERE 1=1 `
  285. if condition != "" {
  286. sql += condition
  287. }
  288. if orderBy != "" {
  289. sql += ` ORDER BY ` + orderBy
  290. } else {
  291. sql += ` ORDER BY base_from_smm_index_id ASC `
  292. }
  293. sql += ` LIMIT ?,? `
  294. pars = append(pars, startSize, pageSize)
  295. err = o.Raw(sql, pars...).Find(&items).Error
  296. return
  297. }
  298. // ModifySmmUpdateStatus
  299. // @Description: 修改有色数据停更状态
  300. // @author: Roc
  301. // @datetime 2024-01-08 16:23:31
  302. // @param edbIdList []int
  303. // @param indexCodeList []string
  304. // @param isStop int
  305. // @return err error
  306. func ModifySmmUpdateStatus(edbIdList []int, indexCodeList []string, isStop int) (err error) {
  307. idNum := len(edbIdList)
  308. if idNum <= 0 {
  309. return
  310. }
  311. o := global.DbMap[utils.DbNameIndex].Begin()
  312. defer func() {
  313. if err != nil {
  314. _ = o.Rollback()
  315. return
  316. }
  317. _ = o.Commit()
  318. }()
  319. // 更改数据源的更新状态
  320. sql := ` UPDATE base_from_smm_index SET is_stop = ? WHERE base_from_smm_index_id IN (` + utils.GetOrmInReplace(idNum) + `) `
  321. err = o.Exec(sql, isStop, edbIdList).Error
  322. if err != nil {
  323. return
  324. }
  325. codeNum := len(indexCodeList)
  326. if codeNum <= 0 {
  327. // 需要通过指标id列表查找code列表
  328. sql := ` SELECT index_code FROM base_from_smm_index WHERE base_from_smm_index_id IN (` + utils.GetOrmInReplace(idNum) + `) `
  329. err = o.Raw(sql, edbIdList).Scan(&indexCodeList).Error
  330. if err != nil {
  331. return
  332. }
  333. }
  334. codeNum = len(indexCodeList)
  335. // 查出来的编码是空的话,那么就直接返回了
  336. if codeNum <= 0 {
  337. return
  338. }
  339. // 更改指标的更新状态
  340. sql = ` UPDATE edb_info SET no_update = ?, set_update_time=? WHERE source = ? AND sub_source= ? AND edb_code IN (` + utils.GetOrmInReplace(codeNum) + `) `
  341. err = o.Exec(sql, isStop, time.Now(), utils.DATA_SOURCE_YS, 0, indexCodeList).Error
  342. if err != nil {
  343. return
  344. }
  345. return
  346. }