2022-11-16 10:20:07 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
"xorm.io/builder"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Envs struct {
|
2023-04-22 15:20:10 +00:00
|
|
|
Id int `xorm:"pk autoincr INTEGER" json:"id"`
|
2023-09-04 06:02:25 +00:00
|
|
|
Value string `json:"value"`
|
|
|
|
Timestamp string `json:"timestamp"`
|
2023-04-22 15:20:10 +00:00
|
|
|
Status int `xorm:"TINYINT(1)" json:"status"`
|
|
|
|
Position string `xorm:"TINYINT(1)" json:"position"`
|
2023-09-04 06:02:25 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Remarks string `json:"remarks"`
|
2023-02-18 07:22:05 +00:00
|
|
|
Createdat time.Time `xorm:"not null DATETIME created" json:"createdAt"`
|
|
|
|
Updatedat time.Time `xorm:"not null DATETIME updated" json:"updatedAt"`
|
|
|
|
SerialIndex int64 `xorm:"serial_index INTEGER" json:"serialIndex"`
|
2022-11-16 10:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func QueryEnv(searchValue string) ([]*Envs, error) {
|
|
|
|
envs := make([]*Envs, 0)
|
|
|
|
session := engine.Table(new(Envs)).
|
|
|
|
Where(
|
|
|
|
builder.Like{"name", "%" + searchValue + "%"}.
|
|
|
|
Or(builder.Like{"value", "%" + searchValue + "%"}).
|
2023-02-18 07:22:05 +00:00
|
|
|
Or(builder.Like{"remarks", "%" + searchValue + "%"})).Asc("serial_index")
|
2022-11-16 10:20:07 +00:00
|
|
|
err := session.Find(&envs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return envs, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-02-18 07:22:05 +00:00
|
|
|
func QueryEnvByIndex(from, to int64) ([]*Envs, error) {
|
|
|
|
envs := make([]*Envs, 0)
|
|
|
|
err := engine.Table(new(Envs)).Where(builder.Between{
|
|
|
|
Col: "serial_index",
|
|
|
|
LessVal: from,
|
|
|
|
MoreVal: to,
|
|
|
|
}).Find(&envs)
|
|
|
|
return envs, err
|
|
|
|
}
|
|
|
|
|
2022-11-16 10:20:07 +00:00
|
|
|
func AddEnv(envs *Envs) (int, error) {
|
2023-02-18 07:22:05 +00:00
|
|
|
count, _ := engine.Table(envs).Count()
|
|
|
|
envs.SerialIndex = count
|
2022-11-16 10:20:07 +00:00
|
|
|
_, err := engine.Table(envs).Insert(envs)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
_, _ = engine.Where("name=? and value=?", envs.Name, envs.Value).Get(envs)
|
|
|
|
return envs.Id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetEnv(id int) (*Envs, error) {
|
|
|
|
env := new(Envs)
|
|
|
|
_, err := engine.ID(id).Get(env)
|
|
|
|
return env, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateEnv(env *Envs) error {
|
|
|
|
_, err := engine.Table(env).ID(env.Id).AllCols().Update(env)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteEnv(id int) error {
|
|
|
|
_, err := engine.Table(new(Envs)).Delete(&Envs{Id: id})
|
|
|
|
return err
|
|
|
|
}
|