base_from_mtjh.go 11 KB

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