base_from_smm.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/beego/beego/v2/client/orm"
  9. )
  10. type BaseFromSmmData struct {
  11. SmmDataId int `orm:"column(smm_data_id);pk"`
  12. BaseFromSmmIndexId int
  13. IndexCode string
  14. DataTime string
  15. Value string
  16. CreateTime time.Time
  17. ModifyTime time.Time
  18. DataTimestamp int64
  19. }
  20. func AddBaseFromSmmData(item []*BaseFromSmmData) (err error) {
  21. o := orm.NewOrm()
  22. _, err = o.InsertMulti(500, item)
  23. return
  24. }
  25. func GetBaseFromSmmDataByCondition(condition string, pars []interface{}) (list []*BaseFromSmmData, err error) {
  26. o := orm.NewOrm()
  27. sql := `SELECT * FROM base_from_smm_data WHERE 1=1 `
  28. if condition != "" {
  29. sql += condition
  30. }
  31. _, err = o.Raw(sql, pars).QueryRows(&list)
  32. return
  33. }
  34. func UpdateBaseFromSmmDataListByIndexCode(items []*BaseFromSmmData) (err error) {
  35. o := orm.NewOrm()
  36. sql := `UPDATE base_from_smm_data SET value=? WHERE index_code=? AND data_time=? `
  37. stmt, err := o.Raw(sql).Prepare()
  38. if err != nil {
  39. return
  40. }
  41. defer stmt.Close()
  42. for _, item := range items {
  43. _, err = stmt.Exec(item.Value, item.IndexCode, item.DataTime)
  44. if err != nil {
  45. return
  46. }
  47. }
  48. return
  49. }
  50. func GetBaseFromSmmDataByIds(smmDataIds []int) (list []*BaseFromSmmData, err error) {
  51. if len(smmDataIds) == 0 {
  52. return
  53. }
  54. o := orm.NewOrm()
  55. sql := `SELECT * FROM base_from_smm_data WHERE 1=1 AND base_from_smm_index_id in (` + utils.GetOrmInReplace(len(smmDataIds)) + `)`
  56. _, err = o.Raw(sql, smmDataIds).QueryRows(&list)
  57. return
  58. }
  59. // 新增有色指标数据
  60. func AddEdbDataFromSmm(edbCode string, smmBaseDataAll []*BaseFromSmmData) (err error) {
  61. o := orm.NewOrm()
  62. var isAdd bool
  63. addSql := ` INSERT INTO edb_data_ys(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  64. existMap := make(map[string]string)
  65. for _, sv := range smmBaseDataAll {
  66. eDate := sv.DataTime
  67. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  68. if err != nil {
  69. return err
  70. }
  71. timestamp := dataTime.UnixNano() / 1e6
  72. timeStr := fmt.Sprintf("%d", timestamp)
  73. if _, ok := existMap[eDate]; !ok {
  74. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Value)
  75. isAdd = true
  76. }
  77. existMap[eDate] = sv.Value
  78. }
  79. if isAdd {
  80. addSql = strings.TrimRight(addSql, ",")
  81. utils.FileLog.Info("addSql:" + addSql)
  82. _, err = o.Raw(addSql).Exec()
  83. if err != nil {
  84. return err
  85. }
  86. }
  87. return
  88. }
  89. // RefreshEdbDataFromSmm 刷新有色指标数据
  90. func RefreshEdbDataFromSmm(edbInfoId int, edbCode, startDate string) (err error) {
  91. source := utils.DATA_SOURCE_YS
  92. subSource := utils.DATA_SUB_SOURCE_EDB
  93. o := orm.NewOrm()
  94. if err != nil {
  95. return
  96. }
  97. edbInfoIdStr := strconv.Itoa(edbInfoId)
  98. //计算数据
  99. var condition string
  100. var pars []interface{}
  101. if edbCode != "" {
  102. condition += " AND index_code=? "
  103. pars = append(pars, edbCode)
  104. }
  105. if startDate != "" {
  106. condition += " AND data_time>=? "
  107. pars = append(pars, startDate)
  108. }
  109. smmDataList, err := GetBaseFromSmmDataByCondition(condition, pars)
  110. if err != nil {
  111. return
  112. }
  113. // 真实数据的最大日期 , 插入规则配置的日期
  114. var realDataMaxDate, edbDataInsertConfigDate time.Time
  115. var edbDataInsertConfig *EdbDataInsertConfig
  116. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  117. {
  118. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  119. if err != nil && err.Error() != utils.ErrNoRow() {
  120. return
  121. }
  122. if edbDataInsertConfig != nil {
  123. edbDataInsertConfigDate = edbDataInsertConfig.Date
  124. }
  125. }
  126. var existCondition string
  127. var existPars []interface{}
  128. existCondition += " AND edb_info_id=? "
  129. existPars = append(existPars, edbInfoId)
  130. if startDate != "" {
  131. existCondition += " AND data_time>=? "
  132. existPars = append(existPars, startDate)
  133. }
  134. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  135. if err != nil {
  136. return err
  137. }
  138. existMap := make(map[string]*EdbInfoSearchData)
  139. for _, v := range existList {
  140. existMap[v.DataTime] = v
  141. }
  142. addSql := ` INSERT INTO edb_data_ys(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  143. var isAdd bool
  144. for _, v := range smmDataList {
  145. item := v
  146. eDate := item.DataTime
  147. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  148. if err != nil {
  149. return err
  150. }
  151. if findItem, ok := existMap[v.DataTime]; !ok {
  152. sValue := item.Value
  153. timestamp := dataTime.UnixNano() / 1e6
  154. timeStr := fmt.Sprintf("%d", timestamp)
  155. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  156. isAdd = true
  157. } else {
  158. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Value {
  159. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, item.Value)
  160. if err != nil {
  161. return err
  162. }
  163. }
  164. }
  165. // 下面代码主要目的是处理掉手动插入的数据判断
  166. {
  167. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  168. realDataMaxDate = dataTime
  169. }
  170. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  171. isFindConfigDateRealData = true
  172. }
  173. }
  174. }
  175. // 处理手工数据补充的配置
  176. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  177. if isAdd {
  178. addSql = strings.TrimRight(addSql, ",")
  179. _, err = o.Raw(addSql).Exec()
  180. if err != nil {
  181. fmt.Println("RefreshEdbDataFromSmm add Err", err.Error())
  182. return
  183. }
  184. }
  185. return
  186. }
  187. type BaseFromSmmIndex struct {
  188. BaseFromSmmIndexId int64 `orm:"column(base_from_smm_index_id);pk"`
  189. ClassifyId int
  190. Interface string
  191. Name string
  192. IndexCode string
  193. IndexName string
  194. Type1 string `orm:"column(type_1)"`
  195. Type2 string `orm:"column(type_2)"`
  196. Type3 string `orm:"column(type_3)"`
  197. Frequency string
  198. Unit string
  199. ApiStartTime string
  200. ApiUpdateTime string
  201. StartTime string
  202. FinishTime string
  203. BaseFileName string
  204. RenameFileName string
  205. StartDate string
  206. EndDate string
  207. TerminalCode string `description:"编码"`
  208. CreateTime time.Time
  209. ModifyTime time.Time
  210. DataState string
  211. ReleaseTime string
  212. }
  213. func (d *BaseFromSmmIndex) AddBaseFromSmmIndex() (lastId int64, err error) {
  214. o := orm.NewOrm()
  215. lastId, err = o.Insert(d)
  216. return
  217. }
  218. func (d *BaseFromSmmIndex) GetSmmIndexItem(indexCode string) (item *BaseFromSmmIndex, err error) {
  219. o := orm.NewOrm()
  220. sql := ` SELECT * FROM base_from_smm_index WHERE index_code=? `
  221. fmt.Println(sql, indexCode)
  222. err = o.Raw(sql, indexCode).QueryRow(&item)
  223. return
  224. }
  225. // Add 新增
  226. func (m *BaseFromSmmIndex) Add() (err error) {
  227. o := orm.NewOrm()
  228. _, err = o.Insert(m)
  229. return
  230. }
  231. func GetBaseFromSmmDataBySmmCode(smmCode string) (list []*BaseFromSmmData, err error) {
  232. o := orm.NewOrm()
  233. sql := ` SELECT * FROM base_from_smm_data WHERE index_code=? `
  234. _, err = o.Raw(sql, smmCode).QueryRows(&list)
  235. return
  236. }
  237. func ModifySmmIndexSort(baseFromSmmIndexId int64) (err error) {
  238. o := orm.NewOrm()
  239. sql := ` UPDATE base_from_smm_index SET sort=? WHERE base_from_smm_index_id=? `
  240. _, err = o.Raw(sql, baseFromSmmIndexId, baseFromSmmIndexId).Exec()
  241. return
  242. }
  243. func GetSmmIndexInfoMaxAndMinInfo(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  244. o := orm.NewOrm()
  245. sql := ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM base_from_smm_data WHERE index_code=? `
  246. err = o.Raw(sql, indexCode).QueryRow(&item)
  247. if err != nil {
  248. return
  249. }
  250. // 获取最新值
  251. var latest_value float64
  252. sql = ` SELECT value AS latest_value FROM base_from_smm_data WHERE index_code=? ORDER BY data_time DESC LIMIT 1 `
  253. err = o.Raw(sql, indexCode).QueryRow(&latest_value)
  254. if err != nil {
  255. return
  256. }
  257. item.LatestValue = latest_value
  258. return
  259. }
  260. func ModifySmmIndexMaxAndMinInfo(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) {
  261. o := orm.NewOrm()
  262. sql := ` UPDATE base_from_smm_index SET start_date=?,end_date=?,end_value=?,modify_time=NOW() WHERE index_code=? `
  263. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.LatestValue, indexCode).Exec()
  264. return
  265. }
  266. // HandleSmmExcelDataReq SMM的excel数据
  267. type HandleSmmExcelDataReq struct {
  268. BaseFilePath string
  269. RenameFilePath string
  270. IndexName string
  271. Source string
  272. IndexCode string
  273. Unit string
  274. Frequency string
  275. ExcelDataMap map[string]string
  276. TerminalCode string `description:"编码"`
  277. }
  278. // MultiUpdateBaseFromSmmDataValue 批量更新SMM指标数据
  279. func MultiUpdateBaseFromSmmDataValue(items []*BaseFromSmmData) (err error) {
  280. if len(items) == 0 {
  281. return
  282. }
  283. o := orm.NewOrm()
  284. sql := `UPDATE base_from_smm_data SET value = ?, modify_time = NOW() WHERE index_code = ? AND data_time = ? LIMIT 1`
  285. p, err := o.Raw(sql).Prepare()
  286. if err != nil {
  287. return
  288. }
  289. defer func() {
  290. _ = p.Close()
  291. }()
  292. for _, v := range items {
  293. if v.IndexCode == "" || v.DataTime == "" {
  294. continue
  295. }
  296. _, err = p.Exec(v.Value, v.IndexCode, v.DataTime)
  297. if err != nil {
  298. return
  299. }
  300. }
  301. return
  302. }
  303. type AddSmmIndexToDataSourceResp struct {
  304. EdbCode string `description:"指标编码"`
  305. ClassifyId int `description:"指标分类"`
  306. SysUserId int `description:"操作人id"`
  307. SysUserRealName string `description:"操作人真实名称"`
  308. }
  309. func (m *BaseFromSmmIndex) GetIndexItem(indexCode string) (item *BaseFromSmmIndex, err error) {
  310. o := orm.NewOrm()
  311. sql := `SELECT * FROM base_from_smm_index WHERE index_code = ? `
  312. err = o.Raw(sql, indexCode).QueryRow(&item)
  313. return
  314. }
  315. // 修改
  316. func (m *BaseFromSmmIndex) Update(updateParams, whereParam map[string]interface{}) (err error) {
  317. to := orm.NewOrm()
  318. ptrStructOrTableName := "base_from_smm_index"
  319. qs := to.QueryTable(ptrStructOrTableName)
  320. for expr, exprV := range whereParam {
  321. qs = qs.Filter(expr, exprV)
  322. }
  323. _, err = qs.Update(updateParams)
  324. return
  325. }
  326. // BridgeZhongJiIndexDataParams 桥接服务-获取中基指标数据入参
  327. type BridgeZhongJiIndexDataParams struct {
  328. EdbCode string `json:"edb_code" form:"edb_code" description:"指标编码"`
  329. }
  330. type BridgeZhongJiIndexListParams struct {
  331. Page int `json:"page" form:"page" description:"页码"`
  332. PageSize int `json:"page_size" form:"page_size" description:"每页指标个数,每页不能超过500条,超过会固定为500"`
  333. }
  334. type BaseFromSmmDataList struct {
  335. Data []*BaseFromSmmData
  336. IndexCode string
  337. }
  338. type UpdateRule struct {
  339. RuleDesc string `json:"rule_desc"`
  340. Notice string `json:"notice"`
  341. ReleaseTime string `json:"release_time"`
  342. RuleInfo UpdateRuleInfo `json:"rule_info"`
  343. }
  344. type UpdateRuleInfo struct {
  345. UpdCal string `json:"upd_cal"`
  346. UpdCalUnit string `json:"upd_cal_unit"`
  347. PeriodOffset int `json:"period_offset"`
  348. UpdCount int `json:"upd_count"`
  349. }
  350. type DataItem struct {
  351. QuotaID string `json:"quota_id"`
  352. QuotaName string `json:"quota_name"`
  353. Mark string `json:"mark"`
  354. Unit string `json:"unit"`
  355. ValueType string `json:"value_type"`
  356. Source string `json:"source"`
  357. ListOrder int `json:"list_order"`
  358. DataStart string `json:"data_start"`
  359. DataEnd string `json:"data_end"`
  360. Frequency string `json:"frequency"`
  361. Precision int `json:"precision"`
  362. TypeCode string `json:"type_code"`
  363. TypeName string `json:"type_name"`
  364. TypeCodeAll string `json:"type_code_all"`
  365. TypeAll string `json:"type_all"`
  366. Tags string `json:"tags"`
  367. AuthStart string `json:"auth_start"`
  368. AuthFinish string `json:"auth_finish"`
  369. AuthLang string `json:"auth_lang"`
  370. AuthModule string `json:"auth_module"`
  371. DataDur string `json:"data_dur"`
  372. Spec string `json:"spec"`
  373. Area string `json:"area"`
  374. DataState string `json:"data_state"`
  375. UpdateRule UpdateRule `json:"update_rule"`
  376. }
  377. type ListData struct {
  378. Total int `json:"total"`
  379. CurrentPage int `json:"current_page"`
  380. PageSize int `json:"page_size"`
  381. PageCount int `json:"page_count"`
  382. Data []DataItem `json:"data"`
  383. }
  384. type SmmIndexListResponse struct {
  385. Code int `json:"code"`
  386. Msg string `json:"msg"`
  387. Data ListData `json:"data"`
  388. }
  389. type SmmListResp struct {
  390. Code int `json:"code"`
  391. Msg string `json:"msg"`
  392. Data SmmIndexListResponse `json:"data"`
  393. }
  394. func AddBaseFromSmmIndex(list []*BaseFromSmmIndex) (lastId int64, err error) {
  395. o := orm.NewOrm()
  396. _, err = o.InsertMulti(len(list), list)
  397. return
  398. }
  399. // BridgeZhongJiIndexLatestDataParams 桥接服务-获取中基更新指标数据入参
  400. type BridgeZhongJiIndexLatestDataParams struct {
  401. EdbCode string `json:"edb_code" form:"edb_code" description:"指标编码"`
  402. LastTime int64 `json:"last_time" form:"last_time" description:"上次更新时间戳(秒)"`
  403. }
  404. func GetBaseFromSmmIndex() (list []*BaseFromSmmIndex, err error) {
  405. o := orm.NewOrm()
  406. sql := ` SELECT * FROM base_from_smm_index`
  407. _, err = o.Raw(sql).QueryRows(&list)
  408. return
  409. }
  410. func ModifyBaseFromSmmIndex(item *BaseFromSmmIndex) (err error) {
  411. o := orm.NewOrm()
  412. sql := ` UPDATE base_from_smm_index SET end_date = ?, modify_time=NOW(), data_state= ? `
  413. _, err = o.Raw(sql, item.EndDate, item.DataState).Exec()
  414. return
  415. }
  416. func (m *BaseFromSmmIndex) UpdateCols(cols []string) (err error) {
  417. o := orm.NewOrm()
  418. _, err = o.Update(m, cols...)
  419. return
  420. }
  421. func ModifyBaseFromSmmIndexDate(item *BaseFromSmmIndex) (err error) {
  422. o := orm.NewOrm()
  423. sql := ` UPDATE base_from_smm_index SET end_date = ?, modify_time=NOW(), data_state= ? WHERE base_from_smm_index_id=?`
  424. _, err = o.Raw(sql, item.EndDate, item.DataState, item.BaseFromSmmIndexId).Exec()
  425. return
  426. }
  427. type SmmLatestDataResponse struct {
  428. Code int `json:"code"`
  429. Msg string `json:"msg"`
  430. Data SmmLatestResp `json:"data"`
  431. }
  432. type SmmLatestResp struct {
  433. Code int `json:"code"`
  434. Msg string `json:"msg"`
  435. Data SmmLatestData `json:"data"`
  436. }
  437. type SmmLatestData struct {
  438. DataLen int `json:"data_len"`
  439. DataList []Datum `json:"data_list"`
  440. }
  441. type Datum struct {
  442. QuotaID string `json:"quota_id"`
  443. Date string `json:"date"`
  444. Value string `json:"value"`
  445. Mark string `json:"mark"`
  446. Create string `json:"create"`
  447. Update string `json:"update"`
  448. }
  449. // RefreshEdbDataFromSmmToEdb 刷新有色指标数据
  450. func RefreshEdbDataFromSmmToEdb(edbInfoId int, edbCode, startDate string, smmDataList []*BaseFromSmmData) (err error) {
  451. source := utils.DATA_SOURCE_YS
  452. subSource := utils.DATA_SUB_SOURCE_EDB
  453. o := orm.NewOrm()
  454. if err != nil {
  455. return
  456. }
  457. edbInfoIdStr := strconv.Itoa(edbInfoId)
  458. //计算数据
  459. var condition string
  460. var pars []interface{}
  461. if edbCode != "" {
  462. condition += " AND index_code=? "
  463. pars = append(pars, edbCode)
  464. }
  465. if startDate != "" {
  466. condition += " AND data_time>=? "
  467. pars = append(pars, startDate)
  468. }
  469. // 真实数据的最大日期 , 插入规则配置的日期
  470. var realDataMaxDate, edbDataInsertConfigDate time.Time
  471. var edbDataInsertConfig *EdbDataInsertConfig
  472. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  473. {
  474. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  475. if err != nil && err.Error() != utils.ErrNoRow() {
  476. return
  477. }
  478. if edbDataInsertConfig != nil {
  479. edbDataInsertConfigDate = edbDataInsertConfig.Date
  480. }
  481. }
  482. var existCondition string
  483. var existPars []interface{}
  484. existCondition += " AND edb_info_id=? "
  485. existPars = append(existPars, edbInfoId)
  486. if startDate != "" {
  487. existCondition += " AND data_time>=? "
  488. existPars = append(existPars, startDate)
  489. }
  490. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  491. if err != nil {
  492. return err
  493. }
  494. existMap := make(map[string]*EdbInfoSearchData)
  495. for _, v := range existList {
  496. existMap[v.DataTime] = v
  497. }
  498. addSql := ` INSERT INTO edb_data_ys(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  499. var isAdd bool
  500. for _, v := range smmDataList {
  501. item := v
  502. eDate := item.DataTime
  503. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  504. if err != nil {
  505. return err
  506. }
  507. if findItem, ok := existMap[v.DataTime]; !ok {
  508. sValue := item.Value
  509. timestamp := dataTime.UnixNano() / 1e6
  510. timeStr := fmt.Sprintf("%d", timestamp)
  511. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  512. isAdd = true
  513. } else {
  514. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Value {
  515. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, item.Value)
  516. if err != nil {
  517. return err
  518. }
  519. }
  520. }
  521. // 下面代码主要目的是处理掉手动插入的数据判断
  522. {
  523. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  524. realDataMaxDate = dataTime
  525. }
  526. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  527. isFindConfigDateRealData = true
  528. }
  529. }
  530. }
  531. // 处理手工数据补充的配置
  532. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  533. if isAdd {
  534. addSql = strings.TrimRight(addSql, ",")
  535. _, err = o.Raw(addSql).Exec()
  536. if err != nil {
  537. fmt.Println("RefreshEdbDataFromSmm add Err", err.Error())
  538. return
  539. }
  540. }
  541. return
  542. }