base_from_baiinfo.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 BaseFromBaiinfoData struct {
  11. BaiinfoDataId int `orm:"column(baiinfo_data_id);pk"`
  12. BaseFromBaiinfoIndexId int
  13. IndexCode string
  14. DataTime string
  15. Value string
  16. CreateTime time.Time
  17. ModifyTime time.Time
  18. DataTimestamp int64
  19. }
  20. func GetBaseFromBaiinfoDataByCondition(condition string, pars []interface{}) (list []*BaseFromBaiinfoData, err error) {
  21. o := orm.NewOrm()
  22. sql := `SELECT * FROM base_from_baiinfo_data WHERE 1=1 `
  23. if condition != "" {
  24. sql += condition
  25. }
  26. _, err = o.Raw(sql, pars).QueryRows(&list)
  27. return
  28. }
  29. // AddEdbDataFromBaiinfo 新增百川盈弗指标数据
  30. func AddEdbDataFromBaiinfo(edbCode string) (err error) {
  31. o := orm.NewOrm()
  32. var condition string
  33. var pars []interface{}
  34. if edbCode != "" {
  35. condition += " AND index_code=? "
  36. pars = append(pars, edbCode)
  37. }
  38. baiinfoBaseDataAll, err := GetBaseFromBaiinfoDataByCondition(condition, pars)
  39. if err != nil && err.Error() != utils.ErrNoRow() {
  40. return
  41. }
  42. var isAdd bool
  43. addSql := ` INSERT INTO edb_data_baiinfo(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  44. existMap := make(map[string]string)
  45. for _, sv := range baiinfoBaseDataAll {
  46. eDate := sv.DataTime
  47. dataTime, err := time.Parse(utils.FormatDate, eDate)
  48. if err != nil {
  49. return err
  50. }
  51. timestamp := dataTime.UnixNano() / 1e6
  52. timeStr := fmt.Sprintf("%d", timestamp)
  53. if _, ok := existMap[eDate]; !ok {
  54. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Value)
  55. isAdd = true
  56. }
  57. existMap[eDate] = sv.Value
  58. }
  59. if isAdd {
  60. addSql = strings.TrimRight(addSql, ",")
  61. utils.FileLog.Info("addSql:" + addSql)
  62. _, err = o.Raw(addSql).Exec()
  63. if err != nil {
  64. return err
  65. }
  66. }
  67. return
  68. }
  69. // RefreshEdbDataFromBaiinfo 刷新百川盈弗指标数据
  70. func RefreshEdbDataFromBaiinfo(edbInfoId int, edbCode, startDate string) (err error) {
  71. source := utils.DATA_SOURCE_BAIINFO
  72. subSource := utils.DATA_SUB_SOURCE_EDB
  73. o := orm.NewOrm()
  74. if err != nil {
  75. return
  76. }
  77. edbInfoIdStr := strconv.Itoa(edbInfoId)
  78. //计算数据
  79. var condition string
  80. var pars []interface{}
  81. if edbCode != "" {
  82. condition += " AND index_code=? "
  83. pars = append(pars, edbCode)
  84. }
  85. if startDate != "" {
  86. condition += " AND data_time>=? "
  87. pars = append(pars, startDate)
  88. }
  89. baiinfoDataList, err := GetBaseFromBaiinfoDataByCondition(condition, pars)
  90. if err != nil {
  91. return
  92. }
  93. // 真实数据的最大日期 , 插入规则配置的日期
  94. var realDataMaxDate, edbDataInsertConfigDate time.Time
  95. var edbDataInsertConfig *EdbDataInsertConfig
  96. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  97. {
  98. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  99. if err != nil && err.Error() != utils.ErrNoRow() {
  100. return
  101. }
  102. if edbDataInsertConfig != nil {
  103. edbDataInsertConfigDate = edbDataInsertConfig.Date
  104. }
  105. }
  106. var existCondition string
  107. var existPars []interface{}
  108. existCondition += " AND edb_info_id=? "
  109. existPars = append(existPars, edbInfoId)
  110. if startDate != "" {
  111. existCondition += " AND data_time>=? "
  112. existPars = append(existPars, startDate)
  113. }
  114. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  115. if err != nil {
  116. return err
  117. }
  118. existMap := make(map[string]*EdbInfoSearchData)
  119. for _, v := range existList {
  120. existMap[v.DataTime] = v
  121. }
  122. addSql := ` INSERT INTO edb_data_baiinfo(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  123. var isAdd bool
  124. for _, v := range baiinfoDataList {
  125. item := v
  126. eDate := item.DataTime
  127. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  128. if err != nil {
  129. return err
  130. }
  131. if findItem, ok := existMap[v.DataTime]; !ok {
  132. sValue := item.Value
  133. timestamp := dataTime.UnixNano() / 1e6
  134. timeStr := fmt.Sprintf("%d", timestamp)
  135. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  136. isAdd = true
  137. } else {
  138. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Value {
  139. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, item.Value)
  140. if err != nil {
  141. return err
  142. }
  143. }
  144. }
  145. // 下面代码主要目的是处理掉手动插入的数据判断
  146. {
  147. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  148. realDataMaxDate = dataTime
  149. }
  150. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  151. isFindConfigDateRealData = true
  152. }
  153. }
  154. }
  155. // 处理手工数据补充的配置
  156. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  157. if isAdd {
  158. addSql = strings.TrimRight(addSql, ",")
  159. _, err = o.Raw(addSql).Exec()
  160. if err != nil {
  161. fmt.Println("RefreshEdbDataFromBaiinfo add Err", err.Error())
  162. return
  163. }
  164. }
  165. return
  166. }
  167. // HandleBaiinfoExcelDataReq 百川盈弗的excel数据
  168. type HandleBaiinfoExcelDataReq struct {
  169. BaseFilePath string
  170. RenameFilePath string
  171. IndexName string
  172. IndexCode string
  173. ExcelDataMap map[string]string
  174. TerminalCode string `description:"编码"`
  175. }
  176. type BaseFromBaiinfoIndex struct {
  177. BaseFromBaiinfoIndexId int64 `orm:"column(base_from_baiinfo_index_id);pk"`
  178. IndexCode string
  179. IndexName string
  180. Frequency string
  181. Unit string
  182. StartDate string
  183. EndDate string
  184. Sort int
  185. BaseFileName string
  186. RenameFileName string
  187. TerminalCode string
  188. CreateTime time.Time
  189. ModifyTime time.Time
  190. }
  191. func (d *BaseFromBaiinfoIndex) AddBaseFromBaiinfoIndex() (lastId int64, err error) {
  192. o := orm.NewOrm()
  193. lastId, err = o.Insert(d)
  194. return
  195. }
  196. func AddBaseFromBaiinfoData(item []*BaseFromBaiinfoData) (err error) {
  197. o := orm.NewOrm()
  198. _, err = o.InsertMulti(1, item)
  199. return
  200. }
  201. func (d *BaseFromBaiinfoIndex) GetSmmIndexItem(indexCode string) (item *BaseFromBaiinfoIndex, err error) {
  202. o := orm.NewOrm()
  203. sql := ` SELECT * FROM base_from_baiinfo_index WHERE index_code=? `
  204. err = o.Raw(sql, indexCode).QueryRow(&item)
  205. return
  206. }
  207. func GetBaseFromBaiinfoDataByCode(indexCode string) (list []*BaseFromBaiinfoData, err error) {
  208. o := orm.NewOrm()
  209. sql := ` SELECT * FROM base_from_baiinfo_data WHERE index_code=? `
  210. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  211. return
  212. }
  213. func ModifyBaiinfoIndexSort(baseFromBaiinfoIndexId int64) (err error) {
  214. o := orm.NewOrm()
  215. sql := ` UPDATE base_from_baiinfo_index SET sort=? WHERE base_from_baiinfo_index_id=? `
  216. _, err = o.Raw(sql, baseFromBaiinfoIndexId, baseFromBaiinfoIndexId).Exec()
  217. return
  218. }
  219. func GetBaiinfoIndexInfoMaxAndMinInfo(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  220. o := orm.NewOrm()
  221. 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_baiinfo_data WHERE index_code=? `
  222. err = o.Raw(sql, indexCode).QueryRow(&item)
  223. var latest_value float64
  224. sql = ` SELECT value AS latest_value FROM base_from_baiinfo_data WHERE index_code=? ORDER BY data_time DESC LIMIT 1 `
  225. err = o.Raw(sql, indexCode).QueryRow(&latest_value)
  226. item.LatestValue = latest_value
  227. return
  228. }
  229. func ModifyBaiinfoIndexMaxAndMinInfo(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) {
  230. o := orm.NewOrm()
  231. sql := ` UPDATE base_from_baiinfo_index SET start_date=?,end_date=?,latest_value = ?,modify_time=NOW() WHERE index_code=? `
  232. _, err = o.Raw(sql, item.MinDate, item.MaxDate, item.LatestValue, indexCode).Exec()
  233. return
  234. }
  235. // Update 修改
  236. func (r *BaseFromBaiinfoIndex) Update(updateCols []string) (err error) {
  237. o := orm.NewOrm()
  238. _, err = o.Update(r, updateCols...)
  239. return
  240. }