159 lines
4.2 KiB
Go
159 lines
4.2 KiB
Go
|
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
|
||
|
}
|