base_from_mysteel_chemical.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. //钢联
  11. type MysteelChemicalData struct {
  12. InputValue float64 `orm:"column(value)" description:"值"`
  13. DataTime string `orm:"column(data_time)" description:"日期"`
  14. }
  15. func GetMysteelChemicalDataByCondition(condition string, pars []interface{}) (item []*MysteelChemicalData, err error) {
  16. sql1 := ` SELECT * FROM base_from_mysteel_chemical_data WHERE 1=1 `
  17. o := orm.NewOrm()
  18. if condition != "" {
  19. sql1 += condition
  20. }
  21. sql := `select * from (` + sql1 + ` having 1 order by modify_time DESC ) tmp GROUP BY data_time ORDER BY data_time DESC `
  22. _, err = o.Raw(sql, pars).QueryRows(&item)
  23. return
  24. }
  25. // AddEdbDataFromMysteelChemical 新增钢联指标数据
  26. func AddEdbDataFromMysteelChemical(edbCode string) (err error) {
  27. o := orm.NewOrm()
  28. var condition string
  29. var pars []interface{}
  30. if edbCode != "" {
  31. condition += " AND index_code = ? "
  32. pars = append(pars, edbCode)
  33. }
  34. mysteelChemicalDataList, err := GetMysteelChemicalDataByCondition(condition, pars)
  35. if err != nil {
  36. return
  37. }
  38. dataLen := len(mysteelChemicalDataList)
  39. existMap := make(map[string]string)
  40. if dataLen > 0 {
  41. var isAdd bool
  42. addSql := ` INSERT INTO edb_data_mysteel_chemical (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  43. for i := 0; i < dataLen; i++ {
  44. item := mysteelChemicalDataList[i]
  45. eDate := item.DataTime
  46. sValue := utils.SubFloatToString(item.InputValue, 30)
  47. if sValue != "" {
  48. if _, ok := existMap[eDate]; !ok {
  49. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  50. if err != nil {
  51. return err
  52. }
  53. timestamp := dataTime.UnixNano() / 1e6
  54. timeStr := fmt.Sprintf("%d", timestamp)
  55. addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
  56. isAdd = true
  57. }
  58. }
  59. existMap[eDate] = eDate
  60. }
  61. if isAdd {
  62. addSql = strings.TrimRight(addSql, ",")
  63. utils.FileLog.Info("addSql:" + addSql)
  64. _, err = o.Raw(addSql).Exec()
  65. if err != nil {
  66. return err
  67. }
  68. }
  69. }
  70. return
  71. }
  72. // RefreshEdbDataFromMysteelChemical 刷新钢联指标数据
  73. func RefreshEdbDataFromMysteelChemical(edbInfoId int, edbCode, startDate string) (err error) {
  74. source := utils.DATA_SOURCE_MYSTEEL_CHEMICAL
  75. o := orm.NewOrm()
  76. if err != nil {
  77. return
  78. }
  79. edbInfoIdStr := strconv.Itoa(edbInfoId)
  80. //计算数据
  81. var condition string
  82. var pars []interface{}
  83. if edbCode != "" {
  84. condition += " AND index_code=? "
  85. pars = append(pars, edbCode)
  86. }
  87. if startDate != "" {
  88. condition += " AND data_time>=? "
  89. pars = append(pars, startDate)
  90. }
  91. mysteelChemicalDataList, err := GetMysteelChemicalDataByCondition(condition, pars)
  92. if err != nil {
  93. return
  94. }
  95. // 真实数据的最大日期 , 插入规则配置的日期
  96. var realDataMaxDate, edbDataInsertConfigDate time.Time
  97. var edbDataInsertConfig *EdbDataInsertConfig
  98. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  99. {
  100. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  101. if err != nil && err.Error() != utils.ErrNoRow() {
  102. return
  103. }
  104. if edbDataInsertConfig != nil {
  105. edbDataInsertConfigDate = edbDataInsertConfig.Date
  106. }
  107. }
  108. var existCondition string
  109. var existPars []interface{}
  110. existCondition += " AND edb_info_id=? "
  111. existPars = append(existPars, edbInfoId)
  112. if startDate != "" {
  113. existCondition += " AND data_time>=? "
  114. existPars = append(existPars, startDate)
  115. }
  116. //获取指标所有数据
  117. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  118. if err != nil {
  119. return err
  120. }
  121. existMap := make(map[string]*EdbInfoSearchData)
  122. for _, v := range existList {
  123. existMap[v.DataTime] = v
  124. }
  125. addSql := ` INSERT INTO edb_data_mysteel_chemical(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  126. var isAdd bool
  127. addMap := make(map[string]string)
  128. for _, v := range mysteelChemicalDataList {
  129. item := v
  130. eDate := item.DataTime
  131. sValue := utils.SubFloatToString(item.InputValue, 30)
  132. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  133. if err != nil {
  134. return err
  135. }
  136. if findItem, ok := existMap[v.DataTime]; !ok {
  137. if sValue != "" {
  138. timestamp := dataTime.UnixNano() / 1e6
  139. timeStr := fmt.Sprintf("%d", timestamp)
  140. saveValue := sValue
  141. if _, addOk := addMap[eDate]; !addOk {
  142. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  143. isAdd = true
  144. }
  145. }
  146. } else {
  147. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  148. err = ModifyEdbDataById(source, findItem.EdbDataId, sValue)
  149. if err != nil {
  150. return err
  151. }
  152. }
  153. }
  154. addMap[v.DataTime] = v.DataTime
  155. // 下面代码主要目的是处理掉手动插入的数据判断
  156. {
  157. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  158. realDataMaxDate = dataTime
  159. }
  160. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  161. isFindConfigDateRealData = true
  162. }
  163. }
  164. }
  165. // 处理手工数据补充的配置
  166. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  167. if isAdd {
  168. addSql = strings.TrimRight(addSql, ",")
  169. _, err = o.Raw(addSql).Exec()
  170. if err != nil {
  171. return err
  172. }
  173. }
  174. return
  175. }
  176. // 钢联化工指标数据
  177. type BaseFromMysteelChemicalIndex struct {
  178. BaseFromMysteelChemicalIndexId int64 `orm:"column(base_from_mysteel_chemical_index_id);pk"`
  179. BaseFromMysteelChemicalClassifyId int `description:"分类id"`
  180. IndexCode string `description:"指标编码"`
  181. IndexName string `description:"指标名称"`
  182. Unit string `description:"单位"`
  183. Source string `description:"数据来源"`
  184. Frequency string `description:"频度"`
  185. StartDate time.Time `description:"开始日期"`
  186. EndDate time.Time `description:"结束日期"`
  187. Describe string `description:"指标描述"`
  188. UpdateWeek string `description:"更新周期"`
  189. UpdateTime string `description:"更新时间,多个时间点用英文,隔开"`
  190. UpdateTime2 string `description:"更新时间2"`
  191. SysUserId int `description:"创建人id"`
  192. SysUserRealName string `description:"创建人姓名"`
  193. FilePath string `description:"文件存储路径"`
  194. MergeFilePath string `description:"更新文件"`
  195. FileIndex int `description:"文件索引"`
  196. MergeUpdateWeek string `description:"合并文件的更新周"`
  197. UpdateDate string `description:"更新日期"`
  198. CreateTime time.Time `description:"创建时间"`
  199. ModifyTime time.Time `description:"修改时间"`
  200. IsStop int `description:"是否停更:1:停更,0:未停更"`
  201. }
  202. // GetIndexRefreshAllByMergeFile 根据合并文件去分组查询需要刷新的文件
  203. func (m *BaseFromMysteelChemicalIndex) GetIndexRefreshAllByMergeFile() (items []*BaseFromMysteelChemicalIndex, err error) {
  204. o := orm.NewOrm()
  205. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE merge_file_path <>'' AND base_from_mysteel_chemical_classify_id NOT IN(54,55,56,57) GROUP BY merge_file_path`
  206. _, err = o.Raw(sql).QueryRows(&items)
  207. return
  208. }
  209. func (m *BaseFromMysteelChemicalIndex) GetIndexItem(indexCode string) (item *BaseFromMysteelChemicalIndex, err error) {
  210. o := orm.NewOrm()
  211. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE index_code = ? `
  212. err = o.Raw(sql, indexCode).QueryRow(&item)
  213. return
  214. }
  215. func (m *BaseFromMysteelChemicalIndex) GetIndexCreate() (items []*BaseFromMysteelChemicalIndex, err error) {
  216. o := orm.NewOrm()
  217. endTime := time.Now().Add(-2 * time.Minute).Format(utils.FormatDateTime)
  218. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE index_name = '' AND create_time <= ? `
  219. _, err = o.Raw(sql, endTime).QueryRows(&items)
  220. return
  221. }
  222. // Add 新增
  223. func (m *BaseFromMysteelChemicalIndex) Add() (err error) {
  224. o := orm.NewOrm()
  225. _, err = o.Insert(m)
  226. return
  227. }
  228. func (m *BaseFromMysteelChemicalIndex) Update(cols []string) (err error) {
  229. o := orm.NewOrm()
  230. _, err = o.Update(m, cols...)
  231. return
  232. }
  233. // GetNoMergeIndexByFrequencyCount 获取未合并的指标总数量
  234. func (m *BaseFromMysteelChemicalIndex) GetNoMergeIndexByFrequencyCount(frequency string) (total int64, err error) {
  235. o := orm.NewOrm()
  236. sql := `SELECT count(1) AS total FROM base_from_mysteel_chemical_index WHERE frequency = ? AND merge_file_path = '' AND index_name NOT LIKE "%停%" `
  237. err = o.Raw(sql, frequency).QueryRow(&total)
  238. return
  239. }
  240. // GetMaxFileIndexReq 获取最大文件数请求
  241. type GetMaxFileIndexReq struct {
  242. Frequency string
  243. EndDate string
  244. }
  245. // GetMaxFileIndex 获取最大文件的
  246. func (m *BaseFromMysteelChemicalIndex) GetMaxFileIndex(frequency string) (item *BaseFromMysteelChemicalIndex, err error) {
  247. o := orm.NewOrm()
  248. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE frequency=? AND index_name NOT LIKE "%停%" ORDER BY file_index DESC `
  249. err = o.Raw(sql, frequency).QueryRow(&item)
  250. return
  251. }
  252. // GetNoMergeIndexByFrequencyPageReq 获取未合并的指标列表
  253. type GetNoMergeIndexByFrequencyPageReq struct {
  254. Frequency string
  255. Limit int
  256. }
  257. // GetNoMergeIndexByFrequencyPage 获取未合并的指标列表
  258. func (m *BaseFromMysteelChemicalIndex) GetNoMergeIndexByFrequencyPage(frequency string, limit int) (items []*BaseFromMysteelChemicalIndex, err error) {
  259. o := orm.NewOrm()
  260. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE frequency=? AND merge_file_path = '' limit ? `
  261. _, err = o.Raw(sql, frequency, limit).QueryRows(&items)
  262. return
  263. }
  264. type GetIndexCountByMergeFilePathReq struct {
  265. MergeFilePath string
  266. }
  267. func (m *BaseFromMysteelChemicalIndex) GetIndexCountByMergeFilePath(mergeFilePath string) (total int64, err error) {
  268. o := orm.NewOrm()
  269. sql := `SELECT count(1) AS total FROM base_from_mysteel_chemical_index WHERE merge_file_path = ?`
  270. err = o.Raw(sql, mergeFilePath).QueryRow(&total)
  271. return
  272. }
  273. // GetIndexRefreshMethanolByMergeFile 根据合并文件去分组查询需要刷新的甲醇文件
  274. func (m *BaseFromMysteelChemicalIndex) GetIndexRefreshMethanolByMergeFile() (items []*BaseFromMysteelChemicalIndex, err error) {
  275. o := orm.NewOrm()
  276. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE merge_file_path <> '' AND base_from_mysteel_chemical_classify_id IN(54,55,56,57) GROUP BY merge_file_path`
  277. _, err = o.Raw(sql).QueryRows(&items)
  278. return
  279. }
  280. // GetIndexRefreshMethanolByTimely 获取需要及时刷新的文件
  281. func (m *BaseFromMysteelChemicalIndex) GetIndexRefreshMethanolByTimely() (items []*BaseFromMysteelChemicalIndex, err error) {
  282. o := orm.NewOrm()
  283. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE merge_file_path <> '' AND is_refresh = 1 GROUP BY merge_file_path`
  284. _, err = o.Raw(sql).QueryRows(&items)
  285. return
  286. }
  287. type GetIndexByFrequencyReq struct {
  288. Frequency string
  289. }
  290. // GetIndexByFrequency 根据频率获取指标
  291. func (m *BaseFromMysteelChemicalIndex) GetIndexByFrequency(frequency string) (items []*BaseFromMysteelChemicalIndex, err error) {
  292. o := orm.NewOrm()
  293. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE frequency = ? AND merge_file_path = '' `
  294. _, err = o.Raw(sql, frequency).QueryRows(&items)
  295. return
  296. }
  297. type GetIndexRefreshWeekReq struct {
  298. NowWeekZn string
  299. StartTime string
  300. EndTime string
  301. }
  302. func (m *BaseFromMysteelChemicalIndex) GetIndexRefreshWeek(nowWeekZn, startTime, endTime string) (items []*BaseFromMysteelChemicalIndex, err error) {
  303. where := `update_week = ? AND ((update_time >= ? AND update_time <= ?) OR (update_time2 >= ? AND update_time2 <= ?)) `
  304. o := orm.NewOrm()
  305. sql := fmt.Sprintf(`SELECT * FROM base_from_mysteel_chemical_index WHERE %s `, where)
  306. _, err = o.Raw(sql, nowWeekZn, startTime, endTime, startTime, endTime).QueryRows(&items)
  307. return
  308. }
  309. // UpdateIndexReq 根据编码更新指标请求体
  310. type UpdateIndexReq struct {
  311. Item *BaseFromMysteelChemicalIndex
  312. UpdateCols []string
  313. }
  314. func (m *BaseFromMysteelChemicalIndex) UpdateIndex(item *BaseFromMysteelChemicalIndex, updateCols []string) (err error) {
  315. if item == nil {
  316. return
  317. }
  318. if len(updateCols) == 0 {
  319. return
  320. }
  321. o := orm.NewOrm()
  322. _, err = o.Update(item, updateCols...)
  323. return
  324. }
  325. // MultiUpdateIndex 批量更新指标
  326. func (m *BaseFromMysteelChemicalIndex) MultiUpdateIndex(items []UpdateIndexReq) (err error) {
  327. if len(items) == 0 {
  328. return
  329. }
  330. o := orm.NewOrm()
  331. for _, v := range items {
  332. if len(v.UpdateCols) == 0 {
  333. continue
  334. }
  335. if v.Item == nil {
  336. continue
  337. }
  338. if _, e := o.Update(v.Item, v.UpdateCols...); e != nil {
  339. err = fmt.Errorf("update err: %s", e.Error())
  340. return
  341. }
  342. }
  343. return
  344. }
  345. type HandleMysteelIndex struct {
  346. IndexName string `description:"指标名称"`
  347. IndexCode string `description:"指标编码"`
  348. Unit string `description:"单位"`
  349. Source string `description:"数据来源"`
  350. Frequency string `description:"频度"`
  351. StartDate string `description:"开始日期"`
  352. EndDate string `description:"结束日期"`
  353. Describe string `description:"指标描述"`
  354. UpdateDate string `description:"更新日期"`
  355. ExcelDataMap map[string]string
  356. }
  357. type HandleMysteelIndexResp struct {
  358. List []*HandleMysteelIndex
  359. }
  360. // 钢联化工指标数据
  361. type BaseFromMysteelChemicalData struct {
  362. BaseFromMysteelChemicalDataId int64 `orm:"column(base_from_mysteel_chemical_data_id);pk"`
  363. BaseFromMysteelChemicalIndexId int64
  364. IndexCode string
  365. DataTime time.Time
  366. Value string
  367. UpdateDate string
  368. CreateTime time.Time `description:"创建时间"`
  369. ModifyTime time.Time `description:"修改时间"`
  370. }
  371. // GetIndexDataList 根据指标编码获取数据
  372. func (d *BaseFromMysteelChemicalData) GetIndexDataList(indexCode string) (item []*BaseFromMysteelChemicalData, err error) {
  373. o := orm.NewOrm()
  374. sql := `SELECT * FROM base_from_mysteel_chemical_data WHERE index_code = ? `
  375. _, err = o.Raw(sql, indexCode).QueryRows(&item)
  376. return
  377. }
  378. // Update 修改
  379. func (r *BaseFromMysteelChemicalData) Update(updateCols []string) (err error) {
  380. o := orm.NewOrm()
  381. _, err = o.Update(r, updateCols...)
  382. return
  383. }
  384. // Add 新增
  385. func (r *BaseFromMysteelChemicalData) Add(list []BaseFromMysteelChemicalData) (err error) {
  386. o := orm.NewOrm()
  387. _, err = o.InsertMulti(len(list), list)
  388. return
  389. }
  390. type AddMysteelIndexResp struct {
  391. EdbCode string `description:"指标编码"`
  392. BaseFromMysteelChemicalClassifyId int `description:"指标分类"`
  393. }
  394. type GetIndexDetailReq struct {
  395. IndexCode string `description:"指标编码"`
  396. }
  397. // GetIndexCountByFrequency 获取未合并的指标总数量
  398. func (m *BaseFromMysteelChemicalIndex) GetIndexCountByFrequency(condition string, pars []interface{}) (total int64, err error) {
  399. o := orm.NewOrm()
  400. sql := `SELECT count(1) AS total FROM base_from_mysteel_chemical_index WHERE 1=1 `
  401. if condition != "" {
  402. sql += condition
  403. }
  404. err = o.Raw(sql, pars).QueryRow(&total)
  405. return
  406. }
  407. // GetNoMergeIndexByFrequencyPage 获取未合并的指标列表
  408. func (m *BaseFromMysteelChemicalIndex) GetIndexLimitByFrequency(frequency string, limit int) (items []*BaseFromMysteelChemicalIndex, err error) {
  409. o := orm.NewOrm()
  410. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE frequency=? limit ? `
  411. _, err = o.Raw(sql, frequency, limit).QueryRows(&items)
  412. return
  413. }
  414. type GetIndexPageByFrequencyPageReq struct {
  415. Frequency string
  416. EndDate string
  417. StartSize int
  418. PageSize int
  419. }
  420. // GetNoMergeIndexByFrequencyPage 获取未合并的指标列表
  421. func (m *BaseFromMysteelChemicalIndex) GetIndexPageByFrequency(condition string, pars []interface{}, startSize, pageSize int) (items []*BaseFromMysteelChemicalIndex, err error) {
  422. o := orm.NewOrm()
  423. sql := `SELECT * FROM base_from_mysteel_chemical_index WHERE 1=1 `
  424. if condition != "" {
  425. sql += condition
  426. }
  427. sql += ` LIMIT ?,? `
  428. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  429. return
  430. }
  431. func (d *BaseFromMysteelChemicalData) GetMysteelIndexInfoMaxAndMinInfo(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  432. o := orm.NewOrm()
  433. 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_mysteel_chemical_data WHERE index_code=? `
  434. err = o.Raw(sql, indexCode).QueryRow(&item)
  435. return
  436. }
  437. func (d *BaseFromMysteelChemicalData) ModifyMysteelIndexMaxAndMinInfo(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) {
  438. o := orm.NewOrm()
  439. sql := ` UPDATE base_from_mysteel_chemical_index SET start_date=?,end_date=?,modify_time=NOW() WHERE index_code=? `
  440. _, err = o.Raw(sql, item.MinDate, item.MaxDate, indexCode).Exec()
  441. return
  442. }