247 lines
6.7 KiB
Go
247 lines
6.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"myschools.me/heritage/heritage-api/model"
|
|
"myschools.me/heritage/heritage-api/service"
|
|
)
|
|
|
|
type TaskCreateRequest struct {
|
|
Name string `json:"name"`
|
|
OrgID string `json:"orgId"`
|
|
ProjectID string `json:"projectId"`
|
|
ObjectIDs string `json:"objectIds"`
|
|
TypeCode string `json:"typeCode"`
|
|
IndicatorIDs string `json:"indicatorIds"`
|
|
PackageIDs string `json:"packageIds"`
|
|
ExecutorPersonID string `json:"executorPersonId"`
|
|
ConfirmerPersonID string `json:"confirmerPersonId"`
|
|
StartAt string `json:"startAt"`
|
|
EndAt string `json:"endAt"`
|
|
ExecutedAt string `json:"executedAt"`
|
|
SinglePoint bool `json:"singlePoint"`
|
|
PeriodicType int `json:"periodicType"`
|
|
PeriodDays int `json:"periodDays"`
|
|
StatusCode string `json:"statusCode"`
|
|
Remark string `json:"remark"`
|
|
}
|
|
|
|
type TaskUpdateRequest struct {
|
|
Name *string `json:"name"`
|
|
OrgID *string `json:"orgId"`
|
|
ProjectID *string `json:"projectId"`
|
|
ObjectIDs *string `json:"objectIds"`
|
|
TypeCode *string `json:"typeCode"`
|
|
IndicatorIDs *string `json:"indicatorIds"`
|
|
PackageIDs *string `json:"packageIds"`
|
|
ExecutorPersonID *string `json:"executorPersonId"`
|
|
ConfirmerPersonID *string `json:"confirmerPersonId"`
|
|
StartAt *string `json:"startAt"`
|
|
EndAt *string `json:"endAt"`
|
|
ExecutedAt *string `json:"executedAt"`
|
|
SinglePoint *bool `json:"singlePoint"`
|
|
PeriodicType *int `json:"periodicType"`
|
|
PeriodDays *int `json:"periodDays"`
|
|
StatusCode *string `json:"statusCode"`
|
|
Remark *string `json:"remark"`
|
|
}
|
|
|
|
func TaskCreate(c *gin.Context) {
|
|
var req TaskCreateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"})
|
|
return
|
|
}
|
|
|
|
startAt, err := parseDateOnly(&req.StartAt)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "开始时间格式错误"})
|
|
return
|
|
}
|
|
endAt, err := parseDateOnly(&req.EndAt)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "结束时间格式错误"})
|
|
return
|
|
}
|
|
executedAt, err := parseDateOnly(&req.ExecutedAt)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "实际实施时间格式错误"})
|
|
return
|
|
}
|
|
|
|
t := &model.Task{
|
|
Name: req.Name,
|
|
OrgID: req.OrgID,
|
|
ProjectID: req.ProjectID,
|
|
ObjectIDs: req.ObjectIDs,
|
|
TypeCode: req.TypeCode,
|
|
IndicatorIDs: req.IndicatorIDs,
|
|
PackageIDs: req.PackageIDs,
|
|
ExecutorPersonID: req.ExecutorPersonID,
|
|
ConfirmerPersonID: req.ConfirmerPersonID,
|
|
SinglePoint: req.SinglePoint,
|
|
PeriodicType: req.PeriodicType,
|
|
PeriodDays: req.PeriodDays,
|
|
StatusCode: req.StatusCode,
|
|
Remark: req.Remark,
|
|
}
|
|
if startAt != nil {
|
|
t.StartAt = *startAt
|
|
}
|
|
if endAt != nil {
|
|
t.EndAt = *endAt
|
|
}
|
|
if executedAt != nil {
|
|
t.ExecutedAt = executedAt
|
|
}
|
|
|
|
out, err := service.TaskCreateTask(t)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"task": out})
|
|
}
|
|
|
|
func TaskUpdate(c *gin.Context) {
|
|
id := c.Param("id")
|
|
var req TaskUpdateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"})
|
|
return
|
|
}
|
|
|
|
patch := &model.Task{}
|
|
if req.Name != nil {
|
|
patch.Name = *req.Name
|
|
}
|
|
if req.OrgID != nil {
|
|
patch.OrgID = *req.OrgID
|
|
}
|
|
if req.ProjectID != nil {
|
|
patch.ProjectID = *req.ProjectID
|
|
}
|
|
if req.ObjectIDs != nil {
|
|
patch.ObjectIDs = *req.ObjectIDs
|
|
}
|
|
if req.TypeCode != nil {
|
|
patch.TypeCode = *req.TypeCode
|
|
}
|
|
if req.IndicatorIDs != nil {
|
|
patch.IndicatorIDs = *req.IndicatorIDs
|
|
}
|
|
if req.PackageIDs != nil {
|
|
patch.PackageIDs = *req.PackageIDs
|
|
}
|
|
if req.ExecutorPersonID != nil {
|
|
patch.ExecutorPersonID = *req.ExecutorPersonID
|
|
}
|
|
if req.ConfirmerPersonID != nil {
|
|
patch.ConfirmerPersonID = *req.ConfirmerPersonID
|
|
}
|
|
if req.StartAt != nil {
|
|
t, err := parseDateOnly(req.StartAt)
|
|
if err != nil || t == nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "开始时间格式错误"})
|
|
return
|
|
}
|
|
patch.StartAt = *t
|
|
}
|
|
if req.EndAt != nil {
|
|
t, err := parseDateOnly(req.EndAt)
|
|
if err != nil || t == nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "结束时间格式错误"})
|
|
return
|
|
}
|
|
patch.EndAt = *t
|
|
}
|
|
if req.ExecutedAt != nil {
|
|
t, err := parseDateOnly(req.ExecutedAt)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "实际实施时间格式错误"})
|
|
return
|
|
}
|
|
patch.ExecutedAt = t
|
|
}
|
|
if req.SinglePoint != nil {
|
|
patch.SinglePoint = *req.SinglePoint
|
|
}
|
|
if req.PeriodicType != nil {
|
|
patch.PeriodicType = *req.PeriodicType
|
|
}
|
|
if req.PeriodDays != nil {
|
|
patch.PeriodDays = *req.PeriodDays
|
|
}
|
|
if req.StatusCode != nil {
|
|
patch.StatusCode = *req.StatusCode
|
|
}
|
|
if req.Remark != nil {
|
|
patch.Remark = *req.Remark
|
|
}
|
|
|
|
out, err := service.TaskUpdateTask(id, patch)
|
|
if err != nil {
|
|
if err == service.ErrNotFound {
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"data": "不存在"})
|
|
return
|
|
}
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"task": out})
|
|
}
|
|
|
|
func TaskDelete(c *gin.Context) {
|
|
id := c.Param("id")
|
|
if err := service.TaskDeleteTask(id); err != nil {
|
|
if err == service.ErrNotFound {
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"data": "不存在"})
|
|
return
|
|
}
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"data": "删除失败"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": "ok"})
|
|
}
|
|
|
|
func TaskGet(c *gin.Context) {
|
|
id := c.Param("id")
|
|
t, err := service.TaskGetTask(id)
|
|
if err != nil {
|
|
if err == service.ErrNotFound {
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"data": "不存在"})
|
|
return
|
|
}
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"data": "查询失败"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"task": t})
|
|
}
|
|
|
|
func TaskList(c *gin.Context) {
|
|
page, size := pageAndSize(c)
|
|
projectID := c.Query("projectId")
|
|
keyword := c.Query("keyword")
|
|
var pid *string
|
|
if projectID != "" {
|
|
pid = &projectID
|
|
}
|
|
var kw *string
|
|
if keyword != "" {
|
|
kw = &keyword
|
|
}
|
|
items, total, err := service.TaskListTasks(pid, kw, page, size)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"data": "查询失败"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"items": items,
|
|
"total": total,
|
|
"page": page,
|
|
"size": size,
|
|
})
|
|
}
|