gin+grom 求当天的数据列表 0点到24点

框架go-admin

gin+gorm

mysql表


CREATE TABLE `sq_user_ticket` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '用户id',
  `to_uid` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '被投票用户的id',
  `from_uid` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '投票用户的id',
  `from_user_name` char(100) NOT NULL DEFAULT '' COMMENT '用户名',
  `from_nick_name` char(100) NOT NULL DEFAULT '' COMMENT '昵称',
  `number` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '得票数量',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='单身用户主表';

model里


type SqUserTicket struct {
	Id           int    `json:"Id" gorm:"type:int(11);primary_key"`     //
	ToUid        int    `json:"toUid" gorm:"type:int(11)"`              //
	FromUid      int `json:"fromUid" gorm:"type:varchar(128);"`      //
	FromUserName string `json:"fromUserName" gorm:"type:varchar(255);"` //
	FromNickName string `json:"fromNickName" gorm:"type:varchar(255);"` //
	Number       int64  `json:"number" gorm:"type:int(20)"`             //
	DataScope    string `json:"dataScope" gorm:"-"`
	Params       string `json:"params"  gorm:"-"`
	BaseModel
}

func (SqUserTicket) TableName() string {
	return "sq_user_ticket"
}

// 获取当天的数据 0点到24点
func (e *SqUserTicket) GetCount() ([]SqUserTicket, int, error) {
	var doc []SqUserTicket

	table := orm.Eloquent.Select("*").Table(e.TableName())

	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}

	if e.ToUid != 0 {
		table = table.Where("to_uid = ?", e.ToUid)
	}
	if e.FromUid != 0 {
		table = table.Where("from_uid = ?", e.FromUid)
	}

	startTime, endTime := tools.GetCurrentTimestamp()
	table = table.Where("created_at between ? and ?",startTime,endTime)

	var count int
	if err := table.Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}


//获取当天的时间范伟 Time类型 2020-08-18 00:00:00 +0800 CST 2020-08-18 23:59:59 +0800 CST
func GetCurrentTimestamp() (beginTime, endTime time.Time) {
	t := time.Now()
	timeStr := t.Format("2006-01-02")
	beginTime, _ = time.ParseInLocation("2006-01-02", timeStr, time.Local)
	endTimeTmp := beginTime.Unix() + 86399
	endTime = time.Unix(endTimeTmp, 0)
	return beginTime, endTime
}

//获取当天的时间范伟 int64类型 //1597680000 1597766399
func GetCurrentTimeInt64() (beginTime, endTime int64) {
	t := time.Now()
	timeStr := t.Format("2006-01-02")
	beginTimeTmp, _ := time.ParseInLocation("2006-01-02", timeStr, time.Local)
	beginTime = beginTimeTmp.Unix()
	endTime = beginTime + 86399
	return beginTime, endTime
}

控制器

func InsertSqUserTicket(c *gin.Context) {

	var data models.SqUserTicket
	err := c.ShouldBindJSON(&data)
	if err != nil {
		app.Error(c, 400, err, err.Error())
		return
	}
	//data.FromUid = tools.GetUserIdStr(c)
	result, count, err := data.GetCount()

	if err != nil {
		app.Error(c, 400, err, err.Error())
		return
	}
	app.OK(c, result, fmt.Sprintf("%d", count))
}

执行的sql效果


(/media/haima/34E401CC64DD0E28/site/go/src/linkbook/xiangqin/go-admin/models/sqUserTicket.go:92) 
[2020-08-18 15:28:59]  [228.05ms]  SELECT * FROM `sq_user_ticket`  WHERE `sq_user_ticket`.`deleted_at` IS NULL AND ((to_uid = 1111) AND (from_uid = 222) AND (created_at between '2020-08-18 00:00:00' and '2020-08-18 23:59:59'))  
[0 rows affected or returned ] 

(/media/haima/34E401CC64DD0E28/site/go/src/linkbook/xiangqin/go-admin/models/sqUserTicket.go:95) 
[2020-08-18 15:29:32]  [20.31ms]  SELECT count(*) FROM `sq_user_ticket`  WHERE (to_uid = 1111) AND (from_uid = 222) AND (created_at between '2020-08-18 00:00:00' and '2020-08-18 23:59:59') AND (`deleted_at` IS NULL)  
[0 rows affected or returned ] 
posted @ 2020-08-18 15:35  HaimaBlog  阅读(671)  评论(0编辑  收藏  举报