105 lines
3.5 KiB
Go
105 lines
3.5 KiB
Go
package model
|
||
|
||
import (
|
||
"cc/db"
|
||
"errors"
|
||
"math/rand"
|
||
)
|
||
|
||
// Project 项目
|
||
type Project struct {
|
||
ID int `db:"id" json:"id"` // 项目ID
|
||
Name string `db:"name" json:"name"` // 项目名称
|
||
Code int `db:"code" json:"code"` // 项目编码
|
||
User string `db:"user" json:"user"` // 项目用户
|
||
Description string `db:"description" json:"description"` // 项目描述
|
||
UpdateFrequency int `db:"update_frequency" json:"update_frequency"` // 更新频率
|
||
LocaltionID int `db:"localtion_id" json:"localtion_id"` // 项目位置ID
|
||
WeatherTypeID int `db:"weather_type_id" json:"weather_type_id"` // 天气类型ID
|
||
Dev bool `db:"dev" json:"dev"` // 是否为开发模式
|
||
}
|
||
|
||
// GetAll 获取所有项目
|
||
func (pro *Project) GetAll() (rv []Project, rer error) {
|
||
sql := "SELECT * FROM projects"
|
||
rer = db.DB.Select(&rv, sql)
|
||
return
|
||
}
|
||
|
||
// GetProjectByID 通过项目ID获取项目
|
||
func (pro *Project) GetProjectByID() (rer error) {
|
||
sql := "SELECT * FROM projects WHERE id =?"
|
||
return db.DB.Get(pro, sql, pro.ID)
|
||
}
|
||
|
||
// GetProjectByCode 通过项目代码获取项目
|
||
func (pro *Project) GetProjectByCode() (rer error) {
|
||
sql := "SELECT * FROM projects WHERE code =?"
|
||
return db.DB.Get(pro, sql, pro.Code)
|
||
}
|
||
|
||
// CreateProject 创建项目
|
||
func (pro *Project) CreateProject() (rer error) {
|
||
// 判断是否存在相同的项目名称和天气类型
|
||
var count int
|
||
err := db.DB.Get(&count, "SELECT COUNT(*) FROM projects WHERE name =? AND weather_type_id =?", pro.Name, pro.WeatherTypeID)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if count > 0 {
|
||
return errors.New("已存在相同配置")
|
||
}
|
||
// 判断项目代码,当前项目代码不为0时,必须是4位数字
|
||
if pro.Code != 0 {
|
||
if pro.Code < 1000 || pro.Code > 9999 {
|
||
return errors.New("项目代码必须是4位数字")
|
||
}
|
||
err := db.DB.Get(&count, "SELECT COUNT(*) FROM projects WHERE code =?", pro.Code)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if count > 0 {
|
||
return errors.New("项目代码已存在,请重新输入,或者将项目代码设置为0,系统将随机生成一个4位数字的项目代码")
|
||
}
|
||
} else {
|
||
// 随机生成一个4位数字的项目代码
|
||
for {
|
||
pro.Code = rand.Intn(9000) + 1000
|
||
err := db.DB.Get(&count, "SELECT COUNT(*) FROM projects WHERE code =?", pro.Code)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if count == 0 {
|
||
break
|
||
}
|
||
}
|
||
}
|
||
// 更新频率为0时,设置为60分钟
|
||
if pro.UpdateFrequency == 0 {
|
||
pro.UpdateFrequency = 60
|
||
}
|
||
// 插入数据
|
||
sql := "INSERT INTO projects (name, code, user, description, update_frequency, localtion_id, weather_type_id, dev) VALUES (?,?,?,?,?,?,?,?)"
|
||
_, rer = db.DB.Exec(sql, pro.Name, pro.Code, pro.User, pro.Description, pro.UpdateFrequency, pro.LocaltionID, pro.WeatherTypeID, pro.Dev)
|
||
return
|
||
}
|
||
|
||
// UpdateProject 更新项目
|
||
func (pro *Project) UpdateProject() (rer error) {
|
||
// 更新频率为0时,设置为60分钟
|
||
if pro.UpdateFrequency == 0 {
|
||
pro.UpdateFrequency = 60
|
||
}
|
||
// 更新数据
|
||
sql := "UPDATE projects SET name=?, user=?, description=?, update_frequency=?, localtion_id=?, weather_type_id=?, dev=? WHERE id=?"
|
||
_, rer = db.DB.Exec(sql, pro.Name, pro.User, pro.Description, pro.UpdateFrequency, pro.LocaltionID, pro.WeatherTypeID, pro.Dev, pro.ID)
|
||
return
|
||
}
|
||
|
||
// DeleteProject 删除项目
|
||
func (pro *Project) DeleteProject() (rer error) {
|
||
sql := "DELETE FROM projects WHERE id=?"
|
||
_, rer = db.DB.Exec(sql, pro.ID)
|
||
return
|
||
}
|