base_from_coal.go 11 KB

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