base_from_sci.go 8.6 KB

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