|
|
@@ -24,17 +24,19 @@ const TIME_LAYOUT = "2006-01-02 15:04:05"
|
|
|
|
|
|
type CustomerService struct {
|
|
|
*service.ContextService
|
|
|
- Dao *cust.CustCustomerDao
|
|
|
- BelongDao *cust.CustCustomerBelongDao
|
|
|
- DynamicsDao *cust.CustCustomerDynamicsDao
|
|
|
- ContactDao *cust.CustCustomerContactDao
|
|
|
- FollowDao *platdao.PlatFollowupDao
|
|
|
+ Dao *cust.CustCustomerDao
|
|
|
+ BelongDao *cust.CustCustomerBelongDao
|
|
|
+ DynamicsDao *cust.CustCustomerDynamicsDao
|
|
|
+ ContactDao *cust.CustCustomerContactDao
|
|
|
+ FollowDao *platdao.PlatFollowupDao
|
|
|
+ BelongServer *CustomerbelongService
|
|
|
+ ContanctServer *CustomercontactService
|
|
|
}
|
|
|
|
|
|
-var isPublic, noPublic = "10", "20" // 公海,非公海
|
|
|
-var isTransfer int8 = 1 //转移
|
|
|
-var isAllocation int8 = 2 //分配 Allocation
|
|
|
-var OperaTion, AllocaTion, Receive = "20", "10", "30"
|
|
|
+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合并
|
|
|
|
|
|
type OpnType struct {
|
|
|
OperaTion string
|
|
|
@@ -50,6 +52,8 @@ func NewCustomerService(ctx context.Context) (svc *CustomerService, err error) {
|
|
|
svc.DynamicsDao = cust.NewCustCustomerDynamicsDao(svc.Tenant)
|
|
|
svc.ContactDao = cust.NewCustCustomerContactDao(svc.Tenant)
|
|
|
svc.FollowDao = platdao.NewPlatFollowupDao(svc.Tenant)
|
|
|
+ svc.BelongServer, _ = NewCustomerBelongService(ctx)
|
|
|
+ svc.ContanctServer, _ = NewCustomerContactService(ctx)
|
|
|
return svc, nil
|
|
|
}
|
|
|
|
|
|
@@ -70,6 +74,7 @@ func (c *CustomerService) Derive(req *model.CustCustomerExport) (content *model.
|
|
|
|
|
|
for ck, item := range data {
|
|
|
for index, v := range req.Columns {
|
|
|
+ // "CustCode":客户编码
|
|
|
if v == "客户编码" {
|
|
|
f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), item.CustCode)
|
|
|
}
|
|
|
@@ -130,75 +135,37 @@ func (c *CustomerService) Create(req *model.Customer) (insertId int64, err error
|
|
|
err = gerror.New("该客户信息已存在,不可重复添加")
|
|
|
return
|
|
|
}
|
|
|
- //OpnPeopleId int ` json:"opnPeopleId"` // 操作人ID
|
|
|
- //OpnPeople string ` json:"opnPeople"` // 操作人
|
|
|
- //OpnDate *gtime.Time ` json:"opnDate"` // 操作日期
|
|
|
- //OpnType string ` json:"opnType"` // 操作类型
|
|
|
- //OpnContent string ` json:"opnContent"` // 操作内容
|
|
|
|
|
|
- //c.OperationLog()
|
|
|
if err = gconv.Struct(req, cusTomer); err != nil {
|
|
|
return
|
|
|
}
|
|
|
- g.Log().Info("部门ID", c.CxtUser.DeptId)
|
|
|
service.SetCreatedInfo(cusTomer, c.GetCxtUserId(), c.GetCxtUserName())
|
|
|
cusTomer.CustCode = strconv.Itoa(int(gtime.Timestamp()))
|
|
|
+
|
|
|
cusTomer.IsPublic = isPublic
|
|
|
cusTomer.CustStatus = "10"
|
|
|
- //if c.CxtUser.DeptId == 2 {
|
|
|
- // cusTomer.IsPublic = noPublic // 非公海用户
|
|
|
- // cusTomer.DeptId = c.GetCxtUserDeptId() // 部门id
|
|
|
- // cusTomer.DeptId = c.GetCxtUserDeptId() // 部门名称
|
|
|
- // cusTomer.SalesId = c.GetCxtUserId() // 销售id
|
|
|
- // cusTomer.SalesName = c.GetCxtUserName() // 销售名称
|
|
|
- //}
|
|
|
res, err := Model.Insert(cusTomer)
|
|
|
if err != nil {
|
|
|
g.Log().Error(err)
|
|
|
err = gerror.New("创建失败")
|
|
|
return
|
|
|
}
|
|
|
- //
|
|
|
|
|
|
- insertId, _ = res.LastInsertId()
|
|
|
+ /**
|
|
|
+ CustomerbelongService := new(CustomerbelongService)
|
|
|
+ err = CustomerbelongService.Create(req)
|
|
|
+ */
|
|
|
|
|
|
- //销售人员创建条件成立 同步belong 表
|
|
|
- //custBelong := new(model.AddCustomerBelong)
|
|
|
- //custBelong.CustId = int(insertId)
|
|
|
- //custBelong.SaleName = "xxx"
|
|
|
- //custBelong.OpnPeople = "xxxx"
|
|
|
- //custBelong.OpnDatetime = gtime.Now()
|
|
|
- //err = c.CreateBelong()
|
|
|
+ insertId, _ = res.LastInsertId()
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//销售人员创建 直接认领客户
|
|
|
-func (c *CustomerService) CreateBelong(req *model.AddCustomerBelong) (err error) {
|
|
|
- cusTomerBelong := new(model.CustCustomerBelong)
|
|
|
- if err = gconv.Struct(req, cusTomerBelong); err != nil {
|
|
|
- g.Log().Info("error", err)
|
|
|
- return
|
|
|
- }
|
|
|
- service.SetCreatedInfo(cusTomerBelong, c.GetCxtUserId(), c.GetCxtUserName())
|
|
|
-
|
|
|
- cusTomerBelong.OpnType = isPublic
|
|
|
- cusTomerBelong.OpnDatetime = gtime.Now()
|
|
|
- _, err = c.BelongDao.Insert(cusTomerBelong)
|
|
|
- if err != nil {
|
|
|
- g.Log().Error(err)
|
|
|
- err = gerror.New("创建失败")
|
|
|
- return
|
|
|
- }
|
|
|
- return nil
|
|
|
-
|
|
|
-}
|
|
|
-
|
|
|
//客户列表列表
|
|
|
func (c *CustomerService) GetList(req *model.CustCustomerSearchReq) (total int, customerList []*model.CustList, err error) {
|
|
|
|
|
|
g.Log().Info("serverS", req)
|
|
|
Model := c.Dao.M
|
|
|
- Model = Model.Where(c.Dao.Columns.DeletedTime + " is null")
|
|
|
+ //Model = Model.Where(c.Dao.Columns.DeletedTime + " is null")
|
|
|
if req.TargetType == "" {
|
|
|
if !req.IsPublic {
|
|
|
|
|
|
@@ -238,7 +205,8 @@ func (c *CustomerService) GetList(req *model.CustCustomerSearchReq) (total int,
|
|
|
if req.PageNum == 0 {
|
|
|
req.PageNum = 1
|
|
|
}
|
|
|
- err = Model.Page(req.PageNum, req.PageSize).Order("id desc").Scan(&customerList)
|
|
|
+
|
|
|
+ err = Model.Page(req.GetPage()).Order("id desc").Scan(&customerList)
|
|
|
for _, v := range customerList {
|
|
|
times := gconv.String(v.CreatedTime)
|
|
|
v.FollowUpDate = gstr.SubStr(v.FollowUpDate, 0, 16)
|
|
|
@@ -249,9 +217,9 @@ func (c *CustomerService) GetList(req *model.CustCustomerSearchReq) (total int,
|
|
|
|
|
|
//删除客户
|
|
|
func (c *CustomerService) DeleteById(id int) error {
|
|
|
-
|
|
|
- Model := c.Dao.M
|
|
|
+ Model := c.Dao
|
|
|
ContactModel := c.ContactDao //联系人
|
|
|
+ BelongModel := c.BelongDao
|
|
|
regionDetail := new(model.CustCustomer)
|
|
|
err := Model.Where(c.Dao.Columns.Id, id).Scan(®ionDetail)
|
|
|
//g.Log().Info("DeleteByIds", one)
|
|
|
@@ -260,17 +228,22 @@ func (c *CustomerService) DeleteById(id int) error {
|
|
|
err = gerror.New("没有要删除的数据")
|
|
|
return err
|
|
|
}
|
|
|
- deleteTime := gtime.Now()
|
|
|
- //删除客户表
|
|
|
- _, err = Model.Data(g.Map{
|
|
|
- "deleted_time": deleteTime,
|
|
|
- }).Where(c.Dao.Columns.Id, id).Update()
|
|
|
- // 删除客户联系人表
|
|
|
- _, err = ContactModel.Data(g.Map{
|
|
|
- "deleted_time": deleteTime,
|
|
|
- }).Where(c.ContactDao.Columns.CustId, id).Update()
|
|
|
|
|
|
- if err != nil {
|
|
|
+ //删除客户表
|
|
|
+ _, err = Model.Where(c.Dao.Columns.Id, id).Delete()
|
|
|
+ if err == nil {
|
|
|
+ _, err = ContactModel.Where(c.ContactDao.Columns.CustId, id).Delete()
|
|
|
+ if err == nil {
|
|
|
+ _, err = BelongModel.Where(c.BelongDao.Columns.CustId, id).Delete()
|
|
|
+ if err != nil {
|
|
|
+ err = gerror.New("删除数据失败")
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ err = gerror.New("删除数据失败")
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ } else {
|
|
|
g.Log().Error(err)
|
|
|
err = gerror.New("删除数据失败")
|
|
|
return err
|
|
|
@@ -361,9 +334,6 @@ func (c *CustomerService) DistriCustomer(req *model.DistriCustomer) error {
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
-
|
|
|
- //belongServer := new(CustomerbelongService)
|
|
|
-
|
|
|
if req.Receive != "" {
|
|
|
req.Receive = Receive
|
|
|
} else {
|
|
|
@@ -565,23 +535,19 @@ func (c *CustomerService) DynamicsList(req *model.CustomerDynameicsReq) (total i
|
|
|
//合并客户
|
|
|
func (c *CustomerService) Mergecustomer(req *model.MergecustomerRep) (err error) {
|
|
|
Model := c.Dao.M
|
|
|
- ContactModel := c.ContactDao.M
|
|
|
- BelongDao := c.BelongDao.M
|
|
|
+ //ContactModel := c.ContactDao.M
|
|
|
+ BelongDao := c.BelongDao
|
|
|
//当前目标客户是否存在
|
|
|
FindOne, err := Model.Where(c.Dao.Columns.DeletedTime+" is null").Where(c.Dao.Columns.Id, req.Id).FindOne()
|
|
|
if err != nil || FindOne.IsEmpty() {
|
|
|
err = gerror.New("该数据不存在")
|
|
|
return
|
|
|
}
|
|
|
- //所选客户联系人信息
|
|
|
- List, err := ContactModel.Where(c.ContactDao.Columns.CustId+" in (?)", req.ChooseId).All()
|
|
|
- g.Log().Info("list", List.List())
|
|
|
- if err != nil || List.Len() > 0 {
|
|
|
- err = c.Createcontact(req.Id, req.ChooseId, List.List())
|
|
|
- if err != nil {
|
|
|
- err = gerror.New("合并失败")
|
|
|
- return
|
|
|
- }
|
|
|
+ ContactModel := c.ContactDao
|
|
|
+ _, err = ContactModel.Where(c.ContactDao.Columns.CustId+" in (?)", req.ChooseId).WhereOr(c.ContactDao.Columns.CustId, req.Id).Delete()
|
|
|
+ if err != nil {
|
|
|
+ err = gerror.New("合并联系人失败")
|
|
|
+ return
|
|
|
}
|
|
|
CustomertData := new(model.Customer)
|
|
|
if err = gconv.Struct(req, CustomertData); err != nil {
|
|
|
@@ -590,75 +556,47 @@ func (c *CustomerService) Mergecustomer(req *model.MergecustomerRep) (err error)
|
|
|
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,
|
|
|
- c.Dao.Columns.SalesName,
|
|
|
- c.Dao.Columns.SalesId).WherePri(c.Dao.Columns.Id, req.Id).Update(CustomertData)
|
|
|
+ c.Dao.Columns.CustCode).WherePri(c.Dao.Columns.Id, req.Id).Update(CustomertData)
|
|
|
+ if err != nil {
|
|
|
+ err = gerror.New("合并失败")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //删除被合并的客户信息
|
|
|
+ Model.Where(c.Dao.Columns.Id+" in (?)", req.ChooseId).Delete()
|
|
|
+ //删除 所选客户销售联系人
|
|
|
+ _, err = BelongDao.Where(c.BelongDao.Columns.CustId+" in (?)", req.ChooseId).WhereOr(c.BelongDao.Columns.CustId, req.Id).Delete()
|
|
|
if err != nil {
|
|
|
err = gerror.New("合并失败")
|
|
|
return
|
|
|
}
|
|
|
- Model.Data(g.Map{"deleted_time": gtime.Now()}).Where(c.ContactDao.Columns.Id+" in (?)", req.ChooseId).Update()
|
|
|
|
|
|
- ////所选客户销售联系人信息
|
|
|
- BelongList, err := BelongDao.Where(c.BelongDao.Columns.CustId+" in (?)", req.ChooseId).All()
|
|
|
- if err != nil || BelongList.Len() > 0 {
|
|
|
- err = c.belongInster(req.Id, req.ChooseId, req.Customer.SalesName)
|
|
|
- if err != nil {
|
|
|
- err = gerror.New("合并失败")
|
|
|
- return
|
|
|
- }
|
|
|
+ //插入一条合并成功的归属记录
|
|
|
+ belongService := c.BelongServer //
|
|
|
+ req.AddCustomerBelong.CustId = req.Id
|
|
|
+ req.AddCustomerBelong.OpnType = Merge
|
|
|
+ req.AddCustomerBelong.OpnPeople = c.GetCxtUserName()
|
|
|
+ req.AddCustomerBelong.OpnDatetime = gtime.Now()
|
|
|
+ req.AddCustomerBelong.SaleName = req.SalesName
|
|
|
+ err = belongService.Create(req.AddCustomerBelong)
|
|
|
+ if err != nil {
|
|
|
+ err = gerror.New("合并失败")
|
|
|
+ return
|
|
|
}
|
|
|
return
|
|
|
}
|
|
|
|
|
|
-//联系人
|
|
|
-func (c *CustomerService) Createcontact(id int, Ids []int64, list []map[string]interface{}) (err error) {
|
|
|
+//联系人(合并)
|
|
|
+func (c *CustomerService) Createcontact(id int, Ids []int64, req *model.CustCustomerContactSeq) (err error) {
|
|
|
ContactModel := c.ContactDao.M
|
|
|
- ContactModel.Data(g.Map{"deleted_time": gtime.Now()}).Where(c.ContactDao.Columns.CustId+" in (?)", Ids).Update()
|
|
|
- maps := []map[string]interface{}{}
|
|
|
- for _, v := range list {
|
|
|
- contact := map[string]interface{}{}
|
|
|
- contact["cust_id"] = id
|
|
|
- contact["cuct_name"] = v["cuct_name"]
|
|
|
- contact["cuct_gender"] = v["cuct_gender"]
|
|
|
- contact["postion"] = v["postion"]
|
|
|
- contact["telephone"] = v["telephone"]
|
|
|
- contact["wechat"] = v["wechat"]
|
|
|
- contact["email"] = v["email"]
|
|
|
- contact["policy"] = v["policy"]
|
|
|
- contact["remark"] = v["remark"]
|
|
|
- contact["created_by"] = v["created_by"]
|
|
|
- contact["created_name"] = v["created_name"]
|
|
|
- contact["created_time"] = v["created_time"]
|
|
|
- maps = append(maps, contact)
|
|
|
- }
|
|
|
- _, err = ContactModel.Insert(maps)
|
|
|
+ _, err = ContactModel.Where(c.ContactDao.Columns.CustId+" in (?)", Ids).WhereOr(c.ContactDao.Columns.CustId, id).Delete()
|
|
|
if err != nil {
|
|
|
- err = gerror.New("操作失败")
|
|
|
- g.Log().Info("Createcontact", err)
|
|
|
+ err = gerror.New("合并联系人失败")
|
|
|
return
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
- return
|
|
|
-}
|
|
|
-
|
|
|
-//销售
|
|
|
-func (c *CustomerService) belongInster(Id int, BelongIds []int64, SaleName string) (err error) {
|
|
|
- BelongDao := c.BelongDao.M
|
|
|
- BelongDao.Data(g.Map{"deleted_time": gtime.Now()}).Where(c.BelongDao.Columns.CustId+" in (?)", BelongIds).Update()
|
|
|
- BelongData := new(model.CustCustomerBelong)
|
|
|
- BelongData.CustId = Id
|
|
|
- service.SetCreatedInfo(BelongData, c.GetCxtUserId(), c.GetCxtUserName())
|
|
|
- BelongData.SaleName = SaleName
|
|
|
- BelongData.OpnType = AllocaTion
|
|
|
- BelongData.OrigSaleName = ""
|
|
|
- BelongData.OpnPeople = "admin"
|
|
|
- BelongData.OpnDatetime = gtime.Now()
|
|
|
- _, err = BelongDao.Insert(BelongData)
|
|
|
+ contactService := c.ContanctServer //new(CustomercontactService)
|
|
|
+ err = contactService.Create(req)
|
|
|
if err != nil {
|
|
|
- g.Log().Info("BeloongInster", err)
|
|
|
- err = gerror.New("操作失败")
|
|
|
+ err = gerror.New("合并联系人失败")
|
|
|
return
|
|
|
}
|
|
|
return
|