goploy/model/ProjectTaskModel.go

162 lines
3.7 KiB
Go
Raw Normal View History

2022-04-09 21:53:37 +08:00
// Copyright 2022 The Goploy Authors. All rights reserved.
// Use of this source code is governed by a GPLv3-style
// license that can be found in the LICENSE file.
2020-08-04 14:28:25 +08:00
package model
import (
sq "github.com/Masterminds/squirrel"
)
const projectTaskTable = "`project_task`"
2020-08-04 20:00:21 +08:00
2020-08-04 14:28:25 +08:00
// state type
const (
NotRun = iota
Run
)
2020-08-04 20:00:21 +08:00
2020-08-15 13:38:06 +08:00
// ProjectTask -
2020-08-04 14:28:25 +08:00
type ProjectTask struct {
ID int64 `json:"id"`
ProjectID int64 `json:"projectId"`
2020-12-03 17:05:03 +08:00
Branch string `json:"branch"`
2021-05-26 11:09:28 +08:00
CommitID string `json:"commit"`
2020-08-04 14:28:25 +08:00
Date string `json:"date"`
State uint8 `json:"state"`
IsRun uint8 `json:"isRun"`
Creator string `json:"creator"`
CreatorID int64 `json:"creatorId"`
Editor string `json:"editor"`
EditorID int64 `json:"editorId"`
InsertTime string `json:"insertTime"`
UpdateTime string `json:"updateTime"`
}
2020-08-15 13:38:06 +08:00
// ProjectTasks -
2020-08-04 14:28:25 +08:00
type ProjectTasks []ProjectTask
2020-08-15 13:38:06 +08:00
// GetListByProjectID -
2020-08-04 14:28:25 +08:00
func (pt ProjectTask) GetListByProjectID(pagination Pagination) (ProjectTasks, Pagination, error) {
rows, err := sq.
2021-05-26 11:09:28 +08:00
Select("id, project_id, branch, commit, date, is_run, state, creator, creator_id, editor, editor_id, insert_time, update_time").
2020-08-04 14:28:25 +08:00
From(projectTaskTable).
Where(sq.Eq{"project_id": pt.ProjectID}).
Limit(pagination.Rows).
Offset((pagination.Page - 1) * pagination.Rows).
OrderBy("id DESC").
RunWith(DB).
Query()
if err != nil {
return nil, pagination, err
}
projectTasks := ProjectTasks{}
for rows.Next() {
var projectTask ProjectTask
if err := rows.Scan(
&projectTask.ID,
&projectTask.ProjectID,
2020-12-03 17:05:03 +08:00
&projectTask.Branch,
2020-08-04 14:28:25 +08:00
&projectTask.CommitID,
&projectTask.Date,
&projectTask.IsRun,
&projectTask.State,
&projectTask.Creator,
&projectTask.CreatorID,
&projectTask.Editor,
&projectTask.EditorID,
&projectTask.InsertTime,
&projectTask.UpdateTime,
); err != nil {
2020-09-25 20:05:25 +08:00
return projectTasks, pagination, err
2020-08-04 14:28:25 +08:00
}
projectTasks = append(projectTasks, projectTask)
}
err = sq.
Select("COUNT(*) AS count").
From(projectTaskTable).
Where(sq.Eq{"project_id": pt.ProjectID}).
RunWith(DB).
QueryRow().
Scan(&pagination.Total)
if err != nil {
2020-09-25 20:05:25 +08:00
return projectTasks, pagination, err
2020-08-04 14:28:25 +08:00
}
return projectTasks, pagination, nil
}
2020-08-15 13:38:06 +08:00
// GetNotRunListLTDate -
2020-08-04 14:28:25 +08:00
func (pt ProjectTask) GetNotRunListLTDate(date string) (ProjectTasks, error) {
rows, err := sq.
2021-05-26 11:09:28 +08:00
Select("id, project_id, commit, date").
2020-08-04 14:28:25 +08:00
From(projectTaskTable).
Where(sq.LtOrEq{"date": date}).
Where(sq.Eq{"state": Enable, "is_run": NotRun}).
RunWith(DB).
Query()
if err != nil {
return nil, err
}
projectTasks := ProjectTasks{}
for rows.Next() {
var projectTask ProjectTask
if err := rows.Scan(
&projectTask.ID,
&projectTask.ProjectID,
&projectTask.CommitID,
&projectTask.Date,
); err != nil {
return nil, err
}
projectTasks = append(projectTasks, projectTask)
}
return projectTasks, nil
}
2020-08-15 13:38:06 +08:00
// AddRow -
2020-08-04 14:28:25 +08:00
func (pt ProjectTask) AddRow() (int64, error) {
result, err := sq.
Insert(projectTaskTable).
2021-05-26 11:09:28 +08:00
Columns("project_id", "branch", "commit", "date", "creator", "creator_id").
2020-12-03 17:05:03 +08:00
Values(pt.ProjectID, pt.Branch, pt.CommitID, pt.Date, pt.Creator, pt.CreatorID).
2020-08-04 14:28:25 +08:00
RunWith(DB).
Exec()
if err != nil {
return 0, err
}
id, err := result.LastInsertId()
return id, err
}
2020-08-04 20:00:21 +08:00
// SetRun set project task to run
2020-08-04 14:28:25 +08:00
func (pt ProjectTask) SetRun() error {
_, err := sq.
Update(projectTaskTable).
SetMap(sq.Eq{
"is_run": Run,
}).
Where(sq.Eq{"id": pt.ID}).
RunWith(DB).
Exec()
return err
}
2020-08-15 13:38:06 +08:00
// RemoveRow -
2020-08-04 14:28:25 +08:00
func (pt ProjectTask) RemoveRow() error {
_, err := sq.
Update(projectTaskTable).
SetMap(sq.Eq{
"state": Disable,
"editor": pt.Editor,
"editor_id": pt.EditorID,
}).
Where(sq.Eq{"id": pt.ID}).
RunWith(DB).
Exec()
return err
}