供应商管理
This commit is contained in:
parent
ede53d0421
commit
d13db94020
158
app/admin/apis/basic/supplier.go
Normal file
158
app/admin/apis/basic/supplier.go
Normal file
|
@ -0,0 +1,158 @@
|
|||
package basic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go-admin/app/admin/models"
|
||||
orm "go-admin/common/global"
|
||||
"go-admin/logger"
|
||||
"go-admin/tools/app"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type SupplierCreateRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Contact string `json:"contact" binding:"required"`
|
||||
Tel string `json:"tel" binding:"required"`
|
||||
Address string `json:"address" binding:"required"`
|
||||
OpeningBank string `json:"opening_bank" binding:"required"`
|
||||
BankAccount string `json:"bank_account" binding:"required"`
|
||||
PaymentCycle uint32 `json:"payment_cycle" binding:"required"`
|
||||
TaxNumber string `json:"tax_number"`
|
||||
StoreIds string `json:"store_ids"`
|
||||
Landline string `json:"landline"`
|
||||
Email string `json:"email"`
|
||||
CompanyWebsite string `json:"company_website"`
|
||||
Province string `json:"province" binding:"required"`
|
||||
City string `json:"city" binding:"required"`
|
||||
Area string `json:"area" binding:"required"`
|
||||
}
|
||||
|
||||
// SupplierCreate 添加供应商
|
||||
func SupplierCreate(c *gin.Context) {
|
||||
req := new(SupplierCreateRequest)
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(err)
|
||||
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
||||
return
|
||||
}
|
||||
supplier := &models.Supplier{
|
||||
Name: req.Name,
|
||||
Contact: req.Contact,
|
||||
Tel: req.Tel,
|
||||
Address: req.Address,
|
||||
OpeningBank: req.OpeningBank,
|
||||
BankAccount: req.BankAccount,
|
||||
PaymentCycle: req.PaymentCycle,
|
||||
TaxNumber: req.TaxNumber,
|
||||
Landline: req.Landline,
|
||||
Email: req.Email,
|
||||
CompanyWebsite: req.CompanyWebsite,
|
||||
}
|
||||
|
||||
err := orm.Eloquent.Create(supplier).Error
|
||||
if err != nil {
|
||||
logger.Error("create supplier err:", err)
|
||||
app.Error(c, http.StatusInternalServerError, err, "操作失败")
|
||||
return
|
||||
}
|
||||
|
||||
app.OK(c, supplier, "创建成功")
|
||||
return
|
||||
}
|
||||
|
||||
type SupplierUpdateRequest struct {
|
||||
SupplierCreateRequest
|
||||
Id uint32 `json:"id" binding:"required"`
|
||||
}
|
||||
|
||||
// SupplierUpdate 更新供应商
|
||||
func SupplierUpdate(c *gin.Context) {
|
||||
req := new(SupplierUpdateRequest)
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(err)
|
||||
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
||||
return
|
||||
}
|
||||
supplier := &models.Supplier{
|
||||
Name: req.Name,
|
||||
Contact: req.Contact,
|
||||
Tel: req.Tel,
|
||||
Address: req.Address,
|
||||
OpeningBank: req.OpeningBank,
|
||||
BankAccount: req.BankAccount,
|
||||
PaymentCycle: req.PaymentCycle,
|
||||
TaxNumber: req.TaxNumber,
|
||||
Landline: req.Landline,
|
||||
Email: req.Email,
|
||||
CompanyWebsite: req.CompanyWebsite,
|
||||
}
|
||||
err := orm.Eloquent.Where("id", req.Id).Updates(supplier).Error
|
||||
if err != nil {
|
||||
logger.Error("update supplier err :", err)
|
||||
app.Error(c, http.StatusInternalServerError, err, "更新失败")
|
||||
return
|
||||
}
|
||||
app.OK(c, nil, "更新成功")
|
||||
return
|
||||
}
|
||||
|
||||
type SupplierListRequest struct {
|
||||
Number string `json:"number"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// SupplierList 供应商列表
|
||||
func SupplierList(c *gin.Context) {
|
||||
req := new(SupplierListRequest)
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
logger.Error(err)
|
||||
app.Error(c, http.StatusBadRequest, errors.New("para err"), "参数错误")
|
||||
return
|
||||
}
|
||||
|
||||
list, err := models.GetSupplier(models.GetSupplierRequest{
|
||||
Name: req.Name,
|
||||
Number: req.Number,
|
||||
})
|
||||
if err != nil {
|
||||
app.Error(c, http.StatusInternalServerError, err, "获取列表失败")
|
||||
return
|
||||
}
|
||||
|
||||
app.OK(c, list, "")
|
||||
return
|
||||
}
|
||||
|
||||
// SupplierDetail 供应商详情
|
||||
func SupplierDetail(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
var supplier models.Supplier
|
||||
err := orm.Eloquent.Table("erp_supplier").Where("id=?", id).Find(&supplier).Error
|
||||
if err != nil {
|
||||
logger.Error("get supplier err:", err)
|
||||
app.Error(c, http.StatusInternalServerError, err, "供应商不存在")
|
||||
return
|
||||
}
|
||||
|
||||
app.OK(c, supplier, "")
|
||||
return
|
||||
}
|
||||
|
||||
// SupplierDel 删除供应商
|
||||
func SupplierDel(c *gin.Context) {
|
||||
id := c.Param("id")
|
||||
|
||||
err := orm.
|
||||
Eloquent.Table("erp_supplier").
|
||||
Delete(&models.Supplier{}, id).Error
|
||||
if err != nil {
|
||||
logger.Error("delete supplier failed:", err)
|
||||
app.Error(c, http.StatusInternalServerError, err, "删除失败")
|
||||
return
|
||||
}
|
||||
|
||||
app.OK(c, nil, "")
|
||||
return
|
||||
}
|
74
app/admin/models/supplier.go
Normal file
74
app/admin/models/supplier.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
orm "go-admin/common/global"
|
||||
"go-admin/logger"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Supplier struct {
|
||||
Model
|
||||
|
||||
Number string `json:"number" gorm:"index"` //编号
|
||||
Name string `json:"name"` //供应商名称
|
||||
Contact string `json:"contact"` //联系人
|
||||
Tel string `json:"tel"` //手机号
|
||||
Province string `json:"province"` //省
|
||||
City string `json:"city"` //市
|
||||
Area string `json:"area"` //区
|
||||
Address string `json:"address"` //详细地址
|
||||
AccountHolder string `json:"account_holder"` //开户人
|
||||
OpeningBank string `json:"opening_bank"` //开户银行
|
||||
BankAccount string `json:"bank_account"` //银行账号
|
||||
PaymentCycle uint32 `json:"payment_cycle"` //支付周期
|
||||
TaxNumber string `json:"tax_number"` //税号
|
||||
Landline string `json:"landline"` //固定电话
|
||||
Email string `json:"email"` //邮件
|
||||
CompanyWebsite string `json:"company_website"` //网站
|
||||
}
|
||||
|
||||
func (s *Supplier) TableName() string {
|
||||
return "erp_supplier"
|
||||
}
|
||||
|
||||
func (s *Supplier) BeforeCreate(tx *gorm.DB) error {
|
||||
if s.Number == "" {
|
||||
var partnerCount int64
|
||||
err := tx.Table(s.TableName()).Unscoped().Count(&partnerCount).Error
|
||||
if err != nil {
|
||||
logger.Error("get supplier count failed", err, s)
|
||||
return err
|
||||
}
|
||||
s.Number = fmt.Sprintf("%03d", partnerCount)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type GetSupplierRequest struct {
|
||||
Name string //供应商名称
|
||||
Number string //供应商编号
|
||||
}
|
||||
|
||||
func GetSupplier(req GetSupplierRequest) ([]*Supplier, error) {
|
||||
var list []*Supplier
|
||||
m := orm.Eloquent.Model(Supplier{})
|
||||
if req.Name != "" {
|
||||
m = m.Where("name = ?", req.Number)
|
||||
}
|
||||
|
||||
if req.Number != "" {
|
||||
m = m.Where("number = ?", req.Number)
|
||||
}
|
||||
|
||||
err := m.
|
||||
Order("created_at desc").
|
||||
Find(&list).
|
||||
Error
|
||||
if err != nil {
|
||||
logger.Error("GetSupplierByPartner err:", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return list, nil
|
||||
}
|
|
@ -94,4 +94,5 @@ func examplesCheckRoleRouter(r *gin.Engine, authMiddleware *jwtauth.GinJWTMiddle
|
|||
// 回收卡
|
||||
registerRecycleCardManageRouter(v1, authMiddleware)
|
||||
|
||||
registerSupplierRouter(v1, authMiddleware)
|
||||
}
|
||||
|
|
17
app/admin/router/supplier.go
Normal file
17
app/admin/router/supplier.go
Normal file
|
@ -0,0 +1,17 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"go-admin/app/admin/apis/basic"
|
||||
"go-admin/app/admin/middleware"
|
||||
jwt "go-admin/pkg/jwtauth"
|
||||
)
|
||||
|
||||
func registerSupplierRouter(v1 *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
|
||||
r := v1.Group("/supplier").Use(authMiddleware.MiddlewareFunc()).Use(middleware.AuthCheckRole())
|
||||
r.POST("create", basic.SupplierCreate)
|
||||
r.POST("update", basic.SupplierUpdate)
|
||||
r.DELETE("delete/{id}", basic.SupplierDel)
|
||||
r.GET("detail/{id}", basic.SupplierDetail)
|
||||
r.POST("list", basic.SupplierList)
|
||||
}
|
Loading…
Reference in New Issue
Block a user