210 lines
5.8 KiB
Go
210 lines
5.8 KiB
Go
package customer
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"management/internal/db/model/form"
|
|
db "management/internal/db/sqlc"
|
|
"management/internal/erpserver/biz"
|
|
"management/internal/pkg/convertor"
|
|
"management/internal/pkg/know"
|
|
"management/internal/pkg/middleware"
|
|
"management/internal/pkg/snowflake"
|
|
"management/internal/pkg/tpl"
|
|
"management/internal/pkg/tpl/html"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
type CustomerHandler 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 customerHandler struct {
|
|
render tpl.Renderer
|
|
biz biz.IBiz
|
|
mi middleware.Middleware
|
|
}
|
|
|
|
var _ CustomerHandler = (*customerHandler)(nil)
|
|
|
|
func NewCustomerHandler(render tpl.Renderer, biz biz.IBiz, mi middleware.Middleware) *customerHandler {
|
|
return &customerHandler{
|
|
render: render,
|
|
biz: biz,
|
|
mi: mi,
|
|
}
|
|
}
|
|
|
|
func (h *customerHandler) List(w http.ResponseWriter, r *http.Request) {
|
|
switch r.Method {
|
|
case http.MethodGet:
|
|
ctx := r.Context()
|
|
cc, _ := h.biz.SystemV1().CategoryBiz().ListHtmlByLetter(ctx, know.CustomerCategory)
|
|
ss, _ := h.biz.SystemV1().CategoryBiz().ListHtmlByLetter(ctx, know.CustomerSource)
|
|
h.render.HTML(w, r, "customer/list.tmpl", map[string]any{
|
|
"Statuses": html.NewSelectControls(html.Statuses, "0"),
|
|
"Categories": html.NewSelectControls(cc, "0"),
|
|
"Sources": html.NewSelectControls(ss, "0"),
|
|
})
|
|
case http.MethodPost:
|
|
ctx := r.Context()
|
|
category := convertor.ConvertInt[int32](r.PostFormValue("category"), 9999)
|
|
if category == 0 {
|
|
category = 9999
|
|
}
|
|
source := convertor.ConvertInt[int32](r.PostFormValue("source"), 9999)
|
|
if source == 0 {
|
|
source = 9999
|
|
}
|
|
|
|
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.ListCustomerConditionParam{
|
|
IsTitle: len(search) > 0,
|
|
Title: search,
|
|
Status: convertor.ConvertInt[int16](r.PostFormValue("status"), 9999),
|
|
Category: category,
|
|
Source: source,
|
|
PageID: convertor.ConvertInt[int32](r.PostFormValue("page"), 1),
|
|
PageSize: convertor.ConvertInt[int32](r.PostFormValue("rows"), 10),
|
|
}
|
|
arg.TimeBegin, arg.TimeEnd = convertor.DefaultStartTimeAndEndTime(r.PostFormValue("timeBegin"), r.PostFormValue("timeEnd"))
|
|
res, total, err := h.biz.CustomerV1().List(ctx, 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 *customerHandler) Add(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
cc, _ := h.biz.SystemV1().CategoryBiz().ListHtmlByLetter(ctx, know.CustomerCategory)
|
|
ss, _ := h.biz.SystemV1().CategoryBiz().ListHtmlByLetter(ctx, know.CustomerSource)
|
|
h.render.HTML(w, r, "customer/edit.tmpl", map[string]any{
|
|
"Item": &form.CustomerForm{},
|
|
"Statuses": html.NewSelectControls(html.Statuses, "0"),
|
|
"Categories": html.NewSelectControls(cc, "0"),
|
|
"Sources": html.NewSelectControls(ss, "0"),
|
|
})
|
|
}
|
|
|
|
func (h *customerHandler) Edit(w http.ResponseWriter, r *http.Request) {
|
|
vars := r.URL.Query()
|
|
id := convertor.ConvertInt[int64](vars.Get("id"), 0)
|
|
customer := &form.CustomerForm{}
|
|
ctx := r.Context()
|
|
if id > 0 {
|
|
if cus, err := h.biz.CustomerV1().Get(ctx, id); err == nil {
|
|
customer = customer.ToForm(cus)
|
|
if u, err := h.biz.SystemV1().UserBiz().Get(ctx, cus.CreatedBy); err == nil {
|
|
customer.CreatedBy = u.Username
|
|
}
|
|
if u, err := h.biz.SystemV1().UserBiz().Get(ctx, cus.UpdatedBy); err == nil {
|
|
customer.UpdatedBy = u.Username
|
|
}
|
|
}
|
|
}
|
|
cc, _ := h.biz.SystemV1().CategoryBiz().ListHtmlByLetter(ctx, know.CustomerCategory)
|
|
ss, _ := h.biz.SystemV1().CategoryBiz().ListHtmlByLetter(ctx, know.CustomerSource)
|
|
h.render.HTML(w, r, "customer/edit.tmpl", map[string]any{
|
|
"Item": customer,
|
|
"Statuses": html.NewSelectControls(html.Statuses, strconv.Itoa(int(customer.Status))),
|
|
"Categories": html.NewSelectControls(cc, strconv.Itoa(int(customer.Category))),
|
|
"Sources": html.NewSelectControls(ss, strconv.Itoa(int(customer.Source))),
|
|
})
|
|
}
|
|
|
|
func (h *customerHandler) Save(w http.ResponseWriter, r *http.Request) {
|
|
data := &form.CustomerForm{}
|
|
if err := form.BindForm(r, data); err != nil {
|
|
h.render.JSONERR(w, err.Error())
|
|
return
|
|
}
|
|
|
|
ctx := r.Context()
|
|
authUser := h.mi.AuthUser(ctx)
|
|
if data.ID > 0 {
|
|
arg := &db.UpdateCustomerParams{
|
|
ID: data.ID,
|
|
Name: pgtype.Text{
|
|
String: data.Name,
|
|
Valid: true,
|
|
},
|
|
Category: pgtype.Int4{
|
|
Int32: data.Category,
|
|
Valid: true,
|
|
},
|
|
Source: pgtype.Int4{
|
|
Int32: data.Source,
|
|
Valid: true,
|
|
},
|
|
Address: pgtype.Text{
|
|
String: data.Address,
|
|
Valid: true,
|
|
},
|
|
ContactName: pgtype.Text{
|
|
String: data.ContactName,
|
|
Valid: true,
|
|
},
|
|
ContactPhone: pgtype.Text{
|
|
String: data.ContactPhone,
|
|
Valid: true,
|
|
},
|
|
Status: pgtype.Int2{
|
|
Int16: data.Status,
|
|
Valid: true,
|
|
},
|
|
UpdatedBy: pgtype.Int4{
|
|
Int32: authUser.ID,
|
|
Valid: true,
|
|
},
|
|
}
|
|
_, err := h.biz.CustomerV1().Update(ctx, arg)
|
|
if err != nil {
|
|
h.render.JSONERR(w, err.Error())
|
|
return
|
|
}
|
|
h.render.JSONOK(w, "更新成功")
|
|
} else {
|
|
arg := &db.CreateCustomerParams{
|
|
ID: snowflake.GetId(),
|
|
Name: data.Name,
|
|
Category: data.Category,
|
|
Source: data.Source,
|
|
Address: data.Address,
|
|
ContactName: data.ContactName,
|
|
ContactPhone: data.ContactPhone,
|
|
CreatedBy: authUser.ID,
|
|
}
|
|
_, err := h.biz.CustomerV1().Create(ctx, arg)
|
|
if err != nil {
|
|
h.render.JSONERR(w, err.Error())
|
|
return
|
|
}
|
|
h.render.JSONOK(w, "添加成功")
|
|
}
|
|
}
|