base_from_mysteel_chemical.go 17 KB

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