• 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 ] 
    
  • 相关阅读:
    pagination分页插件使用
    ajax实现图片文件上传和图片预览
    屏幕护眼色调调节
    css画三角形以及实现带三角的阴影效果
    软件测试homework3
    使用Junit包进行单元测试并导入eclemma包。
    软件测试homework2
    软件测试homework1 编程过程的一个小错误
    ReentrantLock
    Elasticsearch-基本概念
  • 原文地址:https://www.cnblogs.com/haima/p/13523841.html
Copyright © 2020-2023  润新知