base_from_oilchem.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type BaseFromOilchemIndex struct {
  11. BaseFromOilchemIndexId int // 主键ID
  12. IndexCode string // 指标编码
  13. IndexName string // 指标名称
  14. ClassifyId uint // 分类ID
  15. Unit string // 单位
  16. Frequency string // 频度
  17. Describe string // 指标描述
  18. Sort int // 排序
  19. CreateTime time.Time // 创建时间
  20. ModifyTime time.Time // 修改时间
  21. }
  22. type BaseFromOilchemData struct {
  23. BaseFromOilchemDataId int // 数据表ID
  24. BaseFromOilchemIndexId int // 指标ID
  25. IndexCode string // 指标编码
  26. DataTime string
  27. Value string
  28. CreateTime time.Time // 创建时间
  29. ModifyTime time.Time // 修改时间
  30. }
  31. //添加数据
  32. func AddBaseFromOilchemIndexMuti(items []*BaseFromOilchemIndex) (err error) {
  33. o := orm.NewOrm()
  34. _, err = o.InsertMulti(500, items)
  35. return
  36. }
  37. func AddBaseFromOilchemIndex(item *BaseFromOilchemIndex) (lastId int64, err error) {
  38. o := orm.NewOrm()
  39. lastId, err = o.Insert(item)
  40. return
  41. }
  42. func AddBaseFromOilchemData(item *BaseFromOilchemData) (lastId int64, err error) {
  43. o := orm.NewOrm()
  44. lastId, err = o.Insert(item)
  45. return
  46. }
  47. func AddEdbDataFromOilchem(edbCode string) (err error) {
  48. o := orm.NewOrm()
  49. oilchemBaseDataAll, err := GetOilchemDataByCode(edbCode)
  50. if err != nil && err.Error() != utils.ErrNoRow() {
  51. return
  52. }
  53. var isAdd bool
  54. addSql := ` INSERT INTO edb_data_oilchem(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  55. existMap := make(map[string]string)
  56. for _, sv := range oilchemBaseDataAll {
  57. eDate := sv.DataTime
  58. var timeStr string
  59. var dataTime time.Time
  60. var sDataTime string
  61. var timestamp int64
  62. sDataTime = eDate
  63. dataTime, err = time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  64. if err != nil {
  65. fmt.Println("time.Parse Err:" + eDate)
  66. return err
  67. }
  68. timestamp = dataTime.UnixNano() / 1e6
  69. timeStr = fmt.Sprintf("%d", timestamp)
  70. value := strings.Replace(sv.Value, "%", "", -1)
  71. if _, ok := existMap[sDataTime]; !ok {
  72. addSql += GetAddSql("0", edbCode, sDataTime, timeStr, value)
  73. fmt.Println("edbCode:", edbCode)
  74. fmt.Println("sDataTime:", sDataTime)
  75. fmt.Println("timeStr:", timeStr)
  76. fmt.Println("value:", value)
  77. isAdd = true
  78. }
  79. existMap[eDate] = value
  80. }
  81. if isAdd {
  82. addSql = strings.TrimRight(addSql, ",")
  83. utils.FileLog.Info("addSql:" + addSql)
  84. _, err = o.Raw(addSql).Exec()
  85. if err != nil {
  86. return err
  87. }
  88. }
  89. return
  90. }
  91. // GetOilchemDataByCode
  92. func GetOilchemDataByCode(indexCode string) (items []*BaseFromOilchemData, err error) {
  93. o := orm.NewOrm()
  94. sql := "SELECT * FROM base_from_oilchem_data WHERE index_code=? "
  95. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  96. return
  97. }
  98. // RefreshEdbDataFromOilchem 刷新隆众资讯
  99. func RefreshEdbDataFromOilchem(edbInfoId int, edbCode, startDate string) (err error) {
  100. source := utils.DATA_SOURCE_OILCHEM
  101. subSource := utils.DATA_SUB_SOURCE_EDB
  102. o := orm.NewOrm()
  103. if err != nil {
  104. return
  105. }
  106. edbInfoIdStr := strconv.Itoa(edbInfoId)
  107. //计算数据
  108. var condition string
  109. var pars []interface{}
  110. if edbCode != "" {
  111. condition += " AND index_code=? "
  112. pars = append(pars, edbCode)
  113. }
  114. if startDate != "" {
  115. condition += " AND data_time>=? "
  116. pars = append(pars, startDate)
  117. }
  118. oilchemDataList, err := GetBaseFromOilchemDataByCondition(condition, pars)
  119. if err != nil {
  120. return
  121. }
  122. // 真实数据的最大日期 , 插入规则配置的日期
  123. var realDataMaxDate, edbDataInsertConfigDate time.Time
  124. var edbDataInsertConfig *EdbDataInsertConfig
  125. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  126. {
  127. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  128. if err != nil && err.Error() != utils.ErrNoRow() {
  129. return
  130. }
  131. if edbDataInsertConfig != nil {
  132. edbDataInsertConfigDate = edbDataInsertConfig.Date
  133. }
  134. }
  135. var existCondition string
  136. var existPars []interface{}
  137. existCondition += " AND edb_info_id=? "
  138. existPars = append(existPars, edbInfoId)
  139. if startDate != "" {
  140. existCondition += " AND data_time>=? "
  141. existPars = append(existPars, startDate)
  142. }
  143. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  144. if err != nil {
  145. return err
  146. }
  147. existMap := make(map[string]*EdbInfoSearchData)
  148. for _, v := range existList {
  149. existMap[v.DataTime] = v
  150. }
  151. addSql := ` INSERT INTO edb_data_oilchem(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  152. var isAdd bool
  153. for _, v := range oilchemDataList {
  154. item := v
  155. eDate := item.DataTime
  156. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  157. if err != nil {
  158. return err
  159. }
  160. if findItem, ok := existMap[v.DataTime]; !ok {
  161. sValue := item.Value
  162. timestamp := dataTime.UnixNano() / 1e6
  163. timeStr := fmt.Sprintf("%d", timestamp)
  164. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  165. isAdd = true
  166. } else {
  167. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Value {
  168. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, item.Value)
  169. if err != nil {
  170. return err
  171. }
  172. }
  173. }
  174. // 下面代码主要目的是处理掉手动插入的数据判断
  175. {
  176. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  177. realDataMaxDate = dataTime
  178. }
  179. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  180. isFindConfigDateRealData = true
  181. }
  182. }
  183. }
  184. // 处理手工数据补充的配置
  185. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  186. if isAdd {
  187. addSql = strings.TrimRight(addSql, ",")
  188. _, err = o.Raw(addSql).Exec()
  189. if err != nil {
  190. fmt.Println("RefreshEdbDataFromSci add Err", err.Error())
  191. return
  192. }
  193. }
  194. return
  195. }
  196. func GetBaseFromOilchemDataByCondition(condition string, pars []interface{}) (list []*BaseFromOilchemData, err error) {
  197. o := orm.NewOrm()
  198. sql := `SELECT * FROM base_from_oilchem_data WHERE 1=1 `
  199. if condition != "" {
  200. sql += condition
  201. }
  202. _, err = o.Raw(sql, pars).QueryRows(&list)
  203. return
  204. }
  205. type BaseFromOilchemIndexReq struct {
  206. BaseFromOilchemIndexId int // 主键ID
  207. IndexCode string // 指标编码
  208. IndexName string // 指标名称
  209. ClassifyId uint // 分类ID
  210. Unit string // 单位
  211. Source string // 数据来源
  212. Frequency string // 频度
  213. Describe string // 指标描述
  214. DataTime string // 数据日期
  215. Value string // 数据值
  216. Sort int // 排序
  217. CreateTime time.Time // 创建时间
  218. ModifyTime time.Time // 修改时间
  219. IndexNameStr string // 指标名称字符串
  220. MarketName string // 市场名称
  221. }
  222. type HandleOielchemEdbDataReq struct {
  223. List []*BaseFromOilchemIndexReq
  224. }
  225. func GetBaseFromOilchemIndex() (list []*BaseFromOilchemIndex, err error) {
  226. o := orm.NewOrm()
  227. sql := `SELECT * FROM base_from_oilchem_index group by index_name `
  228. _, err = o.Raw(sql).QueryRows(&list)
  229. return
  230. }
  231. func GetBaseFromOilchemData(indexCode, dataTime string) (item *BaseFromOilchemData, err error) {
  232. o := orm.NewOrm()
  233. sql := `SELECT * FROM base_from_oilchem_data where index_code=? And data_time = ? `
  234. err = o.Raw(sql, indexCode, dataTime).QueryRow(&item)
  235. return
  236. }
  237. // UpdateBaseFromSci99Data
  238. func UpdateBaseFromOilchemData(value , indexCode, dataTime string) (err error) {
  239. o := orm.NewOrm()
  240. sql := `UPDATE base_from_sci99_data SET value=?,modify_time=NOW() WHERE index_code = ? AND data_time = ? `
  241. _, err = o.Raw(sql, value, indexCode, dataTime).Exec()
  242. return
  243. }