فهرست منبع

feature: 添加公海客户领取审批

liuyaqi 3 سال پیش
والد
کامیت
dd75a77009

+ 1 - 1
opms_parent/app/handler/cust/customer.go

@@ -176,7 +176,7 @@ func (c *CustomerHeader) AssignCustomer(ctx context.Context, req *model.AssignCu
 	} else {
 		behavior = "领取"
 	}
-	err = s.AssignCustomer(req)
+	err = s.AssignCustomer(ctx, req)
 	if err != nil {
 		return err
 	}

+ 1 - 1
opms_parent/app/model/cust/cust_customer.go

@@ -59,7 +59,7 @@ type CustomerAddSeq struct {
 	FollowUpDate     *gtime.Time `p:"followUpDate"    json:"followUpDate"   `                     //跟进时间
 	CustIndustry     string      `p:"custIndustry"    json:"custIndustry"  v:"required#客户行业不能为空"` //客户行业
 	CustIndustryCode string      `json:"custIndustryCode"`                                        //客户行业编码
-	CustLevel        string      `p:"custLevel"       json:"custLevel"     v:"required#客户级别不能为空"` //客户级别
+	CustLevel        string      `p:"custLevel"       json:"custLevel"` //客户级别
 	CustSource       string      `p:"custSource"      json:"source"        v:"required#客户来源不能为空"` //客户来源
 	CustDistCode     int         `p:"custDistCode"    json:"custDistCode"  v:"required#省份不能为空" `  // 省份Id
 	Remark           string      `p:"remark"          json:"remark"`                              //备注

+ 134 - 15
opms_parent/app/service/cust/cust_customer.go

@@ -9,6 +9,8 @@ import (
 	"strings"
 
 	"dashoo.cn/opms_libary/myerrors"
+	"dashoo.cn/opms_libary/plugin/dingtalk/message"
+	"dashoo.cn/opms_libary/plugin/dingtalk/workflow"
 	"github.com/360EntSecGroup-Skylar/excelize"
 	"github.com/gogf/gf/database/gdb"
 	"github.com/gogf/gf/encoding/gjson"
@@ -20,7 +22,10 @@ import (
 	"dashoo.cn/micro/app/dao/cust"
 	platdao "dashoo.cn/micro/app/dao/plat"
 	model "dashoo.cn/micro/app/model/cust"
+	workflowModel "dashoo.cn/micro/app/model/workflow"
+	sysDao "dashoo.cn/micro/app/dao/sys"
 	"dashoo.cn/micro/app/service"
+	workflowService "dashoo.cn/micro/app/service/workflow"
 )
 
 type CustomerService struct {
@@ -30,6 +35,7 @@ type CustomerService struct {
 	DynamicsDao    *cust.CustCustomerDynamicsDao
 	ContactDao     *cust.CustCustomerContactDao
 	FollowDao      *platdao.PlatFollowupDao
+	UserDao *sysDao.SysUserDao
 	BelongServer   *CustomerbelongService
 	ContanctServer *CustomercontactService
 }
@@ -53,6 +59,7 @@ func NewCustomerService(ctx context.Context) (svc *CustomerService, err error) {
 	svc.FollowDao = platdao.NewPlatFollowupDao(svc.Tenant)
 	svc.BelongServer, _ = NewCustomerBelongService(ctx)
 	svc.ContanctServer, _ = NewCustomerContactService(ctx)
+	svc.UserDao = sysDao.NewSysUserDao(svc.Tenant)
 	return svc, nil
 }
 
@@ -327,8 +334,130 @@ func (s *CustomerService) MoveToPubic(Ids []int64) (err error) {
 	return
 }
 
+var AssignCustomerRequestProcessCode = ""
+
+func ptrString(s string) *string {
+	return &s
+}
+
+// 领取客户申请
+func (s *CustomerService) AssignCustomerRequest(ctx context.Context, req *model.AssignCustomerReq) error {
+	data, err := s.Dao.Where("id in (?)", req.Ids).LockShared().All()
+	if err != nil {
+		return err
+	}
+	if len(data) == 0 {
+		return myerrors.TipsError("领取用户不能为空")
+	}
+
+	for _, v := range data {
+		if v.CustStatus != "10" {
+			return myerrors.TipsError(fmt.Sprintf("客户: %s 已被领取", v.CustName))
+		}
+	}
+
+	workflowSrv, err := workflowService.NewTaskService(ctx)
+	if err != nil {
+		return err
+	}
+	for _, u := range data {
+		bizCode := strconv.Itoa(u.Id) + ":" + strconv.Itoa(s.GetCxtUserId())
+		_, err = workflowSrv.StartProcessInstance(bizCode, "11", &workflow.StartProcessInstanceRequest{
+			ProcessCode: &AssignCustomerRequestProcessCode,
+			FormComponentValues: []*workflow.StartProcessInstanceRequestFormComponentValues{
+				{
+					Name:  ptrString(""),
+					Value: ptrString(""),
+				},
+			},
+		})
+		if err != nil {
+			return err
+		}
+
+		_, err = s.Dao.Where("id = ?", u.Id).Data(map[string]interface{}{
+			"is_public":   noPublic,
+			"cust_status": "20",
+		}).Update()
+		if err != nil {
+			return err
+		}
+	}
+	return nil
+}
+
+func (s *CustomerService) AssignCustomerRequestApproval(flow workflowModel.PlatWorkflow, msg message.MixMessage) error {
+	bizCode := strings.Split(flow.BizCode, ":")
+	if len(bizCode) != 2 {
+		return fmt.Errorf("客户领取审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
+	}
+	custId, err := strconv.Atoi(bizCode[0])
+	if err != nil {
+		return fmt.Errorf("客户领取审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
+	}
+	userId, err := strconv.Atoi(bizCode[1])
+	if err != nil {
+		return fmt.Errorf("客户领取审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
+	}
+	user, err := s.UserDao.Where("id = ?", userId).One()
+	if err != nil {
+		return err
+	}
+	if user == nil {
+		return fmt.Errorf("用户不存在:%s Id: %d", flow.BizCode, flow.Id)
+	}
+	cust, err := s.Dao.Where("id = ?", custId).One()
+	if err != nil {
+		return err
+	}
+	if cust == nil {
+		return fmt.Errorf("客户不存在:%s Id: %d", flow.BizCode, flow.Id)
+	}
+
+	if (msg.ProcessType != "finish" && msg.ProcessType != "terminate") {
+		return fmt.Errorf("无法识别的 ProcessType :%s", msg.ProcessType)
+	}
+	if (msg.Result != "agree" && msg.Result != "refuse") {
+		return fmt.Errorf("无法识别的 Result :%s", msg.Result)
+	}
+
+	if msg.ProcessType == "terminate" {
+		_, err = s.Dao.Where("id = ?", custId).Data(map[string]interface{}{
+			"is_public":   isPublic,
+			"cust_status": "10",
+		}).Update()
+		return err
+	}
+
+	pass := msg.Result == "agree"
+	if !pass {
+		_, err = s.Dao.Where("id = ?", custId).Data(map[string]interface{}{
+			"is_public":   isPublic,
+			"cust_status": "10",
+		}).Update()
+		return err
+	}
+
+	err = s.ChangeCustBelong([]int64{int64(custId)}, int64(userId), user.NickName)
+	if err != nil {
+		return err
+	}
+	s.BatchCreatebelong([]*model.CustCustomer{cust}, &model.AssignCustomerReq{
+		Ids: []int64{int64(custId)},
+		SalesId: int64(user.Id),
+		SalesName: user.NickName,
+		Remark: "",
+		Receive: Receive,
+	})
+	return nil
+}
+
 // AssignCustomer 分配客户
-func (s *CustomerService) AssignCustomer(req *model.AssignCustomerReq) (err error) {
+func (s *CustomerService) AssignCustomer(ctx context.Context, req *model.AssignCustomerReq) (err error) {
+	if req.Receive != "" {
+		return s.AssignCustomerRequest(ctx, req)
+	}
+
 	data, err := s.Dao.Where("id in (?)", req.Ids).LockShared().All()
 	if err != nil {
 		g.Log().Error(err)
@@ -447,6 +576,7 @@ func (s *CustomerService) TransCustomer(req *model.AssignCustomerReq) (err error
 // ChangeCustBelong 变更客户所属关系
 func (s *CustomerService) ChangeCustBelong(Ids []int64, salesId int64, salesName string) (err error) {
 	_, err = s.Dao.Data(g.Map{
+		"cust_status":  "30",
 		"sales_id":     salesId,
 		"is_public":    noPublic,
 		"sales_name":   salesName,
@@ -454,12 +584,7 @@ func (s *CustomerService) ChangeCustBelong(Ids []int64, salesId int64, salesName
 		"updated_name": s.GetCxtUserName(),
 		"updated_time": gtime.Now(),
 	}).Where("id in (?)", Ids).Update()
-
-	if err != nil {
-		g.Log().Error(err)
-		return
-	}
-	return
+	return err
 }
 
 // CreateDynamics 创建客户动态信息
@@ -603,8 +728,7 @@ func (s *CustomerService) BatchCreatebelong(rep []*model.CustCustomer, req *mode
 			"end_date":     gtime.Now(),
 		}).WhereIn(s.BelongDao.Columns.CustId, req.Ids).Update()
 	if err != nil {
-		g.Log().Error(err)
-		return
+		return err
 	}
 	var belongData []*model.CustCustomerBelong
 	userName := s.GetCxtUserName()
@@ -624,12 +748,7 @@ func (s *CustomerService) BatchCreatebelong(rep []*model.CustCustomer, req *mode
 		belongData = append(belongData, belong)
 	}
 	_, err = s.BelongDao.Insert(belongData)
-	if err != nil {
-		g.Log().Error(err)
-		return
-	}
-
-	return
+	return err
 }
 
 // 导出数据