base_from_sci.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 BaseFromSciData struct {
  11. SciDataId int `orm:"column(sci_data_id);pk"`
  12. BaseFromSciIndexId int
  13. IndexCode string
  14. DataTime string
  15. Value string
  16. CreateTime time.Time
  17. ModifyTime time.Time
  18. DataTimestamp int64
  19. }
  20. // Update 修改
  21. func (r *BaseFromSciData) Update(updateCols []string) (err error) {
  22. o := orm.NewOrm()
  23. _, err = o.Update(r, updateCols...)
  24. return
  25. }
  26. // GetIndexDataList 获取所有的指标数据
  27. func (r *BaseFromSciData) GetIndexDataList(indexCode string) (items []*BaseFromSciData, err error) {
  28. o := orm.NewOrm()
  29. sql := `SELECT * FROM base_from_sci_data WHERE 1=1 AND index_code = ? `
  30. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  31. return
  32. }
  33. // BatchAdd 批量添加指标
  34. func (r *BaseFromSciData) BatchAdd(list []*BaseFromSciData) (err error) {
  35. o := orm.NewOrm()
  36. _, err = o.InsertMulti(len(list), list)
  37. return
  38. }
  39. func GetBaseFromSciDataByCondition(condition string, pars []interface{}) (list []*BaseFromSciData, err error) {
  40. o := orm.NewOrm()
  41. sql := `SELECT * FROM base_from_sci_data WHERE 1=1 `
  42. if condition != "" {
  43. sql += condition
  44. }
  45. _, err = o.Raw(sql, pars).QueryRows(&list)
  46. return
  47. }
  48. // AddEdbDataFromSci 新增卓创(红桃3)指标数据
  49. func AddEdbDataFromSci(edbCode string) (err error) {
  50. o := orm.NewOrm()
  51. var condition string
  52. var pars []interface{}
  53. if edbCode != "" {
  54. condition += " AND index_code=? "
  55. pars = append(pars, edbCode)
  56. }
  57. sciBaseDataAll, err := GetBaseFromSciDataByCondition(condition, pars)
  58. if err != nil && err.Error() != utils.ErrNoRow() {
  59. return
  60. }
  61. var isAdd bool
  62. addSql := ` INSERT INTO edb_data_sci(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  63. existMap := make(map[string]string)
  64. for _, sv := range sciBaseDataAll {
  65. eDate := sv.DataTime
  66. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  67. if err != nil {
  68. return err
  69. }
  70. timestamp := dataTime.UnixNano() / 1e6
  71. timeStr := fmt.Sprintf("%d", timestamp)
  72. if _, ok := existMap[eDate]; !ok {
  73. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Value)
  74. isAdd = true
  75. }
  76. existMap[eDate] = sv.Value
  77. }
  78. if isAdd {
  79. addSql = strings.TrimRight(addSql, ",")
  80. utils.FileLog.Info("addSql:" + addSql)
  81. _, err = o.Raw(addSql).Exec()
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. return
  87. }
  88. // RefreshEdbDataFromSci 刷新卓创(红桃3)指标数据
  89. func RefreshEdbDataFromSci(edbInfoId int, edbCode, startDate string) (err error) {
  90. source := utils.DATA_SOURCE_SCI
  91. subSource := utils.DATA_SUB_SOURCE_EDB
  92. o := orm.NewOrm()
  93. if err != nil {
  94. return
  95. }
  96. edbInfoIdStr := strconv.Itoa(edbInfoId)
  97. //计算数据
  98. var condition string
  99. var pars []interface{}
  100. if edbCode != "" {
  101. condition += " AND index_code=? "
  102. pars = append(pars, edbCode)
  103. }
  104. if startDate != "" {
  105. condition += " AND data_time>=? "
  106. pars = append(pars, startDate)
  107. }
  108. sciDataList, err := GetBaseFromSciDataByCondition(condition, pars)
  109. if err != nil {
  110. return
  111. }
  112. // 真实数据的最大日期 , 插入规则配置的日期
  113. var realDataMaxDate, edbDataInsertConfigDate time.Time
  114. var edbDataInsertConfig *EdbDataInsertConfig
  115. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  116. {
  117. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  118. if err != nil && err.Error() != utils.ErrNoRow() {
  119. return
  120. }
  121. if edbDataInsertConfig != nil {
  122. edbDataInsertConfigDate = edbDataInsertConfig.Date
  123. }
  124. }
  125. var existCondition string
  126. var existPars []interface{}
  127. existCondition += " AND edb_info_id=? "
  128. existPars = append(existPars, edbInfoId)
  129. if startDate != "" {
  130. existCondition += " AND data_time>=? "
  131. existPars = append(existPars, startDate)
  132. }
  133. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  134. if err != nil {
  135. return err
  136. }
  137. existMap := make(map[string]*EdbInfoSearchData)
  138. for _, v := range existList {
  139. existMap[v.DataTime] = v
  140. }
  141. addSql := ` INSERT INTO edb_data_sci(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  142. var isAdd bool
  143. for _, v := range sciDataList {
  144. item := v
  145. eDate := item.DataTime
  146. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  147. if err != nil {
  148. return err
  149. }
  150. if findItem, ok := existMap[v.DataTime]; !ok {
  151. sValue := item.Value
  152. timestamp := dataTime.UnixNano() / 1e6
  153. timeStr := fmt.Sprintf("%d", timestamp)
  154. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  155. isAdd = true
  156. } else {
  157. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Value {
  158. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, item.Value)
  159. if err != nil {
  160. return err
  161. }
  162. }
  163. }
  164. // 下面代码主要目的是处理掉手动插入的数据判断
  165. {
  166. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  167. realDataMaxDate = dataTime
  168. }
  169. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  170. isFindConfigDateRealData = true
  171. }
  172. }
  173. }
  174. // 处理手工数据补充的配置
  175. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  176. if isAdd {
  177. addSql = strings.TrimRight(addSql, ",")
  178. _, err = o.Raw(addSql).Exec()
  179. if err != nil {
  180. fmt.Println("RefreshEdbDataFromSci add Err", err.Error())
  181. return
  182. }
  183. }
  184. return
  185. }
  186. // HandleSciExcelDataReq 卓创(红桃3)的excel数据
  187. type HandleSciExcelDataReq struct {
  188. DataMap map[string]map[string]string
  189. IndexNameList []string
  190. ThirdIndexIdList []string
  191. FrequencyList []string
  192. UnitList []string
  193. FilePath string `description:"文件路径"`
  194. TerminalCode string `description:"编码"`
  195. }
  196. // BaseFromSciIndex 红桃3指标表格
  197. type BaseFromSciIndex struct {
  198. BaseFromSciIndexId int `orm:"column(base_from_sci_index_id);pk" json:"base_from_sci_index_id"` //序号
  199. ClassifyId int `gorm:"column:classify_id" json:"classify_id"`
  200. IndexCode string `gorm:"column:index_code" json:"index_code" description:"指标编码"`
  201. IndexName string `gorm:"column:index_name" json:"index_name" description:"指标名称"`
  202. Frequency string `gorm:"column:frequency" json:"frequency"`
  203. Unit string `gorm:"column:unit" json:"unit"`
  204. StartDate time.Time `gorm:"column:start_date" json:"start_date"`
  205. EndDate time.Time `gorm:"column:end_date" json:"end_date"`
  206. CreateTime time.Time `gorm:"autoCreateTime;column:create_time" json:"create_time"` //创建时间
  207. ModifyTime time.Time `gorm:"autoUpdateTime:milli;column:modify_time" json:"modify_time"` //最后更新时间
  208. FilePath string `gorm:"column:file_path" json:"file_path"` // 文件路径
  209. TerminalCode string `gorm:"column:terminal_code" json:"terminal_code"` // 指标编码
  210. }
  211. // TableName get sql table name.获取数据库表名
  212. func (r *BaseFromSciIndex) TableName() string {
  213. return "base_from_sci_index"
  214. }
  215. // Update 修改
  216. func (r *BaseFromSciIndex) Update(updateCols []string) (err error) {
  217. o := orm.NewOrm()
  218. _, err = o.Update(r, updateCols...)
  219. return
  220. }
  221. // GetAllIndex 获取所有的指标
  222. func (r *BaseFromSciIndex) GetAllIndex() (items []*BaseFromSciIndex, err error) {
  223. o := orm.NewOrm()
  224. sql := `SELECT * FROM base_from_sci_index WHERE 1=1 `
  225. _, err = o.Raw(sql).QueryRows(&items)
  226. return
  227. }
  228. // BatchAdd 批量添加指标
  229. func (r *BaseFromSciIndex) BatchAdd(list []*BaseFromSciIndex) (err error) {
  230. o := orm.NewOrm()
  231. _, err = o.InsertMulti(len(list), list)
  232. return
  233. }