smm_data.go 12 KB

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