base_from_mtjh.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. "time"
  8. )
  9. type BaseFromMtjhMapping struct {
  10. BaseFromMtjhMappingId int `orm:"column(base_from_mtjh_mapping_id);pk"`
  11. IndexName string `description:"持买单量指标名称"`
  12. IndexCode string `description:"持买单量指标编码"`
  13. CreateTime time.Time `description:"时间"`
  14. Area string `description:"区域"`
  15. Port string `description:"港口或码头"`
  16. Variety string `description:"品种"`
  17. Unit string `description:"单位"`
  18. Frequency string `description:"频率"`
  19. }
  20. type BaseFromMtjhIndex struct {
  21. BaseFromMtjhIndexId int `orm:"column(base_from_mtjh_index_id);pk"`
  22. IndexName string `description:"持买单量指标名称"`
  23. IndexCode string `description:"持买单量指标编码"`
  24. DealValue string `description:"成交量"`
  25. DataTime string `description:"数据日期"`
  26. Area string `description:"区域"`
  27. Port string `description:"港口或码头"`
  28. Unit string `description:"单位"`
  29. Frequency string `description:"频率"`
  30. Variety string `description:"品种"`
  31. CreateTime time.Time `description:"插入时间"`
  32. ModifyTime time.Time `description:"修改时间"`
  33. }
  34. type BaseFromMtjhIndexItem struct {
  35. BaseFromMtjhIndexId int `orm:"column(base_from_mtjh_index_id);pk"`
  36. IndexName string `description:"持买单量指标名称"`
  37. IndexCode string `description:"持买单量指标编码"`
  38. DealValue string `description:"成交量"`
  39. DataTime string `description:"数据日期"`
  40. Area string `description:"区域"`
  41. Port string `description:"港口或码头"`
  42. Unit string `description:"单位"`
  43. Frequency string `description:"频率"`
  44. Variety string `description:"品种"`
  45. CreateTime string `description:"插入时间"`
  46. ModifyTime string `description:"修改时间"`
  47. }
  48. // 查询指标
  49. func GetBaseFromMtjhMapping() (items []*BaseFromMtjhMapping, err error) {
  50. o := orm.NewOrm()
  51. sql := `SELECT * FROM base_from_mtjh_mapping`
  52. _, err = o.Raw(sql).QueryRows(&items)
  53. return
  54. }
  55. // 查询指标
  56. func GetBaseFromMtjhIndex() (items []*BaseFromMtjhIndex, err error) {
  57. o := orm.NewOrm()
  58. sql := `SELECT * FROM base_from_mtjh_index`
  59. _, err = o.Raw(sql).QueryRows(&items)
  60. return
  61. }
  62. // 添加数据
  63. func AddBaseFromMtjhIndex(item *BaseFromMtjhIndex) (lastId int64, err error) {
  64. o := orm.NewOrm()
  65. lastId, err = o.Insert(item)
  66. return
  67. }
  68. func AddBaseFromMtjhIndexMuti(items []*BaseFromMtjhIndex) (lastId int64, err error) {
  69. o := orm.NewOrm()
  70. lastId, err = o.InsertMulti(500, items)
  71. return
  72. }
  73. // 添加指标
  74. func AddBaseFromMtjhMapping(item *BaseFromMtjhMapping) (lastId int64, err error) {
  75. o := orm.NewOrm()
  76. lastId, err = o.Insert(item)
  77. return
  78. }
  79. func AddBaseFromMtjhMappingMuti(items []*BaseFromMtjhMapping) (lastId int64, err error) {
  80. o := orm.NewOrm()
  81. lastId, err = o.InsertMulti(500, items)
  82. return
  83. }
  84. func UpdateBaseFromMtjhIndex(item *BaseFromMtjhIndex) (err error) {
  85. o := orm.NewOrm()
  86. sql := `UPDATE base_from_mtjh_index SET deal_value=? WHERE index_name=? AND data_time = ?`
  87. _, err = o.Raw(sql, item.DealValue, item.IndexName, item.DataTime).Exec()
  88. return
  89. }
  90. func GetMtjhClassifyList() (list []*BaseFromMtjhMapping, err error) {
  91. o := orm.NewOrmUsingDB("data")
  92. sql := "SELECT * FROM base_from_mtjh_mapping group by area"
  93. _, err = o.Raw(sql).QueryRows(&list)
  94. return
  95. }
  96. type BaseFromMtjhMappingItem struct {
  97. BaseFromMtjhMappingId int `orm:"column(base_from_mtjh_mapping_id);pk"`
  98. IndexName string `description:"持买单量指标名称"`
  99. IndexCode string `description:"持买单量指标编码"`
  100. CreateTime string `description:"时间"`
  101. Area string `description:"区域"`
  102. Port string `description:"港口或码头"`
  103. Variety string `description:"品种"`
  104. Unit string `description:"单位"`
  105. Frequency string `description:"频率"`
  106. }
  107. func GetMtjhMapping(condition string, pars interface{}) (items []*BaseFromMtjhMappingItem, err error) {
  108. o := orm.NewOrmUsingDB("data")
  109. sql := ` SELECT * FROM base_from_mtjh_mapping WHERE 1=1 `
  110. if condition != "" {
  111. sql += condition
  112. }
  113. sql += ` ORDER BY base_from_mtjh_mapping_id asc`
  114. _, err = o.Raw(sql, pars).QueryRows(&items)
  115. return
  116. }
  117. type BaseFromMtjhIndexList struct {
  118. BaseFromMtjhMappingId int `orm:"column(base_from_mtjh_mapping_id);pk"`
  119. IndexName string `description:"持买单量指标名称"`
  120. IndexCode string `description:"持买单量指标编码"`
  121. Area string `description:"区域"`
  122. Port string `description:"港口或码头"`
  123. Unit string `description:"单位"`
  124. Frequency string `description:"频率"`
  125. CreateTime string `description:"插入时间"`
  126. ModifyTime string `description:"修改时间"`
  127. Variety string `description:"品种"`
  128. DataList []*BaseFromMtjhIndexItem
  129. Paging *paging.PagingItem `description:"分页数据"`
  130. }
  131. func GetMtjhIndexDataCount(indexCode string) (count int, err error) {
  132. o := orm.NewOrmUsingDB("data")
  133. sql := ` SELECT COUNT(1) AS count FROM base_from_mtjh_index WHERE index_code=? `
  134. err = o.Raw(sql, indexCode).QueryRow(&count)
  135. return
  136. }
  137. func GetMtjhIndexData(indexCode string, startSize, pageSize int) (items []*BaseFromMtjhIndexItem, err error) {
  138. o := orm.NewOrmUsingDB("data")
  139. sql := ` SELECT * FROM base_from_mtjh_index WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  140. _, err = o.Raw(sql, indexCode, startSize, pageSize).QueryRows(&items)
  141. return
  142. }
  143. // GetMtjhItemList 模糊查询煤炭江湖数据库指标列表
  144. func GetMtjhItemList(keyword string) (items []*BaseFromMtjhMappingItem, err error) {
  145. o := orm.NewOrmUsingDB("data")
  146. sql := "SELECT * FROM base_from_mtjh_mapping WHERE CONCAT(index_name,index_code) LIKE ? "
  147. _, err = o.Raw(sql, utils.GetLikeKeyword(keyword)).QueryRows(&items)
  148. return
  149. }
  150. // 查询数据
  151. func GetBaseFromMtjhIndexByCode(indexCode string) (items []*BaseFromMtjhIndexItem, err error) {
  152. o := orm.NewOrmUsingDB("data")
  153. sql := `SELECT * FROM base_from_mtjh_index WHERE index_code=? ORDER BY data_time DESC `
  154. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  155. return
  156. }
  157. // GetMtjhMappingItemByCode
  158. func GetMtjhMappingItemByCode(indexCode string) (item *BaseFromMtjhMappingItem, err error) {
  159. o := orm.NewOrmUsingDB("data")
  160. sql := "SELECT * FROM base_from_mtjh_mapping WHERE index_code=? "
  161. err = o.Raw(sql, indexCode).QueryRow(&item)
  162. return
  163. }
  164. // GetMtjhFrequencyByArea
  165. func GetMtjhFrequencyByArea(area string) (items []*string, err error) {
  166. o := orm.NewOrmUsingDB("data")
  167. sql := "SELECT frequency FROM base_from_mtjh_index WHERE area=? group by area "
  168. _, err = o.Raw(sql, area).QueryRows(&items)
  169. return
  170. }
  171. func GetClassifyMtjhByArea(area string) (items []*string, err error) {
  172. o := orm.NewOrmUsingDB("data")
  173. sql := `SELECT DISTINCT index_code FROM base_from_mtjh_index WHERE area=? `
  174. _, err = o.Raw(sql, area).QueryRows(&items)
  175. return
  176. }
  177. func GetCoalMtjhMaxCount(area string) (count int, err error) {
  178. o := orm.NewOrmUsingDB("data")
  179. sql := `SELECT
  180. MAX( t.num ) AS count
  181. FROM
  182. (
  183. SELECT
  184. COUNT( 1 ) AS num
  185. FROM
  186. base_from_mtjh_index
  187. WHERE
  188. area =?
  189. GROUP BY
  190. index_name ) AS t `
  191. err = o.Raw(sql, area).QueryRow(&count)
  192. return
  193. }
  194. func GetMtjhCount(indexCode string) (count int, err error) {
  195. o := orm.NewOrmUsingDB("data")
  196. sql := `SELECT
  197. COUNT( 1 ) AS num
  198. FROM
  199. base_from_mtjh_index
  200. WHERE
  201. index_code =? `
  202. err = o.Raw(sql, indexCode).QueryRow(&count)
  203. return
  204. }
  205. func GetMtjhIndexLatestDate(indexCode string) (ModifyTime string, err error) {
  206. o := orm.NewOrmUsingDB("data")
  207. sql := ` SELECT modify_time FROM base_from_mtjh_index WHERE index_code=? ORDER BY modify_time DESC limit 1 `
  208. err = o.Raw(sql, indexCode).QueryRow(&ModifyTime)
  209. return
  210. }
  211. // GetMtjhItemsByCondition 获取煤炭江湖指标
  212. func GetMtjhItemsByCondition(cond string, pars []interface{}) (items []*BaseFromMtjhMappingItem, err error) {
  213. o := orm.NewOrmUsingDB("data")
  214. sql := fmt.Sprintf(`SELECT * FROM base_from_mtjh_mapping WHERE 1=1 %s`, cond)
  215. _, err = o.Raw(sql, pars).QueryRows(&items)
  216. return
  217. }