base_from_sci.go 8.4 KB

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