base_from_mtjh.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. package models
  2. import (
  3. "eta/eta_index_lib/global"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "gorm.io/gorm"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type BaseFromMtjhMapping struct {
  13. BaseFromMtjhMappingId int `gorm:"column:base_from_Mtjh_mapping_id;primaryKey"`
  14. //BaseFromMtjhMappingId int `orm:"column(base_from_Mtjh_mapping_id);pk"`
  15. IndexName string `description:"持买单量指标名称"`
  16. IndexCode string `description:"持买单量指标编码"`
  17. CreateTime time.Time `description:"时间"`
  18. Area string `description:"区域"`
  19. Port string `description:"港口或码头"`
  20. Variety string `description:"品种"`
  21. Unit string `description:"单位"`
  22. Frequency string `description:"频率"`
  23. }
  24. type BaseFromMtjhIndex struct {
  25. BaseFromMtjhIndexId int `gorm:"column:base_from_mtjh_index_id;primaryKey"`
  26. //BaseFromMtjhIndexId int `orm:"column(base_from_mtjh_index_id);pk"`
  27. IndexName string `description:"持买单量指标名称"`
  28. IndexCode string `description:"持买单量指标编码"`
  29. DealValue string `description:"成交量"`
  30. DataTime string `description:"数据日期"`
  31. Area string `description:"区域"`
  32. Port string `description:"港口或码头"`
  33. Variety string `description:"品种"`
  34. Unit string `description:"单位"`
  35. Frequency string `description:"频率"`
  36. CreateTime time.Time `description:"插入时间"`
  37. ModifyTime time.Time `description:"修改时间"`
  38. }
  39. func (e *BaseFromMtjhIndex) AfterFind(db *gorm.DB) (err error) {
  40. e.DataTime = utils.GormDateStrToDateStr(e.DataTime)
  41. return
  42. }
  43. // 查询指标
  44. func GetBaseFromMtjhMapping() (items []*BaseFromMtjhMapping, err error) {
  45. //o := orm.NewOrm()
  46. sql := `SELECT * FROM base_from_mtjh_mapping`
  47. //_, err = o.Raw(sql).QueryRows(&items)
  48. err = global.DEFAULT_DB.Raw(sql).Find(&items).Error
  49. return
  50. }
  51. // 查询指标
  52. func GetBaseFromMtjhIndex() (items []*BaseFromMtjhIndex, err error) {
  53. //o := orm.NewOrm()
  54. sql := `SELECT * FROM base_from_mtjh_index`
  55. //_, err = o.Raw(sql).QueryRows(&items)
  56. err = global.DEFAULT_DB.Raw(sql).Find(&items).Error
  57. return
  58. }
  59. // 添加数据
  60. func AddBaseFromMtjhIndex(item *BaseFromMtjhIndex) (lastId int64, err error) {
  61. //o := orm.NewOrm()
  62. //lastId, err = o.Insert(item)
  63. err = global.DEFAULT_DB.Create(&item).Error
  64. if err != nil {
  65. return
  66. }
  67. lastId = int64(item.BaseFromMtjhIndexId)
  68. return
  69. }
  70. func AddBaseFromMtjhIndexMuti(items []*BaseFromMtjhIndex) (lastId int64, err error) {
  71. o := orm.NewOrm()
  72. lastId, err = o.InsertMulti(500, items)
  73. return
  74. }
  75. // 添加指标
  76. func AddBaseFromMtjhMapping(item *BaseFromMtjhMapping) (lastId int64, err error) {
  77. //o := orm.NewOrm()
  78. //lastId, err = o.Insert(item)
  79. err = global.DEFAULT_DB.Create(&item).Error
  80. if err != nil {
  81. return
  82. }
  83. lastId = int64(item.BaseFromMtjhMappingId)
  84. return
  85. }
  86. func AddBaseFromMtjhMappingMuti(items []*BaseFromMtjhMapping) (lastId int64, err error) {
  87. //o := orm.NewOrm()
  88. //lastId, err = o.InsertMulti(500, items)
  89. err = global.DEFAULT_DB.CreateInBatches(items, utils.MultiAddNum).Error
  90. return
  91. }
  92. func UpdateBaseFromMtjhIndex(item *BaseFromMtjhIndex) (err error) {
  93. //o := orm.NewOrm()
  94. sql := `UPDATE base_from_mtjh_index SET deal_value=? WHERE index_name=? AND data_time = ?`
  95. //_, err = o.Raw(sql, item.DealValue, item.IndexName, item.DataTime).Exec()
  96. err = global.DEFAULT_DB.Exec(sql, item.DealValue, item.IndexName, item.DataTime).Error
  97. return
  98. }
  99. func AddEdbDataFromMtjh(edbCode string) (err error) {
  100. //o := orm.NewOrm()
  101. coalBaseDataAll, err := GetMtjhindexByCode(edbCode)
  102. if err != nil && !utils.IsErrNoRow(err) {
  103. return
  104. }
  105. var isAdd bool
  106. addSql := ` INSERT INTO edb_data_mtjh(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  107. existMap := make(map[string]string)
  108. for _, sv := range coalBaseDataAll {
  109. eDate := sv.DataTime
  110. var timeStr string
  111. var dataTime time.Time
  112. var sDataTime string
  113. var timestamp int64
  114. sDataTime = eDate
  115. dataTime, err = time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  116. if err != nil {
  117. fmt.Println("time.Parse Err:" + eDate)
  118. return err
  119. }
  120. timestamp = dataTime.UnixNano() / 1e6
  121. timeStr = fmt.Sprintf("%d", timestamp)
  122. value := strings.Replace(sv.DealValue, "%", "", -1)
  123. if _, ok := existMap[sDataTime]; !ok {
  124. addSql += GetAddSql("0", edbCode, sDataTime, timeStr, value)
  125. fmt.Println("edbCode:", edbCode)
  126. fmt.Println("sDataTime:", sDataTime)
  127. fmt.Println("timeStr:", timeStr)
  128. fmt.Println("value:", value)
  129. isAdd = true
  130. }
  131. existMap[eDate] = value
  132. }
  133. if isAdd {
  134. addSql = strings.TrimRight(addSql, ",")
  135. utils.FileLog.Info("addSql:" + addSql)
  136. //_, err = o.Raw(addSql).Exec()
  137. err = global.DEFAULT_DB.Exec(addSql).Error
  138. if err != nil {
  139. return err
  140. }
  141. }
  142. return
  143. }
  144. func RefreshEdbDataFromMtjh(edbInfoId int, edbCode, startDate string) (err error) {
  145. source := utils.DATA_SOURCE_MTJH
  146. subSource := utils.DATA_SUB_SOURCE_EDB
  147. //o := orm.NewOrm()
  148. if err != nil {
  149. return
  150. }
  151. edbInfoIdStr := strconv.Itoa(edbInfoId)
  152. //计算数据
  153. var condition string
  154. var pars []interface{}
  155. if edbCode != "" {
  156. condition += " AND index_code=? "
  157. pars = append(pars, edbCode)
  158. }
  159. if startDate != "" {
  160. condition += " AND data_time>=? "
  161. pars = append(pars, startDate)
  162. }
  163. glDataList, err := GetMtjhDataByTradeCode(condition, pars)
  164. if err != nil {
  165. return
  166. }
  167. // 真实数据的最大日期 , 插入规则配置的日期
  168. var realDataMaxDate, edbDataInsertConfigDate time.Time
  169. var edbDataInsertConfig *EdbDataInsertConfig
  170. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  171. {
  172. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  173. if err != nil && !utils.IsErrNoRow(err) {
  174. return
  175. }
  176. if edbDataInsertConfig != nil {
  177. edbDataInsertConfigDate = edbDataInsertConfig.Date
  178. }
  179. }
  180. //获取指标所有数据
  181. var existCondition string
  182. var existPars []interface{}
  183. existCondition += " AND edb_info_id=? "
  184. existPars = append(existPars, edbInfoId)
  185. if startDate != "" {
  186. existCondition += " AND data_time>=? "
  187. existPars = append(existPars, startDate)
  188. }
  189. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  190. if err != nil {
  191. return err
  192. }
  193. existMap := make(map[string]*EdbInfoSearchData)
  194. for _, v := range existList {
  195. existMap[v.DataTime] = v
  196. }
  197. addSql := ` INSERT INTO edb_data_mtjh(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  198. var isAdd bool
  199. for _, v := range glDataList {
  200. var value string
  201. value = strings.Replace(v.DealValue, "%", "", -1)
  202. item := v
  203. itemValue := value
  204. if _, ok := existMap[v.DataTime]; !ok {
  205. eDate := item.DataTime
  206. var timeStr string
  207. var dataTime time.Time
  208. var sDataTime string
  209. var timestamp int64
  210. sDataTime = eDate
  211. dataTime, err = time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  212. if err != nil {
  213. fmt.Println("time.Parse Err:" + eDate)
  214. return err
  215. }
  216. timestamp = dataTime.UnixNano() / 1e6
  217. timeStr = fmt.Sprintf("%d", timestamp)
  218. sValue := itemValue
  219. if sValue != "" {
  220. saveValue := sValue
  221. if findItem, ok := existMap[eDate]; !ok {
  222. addSql += GetAddSql(edbInfoIdStr, edbCode, sDataTime, timeStr, saveValue)
  223. isAdd = true
  224. } else {
  225. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  226. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  227. if err != nil {
  228. return err
  229. }
  230. }
  231. }
  232. }
  233. // 下面代码主要目的是处理掉手动插入的数据判断
  234. {
  235. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  236. realDataMaxDate = dataTime
  237. }
  238. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  239. isFindConfigDateRealData = true
  240. }
  241. }
  242. }
  243. }
  244. // 处理手工数据补充的配置
  245. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  246. if isAdd {
  247. addSql = strings.TrimRight(addSql, ",")
  248. //_, err = o.Raw(addSql).Exec()
  249. err = global.DEFAULT_DB.Exec(addSql).Error
  250. if err != nil {
  251. return err
  252. }
  253. }
  254. return
  255. }
  256. // GetMtjhindexByCode
  257. func GetMtjhindexByCode(indexCode string) (items []*BaseFromMtjhIndex, err error) {
  258. //o := orm.NewOrm()
  259. sql := "SELECT * FROM base_from_mtjh_index WHERE index_code=? "
  260. //_, err = o.Raw(sql, indexCode).QueryRows(&items)
  261. err = global.DEFAULT_DB.Raw(sql, indexCode).Find(&items).Error
  262. return
  263. }
  264. func GetMtjhDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromMtjhIndex, err error) {
  265. sql := ` SELECT * FROM base_from_mtjh_index WHERE 1=1 `
  266. //o := orm.NewOrm()
  267. if condition != "" {
  268. sql += condition
  269. }
  270. sql += ` ORDER BY data_time DESC `
  271. //_, err = o.Raw(sql, pars).QueryRows(&item)
  272. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&item).Error
  273. return
  274. }
  275. func (m *BaseFromMtjhMapping) GetMaxAndMinDateByIndexCode(indexCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  276. var suffix string
  277. if strings.Contains(indexCode, "jsm") {
  278. suffix = "jsm_index"
  279. } else if strings.Contains(indexCode, "company") {
  280. suffix = "company_index"
  281. } else if strings.Contains(indexCode, "firm") {
  282. suffix = "firm_index"
  283. } else if strings.Contains(indexCode, "coastal") {
  284. suffix = "coastal_index"
  285. } else if strings.Contains(indexCode, "inland") {
  286. suffix = "inland_index"
  287. }
  288. tableName := fmt.Sprintf("base_from_coalmine_%s", suffix)
  289. //o := orm.NewOrm()
  290. sql := fmt.Sprintf(` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(deal_value) AS min_value,MAX(deal_value) AS max_value FROM %s WHERE index_code=? `, tableName)
  291. //err = o.Raw(sql, indexCode).QueryRow(&item)
  292. err = global.DEFAULT_DB.Raw(sql, indexCode).First(&item).Error
  293. if err != nil {
  294. return
  295. }
  296. // 获取最新值
  297. var latest_value float64
  298. sql = fmt.Sprintf(` SELECT value AS deal_value FROM %s WHERE index_code=? ORDER BY data_time DESC LIMIT 1 `, tableName)
  299. //err = o.Raw(sql, indexCode).QueryRow(&latest_value)
  300. err = global.DEFAULT_DB.Raw(sql, indexCode).Scan(&latest_value).Error
  301. if err != nil {
  302. return
  303. }
  304. item.LatestValue = latest_value
  305. return
  306. }
  307. func (m *BaseFromMtjhMapping) ModifyIndexMaxAndMinDate(indexCode string, item *EdbInfoMaxAndMinInfo) (err error) {
  308. //o := orm.NewOrm()
  309. sql := ` UPDATE base_from_mtjh_mapping SET start_date=?,end_date=?,latest_value=?,modify_time=NOW() WHERE index_code=? `
  310. //_, err = o.Raw(sql, item.MinDate, item.MaxDate, item.LatestValue, indexCode).Exec()
  311. err = global.DEFAULT_DB.Exec(sql, item.MinDate, item.MaxDate, item.LatestValue, indexCode).Error
  312. return
  313. }