stock_dao.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package dao
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/druidcaesa/gotool"
  6. "strconv"
  7. "ulink-admin/frame"
  8. "ulink-admin/modules/stock/models/model"
  9. "ulink-admin/modules/stock/models/req"
  10. "ulink-admin/pkg/base"
  11. "ulink-admin/pkg/page"
  12. )
  13. type StockDao struct {
  14. base.BaseDao
  15. }
  16. // Page 查询大盘走势分页数据
  17. func (this StockDao) Page(query *req.StockQuery) ([]*model.Stock, int64) {
  18. stock := make([]*model.Stock, 0)
  19. session := this.GetSession().Table(model.Stock{}.TableName())
  20. if query.Id > 0 {
  21. session.And("id = ?", query.Id)
  22. }
  23. if !gotool.StrUtils.HasEmpty(query.CreateDate) {
  24. session.And("create_date = ?", query.CreateDate)
  25. }
  26. if query.Type > 0 {
  27. session.And("type = ?", query.Type)
  28. }
  29. if query.Label > 0 {
  30. session.And("label = ?", query.Label)
  31. }
  32. if !gotool.StrUtils.HasEmpty(query.Val) {
  33. session.And("val = ?", query.Val)
  34. }
  35. if !gotool.StrUtils.HasEmpty(query.CreateBy) {
  36. session.And("create_by = ?", query.CreateBy)
  37. }
  38. if !gotool.StrUtils.HasEmpty(query.UpdateBy) {
  39. session.And("update_by = ?", query.UpdateBy)
  40. }
  41. if !gotool.StrUtils.HasEmpty(query.BeginTime) {
  42. session.And("date_format(u.create_time,'%y%m%d') >= date_format(?,'%y%m%d')", query.BeginTime)
  43. }
  44. if !gotool.StrUtils.HasEmpty(query.EndTime) {
  45. session.And("date_format(u.create_time,'%y%m%d') <= date_format(?,'%y%m%d')", query.EndTime)
  46. }
  47. total, _ := page.GetTotal(session.Clone())
  48. session.Desc("create_date")
  49. err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&stock)
  50. if err != nil {
  51. frame.Throw(frame.SQL_CODE, "数据查询错误"+err.Error())
  52. }
  53. return stock, total
  54. }
  55. // List 查询大盘走势分页数据
  56. func (this StockDao) List(query *req.StockQuery, list interface{}) {
  57. session := this.GetSession().Table(model.Stock{}.TableName()).Alias("o")
  58. if query.Id > 0 {
  59. session.And("id = ?", query.Id)
  60. }
  61. if !gotool.StrUtils.HasEmpty(query.CreateDate) {
  62. session.And("create_date = ?", query.CreateDate)
  63. }
  64. if query.Type > 0 {
  65. session.And("type = ?", query.Type)
  66. }
  67. if query.Label > 0 {
  68. session.And("label = ?", query.Label)
  69. }
  70. if !gotool.StrUtils.HasEmpty(query.Val) {
  71. session.And("val = ?", query.Val)
  72. }
  73. if !gotool.StrUtils.HasEmpty(query.CreateBy) {
  74. session.And("create_by = ?", query.CreateBy)
  75. }
  76. if !gotool.StrUtils.HasEmpty(query.UpdateBy) {
  77. session.And("update_by = ?", query.UpdateBy)
  78. }
  79. if !gotool.StrUtils.HasEmpty(query.BeginTime) {
  80. session.And("date_format(u.create_time,'%y%m%d') >= date_format(?,'%y%m%d')", query.BeginTime)
  81. }
  82. if !gotool.StrUtils.HasEmpty(query.EndTime) {
  83. session.And("date_format(u.create_time,'%y%m%d') <= date_format(?,'%y%m%d')", query.EndTime)
  84. }
  85. var details []map[string]int
  86. json.Unmarshal([]byte(query.Site), &details)
  87. if len(details) > 0 {
  88. con := "exists ( select count(1) from stock_detail d where d.stock_id=o.id "
  89. sql := ""
  90. if len(details) > 0 {
  91. sql = this.getSql(details[0])
  92. if len(details) > 1 {
  93. sql = sql + " or " + this.getSql(details[1])
  94. }
  95. }
  96. if len(sql) > 0 {
  97. con = fmt.Sprintf("%s and (%s) having count(1)=%d)", con, sql, len(details))
  98. } else {
  99. con = con + " )"
  100. }
  101. session.And(con)
  102. }
  103. err := session.Find(list)
  104. if err != nil {
  105. frame.Throw(frame.SQL_CODE, "数据查询错误"+err.Error())
  106. }
  107. }
  108. func (this StockDao) getSql(m map[string]int) string {
  109. sub := "("
  110. cat, _ := m["type"]
  111. sub = sub + "target=" + strconv.Itoa(cat)
  112. if info, ok := m["site30"]; ok {
  113. sub = sub + " and site30=" + strconv.Itoa(info)
  114. }
  115. if info, ok := m["site60"]; ok {
  116. sub = sub + " and site60=" + strconv.Itoa(info)
  117. }
  118. sub = sub + ")"
  119. return sub
  120. }