package models

import (
	"github.com/jinzhu/gorm"
	"fmt"
)


type RaspFeedbackList struct {
	Id 			string 	`json:"id" gorm:"PRIMARY_KEY"`
	Addr        string  `json:"addr"`
	Description string	`json:"description"`
	Addtime 	int64	`json:"addtime"`
}

type ReqRaspFeedbackList struct {
	Addr string `json:"addr" binding:"required"`
	Page int32 `json:"page"`
	Pagesize int32 `json:"pagesize"`
}


func AddFeedBackList(fd RaspFeedbackList)bool{
	if err := db.Create(&fd).Error;err != nil {
		fmt.Println(err)
		return false
	}
	return true
}

func GetAddrFeedBackList(req *ReqRaspFeedbackList) ([]*RaspFeedbackList, error) {
	var txs []*RaspFeedbackList
	maps := map[string]interface{}{"addr":req.Addr}
	err := db.Model(&RaspFeedbackList{}).Where(maps).Order("id "+"desc").Offset((req.Page-1)*req.Pagesize).Limit(req.Pagesize).Find(&txs).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}

	return txs, nil
}

func GetAddrFeedBackCount(addr string) int64 {
	maps := map[string]interface{}{"addr":addr}
	var count int64
	db.Model(&RaspFeedbackList{}).Where(maps).Count(&count)
	return count
}