增加backend的erp若干功能
This commit is contained in:
@@ -0,0 +1,263 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"server/models"
|
||||
"server/pkg/jwtutil"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// BackendErpCompanyContactController ERP客户/供应商联系人控制器
|
||||
type BackendErpCompanyContactController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *BackendErpCompanyContactController) contactClaims() (*jwtutil.Claims, error) {
|
||||
auth := c.Ctx.Request.Header.Get("Authorization")
|
||||
if auth == "" {
|
||||
return nil, fmt.Errorf("未登录")
|
||||
}
|
||||
parts := strings.SplitN(auth, " ", 2)
|
||||
if len(parts) != 2 || parts[0] != "Bearer" {
|
||||
return nil, fmt.Errorf("认证信息格式错误")
|
||||
}
|
||||
claims, err := jwtutil.ParseToken(parts[1])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("无效的token")
|
||||
}
|
||||
if claims.UserType != "backend" {
|
||||
return nil, fmt.Errorf("无权访问")
|
||||
}
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
func (c *BackendErpCompanyContactController) contactJsonErr(httpStatus, bizCode int, msg string) {
|
||||
c.Ctx.Output.SetStatus(httpStatus)
|
||||
c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *BackendErpCompanyContactController) contactOk(data interface{}) {
|
||||
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": data}
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
type companyContactPayload struct {
|
||||
CompanyType string `json:"company_type"`
|
||||
CompanyID uint64 `json:"company_id"`
|
||||
Name string `json:"name"`
|
||||
Gender int8 `json:"gender"`
|
||||
Phone string `json:"phone"`
|
||||
Mobiles string `json:"mobiles"` // JSON数组字符串
|
||||
Wechat string `json:"wechat"`
|
||||
QQ string `json:"qq"`
|
||||
Dingtalk string `json:"dingtalk"`
|
||||
Email string `json:"email"`
|
||||
Department string `json:"department"`
|
||||
Position string `json:"position"`
|
||||
HomeAddress string `json:"home_address"`
|
||||
IsPrimary int8 `json:"is_primary"`
|
||||
Status int8 `json:"status"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
// List GET /backend/erp/company-contact/list?company_type=customer&company_id=1
|
||||
func (c *BackendErpCompanyContactController) List() {
|
||||
claims, err := c.contactClaims()
|
||||
if err != nil {
|
||||
c.contactJsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
companyType := strings.TrimSpace(c.GetString("company_type"))
|
||||
companyIDStr := strings.TrimSpace(c.GetString("company_id"))
|
||||
|
||||
if companyType == "" || companyIDStr == "" {
|
||||
c.contactJsonErr(400, 400, "缺少公司参数")
|
||||
return
|
||||
}
|
||||
|
||||
companyID, err := strconv.ParseUint(companyIDStr, 10, 64)
|
||||
if err != nil {
|
||||
c.contactJsonErr(400, 400, "公司ID格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
qs := models.Orm.QueryTable(new(models.ErpCompanyContact)).
|
||||
Filter("tenant_id", fmt.Sprintf("%d", claims.TenantId)).
|
||||
Filter("company_type", companyType).
|
||||
Filter("company_id", companyID).
|
||||
Filter("delete_time__isnull", true)
|
||||
|
||||
var list []models.ErpCompanyContact
|
||||
_, err = qs.OrderBy("-is_primary", "id").All(&list)
|
||||
if err != nil {
|
||||
c.contactJsonErr(500, 500, "查询失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.contactOk(map[string]interface{}{"list": list})
|
||||
}
|
||||
|
||||
// Create POST /backend/erp/company-contact
|
||||
func (c *BackendErpCompanyContactController) Create() {
|
||||
claims, err := c.contactClaims()
|
||||
if err != nil {
|
||||
c.contactJsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(c.Ctx.Request.Body)
|
||||
if err != nil {
|
||||
c.contactJsonErr(400, 400, "读取请求体失败")
|
||||
return
|
||||
}
|
||||
var p companyContactPayload
|
||||
if err := json.Unmarshal(body, &p); err != nil {
|
||||
c.contactJsonErr(400, 400, "参数格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
if strings.TrimSpace(p.Name) == "" {
|
||||
c.contactJsonErr(400, 400, "联系人姓名不能为空")
|
||||
return
|
||||
}
|
||||
if p.CompanyType == "" || p.CompanyID == 0 {
|
||||
c.contactJsonErr(400, 400, "缺少公司参数")
|
||||
return
|
||||
}
|
||||
|
||||
contact := models.ErpCompanyContact{
|
||||
TenantID: fmt.Sprintf("%d", claims.TenantId),
|
||||
CompanyType: p.CompanyType,
|
||||
CompanyID: p.CompanyID,
|
||||
Name: strings.TrimSpace(p.Name),
|
||||
Gender: p.Gender,
|
||||
Phone: strings.TrimSpace(p.Phone),
|
||||
Mobiles: p.Mobiles,
|
||||
Wechat: strings.TrimSpace(p.Wechat),
|
||||
QQ: strings.TrimSpace(p.QQ),
|
||||
Dingtalk: strings.TrimSpace(p.Dingtalk),
|
||||
Email: strings.TrimSpace(p.Email),
|
||||
Department: strings.TrimSpace(p.Department),
|
||||
Position: strings.TrimSpace(p.Position),
|
||||
HomeAddress: strings.TrimSpace(p.HomeAddress),
|
||||
IsPrimary: p.IsPrimary,
|
||||
Status: p.Status,
|
||||
Remark: strings.TrimSpace(p.Remark),
|
||||
CreateTime: time.Now(),
|
||||
UpdateTime: time.Now(),
|
||||
}
|
||||
|
||||
id, err := models.Orm.Insert(&contact)
|
||||
if err != nil {
|
||||
c.contactJsonErr(500, 500, "创建失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.contactOk(map[string]interface{}{"id": id})
|
||||
}
|
||||
|
||||
// Update PUT /backend/erp/company-contact/:id
|
||||
func (c *BackendErpCompanyContactController) Update() {
|
||||
claims, err := c.contactClaims()
|
||||
if err != nil {
|
||||
c.contactJsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.contactJsonErr(400, 400, "ID格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(c.Ctx.Request.Body)
|
||||
if err != nil {
|
||||
c.contactJsonErr(400, 400, "读取请求体失败")
|
||||
return
|
||||
}
|
||||
var p companyContactPayload
|
||||
if err := json.Unmarshal(body, &p); err != nil {
|
||||
c.contactJsonErr(400, 400, "参数格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
contact := models.ErpCompanyContact{ID: id}
|
||||
if err := models.Orm.Read(&contact); err != nil {
|
||||
c.contactJsonErr(404, 404, "联系人不存在")
|
||||
return
|
||||
}
|
||||
if contact.TenantID != fmt.Sprintf("%d", claims.TenantId) {
|
||||
c.contactJsonErr(403, 403, "无权操作")
|
||||
return
|
||||
}
|
||||
|
||||
contact.Name = strings.TrimSpace(p.Name)
|
||||
contact.Gender = p.Gender
|
||||
contact.Phone = strings.TrimSpace(p.Phone)
|
||||
contact.Mobiles = p.Mobiles
|
||||
contact.Wechat = strings.TrimSpace(p.Wechat)
|
||||
contact.QQ = strings.TrimSpace(p.QQ)
|
||||
contact.Dingtalk = strings.TrimSpace(p.Dingtalk)
|
||||
contact.Email = strings.TrimSpace(p.Email)
|
||||
contact.Department = strings.TrimSpace(p.Department)
|
||||
contact.Position = strings.TrimSpace(p.Position)
|
||||
contact.HomeAddress = strings.TrimSpace(p.HomeAddress)
|
||||
contact.IsPrimary = p.IsPrimary
|
||||
contact.Status = p.Status
|
||||
contact.Remark = strings.TrimSpace(p.Remark)
|
||||
contact.UpdateTime = time.Now()
|
||||
|
||||
_, err = models.Orm.Update(&contact)
|
||||
if err != nil {
|
||||
c.contactJsonErr(500, 500, "更新失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.contactOk(nil)
|
||||
}
|
||||
|
||||
// Delete DELETE /backend/erp/company-contact/:id
|
||||
func (c *BackendErpCompanyContactController) Delete() {
|
||||
claims, err := c.contactClaims()
|
||||
if err != nil {
|
||||
c.contactJsonErr(401, 401, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
idStr := c.Ctx.Input.Param(":id")
|
||||
id, err := strconv.ParseUint(idStr, 10, 64)
|
||||
if err != nil {
|
||||
c.contactJsonErr(400, 400, "ID格式错误")
|
||||
return
|
||||
}
|
||||
|
||||
contact := models.ErpCompanyContact{ID: id}
|
||||
if err := models.Orm.Read(&contact); err != nil {
|
||||
c.contactJsonErr(404, 404, "联系人不存在")
|
||||
return
|
||||
}
|
||||
if contact.TenantID != fmt.Sprintf("%d", claims.TenantId) {
|
||||
c.contactJsonErr(403, 403, "无权操作")
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
contact.DeleteTime = &now
|
||||
_, err = models.Orm.Update(&contact, "delete_time")
|
||||
if err != nil {
|
||||
c.contactJsonErr(500, 500, "删除失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
c.contactOk(nil)
|
||||
}
|
||||
Reference in New Issue
Block a user