123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- package dao
- import (
- "encoding/json"
- "fmt"
- "github.com/druidcaesa/gotool"
- "strconv"
- "ulink-admin/frame"
- "ulink-admin/modules/stock/models/model"
- "ulink-admin/modules/stock/models/req"
- "ulink-admin/pkg/base"
- "ulink-admin/pkg/page"
- )
- type StockDao struct {
- base.BaseDao
- }
- // Page 查询大盘走势分页数据
- func (this StockDao) Page(query *req.StockQuery) ([]*model.Stock, int64) {
- stock := make([]*model.Stock, 0)
- session := this.GetSession().Table(model.Stock{}.TableName())
- if query.Id > 0 {
- session.And("id = ?", query.Id)
- }
- if !gotool.StrUtils.HasEmpty(query.CreateDate) {
- session.And("create_date = ?", query.CreateDate)
- }
- if query.Type > 0 {
- session.And("type = ?", query.Type)
- }
- if query.Label > 0 {
- session.And("label = ?", query.Label)
- }
- if !gotool.StrUtils.HasEmpty(query.Val) {
- session.And("val = ?", query.Val)
- }
- if !gotool.StrUtils.HasEmpty(query.CreateBy) {
- session.And("create_by = ?", query.CreateBy)
- }
- if !gotool.StrUtils.HasEmpty(query.UpdateBy) {
- session.And("update_by = ?", query.UpdateBy)
- }
- if !gotool.StrUtils.HasEmpty(query.BeginTime) {
- session.And("date_format(u.create_time,'%y%m%d') >= date_format(?,'%y%m%d')", query.BeginTime)
- }
- if !gotool.StrUtils.HasEmpty(query.EndTime) {
- session.And("date_format(u.create_time,'%y%m%d') <= date_format(?,'%y%m%d')", query.EndTime)
- }
- total, _ := page.GetTotal(session.Clone())
- session.Desc("create_date")
- err := session.Limit(query.PageSize, page.StartSize(query.PageNum, query.PageSize)).Find(&stock)
- if err != nil {
- frame.Throw(frame.SQL_CODE, "数据查询错误"+err.Error())
- }
- return stock, total
- }
- // List 查询大盘走势分页数据
- func (this StockDao) List(query *req.StockQuery, list interface{}) {
- session := this.GetSession().Table(model.Stock{}.TableName()).Alias("o")
- if query.Id > 0 {
- session.And("id = ?", query.Id)
- }
- if !gotool.StrUtils.HasEmpty(query.CreateDate) {
- session.And("create_date = ?", query.CreateDate)
- }
- if query.Type > 0 {
- session.And("type = ?", query.Type)
- }
- if query.Label > 0 {
- session.And("label = ?", query.Label)
- }
- if !gotool.StrUtils.HasEmpty(query.Val) {
- session.And("val = ?", query.Val)
- }
- if !gotool.StrUtils.HasEmpty(query.CreateBy) {
- session.And("create_by = ?", query.CreateBy)
- }
- if !gotool.StrUtils.HasEmpty(query.UpdateBy) {
- session.And("update_by = ?", query.UpdateBy)
- }
- if !gotool.StrUtils.HasEmpty(query.BeginTime) {
- session.And("date_format(u.create_time,'%y%m%d') >= date_format(?,'%y%m%d')", query.BeginTime)
- }
- if !gotool.StrUtils.HasEmpty(query.EndTime) {
- session.And("date_format(u.create_time,'%y%m%d') <= date_format(?,'%y%m%d')", query.EndTime)
- }
- var details []map[string]int
- json.Unmarshal([]byte(query.Site), &details)
- if len(details) > 0 {
- con := "exists ( select count(1) from stock_detail d where d.stock_id=o.id "
- sql := ""
- if len(details) > 0 {
- sql = this.getSql(details[0])
- if len(details) > 1 {
- sql = sql + " or " + this.getSql(details[1])
- }
- }
- if len(sql) > 0 {
- con = fmt.Sprintf("%s and (%s) having count(1)=%d)", con, sql, len(details))
- } else {
- con = con + " )"
- }
- session.And(con)
- }
- err := session.Find(list)
- if err != nil {
- frame.Throw(frame.SQL_CODE, "数据查询错误"+err.Error())
- }
- }
- func (this StockDao) getSql(m map[string]int) string {
- sub := "("
- cat, _ := m["type"]
- sub = sub + "target=" + strconv.Itoa(cat)
- if info, ok := m["site30"]; ok {
- sub = sub + " and site30=" + strconv.Itoa(info)
- }
- if info, ok := m["site60"]; ok {
- sub = sub + " and site60=" + strconv.Itoa(info)
- }
- sub = sub + ")"
- return sub
- }
|