258 lines
6.9 KiB
Go
258 lines
6.9 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 ProjectCreateRequest struct {
|
|
ProjectNo string `json:"projectNo"`
|
|
Name string `json:"name"`
|
|
TypeCode string `json:"typeCode"`
|
|
BuildingIDs string `json:"buildingIds"`
|
|
StatusCode string `json:"statusCode"`
|
|
|
|
ResponsibleOrgID string `json:"responsibleOrgId"`
|
|
ImplementOrgIDs string `json:"implementOrgIds"`
|
|
LeaderPersonID string `json:"leaderPersonId"`
|
|
ParticipantPersonIDs string `json:"participantPersonIds"`
|
|
|
|
StartAt string `json:"startAt"`
|
|
EndAt string `json:"endAt"`
|
|
CompletedAt string `json:"completedAt"`
|
|
|
|
Description string `json:"description"`
|
|
Attachments string `json:"attachments"`
|
|
OrgID string `json:"orgId"`
|
|
CreatorUserID string `json:"creatorUserId"`
|
|
Remark string `json:"remark"`
|
|
}
|
|
|
|
type ProjectUpdateRequest struct {
|
|
ProjectNo *string `json:"projectNo"`
|
|
Name *string `json:"name"`
|
|
TypeCode *string `json:"typeCode"`
|
|
BuildingIDs *string `json:"buildingIds"`
|
|
StatusCode *string `json:"statusCode"`
|
|
|
|
ResponsibleOrgID *string `json:"responsibleOrgId"`
|
|
ImplementOrgIDs *string `json:"implementOrgIds"`
|
|
LeaderPersonID *string `json:"leaderPersonId"`
|
|
ParticipantPersonIDs *string `json:"participantPersonIds"`
|
|
|
|
StartAt *string `json:"startAt"`
|
|
EndAt *string `json:"endAt"`
|
|
CompletedAt *string `json:"completedAt"`
|
|
|
|
Description *string `json:"description"`
|
|
Attachments *string `json:"attachments"`
|
|
OrgID *string `json:"orgId"`
|
|
CreatorUserID *string `json:"creatorUserId"`
|
|
Remark *string `json:"remark"`
|
|
}
|
|
|
|
func ProjectCreate(c *gin.Context) {
|
|
var req ProjectCreateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"})
|
|
return
|
|
}
|
|
|
|
startAt, err := parseDateOnly(&req.StartAt)
|
|
if err != nil || startAt == nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "开始时间格式错误"})
|
|
return
|
|
}
|
|
endAt, err := parseDateOnly(&req.EndAt)
|
|
if err != nil || endAt == nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "结束时间格式错误"})
|
|
return
|
|
}
|
|
completedAt, err := parseDateOnly(&req.CompletedAt)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "实际完成时间格式错误"})
|
|
return
|
|
}
|
|
|
|
usr := currentUser(c)
|
|
creatorID := req.CreatorUserID
|
|
if creatorID == "" && usr != nil {
|
|
creatorID = usr.ID
|
|
}
|
|
|
|
p := &model.Project{
|
|
ProjectNo: req.ProjectNo,
|
|
Name: req.Name,
|
|
TypeCode: req.TypeCode,
|
|
BuildingIDs: req.BuildingIDs,
|
|
StatusCode: req.StatusCode,
|
|
ResponsibleOrgID: req.ResponsibleOrgID,
|
|
ImplementOrgIDs: req.ImplementOrgIDs,
|
|
LeaderPersonID: req.LeaderPersonID,
|
|
ParticipantPersonIDs: req.ParticipantPersonIDs,
|
|
StartAt: *startAt,
|
|
EndAt: *endAt,
|
|
Description: req.Description,
|
|
Attachments: req.Attachments,
|
|
OrgID: req.OrgID,
|
|
CreatorUserID: creatorID,
|
|
Remark: req.Remark,
|
|
}
|
|
if completedAt != nil {
|
|
p.CompletedAt = completedAt
|
|
}
|
|
|
|
out, err := service.ProjectCreate(p)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": out})
|
|
}
|
|
|
|
func ProjectUpdate(c *gin.Context) {
|
|
id := c.Param("id")
|
|
var req ProjectUpdateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"})
|
|
return
|
|
}
|
|
|
|
patch := &model.Project{}
|
|
if req.ProjectNo != nil {
|
|
patch.ProjectNo = *req.ProjectNo
|
|
}
|
|
if req.Name != nil {
|
|
patch.Name = *req.Name
|
|
}
|
|
if req.TypeCode != nil {
|
|
patch.TypeCode = *req.TypeCode
|
|
}
|
|
if req.BuildingIDs != nil {
|
|
patch.BuildingIDs = *req.BuildingIDs
|
|
}
|
|
if req.StatusCode != nil {
|
|
patch.StatusCode = *req.StatusCode
|
|
}
|
|
if req.ResponsibleOrgID != nil {
|
|
patch.ResponsibleOrgID = *req.ResponsibleOrgID
|
|
}
|
|
if req.ImplementOrgIDs != nil {
|
|
patch.ImplementOrgIDs = *req.ImplementOrgIDs
|
|
}
|
|
if req.LeaderPersonID != nil {
|
|
patch.LeaderPersonID = *req.LeaderPersonID
|
|
}
|
|
if req.ParticipantPersonIDs != nil {
|
|
patch.ParticipantPersonIDs = *req.ParticipantPersonIDs
|
|
}
|
|
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.CompletedAt != nil {
|
|
t, err := parseDateOnly(req.CompletedAt)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "实际完成时间格式错误"})
|
|
return
|
|
}
|
|
patch.CompletedAt = t
|
|
}
|
|
if req.Description != nil {
|
|
patch.Description = *req.Description
|
|
}
|
|
if req.Attachments != nil {
|
|
patch.Attachments = *req.Attachments
|
|
}
|
|
if req.OrgID != nil {
|
|
patch.OrgID = *req.OrgID
|
|
}
|
|
if req.CreatorUserID != nil {
|
|
patch.CreatorUserID = *req.CreatorUserID
|
|
}
|
|
if req.Remark != nil {
|
|
patch.Remark = *req.Remark
|
|
}
|
|
|
|
out, err := service.ProjectUpdate(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{"data": out})
|
|
}
|
|
|
|
func ProjectDelete(c *gin.Context) {
|
|
usr := currentUser(c)
|
|
id := c.Param("id")
|
|
if err := service.ProjectDelete(usr, &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 ProjectGet(c *gin.Context) {
|
|
id := c.Param("id")
|
|
p, err := service.ProjectGet(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{"project": p})
|
|
}
|
|
|
|
func ProjectList(c *gin.Context) {
|
|
page, size := pageAndSize(c)
|
|
orgID := c.Query("orgId")
|
|
keyword := c.Query("keyword")
|
|
|
|
var oid *string
|
|
if orgID != "" {
|
|
oid = &orgID
|
|
}
|
|
var kw *string
|
|
if keyword != "" {
|
|
kw = &keyword
|
|
}
|
|
|
|
items, total, err := service.ProjectPage(oid, 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,
|
|
})
|
|
}
|