835 lines
30 KiB
Go
835 lines
30 KiB
Go
package controllers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"server/models"
|
|
"server/pkg/jwtutil"
|
|
"server/services"
|
|
|
|
"github.com/beego/beego/v2/client/orm"
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
// BackendCrmProjectController CRM 项目管理
|
|
type BackendCrmProjectController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
type projectPayload struct {
|
|
ProjectName string `json:"project_name"`
|
|
ProjectNo string `json:"project_no"`
|
|
// 项目类型:1=单一项目(仅上游客户) 2=上下游项目(上游客户 + 下游供应商)
|
|
ProjectType int8 `json:"project_type"`
|
|
CustomerID uint64 `json:"customer_id"`
|
|
CustomerName string `json:"customer_name"`
|
|
SupplierID uint64 `json:"supplier_id"`
|
|
SupplierRefType int8 `json:"supplier_ref_type"` // 下游主体来源:1客户 2供应商
|
|
SupplierName string `json:"supplier_name"`
|
|
SupplierIndustry string `json:"supplier_industry"`
|
|
OwnerUserID string `json:"owner_user_id"`
|
|
OwnerUserName string `json:"owner_user_name"`
|
|
Industry string `json:"industry"`
|
|
// 金额:上游=对客户收入,下游=对供应商成本
|
|
UpstreamAmount float64 `json:"upstream_amount"`
|
|
DownstreamAmount float64 `json:"downstream_amount"`
|
|
// Amount 为旧「项目金额」,未传上游金额时回退使用(兼容未升级的调用方)
|
|
Amount float64 `json:"amount"`
|
|
Status int8 `json:"status"`
|
|
StartDate string `json:"start_date"`
|
|
EndDate string `json:"end_date"`
|
|
// 上下游对接人:仅姓名 + 电话;Contact* 为旧字段,未传新字段时回退使用
|
|
UpstreamContact string `json:"upstream_contact"`
|
|
UpstreamPhone string `json:"upstream_phone"`
|
|
DownstreamContact string `json:"downstream_contact"`
|
|
DownstreamPhone string `json:"downstream_phone"`
|
|
ContactPerson string `json:"contact_person"`
|
|
ContactPosition string `json:"contact_position"`
|
|
ContactPhone string `json:"contact_phone"`
|
|
Address string `json:"address"`
|
|
Remark string `json:"remark"`
|
|
Products string `json:"products"` // 项目产品清单JSON(生成时写入产品管理)
|
|
}
|
|
|
|
// normalizeProjectFields 归一项目类型、上下游金额与上下游对接人:
|
|
// - 项目类型仅识别 2=上下游,其余一律按 1=单一项目处理;
|
|
// - 上游金额 / 上游对接人缺省时回退旧的 amount / contact_* 字段(兼容未升级的调用方);
|
|
// - 单一项目不存在下游,下游金额与下游对接人一并清空。
|
|
func normalizeProjectFields(p *projectPayload) (pt int8, upstream, downstream float64, upContact, upPhone, downContact, downPhone string) {
|
|
pt = p.ProjectType
|
|
if pt != 2 {
|
|
pt = 1
|
|
}
|
|
upstream = p.UpstreamAmount
|
|
if upstream == 0 && p.Amount != 0 {
|
|
upstream = p.Amount
|
|
}
|
|
upContact = strings.TrimSpace(firstNonEmpty(p.UpstreamContact, p.ContactPerson))
|
|
upPhone = strings.TrimSpace(firstNonEmpty(p.UpstreamPhone, p.ContactPhone))
|
|
if pt == 2 {
|
|
downstream = p.DownstreamAmount
|
|
downContact = strings.TrimSpace(p.DownstreamContact)
|
|
downPhone = strings.TrimSpace(p.DownstreamPhone)
|
|
}
|
|
return
|
|
}
|
|
|
|
// resolveProjectSupplier 解析项目下游主体:
|
|
// refType=2 关联供应商库,refType=1 关联客户库(同一家单位可能以客户身份建档);
|
|
// 命中时以库中的 ID 与标准名称为准,未命中(或未选择库记录)时保留手工填写的名称、不保留关联。
|
|
func resolveProjectSupplier(tenantID string, refType int8, refID uint64, name string) (*uint64, int8, string) {
|
|
name = strings.TrimSpace(name)
|
|
if refID == 0 {
|
|
return nil, 0, name
|
|
}
|
|
switch refType {
|
|
case 2:
|
|
var sup models.TenantCrmSupplier
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmSupplier)).
|
|
Filter("id", refID).Filter("tenant_id", tenantID).
|
|
Filter("delete_time__isnull", true).One(&sup); err != nil {
|
|
return nil, 0, name
|
|
}
|
|
id := sup.ID
|
|
if strings.TrimSpace(sup.SupplierName) != "" {
|
|
name = sup.SupplierName
|
|
}
|
|
return &id, 2, name
|
|
case 1:
|
|
var cus models.TenantCrmCustomer
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmCustomer)).
|
|
Filter("id", refID).Filter("tenant_id", tenantID).
|
|
Filter("delete_time__isnull", true).One(&cus); err != nil {
|
|
return nil, 0, name
|
|
}
|
|
id := cus.ID
|
|
if strings.TrimSpace(cus.CustomerName) != "" {
|
|
name = cus.CustomerName
|
|
}
|
|
return &id, 1, name
|
|
}
|
|
return nil, 0, name
|
|
}
|
|
|
|
// attachProjectContractPeriod 汇总项目下所有合同的实际起止时间:
|
|
// 开始取各合同最早的生效日期(未填生效日期时回退签订日期),结束取最晚的结束日期。
|
|
// 项目不再手工维护起止时间,列表 / 详情统一按此展示;改合同后自动跟随,无需同步。
|
|
func attachProjectContractPeriod(tenantID string, list []models.TenantCrmProject) {
|
|
if len(list) == 0 {
|
|
return
|
|
}
|
|
idx := make(map[uint64]int, len(list))
|
|
ids := make([]uint64, 0, len(list))
|
|
for i := range list {
|
|
idx[list[i].ID] = i
|
|
ids = append(ids, list[i].ID)
|
|
}
|
|
var rows []models.TenantCrmContract
|
|
_, err := models.Orm.QueryTable(new(models.TenantCrmContract)).
|
|
Filter("tenant_id", tenantID).Filter("delete_time__isnull", true).
|
|
Filter("project_id__in", ids).
|
|
All(&rows, "project_id", "sign_date", "effective_date", "expire_date")
|
|
if err != nil {
|
|
return
|
|
}
|
|
for i := range rows {
|
|
ct := rows[i]
|
|
if ct.ProjectID == nil {
|
|
continue
|
|
}
|
|
pos, ok := idx[*ct.ProjectID]
|
|
if !ok {
|
|
continue
|
|
}
|
|
start := ct.EffectiveDate
|
|
if start == nil {
|
|
start = ct.SignDate
|
|
}
|
|
if start != nil && (list[pos].ContractStartDate == nil || start.Before(*list[pos].ContractStartDate)) {
|
|
list[pos].ContractStartDate = start
|
|
}
|
|
if ct.ExpireDate != nil && (list[pos].ContractEndDate == nil || ct.ExpireDate.After(*list[pos].ContractEndDate)) {
|
|
list[pos].ContractEndDate = ct.ExpireDate
|
|
}
|
|
}
|
|
}
|
|
|
|
// List GET /backend/crm/project/list
|
|
func (c *BackendCrmProjectController) List() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
page, _ := c.GetInt("page", 1)
|
|
pageSize, _ := c.GetInt("pageSize", 20)
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if pageSize < 1 || pageSize > 100 {
|
|
pageSize = 20
|
|
}
|
|
keyword := strings.TrimSpace(c.GetString("keyword"))
|
|
status := strings.TrimSpace(c.GetString("status"))
|
|
ownerID := strings.TrimSpace(c.GetString("owner_user_id"))
|
|
|
|
tenantID := pipelineTenantID(claims)
|
|
cond := orm.NewCondition().And("tenant_id", tenantID).And("delete_time__isnull", true)
|
|
// 数据范围:平台/租户管理员可见全部,其他用户仅本人(负责人或创建人)
|
|
cond = crmApplyOwnerScope(cond, claims)
|
|
if keyword != "" {
|
|
kw := orm.NewCondition().
|
|
Or("project_name__contains", keyword).
|
|
Or("project_no__contains", keyword).
|
|
Or("customer_name__contains", keyword).
|
|
Or("supplier_name__contains", keyword)
|
|
cond = cond.AndCond(kw)
|
|
}
|
|
if status != "" {
|
|
cond = cond.And("status", status)
|
|
}
|
|
if ownerID != "" {
|
|
cond = cond.And("owner_user_id", ownerID)
|
|
}
|
|
qs := models.Orm.QueryTable(new(models.TenantCrmProject)).SetCond(cond)
|
|
|
|
total, _ := qs.Count()
|
|
var list []models.TenantCrmProject
|
|
if total > 0 {
|
|
_, _ = qs.OrderBy("-id").Offset((page - 1) * pageSize).Limit(pageSize).All(&list)
|
|
}
|
|
attachBusinessName(tenantID, list)
|
|
// 项目起止时间由关联合同汇总(不再依赖手工维护的开始 / 结束日期)
|
|
attachProjectContractPeriod(tenantID, list)
|
|
pipelineOk(&c.Controller, map[string]interface{}{
|
|
"list": list, "total": total, "page": page, "pageSize": pageSize,
|
|
})
|
|
}
|
|
|
|
// Detail GET /backend/crm/project/:id
|
|
func (c *BackendCrmProjectController) Detail() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
id, _ := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if id == 0 {
|
|
pipelineErr(&c.Controller, 400, 400, "无效的ID")
|
|
return
|
|
}
|
|
var proj models.TenantCrmProject
|
|
err = models.Orm.QueryTable(new(models.TenantCrmProject)).
|
|
Filter("id", id).Filter("tenant_id", pipelineTenantID(claims)).
|
|
Filter("delete_time__isnull", true).One(&proj)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 404, 404, "项目未找到")
|
|
return
|
|
}
|
|
tmp := []models.TenantCrmProject{proj}
|
|
attachBusinessName(pipelineTenantID(claims), tmp)
|
|
attachProjectContractPeriod(pipelineTenantID(claims), tmp)
|
|
proj.BusinessName = tmp[0].BusinessName
|
|
proj.ContractStartDate = tmp[0].ContractStartDate
|
|
proj.ContractEndDate = tmp[0].ContractEndDate
|
|
// 存量项目兜底:确保文档库「共享文档 / 项目文档」下已建立同名文件夹(失败静默,不影响详情)
|
|
if proj.DocCategoryID == 0 {
|
|
if cid, err := services.EnsureCrmProjectDocCategory(claims.TenantId, proj.ID, proj.ProjectName, 0); err == nil && cid > 0 {
|
|
proj.DocCategoryID = cid
|
|
}
|
|
}
|
|
pipelineOk(&c.Controller, proj)
|
|
}
|
|
|
|
// Create POST /backend/crm/project
|
|
func (c *BackendCrmProjectController) Create() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
raw, _ := io.ReadAll(c.Ctx.Request.Body)
|
|
var p projectPayload
|
|
if err := json.Unmarshal(raw, &p); err != nil {
|
|
pipelineErr(&c.Controller, 400, 400, "参数错误")
|
|
return
|
|
}
|
|
if strings.TrimSpace(p.ProjectName) == "" {
|
|
pipelineErr(&c.Controller, 400, 400, "项目名称不能为空")
|
|
return
|
|
}
|
|
|
|
tenantID := pipelineTenantID(claims)
|
|
var custIDVal *uint64
|
|
customerName := strings.TrimSpace(p.CustomerName)
|
|
if p.CustomerID > 0 {
|
|
var cust models.TenantCrmCustomer
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmCustomer)).
|
|
Filter("id", p.CustomerID).Filter("tenant_id", tenantID).
|
|
Filter("delete_time__isnull", true).One(&cust); err == nil {
|
|
cid := cust.ID
|
|
custIDVal = &cid
|
|
customerName = cust.CustomerName
|
|
}
|
|
}
|
|
status := p.Status
|
|
if status == 0 {
|
|
status = 1
|
|
}
|
|
// 项目类型 / 上下游金额 / 上下游对接人归一(单一项目自动清空下游信息)
|
|
pt, upstream, downstream, upContact, upPhone, downContact, downPhone := normalizeProjectFields(&p)
|
|
supplierID, supplierRefType, supplierName := resolveProjectSupplier(tenantID, p.SupplierRefType, p.SupplierID, p.SupplierName)
|
|
if pt == 1 {
|
|
supplierID, supplierRefType, supplierName = nil, 0, ""
|
|
}
|
|
now := time.Now()
|
|
proj := models.TenantCrmProject{
|
|
TenantID: tenantID,
|
|
ProjectName: strings.TrimSpace(p.ProjectName),
|
|
ProjectNo: strings.TrimSpace(p.ProjectNo),
|
|
ProjectType: pt,
|
|
CustomerID: custIDVal,
|
|
CustomerName: customerName,
|
|
SupplierID: supplierID,
|
|
SupplierRefType: supplierRefType,
|
|
SupplierName: supplierName,
|
|
SupplierIndustry: strings.TrimSpace(p.SupplierIndustry),
|
|
OwnerUserID: firstNonEmpty(p.OwnerUserID, pipelineUID(claims)),
|
|
OwnerUserName: firstNonEmpty(p.OwnerUserName, resolveUserName(claims)),
|
|
Industry: strings.TrimSpace(p.Industry),
|
|
UpstreamAmount: upstream,
|
|
DownstreamAmount: downstream,
|
|
Amount: upstream, // 兼容旧字段:始终等于上游金额
|
|
Status: status,
|
|
StartDate: parsePipelineDate(p.StartDate),
|
|
EndDate: parsePipelineDate(p.EndDate),
|
|
UpstreamContact: upContact,
|
|
UpstreamPhone: upPhone,
|
|
DownstreamContact: downContact,
|
|
DownstreamPhone: downPhone,
|
|
ContactPerson: upContact, // 旧字段同步为上游对接人,便于历史查询
|
|
ContactPhone: upPhone,
|
|
Address: strings.TrimSpace(p.Address),
|
|
Remark: p.Remark,
|
|
Products: p.Products,
|
|
CreateUserID: pipelineUID(claims),
|
|
CreateTime: now,
|
|
UpdateTime: now,
|
|
}
|
|
// 唯一性校验:同租户内不允许重复创建同名项目(重复数据请联系管理员转移跟进)
|
|
if crmNameDuplicated(new(models.TenantCrmProject), tenantID, "project_name", proj.ProjectName) {
|
|
pipelineErr(&c.Controller, 409, 409, "该项目名称已存在(可能由其他同事创建),如需继续跟进请联系管理员转移")
|
|
return
|
|
}
|
|
id, err := models.Orm.Insert(&proj)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 500, 500, "创建失败: "+err.Error())
|
|
return
|
|
}
|
|
// 项目生成时,把产品清单里的产品参数写入产品管理(按 名称+分类 去重)
|
|
if strings.TrimSpace(p.Products) != "" {
|
|
var items []crmProductItem
|
|
if json.Unmarshal([]byte(p.Products), &items) == nil {
|
|
if _, _, serr := SyncProjectProducts(tenantID, uint64(id), proj.ProjectName, items); serr != nil {
|
|
// 同步失败不影响项目创建,仅记录
|
|
_ = serr
|
|
}
|
|
}
|
|
}
|
|
// 项目建立后,同步在文档库「共享文档 / 项目文档」下建立同名文件夹(失败不影响项目创建)
|
|
if cid, err := services.EnsureCrmProjectDocCategory(claims.TenantId, uint64(id), proj.ProjectName, 0); err == nil {
|
|
proj.DocCategoryID = cid
|
|
}
|
|
crmWriteLog(tenantID, 3, uint64(id), "create", "创建项目:"+proj.ProjectName, claims)
|
|
pipelineOk(&c.Controller, map[string]interface{}{"id": id, "doc_category_id": proj.DocCategoryID})
|
|
}
|
|
|
|
// Update PUT /backend/crm/project/:id
|
|
func (c *BackendCrmProjectController) Update() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
id, _ := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if id == 0 {
|
|
pipelineErr(&c.Controller, 400, 400, "无效的ID")
|
|
return
|
|
}
|
|
raw, _ := io.ReadAll(c.Ctx.Request.Body)
|
|
var p projectPayload
|
|
if err := json.Unmarshal(raw, &p); err != nil {
|
|
pipelineErr(&c.Controller, 400, 400, "参数错误")
|
|
return
|
|
}
|
|
tenantID := pipelineTenantID(claims)
|
|
var proj models.TenantCrmProject
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmProject)).
|
|
Filter("id", id).Filter("tenant_id", tenantID).
|
|
Filter("delete_time__isnull", true).One(&proj); err != nil {
|
|
pipelineErr(&c.Controller, 404, 404, "项目未找到")
|
|
return
|
|
}
|
|
// 数据范围校验:非管理员仅能操作本人(负责人或创建人)的数据
|
|
if !canAccessCrmRecord(claims, proj.OwnerUserID, proj.CreateUserID) {
|
|
pipelineErr(&c.Controller, 403, 403, "无权操作该项目数据")
|
|
return
|
|
}
|
|
if strings.TrimSpace(p.ProjectName) == "" {
|
|
pipelineErr(&c.Controller, 400, 400, "项目名称不能为空")
|
|
return
|
|
}
|
|
|
|
proj.ProjectName = strings.TrimSpace(p.ProjectName)
|
|
proj.ProjectNo = strings.TrimSpace(p.ProjectNo)
|
|
if p.CustomerID > 0 {
|
|
var cust models.TenantCrmCustomer
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmCustomer)).
|
|
Filter("id", p.CustomerID).Filter("tenant_id", tenantID).
|
|
Filter("delete_time__isnull", true).One(&cust); err == nil {
|
|
cid := cust.ID
|
|
proj.CustomerID = &cid
|
|
proj.CustomerName = cust.CustomerName
|
|
}
|
|
} else if strings.TrimSpace(p.CustomerName) != "" {
|
|
proj.CustomerName = strings.TrimSpace(p.CustomerName)
|
|
}
|
|
if strings.TrimSpace(p.OwnerUserID) != "" {
|
|
proj.OwnerUserID = strings.TrimSpace(p.OwnerUserID)
|
|
}
|
|
if strings.TrimSpace(p.OwnerUserName) != "" {
|
|
proj.OwnerUserName = strings.TrimSpace(p.OwnerUserName)
|
|
}
|
|
proj.Industry = strings.TrimSpace(p.Industry)
|
|
// 项目类型 / 上下游金额 / 上下游对接人(单一项目自动清空下游信息)
|
|
pt, upstream, downstream, upContact, upPhone, downContact, downPhone := normalizeProjectFields(&p)
|
|
supplierID, supplierRefType, supplierName := resolveProjectSupplier(tenantID, p.SupplierRefType, p.SupplierID, p.SupplierName)
|
|
if pt == 1 {
|
|
supplierID, supplierRefType, supplierName = nil, 0, ""
|
|
}
|
|
proj.ProjectType = pt
|
|
proj.SupplierID = supplierID
|
|
proj.SupplierRefType = supplierRefType
|
|
proj.SupplierName = supplierName
|
|
proj.SupplierIndustry = strings.TrimSpace(p.SupplierIndustry)
|
|
proj.UpstreamAmount = upstream
|
|
proj.DownstreamAmount = downstream
|
|
proj.Amount = upstream // 兼容旧字段:始终等于上游金额
|
|
proj.UpstreamContact = upContact
|
|
proj.UpstreamPhone = upPhone
|
|
proj.DownstreamContact = downContact
|
|
proj.DownstreamPhone = downPhone
|
|
proj.ContactPerson = upContact
|
|
proj.ContactPhone = upPhone
|
|
if p.Status != 0 {
|
|
proj.Status = p.Status
|
|
}
|
|
proj.StartDate = parsePipelineDate(p.StartDate)
|
|
proj.EndDate = parsePipelineDate(p.EndDate)
|
|
proj.Address = strings.TrimSpace(p.Address)
|
|
proj.Remark = p.Remark
|
|
proj.Products = p.Products
|
|
proj.UpdateTime = time.Now()
|
|
|
|
if _, err := models.Orm.Update(&proj); err != nil {
|
|
pipelineErr(&c.Controller, 500, 500, "更新失败: "+err.Error())
|
|
return
|
|
}
|
|
// 项目保存时,把产品清单里的产品参数写入产品管理(按 名称+分类 去重)
|
|
if strings.TrimSpace(p.Products) != "" {
|
|
var items []crmProductItem
|
|
if json.Unmarshal([]byte(p.Products), &items) == nil {
|
|
_, _, _ = SyncProjectProducts(tenantID, proj.ID, proj.ProjectName, items)
|
|
}
|
|
}
|
|
// 项目改名时同步重命名文档库中的项目文件夹(失败静默,不影响项目更新)
|
|
if proj.DocCategoryID > 0 {
|
|
_ = services.RenameCrmProjectDocCategory(claims.TenantId, proj.DocCategoryID, proj.ProjectName)
|
|
}
|
|
crmWriteLog(tenantID, 3, proj.ID, "update", "更新项目:"+proj.ProjectName, claims)
|
|
pipelineOk(&c.Controller, map[string]interface{}{"id": proj.ID})
|
|
}
|
|
|
|
// Delete DELETE /backend/crm/project/:id
|
|
func (c *BackendCrmProjectController) Delete() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
id, _ := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if id == 0 {
|
|
pipelineErr(&c.Controller, 400, 400, "无效的ID")
|
|
return
|
|
}
|
|
tenantID := pipelineTenantID(claims)
|
|
var proj models.TenantCrmProject
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmProject)).
|
|
Filter("id", id).Filter("tenant_id", tenantID).
|
|
Filter("delete_time__isnull", true).One(&proj); err != nil {
|
|
pipelineErr(&c.Controller, 404, 404, "项目未找到")
|
|
return
|
|
}
|
|
// 数据范围校验:非管理员仅能操作本人(负责人或创建人)的数据
|
|
if !canAccessCrmRecord(claims, proj.OwnerUserID, proj.CreateUserID) {
|
|
pipelineErr(&c.Controller, 403, 403, "无权操作该项目数据")
|
|
return
|
|
}
|
|
if !canDeleteCrmRecord(claims, proj.CreateUserID) {
|
|
pipelineErr(&c.Controller, 403, 403, "只有创建人、租户管理员或平台管理员可以删除该项目")
|
|
return
|
|
}
|
|
now := time.Now()
|
|
_, err = models.Orm.QueryTable(new(models.TenantCrmProject)).
|
|
Filter("id", id).Filter("tenant_id", tenantID).
|
|
Update(map[string]interface{}{"delete_time": now, "update_time": now})
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 500, 500, "删除失败: "+err.Error())
|
|
return
|
|
}
|
|
pipelineOk(&c.Controller, nil)
|
|
}
|
|
|
|
// ========================== 项目文档(OA 文档库绑定) ==========================
|
|
|
|
// projectDocItem 项目文档条目:文档库文档 + 所在文件夹名称。
|
|
type projectDocItem struct {
|
|
models.OaDoc
|
|
FolderName string `json:"folder_name"`
|
|
}
|
|
|
|
// projectDocContext 解析项目并确保其在文档库「共享文档 / 项目文档」下的同名文件夹存在。
|
|
// 存量项目(doc_category_id=0)在首次访问项目文档时自动补建,保证历史项目也能在文档库中看到。
|
|
// 失败时已写出错误响应,返回 ok=false。
|
|
func (c *BackendCrmProjectController) projectDocContext(claims *jwtutil.Claims) (*models.TenantCrmProject, uint64, bool) {
|
|
id, _ := strconv.ParseUint(c.Ctx.Input.Param(":id"), 10, 64)
|
|
if id == 0 {
|
|
pipelineErr(&c.Controller, 400, 400, "无效的ID")
|
|
return nil, 0, false
|
|
}
|
|
var proj models.TenantCrmProject
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmProject)).
|
|
Filter("id", id).Filter("tenant_id", pipelineTenantID(claims)).
|
|
Filter("delete_time__isnull", true).One(&proj); err != nil {
|
|
pipelineErr(&c.Controller, 404, 404, "项目未找到")
|
|
return nil, 0, false
|
|
}
|
|
cid, err := services.EnsureCrmProjectDocCategory(claims.TenantId, proj.ID, proj.ProjectName, proj.DocCategoryID)
|
|
if err != nil || cid == 0 {
|
|
pipelineErr(&c.Controller, 500, 500, "项目文档文件夹准备失败")
|
|
return nil, 0, false
|
|
}
|
|
proj.DocCategoryID = cid
|
|
return &proj, cid, true
|
|
}
|
|
|
|
// DocList GET /backend/crm/project/:id/docs
|
|
// 项目文档列表:读取文档库中该项目文件夹(含子文件夹)下的全部文档,与文档库内容实时一致。
|
|
func (c *BackendCrmProjectController) DocList() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
proj, cid, ok := c.projectDocContext(claims)
|
|
if !ok {
|
|
return
|
|
}
|
|
page, _ := c.GetInt("page", 1)
|
|
pageSize, _ := c.GetInt("pageSize", 20)
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if pageSize < 1 || pageSize > 200 {
|
|
pageSize = 20
|
|
}
|
|
actor := services.NewOaDocActor(claims.TenantId, uint64(claims.UserID))
|
|
// 可选按文件夹筛选(含其子文件夹);不属于项目文件夹的参数回落到项目根,避免越权查询
|
|
filterCategory := cid
|
|
if v, _ := c.GetUint64("category_id"); v > 0 && services.CrmProjectDocCategoryInScope(actor, cid, v) {
|
|
filterCategory = v
|
|
}
|
|
list, total, err := services.OaDocList(actor, services.OaDocListParams{
|
|
Actor: actor,
|
|
Scope: models.DocScopeShared,
|
|
Keyword: strings.TrimSpace(c.GetString("keyword")),
|
|
CategoryID: filterCategory,
|
|
Status: -1,
|
|
DocType: -1,
|
|
Star: -1,
|
|
Page: page,
|
|
PageSize: pageSize,
|
|
})
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 500, 500, "查询失败: "+err.Error())
|
|
return
|
|
}
|
|
// 附带所在文件夹名称(项目文件夹本身或其中子分类)
|
|
nameMap := services.CrmProjectDocCategoryNameMap(claims.TenantId)
|
|
items := make([]projectDocItem, 0, len(list))
|
|
for _, d := range list {
|
|
folder := nameMap[d.CategoryID]
|
|
if folder == "" {
|
|
folder = proj.ProjectName
|
|
}
|
|
items = append(items, projectDocItem{OaDoc: d, FolderName: folder})
|
|
}
|
|
// 项目文件夹树(根为项目文件夹),供前端选择/新建子文件夹
|
|
folders, err := services.CrmProjectDocFolderTree(actor, cid)
|
|
if err != nil {
|
|
folders = []*services.ProjectDocFolder{}
|
|
}
|
|
pipelineOk(&c.Controller, map[string]interface{}{
|
|
"category_id": cid,
|
|
"category_name": proj.ProjectName,
|
|
"folders": folders,
|
|
"list": items,
|
|
"total": total,
|
|
"page": page,
|
|
"pageSize": pageSize,
|
|
})
|
|
}
|
|
|
|
// DocUpload POST /backend/crm/project/:id/doc-upload
|
|
// 上传项目文档:文件先经 POST /backend/uploadfile 上传,再调用本接口把元数据写入文档库项目文件夹,
|
|
// 同时同步登记一条 CRM 附件,保证项目「附件」链路同样可见。
|
|
func (c *BackendCrmProjectController) DocUpload() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
proj, cid, ok := c.projectDocContext(claims)
|
|
if !ok {
|
|
return
|
|
}
|
|
var p struct {
|
|
FileID uint64 `json:"file_id"`
|
|
FileName string `json:"file_name"`
|
|
FileURL string `json:"file_url"`
|
|
FileSize int64 `json:"file_size"`
|
|
Ext string `json:"ext"`
|
|
Title string `json:"title"`
|
|
Summary string `json:"summary"`
|
|
CategoryID uint64 `json:"category_id"` // 目标文件夹,0=项目根文件夹
|
|
}
|
|
raw, _ := io.ReadAll(c.Ctx.Request.Body)
|
|
if err := json.Unmarshal(raw, &p); err != nil {
|
|
pipelineErr(&c.Controller, 400, 400, "参数错误")
|
|
return
|
|
}
|
|
fileName := strings.TrimSpace(p.FileName)
|
|
fileURL := strings.TrimSpace(p.FileURL)
|
|
if fileName == "" || fileURL == "" {
|
|
pipelineErr(&c.Controller, 400, 400, "文件信息不完整")
|
|
return
|
|
}
|
|
ext := strings.ToLower(strings.TrimPrefix(strings.TrimSpace(p.Ext), "."))
|
|
if ext == "" {
|
|
ext = strings.ToLower(strings.TrimPrefix(filepath.Ext(fileName), "."))
|
|
}
|
|
var size uint64
|
|
if p.FileSize > 0 {
|
|
size = uint64(p.FileSize)
|
|
}
|
|
operatorName := resolveUserName(claims)
|
|
actor := services.NewOaDocActor(claims.TenantId, uint64(claims.UserID))
|
|
// 目标文件夹:默认项目根文件夹;指定时必须在项目文件夹内
|
|
targetCategory := cid
|
|
if p.CategoryID > 0 {
|
|
if !services.CrmProjectDocCategoryInScope(actor, cid, p.CategoryID) {
|
|
pipelineErr(&c.Controller, 403, 403, "只能上传到项目自己的文件夹内")
|
|
return
|
|
}
|
|
targetCategory = p.CategoryID
|
|
}
|
|
doc, err := services.OaDocCreate(actor, operatorName, services.OaDocSaveParams{
|
|
SetCategory: true,
|
|
CategoryID: targetCategory,
|
|
SetVisibility: true,
|
|
Visibility: models.DocVisibilityPublic,
|
|
Title: strings.TrimSpace(p.Title),
|
|
FileID: p.FileID,
|
|
FileURL: fileURL,
|
|
FileName: fileName,
|
|
Ext: ext,
|
|
Size: size,
|
|
Summary: strings.TrimSpace(p.Summary),
|
|
Status: models.DocStatusPublish,
|
|
OwnerID: uint64(claims.UserID),
|
|
OwnerName: operatorName,
|
|
})
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 400, 400, "同步文档库失败: "+err.Error())
|
|
return
|
|
}
|
|
// 同步登记 CRM 附件(失败不影响文档库结果)
|
|
now := time.Now()
|
|
var fileIDPtr *uint64
|
|
if doc.FileID > 0 {
|
|
fid := doc.FileID
|
|
fileIDPtr = &fid
|
|
}
|
|
_, _ = models.Orm.Insert(&models.TenantCrmAttach{
|
|
TenantID: pipelineTenantID(claims),
|
|
RelatedType: 3,
|
|
RelatedID: proj.ID,
|
|
FileID: fileIDPtr,
|
|
FileName: doc.FileName,
|
|
FileURL: doc.FileURL,
|
|
FileSize: int64(doc.Size),
|
|
UploaderID: pipelineUID(claims),
|
|
UploaderName: operatorName,
|
|
CreateTime: now,
|
|
UpdateTime: now,
|
|
})
|
|
crmWriteLog(pipelineTenantID(claims), 3, proj.ID, "doc", "上传项目文档:"+doc.Title, claims)
|
|
pipelineOk(&c.Controller, doc)
|
|
}
|
|
|
|
// DocDelete POST /backend/crm/project/:id/doc-delete
|
|
// 删除项目文档:同步软删文档库文档与对应的 CRM 附件记录。
|
|
func (c *BackendCrmProjectController) DocDelete() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
proj, cid, ok := c.projectDocContext(claims)
|
|
if !ok {
|
|
return
|
|
}
|
|
var p struct {
|
|
DocID uint64 `json:"doc_id"`
|
|
}
|
|
raw, _ := io.ReadAll(c.Ctx.Request.Body)
|
|
if err := json.Unmarshal(raw, &p); err != nil || p.DocID == 0 {
|
|
pipelineErr(&c.Controller, 400, 400, "参数错误")
|
|
return
|
|
}
|
|
actor := services.NewOaDocActor(claims.TenantId, uint64(claims.UserID))
|
|
doc, err := services.OaDocGet(actor, p.DocID)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 404, 404, "文档不存在")
|
|
return
|
|
}
|
|
// 仅允许删除本项目文件夹(含子文件夹)内的文档
|
|
if !services.CrmProjectDocCategoryInScope(actor, cid, doc.CategoryID) {
|
|
pipelineErr(&c.Controller, 403, 403, "该文档不属于当前项目")
|
|
return
|
|
}
|
|
if _, err := services.OaDocDelete(actor, []uint64{doc.ID}); err != nil {
|
|
pipelineErr(&c.Controller, 400, 400, err.Error())
|
|
return
|
|
}
|
|
// 同步软删 CRM 附件记录(按文件ID匹配,缺失时按文件名+地址匹配)
|
|
now := time.Now()
|
|
q := models.Orm.QueryTable(new(models.TenantCrmAttach)).
|
|
Filter("tenant_id", pipelineTenantID(claims)).
|
|
Filter("related_type", 3).
|
|
Filter("related_id", proj.ID).
|
|
Filter("delete_time__isnull", true)
|
|
if doc.FileID > 0 {
|
|
q = q.Filter("file_id", doc.FileID)
|
|
} else {
|
|
q = q.Filter("file_name", doc.FileName).Filter("file_url", doc.FileURL)
|
|
}
|
|
_, _ = q.Update(map[string]interface{}{"delete_time": now, "update_time": now})
|
|
crmWriteLog(pipelineTenantID(claims), 3, proj.ID, "doc", "删除项目文档:"+doc.Title, claims)
|
|
pipelineOk(&c.Controller, nil)
|
|
}
|
|
|
|
// DocFolderCreate POST /backend/crm/project/:id/doc-folder
|
|
// 在项目文件夹下新建子文件夹(对应文档库中的文档分类),用于分类存放项目文档。
|
|
// parent_id 缺省时建在项目根文件夹下;只允许在项目自己的文件夹树内创建。
|
|
func (c *BackendCrmProjectController) DocFolderCreate() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
proj, cid, ok := c.projectDocContext(claims)
|
|
if !ok {
|
|
return
|
|
}
|
|
var p struct {
|
|
Name string `json:"name"`
|
|
ParentID uint64 `json:"parent_id"`
|
|
}
|
|
raw, _ := io.ReadAll(c.Ctx.Request.Body)
|
|
if err := json.Unmarshal(raw, &p); err != nil {
|
|
pipelineErr(&c.Controller, 400, 400, "参数错误")
|
|
return
|
|
}
|
|
name := strings.TrimSpace(p.Name)
|
|
if name == "" {
|
|
pipelineErr(&c.Controller, 400, 400, "请输入文件夹名称")
|
|
return
|
|
}
|
|
parentID := p.ParentID
|
|
if parentID == 0 {
|
|
parentID = cid
|
|
}
|
|
actor := services.NewOaDocActor(claims.TenantId, uint64(claims.UserID))
|
|
if !services.CrmProjectDocCategoryInScope(actor, cid, parentID) {
|
|
pipelineErr(&c.Controller, 403, 403, "只能在项目文件夹内创建子文件夹")
|
|
return
|
|
}
|
|
item, err := services.OaDocCategoryCreate(actor, models.DocScopeShared, parentID, name, "项目文档:"+proj.ProjectName, 0)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 400, 400, err.Error())
|
|
return
|
|
}
|
|
crmWriteLog(pipelineTenantID(claims), 3, proj.ID, "doc-folder", "新建项目文档文件夹:"+item.Name, claims)
|
|
pipelineOk(&c.Controller, item)
|
|
}
|
|
|
|
// DocFolderDelete POST /backend/crm/project/:id/doc-folder-delete
|
|
// 删除项目内的子文件夹(需为空:无子文件夹、无文档);项目根文件夹不可删除。
|
|
func (c *BackendCrmProjectController) DocFolderDelete() {
|
|
claims, err := pipelineClaims(&c.Controller)
|
|
if err != nil {
|
|
pipelineErr(&c.Controller, 401, 401, err.Error())
|
|
return
|
|
}
|
|
proj, cid, ok := c.projectDocContext(claims)
|
|
if !ok {
|
|
return
|
|
}
|
|
var p struct {
|
|
FolderID uint64 `json:"folder_id"`
|
|
}
|
|
raw, _ := io.ReadAll(c.Ctx.Request.Body)
|
|
if err := json.Unmarshal(raw, &p); err != nil || p.FolderID == 0 {
|
|
pipelineErr(&c.Controller, 400, 400, "参数错误")
|
|
return
|
|
}
|
|
if p.FolderID == cid {
|
|
pipelineErr(&c.Controller, 400, 400, "项目根文件夹不可删除")
|
|
return
|
|
}
|
|
actor := services.NewOaDocActor(claims.TenantId, uint64(claims.UserID))
|
|
if !services.CrmProjectDocCategoryInScope(actor, cid, p.FolderID) {
|
|
pipelineErr(&c.Controller, 403, 403, "该文件夹不属于当前项目")
|
|
return
|
|
}
|
|
var folder models.OaDocCategory
|
|
if err := models.Orm.QueryTable(new(models.OaDocCategory)).
|
|
Filter("tid", claims.TenantId).Filter("id", p.FolderID).
|
|
Filter("is_deleted", 0).One(&folder); err != nil {
|
|
pipelineErr(&c.Controller, 404, 404, "文件夹不存在")
|
|
return
|
|
}
|
|
if err := services.OaDocCategoryDelete(actor, p.FolderID); err != nil {
|
|
pipelineErr(&c.Controller, 400, 400, err.Error())
|
|
return
|
|
}
|
|
crmWriteLog(pipelineTenantID(claims), 3, proj.ID, "doc-folder", "删除项目文档文件夹:"+folder.Name, claims)
|
|
pipelineOk(&c.Controller, nil)
|
|
}
|