base_from_sci.go 9.0 KB

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