| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- package cust
- import (
- "context"
- "strconv"
- "github.com/gogf/gf/database/gdb"
- "github.com/gogf/gf/errors/gerror"
- "github.com/gogf/gf/frame/g"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gconv"
- "dashoo.cn/micro/app/dao/cust"
- model "dashoo.cn/micro/app/model/cust"
- "dashoo.cn/micro/app/service"
- )
- type customerService struct {
- *service.ContextService
- Dao *cust.CustCustomerDao
- BelongDao *cust.CustCustomerBelongDao
- }
- func NewCustomerService(ctx context.Context) (svc *customerService, err error) {
- svc = new(customerService)
- if svc.ContextService, err = svc.Init(ctx); err != nil {
- return nil, err
- }
- svc.Dao = cust.NewCustCustomerDao(svc.Tenant)
- svc.BelongDao = cust.NewCustCustomerBelongDao(svc.Tenant)
- return svc, nil
- }
- //创建客户
- func (c *customerService) Create(req *model.AddCustCustomerReq) (err error) {
- cusTomer := new(model.CustCustomer)
- conTact := new(model.CustCustomerContact)
- if err = gconv.Struct(req.Cust, cusTomer); err != nil {
- g.Log().Info("error", err)
- return
- }
- if err = gconv.Struct(req.Info, conTact); err != nil {
- return
- }
- g.Log().Info(err)
- Model := c.Dao.M
- record, err := Model.As("c").LeftJoin("cust_customer_contact ct", "c.id=ct.cust_id").Fields("c.*").Where(
- g.Map{
- "ct.telephone": req.Info.TelePhone,
- "ct.cuct_name": req.Info.CuctName,
- "c.cust_name": req.Cust.CustName,
- },
- ).One()
- g.Log().Info("recordE", record.IsEmpty())
- if err != nil || !record.IsEmpty() {
- err = gerror.New("该客户信息已存在,不可重复添加")
- return err
- }
- cusTomer.CustCode = strconv.Itoa(int(gtime.Timestamp()))
- cusTomer.IsPublic = "10"
- service.SetCreatedInfo(cusTomer, c.GetCxtUserId(), c.GetCxtUserName())
- var tx *gdb.TX
- tx, err = g.DB().Begin()
- if err != nil {
- err = gerror.New("事务开启失败")
- return
- }
- cus_res, err := Model.Insert(cusTomer)
- if err != nil {
- g.Log().Error(err)
- err = gerror.New("cusTomer表插入失败")
- tx.Rollback()
- return
- }
- InsertId, _ := cus_res.LastInsertId()
- conTact.CustId = int(InsertId)
- service.SetCreatedInfo(conTact, c.GetCxtUserId(), c.GetCxtUserName())
- _, err = cust.CustCustomerContact.Insert(conTact)
- if err != nil {
- g.Log().Error(err)
- err = gerror.New("conTact表插入失败")
- tx.Rollback()
- return
- }
- /**
- 预留逻辑判断当前登录角色是否是销售人员
- if 销售人员 {
- cusTomer.SalesId = 当前销售人员id
- cusTomer.SalesName = 当前销售人员姓名
- c.BelongDao.M.Insert()
- }
- */
- tx.Commit()
- return
- }
- //客户列表列表
- func (c *customerService) GetList(req *model.CustCustomerSearchReq) (total int, customerList []*model.CustList, err error) {
- Model := c.Dao.M.As("c").LeftJoin("cust_customer_contact ct", "c.id=ct.cust_id").Where("c.is_public =?", 20) //非公海客户
- //客户名称
- if req.CustName != "" {
- Model = Model.Where("c.cust_name ", req.CustName)
- }
- //联系人
- if req.CuctName != "" {
- Model = Model.Where("ct.cuct_name", req.CuctName)
- }
- // 联系人姓名
- if req.CustStatus != "" {
- Model = Model.Where("ct.cuct_name", req.CuctName)
- }
- //所属销售
- if req.SalesName != "" {
- Model = Model.Where("c.sales_name ?", req.SalesName)
- }
- //联系人电话
- if req.Telephone != "" {
- Model = Model.Where("ct.telephone ?", req.Telephone)
- }
- total, err = Model.Fields("c.id").Count()
- if err != nil {
- g.Log().Error(err)
- err = gerror.New("获取总行数失败")
- return
- }
- if req.PageNum == 0 {
- req.PageNum = 1
- }
- fields := "c.*,ct.cuct_name,ct.telephone"
- err = Model.Fields(fields).Page(req.PageNum, req.PageSize).Order("c.id asc").Scan(&customerList)
-
- return
- }
- //分配客户
- func (c *customerService) DistriCustomer(req *model.DistriCustomer) error {
- /**
- 待写逻辑(销售总监或销售助理将公海客户分配给指定销售工程师)
- if c.user.id != 销售总监 || c.user.id!=销售助理 {
- err = gerror.New("该账号无权限操作!")
- return
- }
- */
- custModel := c.Dao.M
- rep, err := custModel.Where(cust.CustCustomer.Columns.Id+" in (?) ", req.Ids).Where(cust.CustCustomer.Columns.IsPublic, 10).All()
- if err != nil || rep.IsEmpty() {
- err = gerror.New("该数据不存在")
- return err
- }
- err = c.UpdateCustomer(req.Ids, req.SalesId, 2)
- if err != nil {
- err = gerror.New("可配客户失败")
- return err
- }
- return nil
- }
- //客户详情
- func (c *customerService) GetEntityById(id int64) (entityInfo []*model.CustCustomer, err error) {
- Model := c.Dao.M
- err = Model.Where(cust.CustCustomer.Columns.Id, id).WithAll().Scan(&entityInfo)
- if err != nil {
- g.Log().Error(err)
- return nil, gerror.New("获取用户数据失败")
- }
- return
- }
- //转移客户
- func (c *customerService) UpdateBytransfer(req *model.CustSalesReq) (entityInfo []*model.CustCustomer, err error) {
- custModel := c.Dao.M
- rep, err := custModel.Fields("sales_id,sales_name,id").Where(cust.CustCustomer.Columns.Id+" in (?)", req.Ids).All()
- if err != nil || rep.IsEmpty() {
- err = gerror.New("该数据不存在")
- return
- }
- err = c.UpdateCustomer(req.Ids, req.SalesIds, 1)
- if err != nil {
- err = gerror.New("转移客户失败")
- return
- }
- belongModel := c.BelongDao.M
- maps := []map[string]interface{}{}
- date_time := gtime.Now()
- for _, v := range rep {
- old_id := v.GMap().Get("id")
- orig_sale_name := v.GMap().Get("sales_name")
- g.Log().Info("orig_sale_name", orig_sale_name)
- belong := map[string]interface{}{}
- belong["cust_id"] = old_id.(int)
- belong["sale_name"] = ""
- belong["orig_sale_name"] = orig_sale_name
- belong["opn_type"] = "20"
- belong["opn_people"] = 1
- belong["opn_datetime"] = date_time
- belong["created_by"] = 1
- belong["remark"] = req.Remark
- belong["created_name"] = "admin"
- belong["created_time"] = date_time
- belong["opn_datetime"] = date_time
- maps = append(maps, belong)
- }
- _, err = belongModel.Insert(maps)
- if err != nil {
- err = gerror.New("转移客户失败")
- return
- }
- return
- }
- //变更客户所属关系
- func (c *customerService) UpdateCustomer(ids []int64, salesId int64, behavior int8) error {
- custModel := c.Dao.M
- custCustomerData := new(model.CustCustomer)
- custCustomerData.SalesId = int(salesId)
- switch behavior {
- case 1:
- custCustomerData.SalesName = "转移销售"
- custCustomerData.IsPublic = "10"
- case 2:
- custCustomerData.SalesName = "分配销售"
- custCustomerData.IsPublic = "20"
- }
- service.SetUpdatedInfo(custCustomerData, c.GetCxtUserId(), c.GetCxtUserName())
- _, err := custModel.FieldsEx(
- cust.CustCustomer.Columns.CustCode,
- cust.CustCustomer.Columns.CustName,
- cust.CustCustomer.Columns.AbbrName,
- cust.CustCustomer.Columns.CustLocation,
- cust.CustCustomer.Columns.Id,
- cust.CustCustomer.Columns.CustAddress,
- cust.CustCustomer.Columns.CreatedTime,
- cust.CustCustomer.Columns.CustStatus,
- cust.CustCustomer.Columns.CreatedBy,
- cust.CustCustomer.Columns.CreatedName).
- WherePri(cust.CustCustomer.Columns.Id+" in (?)", ids).Update(custCustomerData)
- if err != nil {
- g.Log().Error(err)
- err = gerror.New("变更失败")
- return err
- }
- return nil
- }
|