|
|
@@ -3,12 +3,12 @@ package cust
|
|
|
import (
|
|
|
"bytes"
|
|
|
"context"
|
|
|
+ "fmt"
|
|
|
"math"
|
|
|
"strconv"
|
|
|
|
|
|
"dashoo.cn/opms_libary/myerrors"
|
|
|
"github.com/360EntSecGroup-Skylar/excelize"
|
|
|
- "github.com/gogf/gf/errors/gerror"
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
"github.com/gogf/gf/os/gtime"
|
|
|
"github.com/gogf/gf/text/gstr"
|
|
|
@@ -36,7 +36,7 @@ type CustomerService struct {
|
|
|
var isPublic, noPublic = "10", "20" // 公海,非公海
|
|
|
var isTransfer int8 = 1 //转移
|
|
|
var isAllocation int8 = 2 //分配 Allocation
|
|
|
-var OperaTion, AllocaTion, Receive, Merge = "20", "10", "30", "40" // 10分配20转移30领取40合并
|
|
|
+var AllocaTion, OperaTion, Receive, Merge = "10", "20", "30", "40" // 10分配20转移30领取40合并
|
|
|
|
|
|
type OpnType struct {
|
|
|
OperaTion string
|
|
|
@@ -57,27 +57,72 @@ func NewCustomerService(ctx context.Context) (svc *CustomerService, err error) {
|
|
|
return svc, nil
|
|
|
}
|
|
|
|
|
|
-//创建客户
|
|
|
-func (c *CustomerService) Create(req *model.CustomerAddSeq) (insertId int64, err error) {
|
|
|
+//GetList 客户列表列表
|
|
|
+func (s *CustomerService) GetList(req *model.CustCustomerSearchReq) (total int, customerList []*model.CustList, err error) {
|
|
|
+ Model := s.Dao.M
|
|
|
+ if req.TargetType == "" {
|
|
|
+ if !req.IsPublic {
|
|
|
+ Model = Model.Where(s.Dao.Columns.SalesId, s.CxtUser.Id).Where(s.Dao.Columns.IsPublic, noPublic)
|
|
|
+ } else {
|
|
|
+
|
|
|
+ Model = Model.Where(s.Dao.Columns.IsPublic, isPublic)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //客户名称
|
|
|
+ if req.CustName != "" {
|
|
|
+ Model = Model.Where(s.Dao.Columns.CustName+" like ?", "%"+req.CustName+"%")
|
|
|
+ }
|
|
|
+ //客户编码
|
|
|
+ if req.CustCode != "" {
|
|
|
+ Model = Model.Where(s.Dao.Columns.CustCode+" like ?", "%"+req.CustCode+"%")
|
|
|
+ }
|
|
|
+ //客户行业
|
|
|
+ if req.CustIndustry != "" {
|
|
|
+ Model = Model.Where(s.Dao.Columns.CustIndustry+" like ?", "%"+req.CustIndustry+"%")
|
|
|
+ }
|
|
|
+ //客户级别
|
|
|
+ if req.CustLevel != "" {
|
|
|
+
|
|
|
+ Model = Model.Where(s.Dao.Columns.CustLevel, req.CustLevel)
|
|
|
+ }
|
|
|
+ //
|
|
|
+ if req.FollowUpDate != "" {
|
|
|
+ Model = Model.Where(s.Dao.Columns.FollowUpDate+" like ? ", req.FollowUpDate+"%")
|
|
|
+ }
|
|
|
+ total, err = Model.Count()
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ err = Model.Page(req.GetPage()).Order("id desc").Scan(&customerList)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Error(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+//Create 创建客户
|
|
|
+func (s *CustomerService) Create(req *model.CustomerAddSeq) (insertId int64, err error) {
|
|
|
cusTomer := new(model.CustCustomer)
|
|
|
- count, err := c.Dao.Where(g.Map{"cust_name": req.CustName}).Count()
|
|
|
+ count, err := s.Dao.Where(s.Dao.Columns.CustName, req.CustName).Count()
|
|
|
if err != nil {
|
|
|
- err = myerrors.New("Sql执行异常", err)
|
|
|
+ g.Log().Error(err)
|
|
|
return
|
|
|
}
|
|
|
if count > 0 {
|
|
|
- err = myerrors.NewMsgError(nil, "该客户信息已存在,不可重复添加")
|
|
|
- return
|
|
|
+ return 0, myerrors.NewMsgError(nil, "该客户信息已存在,不可重复添加")
|
|
|
}
|
|
|
|
|
|
if err = gconv.Struct(req, cusTomer); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- service.SetCreatedInfo(cusTomer, c.GetCxtUserId(), c.GetCxtUserName())
|
|
|
+ service.SetCreatedInfo(cusTomer, s.GetCxtUserId(), s.GetCxtUserName())
|
|
|
cusTomer.CustCode = "CT" + strconv.Itoa(int(gtime.Timestamp()))
|
|
|
cusTomer.CustStatus = "10"
|
|
|
|
|
|
- roles := c.GetCxtUserRoles()
|
|
|
+ roles := s.GetCxtUserRoles()
|
|
|
isSales := false
|
|
|
for _, v := range roles {
|
|
|
if v == "Sales" { // 销售角色
|
|
|
@@ -87,48 +132,49 @@ func (c *CustomerService) Create(req *model.CustomerAddSeq) (insertId int64, err
|
|
|
// 销售角色
|
|
|
if isSales {
|
|
|
cusTomer.IsPublic = noPublic
|
|
|
- cusTomer.SalesId = c.GetCxtUserId()
|
|
|
- cusTomer.SalesName = c.GetCxtUserName()
|
|
|
- insertId, err = c.Dao.InsertAndGetId(cusTomer)
|
|
|
+ cusTomer.SalesId = s.GetCxtUserId()
|
|
|
+ cusTomer.SalesName = s.GetCxtUserName()
|
|
|
+ insertId, err = s.Dao.InsertAndGetId(cusTomer)
|
|
|
if err != nil {
|
|
|
- return
|
|
|
+ g.Log().Error(err)
|
|
|
+ return 0, err
|
|
|
}
|
|
|
- c.CreateContact(int(insertId))
|
|
|
+ s.CreateBelong(int(insertId))
|
|
|
|
|
|
} else {
|
|
|
cusTomer.IsPublic = isPublic
|
|
|
- insertId, err = c.Dao.InsertAndGetId(cusTomer)
|
|
|
+ insertId, err = s.Dao.InsertAndGetId(cusTomer)
|
|
|
if err != nil {
|
|
|
- err = gerror.New("创建失败")
|
|
|
- return
|
|
|
+ g.Log().Error(err)
|
|
|
+ return 0, err
|
|
|
}
|
|
|
}
|
|
|
- return
|
|
|
+ return insertId, err
|
|
|
}
|
|
|
-func (c *CustomerService) CreateContact(cust_id int) (err error) {
|
|
|
-
|
|
|
- belongData := new(model.CustCustomerBelong)
|
|
|
- service.SetCreatedInfo(belongData, c.GetCxtUserId(), c.GetCxtUserName())
|
|
|
- belongData.CustId = cust_id
|
|
|
- belongData.SaleName = c.GetCxtUserName()
|
|
|
- belongData.OpnType = AllocaTion
|
|
|
- belongData.OpnPeople = c.GetCxtUserName()
|
|
|
- belongData.OpnDatetime = gtime.Now()
|
|
|
- _, err = c.BelongDao.InsertAndGetId(belongData)
|
|
|
- if err != nil {
|
|
|
|
|
|
+//CreateBelong 创建客户归属信息
|
|
|
+func (s *CustomerService) CreateBelong(custId int) (insertId int64, err error) {
|
|
|
+ belong := new(model.CustCustomerBelong)
|
|
|
+ service.SetCreatedInfo(belong, s.GetCxtUserId(), s.GetCxtUserName())
|
|
|
+ belong.CustId = custId
|
|
|
+ belong.SaleName = s.GetCxtUserName()
|
|
|
+ belong.OpnType = AllocaTion
|
|
|
+ belong.OpnPeople = s.GetCxtUserName()
|
|
|
+ belong.OpnDatetime = gtime.Now()
|
|
|
+ insertId, err = s.BelongDao.InsertAndGetId(belong)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Error(err)
|
|
|
return
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//删除客户
|
|
|
-func (c *CustomerService) DeleteById(id int) error {
|
|
|
- Model := c.Dao
|
|
|
- ContactModel := c.ContactDao //联系人
|
|
|
- BelongModel := c.BelongDao
|
|
|
- //regionDetail := new(model.CustCustomer)
|
|
|
- customerCount, err := Model.Where(c.Dao.Columns.Id, id).Count()
|
|
|
+func (s *CustomerService) DeleteById(id int) error {
|
|
|
+ Model := s.Dao
|
|
|
+ ContactModel := s.ContactDao //联系人
|
|
|
+ BelongModel := s.BelongDao
|
|
|
+ customerCount, err := Model.Where(s.Dao.Columns.Id, id).Count()
|
|
|
if err != nil {
|
|
|
return myerrors.New(" 删除客户 DeleteById Sql执行异常", err)
|
|
|
}
|
|
|
@@ -138,11 +184,11 @@ func (c *CustomerService) DeleteById(id int) error {
|
|
|
}
|
|
|
|
|
|
//删除客户表
|
|
|
- _, err = Model.Where(c.Dao.Columns.Id, id).Delete()
|
|
|
+ _, err = Model.Where(s.Dao.Columns.Id, id).Delete()
|
|
|
if err == nil {
|
|
|
- _, err = ContactModel.Where(c.ContactDao.Columns.CustId, id).Delete()
|
|
|
+ _, err = ContactModel.Where(s.ContactDao.Columns.CustId, id).Delete()
|
|
|
if err == nil {
|
|
|
- _, err = BelongModel.Where(c.BelongDao.Columns.CustId, id).Delete()
|
|
|
+ _, err = BelongModel.Where(s.BelongDao.Columns.CustId, id).Delete()
|
|
|
if err != nil {
|
|
|
return myerrors.New(" 删除客户 BelongModel Sql执行异常", err)
|
|
|
|
|
|
@@ -158,35 +204,33 @@ func (c *CustomerService) DeleteById(id int) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-//修改客户
|
|
|
-func (c *CustomerService) UpdateById(req *model.UpdateCustomer) (err error) {
|
|
|
- db := c.Dao.M
|
|
|
- count, err := db.Where(c.Dao.Columns.Id, req.Id).Count()
|
|
|
+//UpdateById 修改客户
|
|
|
+func (s *CustomerService) UpdateById(req *model.UpdateCustomer) (err error) {
|
|
|
+ //判断数据是否存在
|
|
|
+ count, err := s.Dao.Where(s.Dao.Columns.Id, req.Id).Count()
|
|
|
if err != nil {
|
|
|
- err = myerrors.New(" UpdateById Sql执行异常", err)
|
|
|
return
|
|
|
}
|
|
|
if count == 0 {
|
|
|
- err = myerrors.NewMsgError(nil, "用户信息不存在")
|
|
|
return
|
|
|
}
|
|
|
- countNums, err := db.Where(c.Dao.Columns.CustName+" = ", req.CustName).Where(c.Dao.Columns.Id+" not in (?)", req.Id).Count()
|
|
|
+ //新的客户名字是否存在
|
|
|
+ num, err := s.Dao.Where(s.Dao.Columns.CustName, req.CustName).WhereNot(s.Dao.Columns.Id, req.Id).Count()
|
|
|
if err != nil {
|
|
|
err = myerrors.New(" Sql执行异常", err)
|
|
|
return
|
|
|
}
|
|
|
- if countNums > 0 {
|
|
|
- err = myerrors.NewMsgError(nil, "客户名称已存在")
|
|
|
- return
|
|
|
+ if num > 0 {
|
|
|
+ return myerrors.NewMsgError(nil, fmt.Sprintf("客户名称[%s]已存在", req.CustName))
|
|
|
}
|
|
|
|
|
|
CustomertData := new(model.CustomerAddSeq)
|
|
|
if err = gconv.Struct(req, CustomertData); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- service.SetUpdatedInfo(CustomertData, c.GetCxtUserId(), c.GetCxtUserName())
|
|
|
- _, err = db.FieldsEx(c.Dao.Columns.CreatedTime, c.Dao.Columns.CreatedBy, c.Dao.Columns.CreatedName, c.Dao.Columns.Id, c.Dao.Columns.CustCode, c.Dao.Columns.SalesName, c.Dao.Columns.SalesId).
|
|
|
- WherePri(c.Dao.Columns.Id, req.Id).Update(CustomertData)
|
|
|
+ service.SetUpdatedInfo(CustomertData, s.GetCxtUserId(), s.GetCxtUserName())
|
|
|
+ _, err = s.Dao.FieldsEx(s.Dao.Columns.CreatedTime, s.Dao.Columns.CreatedBy, s.Dao.Columns.CreatedName, s.Dao.Columns.Id, s.Dao.Columns.CustCode, s.Dao.Columns.SalesName, s.Dao.Columns.SalesId).
|
|
|
+ WherePri(s.Dao.Columns.Id, req.Id).Update(CustomertData)
|
|
|
if err != nil {
|
|
|
err = myerrors.New("修改用户信息失败", err)
|
|
|
return
|
|
|
@@ -194,134 +238,69 @@ func (c *CustomerService) UpdateById(req *model.UpdateCustomer) (err error) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//客户列表列表
|
|
|
-func (c *CustomerService) GetList(req *model.CustCustomerSearchReq) (total int, customerList []*model.CustList, err error) {
|
|
|
- Model := c.Dao.M
|
|
|
- if req.TargetType == "" {
|
|
|
- if !req.IsPublic {
|
|
|
-
|
|
|
- Model = Model.Where(c.Dao.Columns.SalesId, c.CxtUser.Id).Where(c.Dao.Columns.IsPublic, noPublic)
|
|
|
- } else {
|
|
|
-
|
|
|
- Model = Model.Where(c.Dao.Columns.IsPublic, isPublic)
|
|
|
- }
|
|
|
- }
|
|
|
- //客户名称
|
|
|
- if req.CustName != "" {
|
|
|
- Model = Model.Where(c.Dao.Columns.CustName+" like ?", "%"+req.CustName+"%")
|
|
|
- }
|
|
|
- //客户编码
|
|
|
- if req.CustCode != "" {
|
|
|
- Model = Model.Where(c.Dao.Columns.CustCode+" like ?", "%"+req.CustCode+"%")
|
|
|
- }
|
|
|
- //客户行业
|
|
|
- if req.CustIndustry != "" {
|
|
|
- Model = Model.Where(c.Dao.Columns.CustIndustry+" like ?", "%"+req.CustIndustry+"%")
|
|
|
- }
|
|
|
- //客户级别
|
|
|
- if req.CustLevel != "" {
|
|
|
-
|
|
|
- Model = Model.Where(c.Dao.Columns.CustLevel, req.CustLevel)
|
|
|
- }
|
|
|
- //
|
|
|
- if req.FollowUpDate != "" {
|
|
|
- Model = Model.Where(c.Dao.Columns.FollowUpDate+" like ? ", req.FollowUpDate+"%")
|
|
|
- }
|
|
|
- total, err = Model.Count()
|
|
|
- if err != nil {
|
|
|
- err = myerrors.New("获取总行数失败", err)
|
|
|
-
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- err = Model.Page(req.GetPage()).Order("id desc").Scan(&customerList)
|
|
|
- if err != nil {
|
|
|
- err = myerrors.New("获取列表失败", err)
|
|
|
- return
|
|
|
- }
|
|
|
- for _, v := range customerList {
|
|
|
- times := gconv.String(v.CreatedTime)
|
|
|
- v.FollowUpDate = gstr.SubStr(v.FollowUpDate, 0, 16)
|
|
|
- v.CreatedTime = gstr.SubStr(times, 0, 16)
|
|
|
- }
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
-//移入公海
|
|
|
-func (c *CustomerService) MoveToPubic(ids []int64) (err error) {
|
|
|
- Model := c.Dao
|
|
|
- columnsCount, err := Model.Fields(c.Dao.Columns.CreatedTime).Where(c.Dao.Columns.Id+" in (?) ", ids).Count()
|
|
|
+//MoveToPubic 移入公海
|
|
|
+func (s *CustomerService) MoveToPubic(ids []int64) (err error) {
|
|
|
+ count, err := s.Dao.WhereIn(s.Dao.Columns.Id, ids).Count()
|
|
|
if err != nil {
|
|
|
-
|
|
|
- err = myerrors.New("MoveToPubic Sql 执行异常", err)
|
|
|
+ g.Log().Error(err)
|
|
|
return
|
|
|
}
|
|
|
- if columnsCount == 0 {
|
|
|
+ if count == 0 {
|
|
|
err = myerrors.NewMsgError(nil, "没有要移除的数据")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- _, err = Model.Data(g.Map{
|
|
|
+ _, err = s.Dao.Data(g.Map{
|
|
|
"is_public": isPublic,
|
|
|
"sales_id": 0,
|
|
|
"sales_name": "",
|
|
|
"dept_id": 0,
|
|
|
"dept_name": "",
|
|
|
"create_time": gtime.Now(),
|
|
|
- "updated_by": c.GetCxtUserId(),
|
|
|
- "updated_name": c.GetCxtUserName(),
|
|
|
+ "updated_by": s.GetCxtUserId(),
|
|
|
+ "updated_name": s.GetCxtUserName(),
|
|
|
"updated_time": gtime.Now(),
|
|
|
- }).Where(c.ContactDao.Columns.Id+" in (?)", ids).Update()
|
|
|
+ }).WhereIn(s.ContactDao.Columns.Id, ids).Update()
|
|
|
|
|
|
if err != nil {
|
|
|
- err = myerrors.New("MoveToPubic updateSql 执行异常", err)
|
|
|
+ g.Log().Error(err)
|
|
|
return
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-//分配客户
|
|
|
-func (c *CustomerService) DistriCustomer(req *model.DistriCustomer) (err error) {
|
|
|
- custModel := c.Dao
|
|
|
- customerData, err := custModel.Where(cust.CustCustomer.Columns.Id+" in (?) ", req.Ids).All()
|
|
|
+//AssignCustomer 分配客户
|
|
|
+func (s *CustomerService) AssignCustomer(req *model.AssignCustomerReq) (err error) {
|
|
|
+ data, err := s.Dao.Where("id in (?)", req.Ids).LockShared().All()
|
|
|
if err != nil {
|
|
|
- err = myerrors.New("DistriCustomer Sql执行异常", err)
|
|
|
+ g.Log().Error(err)
|
|
|
return
|
|
|
}
|
|
|
- if len(customerData) == 0 {
|
|
|
- err = myerrors.NewMsgError(nil, "无可分配客户")
|
|
|
- return
|
|
|
- }
|
|
|
- var custMap = make(map[int]string)
|
|
|
- for _, v := range customerData {
|
|
|
- custMap[v.Id] = v.SalesName
|
|
|
+ if len(data) == 0 {
|
|
|
+ return myerrors.NewMsgError(nil, "无可分配客户")
|
|
|
}
|
|
|
- for _, v := range req.Ids {
|
|
|
- g.Log().Info("req.Ids", v)
|
|
|
- if custMap[gconv.Int(v)] != "" {
|
|
|
- err = myerrors.NewMsgError(nil, "ID"+gconv.String(v)+"已被认领")
|
|
|
- return
|
|
|
+
|
|
|
+ for _, v := range data {
|
|
|
+ if v.SalesId != 0 {
|
|
|
+ return myerrors.NewMsgError(nil, fmt.Sprintf("客户名称[%s]已被领取或分配", v.CustName))
|
|
|
}
|
|
|
}
|
|
|
- c.updateCustomer(req.Ids, req.SalesId, req.SalesName)
|
|
|
- //if err != nil {
|
|
|
- // err = myerrors.New("DistriCustomer update Sql执行异常", err)
|
|
|
- // return
|
|
|
- //
|
|
|
- //}
|
|
|
+
|
|
|
+ s.ChangeCustBelong(req.Ids, req.SalesId, req.SalesName)
|
|
|
+
|
|
|
if req.Receive != "" {
|
|
|
req.Receive = Receive
|
|
|
} else {
|
|
|
req.Receive = AllocaTion
|
|
|
}
|
|
|
|
|
|
- c.Createbelong(customerData, req)
|
|
|
+ s.BatchCreatebelong(data, req)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//客户详情
|
|
|
-func (c *CustomerService) GetEntityById(ids []int64) (entityInfo []*model.CustList, err error) {
|
|
|
- Model := c.Dao //
|
|
|
+//GetEntityById 客户详情
|
|
|
+func (s *CustomerService) GetEntityById(ids []int64) (entityInfo []*model.CustList, err error) {
|
|
|
+ Model := s.Dao //
|
|
|
err = Model.Where(cust.CustCustomer.Columns.Id+" in (?)", ids).Scan(&entityInfo)
|
|
|
if err != nil {
|
|
|
return nil, myerrors.New("获取用户数据失败", err)
|
|
|
@@ -335,33 +314,31 @@ func (c *CustomerService) GetEntityById(ids []int64) (entityInfo []*model.CustLi
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//获取客户名称
|
|
|
-func (c *CustomerService) GetCustNameIsExist(name string) (exist bool, err error) {
|
|
|
- Model := c.Dao //
|
|
|
- count, err := Model.Where(cust.CustCustomer.Columns.CustName, name).Count()
|
|
|
+//GetCustNameIsExist 获取客户名称
|
|
|
+func (s *CustomerService) GetCustNameIsExist(name string) (exist bool, err error) {
|
|
|
+ count, err := s.Dao.Where(cust.CustCustomer.Columns.CustName, name).Count()
|
|
|
if err != nil {
|
|
|
- err = myerrors.New("获取用户名称是否存在 GetCustName Sql执行异常", err)
|
|
|
+ g.Log().Error(err)
|
|
|
return
|
|
|
}
|
|
|
exist = false
|
|
|
if count > 0 {
|
|
|
exist = true
|
|
|
}
|
|
|
-
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//客户摘要
|
|
|
-func (c *CustomerService) CustAbstract(id int64) (followInfo *model.Follow, err error) {
|
|
|
- custModel := c.Dao //
|
|
|
- Model := c.FollowDao
|
|
|
- count, err := Model.Where(c.FollowDao.Columns.CustId, id).Count()
|
|
|
+//CustAbstract 客户摘要
|
|
|
+func (s *CustomerService) CustAbstract(id int64) (followInfo *model.Follow, err error) {
|
|
|
+ custModel := s.Dao //
|
|
|
+ Model := s.FollowDao
|
|
|
+ count, err := Model.Where(s.FollowDao.Columns.CustId, id).Count()
|
|
|
if err != nil {
|
|
|
return nil, myerrors.New(" CustAbstract Sql执行异常", err)
|
|
|
}
|
|
|
followInfo = new(model.Follow)
|
|
|
followInfo.FollowCount = count
|
|
|
- followTime, err := custModel.Fields(c.Dao.Columns.FollowUpDate, c.Dao.Columns.CreatedTime).FindOne(id)
|
|
|
+ followTime, err := custModel.Fields(s.Dao.Columns.FollowUpDate, s.Dao.Columns.CreatedTime).FindOne(id)
|
|
|
if err != nil {
|
|
|
err = myerrors.New("CustAbstract Sql执行错误", err)
|
|
|
return
|
|
|
@@ -383,12 +360,11 @@ func (c *CustomerService) CustAbstract(id int64) (followInfo *model.Follow, err
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//转移客户
|
|
|
-func (c *CustomerService) UpdateBytransfer(req *model.DistriCustomer) (err error) {
|
|
|
- custModel := c.Dao
|
|
|
- data, err := custModel.Fields("sales_id,sales_name,id").Where(cust.CustCustomer.Columns.Id+" in (?)", req.Ids).All()
|
|
|
+//TransCustomer 转移客户
|
|
|
+func (s *CustomerService) TransCustomer(req *model.AssignCustomerReq) (err error) {
|
|
|
+ data, err := s.Dao.Fields("sales_id,sales_name,id").Where("id in (?)", req.Ids).All()
|
|
|
if err != nil {
|
|
|
- err = myerrors.New("Sql执行异常", err)
|
|
|
+ g.Log().Error(err)
|
|
|
return err
|
|
|
}
|
|
|
|
|
|
@@ -396,64 +372,55 @@ func (c *CustomerService) UpdateBytransfer(req *model.DistriCustomer) (err error
|
|
|
err = myerrors.New("该数据不存在", err)
|
|
|
return
|
|
|
}
|
|
|
- err = c.updateCustomer(req.Ids, req.SalesId, req.SalesName)
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
- err = c.Createbelong(data, req)
|
|
|
- if err != nil {
|
|
|
- return
|
|
|
- }
|
|
|
+ s.ChangeCustBelong(req.Ids, req.SalesId, req.SalesName)
|
|
|
+ s.BatchCreatebelong(data, req)
|
|
|
+
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//变更客户所属关系
|
|
|
-func (c *CustomerService) updateCustomer(ids []int64, salesId int64, salesName string) error {
|
|
|
- custModel := c.Dao.M
|
|
|
-
|
|
|
- _, err := custModel.Data(g.Map{
|
|
|
+//ChangeCustBelong 变更客户所属关系
|
|
|
+func (s *CustomerService) ChangeCustBelong(ids []int64, salesId int64, salesName string) error {
|
|
|
+ _, err := s.Dao.Data(g.Map{
|
|
|
"sales_id": salesId,
|
|
|
"is_public": noPublic,
|
|
|
"sales_name": salesName,
|
|
|
- "updated_by": c.GetCxtUserId(),
|
|
|
- "updated_name": c.GetCxtUserName(),
|
|
|
+ "updated_by": s.GetCxtUserId(),
|
|
|
+ "updated_name": s.GetCxtUserName(),
|
|
|
"updated_time": gtime.Now(),
|
|
|
- }).Where(cust.CustCustomer.Columns.Id+" in (?)", ids).Update()
|
|
|
+ }).Where("id in (?)", ids).Update()
|
|
|
|
|
|
if err != nil {
|
|
|
- err = myerrors.New("updateCustomer 变更Sql执行失败", err)
|
|
|
+ g.Log().Error(err)
|
|
|
return err
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-//客户操作日志
|
|
|
-func (c *CustomerService) OperationLog(ctx context.Context, ids []int64, req *model.AddCustomerDynameicsReq) (err error) {
|
|
|
+//OperationLog 客户操作日志
|
|
|
+func (s *CustomerService) OperationLog(ctx context.Context, ids []int64, req *model.AddCustomerDynameicsReq) (err error) {
|
|
|
cusDynameics := new(model.CustCustomerDynamics)
|
|
|
if err = gconv.Struct(req, cusDynameics); err != nil {
|
|
|
err = myerrors.NewMsgError(nil, "操作日志验证结构体失败")
|
|
|
return
|
|
|
}
|
|
|
- Model := c.DynamicsDao.M
|
|
|
- g.Log().Info("IDS", ids)
|
|
|
|
|
|
maps := []map[string]interface{}{}
|
|
|
for _, v := range ids {
|
|
|
contact := map[string]interface{}{}
|
|
|
contact["cust_id"] = v
|
|
|
- contact["opn_people_id"] = c.GetCxtUserId()
|
|
|
- contact["opn_people"] = c.GetCxtUserName()
|
|
|
+ contact["opn_people_id"] = s.GetCxtUserId()
|
|
|
+ contact["opn_people"] = s.GetCxtUserName()
|
|
|
contact["opn_date"] = req.OpnDate
|
|
|
contact["opn_type"] = req.OpnType
|
|
|
contact["remark"] = ""
|
|
|
- contact["created_by"] = c.GetCxtUserId()
|
|
|
- contact["created_name"] = c.GetCxtUserName()
|
|
|
- contact["created_by"] = c.GetCxtUserId()
|
|
|
+ contact["created_by"] = s.GetCxtUserId()
|
|
|
+ contact["created_name"] = s.GetCxtUserName()
|
|
|
+ contact["created_by"] = s.GetCxtUserId()
|
|
|
contact["created_time"] = gtime.Now()
|
|
|
contact["opn_content"] = req.OpnContent
|
|
|
maps = append(maps, contact)
|
|
|
}
|
|
|
- lastId, err := Model.InsertAndGetId(maps)
|
|
|
+ lastId, err := s.DynamicsDao.InsertAndGetId(maps)
|
|
|
if err != nil {
|
|
|
err = myerrors.New("OperationLog Sql执行失败", err)
|
|
|
return
|
|
|
@@ -465,19 +432,16 @@ func (c *CustomerService) OperationLog(ctx context.Context, ids []int64, req *mo
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//客户动态
|
|
|
-func (c *CustomerService) DynamicsList(req *model.CustomerDynameicsReq) (total int, result []interface{}, err error) {
|
|
|
- Model := c.DynamicsDao
|
|
|
- total, err = Model.Where("cust_id = ", req.CustId).Count()
|
|
|
+//GetDynamicsList 客户动态
|
|
|
+func (s *CustomerService) GetDynamicsList(req *model.CustomerDynameicsReq) (total int, result []interface{}, err error) {
|
|
|
+ total, err = s.DynamicsDao.Where("cust_id = ", req.CustId).Count()
|
|
|
if err != nil {
|
|
|
- err = myerrors.New("DynamicsList Sql执行异常", err)
|
|
|
+ g.Log().Error(err)
|
|
|
return
|
|
|
}
|
|
|
- //if req.PageNum == 0 {
|
|
|
- // req.PageNum = 1
|
|
|
- //}
|
|
|
dynamics := []*model.CustomerDynameicsRep{}
|
|
|
- err = Model.Where("cust_id = ", req.CustId).Order("created_time desc").Scan(&dynamics)
|
|
|
+
|
|
|
+ err = s.DynamicsDao.Where("CustId = ?", req.CustId).Order("created_time desc").Scan(&dynamics)
|
|
|
dynamicsList := make(map[string][]*model.CustomerDynameicsRep)
|
|
|
|
|
|
for _, v := range dynamics {
|
|
|
@@ -495,25 +459,24 @@ func (c *CustomerService) DynamicsList(req *model.CustomerDynameicsReq) (total i
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//合并客户
|
|
|
-func (c *CustomerService) Mergecustomer(req *model.MergecustomerRep) (err error) {
|
|
|
- Model := c.Dao.M
|
|
|
- //ContactModel := c.ContactDao.M
|
|
|
- BelongDao := c.BelongDao
|
|
|
+//MergeCustomer 合并客户
|
|
|
+func (s *CustomerService) MergeCustomer(req *model.MergecustomerRep) (err error) {
|
|
|
+ Model := s.Dao.M
|
|
|
+ //ContactModel := s.ContactDao.M
|
|
|
+ BelongDao := s.BelongDao
|
|
|
//当前目标客户是否存在
|
|
|
- customerCount, err := Model.Where(c.Dao.Columns.Id, req.Id).Count()
|
|
|
+ customerCount, err := s.Dao.Where(s.Dao.Columns.Id, req.Id).Count()
|
|
|
if err != nil {
|
|
|
- err = myerrors.New("Mergecustomer 合并客户 Sql执行异常", err)
|
|
|
+ g.Log().Error(err)
|
|
|
return
|
|
|
}
|
|
|
if customerCount == 0 {
|
|
|
- err = myerrors.NewMsgError(nil, "该客户不存在")
|
|
|
- return
|
|
|
+ return myerrors.NewMsgError(nil, "该客户不存在")
|
|
|
}
|
|
|
- ContactModel := c.ContactDao
|
|
|
- _, err = ContactModel.Where(c.ContactDao.Columns.CustId+" in (?)", req.ChooseId).WhereOr(c.ContactDao.Columns.CustId, req.Id).Delete()
|
|
|
+ ContactModel := s.ContactDao
|
|
|
+ _, err = ContactModel.Where(s.ContactDao.Columns.CustId+" in (?)", req.ChooseId).WhereOr(s.ContactDao.Columns.CustId, req.Id).Delete()
|
|
|
if err != nil {
|
|
|
- err = myerrors.New("Mergecustomer 合并联系人 Sql执行异常", err)
|
|
|
+ err = myerrors.New("MergeCustomer 合并联系人 Sql执行异常", err)
|
|
|
return
|
|
|
|
|
|
}
|
|
|
@@ -521,32 +484,32 @@ func (c *CustomerService) Mergecustomer(req *model.MergecustomerRep) (err error)
|
|
|
if err = gconv.Struct(req, CustomertData); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- service.SetUpdatedInfo(CustomertData, c.GetCxtUserId(), c.GetCxtUserName())
|
|
|
- _, err = Model.FieldsEx(c.Dao.Columns.CreatedTime, c.Dao.Columns.CreatedBy,
|
|
|
- c.Dao.Columns.CreatedName, c.Dao.Columns.Id,
|
|
|
- c.Dao.Columns.CustCode).WherePri(c.Dao.Columns.Id, req.Id).Update(CustomertData)
|
|
|
+ service.SetUpdatedInfo(CustomertData, s.GetCxtUserId(), s.GetCxtUserName())
|
|
|
+ _, err = Model.FieldsEx(s.Dao.Columns.CreatedTime, s.Dao.Columns.CreatedBy,
|
|
|
+ s.Dao.Columns.CreatedName, s.Dao.Columns.Id,
|
|
|
+ s.Dao.Columns.CustCode).WherePri(s.Dao.Columns.Id, req.Id).Update(CustomertData)
|
|
|
if err != nil {
|
|
|
- err = myerrors.New("Mergecustomer 合并客户 update Sql执行异常", err)
|
|
|
+ err = myerrors.New("MergeCustomer 合并客户 update Sql执行异常", err)
|
|
|
return
|
|
|
}
|
|
|
//删除被合并的客户信息
|
|
|
- _, err = Model.Where(c.Dao.Columns.Id+" in (?)", req.ChooseId).Delete()
|
|
|
+ _, err = Model.Where(s.Dao.Columns.Id+" in (?)", req.ChooseId).Delete()
|
|
|
if err != nil {
|
|
|
- err = myerrors.New("Mergecustomer 删除被合并的客户信息 Sql执行异常", err)
|
|
|
+ err = myerrors.New("MergeCustomer 删除被合并的客户信息 Sql执行异常", err)
|
|
|
return
|
|
|
}
|
|
|
//删除 所选客户销售联系人
|
|
|
- _, err = BelongDao.Where(c.BelongDao.Columns.CustId+" in (?)", req.ChooseId).WhereOr(c.BelongDao.Columns.CustId, req.Id).Delete()
|
|
|
+ _, err = BelongDao.Where(s.BelongDao.Columns.CustId+" in (?)", req.ChooseId).WhereOr(s.BelongDao.Columns.CustId, req.Id).Delete()
|
|
|
if err != nil {
|
|
|
- err = myerrors.New("Mergecustomer 删除所选客户销售联系人 Sql执行异常", err)
|
|
|
+ err = myerrors.New("MergeCustomer 删除所选客户销售联系人 Sql执行异常", err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//插入一条合并成功的归属记录
|
|
|
- belongService := c.BelongServer //
|
|
|
+ belongService := s.BelongServer //
|
|
|
req.AddCustomerBelong.CustId = req.Id
|
|
|
req.AddCustomerBelong.OpnType = Merge
|
|
|
- req.AddCustomerBelong.OpnPeople = c.GetCxtUserName()
|
|
|
+ req.AddCustomerBelong.OpnPeople = s.GetCxtUserName()
|
|
|
req.AddCustomerBelong.OpnDatetime = gtime.Now()
|
|
|
req.AddCustomerBelong.SaleName = req.SalesName
|
|
|
belongService.Create(req.AddCustomerBelong)
|
|
|
@@ -554,14 +517,14 @@ func (c *CustomerService) Mergecustomer(req *model.MergecustomerRep) (err error)
|
|
|
}
|
|
|
|
|
|
//联系人(合并)
|
|
|
-func (c *CustomerService) Createcontact(id int, Ids []int64, req *model.CustCustomerContactSeq) (err error) {
|
|
|
- ContactModel := c.ContactDao
|
|
|
- _, err = ContactModel.Where(c.ContactDao.Columns.CustId+" in (?)", Ids).WhereOr(c.ContactDao.Columns.CustId, id).Delete()
|
|
|
+func (s *CustomerService) Createcontact(id int, Ids []int64, req *model.CustCustomerContactSeq) (err error) {
|
|
|
+ ContactModel := s.ContactDao
|
|
|
+ _, err = ContactModel.Where(s.ContactDao.Columns.CustId+" in (?)", Ids).WhereOr(s.ContactDao.Columns.CustId, id).Delete()
|
|
|
if err != nil {
|
|
|
err = myerrors.New("Createcontact 合并删除联系人 Sql执行异常", err)
|
|
|
return
|
|
|
}
|
|
|
- contactService := c.ContanctServer //new(CustomercontactService)
|
|
|
+ contactService := s.ContanctServer //new(CustomercontactService)
|
|
|
err = contactService.Create(req)
|
|
|
if err != nil {
|
|
|
err = myerrors.New("Createcontact 合并删除并创建联系人 Sql执行异常", err)
|
|
|
@@ -570,12 +533,10 @@ func (c *CustomerService) Createcontact(id int, Ids []int64, req *model.CustCust
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//批量插入客户归属记录表//parameter map[string]string
|
|
|
-func (c *CustomerService) Createbelong(rep []*model.CustCustomer, req *model.DistriCustomer) (err error) {
|
|
|
- belongModel := c.BelongDao
|
|
|
+//BatchCreatebelong 批量插入客户归属记录表//parameter map[string]string
|
|
|
+func (s *CustomerService) BatchCreatebelong(rep []*model.CustCustomer, req *model.AssignCustomerReq) (err error) {
|
|
|
var belongData []*model.CustCustomerBelong
|
|
|
- date_time := gtime.Now()
|
|
|
- userName := c.GetCxtUserName()
|
|
|
+ userName := s.GetCxtUserName()
|
|
|
for _, v := range rep {
|
|
|
orig_sale_name := v.SalesName
|
|
|
belong := new(model.CustCustomerBelong) //map[string]interface{}{}
|
|
|
@@ -585,28 +546,27 @@ func (c *CustomerService) Createbelong(rep []*model.CustCustomer, req *model.Dis
|
|
|
belong.OpnType = req.Receive
|
|
|
belong.OpnPeople = userName
|
|
|
belong.CreatedName = userName
|
|
|
- belong.OpnDatetime = date_time
|
|
|
+ belong.OpnDatetime = gtime.Now()
|
|
|
belong.Remark = req.Remark
|
|
|
- belong.CreatedBy = c.GetCxtUserId()
|
|
|
+ belong.CreatedBy = s.GetCxtUserId()
|
|
|
belongData = append(belongData, belong)
|
|
|
}
|
|
|
- lastId, err := belongModel.InsertAndGetId(belongData)
|
|
|
- g.Log().Info("belong", belongData)
|
|
|
+ lastId, err := s.BelongDao.InsertAndGetId(belongData)
|
|
|
if err != nil {
|
|
|
- err = myerrors.New("Createbelong Sql执行异常", err)
|
|
|
+ g.Log().Error(err)
|
|
|
return err
|
|
|
}
|
|
|
if lastId == 0 {
|
|
|
- err = myerrors.New("Createbelong 插入", err)
|
|
|
+ err = myerrors.New("BatchCreatebelong 插入", err)
|
|
|
return err
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
//导出数据
|
|
|
-func (c *CustomerService) Export(req *model.CustCustomerExport) (content *model.CustExport, err error) {
|
|
|
+func (s *CustomerService) Export(req *model.CustCustomerExport) (content *model.CustExport, err error) {
|
|
|
var con model.CustExport
|
|
|
- total, data, err := c.GetList(&req.CustCustomerSearchReq)
|
|
|
+ total, data, err := s.GetList(&req.CustCustomerSearchReq)
|
|
|
if err != nil {
|
|
|
return
|
|
|
}
|