base_from_clarksons.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 BaseFromClarksonsIndex struct {
  11. BaseFromClarksonsIndexId int `orm:"pk"`
  12. ClassifyId int `description:"指标分类id"`
  13. IndexCode string `description:"指标编码"`
  14. IndexName string `description:"指标名称"`
  15. Unit string `description:"单位"`
  16. Frequency string `description:"频度"`
  17. Sort int `description:"排序"`
  18. CreateTime time.Time
  19. ModifyTime time.Time
  20. }
  21. type BaseFromClarksonsData struct {
  22. BaseFromClarksonsDataId int `orm:"column(base_from_clarksons_data_id);pk"`
  23. BaseFromClarksonsIndexId int `description:"指标id"`
  24. IndexCode string `description:"指标编码"`
  25. DataTime string `description:"数据日期"`
  26. Value string `description:"数据值"`
  27. CreateTime time.Time `description:"创建时间"`
  28. ModifyTime time.Time `description:"修改时间"`
  29. }
  30. // 查询指标
  31. func GetBaseFromClarksonsIndex() (items []*BaseFromClarksonsIndex, err error) {
  32. o := orm.NewOrm()
  33. sql := `SELECT * FROM base_from_clarksons_index`
  34. _, err = o.Raw(sql).QueryRows(&items)
  35. return
  36. }
  37. // 查询指标
  38. func GetBaseFromClarksonsData() (items []*BaseFromClarksonsData, err error) {
  39. o := orm.NewOrm()
  40. sql := `SELECT * FROM base_from_clarksons_data`
  41. _, err = o.Raw(sql).QueryRows(&items)
  42. return
  43. }
  44. // 添加数据
  45. func AddBaseFromClarksonsIndex(item *BaseFromClarksonsIndex) (lastId int64, err error) {
  46. o := orm.NewOrm()
  47. lastId, err = o.Insert(item)
  48. return
  49. }
  50. func AddBaseFromClarksonsDataMuti(items []*BaseFromClarksonsData) (count int64, err error) {
  51. o := orm.NewOrm()
  52. count, err = o.InsertMulti(500, items)
  53. return
  54. }
  55. func UpdateBaseFromClarksonsIndex(item *BaseFromClarksonsIndex) (err error) {
  56. o := orm.NewOrm()
  57. sql := `UPDATE base_from_clarksons_index SET frequency=? WHERE index_code=? `
  58. _, err = o.Raw(sql, item.Frequency, item.IndexCode).Exec()
  59. return
  60. }
  61. func AddEdbDataFromClarksons(edbCode string) (err error) {
  62. o := orm.NewOrm()
  63. coalBaseDataAll, err := GetClarksonsindexByCode(edbCode)
  64. if err != nil && err.Error() != utils.ErrNoRow() {
  65. return
  66. }
  67. var isAdd bool
  68. addSql := ` INSERT INTO edb_data_clarksons(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  69. existMap := make(map[string]string)
  70. for _, sv := range coalBaseDataAll {
  71. eDate := sv.DataTime
  72. var timeStr string
  73. var dataTime time.Time
  74. var sDataTime string
  75. var timestamp int64
  76. sDataTime = eDate
  77. dataTime, err = time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  78. if err != nil {
  79. fmt.Println("time.Parse Err:" + eDate)
  80. return err
  81. }
  82. timestamp = dataTime.UnixNano() / 1e6
  83. timeStr = fmt.Sprintf("%d", timestamp)
  84. value := strings.Replace(sv.Value, "%", "", -1)
  85. if _, ok := existMap[sDataTime]; !ok {
  86. addSql += GetAddSql("0", edbCode, sDataTime, timeStr, value)
  87. fmt.Println("edbCode:", edbCode)
  88. fmt.Println("sDataTime:", sDataTime)
  89. fmt.Println("timeStr:", timeStr)
  90. fmt.Println("value:", value)
  91. isAdd = true
  92. }
  93. existMap[eDate] = value
  94. }
  95. if isAdd {
  96. addSql = strings.TrimRight(addSql, ",")
  97. utils.FileLog.Info("addSql:" + addSql)
  98. _, err = o.Raw(addSql).Exec()
  99. if err != nil {
  100. return err
  101. }
  102. }
  103. return
  104. }
  105. func RefreshEdbDataFromClarksons(edbInfoId int, edbCode, startDate string) (err error) {
  106. source := utils.DATA_SOURCE_CLARKSONS
  107. subSource := utils.DATA_SUB_SOURCE_EDB
  108. o := orm.NewOrm()
  109. if err != nil {
  110. return
  111. }
  112. edbInfoIdStr := strconv.Itoa(edbInfoId)
  113. //计算数据
  114. var condition string
  115. var pars []interface{}
  116. if edbCode != "" {
  117. condition += " AND index_code=? "
  118. pars = append(pars, edbCode)
  119. }
  120. if startDate != "" {
  121. condition += " AND data_time>=? "
  122. pars = append(pars, startDate)
  123. }
  124. glDataList, err := GetClarksonsDataByTradeCode(condition, pars)
  125. if err != nil {
  126. return
  127. }
  128. // 真实数据的最大日期 , 插入规则配置的日期
  129. var realDataMaxDate, edbDataInsertConfigDate time.Time
  130. var edbDataInsertConfig *EdbDataInsertConfig
  131. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  132. {
  133. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  134. if err != nil && err.Error() != utils.ErrNoRow() {
  135. return
  136. }
  137. if edbDataInsertConfig != nil {
  138. edbDataInsertConfigDate = edbDataInsertConfig.Date
  139. }
  140. }
  141. //获取指标所有数据
  142. var existCondition string
  143. var existPars []interface{}
  144. existCondition += " AND edb_info_id=? "
  145. existPars = append(existPars, edbInfoId)
  146. if startDate != "" {
  147. existCondition += " AND data_time>=? "
  148. existPars = append(existPars, startDate)
  149. }
  150. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  151. if err != nil {
  152. return err
  153. }
  154. existMap := make(map[string]*EdbInfoSearchData)
  155. for _, v := range existList {
  156. existMap[v.DataTime] = v
  157. }
  158. addSql := ` INSERT INTO edb_data_clarksons(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  159. var isAdd bool
  160. for _, v := range glDataList {
  161. var value string
  162. value = strings.Replace(v.Value, "%", "", -1)
  163. item := v
  164. itemValue := value
  165. if _, ok := existMap[v.DataTime]; !ok {
  166. eDate := item.DataTime
  167. var timeStr string
  168. var dataTime time.Time
  169. var sDataTime string
  170. var timestamp int64
  171. sDataTime = eDate
  172. dataTime, err = time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  173. if err != nil {
  174. fmt.Println("time.Parse Err:" + eDate)
  175. return err
  176. }
  177. timestamp = dataTime.UnixNano() / 1e6
  178. timeStr = fmt.Sprintf("%d", timestamp)
  179. sValue := itemValue
  180. if sValue != "" {
  181. saveValue := sValue
  182. if findItem, ok := existMap[eDate]; !ok {
  183. addSql += GetAddSql(edbInfoIdStr, edbCode, sDataTime, timeStr, saveValue)
  184. isAdd = true
  185. } else {
  186. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  187. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  188. if err != nil {
  189. return err
  190. }
  191. }
  192. }
  193. }
  194. // 下面代码主要目的是处理掉手动插入的数据判断
  195. {
  196. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  197. realDataMaxDate = dataTime
  198. }
  199. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  200. isFindConfigDateRealData = true
  201. }
  202. }
  203. }
  204. }
  205. // 处理手工数据补充的配置
  206. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  207. if isAdd {
  208. addSql = strings.TrimRight(addSql, ",")
  209. _, err = o.Raw(addSql).Exec()
  210. if err != nil {
  211. return err
  212. }
  213. }
  214. return
  215. }
  216. // GetClarksonsindexByCode
  217. func GetClarksonsindexByCode(indexCode string) (items []*BaseFromClarksonsData, err error) {
  218. o := orm.NewOrm()
  219. sql := "SELECT * FROM base_from_clarksons_data WHERE index_code=? "
  220. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  221. return
  222. }
  223. func GetClarksonsDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromClarksonsData, err error) {
  224. sql := ` SELECT * FROM base_from_clarksons_data WHERE 1=1 `
  225. o := orm.NewOrm()
  226. if condition != "" {
  227. sql += condition
  228. }
  229. sql += ` ORDER BY data_time DESC `
  230. _, err = o.Raw(sql, pars).QueryRows(&item)
  231. return
  232. }