360 lines
9.0 KiB
Go
360 lines
9.0 KiB
Go
package controllers
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"server/models"
|
|
"server/pkg/identity"
|
|
"server/pkg/jwtutil"
|
|
"server/services"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
func resolveUserName(claims *jwtutil.Claims) string {
|
|
if claims == nil {
|
|
return ""
|
|
}
|
|
tid := uint64(claims.TenantId)
|
|
account, name := services.OperationLogUser(&tid, uint64(claims.UserID))
|
|
if strings.TrimSpace(name) != "" {
|
|
return strings.TrimSpace(name)
|
|
}
|
|
if strings.TrimSpace(account) != "" {
|
|
return strings.TrimSpace(account)
|
|
}
|
|
return claims.Username
|
|
}
|
|
|
|
func pipelineClaims(c *beego.Controller) (*jwtutil.Claims, error) {
|
|
return identity.Claims(c.Ctx.Request.Header.Get("Authorization"), identity.EndBackend)
|
|
}
|
|
|
|
func pipelineOk(c *beego.Controller, data interface{}) {
|
|
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success", "data": data}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
func pipelineErr(c *beego.Controller, httpStatus, bizCode int, msg string) {
|
|
c.Ctx.Output.SetStatus(httpStatus)
|
|
c.Data["json"] = map[string]interface{}{"code": bizCode, "msg": msg}
|
|
_ = c.ServeJSON()
|
|
}
|
|
|
|
func pipelineTenantID(claims *jwtutil.Claims) string {
|
|
return fmt.Sprintf("%d", claims.TenantId)
|
|
}
|
|
|
|
func pipelineUID(claims *jwtutil.Claims) string {
|
|
return fmt.Sprintf("%d", claims.UserID)
|
|
}
|
|
|
|
func parsePipelineDateTime(s string) *time.Time {
|
|
s = strings.TrimSpace(s)
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
layouts := []string{
|
|
"2006-01-02 15:04:05",
|
|
time.RFC3339Nano,
|
|
time.RFC3339,
|
|
"2006-01-02T15:04:05",
|
|
"2006-01-02T15:04",
|
|
"2006-01-02 15:04",
|
|
"2006-01-02",
|
|
}
|
|
for _, layout := range layouts {
|
|
if t, err := time.ParseInLocation(layout, s, time.Local); err == nil {
|
|
return &t
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func parsePipelineDate(s string) *time.Time {
|
|
s = strings.TrimSpace(s)
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
if t, err := time.ParseInLocation("2006-01-02", s, time.Local); err == nil {
|
|
return &t
|
|
}
|
|
return parsePipelineDateTime(s)
|
|
}
|
|
|
|
func crmWriteLog(tenantID string, relatedType int8, relatedID uint64, action, content string, claims *jwtutil.Claims) {
|
|
if models.Orm == nil || relatedID == 0 {
|
|
return
|
|
}
|
|
operatorID, operatorName := "", ""
|
|
if claims != nil {
|
|
operatorID = pipelineUID(claims)
|
|
operatorName = resolveUserName(claims)
|
|
}
|
|
log := models.TenantCrmOperateLog{
|
|
TenantID: tenantID,
|
|
RelatedType: relatedType,
|
|
RelatedID: relatedID,
|
|
Action: action,
|
|
Content: content,
|
|
OperatorID: operatorID,
|
|
OperatorName: operatorName,
|
|
CreateTime: time.Now(),
|
|
}
|
|
_, _ = models.Orm.Insert(&log)
|
|
}
|
|
|
|
func relatedTypeText(t int8) string {
|
|
switch t {
|
|
case 1:
|
|
return "线索"
|
|
case 2:
|
|
return "商机"
|
|
case 3:
|
|
return "项目"
|
|
default:
|
|
return "未知"
|
|
}
|
|
}
|
|
|
|
func mobileInMobiles(mobile, mobiles string) bool {
|
|
mobile = strings.TrimSpace(mobile)
|
|
if mobile == "" || strings.TrimSpace(mobiles) == "" {
|
|
return false
|
|
}
|
|
if firstMobile(mobiles) == mobile {
|
|
return true
|
|
}
|
|
return strings.Contains(mobiles, mobile)
|
|
}
|
|
|
|
func entityContactMobiles(src models.TenantCrmEntityContact) string {
|
|
if s := strings.TrimSpace(src.Mobiles); s != "" {
|
|
return s
|
|
}
|
|
return buildMobiles(src.Mobile)
|
|
}
|
|
|
|
func mobilesMatched(a, b string) bool {
|
|
for _, x := range splitMobiles(a) {
|
|
if x == "" {
|
|
continue
|
|
}
|
|
for _, y := range splitMobiles(b) {
|
|
if x == y {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func mergeCompanyContact(dst *models.ErpCompanyContact, src models.TenantCrmEntityContact) {
|
|
changed := false
|
|
if dst.Wechat == "" && strings.TrimSpace(src.Wechat) != "" {
|
|
dst.Wechat = strings.TrimSpace(src.Wechat)
|
|
changed = true
|
|
}
|
|
if dst.QQ == "" && strings.TrimSpace(src.QQ) != "" {
|
|
dst.QQ = strings.TrimSpace(src.QQ)
|
|
changed = true
|
|
}
|
|
if dst.Email == "" && strings.TrimSpace(src.Email) != "" {
|
|
dst.Email = strings.TrimSpace(src.Email)
|
|
changed = true
|
|
}
|
|
if dst.Position == "" && strings.TrimSpace(src.Position) != "" {
|
|
dst.Position = strings.TrimSpace(src.Position)
|
|
changed = true
|
|
}
|
|
if strings.TrimSpace(dst.Mobiles) == "" {
|
|
if m := entityContactMobiles(src); m != "" {
|
|
dst.Mobiles = m
|
|
changed = true
|
|
}
|
|
}
|
|
if dst.Remark == "" && strings.TrimSpace(src.Remark) != "" {
|
|
dst.Remark = strings.TrimSpace(src.Remark)
|
|
changed = true
|
|
}
|
|
if src.IsPrimary == 1 && dst.IsPrimary != 1 {
|
|
dst.IsPrimary = 1
|
|
changed = true
|
|
}
|
|
if changed {
|
|
dst.UpdateTime = time.Now()
|
|
_, _ = models.Orm.Update(dst)
|
|
}
|
|
}
|
|
|
|
func syncContactsToCompany(tenantID string, customerID uint64, contacts []models.TenantCrmEntityContact) (int, int) {
|
|
if models.Orm == nil || customerID == 0 || len(contacts) == 0 {
|
|
return 0, 0
|
|
}
|
|
inserted, merged := 0, 0
|
|
seen := map[string]bool{}
|
|
for _, src := range contacts {
|
|
name := strings.TrimSpace(src.ContactName)
|
|
if name == "" {
|
|
continue
|
|
}
|
|
mobiles := entityContactMobiles(src)
|
|
mobile := firstMobile(mobiles)
|
|
key := name + "|" + mobile
|
|
if seen[key] {
|
|
continue
|
|
}
|
|
seen[key] = true
|
|
|
|
var existingList []models.ErpCompanyContact
|
|
_, _ = models.Orm.QueryTable(new(models.ErpCompanyContact)).
|
|
Filter("tenant_id", tenantID).
|
|
Filter("company_type", "customer").
|
|
Filter("company_id", customerID).
|
|
Filter("name", name).
|
|
Filter("delete_time__isnull", true).
|
|
All(&existingList)
|
|
|
|
matched := false
|
|
for i := range existingList {
|
|
if mobile == "" || mobilesMatched(mobiles, existingList[i].Mobiles) {
|
|
mergeCompanyContact(&existingList[i], src)
|
|
merged++
|
|
matched = true
|
|
break
|
|
}
|
|
}
|
|
if matched {
|
|
continue
|
|
}
|
|
|
|
row := models.ErpCompanyContact{
|
|
TenantID: tenantID,
|
|
CompanyType: "customer",
|
|
CompanyID: customerID,
|
|
Name: name,
|
|
Phone: "",
|
|
Mobiles: mobiles,
|
|
Wechat: strings.TrimSpace(src.Wechat),
|
|
QQ: strings.TrimSpace(src.QQ),
|
|
Email: strings.TrimSpace(src.Email),
|
|
Position: strings.TrimSpace(src.Position),
|
|
IsPrimary: src.IsPrimary,
|
|
Status: 1,
|
|
Remark: strings.TrimSpace(src.Remark),
|
|
CreateTime: time.Now(),
|
|
UpdateTime: time.Now(),
|
|
}
|
|
if _, err := models.Orm.Insert(&row); err == nil {
|
|
inserted++
|
|
}
|
|
}
|
|
return inserted, merged
|
|
}
|
|
|
|
type relatedRef struct {
|
|
Type int8
|
|
ID uint64
|
|
}
|
|
|
|
func relatedChain(tenantID string, relatedType int8, relatedID uint64) []relatedRef {
|
|
if models.Orm == nil || relatedID == 0 {
|
|
return []relatedRef{{Type: relatedType, ID: relatedID}}
|
|
}
|
|
refs := make([]relatedRef, 0, 3)
|
|
switch relatedType {
|
|
case 2:
|
|
var biz models.TenantCrmBusiness
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmBusiness)).
|
|
Filter("id", relatedID).Filter("tenant_id", tenantID).
|
|
Filter("delete_time__isnull", true).One(&biz); err == nil {
|
|
if biz.ClueID != nil && *biz.ClueID > 0 {
|
|
refs = append(refs, relatedRef{Type: 1, ID: *biz.ClueID})
|
|
}
|
|
}
|
|
refs = append(refs, relatedRef{Type: 2, ID: relatedID})
|
|
case 3:
|
|
var proj models.TenantCrmProject
|
|
if err := models.Orm.QueryTable(new(models.TenantCrmProject)).
|
|
Filter("id", relatedID).Filter("tenant_id", tenantID).
|
|
Filter("delete_time__isnull", true).One(&proj); err == nil {
|
|
if proj.BusinessID != nil && *proj.BusinessID > 0 {
|
|
var biz models.TenantCrmBusiness
|
|
if err2 := models.Orm.QueryTable(new(models.TenantCrmBusiness)).
|
|
Filter("id", *proj.BusinessID).Filter("tenant_id", tenantID).
|
|
Filter("delete_time__isnull", true).One(&biz); err2 == nil {
|
|
if biz.ClueID != nil && *biz.ClueID > 0 {
|
|
refs = append(refs, relatedRef{Type: 1, ID: *biz.ClueID})
|
|
}
|
|
refs = append(refs, relatedRef{Type: 2, ID: biz.ID})
|
|
}
|
|
}
|
|
}
|
|
refs = append(refs, relatedRef{Type: 3, ID: relatedID})
|
|
default:
|
|
refs = append(refs, relatedRef{Type: relatedType, ID: relatedID})
|
|
}
|
|
return refs
|
|
}
|
|
|
|
func attachBusinessName(tenantID string, list []models.TenantCrmProject) {
|
|
ids := make([]uint64, 0, len(list))
|
|
seen := map[uint64]bool{}
|
|
for _, p := range list {
|
|
if p.BusinessID != nil && *p.BusinessID > 0 && !seen[*p.BusinessID] {
|
|
seen[*p.BusinessID] = true
|
|
ids = append(ids, *p.BusinessID)
|
|
}
|
|
}
|
|
if len(ids) == 0 {
|
|
return
|
|
}
|
|
var rows []models.TenantCrmBusiness
|
|
_, _ = models.Orm.QueryTable(new(models.TenantCrmBusiness)).
|
|
Filter("tenant_id", tenantID).
|
|
Filter("id__in", ids).
|
|
Filter("delete_time__isnull", true).
|
|
All(&rows)
|
|
nameMap := make(map[uint64]string, len(rows))
|
|
for _, r := range rows {
|
|
nameMap[r.ID] = r.BusinessName
|
|
}
|
|
for i := range list {
|
|
if list[i].BusinessID != nil {
|
|
list[i].BusinessName = nameMap[*list[i].BusinessID]
|
|
}
|
|
}
|
|
}
|
|
|
|
func attachClueName(tenantID string, list []models.TenantCrmBusiness) {
|
|
ids := make([]uint64, 0, len(list))
|
|
seen := map[uint64]bool{}
|
|
for _, b := range list {
|
|
if b.ClueID != nil && *b.ClueID > 0 && !seen[*b.ClueID] {
|
|
seen[*b.ClueID] = true
|
|
ids = append(ids, *b.ClueID)
|
|
}
|
|
}
|
|
if len(ids) == 0 {
|
|
return
|
|
}
|
|
var rows []models.TenantCrmClue
|
|
_, _ = models.Orm.QueryTable(new(models.TenantCrmClue)).
|
|
Filter("tenant_id", tenantID).
|
|
Filter("id__in", ids).
|
|
Filter("delete_time__isnull", true).
|
|
All(&rows)
|
|
nameMap := make(map[uint64]string, len(rows))
|
|
for _, r := range rows {
|
|
nameMap[r.ID] = r.ClueName
|
|
}
|
|
for i := range list {
|
|
if list[i].ClueID != nil {
|
|
list[i].ClueName = nameMap[*list[i].ClueID]
|
|
}
|
|
}
|
|
}
|
|
|