v2_10
This commit is contained in:
318
internal/erpserver/handler/project/project.go
Normal file
318
internal/erpserver/handler/project/project.go
Normal file
@@ -0,0 +1,318 @@
|
||||
package project
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
formDto "management/internal/db/model/form"
|
||||
db "management/internal/db/sqlc"
|
||||
"management/internal/erpserver/biz"
|
||||
"management/internal/global"
|
||||
"management/internal/global/html"
|
||||
"management/internal/middleware/manage/auth"
|
||||
"management/internal/pkg/convertor"
|
||||
"management/internal/pkg/snowflake"
|
||||
"management/internal/router/manage/util"
|
||||
"management/internal/tpl"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
type PorjectHandler interface {
|
||||
List(w http.ResponseWriter, r *http.Request)
|
||||
Add(w http.ResponseWriter, r *http.Request)
|
||||
Edit(w http.ResponseWriter, r *http.Request)
|
||||
Save(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
|
||||
type porjectHandler struct {
|
||||
render tpl.Renderer
|
||||
biz biz.IBiz
|
||||
}
|
||||
|
||||
var _ PorjectHandler = (*porjectHandler)(nil)
|
||||
|
||||
func NewPorjectHandler(render tpl.Renderer, biz biz.IBiz) *porjectHandler {
|
||||
return &porjectHandler{
|
||||
render: render,
|
||||
biz: biz,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *porjectHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodGet:
|
||||
h.render.HTML(w, r, "project/list.tmpl", map[string]any{
|
||||
"Statuses": html.NewSelectControls(global.ProjectStatuses, 0),
|
||||
})
|
||||
case http.MethodPost:
|
||||
title := strings.TrimSpace(r.PostFormValue("title"))
|
||||
var search string
|
||||
if len(title) > 0 {
|
||||
search = "%" + title + "%"
|
||||
if strings.HasSuffix(title, ":") {
|
||||
search = title[:len(title)-1] + "%"
|
||||
}
|
||||
}
|
||||
arg := &db.ListProjectConditionParam{
|
||||
IsTitle: len(search) > 0,
|
||||
Title: search,
|
||||
Status: convertor.ConvertInt[int16](r.PostFormValue("status"), 9999),
|
||||
PageID: convertor.ConvertInt[int32](r.PostFormValue("page"), 1),
|
||||
PageSize: convertor.ConvertInt[int32](r.PostFormValue("rows"), 10),
|
||||
}
|
||||
arg.TimeBegin, arg.TimeEnd = util.DefaultStartTimeAndEndTime(r.PostFormValue("timeBegin"), r.PostFormValue("timeEnd"))
|
||||
res, total, err := h.biz.ProjectV1().List(r.Context(), arg)
|
||||
if err != nil {
|
||||
h.render.JSONERR(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
data := tpl.ResponseList{
|
||||
Code: 0,
|
||||
Message: "ok",
|
||||
Count: total,
|
||||
Data: res,
|
||||
}
|
||||
h.render.JSON(w, data)
|
||||
default:
|
||||
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *porjectHandler) Add(w http.ResponseWriter, r *http.Request) {
|
||||
authUser := auth.AuthUser(r.Context())
|
||||
h.render.HTML(w, r, "project/edit.tmpl", map[string]any{
|
||||
"Item": &formDto.ProjectForm{
|
||||
ApplyUserID: authUser.ID,
|
||||
ProjectFiles: &formDto.ProjectFileForm{
|
||||
ProjectFileItems: []*formDto.ProjectFileItemForm{},
|
||||
},
|
||||
},
|
||||
"Statuses": html.NewSelectControls(global.ProjectStatuses, 0),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *porjectHandler) Edit(w http.ResponseWriter, r *http.Request) {
|
||||
form := &formDto.ProjectForm{}
|
||||
id := convertor.ConvertInt[int64](r.URL.Query().Get("id"), 0)
|
||||
if id > 0 {
|
||||
ctx := r.Context()
|
||||
if po, err := h.biz.ProjectV1().Get(ctx, id); err == nil {
|
||||
pfs, err := h.biz.ProjectV1().ListProjectFiles(ctx, po.ID)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
form = form.ToForm(po, pfs)
|
||||
if form.ApplyUserID == 0 {
|
||||
authUser := auth.AuthUser(ctx)
|
||||
form.ApplyUserID = authUser.ID
|
||||
}
|
||||
// if c, err := db.Engine.GetCustomer(ctx, po.CustomerID); err == nil {
|
||||
// form.CustomerName = c.Name
|
||||
// }
|
||||
if u, err := h.biz.SystemV1().UserBiz().Get(ctx, po.CreatedUserID); err == nil {
|
||||
form.CreatedName = u.Username
|
||||
}
|
||||
if u, err := h.biz.SystemV1().UserBiz().Get(ctx, po.UpdatedUserID); err == nil {
|
||||
form.UpdatedName = u.Username
|
||||
}
|
||||
}
|
||||
}
|
||||
h.render.HTML(w, r, "project/edit.tmpl", map[string]any{
|
||||
"Item": form,
|
||||
"Statuses": html.NewSelectControls(global.ProjectStatuses, form.Status),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *porjectHandler) Save(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
form, err := validForm(r)
|
||||
if err != nil {
|
||||
tpl.JSONERR(w, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
authUser := auth.AuthUser(ctx)
|
||||
if form.ID > 0 {
|
||||
p := &db.UpdateProjectParams{
|
||||
ID: form.ID,
|
||||
Name: pgtype.Text{
|
||||
String: form.Name,
|
||||
Valid: true,
|
||||
},
|
||||
StartAt: pgtype.Timestamptz{
|
||||
Time: form.StartAt,
|
||||
Valid: true,
|
||||
},
|
||||
EndAt: pgtype.Timestamptz{
|
||||
Time: form.EndAt,
|
||||
Valid: true,
|
||||
},
|
||||
CustomerID: pgtype.Int8{
|
||||
Int64: form.CustomerID,
|
||||
Valid: true,
|
||||
},
|
||||
TotalMoney: form.TotalMoneyF,
|
||||
Description: pgtype.Text{
|
||||
String: form.Description,
|
||||
Valid: true,
|
||||
},
|
||||
ApplyAt: pgtype.Timestamptz{
|
||||
Time: form.ApplyAt,
|
||||
Valid: true,
|
||||
},
|
||||
ApplyUserID: pgtype.Int4{
|
||||
Int32: form.ApplyUserID,
|
||||
Valid: true,
|
||||
},
|
||||
ManagerID: pgtype.Int4{
|
||||
Int32: form.ManagerID,
|
||||
Valid: true,
|
||||
},
|
||||
Members: pgtype.Text{
|
||||
String: form.Members,
|
||||
Valid: true,
|
||||
},
|
||||
Status: pgtype.Int2{
|
||||
Int16: form.Status,
|
||||
Valid: true,
|
||||
},
|
||||
UpdatedUserID: pgtype.Int4{
|
||||
Int32: authUser.ID,
|
||||
Valid: true,
|
||||
},
|
||||
}
|
||||
cpfs := []*db.CreateProjectFileParams{}
|
||||
for _, pfile := range form.ProjectFiles.ProjectFileItems {
|
||||
cpfs = append(cpfs, &db.CreateProjectFileParams{
|
||||
ID: snowflake.GetId(),
|
||||
Name: pfile.Name,
|
||||
Path: pfile.Path,
|
||||
ProjectID: form.ID,
|
||||
CreatedUserID: authUser.ID,
|
||||
})
|
||||
}
|
||||
err := h.biz.ProjectV1().Update(ctx, p, cpfs)
|
||||
if err != nil {
|
||||
tpl.JSONERR(w, err.Error())
|
||||
return
|
||||
}
|
||||
tpl.JSONOK(w, "更新成功")
|
||||
} else {
|
||||
p := &db.CreateProjectParams{
|
||||
ID: snowflake.GetId(),
|
||||
Name: form.Name,
|
||||
StartAt: form.StartAt,
|
||||
EndAt: form.EndAt,
|
||||
CustomerID: form.CustomerID,
|
||||
TotalMoney: form.TotalMoneyF,
|
||||
Description: form.Description,
|
||||
ApplyAt: form.ApplyAt,
|
||||
ApplyUserID: form.ApplyUserID,
|
||||
ManagerID: form.ManagerID,
|
||||
Members: form.Members,
|
||||
Status: form.Status,
|
||||
CreatedUserID: authUser.ID,
|
||||
}
|
||||
cpfs := []*db.CreateProjectFileParams{}
|
||||
for _, pfile := range form.ProjectFiles.ProjectFileItems {
|
||||
cpfs = append(cpfs, &db.CreateProjectFileParams{
|
||||
ID: snowflake.GetId(),
|
||||
Name: pfile.Name,
|
||||
Path: pfile.Path,
|
||||
ProjectID: p.ID,
|
||||
CreatedUserID: authUser.ID,
|
||||
})
|
||||
}
|
||||
err := h.biz.ProjectV1().Create(ctx, p, cpfs)
|
||||
if err != nil {
|
||||
tpl.JSONERR(w, err.Error())
|
||||
return
|
||||
}
|
||||
tpl.JSONOK(w, "添加成功")
|
||||
}
|
||||
}
|
||||
|
||||
func validForm(r *http.Request) (formDto.ProjectForm, error) {
|
||||
var err error
|
||||
form := formDto.ProjectForm{}
|
||||
|
||||
form.ID = convertor.ConvertInt[int64](r.PostFormValue("ID"), 0)
|
||||
form.CustomerID, err = strconv.ParseInt(r.PostFormValue("CustomerID"), 10, 64)
|
||||
if err != nil || form.CustomerID == 0 {
|
||||
return form, errors.New("客户不能为空")
|
||||
}
|
||||
|
||||
form.Name = r.PostFormValue("Name")
|
||||
if len(form.Name) == 0 {
|
||||
return form, errors.New("名称不能为空")
|
||||
}
|
||||
form.StartAt, err = time.ParseInLocation("2006-01-02", r.PostFormValue("StartAt"), time.Local)
|
||||
if err != nil {
|
||||
return form, errors.New("开始时间格式错误")
|
||||
}
|
||||
form.EndAt, err = time.ParseInLocation("2006-01-02", r.PostFormValue("EndAt"), time.Local)
|
||||
if err != nil {
|
||||
return form, errors.New("结束时间格式错误")
|
||||
}
|
||||
|
||||
if err := form.TotalMoneyF.Scan(r.PostFormValue("TotalMoney")); err != nil {
|
||||
return form, errors.New("总金额格式错误")
|
||||
}
|
||||
|
||||
form.Description = r.PostFormValue("Description")
|
||||
form.ApplyAt, err = time.ParseInLocation("2006-01-02", r.PostFormValue("ApplyAt"), time.Local)
|
||||
if err != nil {
|
||||
return form, errors.New("申请时间格式错误")
|
||||
}
|
||||
form.ApplyUserID = convertor.ConvertInt[int32](r.PostFormValue("ApplyUserID"), 0)
|
||||
if form.ApplyUserID == 0 {
|
||||
return form, errors.New("申请人不能为空")
|
||||
}
|
||||
|
||||
form.ManagerID = convertor.ConvertInt[int32](r.PostFormValue("ManagerID"), 0)
|
||||
if form.ManagerID == 0 {
|
||||
return form, errors.New("项目经理不能为空")
|
||||
}
|
||||
|
||||
form.Members = r.PostFormValue("Members")
|
||||
if len(form.Members) > 0 {
|
||||
membersSplit := strings.SplitSeq(form.Members, ",")
|
||||
for v := range membersSplit {
|
||||
m := convertor.ConvertInt[int32](v, 0)
|
||||
if m == 0 {
|
||||
return form, errors.New("项目成员数据错误")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
form.Status = convertor.ConvertInt[int16](r.PostFormValue("Status"), 9999)
|
||||
|
||||
form.ProjectFiles = &formDto.ProjectFileForm{
|
||||
ProjectFileItems: []*formDto.ProjectFileItemForm{},
|
||||
}
|
||||
fns := r.PostFormValue("Paths")
|
||||
if len(fns) > 0 {
|
||||
fnsSplit := strings.SplitSeq(fns, ",")
|
||||
for v := range fnsSplit {
|
||||
if len(v) > 0 {
|
||||
read := strings.Split(v, "|")
|
||||
if len(read) != 2 {
|
||||
return form, errors.New("文件路径数据错误")
|
||||
}
|
||||
pff := &formDto.ProjectFileItemForm{
|
||||
Name: read[0],
|
||||
Path: read[1],
|
||||
Combination: v,
|
||||
}
|
||||
form.ProjectFiles.ProjectFileItems = append(form.ProjectFiles.ProjectFileItems, pff)
|
||||
}
|
||||
}
|
||||
}
|
||||
return form, nil
|
||||
}
|
||||
Reference in New Issue
Block a user