package cust import ( "context" "strconv" "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.Customer) (insertId int64, err error) { cusTomer := new(model.CustCustomer) custBelong := new(model.CustCustomerBelong) if err = gconv.Struct(req, cusTomer); err != nil { g.Log().Info("error", err) return } g.Log().Info(err) Model := c.Dao.M record, err := Model.Where( g.Map{ "cust_name": req.CustName, }, ).One() g.Log().Info("recordE", record.IsEmpty()) if err != nil || !record.IsEmpty() { err = gerror.New("该客户信息已存在,不可重复添加") return } service.SetCreatedInfo(cusTomer, c.GetCxtUserId(), c.GetCxtUserName()) cusTomer.CustCode = strconv.Itoa(int(gtime.Timestamp())) cusTomer.IsPublic = "20" 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("cusTomer表插入失败") } insertId, _ = res.LastInsertId() if c.CxtUser.Id == 1 { // service.SetCreatedInfo(custBelong, c.GetCxtUserId(), c.GetCxtUserName()) custBelong.CustId = int(insertId) custBelong.OpnType = "10" custBelong.OpnDatetime = gtime.Now() service.SetCreatedInfo(custBelong, c.GetCxtUserId(), c.GetCxtUserName()) _, err = c.BelongDao.Insert(custBelong) if err != nil { g.Log().Error(err) err = gerror.New("创建失败") return } } return } //客户列表列表 func (c *customerService) GetList(req *model.CustCustomerSearchReq) (total int, customerList []*model.CustList, err error) { Model := c.Dao.M //非公海客户 Model.Where("is_public", 20) //客户名称 if req.CustName != "" { Model = Model.Where("cust_name ", req.CustName) } //客户编码 if req.CustCode != "" { Model = Model.Where("cust_name ", req.CustCode) } //客户行业 //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().Count() if err != nil { g.Log().Error(err) err = gerror.New("获取总行数失败") return } if req.PageNum == 0 { req.PageNum = 1 } fields := "id,cust_code,cust_name,abbr_name,'' as indus_try,'' as level,cust_location,cust_status,follow_up_date,created_name,created_time" err = Model.Fields(fields).Page(req.PageNum, req.PageSize).Order("id desc").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 }