blog_backend_api/model/model.go

107 lines
3.1 KiB
Go
Raw Normal View History

2019-01-25 09:11:15 +00:00
package model
2019-02-23 06:19:29 +00:00
import (
2021-02-05 17:46:59 +00:00
"background/db"
"background/logs"
2019-04-07 04:25:07 +00:00
"background/utils"
"crypto/md5"
2019-02-23 06:19:29 +00:00
"fmt"
2019-03-07 02:36:09 +00:00
"log"
2021-03-22 04:10:37 +00:00
"time"
"github.com/jinzhu/gorm"
2019-02-23 06:19:29 +00:00
)
2021-02-16 02:52:29 +00:00
type Plan struct {
2021-02-16 06:05:21 +00:00
ID uint32 `gorm:"column:id;primary_key" json:"id"`
2021-02-16 02:52:29 +00:00
StartTime utils.OrmTime `gorm:"column:start_time" json:"start_time"`
EndTime utils.OrmTime `gorm:"column:end_time" json:"end_time"`
2021-02-16 06:05:21 +00:00
Date string `gorm:"column:date" json:"date"`
Content string `gorm:"column:content" json:"content"`
2021-02-21 13:39:37 +00:00
Type uint32 `gorm:"column:type" json:"type"`
2021-02-16 02:52:29 +00:00
}
2021-03-05 02:18:59 +00:00
type Undo struct {
2021-03-22 04:10:37 +00:00
model gorm.Model
ID int32 `json:"id" gorm:"column:id" sql:"id"`
Content string `json:"content" gorm:"column:content" sql:"content"`
SpendTime int32 `json:"spend_time" gorm:"column:spend_time" sql:"spend_time"`
Parent int32 `json:"parent" gorm:"column:parent" sql:"parent"`
Level int32 `json:"level" gorm:"column:level" sql:"level"`
Type int32 `json:"type" gorm:"column:type" sql:"type"`
Done int `json:"done" gorm:"done" sql:"done"`
DeletedAt *time.Time `json:"deleted_at"`
CreateTime string `json:"create_time"`
FinishTime string `json:"finish_time"`
2021-03-05 02:18:59 +00:00
}
2021-02-21 13:39:37 +00:00
type PlanType struct {
2021-03-05 02:18:59 +00:00
ID uint32 `gorm:"column:id;primary_key" json:"id"`
TypeName string `gorm:"column:type_name;" json:"type_name"`
2021-02-21 13:39:37 +00:00
}
2021-03-08 15:59:50 +00:00
func (m *Undo) TableName() string {
return "undo"
}
2021-02-21 13:39:37 +00:00
func (m *PlanType) TableName() string {
return "plan_type"
}
2021-02-16 02:52:29 +00:00
func (m *Plan) TableName() string {
return "plan"
}
2019-01-25 09:11:15 +00:00
type Users struct {
2019-03-07 02:36:09 +00:00
ID int64 `sql:"id" json:"id"`
UserName string `sql:"user_name" json:"user_name"`
2019-04-07 04:25:07 +00:00
UserPwd string `sql:"user_pwd" json:"user_pwd"`
2019-03-07 02:36:09 +00:00
CreatedDate string `sql:"created_date" json:"created_date"`
UpdatedDate string `sql:"updated_date" json:"updated_date"`
DisplayName string `sql:"display_name" json:"display_name"`
2019-02-23 06:19:29 +00:00
EmailAddress string `sql:"email_address" json:"email_address"`
2019-03-07 02:36:09 +00:00
Tel string `sql:"tel" json:"tel"`
2019-06-23 11:33:45 +00:00
Avatar string `sql:"avatar" json:"avatar"`
2019-02-23 06:19:29 +00:00
}
2019-03-07 02:36:09 +00:00
func GetUsers(limit int32, offsetPage int32, name string) ([]Users, int32) {
2019-02-23 06:19:29 +00:00
users := []Users{}
var query string
2019-03-07 02:36:09 +00:00
if name != "" {
2019-02-23 06:19:29 +00:00
log.Println(name)
2019-04-07 04:25:07 +00:00
query = fmt.Sprintf("select * from users where user_name like '%s' limit %d offset %d",
"%%"+name+"%%", limit, offsetPage*limit)
2019-02-23 06:19:29 +00:00
log.Printf(query)
2019-03-07 02:36:09 +00:00
} else {
query = fmt.Sprintf("select * from users limit %d offset %d", limit, offsetPage*limit)
2019-02-23 06:19:29 +00:00
}
2019-03-07 02:36:09 +00:00
err := db.GetMysqlClient().Query2(query, &users)
if nil != err {
2019-02-23 06:19:29 +00:00
logs.Error(err.Error())
}
type Count struct {
Count int32 `sql:"count"`
}
cnts := []Count{}
query = fmt.Sprintf("select count(*) as count from users")
2019-03-07 02:36:09 +00:00
err = db.GetMysqlClient().Query2(query, &cnts)
if nil != err {
2019-02-23 06:19:29 +00:00
logs.Error(err.Error())
}
2019-03-07 02:36:09 +00:00
return users, cnts[0].Count
2019-01-25 09:11:15 +00:00
}
2019-04-07 04:25:07 +00:00
2021-02-16 06:05:21 +00:00
func ModyfyPassword(UserName string, Password string) error {
2019-04-07 04:25:07 +00:00
h := md5.New()
h.Write([]byte(Password))
query := fmt.Sprintf("update users set user_pwd = '%s' where user_name = '%s' ",
2021-02-16 06:05:21 +00:00
utils.ByteSliceToString(h.Sum(nil)), UserName)
n, err := db.GetMysqlClient().Update(query)
2019-04-07 04:25:07 +00:00
if nil != err {
logs.Error(err.Error())
return err
}
2021-02-16 06:05:21 +00:00
if n == 0 {
2019-04-07 04:25:07 +00:00
return nil
}
return nil
2021-02-16 06:05:21 +00:00
}