203 lines
5.4 KiB
Go
203 lines
5.4 KiB
Go
package customer
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"management/internal/db/model/dto"
|
|
"management/internal/db/model/form"
|
|
db "management/internal/db/sqlc"
|
|
"management/internal/global"
|
|
"management/internal/global/html"
|
|
"management/internal/global/know"
|
|
"management/internal/middleware/manage/auth"
|
|
"management/internal/pkg/snowflake"
|
|
"management/internal/router/manage/util"
|
|
categoryservice "management/internal/service/category"
|
|
"management/internal/tpl"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
func List(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
cc, _ := categoryservice.GetParentCategorySelectLetter(ctx, know.CustomerCategory)
|
|
ss, _ := categoryservice.GetParentCategorySelectLetter(ctx, know.CustomerSource)
|
|
tpl.HTML(w, r, "customer/list.tmpl", map[string]any{
|
|
"Statuses": html.NewSelectControls(global.Statuses, 0),
|
|
"Categories": html.NewSelectStringControls(cc, "0"),
|
|
"Sources": html.NewSelectStringControls(ss, "0"),
|
|
})
|
|
}
|
|
|
|
func PostList(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
category := util.ConvertInt32(r.PostFormValue("category"), 9999)
|
|
if category == 0 {
|
|
category = 9999
|
|
}
|
|
source := util.ConvertInt32(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: util.ConvertInt16(r.PostFormValue("status"), 9999),
|
|
Category: category,
|
|
Source: source,
|
|
PageID: util.ConvertInt32(r.PostFormValue("page"), 1),
|
|
PageSize: util.ConvertInt32(r.PostFormValue("rows"), 10),
|
|
}
|
|
arg.TimeBegin, arg.TimeEnd = util.DefaultStartTimeAndEndTime(r.PostFormValue("timeBegin"), r.PostFormValue("timeEnd"))
|
|
res, total, err := db.Engine.ListCustomerCondition(ctx, arg)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
data := tpl.ResponseList{
|
|
Code: 0,
|
|
Message: "ok",
|
|
Count: total,
|
|
Data: res,
|
|
}
|
|
tpl.JSON(w, data)
|
|
}
|
|
|
|
func Add(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
cc, _ := categoryservice.GetParentCategorySelectLetter(ctx, know.CustomerCategory)
|
|
ss, _ := categoryservice.GetParentCategorySelectLetter(ctx, know.CustomerSource)
|
|
tpl.HTML(w, r, "customer/edit.tmpl", map[string]any{
|
|
"Item": &form.CustomerForm{},
|
|
"Statuses": html.NewSelectControls(global.Statuses, 0),
|
|
"Categories": html.NewSelectStringControls(cc, "0"),
|
|
"Sources": html.NewSelectStringControls(ss, "0"),
|
|
})
|
|
}
|
|
|
|
func Edit(w http.ResponseWriter, r *http.Request) {
|
|
vars := r.URL.Query()
|
|
id := util.ConvertInt[int64](vars.Get("id"), 0)
|
|
customer := &form.CustomerForm{}
|
|
ctx := r.Context()
|
|
if id > 0 {
|
|
if cus, err := db.Engine.GetCustomer(ctx, id); err == nil {
|
|
customer = customer.ToForm(cus)
|
|
if u, err := db.Engine.GetSysUser(ctx, cus.CreatedBy); err == nil {
|
|
customer.CreatedBy = u.Username
|
|
}
|
|
if u, err := db.Engine.GetSysUser(ctx, cus.UpdatedBy); err == nil {
|
|
customer.UpdatedBy = u.Username
|
|
}
|
|
}
|
|
}
|
|
cc, _ := categoryservice.GetParentCategorySelectLetter(ctx, know.CustomerCategory)
|
|
ss, _ := categoryservice.GetParentCategorySelectLetter(ctx, know.CustomerSource)
|
|
tpl.HTML(w, r, "customer/edit.tmpl", map[string]any{
|
|
"Item": customer,
|
|
"Statuses": html.NewSelectControls(global.Statuses, customer.Status),
|
|
"Categories": html.NewSelectStringControls(cc, strconv.Itoa(int(customer.Category))),
|
|
"Sources": html.NewSelectStringControls(ss, strconv.Itoa(int(customer.Source))),
|
|
})
|
|
}
|
|
|
|
func Save(w http.ResponseWriter, r *http.Request) {
|
|
data := &form.CustomerForm{}
|
|
if err := form.BindForm(r, data); err != nil {
|
|
tpl.JSONERR(w, err.Error())
|
|
return
|
|
}
|
|
|
|
ctx := r.Context()
|
|
authUser := auth.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 := db.Engine.UpdateCustomer(ctx, arg)
|
|
if err != nil {
|
|
tpl.JSONERR(w, err.Error())
|
|
return
|
|
}
|
|
tpl.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 := db.Engine.CreateCustomer(ctx, arg)
|
|
if err != nil {
|
|
tpl.JSONERR(w, err.Error())
|
|
return
|
|
}
|
|
tpl.JSONOK(w, "添加成功")
|
|
}
|
|
}
|
|
|
|
func XmSelect(w http.ResponseWriter, r *http.Request) {
|
|
all, err := db.Engine.AllCustomers(r.Context())
|
|
if err != nil {
|
|
tpl.JSONERR(w, err.Error())
|
|
return
|
|
}
|
|
|
|
var res []*dto.XmSelectStrDto
|
|
for _, v := range all {
|
|
res = append(res, &dto.XmSelectStrDto{
|
|
Name: v.Name,
|
|
Value: strconv.FormatInt(v.ID, 10),
|
|
})
|
|
}
|
|
tpl.JSON(w, res)
|
|
}
|